From e61878561b2b86e67351eecd57a61947c7a05a9d Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 03:06:17 -0800 Subject: [PATCH 1/3] BDSP AutoStory rebuild: BF3 (program shell, actions, events, segment-00 plans) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/CMakeLists.txt | 14 + .../Options/PokemonBDSP_PlayerModelOption.cpp | 10 +- .../Options/PokemonBDSP_PlayerModelOption.h | 3 + .../Options/PokemonBDSP_RngFilter.cpp | 220 ++++ .../Options/PokemonBDSP_RngFilter.h | 106 ++ .../Source/PokemonBDSP/PokemonBDSP_Panels.cpp | 38 +- .../Source/PokemonBDSP/PokemonBDSP_Tests.cpp | 20 + .../Actions/PokemonBDSP_AutoStory_Actions.cpp | 356 ++++++ .../Actions/PokemonBDSP_AutoStory_Actions.h | 64 + .../PokemonBDSP_AutoStory_Actions_Tests.cpp | 930 ++++++++++++++ .../PokemonBDSP_AutoStory_Actions_Tests.h | 25 + .../Actions/PokemonBDSP_AutoStory_Bag.cpp | 440 +++++++ .../Actions/PokemonBDSP_AutoStory_Bag.h | 80 ++ .../PokemonBDSP_AutoStory_BagItemReader.cpp | 122 ++ .../PokemonBDSP_AutoStory_BagItemReader.h | 36 + .../Actions/PokemonBDSP_AutoStory_BagScreen.h | 60 + .../Actions/PokemonBDSP_AutoStory_Battle.cpp | 401 ++++++ .../Actions/PokemonBDSP_AutoStory_Battle.h | 63 + .../PokemonBDSP_AutoStory_BattleMoves.cpp | 372 ++++++ .../PokemonBDSP_AutoStory_BattleMoves.h | 59 + .../Actions/PokemonBDSP_AutoStory_Center.cpp | 507 ++++++++ .../Actions/PokemonBDSP_AutoStory_Center.h | 94 ++ .../Actions/PokemonBDSP_AutoStory_Events.cpp | 39 + .../Actions/PokemonBDSP_AutoStory_Events.h | 102 ++ .../PokemonBDSP_AutoStory_Events_Jubilife.cpp | 377 ++++++ .../PokemonBDSP_AutoStory_Events_Lake.cpp | 158 +++ .../PokemonBDSP_AutoStory_Events_Oreburgh.cpp | 146 +++ .../PokemonBDSP_AutoStory_Events_Route204.cpp | 85 ++ .../PokemonBDSP_AutoStory_Events_Sandgem.cpp | 259 ++++ .../PokemonBDSP_AutoStory_IntroProbe.h | 47 + .../PokemonBDSP_AutoStory_IntroSteps.h | 111 ++ .../PokemonBDSP_AutoStory_LitDoorway.h | 84 ++ .../Actions/PokemonBDSP_AutoStory_NewGame.cpp | 606 ++++++++++ .../Actions/PokemonBDSP_AutoStory_NewGame.h | 95 ++ .../PokemonBDSP_AutoStory_Settings.cpp | 506 ++++++++ .../Actions/PokemonBDSP_AutoStory_Settings.h | 49 + .../PokemonBDSP_AutoStory_TalkOver.cpp | 62 + .../Actions/PokemonBDSP_AutoStory_TalkOver.h | 89 ++ .../Actions/PokemonBDSP_AutoStory_Talks.cpp | 300 +++++ .../Actions/PokemonBDSP_AutoStory_Talks.h | 106 ++ .../PokemonBDSP_AutoStory_WanderWait.cpp | 176 +++ .../PokemonBDSP_AutoStory_WanderWait.h | 105 ++ .../PokemonBDSP_AutoStory_WildBattle.cpp | 503 ++++++++ .../PokemonBDSP_AutoStory_WildBattle.h | 59 + .../AutoStory/PokemonBDSP_AutoStory.cpp | 355 ++++++ .../AutoStory/PokemonBDSP_AutoStory.h | 77 ++ .../PokemonBDSP_AutoStory_PlanGolden.h | 220 ++++ ...kemonBDSP_AutoStory_PlannedRoute_Tests.cpp | 1002 +++++++++++++++ ...PokemonBDSP_AutoStory_PlannedRoute_Tests.h | 28 + ...PokemonBDSP_AutoStory_RouteBench_Tests.cpp | 647 ++++++++++ .../PokemonBDSP_AutoStory_RouteBench_Tests.h | 25 + ...monBDSP_AutoStory_SimulatedRoute_Tests.cpp | 480 ++++++++ ...kemonBDSP_AutoStory_SimulatedRoute_Tests.h | 28 + .../PokemonBDSP_AutoStory_StarterRng.cpp | 174 +++ .../PokemonBDSP_AutoStory_StarterRng.h | 99 ++ .../PokemonBDSP_AutoStory_StoryActions.cpp | 93 ++ .../PokemonBDSP_AutoStory_StoryActions.h | 54 + .../PokemonBDSP_AutoStory_StoryData.cpp | 256 ++++ .../PokemonBDSP_AutoStory_StoryData.h | 47 + .../PokemonBDSP_AutoStory_StoryData_Tests.cpp | 232 ++++ .../PokemonBDSP_AutoStory_StoryData_Tests.h | 22 + .../PokemonBDSP_AutoStory_StoryExecutor.cpp | 387 ++++++ .../PokemonBDSP_AutoStory_StoryExecutor.h | 242 ++++ .../PokemonBDSP_AutoStory_StoryInterrupts.cpp | 78 ++ .../PokemonBDSP_AutoStory_StoryInterrupts.h | 80 ++ .../PokemonBDSP_AutoStory_StoryMaps_Tests.cpp | 373 ++++++ .../PokemonBDSP_AutoStory_StoryMaps_Tests.h | 24 + .../PokemonBDSP_AutoStory_StoryPlaces.cpp | 313 +++++ .../PokemonBDSP_AutoStory_StoryPlaces.h | 60 + .../PokemonBDSP_AutoStory_StoryStepIO.cpp | 323 +++++ .../PokemonBDSP_AutoStory_StoryStepIO.h | 126 ++ .../PokemonBDSP_AutoStory_StoryTable.cpp | 102 ++ .../PokemonBDSP_AutoStory_StoryTable.h | 83 ++ ...PokemonBDSP_AutoStory_StoryTable_Tests.cpp | 1072 +++++++++++++++++ .../PokemonBDSP_AutoStory_StoryTable_Tests.h | 25 + .../PokemonBDSP_AutoStory_StoryWalkSteps.cpp | 783 ++++++++++++ .../PokemonBDSP_AutoStory_StoryWalkSteps.h | 158 +++ ...PokemonBDSP_AutoStory_StoryWorld_Tests.cpp | 364 ++++++ .../PokemonBDSP_AutoStory_StoryWorld_Tests.h | 24 + .../PokemonBDSP_AutoStory_TableRoutes.cpp | 117 ++ .../PokemonBDSP_AutoStory_TableRoutes.h | 64 + .../PokemonBDSP_AutoStory_Walk_Tests.cpp | 599 +++++++++ .../PokemonBDSP_AutoStory_Walk_Tests.h | 27 + .../AutoStory/PokemonBDSP_AutoStory_Walks.cpp | 248 ++++ .../AutoStory/PokemonBDSP_AutoStory_Walks.h | 82 ++ .../Programs/AutoStory/StoryData/README.md | 16 + .../AutoStory/StoryData/bdsp-00.00.plan.json | 40 + .../AutoStory/StoryData/bdsp-00.01.plan.json | 523 ++++++++ .../AutoStory/StoryData/bdsp-00.02.plan.json | 92 ++ .../AutoStory/StoryData/bdsp-00.03.plan.json | 135 +++ .../AutoStory/StoryData/bdsp-00.04.plan.json | 568 +++++++++ .../AutoStory/StoryData/bdsp-00.04b.plan.json | 303 +++++ .../AutoStory/StoryData/bdsp-00.04c.plan.json | 57 + .../AutoStory/StoryData/bdsp-00.04d.plan.json | 196 +++ .../AutoStory/StoryData/bdsp-00.05.plan.json | 425 +++++++ .../AutoStory/StoryData/bdsp-00.05b.plan.json | 308 +++++ .../AutoStory/StoryData/bdsp-00.05c.plan.json | 149 +++ .../RngManipulation/PokemonBDSP_RngAim.cpp | 77 ++ .../RngManipulation/PokemonBDSP_RngAim.h | 52 + .../PokemonBDSP_RngCalibration.cpp | 188 +++ .../PokemonBDSP_RngCalibration.h | 79 ++ .../PokemonBDSP_RngExecution.cpp | 130 ++ .../PokemonBDSP_RngExecution.h | 59 + .../PokemonBDSP_RngTargets.cpp | 155 +++ .../RngManipulation/PokemonBDSP_RngTargets.h | 52 + .../PokemonBDSP_RngTimeline.cpp | 140 +++ .../RngManipulation/PokemonBDSP_RngTimeline.h | 57 + .../PokemonBDSP_StarterNavigation.cpp | 255 ++++ .../PokemonBDSP_StarterNavigation.h | 102 ++ .../PokemonBDSP_StarterRng.cpp | 207 ++++ .../RngManipulation/PokemonBDSP_StarterRng.h | 82 ++ .../PokemonBDSP_StarterRngAttempts.cpp | 767 ++++++++++++ .../PokemonBDSP_StarterRngAttempts.h | 216 ++++ .../PokemonBDSP_StarterRng_Tests.cpp | 362 ++++++ .../PokemonBDSP_StarterRng_Tests.h | 23 + .../PokemonBDSP_SummaryNavigation.cpp | 185 +++ .../PokemonBDSP_SummaryNavigation.h | 44 + .../PokemonBDSP_TargetSelection.cpp | 122 ++ .../PokemonBDSP_TargetSelection.h | 57 + SerialPrograms/cmake/SourceFiles.cmake | 102 ++ 120 files changed, 23989 insertions(+), 13 deletions(-) create mode 100644 SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/README.md create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.00.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.01.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.02.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.03.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04b.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04c.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04d.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05b.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.h diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index f10c408eba..0ce81e4014 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -244,6 +244,20 @@ pa_pokesim_map_directory(BDSP_MAP_DIRECTORY bdsp "${POKESIM_CONSUMER}" "${POKESI 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) +# TODO: Move JSON syntax preflight into pa_embed_text_files so map and story +# resources share it. Keep game/schema/semantic checks in their native tests. +# Catch JSON syntax errors during a lab build, before static table initialization. +# Native tests still validate the schema, game policy and execution semantics. +file(GLOB BDSP_STORY_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/Source/PokemonBDSP/Programs/AutoStory/StoryData/*.json") +foreach(BDSP_STORY_FILE IN LISTS BDSP_STORY_FILES) + file(READ "${BDSP_STORY_FILE}" BDSP_STORY_TEXT) + string(JSON BDSP_STORY_TYPE ERROR_VARIABLE BDSP_STORY_ERROR TYPE "${BDSP_STORY_TEXT}") + if(BDSP_STORY_ERROR OR NOT BDSP_STORY_TYPE STREQUAL "OBJECT") + message(FATAL_ERROR "Invalid authored checkpoint ${BDSP_STORY_FILE}: ${BDSP_STORY_ERROR}") + endif() +endforeach() +pa_embed_text_files(SerialProgramsLib Source/PokemonBDSP/Programs/AutoStory/StoryData + PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h PokemonAutomation::NintendoSwitch::PokemonBDSP bdsp_checkpoint_json) # Create binary target diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp index 4b81977e67..6d4490bdac 100644 --- a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -42,9 +43,14 @@ const PlayerModelIcons& PLAYER_MODEL_ICONS(){ PlayerModelOption::PlayerModelOption() - : StringSelectOption( + : PlayerModelOption( "Character model:
The appearance chosen for the player character. " - "Each one is watched through a different eye template.", + "Each one is watched through a different eye template." + ) +{} +PlayerModelOption::PlayerModelOption(std::string label) + : StringSelectOption( + std::move(label), PLAYER_MODEL_ICONS().database, LockMode::LOCK_WHILE_RUNNING, "model-1" diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h index e54cd04850..9f3f358206 100644 --- a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h @@ -8,6 +8,7 @@ #define PokemonAutomation_PokemonBDSP_PlayerModelOption_H #include +#include #include "CommonTools/Options/StringSelectOption.h" namespace PokemonAutomation{ @@ -22,6 +23,8 @@ const uint8_t BDSP_PLAYER_MODEL_COUNT = 8; class PlayerModelOption : public StringSelectOption{ public: PlayerModelOption(); + // The same choices, under another label. + explicit PlayerModelOption(std::string label); uint8_t model_number() const{ return (uint8_t)(index() + 1); } }; diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp new file mode 100644 index 0000000000..4db0232e56 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp @@ -0,0 +1,220 @@ +/* BDSP RNG Filter + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Logging/Logger.h" +#include "PokemonBDSP_RngFilter.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +const uint8_t BDSP_HEIGHT_MIN = 0; +const uint8_t BDSP_HEIGHT_MAX = 255; + + +BdspRngFilterRow::BdspRngFilterRow(EditableTableOption& parent_table) + : EditableTableRow(parent_table) + , misc(static_cast(parent_table).feature_flags) + , height( + LockMode::UNLOCK_WHILE_RUNNING, + BDSP_HEIGHT_MIN, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MIN, BDSP_HEIGHT_MIN, + BDSP_HEIGHT_MIN, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MAX + ) + , iv_hp(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_atk(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_def(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_spatk(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_spdef(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_speed(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) +{ + if (misc.feature_flags.action){ + PA_ADD_OPTION(misc.action); + } + if (misc.feature_flags.shiny){ + PA_ADD_OPTION(misc.shiny); + } + if (misc.feature_flags.gender){ + PA_ADD_OPTION(misc.gender); + } + if (misc.feature_flags.nature){ + PA_ADD_OPTION(misc.nature); + } + PA_ADD_OPTION(height); + PA_ADD_OPTION(iv_hp); + PA_ADD_OPTION(iv_atk); + PA_ADD_OPTION(iv_def); + PA_ADD_OPTION(iv_spatk); + PA_ADD_OPTION(iv_spdef); + PA_ADD_OPTION(iv_speed); +} +std::unique_ptr BdspRngFilterRow::clone() const{ + std::unique_ptr ret(new BdspRngFilterRow(parent())); + ret->misc.set(misc); + ret->height.set(height); + ret->iv_hp.set(iv_hp); + ret->iv_atk.set(iv_atk); + ret->iv_def.set(iv_def); + ret->iv_spatk.set(iv_spatk); + ret->iv_spdef.set(iv_spdef); + ret->iv_speed.set(iv_speed); + return ret; +} +bool BdspRngFilterRow::match_iv( + const IntegerRangeCell& desired, const IvRange& actual +){ + uint8_t lo, hi; + desired.current_values(lo, hi); + if (lo == 0 && hi == 31){ + return true; + } + if (actual.high < (int8_t)lo){ + return false; + } + if ((int8_t)hi < actual.low){ + return false; + } + return true; +} +bool BdspRngFilterRow::matches( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + if (!misc.matches(shiny, gender, nature)){ + return false; + } + + uint8_t height_lo, height_hi; + this->height.current_values(height_lo, height_hi); + if (height < height_lo || height_hi < height){ + return false; + } + + if (!match_iv(iv_hp, ivs.hp)){ + return false; + } + if (!match_iv(iv_atk, ivs.attack)){ + return false; + } + if (!match_iv(iv_def, ivs.defense)){ + return false; + } + if (!match_iv(iv_spatk, ivs.spatk)){ + return false; + } + if (!match_iv(iv_spdef, ivs.spdef)){ + return false; + } + if (!match_iv(iv_speed, ivs.speed)){ + return false; + } + + return true; +} + + +BdspRngFilterTable::BdspRngFilterTable( + const std::string& label, + const StatsHuntMiscFeatureFlags& p_feature_flags +) + : EditableTableOption_t(label, LockMode::UNLOCK_WHILE_RUNNING) + , feature_flags(p_feature_flags) +{} +std::vector BdspRngFilterTable::make_header() const{ + std::vector ret; + if (feature_flags.action){ + ret.emplace_back("Action"); + } + if (feature_flags.shiny){ + ret.emplace_back("Shininess"); + } + if (feature_flags.gender){ + ret.emplace_back("Gender"); + } + if (feature_flags.nature){ + ret.emplace_back("Nature"); + } + + ret.emplace_back("Height"); + + ret.emplace_back("HP"); + ret.emplace_back("Atk"); + ret.emplace_back("Def"); + ret.emplace_back("SpAtk"); + ret.emplace_back("SpDef"); + ret.emplace_back("Spd"); + + return ret; +} +BdspRngFilterSnapshot BdspRngFilterTable::make_snapshot() const{ + return BdspRngFilterSnapshot(copy_snapshot()); +} +void set_default_shiny_row(BdspRngFilterTable& table){ + std::vector> defaults; + auto row = std::make_unique(table); + row->misc.shiny.set(StatsHuntShinyFilter::Shiny); + defaults.emplace_back(std::move(row)); + table.set_default(std::move(defaults)); + table.restore_defaults(); +} +StatsHuntAction BdspRngFilterTable::get_action( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + return make_snapshot().get_action(shiny, gender, nature, height, ivs); +} + + +BdspRngFilterSnapshot::BdspRngFilterSnapshot(std::vector> rows) + : m_rows(std::move(rows)) +{} +StatsHuntAction BdspRngFilterSnapshot::get_action( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + StatsHuntAction action = StatsHuntAction::Discard; + for (size_t c = 0; c < m_rows.size(); c++){ + const BdspRngFilterRow& filter = *m_rows[c]; + + if (!filter.matches(shiny, gender, nature, height, ivs)){ + continue; + } + + StatsHuntAction filter_action = filter.misc.action; + + // No action matched so far. Take the current action and continue. + if (action == StatsHuntAction::Discard){ + action = filter_action; + continue; + } + + // Conflicting actions. + if (action != filter_action){ + global_logger_tagged().log( + "Multiple filters matched with conflicting actions. Stopping program...", + COLOR_RED + ); + return StatsHuntAction::StopProgram; + } + } + return action; +} + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h new file mode 100644 index 0000000000..652bc73753 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h @@ -0,0 +1,106 @@ +/* BDSP RNG Filter + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngFilter_H +#define PokemonAutomation_PokemonBDSP_RngFilter_H + +#include +#include +#include +#include "Common/Cpp/Options/EditableTableOption.h" +#include "Common/Cpp/Options/IntegerRangeOption.h" +#include "Pokemon/Pokemon_NatureChecker.h" +#include "Pokemon/Pokemon_StatsCalculation.h" +#include "Pokemon/Options/Pokemon_StatsHuntFilter.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +class BdspRngFilterTable; +class BdspRngFilterRow : public EditableTableRow{ +public: + BdspRngFilterRow(EditableTableOption& parent_table); + virtual std::unique_ptr clone() const override; + + bool matches( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +private: + static bool match_iv(const IntegerRangeCell& desired, const Pokemon::IvRange& actual); + +public: + Pokemon::StatsHuntRowMisc misc; + + IntegerRangeCell height; + + IntegerRangeCell iv_hp; + IntegerRangeCell iv_atk; + IntegerRangeCell iv_def; + IntegerRangeCell iv_spatk; + IntegerRangeCell iv_spdef; + IntegerRangeCell iv_speed; +}; + + +// The table copied once. Calling get_action() on the table itself clones every row on +// every call, which is far too slow for a scan running over millions of advances. +class BdspRngFilterSnapshot{ +public: + explicit BdspRngFilterSnapshot(std::vector> rows); + + Pokemon::StatsHuntAction get_action( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +private: + std::vector> m_rows; +}; + + +class BdspRngFilterTable : public EditableTableOption_t{ +public: + BdspRngFilterTable( + const std::string& label, + const Pokemon::StatsHuntMiscFeatureFlags& p_feature_flags + ); + virtual std::vector make_header() const override; + + BdspRngFilterSnapshot make_snapshot() const; + + Pokemon::StatsHuntAction get_action( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +public: + const Pokemon::StatsHuntMiscFeatureFlags feature_flags; +}; + + +// Makes the table's default, and its current content, one row: a shiny of +// any nature, gender and IVs. An empty table matches nothing, so a program +// that stops on a match must seed its table with this. +void set_default_shiny_row(BdspRngFilterTable& table); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp index e2aa07801f..a9f0675086 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp @@ -7,6 +7,8 @@ #include "CommonFramework/StaticGlobals.h" #include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h" + #include "PokemonBDSP_Panels.h" #include "PokemonBDSP_Settings.h" @@ -39,6 +41,7 @@ #include "Programs/RngManipulation/PokemonBDSP_BedroomSeedFinder.h" #include "Programs/RngManipulation/PokemonBDSP_IntroSeedFinder.h" +#include "Programs/RngManipulation/PokemonBDSP_StarterRng.h" #include "Programs/TestPrograms/PokemonBDSP_ShinyEncounterTester.h" #include "Programs/TestPrograms/PokemonBDSP_SoundListener.h" @@ -57,6 +60,26 @@ PanelListFactory::PanelListFactory() ) {} +namespace{ + +// The programs shown only in beta builds or developer mode. +void add_beta_and_developer_panels(std::vector& ret){ + if (IS_BETA_VERSION || STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Untested/Beta/WIP ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Developer Tools ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } +} + +} // namespace + std::vector PanelListFactory::make_panels() const{ std::vector ret; @@ -91,6 +114,9 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back("---- Auto Story ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back("---- Glitches (v1.1.3) ----"); ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); @@ -98,17 +124,7 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back("---- Glitches (v1.1.2) ----"); ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION || STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Untested/Beta/WIP ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Developer Tools ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } + add_beta_and_developer_panels(ret); return ret; } diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 98bb4c4a6a..b9eacc3ad3 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -7,8 +7,18 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Inference/PokemonBDSP_DialogDetector.h" #include "Inference/PokemonBDSP_MenuDetector.h" +#include "Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h" #include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" +#include "Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -20,8 +30,18 @@ namespace PokemonBDSP{ void add_tests(UnitTestDatabase& database){ add_tests_DialogDetector(database); add_tests_MenuDetector(database); + add_tests_AutoStory_Actions(database); add_tests_AutoStory_Maps(database); + add_tests_AutoStory_StoryWorld(database); + add_tests_AutoStory_RouteBench(database); + add_tests_AutoStory_StoryMaps(database); + add_tests_AutoStory_StoryData(database); + add_tests_AutoStory_StoryTable(database); + add_tests_AutoStory_Walk(database); add_tests_AutoStory_WildBattlePolicy(database); + add_tests_AutoStory_PlannedRoute(database); + add_tests_AutoStory_SimulatedRoute(database); + add_tests_StarterRng(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp new file mode 100644 index 0000000000..884ebfbc35 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp @@ -0,0 +1,356 @@ +/* BDSP AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Globals.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Programs/PokemonBDSP_GameNavigation.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP_AutoStory_Actions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// X from the overworld, then waits up to 10 s for the menu. Reads it with +// the NEW tags allowed: overworld_to_menu() reads it with the tags' columns, +// which a new Pokemon in the party breaks (r19: three misses, then the blind +// save; r52: the Bag check after the Mart lady's Potions). +void open_menu_from_overworld(VideoStream& stream, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 160ms, 840ms); + context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, true); + if (wait_until(stream, context, std::chrono::seconds(10), {{menu}}) < 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Menu not detected after 10 seconds.", stream); + } +} + +// Opens the save prompt from the overworld and keeps a screenshot of it: it +// shows the play time and badges, a record of where each checkpoint save +// stands. Returns the map its card names. +std::string open_save_prompt(VideoStream& stream, ProControllerContext& context){ + open_menu_from_overworld(stream, context); + context.wait_for(100ms); + pbf_press_button(context, BUTTON_R, 80ms, 2000ms); + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (!screen){ + return ""; + } + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/saves/"; + Filesystem::create_directories(Filesystem::Path(folder)); + const std::string path = folder + now_to_filestring() + ".png"; + screen.frame->save(path); + stream.log("Save prompt screenshot: " + path); + const std::string location = read_save_screen_location(*screen.frame); + stream.log("Save screen location: \"" + location + "\"."); + return location; +} + +// Backs out of the save prompt and its menu to the overworld. +void leave_save_prompt(VideoStream& stream, ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, std::chrono::seconds(15), {overworld}) < 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect the overworld after leaving the save prompt.", stream); + } +} + +struct IconGrid { + int row, col; +}; + +IconGrid icon_to_grid(MenuCursorPosition pos, int num_icons){ + switch (pos){ + case MenuCursorPosition::POKEDEX: + return {0, 0}; + case MenuCursorPosition::POKEMON: + return {0, 1}; + case MenuCursorPosition::BAG: + return {0, 2}; + case MenuCursorPosition::CARD: + return {0, 3}; + case MenuCursorPosition::OPTIONS: + return {1, num_icons <= 5 ? 0 : 2}; + case MenuCursorPosition::MAP: + return {1, 0}; + case MenuCursorPosition::CAPSULES: + return {1, 1}; + case MenuCursorPosition::MYSTERYGIFT: + return {1, 3}; + default: + return {-1, -1}; + } +} + +enum class SaveOutcome{ SAVED, WRONG_PLACE, FAILED }; +struct SaveAttempt{ + SaveOutcome outcome = SaveOutcome::FAILED; + std::string wrong_place; +}; + +// One feedback-checked save: the save prompt, its card checked against +// `expected_location` if given, then the save and back to the overworld. +SaveAttempt save_once( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const std::string& expected_location, size_t attempt +){ + try{ + env.console.log("Checkpoint save attempt: " + std::to_string(attempt + 1)); + + // Only mash B on retries - no need to on first attempt + // if already cleanly in the overworld. + if (attempt > 0){ + pbf_mash_button(context, BUTTON_B, 1000ms); + } + + // The feedback-based save of save_game(VideoStream&, ...), split at the + // save prompt to check where it stands. + const std::string location = open_save_prompt(env.console, context); + if (!expected_location.empty() && !location_banner_matches(location, expected_location)){ + SaveAttempt wrong{SaveOutcome::WRONG_PLACE, + "About to save in \"" + location + "\", expected " + expected_location + "; not saving."}; + env.console.log(wrong.wrong_place, COLOR_RED); + leave_save_prompt(env.console, context); + return wrong; + } + pbf_press_button(context, BUTTON_ZL, 80ms, 5000ms); + + // Return to overworld cleanly. + pbf_mash_button(context, BUTTON_B, 1000ms); + + // Drain R/confirm/B before detection; an earlier overworld frame + // must not report success while saving commands are still queued. + context.wait_for_all_requests(); + + // Verify overworld. Generous timeout: save animation + fade can be slow. + OverworldWatcher overworld; + if (wait_until(env.console, context, std::chrono::seconds(15), {overworld}) < 0){ + throw OperationFailedExceptionWithScreenshot(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect overworld after save.", + env.console); + } + + env.console.log("Checkpoint save verified."); + return {SaveOutcome::SAVED, ""}; + }catch (OperationFailedException& e){ + env.console.log("Checkpoint save failed: " + e.message(), COLOR_RED); + context.wait_for(1000ms); + return {SaveOutcome::FAILED, ""}; + } +} + +// The X menu's icons as hovered, where packaged: the cursor detector's +// template match. Without them the dark-pixel detector still works. +void load_menu_icon_references(MenuCursorDetector& detector){ + static const std::string ICON_NAMES[MENU_CURSOR_NUM_ICONS] = { + "POKEDEX", + "POKEMON", + "BAG", + "CARD", + "OPTIONS", + "MAP", + "CAPSULES", + "MYSTERYGIFT", + }; + const std::string ref_dir = RESOURCE_PATH() + "PokemonBDSP/Autostory"; + for (int i = 0; i < MENU_CURSOR_NUM_ICONS; i++){ + const std::string path = ref_dir + "/" + ICON_NAMES[i] + ".png"; + if (!Filesystem::is_regular_file(path)){ + continue; + } + ImageRGB32 img(path); + if (img){ + detector.set_icon_reference(static_cast(i), std::move(img)); + } + } +} + +// The cursor on the open menu, from up to ten frames; UNKNOWN if none reads. +MenuCursorPosition read_menu_cursor(VideoStream& stream, ProControllerContext& context, const MenuCursorDetector& detector){ + MenuCursorPosition cur = MenuCursorPosition::UNKNOWN; + for (int frame = 0; frame < 10 && cur == MenuCursorPosition::UNKNOWN; frame++){ + context.wait_for(50ms); + const VideoSnapshot snapshot = stream.video().snapshot(); + if (snapshot){ + cur = detector.detect(snapshot); + } + } + return cur; +} + +// Opens the menu unless it is already open: a late-opening menu may show +// after a timeout, and X would close it. +void open_menu_unless_open(VideoStream& stream, ProControllerContext& context){ + MenuWatcher already_open(COLOR_RED, true); + if (wait_until(stream, context, 300ms, {{already_open}}) < 0){ + open_menu_from_overworld(stream, context); + }else{ + stream.log("[Open menu] Menu already visible; skipping X."); + } +} + +// Opens the menu and reads its cursor, in three attempts. Once the menu +// is confirmed open, retries only look: no input that could close it. +MenuCursorPosition open_menu_and_find_cursor(VideoStream& stream, ProControllerContext& context, const MenuCursorDetector& detector){ + MenuCursorPosition cur = MenuCursorPosition::UNKNOWN; + bool menu_confirmed_open = false; + for (size_t attempt = 1; attempt <= 3 && cur == MenuCursorPosition::UNKNOWN; attempt++){ + stream.log("[Open menu] Open and detect attempt " + std::to_string(attempt) + "/3."); + try{ + if (!menu_confirmed_open){ + open_menu_unless_open(stream, context); + menu_confirmed_open = true; + }else{ + stream.log("[Open menu] Retrying cursor detection in the open menu."); + } + pbf_wait(context, 1000ms); + context.wait_for_all_requests(); + cur = read_menu_cursor(stream, context, detector); + stream.log("[Open menu] Menu cursor " + std::string(cur == MenuCursorPosition::UNKNOWN ? "not detected" : "detected") + + " on attempt " + std::to_string(attempt) + ".", cur == MenuCursorPosition::UNKNOWN ? COLOR_ORANGE : COLOR_GREEN); + }catch (OperationFailedException& error){ + stream.log("[Open menu] Attempt " + std::to_string(attempt) + " failed: " + error.message(), COLOR_ORANGE); + } + } + if (cur == MenuCursorPosition::UNKNOWN){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Menu or cursor detection failed after 3 opening attempts.", + stream); + } + return cur; +} + +// From the icon `cur` to `target`: a row up or down, then across. +void move_menu_cursor(VideoStream& stream, ProControllerContext& context, MenuCursorPosition cur, MenuCursorPosition target, int num_icons){ + const IconGrid cur_g = icon_to_grid(cur, num_icons); + const IconGrid tgt_g = icon_to_grid(target, num_icons); + if (tgt_g.row == -1){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Unknown target menu icon.", + stream); + } + if (cur_g.row != tgt_g.row){ + pbf_press_dpad(context, cur_g.row < tgt_g.row ? DPAD_DOWN : DPAD_UP, 80ms, 300ms); + context.wait_for_all_requests(); + } + const int col_diff = tgt_g.col - cur_g.col; + for (int i = 0; i < std::abs(col_diff); i++){ + pbf_press_dpad(context, col_diff > 0 ? DPAD_RIGHT : DPAD_LEFT, 80ms, 300ms); + context.wait_for_all_requests(); + } +} + +} // namespace + +std::string read_location_from_save_screen(VideoStream& stream, ProControllerContext& context){ + const std::string location = open_save_prompt(stream, context); + leave_save_prompt(stream, context); + return location; +} + +void save_game( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const std::string& expected_location +){ + static constexpr size_t MAX_RETRIES = 3; + + for (size_t attempt = 0; attempt < MAX_RETRIES; attempt++){ + const SaveAttempt result = save_once(env, context, expected_location, attempt); + if (result.outcome == SaveOutcome::SAVED){ + return; + } + // No blind fallback for the wrong place: it would save there. + if (result.outcome == SaveOutcome::WRONG_PLACE){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, result.wrong_place, env.console); + } + } + + // No blind save: presses the screen never confirmed could land anywhere, + // even on the HOME menu (console B, 2026-11-11). Stop instead. + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("Couldn't confirm the save screen in {} attempts; not saving.", MAX_RETRIES), + env.console + ); +} + +void wait_for_dialogue(VideoStream& stream, + ProControllerContext& context, + const std::string& label, + Milliseconds timeout){ + auto log_detected = [&] { + if (!label.empty()){ + stream.log(label + " dialogue detected."); + } + }; + + ShortDialogWatcher watcher_cyan(COLOR_CYAN); + int ret = run_until(stream, + context, + [timeout](ProControllerContext& context){ + context.wait_for(timeout); + }, + {{watcher_cyan}}); + if (ret == 0){ + log_detected(); + return; + } + + ShortDialogWatcher watcher_red(COLOR_RED); + ret = run_until(stream, + context, + [timeout](ProControllerContext& context){ + context.wait_for(timeout); + }, + {{watcher_red}}); + if (ret == 0){ + log_detected(); + return; + } + + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + (label.empty() ? "wait_for_dialogue" : label) + ": dialogue not detected within timeout.", + stream); +} + +void open_menu(VideoStream& stream, ProControllerContext& context, MenuCursorPosition target, int num_icons){ + context.wait_for_all_requests(); + MenuCursorDetector detector; + load_menu_icon_references(detector); + const MenuCursorPosition cur = open_menu_and_find_cursor(stream, context, detector); + move_menu_cursor(stream, context, cur, target, num_icons); + pbf_press_button(context, BUTTON_A, 80ms, 200ms); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h new file mode 100644 index 0000000000..b7f3235901 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h @@ -0,0 +1,64 @@ +/* BDSP AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The game's menus as the story uses them: saving, opening the X menu, and + * waiting for a dialogue. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Actions_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Actions_H + +#include +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Save the game from the overworld with retries. A save the screen never +// confirmed fails with a screenshot: there is no blind save (console B, +// 2026-11-11: one pressed X/R/ZL on the HOME menu). +// With `expected_location`, the map named on the save screen's card must be +// it (compared as a location banner is), or the save is backed out of and +// the function fails with a screenshot: r124a saved in Sandgem Town after a +// walk that was sure it stood on Route 202. +void save_game( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const std::string& expected_location = "" +); + +// Opens the save screen from the overworld, reads where the game says we +// are (read_save_screen_location() in the Location module), and backs out +// without saving. +std::string read_location_from_save_screen(VideoStream& stream, ProControllerContext& context); + +// Wait (without pressing any buttons) until a short dialogue box appears. +// Logs `label + " dialogue detected."` when found (omit label to skip logging). +// Throws OperationFailedException if no dialogue appears within `timeout`. +void wait_for_dialogue( + VideoStream& stream, + ProControllerContext& context, + const std::string& label = "", + Milliseconds timeout = std::chrono::seconds(60) +); + +// Opens the X-menu from the overworld, navigates the cursor to `target`, and presses A. +// num_icons: 5 (early-game layout) or 8 (late-game layout). +void open_menu(VideoStream& stream, ProControllerContext& context, MenuCursorPosition target, int num_icons); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp new file mode 100644 index 0000000000..d4c98f01e0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -0,0 +1,930 @@ +/* BDSP AutoStory - Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The new game's intro decisions, and the screenshot checks behind the + * Pokemon Center, the save card and the Jubilife School's lamp. Screenshots + * are in the unit test resources (the CommandLineTests checkout); a test + * whose screenshot is missing is skipped. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Logging/Logger.h" +#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_IntroProbe.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" +#include "PokemonBDSP_AutoStory_LitDoorway.h" +#include "PokemonBDSP_AutoStory_NewGame.h" +#include "PokemonBDSP_AutoStory_Settings.h" +#include "PokemonBDSP_AutoStory_Actions_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace detail; + +namespace{ + + +// How far the intro has got, with BDSP confirmed: 1, the player named; +// 2, the rival too. +IntroProgress intro_progress(size_t names_given){ + IntroProgress progress; + progress.player_named = names_given >= 1; + progress.rival_named = names_given >= 2; + progress.game_confirmed = true; + return progress; +} + +UnitTestResult test_intro_decisions(Logger&){ + struct Case{ + std::string screen; + IntroProgress progress; + IntroStep expected; + }; + const IntroProgress start = intro_progress(0); + const IntroProgress named = intro_progress(1); + const IntroProgress done = intro_progress(2); + const Case cases[] = { + {"user-select", start, IntroStep::SELECT_USER}, + {"home", done, IntroStep::FAIL_NOT_IN_GAME}, + {"keyboard", start, IntroStep::ENTER_PLAYER_NAME}, + {"keyboard", named, IntroStep::CANCEL_RIVAL_KEYBOARD}, + {"rival-list-tell", named, IntroStep::MOVE_TO_PRESET}, + {"rival-list-preset", named, IntroStep::PICK_PRESET}, + {"menu", done, IntroStep::CLOSE_MENU_AND_FINISH}, + // The appearance grid has its prompt in a dialogue box. + {"dialog", start, IntroStep::ADVANCE}, + {"dialog", done, IntroStep::ADVANCE_WITH_B}, + // Logos, the title, language and the appearance preview: no X + // before the bedroom is possible. + {"other", start, IntroStep::ADVANCE}, + {"other", named, IntroStep::ADVANCE}, + {"other", done, IntroStep::CLEAR_WITH_B}, + {"black", start, IntroStep::WAIT}, + {"black", done, IntroStep::WAIT}, + }; + for (const Case& c : cases){ + const IntroStep actual = decide_intro_step(c.screen, c.progress); + if (actual != c.expected){ + return std::string("Screen \"") + c.screen + "\" (player named " + + (c.progress.player_named ? "yes" : "no") + ", rival named " + + (c.progress.rival_named ? "yes" : "no") + "): step " + + std::to_string((int)actual) + ", expected " + std::to_string((int)c.expected) + "."; + } + } + return true; +} + + +// Runs `screens` through decide_intro_step() and record_intro_step() from +// `progress`; the steps taken. +std::vector intro_steps(IntroProgress& progress, const std::vector& screens){ + std::vector steps; + for (const std::string& screen : screens){ + steps.push_back(decide_intro_step(screen, progress)); + record_intro_step(progress, steps.back()); + } + return steps; +} + +// The rival's preset name is confirmed with A on "Barry, is it?" and its +// Yes/No; B there answers No and the list comes back (r4). Only +// INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick does B take over. +UnitTestResult test_intro_rival_confirm(Logger&){ + IntroProgress progress = intro_progress(1); + // r4's frames 029 to 032: the pick, "Barry, is it?", Barry alone + // as the Yes/No comes up, the Yes/No. + const std::vector confirm = intro_steps(progress, {"rival-list-preset", "dialog", "other", "dialog"}); + const std::vector expected{IntroStep::PICK_PRESET, IntroStep::ADVANCE, IntroStep::ADVANCE, IntroStep::ADVANCE}; + if (confirm != expected){ + return "The pick and its confirmation must take A, never B."; + } + if (progress.rival_named){ + return "The name is not final until INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick."; + } + // The list back (No was answered): pick again, the count restarts. + if (intro_steps(progress, {"rival-list-tell", "rival-list-preset"}) != + std::vector{IntroStep::MOVE_TO_PRESET, IntroStep::PICK_PRESET}){ + return "The list coming back after a pick must be picked from again."; + } + for (size_t c = 0; c < INTRO_SCREENS_AFTER_RIVAL_PICK; c++){ + if (intro_steps(progress, {c % 2 ? "other" : "dialog"}) != std::vector{IntroStep::ADVANCE}){ + return "Screen " + std::to_string(c) + " after the pick must take A."; + } + } + if (!progress.rival_named){ + return "INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick, the name is final."; + } + return true; +} + + +// After the rival is named, unknown screens take INTRO_B_BEFORE_X B presses, +// then X; only the menu ends the intro (r2: Rowan's last line, misread). +UnitTestResult test_intro_before_menu(Logger&){ + IntroProgress progress = intro_progress(1); + std::vector screens{"rival-list-preset", "dialog", "other"}; + std::vector expected{IntroStep::PICK_PRESET, IntroStep::ADVANCE, IntroStep::ADVANCE}; + for (size_t c = 2; c < INTRO_SCREENS_AFTER_RIVAL_PICK; c++){ + screens.push_back("dialog"); + expected.push_back(IntroStep::ADVANCE); + } + for (size_t round = 0; round < 2; round++){ + screens.insert(screens.end(), INTRO_B_BEFORE_X + 1, "other"); + expected.insert(expected.end(), INTRO_B_BEFORE_X, IntroStep::CLEAR_WITH_B); + expected.push_back(IntroStep::PROBE_FOR_MENU); + } + // A dialogue line in between takes B without counting. + screens.insert(screens.end(), {"dialog", "other", "menu"}); + expected.insert(expected.end(), {IntroStep::ADVANCE_WITH_B, IntroStep::CLEAR_WITH_B, IntroStep::CLOSE_MENU_AND_FINISH}); + const std::vector steps = intro_steps(progress, screens); + for (size_t c = 0; c < steps.size(); c++){ + if (steps[c] != expected[c]){ + return "Screen " + std::to_string(c) + " (\"" + screens[c] + "\"): step " + + std::to_string((int)steps[c]) + ", expected " + std::to_string((int)expected[c]) + "."; + } + } + IntroProgress pressed = intro_progress(2); + pressed.b_presses = INTRO_B_BEFORE_X; + if (decide_intro_step("other", pressed) != IntroStep::PROBE_FOR_MENU){ + return "X must follow INTRO_B_BEFORE_X B presses."; + } + return true; +} + + +// X from a still bedroom opening something the detector misses, closed +// again by B, is the player in control; moving screens or X doing nothing +// are not. +UnitTestResult test_intro_in_control(Logger&){ + struct Case{ + XProbe probe; + bool expected; + std::string what; + }; + const Case cases[] = { + {{1, 60, 2}, true, "still, X opened, B closed"}, + {{12, 60, 2}, false, "moving before X (sparkles, a cutscene)"}, + {{1, 5, 2}, false, "X changed nothing"}, + {{1, 60, 40}, false, "B did not put it back"}, + }; + for (const Case& c : cases){ + if (intro_in_control_without_menu(c.probe) != c.expected){ + return std::string("Wrong for: ") + c.what + "."; + } + } + return true; +} + + +// Black from the restart until the first logo, well past the limit. +std::string check_boot(IntroProgress& progress){ + using std::chrono::milliseconds; + for (milliseconds t(0); t <= 2 * INTRO_TIME_BEFORE_BDSP; t += milliseconds(500)){ + note_intro_screen(progress, "black", t); + if (decide_intro_step("black", progress) != IntroStep::WAIT){ + return "A black screen must take no press."; + } + } + if (progress.first_shown || progress.unconfirmed_for != milliseconds(0)){ + return "Black screens must not start the wrong-game clock."; + } + return ""; +} + +UnitTestResult check_confirming_screens(){ + for (const std::string screen : {"dialog", "language-select", "rival-list-tell", "rival-list-preset", "menu"}){ + if (!intro_screen_confirms_bdsp(screen)){ + return std::string("Expected \"") + screen + "\" to confirm BDSP."; + } + } + for (const std::string screen : {"user-select", "home", "keyboard", "black", "other"}){ + if (intro_screen_confirms_bdsp(screen)){ + return std::string("Expected \"") + screen + "\" not to confirm BDSP."; + } + } + return true; +} + +// The intro gates its A presses on a BDSP screen within a time from the +// first screen that is not black: past it, it is another game (r1, +// 2026-11-01, launched FireRed). The boot's black screens, about 25 s of +// them (r3), neither take presses nor count. +UnitTestResult test_intro_gate(Logger&){ + using std::chrono::milliseconds; + IntroProgress progress; + const std::string boot = check_boot(progress); + if (!boot.empty()){ + return boot; + } + const milliseconds shown = 2 * INTRO_TIME_BEFORE_BDSP + milliseconds(500); + note_intro_screen(progress, "other", shown); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "The first logo must take A."; + } + note_intro_screen(progress, "black", shown + INTRO_TIME_BEFORE_BDSP / 2); + note_intro_screen(progress, "other", shown + INTRO_TIME_BEFORE_BDSP - milliseconds(1)); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "Unknown screens take A until INTRO_TIME_BEFORE_BDSP has passed."; + } + IntroProgress other_game = progress; + note_intro_screen(other_game, "other", shown + INTRO_TIME_BEFORE_BDSP); + if (decide_intro_step("other", other_game) != IntroStep::FAIL_NOT_BDSP){ + return "Past INTRO_TIME_BEFORE_BDSP of unknown screens, it is another game."; + } + note_intro_screen(progress, "dialog", shown + INTRO_TIME_BEFORE_BDSP - milliseconds(1)); + note_intro_screen(progress, "other", shown + 10 * INTRO_TIME_BEFORE_BDSP); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "Once BDSP's dialogue has shown, the clock stops."; + } + return check_confirming_screens(); +} + + +// The menus' data, grids, detectors and the look option agree on their sizes. +UnitTestResult check_intro_menu_sizes(){ + if (bdsp_language_menu().size() != BDSP_LANGUAGE_GRID.count || BDSP_LANGUAGE_GRID.count != LANGUAGE_SELECT_BUTTONS){ + return "The language select's data, grid and detector must agree on ten buttons."; + } + if (BDSP_LOOK_GRID.count != LOOK_SELECT_PORTRAITS || BDSP_LOOK_GRID.count != BDSP_PLAYER_MODEL_COUNT){ + return "The look grid, its detector and PlayerModelOption must agree on eight looks."; + } + return true; +} + +// The presses for the language select (two columns of five, read row by +// row, from wherever its cursor is) and the appearance grid (two rows of +// four, from the first look). +UnitTestResult test_intro_pick_moves(Logger&){ + using Moves = std::optional>; + const IntroPicks french{Language::French, 2}; + struct Case{ + std::string_view what; + Moves actual; + Moves expected; + }; + const Case cases[] = { + {"French from English, where the cursor starts (lab run 3)", + language_pick_moves(0, french), Moves(std::vector{DPAD_DOWN, DPAD_DOWN})}, + {"English from English", + language_pick_moves(0, IntroPicks{Language::English, 0}), Moves(std::vector{})}, + {"French from Traditional Chinese, bottom right", + language_pick_moves(9, french), Moves(std::vector{DPAD_UP, DPAD_UP, DPAD_LEFT})}, + {"Japanese: its kana button, right of English", + language_pick_moves(0, IntroPicks{Language::Japanese, 0}), Moves(std::vector{DPAD_RIGHT})}, + {"Traditional Chinese from English", + language_pick_moves(0, IntroPicks{Language::ChineseTraditional, 0}), + Moves(std::vector{DPAD_RIGHT, DPAD_DOWN, DPAD_DOWN, DPAD_DOWN, DPAD_DOWN})}, + {"Italian, bottom left", + language_pick_moves(0, IntroPicks{Language::Italian, 0}), Moves(std::vector(4, DPAD_DOWN))}, + {"a language the menu lacks", + language_pick_moves(0, IntroPicks{Language::None, 0}), std::nullopt}, + {"a cursor outside the menu", + language_pick_moves(LANGUAGE_SELECT_BUTTONS, french), std::nullopt}, + {"Model 1, where the cursor starts", + look_pick_moves(0, IntroPicks{Language::English, 0}), Moves(std::vector{})}, + {"Model 3, Dawn's first look (lab run 3)", + look_pick_moves(0, french), Moves(std::vector{DPAD_RIGHT, DPAD_RIGHT})}, + {"Model 3 from Model 8", + look_pick_moves(7, french), Moves(std::vector{DPAD_UP, DPAD_LEFT})}, + {"Model 8, the last", + look_pick_moves(0, IntroPicks{Language::English, 7}), Moves(std::vector{DPAD_RIGHT, DPAD_RIGHT, DPAD_RIGHT, DPAD_DOWN})}, + {"no Model 9", + look_pick_moves(0, IntroPicks{Language::English, 8}), std::nullopt}, + {"a cursor outside the look grid", + look_pick_moves(LOOK_SELECT_PORTRAITS, french), std::nullopt}, + }; + for (const Case& c : cases){ + if (c.actual != c.expected){ + return std::string("Wrong presses for ") + std::string(c.what) + "."; + } + } + return check_intro_menu_sizes(); +} + +// Each select takes its pick, then A if it still shows. Other screens take +// A. The player's keyboard with a pick still to make fails. +UnitTestResult test_intro_pick_steps(Logger&){ + struct Case{ + std::string_view what; + std::string_view screen; + bool language_pending; + bool look_pending; + IntroStep expected; + }; + const Case cases[] = { + {"the language select", "language-select", true, true, IntroStep::PICK_LANGUAGE}, + {"the language select, picked", "language-select", false, true, IntroStep::ADVANCE}, + {"a logo before the language", "other", true, true, IntroStep::ADVANCE}, + {"a dialogue before the language", "dialog", true, true, IntroStep::ADVANCE}, + {"the look select", "look-select", false, true, IntroStep::PICK_LOOK}, + {"the look select, picked", "look-select", false, false, IntroStep::ADVANCE}, + {"Rowan, look to pick", "dialog", false, true, IntroStep::ADVANCE}, + {"a cutscene, look to pick", "other", false, true, IntroStep::ADVANCE}, + {"both picked", "dialog", false, false, IntroStep::ADVANCE}, + {"black, look to pick", "black", false, true, IntroStep::WAIT}, + {"the user select", "user-select", true, true, IntroStep::SELECT_USER}, + {"the keyboard, language missed", "keyboard", true, false, IntroStep::FAIL_PICK_MISSED}, + {"the keyboard, look missed", "keyboard", false, true, IntroStep::FAIL_PICK_MISSED}, + {"the keyboard, both picked", "keyboard", false, false, IntroStep::ENTER_PLAYER_NAME}, + }; + for (const Case& c : cases){ + IntroProgress progress = intro_progress(0); + progress.language_pending = c.language_pending; + progress.look_pending = c.look_pending; + if (decide_intro_step(c.screen, progress) != c.expected){ + return std::string("Wrong step for ") + std::string(c.what) + "."; + } + } + const IntroProgress first_look = intro_start(IntroPicks{Language::English, 0}); + if (!first_look.language_pending || first_look.look_pending){ + return "The language is always picked on its select; the first look needs no grid."; + } + const IntroProgress run3 = intro_start(IntroPicks{Language::French, 2}); + if (!run3.language_pending || !run3.look_pending){ + return "French and Model 3 need both menus."; + } + return true; +} + +// The intro's two selects from lab-B 2026-11-09 (each cursor on its first +// item), the screens one A before and after the look select, and screens +// that are neither. Screenshots are in the unit test resources; a missing +// one is passed over, and the test is skipped when all are. +UnitTestResult test_intro_menu_screenshots(Logger& logger){ + ImageRGB32 black(1920, 1080); + black.fill(0xff000000); + if (language_select_cursor(black) || look_select_cursor(black)){ + return "A black screen read as an intro menu."; + } + struct Case{ + std::string_view file; + std::optional language; + std::optional look; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/LanguageSelect-20261109.png", .language = 0, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/LookSelect-20261109.png", .language = std::nullopt, .look = 0}, + {.file = "PokemonBDSP/AutoStory/LookSelectBefore-20261109.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/LookSelectAfterA-20261109.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterCounter-20261108-094735.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterDoorstep-20261108.png", .language = std::nullopt, .look = std::nullopt}, + }; + size_t read = 0; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + logger.log("Screenshot not found: " + path); + continue; + } + read++; + const ImageRGB32 screen(path); + const std::optional language = language_select_cursor(screen); + const std::optional look = look_select_cursor(screen); + logger.log(std::string(c.file) + ": language " + (language ? std::to_string(*language) : "none") + + ", look " + (look ? std::to_string(*look) : "none")); + if (language != c.language || look != c.look){ + return std::string(c.file) + ": wrong intro menu read."; + } + } + if (read == 0){ + return UnitTestResult(UnitTestResult::SKIPPED, "No intro menu screenshots found."); + } + return true; +} + + +// The Mart's doorway on the failure screenshots of r121a and r122a, kept in +// the unit test resources (the CommandLineTests checkout): where it is +// relative to the player, who is in the middle of the screen. +UnitTestResult test_lit_doorway_screenshots(Logger& logger){ + struct Case{ + std::string file; + double tiles_across_min; + double tiles_across_max; + double rows_up_min; + double rows_up_max; + }; + const Case cases[] = { + // r121a: below-left of the door, which is part of a tile east, straight ahead. + {"PokemonBDSP/AutoStory/SandgemMartDoorUpRight-20261030-023443.png", 0.2, 0.7, 0.5, 1.5}, + // r122a: a tile west of the door and three or four rows south of its front. + {"PokemonBDSP/AutoStory/SandgemMartDoorFarLeft-20261030-024648.png", -1.3, -0.5, 2.5, 4.5}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const std::optional doorway = find_lit_doorway(screen); + if (!doorway){ + return std::string(c.file) + ": no lit doorway found."; + } + logger.log(std::string(c.file) + ": doorway at " + std::to_string(doorway->x) + ", " + + std::to_string(doorway->y) + ": " + std::to_string(doorway->tiles_across) + " tiles across, " + + std::to_string(doorway->rows_up) + " rows up."); + if (doorway->tiles_across < c.tiles_across_min || doorway->tiles_across > c.tiles_across_max){ + return std::string(c.file) + ": doorway " + std::to_string(doorway->tiles_across) + + " tiles across, expected " + std::to_string(c.tiles_across_min) + " to " + + std::to_string(c.tiles_across_max) + "."; + } + if (doorway->rows_up < c.rows_up_min || doorway->rows_up > c.rows_up_max){ + return std::string(c.file) + ": doorway " + std::to_string(doorway->rows_up) + + " rows up, expected " + std::to_string(c.rows_up_min) + " to " + + std::to_string(c.rows_up_max) + "."; + } + } + return true; +} + + +// The save prompt r124a took beside Sandgem's Poke Mart, sure it stood on +// Route 202 (in CommandLineTests under PokemonBDSP/AutoStory): its card +// names Sandgem Town, which the save must read before confirming. +UnitTestResult test_save_screen_location(Logger& logger){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + "PokemonBDSP/AutoStory/SaveScreenSandgemTown-20261030-034444.png"; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const std::string read = read_save_screen_location(ImageRGB32(path)); + logger.log("Save screen location read: \"" + read + "\"."); + if (!location_banner_matches(read, MAP_SANDGEM_TOWN)){ + return "Save screen location \"" + read + "\" should match Sandgem Town."; + } + if (location_banner_matches(read, MAP_ROUTE_202)){ + return "Save screen location \"" + read + "\" should not match Route 202."; + } + return true; +} + + +// The two shots around r126's door walk: below the Center's sign, outside, +// and on the entrance mat inside, where the lit floor read as a doorway and +// the walk kept lining up with it (runs 2 and 3). +UnitTestResult test_pokemon_center_inside(Logger& logger){ + struct Case{ + std::string file; + bool inside; + bool doorway; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/SandgemCenterInside-20261030-053555.png", .inside = true, .doorway = false}, + // r127c: walked in by the turn north at the door, facing north. + {.file = "PokemonBDSP/AutoStory/SandgemCenterInsideFacingNorth-r127c.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/SandgemCenterBelowSign-20261030-053527.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/SandgemMartDoorUpRight-20261030-023443.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/SandgemMartDoorFarLeft-20261030-024648.png", .inside = false, .doorway = true}, + // Jubilife (2026-10-30): inside its Center twice, at its door, and + // on the doorstep after walking out (the banner up). + {.file = "PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c6.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c4.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterFrontDoor-20261030-q2.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterDoorstep-20261030-c5.png", .inside = false, .doorway = true}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const bool inside = pokemon_center_inside(screen); + const std::optional doorway = find_lit_doorway(screen); + logger.log(std::string(c.file) + ": inside " + std::to_string(inside) + ", doorway " + + (doorway ? std::to_string(doorway->tiles_across) + " across, " + std::to_string(doorway->rows_up) + " rows up" : "none")); + if (inside != c.inside){ + return std::string(c.file) + ": expected " + (c.inside ? "inside" : "outside") + + " a Pokemon Center, read " + (inside ? "inside" : "outside") + "."; + } + if (doorway.has_value() != c.doorway){ + return std::string(c.file) + ": expected " + (c.doorway ? "a doorway" : "no doorway") + + ", read " + (doorway ? "one" : "none") + "."; + } + } + return true; +} + + +// Oreburgh's Center, lab-B 2026-11-08 (in CommandLineTests under +// PokemonBDSP/AutoStory, skipped without them): 01.03's run in saw no fade +// and stopped at the nurse's counter, the camera in close and the lit floor +// down to the screen's bottom; on the exit mat; and outside on the doorstep +// at sunset, the ground as orange as the Center's back wall. +UnitTestResult test_pokemon_center_counter(Logger& logger){ + struct Case{ + std::string file; + bool inside; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/OreburghCenterCounter-20261108-094735.png", .inside = true}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterExitMat-20261108.png", .inside = true}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterDoorstep-20261108.png", .inside = false}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const bool inside = pokemon_center_inside(screen); + logger.log(std::string(c.file) + ": inside " + std::to_string(inside)); + if (inside != c.inside){ + return std::string(c.file) + ": expected " + (c.inside ? "inside" : "outside") + + " a Pokemon Center, read " + (inside ? "inside" : "outside") + "."; + } + if (inside && find_lit_doorway(screen)){ + return std::string(c.file) + ": inside, yet a lit doorway was read."; + } + } + return true; +} + + +// The School's lamp at night, from its doorsteps' row (2026-10-30): where +// the door took us in, the lamp reads within half a tile of the door; a +// tile either side, more. +UnitTestResult test_school_lamp(Logger& logger){ + const ImageFloatBox box(0.55, 0.20, 0.30, 0.10); + struct Case{ std::string file; double min; double max; }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/JubilifeSchoolRightOfDoor-20261030.png", -1.5, -0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolOneTileShort-20261030.png", -1.5, -0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolAtDoor-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolAtDoor2-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolDoorstep-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolOneTilePast-20261030.png", 0.5, 1.5}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const std::optional lamp = find_lit_lamp_x(screen, box); + if (!lamp){ + return std::string(c.file) + ": no lit lamp found."; + } + const double across = tiles_across_at(*lamp) - tiles_across_at(0.67); + logger.log(std::string(c.file) + ": lamp at " + std::to_string(*lamp) + ", door " + std::to_string(across) + " tiles across."); + if (across < c.min || across > c.max){ + return std::string(c.file) + ": door " + std::to_string(across) + " tiles across, expected " + + std::to_string(c.min) + " to " + std::to_string(c.max) + "."; + } + } + return true; +} + + +// The Options screen of a French game (console B, 2026-11-09; in +// CommandLineTests under PokemonBDSP/AutoStory): read by position and +// colour only, the cursor on Text Speed with Normal (the middle of three) +// selected, and the first value of the two-value rows below it. +UnitTestResult test_options_french(Logger& logger){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + "PokemonBDSP/AutoStory/OptionsFrench-20261109.png"; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + if (!options_screen_shown(screen)){ + return "Options screen not recognised."; + } + const std::optional y = options_selected_row_y(screen); + if (!y || *y < 0.137 || *y > 0.177){ + return "Selected row not found on the first row."; + } + logger.log("Selected row at y=" + std::to_string(*y)); + if (options_highlighted_column(screen, *y, 3) != size_t(1)){ + return "Text Speed: Normal (column 1 of 3) not read."; + } + // Battle Effects to Gyroscope: rows 2 to 6, the first value highlighted. + for (size_t row = 2; row <= 6; row++){ + const double row_y = *y + 0.0858 * row; + if (options_highlighted_column(screen, row_y, 2) != size_t(0)){ + return "Row " + std::to_string(row) + ": the first of two values not read."; + } + } + ImageRGB32 black(1920, 1080); + black.fill(0xff000000); + if (options_screen_shown(black)){ + return "A black frame read as Options."; + } + return true; +} + + +// The move picked from names and PP as read. lab-B 2026-11-09, French: +// no name read, Piplup's Pound at 35 PP and Growl at 22 PP, and the old +// "newest move that is not a status move" fallback used Growl until the +// training gave up after 25 turns. +// A step along the row below the clown: a fade or a dialogue left the row +// and fails the talk (00.05c, 2026-11-11: the step's fade was ignored and +// the search went on in the Poketch Company's lobby); a step that moved +// nothing counts no tile. +UnitTestResult test_wander_row_step(Logger&){ + struct Case{ std::string_view name; WalkStop stop; size_t moved; int east; std::optional expected; }; + const Case cases[] = { + {"east step moved", WalkStop::LIMIT, 1, 1, 1}, + {"west step moved", WalkStop::LIMIT, 1, -1, -1}, + {"west step blocked by him", WalkStop::LIMIT, 0, -1, 0}, + {"east step into a wall", WalkStop::BLOCKED, 0, 1, 0}, + {"east step through a door", WalkStop::TRANSITION, 0, 1, std::nullopt}, + {"west step into a dialogue", WalkStop::DIALOG, 0, -1, std::nullopt}, + }; + for (const Case& c : cases){ + WalkResult result{}; + result.stop = c.stop; + result.moved_chunks = c.moved; + if (wander_row_step(result, c.east) != c.expected){ + return std::format("{}: wrong step.", c.name); + } + } + return true; +} + +// 00.05b's Poketch Company clown (home (15, 18)): the row below him asks +// north from (14..16, 19); from a row low, (13, 20) faces the Company's +// sign (13, 19), whose text came first on the console (lab-B 2026-11-11: +// "answered" in round 5, and the president gave no Poketch). Such a tile +// is skipped; his own home and open tiles are not. +UnitTestResult test_asks_someone_else(Logger&){ + const Pokemon::MapData& city = story_map("JubilifeCity"); + const Pokemon::TilePos home{15, 18}; + struct Case{ std::string_view name; Pokemon::TilePos stand; bool expected; }; + const Case cases[] = { + {"below his home", {15, 19}, false}, + {"a tile west, open north", {14, 19}, false}, + {"a tile east, open north", {16, 19}, false}, + {"a row low, facing the sign", {13, 20}, true}, + }; + for (const Case& c : cases){ + if (asks_someone_else(city, c.stand, home) != c.expected){ + return std::format("{}: wrong answer.", c.name); + } + } + return true; +} + +// A menu read on an empty image, or with buttons outside the frame, reads +// nothing instead of crashing (SIGSEGV in gray_box, 2026-11-12). +UnitTestResult test_move_menu_unreadable(Logger&){ + BattleMoveMenuState moves; + moves.move_count = 2; + moves.buttons[0] = ImageFloatBox{0.5, 0.5, 0.4, 0.1}; + moves.buttons[1] = ImageFloatBox{1.5, 1.5, 0.4, 0.1}; + const ImageRGB32 frame(64, 36); + const ImageViewRGB32 inputs[] = {ImageViewRGB32(), frame}; + for (const ImageViewRGB32& screen : inputs){ + const std::vector slots = read_move_menu(screen, moves); + if (slots.size() != 2){ + return "read_move_menu returned " + std::to_string(slots.size()) + " slots, expected 2."; + } + if (!slots[1].slug.empty() || slots[1].pp != -1){ + return "a button outside the frame read a move."; + } + } + return true; +} + +UnitTestResult test_choose_move(Logger&){ + struct Case{ std::string_view name; std::vector slots; size_t best; }; + const Case cases[] = { + {"no name read, PP 35 and 22", {{"", 35}, {"", 22}}, 1}, + {"no name read, PP 35 and 30", {{"", 35}, {"", 30}}, 1}, + {"no name or PP read", {{"", -1}, {"", -1}}, 1}, + {"Scratch and Leer", {{"scratch", 35}, {"leer", 30}}, 1}, + {"Pound, Growl, Water Gun and Charm", {{"pound", 35}, {"growl", 40}, {"water-gun", 10}, {"charm", 20}}, 3}, + {"Water Gun out of PP", {{"pound", 35}, {"growl", 40}, {"water-gun", 0}, {"charm", 20}}, 1}, + {"slot 1 unread at 38 PP: a status move", {{"", 38}, {"", 40}, {"", 20}}, 3}, + {"Growl unread, Tackle read", {{"", 40}, {"tackle", 35}}, 2}, + }; + for (const Case& c : cases){ + const size_t best = choose_move(c.slots); + if (best != c.best){ + return std::string(c.name) + ": picked move " + std::to_string(best) + ", expected " + std::to_string(c.best) + "."; + } + } + return true; +} + +// Move menus saved on lab-B (SCREENSHOTS_PATH()/AutoStory/moves/), read in +// the game's language. The French ones read nothing before the names came +// from the move name dictionary. +UnitTestResult test_move_menu_screenshots(Logger& logger){ + struct Case{ + std::string file; + Language language; + std::vector slots; + size_t best; + }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/MoveMenuFrenchPoundGrowl-20261109-192734.png", Language::French, + {{"pound", 35}, {"growl", 40}}, 1}, + {"PokemonBDSP/AutoStory/MoveMenuFrenchPoundGrowl16-20261109-193035.png", Language::French, + {{"pound", 35}, {"growl", 16}}, 1}, + {"PokemonBDSP/AutoStory/MoveMenuEnglishPiplup-20261108-181435.png", Language::English, + {{"pound", 35}, {"growl", 40}, {"water-gun", 10}, {"charm", 20}}, 3}, + }; + const Language story = story_language(); + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + set_story_language(c.language); + const std::vector slots = read_move_menu(screen, detect_battle_move_menu(screen)); + set_story_language(story); + std::string read; + for (size_t i = 0; i < slots.size(); i++){ + read += " [" + slots[i].describe(i) + "]"; + } + logger.log(std::string(c.file) + ":" + read); + if (slots.size() != c.slots.size()){ + return std::string(c.file) + ": " + std::to_string(slots.size()) + " moves, expected " + std::to_string(c.slots.size()) + "."; + } + for (size_t i = 0; i < slots.size(); i++){ + if (slots[i].slug != c.slots[i].slug || slots[i].pp != c.slots[i].pp){ + return std::string(c.file) + ": move " + std::to_string(i + 1) + " read as" + read + "."; + } + } + if (choose_move(slots) != c.best){ + return std::string(c.file) + ": picked move " + std::to_string(choose_move(slots)) + ", expected " + std::to_string(c.best) + "."; + } + } + return true; +} + + +// The Potion after a fight reads the lead's HP when the Bag opens and goes +// on to the pocket and the list only if a dose is needed (lab-B 2026-11-12: +// it found the Potion, opened the party, read 80% and "no Potion needed"). +UnitTestResult test_potion_need(Logger&){ + struct Case{ std::string_view name; double hp; size_t dose; PotionNeed need; }; + const Case cases[] = { + {"full HP", 1.0, 0, PotionNeed::FULL}, + {"80% on the first dose with the check at 80%", 0.80, 0, PotionNeed::ENOUGH}, + {"a bit over the check", 0.90, 0, PotionNeed::ENOUGH}, + {"under the check", 0.55, 0, PotionNeed::NEEDED}, + {"the second dose heals on until full", 0.85, 1, PotionNeed::NEEDED}, + {"fainted", 0.0, 0, PotionNeed::FAINTED}, + }; + for (const Case& c : cases){ + if (potion_need(c.hp, c.dose, 0.8) != c.need){ + return std::string(c.name) + ": wrong verdict."; + } + } + return true; +} + +struct AutoStoryActionsInput{ + std::string name; + std::function body; +}; + +struct AutoStoryActionsExpectation{ +}; + +struct AutoStoryActionsCase{ + AutoStoryActionsInput input; + AutoStoryActionsExpectation expectation; +}; + +class Test_AutoStoryActions : public UnitTest{ +public: + explicit Test_AutoStoryActions(AutoStoryActionsCase test_case) + : UnitTest("PokemonBDSP::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryActionsCase m_case; +}; + +} // namespace + + +const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { + { + .input = {"Potion after a fight: the lead's HP decides before the Bag is searched", test_potion_need}, + .expectation = {}, + }, + { + .input = {"new-game intro decisions", test_intro_decisions}, + .expectation = {}, + }, + { + .input = {"new-game intro: the rival's name confirmed with A", test_intro_rival_confirm}, + .expectation = {}, + }, + { + .input = {"new-game intro: B on unknown screens, then X for the menu", test_intro_before_menu}, + .expectation = {}, + }, + { + .input = {"new-game intro: in control without the menu read", test_intro_in_control}, + .expectation = {}, + }, + { + .input = {"new-game intro: BDSP or stop", test_intro_gate}, + .expectation = {}, + }, + { + .input = {"new-game intro: language and look presses", test_intro_pick_moves}, + .expectation = {}, + }, + { + .input = {"new-game intro: steps until the picks are made", test_intro_pick_steps}, + .expectation = {}, + }, + { + .input = {"intro menu screenshots", test_intro_menu_screenshots}, + .expectation = {}, + }, + { + .input = {"lit doorway screenshots", test_lit_doorway_screenshots}, + .expectation = {}, + }, + { + .input = {"save screen location", test_save_screen_location}, + .expectation = {}, + }, + { + .input = {"Pokemon Center inside", test_pokemon_center_inside}, + .expectation = {}, + }, + { + .input = {"Pokemon Center inside: Oreburgh's counter, exit mat and doorstep", test_pokemon_center_counter}, + .expectation = {}, + }, + { + .input = {"Jubilife School lamp", test_school_lamp}, + .expectation = {}, + }, + { + .input = {"Options read by position, French", test_options_french}, + .expectation = {}, + }, + { + .input = {"battle moves: the pick from names and PP", test_choose_move}, + .expectation = {}, + }, + { + .input = {"battle moves: unreadable menu reads nothing", test_move_menu_unreadable}, + .expectation = {}, + }, + { + .input = {"wanderer talk: a step along the row leaves it only by a fade or a dialogue", test_wander_row_step}, + .expectation = {}, + }, + { + .input = {"wanderer talk: no ask from a tile that faces a sign (00.05b, the Poketch Company's sign first)", test_asks_someone_else}, + .expectation = {}, + }, + { + .input = {"battle moves: move menu screenshots in each language", test_move_menu_screenshots}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Actions(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_ACTIONS_CASES){ + database.add(item); + } + // The menu cursor's own tests, with those of the intro, its first user. + add_tests_MenuCursor(database); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h new file mode 100644 index 0000000000..b92b104fd1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Actions_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Actions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Tests that need no console: the new game's intro decisions, and the screenshot checks of the Pokemon Center, the save card and the School's lamp. +void add_tests_AutoStory_Actions(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp new file mode 100644 index 0000000000..5e76a53bb2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp @@ -0,0 +1,440 @@ +/* BDSP AutoStory - Bag + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +ImageFloatBox bag_list_name_box(size_t row){ + // Previous height included the faint lower selection border in rows 2/6. + // return ImageFloatBox(0.492, 0.213 + (row - 1) * 0.075, 0.335, 0.052); + return ImageFloatBox(0.492, 0.213 + (row - 1) * 0.075, 0.335, 0.050); +} + +int bag_confirm_pocket(VideoStream& stream, ProControllerContext& context){ + int previous = 0; + size_t matches = 0; + for (size_t check = 0; check < 10; check++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const int pocket = frame ? bag_pocket_position(frame) : 0; + matches = pocket != 0 && pocket == previous ? matches + 1 : pocket != 0 ? 1 : 0; + previous = pocket; + if (matches >= 3){ + return pocket; + } + pbf_wait(context, 100ms); + } + return 0; +} + +bool bag_navigate_to_pocket(VideoStream& stream, ProControllerContext& context, int target){ + int current = bag_confirm_pocket(stream, context); + for (size_t attempt = 0; attempt < 3; attempt++){ + const auto plan = plan_bag_pocket_move(current, target); + if (plan.action == BagPocketAction::CONFIRM){ + return true; + } + if (plan.action == BagPocketAction::UNKNOWN){ + stream.log("[Bag search] Selected pocket not detected.", COLOR_ORANGE); + return false; + } + stream.log("[Bag search] Pocket " + std::to_string(current) + " to " + std::to_string(target) + ": " + + (plan.action == BagPocketAction::RIGHT ? "right " : "left ") + std::to_string(plan.presses) + + " presses, attempt " + std::to_string(attempt + 1) + "/3."); + for (size_t press = 0; press < plan.presses; press++){ + pbf_press_dpad(context, plan.action == BagPocketAction::RIGHT ? DPAD_RIGHT : DPAD_LEFT, 80ms, 150ms); + } + pbf_wait(context, 350ms); + current = bag_confirm_pocket(stream, context); + stream.log( + "[Bag search] Final pocket=" + std::to_string(current) + ", expected=" + std::to_string(target) + "."); + if (current == target){ + return true; + } + } + stream.log("[Bag search] Target pocket not reached after 3 movement batches.", COLOR_ORANGE); + return false; +} + +// Slot 1's colored HP bar in the Bag. Read fill position rather than OCR. +double potion_hp_fraction(const ImageViewRGB32& frame){ + if (bag_pocket_position(frame) == 0){ + return -1; + } + const ImageViewRGB32 bar = extract_box_reference(frame, ImageFloatBox{0.137, 0.160, 0.202, 0.016}); + size_t filled = 0; + for (size_t x = 0; x < bar.width(); x++){ + size_t color = 0; + for (size_t y = 0; y < bar.height(); y++){ + uint32_t p = bar.pixel(x, y); + int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + const bool green = g > r + 30 && g > b + 20; + const bool yellow = r > 150 && g > 120 && b < 100; + const bool red = r > 150 && r > g + 50 && r > b + 50; + color += green || yellow || red; + } + filled += color > bar.height() / 4; + } + return bar.width() ? (double)filled / bar.width() : -1; +} + +double confirmed_potion_hp(VideoStream& stream, ProControllerContext& context){ + double previous = -1; + size_t stable = 0; + for (size_t check = 0; check < 20; check++){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + double hp = frame ? potion_hp_fraction(frame) : -1; + if (hp >= 0 && previous >= 0 && std::abs(hp - previous) <= 0.015){ + if (++stable >= 3){ + return hp; + } + }else{ + stable = 0; + } + previous = hp; + pbf_wait(context, 150ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 HP bar could not be confirmed.", + stream); +} + +// A walk down a pocket's list, reading the cursor's row. +struct BagScan{ + DpadPosition direction = DPAD_DOWN; + std::string previous; + size_t same = 0; + size_t confirmed = 0; + size_t unknown_cursor = 0; +}; +enum class BagLook{ GO_ON, FOUND, NOT_FOUND }; + +// The story item's slug on the cursor's row `cursor`, in the game's +// language, or "" when the row is none of them. +std::string read_bag_row(Logger& logger, const ImageViewRGB32& frame, int cursor){ + return read_bag_item_slug(logger, story_language(), extract_box_reference(frame, bag_list_name_box(cursor))); +} + +// One look at the list: the row under the cursor is `name` (twice in a +// row), or the list ran out both ways, or the cursor moves on. +BagLook bag_scan_step(VideoStream& stream, ProControllerContext& context, BagScan& scan, const std::string& name){ + context.wait_for_all_requests(); + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame || bag_list_is_empty(*frame.frame)){ + stream.log("[Bag search] " + name + ": pocket is empty.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + const int cursor = bag_cursor_position(*frame.frame); + if (cursor == 0){ + if (++scan.unknown_cursor >= 3){ + stream.log("[Bag search] " + name + ": cursor not found.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + pbf_wait(context, 200ms); + return BagLook::GO_ON; + } + scan.unknown_cursor = 0; + const std::string normalized = read_bag_row(stream.logger(), *frame.frame, cursor); + stream.log("[Bag search] Row " + std::to_string(cursor) + " reads \"" + normalized + "\"."); + if (normalized == name){ + if (++scan.confirmed >= 2){ + stream.log("[Bag search] " + name + " confirmed in row " + std::to_string(cursor) + ".", COLOR_GREEN); + return BagLook::FOUND; + } + pbf_wait(context, 150ms); + return BagLook::GO_ON; + } + scan.confirmed = 0; + scan.same = normalized == scan.previous ? scan.same + 1 : 0; + scan.previous = normalized; + if (scan.same >= 2){ + if (scan.direction == DPAD_UP){ + stream.log("[Bag search] " + name + " not found.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + scan.direction = DPAD_UP; + scan.same = 0; + } + pbf_press_dpad(context, scan.direction, 80ms, 300ms); + return BagLook::GO_ON; +} + +// B out of the Bag to the overworld; fails with `failure` if it does not show. +void bag_back_to_overworld(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, std::chrono::seconds(10), {{overworld}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +enum class Dose{ HEALED, GIVEN, NONE_LEFT }; + +// One Potion on the lead, from the overworld, its HP read before and after +// in the Bag. HEALED when no dose is needed (HP full, or above +// `heal_below` on the first look) or the +// dose filled it; GIVEN when HP rose but is not full yet. +Dose potion_dose(VideoStream& stream, ProControllerContext& context, size_t dose, double heal_below){ + stream.log("[Potion] Opening the Bag; dose " + std::to_string(dose + 1) + "/3."); + // The lead's HP shows as soon as the Bag opens: read it before the + // pocket is walked and the list scanned for a Potion nobody needs. + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 1000ms); + const double before = confirmed_potion_hp(stream, context); + stream.log("[Potion] Slot 1 HP bar before use=" + std::to_string(before * 100) + "%."); + switch (potion_need(before, dose, heal_below)){ + case PotionNeed::FULL: + return Dose::HEALED; + case PotionNeed::ENOUGH: + stream.log("[Potion] Slot 1 HP is at least " + std::to_string(heal_below * 100) + "%; no Potion needed."); + return Dose::HEALED; + case PotionNeed::FAINTED: + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 appears fainted; a Potion cannot revive it.", + stream); + case PotionNeed::NEEDED: + break; + } + if (!find_bag_item_in_open_bag(stream, context, "potion", 1)){ + // No Potion left is not a failure: the next Center heals. After Roark + // the one Potion went on the first dose and the second found the + // Medicine pocket empty (lab-B, 2026-11-08 15:26). + stream.log("[Potion] No Potion left in the Bag after " + std::to_string(dose) + " dose(s); going on.", COLOR_ORANGE); + return Dose::NONE_LEFT; + } + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + bag_back_to_overworld(stream, context, "Potion: Did not return to the overworld after use."); + // Reopen Bag to verify the result, including when the last Potion was used. + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 750ms); + context.wait_for_all_requests(); + const double after = confirmed_potion_hp(stream, context); + stream.log("[Potion] Slot 1 HP bar after use=" + std::to_string(after * 100) + "%."); + if (after <= before + 0.005){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 HP did not increase; refusing to continue movement.", + stream); + } + return after >= 0.98 ? Dose::HEALED : Dose::GIVEN; +} + +} // namespace + +int bag_pocket_position(const ImageViewRGB32& frame){ + if (!frame){ + return 0; + } + int selected = 0; + for (int pocket = 1; pocket <= 8; pocket++){ + // The lower left edge of each tab is below the red NEW badge. + // This strip includes both the normal outline and enlarged selection. + const auto edge = + extract_box_reference(frame, ImageFloatBox(0.570 + (pocket - 1) * 0.0421, 0.098, 0.0045, 0.021)); + size_t colored = 0; + for (size_t y = 0; y < edge.height(); y++){ + for (size_t x = 0; x < edge.width(); x++){ + const uint32_t pixel = edge.pixel(x, y); + const int r = (pixel >> 16) & 255; + const int g = (pixel >> 8) & 255; + const int b = pixel & 255; + // Red when focused, blue when the item list has focus. + colored += (r > 160 && r > g + 60 && r > b + 60) || (b > 160 && b > r + 40 && b > g + 20); + } + } + const double pixels = (double)edge.width() * edge.height(); + if (pixels > 0 && colored / pixels >= 0.18){ + if (selected != 0){ + return 0; + } + selected = pocket; + } + } + return selected; +} + +// One narrow strip on the left side of the red selection border. Divide its +// height into the seven row bands; the item's art and NEW badge are outside it. +int bag_cursor_position(const ImageViewRGB32& frame){ + if (!frame){ + return 0; + } + const ImageViewRGB32 strip = extract_box_reference(frame, ImageFloatBox(0.431, 0.229, 0.005, 0.470)); + double best = 0.0; + double second = 0.0; + int position = 0; + for (int row = 0; row < 7; row++){ + const size_t top = static_cast(strip.height() * (row * 0.075 / 0.470)); + const size_t bottom = static_cast(strip.height() * ((row * 0.075 + 0.020) / 0.470)); + size_t red = 0; + size_t total = 0; + for (size_t y = top; y < bottom && y < strip.height(); y++){ + for (size_t x = 0; x < strip.width(); x++){ + const uint32_t pixel = strip.pixel(x, y); + const unsigned r = (pixel >> 16) & 255; + const unsigned g = (pixel >> 8) & 255; + const unsigned b = pixel & 255; + red += r > 180 && g < 120 && b < 120; + total++; + } + } + const double score = total == 0 ? 0.0 : static_cast(red) / total; + if (score > best){ + second = best; + best = score; + position = row + 1; + }else if (score > second){ + second = score; + } + } + return best >= 0.15 && best - second >= 0.10 ? position : 0; +} + +bool bag_list_is_empty(const ImageViewRGB32& frame){ + if (!frame || bag_pocket_position(frame) == 0 || bag_cursor_position(frame) != 0){ + return false; + } + // Empty pockets have no item icons. The central Empty label is outside + // this column; counting dark pixels in the name area included that label. + const auto icons = extract_box_reference(frame, ImageFloatBox(0.449, 0.213, 0.032, 0.500)); + const auto stats = image_stats(icons); + return stats.average.r > 220 && stats.average.g > 220 && stats.average.b > 220 && stats.stddev.sum() < 20; +} + +PotionNeed potion_need(double hp, size_t dose, double heal_below){ + if (hp >= 0.98){ + return PotionNeed::FULL; + } + if (dose == 0 && hp >= heal_below){ + return PotionNeed::ENOUGH; + } + return hp <= 0.005 ? PotionNeed::FAINTED : PotionNeed::NEEDED; +} + +BagPocketMovePlan plan_bag_pocket_move(int current, int target){ + if (current < 1 || current > 8 || target < 1 || target > 8){ + return {}; + } + const int right = (target - current + 8) % 8; + const int left = (current - target + 8) % 8; + if (right == 0){ + return {BagPocketAction::CONFIRM, 0}; + } + return right <= left ? BagPocketMovePlan{BagPocketAction::RIGHT, static_cast(right)} + : BagPocketMovePlan{BagPocketAction::LEFT, static_cast(left)}; +} + +bool find_bag_item_by_name( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket, + int num_icons +){ + open_menu(stream, context, MenuCursorPosition::BAG, num_icons); + pbf_wait(context, 1000ms); + return find_bag_item_in_open_bag(stream, context, name, target_pocket); +} + +bool find_bag_item_in_open_bag( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +){ + context.wait_for_all_requests(); + if (!bag_navigate_to_pocket(stream, context, target_pocket)){ + return false; + } + + // Walk down the list reading the cursor row. The list does not wrap, so + // turn around once the cursor stops moving. + BagScan scan; + for (size_t look = 0; look < 128; look++){ + const BagLook result = bag_scan_step(stream, context, scan, name); + if (result != BagLook::GO_ON){ + return result == BagLook::FOUND; + } + } + stream.log("[Bag search] " + name + " not found.", COLOR_ORANGE); + return false; +} + +bool bag_has_item(VideoStream& stream, ProControllerContext& context, const std::string& name, int target_pocket){ + const bool found = find_bag_item_by_name(stream, context, name, target_pocket, 5); + bag_back_to_overworld(stream, context, "Bag: Did not return to the overworld after looking for " + name + "."); + return found; +} + +std::vector bag_has_items( + VideoStream& stream, ProControllerContext& context, const std::vector& names, int target_pocket +){ + std::vector found; + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 1000ms); + for (const std::string& name : names){ + found.push_back(find_bag_item_in_open_bag(stream, context, name, target_pocket)); + } + bag_back_to_overworld(stream, context, "Bag: Did not return to the overworld after looking for the Key Items."); + return found; +} + +void use_potion_first_pokemon(VideoStream& stream, ProControllerContext& context, double heal_below){ + OverlayBoxScope hp_overlay(stream.overlay(), COLOR_GREEN, {0.137, 0.160, 0.202, 0.016}, ""); + bool healed = false; + for (size_t dose = 0; dose < 3 && !healed; dose++){ + const Dose result = potion_dose(stream, context, dose, heal_below); + if (result == Dose::NONE_LEFT){ + bag_back_to_overworld(stream, context, "Potion: No overworld after finding no Potion."); + return; + } + healed = result == Dose::HEALED; + if (!healed){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + } + } + if (!healed){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Full HP not confirmed after three uses.", + stream); + } + bag_back_to_overworld(stream, context, "Potion: Final overworld return not confirmed."); + stream.log("[Potion] Slot 1 HP bar full and overworld confirmed."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h new file mode 100644 index 0000000000..f38caf2785 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h @@ -0,0 +1,80 @@ +/* BDSP AutoStory - Bag + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Finding an item in the Bag, and Potions on the lead. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Bag_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Bag_H + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Starts in the overworld, opens the Bag on `target_pocket`, and moves the cursor +// onto the item whose name reads `name` (letters only, lower case, e.g. "potion"). +// Reads the cursor row's name with OCR, so it does not depend on the capture +// card's colors or sharpness the way a template match does. +// Returns false if the pocket is empty or the name is not found. +bool find_bag_item_by_name( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket, + int num_icons = 8 +); +// The same search in a Bag that is already open, such as the one a battle's +// Bag command opens. +bool find_bag_item_in_open_bag( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +); + +// Open the Bag from the overworld, look for `name` in the pocket, and close +// it again. True if the item is there. +bool bag_has_item( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +); + +// Open the Bag from the overworld once, look for each of `names` in the +// pocket, and close it again. Whether each is there, in `names`' order. +std::vector bag_has_items( + VideoStream& stream, + ProControllerContext& context, + const std::vector& names, + int target_pocket +); + +// Read the lead's HP bar in the Bag and, if it is below `heal_below` (a +// fraction of full), use Potions until it is full. heal_below = 1 always +// heals; 0.5 only checks and heals when the lead is under half, and is +// skipped when the Bag has no Potion. +void use_potion_first_pokemon( + VideoStream& stream, + ProControllerContext& context, + double heal_below = 1.0 +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp new file mode 100644 index 0000000000..11bc87234f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp @@ -0,0 +1,122 @@ +/* BDSP AutoStory - Bag item reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +namespace{ + +// The story's items in the same form as Packages' *OCR.json dictionaries +// (language -> slug -> names), so it can move to a resource file unchanged. +// Names are the games' official ones; Poke Ball's match PokeballNameOCR.json. +constexpr std::string_view STORY_ITEM_OCR = R"JSON({ +"eng": { + "potion": ["Potion"], "antidote": ["Antidote"], "paralyze-heal": ["Paralyze Heal"], + "awakening": ["Awakening"], "poke-ball": ["Poké Ball", "Poke Ball"], "repel": ["Repel"], + "super-repel": ["Super Repel"], "max-repel": ["Max Repel"], "escape-rope": ["Escape Rope"], + "coupon-1": ["Coupon 1"], "coupon-2": ["Coupon 2"], "coupon-3": ["Coupon 3"] +}, +"fra": { + "potion": ["Potion"], "antidote": ["Antidote"], "paralyze-heal": ["Anti-Para"], + "awakening": ["Réveil"], "poke-ball": ["Poké Ball"], "repel": ["Repousse"], + "super-repel": ["Super Repousse"], "max-repel": ["Repousse Max"], "escape-rope": ["Corde Sortie"] +}, +"deu": { + "potion": ["Trank"], "antidote": ["Gegengift"], "paralyze-heal": ["Para-Heiler"], + "awakening": ["Aufwecker"], "poke-ball": ["Pokéball"], "repel": ["Schutz"], + "super-repel": ["Superschutz"], "max-repel": ["Top-Schutz"], "escape-rope": ["Fluchtseil"] +}, +"spa": { + "potion": ["Poción"], "antidote": ["Antídoto"], "paralyze-heal": ["Antiparalizador"], + "awakening": ["Despertar"], "poke-ball": ["Poké Ball", "Poke Ball"], "repel": ["Repelente"], + "super-repel": ["Superrepelente"], "max-repel": ["Repelente Máximo"], "escape-rope": ["Cuerda Huida"] +}, +"ita": { + "potion": ["Pozione"], "antidote": ["Antidoto"], "paralyze-heal": ["Antiparalisi"], + "awakening": ["Sveglia"], "poke-ball": ["Poké Ball"], "repel": ["Repellente"], + "super-repel": ["Superrepellente"], "max-repel": ["Repellente Max"], "escape-rope": ["Fune di fuga"] +}, +"jpn": { + "potion": ["キズぐすり"], "antidote": ["どくけし"], "paralyze-heal": ["まひなおし"], + "awakening": ["ねむけざまし"], "poke-ball": ["モンスターボール"], "repel": ["むしよけスプレー"], + "super-repel": ["シルバースプレー"], "max-repel": ["ゴールドスプレー"], "escape-rope": ["あなぬけのヒモ"] +}, +"kor": { + "potion": ["상처약"], "antidote": ["해독제"], "paralyze-heal": ["마비치료제"], + "awakening": ["잠깨는약"], "poke-ball": ["몬스터볼"], "repel": ["벌레회피스프레이"], + "super-repel": ["실버스프레이"], "max-repel": ["골드스프레이"], "escape-rope": ["동굴탈출로프"] +}, +"chi_sim": { + "potion": ["伤药"], "antidote": ["解毒药"], "paralyze-heal": ["解麻药"], + "awakening": ["解眠药"], "poke-ball": ["精灵球"], "repel": ["除虫喷雾"], + "super-repel": ["白银喷雾"], "max-repel": ["黄金喷雾"], "escape-rope": ["离洞绳"] +}, +"chi_tra": { + "potion": ["傷藥"], "antidote": ["解毒藥"], "paralyze-heal": ["解麻藥"], + "awakening": ["解眠藥"], "poke-ball": ["精靈球"], "repel": ["除蟲噴霧"], + "super-repel": ["白銀噴霧"], "max-repel": ["黃金噴霧"], "escape-rope": ["離洞繩"] +} +})JSON"; + +class StoryItemMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P = -1.40; + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + static const StoryItemMatcher& instance(){ + static const StoryItemMatcher matcher; + return matcher; + } + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + // The list's names are dark gray on the white rows and on the + // cursor's highlight. + static const std::vector DARK_TEXT{ + {0xff000000, 0xff606060}, + {0xff000000, 0xff808080}, + {0xff000000, 0xffa0a0a0}, + }; + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, image, DARK_TEXT, + MAX_LOG10P, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + StoryItemMatcher() + : SmallDictionaryMatcher(parse_json(std::string(STORY_ITEM_OCR)).to_object_throw()) + {} +}; + +} // namespace + + +std::string read_bag_item_slug(Logger& logger, Language language, const ImageViewRGB32& image){ + return StoryItemMatcher::instance().read(logger, language, image); +} + +bool bag_item_named(Language language, std::string_view slug){ + const JsonValue json = parse_json(std::string(STORY_ITEM_OCR)); + const JsonObject* names = json.to_object_throw().get_object(language_data(language).code); + return names != nullptr && names->get_value(std::string(slug)) != nullptr; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h new file mode 100644 index 0000000000..f4e7779ffe --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h @@ -0,0 +1,36 @@ +/* BDSP AutoStory - Bag item reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads an item name in the Bag's list against the items the story uses, + * in every game language. Returns the item's slug ("potion"), or "". + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BagItemReader_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BagItemReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The slug of the story item named in `image` (one Bag row's name box), or +// "" when it is none of them. +std::string read_bag_item_slug(Logger& logger, Language language, const ImageViewRGB32& image); + +// Whether the reader has a name for the item `slug` in `language` (the +// coupons have their English names only). +bool bag_item_named(Language language, std::string_view slug); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h new file mode 100644 index 0000000000..c402094232 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h @@ -0,0 +1,60 @@ +/* BDSP AutoStory - Bag screen + * + * From: https://github.com/PokemonAutomation/ + * + * Which pocket and row the open Bag shows, and the presses between pockets. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BagScreen_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BagScreen_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Pocket positions 1-8: Medicine, Balls, Battle Items, Berries, Other Items, +// TMs, Treasures, Key Items. Zero means the selected tab is uncertain. +int bag_pocket_position(const ImageViewRGB32& frame); +// Whether the open pocket shows no item. +bool bag_list_is_empty(const ImageViewRGB32& frame); +// The cursor's row, 1-7, or 0 if unknown. +int bag_cursor_position(const ImageViewRGB32& frame); + +// Navigation action between bag pocket tabs. +enum class BagPocketAction{ + UNKNOWN, + CONFIRM, + LEFT, + RIGHT, +}; +// Plan to navigate from one bag pocket tab to another. +struct BagPocketMovePlan{ + BagPocketAction action = BagPocketAction::UNKNOWN; + size_t presses = 0; +}; +// Circular eight-pocket route; ties prefer Right. Invalid positions send no input. +BagPocketMovePlan plan_bag_pocket_move(int current, int target); + + +// What a first look at the lead's HP says about a Potion. +enum class PotionNeed{ + FULL, // nothing to heal + ENOUGH, // on the first dose, at or above `heal_below`: no Potion needed + FAINTED, // a Potion cannot revive it + NEEDED, +}; +// Whether the lead at `hp` (a fraction of full) needs dose number `dose` +// (0 first) of a Potion. Read before the Bag's pocket is walked for the +// Potion: no search when none is needed. +PotionNeed potion_need(double hp, size_t dose, double heal_below); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp new file mode 100644 index 0000000000..e177f711a6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp @@ -0,0 +1,401 @@ +/* BDSP AutoStory - Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_Battle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +BattleDialogWatcher::BattleDialogWatcher(Color color) + : DetectorToFinder( + "BattleDialogWatcher", + std::chrono::milliseconds(250), + color + ) +{ +} + +namespace{ + +// On the battle menu: the cursor onto Battle (the command list's first +// row), confirmed on three frames, then A. +void choose_battle_command(VideoStream& stream, ProControllerContext& context, BattleType battle_type, const std::string& label){ + BattleMenuDetector commands(battle_type); + VideoOverlaySet overlays(stream.overlay()); + commands.make_overlays(overlays); + overlays.add(COLOR_GREEN, {0.90, 0.48, 0.075, 0.505}); + size_t confirmed = 0; + for (size_t check = 0; check < 30 && confirmed < 3; check++){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + BattleMenuOption selected = frame ? battle_menu_selected_option(commands, frame) : BattleMenuOption::UNKNOWN; + stream.log("[" + label + "] Command cursor=" + std::to_string((int)selected + 1) + "; target Battle=1."); + if (selected == BattleMenuOption::BATTLE){ + if (++confirmed < 3){ + pbf_wait(context, 100ms); + } + }else if (selected == BattleMenuOption::UNKNOWN){ + confirmed = 0; + pbf_wait(context, 150ms); + }else{ + confirmed = 0; + pbf_press_dpad(context, DPAD_UP, 80ms, 250ms); + } + } + if (confirmed < 3){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Could not confirm Battle command; A not pressed.", stream); + } + pbf_press_button(context, BUTTON_A, 80ms, 500ms); +} + +// Watching the move menu settle: how many frames in a row showed the same +// number of moves, and how many had the cursor on the target. +struct MoveMenuWatch{ + size_t previous_move_count = 0; + size_t stable_move_count = 0; + size_t confirmed = 0; + // The move to use, 1-based; 0 until the menu has settled and the best is picked. + size_t target = 0; +}; + +// One look at the move menu. Returns whether the target is confirmed +// under the cursor; otherwise moves the cursor toward it or waits. +bool move_menu_step(VideoStream& stream, ProControllerContext& context, MoveMenuWatch& watch, const std::string& label){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + const BattleMoveMenuState moves = frame ? detect_battle_move_menu(frame) : BattleMoveMenuState{}; + const int slot = (int)moves.selected_slot; + VideoOverlaySet move_overlays(stream.overlay()); + for (size_t i = 0; i < moves.move_count; i++){ + move_overlays.add(moves.selected_slot == i + 1 ? COLOR_GREEN : COLOR_YELLOW, moves.buttons[i]); + } + stream.log("[" + label + "] Available moves=" + std::to_string(moves.move_count) + + "; selected slot=" + std::to_string(slot) + "/4; target=" + std::to_string(watch.target) + "."); + if (moves.move_count == watch.previous_move_count && moves.move_count > 0){ + watch.stable_move_count++; + }else{ + watch.previous_move_count = moves.move_count; + watch.stable_move_count = 1; + watch.confirmed = 0; + } + // Pick the best move once the menu has settled. + if (watch.target == 0 && watch.stable_move_count >= 3 && frame){ + watch.target = pick_best_move(stream, *frame.frame, moves, label); + } + if (moves.move_count > 0 && moves.move_count < watch.target && watch.stable_move_count >= 3){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Move slot " + std::to_string(watch.target) + " is absent; no move confirmed.", stream); + } + if (slot == (int)watch.target && watch.stable_move_count >= 3){ + if (++watch.confirmed >= 3){ + return true; + } + pbf_wait(context, 100ms); + }else{ + watch.confirmed = 0; + if (slot == 0 || slot == (int)watch.target){ + pbf_wait(context, 150ms); + }else{ + pbf_press_dpad(context, slot < (int)watch.target ? DPAD_DOWN : DPAD_UP, 80ms, 250ms); + } + } + context.wait_for_all_requests(); + return false; +} + +// On the battle menu: Battle, then the best move on the move menu once +// it has settled, confirmed under the cursor, then A. +void use_best_move(VideoStream& stream, ProControllerContext& context, BattleType battle_type, const std::string& label){ + choose_battle_command(stream, context, battle_type, label); + MoveMenuWatch watch; + for (size_t check = 0; check < 30; check++){ + if (move_menu_step(stream, context, watch, label)){ + stream.log("[" + label + "] Move " + std::to_string(watch.target) + " confirmed; using it."); + pbf_press_button(context, BUTTON_A, 80ms, 3000ms); + return; + } + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Could not confirm move " + std::to_string(watch.target) + "; A not pressed.", stream); +} + +// Our HP bar in the battle's bottom-left panel, as a fraction; -1 if +// no bar is there. Measured on 1920x1080 frames: the bar runs from +// x 88 to 466 on rows 961 to 965. +double battle_player_hp(const ImageViewRGB32& frame){ + const ImageViewRGB32 bar = extract_box_reference(frame, ImageFloatBox{0.046, 0.890, 0.197, 0.004}); + size_t filled = 0; + for (size_t x = 0; x < bar.width(); x++){ + size_t color = 0; + for (size_t y = 0; y < bar.height(); y++){ + const uint32_t p = bar.pixel(x, y); + const int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + const bool green = g > r + 30 && g > b + 20; + const bool yellow = r > 150 && g > 120 && b < 100; + const bool red = r > 150 && r > g + 50 && r > b + 50; + color += green || yellow || red; + } + filled += color > bar.height() / 2; + } + return bar.width() && filled ? (double)filled / bar.width() : -1; +} + +// Our HP at the battle menu, from up to five looks; -1 if unread. +double read_battle_hp(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + double hp = -1; + for (size_t check = 0; check < 5 && hp < 0; check++){ + const auto frame = stream.video().snapshot(); + if (frame){ + hp = battle_player_hp(*frame.frame); + } + if (hp < 0){ + pbf_wait(context, 150ms); + } + context.wait_for_all_requests(); + } + return hp; +} + +// On the battle menu: the cursor onto Bag. Whether it got there. +bool cursor_to_bag(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + BattleMenuDetector commands(battle_type); + for (size_t check = 0; check < 30; check++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const BattleMenuOption selected = frame ? battle_menu_selected_option(commands, frame) : BattleMenuOption::UNKNOWN; + if (selected == BattleMenuOption::BAG){ + return true; + } + if (selected == BattleMenuOption::UNKNOWN){ + pbf_wait(context, 150ms); + }else{ + pbf_press_dpad(context, (int)selected < (int)BattleMenuOption::BAG ? DPAD_DOWN : DPAD_UP, 80ms, 300ms); + } + } + return false; +} + +enum class BattleHeal{ NOT_NEEDED, USED, NONE_LEFT }; + +// At the battle menu: if our HP is under `heal_below`, use a Potion +// from the Bag on the lead, which takes the turn. Otherwise the +// battle menu is left up. +BattleHeal battle_heal_if_low(VideoStream& stream, ProControllerContext& context, + BattleType battle_type, double heal_below, const std::string& label){ + const double hp = read_battle_hp(stream, context); + stream.log("[" + label + "] Our HP bar reads " + std::to_string((int)(hp * 100)) + "%."); + if (hp < 0 || hp >= heal_below){ + return BattleHeal::NOT_NEEDED; + } + if (!cursor_to_bag(stream, context, battle_type)){ + stream.log("[" + label + "] Could not reach the Bag command; fighting on.", COLOR_ORANGE); + return BattleHeal::NONE_LEFT; + } + pbf_press_button(context, BUTTON_A, 80ms, 2000ms); + if (!find_bag_item_in_open_bag(stream, context, "potion", 1)){ + stream.log("[" + label + "] No Potion left; fighting on.", COLOR_ORANGE); + pbf_press_button(context, BUTTON_B, 80ms, 1500ms); + pbf_press_button(context, BUTTON_B, 80ms, 1500ms); + context.wait_for_all_requests(); + return BattleHeal::NONE_LEFT; + } + // Potion, then the lead in the party list, then Use. + stream.log("[" + label + "] Using a Potion on the lead.", COLOR_BLUE); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + pbf_press_button(context, BUTTON_A, 80ms, 2500ms); + context.wait_for_all_requests(); + return BattleHeal::USED; +} + +// How a battle is fought: every battle here takes the best move each turn. +struct BestMoveBattle{ + BattleType battle_type = BattleType::TRAINER; + BattleFormat format = BattleFormat::SINGLES; + // After a faint the game shows the party: send out the next Pokemon. + // Without, a quiet two minutes is a timeout. + bool switch_on_faint = false; +}; + +// What a battle has done so far, across turns. +struct BattleProgress{ + bool battle_menu_seen = false; + bool out_of_potions = false; + size_t potions_used = 0; + size_t turns = 0; + size_t faint_switches = 0; +}; + +// One turn at the battle menu: a Potion when a trainer's battle has left +// our HP low, else the best move (and the default target in doubles). +void take_turn( + VideoStream& stream, ProControllerContext& context, + const BestMoveBattle& battle, BattleProgress& progress, const std::string& label +){ + progress.battle_menu_seen = true; + // Early battles take a few turns; this many means the chosen + // move is not being used (no PP, a misread menu). + if (++progress.turns > 25){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": still at the battle menu after 25 turns.", stream); + } + stream.log("[AutoStory] Battle menu detected.", COLOR_BLUE); + // A trainer battle we may lose: a Potion when HP is low. + if (battle.battle_type == BattleType::TRAINER && !progress.out_of_potions && progress.potions_used < 4){ + const BattleHeal heal = battle_heal_if_low(stream, context, battle.battle_type, 0.35, label); + if (heal == BattleHeal::USED){ + progress.potions_used++; + return; + } + progress.out_of_potions = heal == BattleHeal::NONE_LEFT; + } + stream.log("[" + label + "] Move list position 1: use move 0."); + use_best_move(stream, context, battle.battle_type, label); + // A double battle asks for the target next; B would + // back out of it. The default target is an opponent. + if (battle.format == BattleFormat::DOUBLES){ + pbf_wait(context, 1200ms); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + } +} + +// The battle has ended: its text to the overworld, which must stay +// dialogue-free for four seconds. +void finish_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ + stream.log("[AutoStory] " + label + " battle complete.", COLOR_GREEN); + OverworldWatcher overworld; + const int cleanup_ret = run_until(stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, {{overworld}}); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Post-battle cleanup timed out.", stream); + } + context.wait_for_all_requests(); + confirm_battle_overworld(stream, context, label + " cleanup"); +} + +// B through the battle's text until its menu, its end, or a move-learning +// prompt shows: 0, 1 or 2; -1 after the wait. +int wait_for_battle_screen( + VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, + const BattleProgress& progress, EndBattleWatcher& end_battle +){ + context.wait_for_all_requests(); + BattleMenuWatcher battle_menu(battle.battle_type); + // Reuse the farming handlers' move-learning prompt recovery. + SelectionArrowFinder learn_move(stream.overlay(), {0.50, 0.62, 0.40, 0.18}, COLOR_YELLOW); + const bool watch_learn_move = battle.format == BattleFormat::DOUBLES || battle.switch_on_faint; + return run_until( + stream, + context, + [&](ProControllerContext& context){ + // Clear narration with B while watching for commands. + // A here can open Fight before selection begins. + // With faint switching, a quiet 30 s means the party + // screen after a faint, which B cannot leave. + pbf_mash_button(context, BUTTON_B, battle.switch_on_faint ? 30000ms : 120000ms); + }, + { + {battle_menu}, + progress.battle_menu_seen ? PeriodicInferenceCallback{end_battle} : PeriodicInferenceCallback{}, + watch_learn_move ? PeriodicInferenceCallback{learn_move} : PeriodicInferenceCallback{}, + } + ); +} + +// Fights the battle on screen to its end and the overworld after it. +void fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ + stream.log("[AutoStory] Starting " + label + " battle...", COLOR_BLUE); + // Preserve the existing early-game transition delay. + pbf_wait(context, 5000ms); + BattleProgress progress; + EndBattleWatcher end_battle; + while (true){ + const int ret = wait_for_battle_screen(stream, context, battle, progress, end_battle); + if (ret == 0){ + take_turn(stream, context, battle, progress, label); + }else if (ret == 1){ + finish_battle(stream, context, label); + return; + }else if (ret == 2){ + stream.log("[" + label + "] Declining move learning; preserving scripted move positions."); + pbf_move_right_joystick(context, {0, -1}, 160ms, 840ms); + pbf_press_button(context, BUTTON_ZL, 160ms, 840ms); + }else if (battle.switch_on_faint && progress.faint_switches < 5){ + // After a faint the game shows the party: Down to the next + // Pokemon, A, then A on "Send out" (by hand, 2026-10-29 against Roark). + progress.faint_switches++; + stream.log("[" + label + "] No battle menu for 30 s: a faint; sending out the next Pokemon.", COLOR_ORANGE); + pbf_press_dpad(context, DPAD_DOWN, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1200ms); + pbf_press_button(context, BUTTON_A, 80ms, 2000ms); + }else{ + stream.log("[AutoStory] " + label + " battle timeout.", COLOR_RED); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "[AutoStory] " + label + " battle timeout.", stream); + } + } +} + +} // namespace + +void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, + const std::string& label){ + talk_over(stream, context, label); +} + +void fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ + // The best move each turn; the next Pokemon after a faint; no new moves. + fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = format, .switch_on_faint = true}, label); +} + +void fight_trainer(VideoStream& stream, ProControllerContext& context, const std::string& label){ + // The best move on the menu each turn, whatever the starter; the next Pokemon after a faint. + fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = BattleFormat::SINGLES, .switch_on_faint = true}, label); +} + +void fight_wild_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ + fight_with_best_move(stream, context, {.battle_type = BattleType::STANDARD, .format = BattleFormat::SINGLES, .switch_on_faint = true}, label); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h new file mode 100644 index 0000000000..371ab3877e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h @@ -0,0 +1,63 @@ +/* BDSP AutoStory - Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Fighting a battle with the best move on the menu: the move names are read + * and scored by power, so it works whatever the starter knows. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Battle_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Battle_H + +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonTools/VisualDetector.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A battle's text box, as a watcher. +class BattleDialogWatcher : public DetectorToFinder{ +public: + BattleDialogWatcher(Color color = COLOR_RED); +}; + +// One opponent, or two (a tag or twin battle, which asks for a target). +enum class BattleFormat{ + SINGLES, + DOUBLES, +}; + +// The speeches after a battle, to their end: talk_over(), B through the +// lines until the overworld is quiet. Roark's badge lines pause for the +// fanfare, and someone may walk off before the next line, so a moment +// with no box is not the end. Fails with a screenshot after two minutes. +void confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// A story battle that may go badly: the best move each turn, Potions +// under 35 %, the next Pokemon when one faints, and "Keep old moves" +// on level-up prompts. `doubles` confirms the default target each +// turn in DOUBLES. Returns once the battle has ended, +// won or lost; after a loss we stand in the last Pokemon Center. +void fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); + +// Any trainer battle, whatever the starter: the best move on the menu each +// turn until the battle ends, then the text after it. +void fight_trainer(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// A wild battle at its battle menu, fought with the best move until it ends. +void fight_wild_battle(VideoStream& stream, ProControllerContext& context, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp new file mode 100644 index 0000000000..6557c928a8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp @@ -0,0 +1,372 @@ +/* BDSP AutoStory - Battle moves + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// How close a read must come to a move's name, as the other move readers. +constexpr double MOVE_NAME_MAX_LOG10P = -1.30; +constexpr double MOVE_NAME_LOG10P_SPREAD = 0.50; + +// Early moves the starters and their foes have, by the slug of the move +// name dictionary, as type, power and most PP. A status move has power 0. +struct KnownMove{ + std::string slug; + std::string type; + int power; + int pp; +}; +const KnownMove KNOWN_MOVES[] = { + {"pound", "normal", 40, 35}, {"tackle", "normal", 40, 35}, {"scratch", "normal", 40, 35}, + {"quick-attack", "normal", 40, 30}, {"headbutt", "normal", 70, 15}, + {"water-gun", "water", 40, 25}, {"bubble", "water", 40, 30}, {"bubble-beam", "water", 65, 20}, + {"ember", "fire", 40, 25}, {"flame-wheel", "fire", 60, 25}, {"mach-punch", "fighting", 40, 30}, + {"absorb", "grass", 20, 25}, {"razor-leaf", "grass", 55, 25}, {"bite", "dark", 60, 25}, + {"peck", "flying", 35, 35}, {"pluck", "flying", 60, 20}, {"wing-attack", "flying", 60, 35}, + {"metal-claw", "steel", 50, 35}, + {"growl", "normal", 0, 40}, {"leer", "normal", 0, 30}, {"tail-whip", "normal", 0, 30}, + {"withdraw", "water", 0, 40}, {"taunt", "dark", 0, 20}, {"charm", "fairy", 0, 20}, + {"curse", "ghost", 0, 10}, +}; + +const KnownMove* known_move(std::string_view slug){ + for (const KnownMove& move : KNOWN_MOVES){ + if (slug == move.slug){ + return &move; + } + } + return nullptr; +} + +// What each starter has in each slot as BDSP teaches it, learnt in this +// order up to level 13: slot 1 is a Normal attack, slot 2 a status move, +// slot 3 the starter's own-type attack, slot 4 either. +const std::vector> STARTER_SLOTS = { + {"tackle", "scratch", "pound"}, + {"withdraw", "leer", "growl"}, + {"absorb", "ember", "water-gun"}, + {"razor-leaf", "taunt", "charm"}, +}; + +// A slot whose name was not read, from its PP, which rules out any move +// with less most PP, among what the starters have in that slot (any known +// move if none of those is left): a known move when every move left is an +// attack (the weakest) or every one a status move. +const KnownMove* guess_unread_move(size_t slot, int pp){ + std::vector left; + if (slot < STARTER_SLOTS.size()){ + for (std::string_view slug : STARTER_SLOTS[slot]){ + left.push_back(known_move(slug)); + } + } + auto pp_rules_out = [&](const KnownMove* move){ return pp > move->pp; }; + std::erase_if(left, pp_rules_out); + if (left.empty()){ + for (const KnownMove& move : KNOWN_MOVES){ + left.push_back(&move); + } + std::erase_if(left, pp_rules_out); + } + if (left.empty()){ + return nullptr; + } + const KnownMove* weakest = left[0]; + for (const KnownMove* move : left){ + if ((move->power > 0) != (weakest->power > 0)){ + return nullptr; + } + if (move->power < weakest->power){ + weakest = move; + } + } + return weakest; +} + +// Every language's names of the known moves, from the move name dictionary. +JsonObject known_move_names(){ + const JsonValue all = load_json_file(RESOURCE_PATH() + "PokemonSV/PokemonMovesOCR.json"); + JsonObject known; + for (const auto& [language, names] : all.to_object_throw()){ + JsonObject names_known; + for (const KnownMove& move : KNOWN_MOVES){ + const JsonValue* name = names.to_object_throw().get_value(move.slug); + if (name){ + names_known[move.slug] = name->clone(); + } + } + known[language] = std::move(names_known); + } + return known; +} + +const OCR::SmallDictionaryMatcher& move_name_matcher(){ + static const OCR::SmallDictionaryMatcher matcher(known_move_names()); + return matcher; +} + +// The box's text, whatever differs from its median colour by more than +// 120, as black on white with an 8-pixel margin. +ImageRGB32 pp_text_image(const ImageViewRGB32& box){ + std::vector pixels; + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + pixels.push_back(box.pixel(x, y)); + } + } + auto brightness = [](uint32_t p){ return ((p >> 16) & 255) + ((p >> 8) & 255) + (p & 255); }; + std::nth_element(pixels.begin(), pixels.begin() + pixels.size() / 2, pixels.end(), + [&](uint32_t a, uint32_t b){ return brightness(a) < brightness(b); }); + const uint32_t background = pixels[pixels.size() / 2]; + const size_t pad = 8; + ImageRGB32 text(box.width() + 2 * pad, box.height() + 2 * pad); + text.fill(0xffffffff); + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + const uint32_t p = box.pixel(x, y); + int distance = 0; + for (int shift : {16, 8, 0}){ + distance += std::abs((int)((p >> shift) & 255) - (int)((background >> shift) & 255)); + } + if (distance > 120){ + text.pixel(x + pad, y + pad) = 0xff000000; + } + } + } + return text; +} + +// A move button's PP left, "8/25" at its right end; -1 if it cannot be +// read. The text is dark, or yellow to red as PP runs low, so text is +// whatever differs from the button's own color. `max_pp` helps when +// the OCR reads the slash as a digit. +int read_move_pp(const ImageViewRGB32& screen, const ImageFloatBox& button, int max_pp){ + const ImageViewRGB32 box = extract_box_reference(screen, ImageFloatBox{ + button.x + button.width * 0.75, button.y + button.height * 0.10, + button.width * 0.21, button.height * 0.80}); + if (box.width() == 0 || box.height() == 0){ + return -1; + } + const std::string read = OCR::tesseract_ocr_read(Language::English, pp_text_image(box)); + const size_t slash = read.find('/'); + std::string digits; + for (size_t i = 0; i < read.size() && i < slash; i++){ + if (std::isdigit((unsigned char)read[i])){ + digits.push_back(read[i]); + } + } + if (slash == std::string::npos && max_pp > 0){ + // "29135" for 29/35: drop the most PP and the misread slash. + const std::string max = std::to_string(max_pp); + if (digits.size() <= max.size() + 1 || digits.compare(digits.size() - max.size(), max.size(), max) != 0){ + return -1; + } + digits.resize(digits.size() - max.size() - 1); + } + if (digits.empty() || digits.size() > 2){ + return -1; + } + return std::stoi(digits); +} + +// A box's gray levels, row by row, and the cut between text and background: +// halfway between the background and the darkest pixel (a fixed cut thins +// the strokes on the blue button). +struct GrayBox{ + std::vector gray; + size_t width = 0; + size_t height = 0; + int threshold = 0; + bool text_in_row(size_t row) const{ + for (size_t x = 0; x < width; x++){ + if (gray[row * width + x] < threshold){ + return true; + } + } + return false; + } +}; +GrayBox gray_box(const ImageViewRGB32& box){ + GrayBox ret{std::vector(box.width() * box.height()), box.width(), box.height(), 0}; + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + const uint32_t pixel = box.pixel(x, y); + ret.gray[y * box.width() + x] = (int)(((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff)) / 3; + } + } + if (ret.gray.empty()){ + // Nothing to read: a box outside the frame, or an empty image. + return ret; + } + std::vector sorted = ret.gray; + std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); + const int background = sorted[sorted.size() / 2]; + const int darkest = *std::min_element(ret.gray.begin(), ret.gray.end()); + ret.threshold = (background + darkest) / 2; + return ret; +} + +// The tallest band of text rows, [top, bottom): the name; "Effective" is smaller. +std::pair tallest_text_band(const GrayBox& box){ + size_t band_top = 0, band_bottom = 0; + for (size_t y = 0; y < box.height;){ + if (!box.text_in_row(y)){ + y++; + continue; + } + size_t end = y; + while (end < box.height && box.text_in_row(end)){ + end++; + } + if (end - y > band_bottom - band_top){ + band_top = y; + band_bottom = end; + } + y = end; + } + return {band_top, band_bottom}; +} + +// The name is dark text on a light button (grey, or light blue under the +// cursor). Take the box right of the type icon and left of the PP, tall +// enough for a status move's name, which sits lower: it has no "Effective" +// line under it. Returns the slug of the known move it reads as, in the +// story's language; "" if none. +std::string read_move_name(const ImageViewRGB32& screen, const ImageFloatBox& button){ + const ImageFloatBox name_box{button.x + button.width * 0.17, button.y + button.height * 0.08, + button.width * 0.53, button.height * 0.80}; + const GrayBox box = gray_box(extract_box_reference(screen, name_box)); + if (box.gray.empty()){ + return ""; + } + const auto [band_top, band_bottom] = tallest_text_band(box); + // Black text on white, with a margin, for the OCR. + const size_t pad = 6; + const size_t top = band_top >= 4 ? band_top - 4 : 0; + const size_t bottom = std::min(box.height, band_bottom + 4); + ImageRGB32 text(box.width + 2 * pad, bottom - top + 2 * pad); + text.fill(0xffffffff); + for (size_t y = top; y < bottom; y++){ + for (size_t x = 0; x < box.width; x++){ + if (box.gray[y * box.width + x] < box.threshold){ + text.pixel(x + pad, y - top + pad) = 0xff000000; + } + } + } + const OCR::StringMatchResult read = move_name_matcher().match_substring_from_image( + nullptr, story_language(), text, MOVE_NAME_MAX_LOG10P, MOVE_NAME_LOG10P_SPREAD); + if (read.results.size() != 1){ + return ""; + } + return read.results.begin()->second.token; +} + +// `slug` read in slot `i`, or what the slot read last time if this read +// failed. +std::string remember_move(std::string slug, size_t i){ + static std::array last_read; + if (slug.empty()){ + return last_read[i]; + } + last_read[i] = slug; + return slug; +} + +} // namespace + + +std::string MoveSlot::describe(size_t slot) const{ + const KnownMove* known = known_move(slug); + const KnownMove* guess = known ? nullptr : guess_unread_move(slot, pp); + const std::string what = known + ? std::string(known->slug) + " (" + known->type + ", " + std::to_string(known->power) + ")" + : guess + ? std::string("unread, ") + (guess->power > 0 ? "an attack" : "a status move") + " by slot and PP" + : "unread"; + return what + ", PP left " + (pp < 0 ? std::string("unread") : std::to_string(pp)); +} + +size_t choose_move(const std::vector& slots){ + size_t best = 0; + int best_score = -1; + for (size_t i = 0; i < slots.size(); i++){ + // A move with no PP left cannot be chosen; the game says so and + // keeps the menu up. + if (slots[i].pp == 0){ + continue; + } + const KnownMove* known = known_move(slots[i].slug); + if (!known){ + known = guess_unread_move(i, slots[i].pp); + } + // A move that might be either counts below any attack known for + // sure, and an earlier one first: slot 1 is an attack for every + // starter. + const int score = known ? known->power * 2 + (std::string_view(known->type) != "normal" ? 1 : 0) : 1; + if (score > best_score){ + best_score = score; + best = i; + } + } + return best + 1; +} + +std::vector read_move_menu(const ImageViewRGB32& screen, const BattleMoveMenuState& moves){ + std::vector slots; + for (size_t i = 0; i < moves.move_count; i++){ + MoveSlot slot; + slot.slug = read_move_name(screen, moves.buttons[i]); + const KnownMove* known = known_move(slot.slug); + slot.pp = read_move_pp(screen, moves.buttons[i], known ? known->pp : 0); + slots.push_back(std::move(slot)); + } + return slots; +} + +size_t pick_best_move(VideoStream& stream, const ImageViewRGB32& screen, + const BattleMoveMenuState& moves, const std::string& label){ + // Keep the menu as it looked, to check the reads against. + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/moves/"; + Filesystem::create_directories(Filesystem::Path(folder)); + screen.save(folder + now_to_filestring() + "-" + label + ".png"); + std::vector slots = read_move_menu(screen, moves); + for (size_t i = 0; i < slots.size(); i++){ + slots[i].slug = remember_move(std::move(slots[i].slug), i); + stream.log("[" + label + "] Move " + std::to_string(i + 1) + ": " + slots[i].describe(i) + "."); + } + const size_t best = choose_move(slots); + stream.log("[" + label + "] Best move: " + std::to_string(best) + "."); + return best; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h new file mode 100644 index 0000000000..9dfc62fbbb --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h @@ -0,0 +1,59 @@ +/* BDSP AutoStory - Battle moves + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Which move to use: the move menu's names, read in the game's language, + * and PP, scored by the power of the early moves the starters and their + * foes have. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BattleMoves_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BattleMoves_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// One move on the move menu as read: the slug of the move name dictionary +// ("" when the name was not read) and the PP left (-1 when not read). +struct MoveSlot{ + std::string slug; + int pp = -1; + + // What the move in 0-based slot `slot` is taken for, for the log. + std::string describe(size_t slot) const; +}; + +// The best of `slots`, 1-based: the most powerful one we know, and on a +// tie one that is not Normal (an early starter's own type). A move with +// no PP left is passed over. A move whose name was not read is told by +// what the starters have in its slot and the PP it has left, which rules +// out moves with less most PP; one that might still be either comes after +// every attack, earliest first. +size_t choose_move(const std::vector& slots); + +// The names, in the story's language, and PP of the moves on the move +// menu `moves` of `screen`. +std::vector read_move_menu(const ImageViewRGB32& screen, const BattleMoveMenuState& moves); + +// The best move on the move menu `moves` of `screen`, 1-based, as +// choose_move() picks from the names read in the game's language and the +// PP left. Keeps a screenshot of the menu, labelled `label`. +size_t pick_best_move(VideoStream& stream, const ImageViewRGB32& screen, const BattleMoveMenuState& moves, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp new file mode 100644 index 0000000000..340b26ba70 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp @@ -0,0 +1,507 @@ +/* BDSP AutoStory - Pokemon Center + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_LitDoorway.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using namespace Pokemon; + + +namespace{ + +// How many tiles the 1080p view spans across: a tile is about 125 px. +constexpr double SCREEN_WIDTH_TILES = 1920.0 / 125; +// The camera keeps the player in the middle of the screen across, and the +// centre of his tile a little above the middle down (r121a and r122a: his +// feet at y = 590 of 1080). +constexpr double PLAYER_X = 0.5; +constexpr double PLAYER_Y = 0.505; +// How much of the screen's height one row of tiles takes near the player: +// the doorway straight ahead of him (r121a) is centred this far above his +// tile's centre. Rows further north look shorter, so this undercounts them. +constexpr double SCREEN_ROW_HEIGHT = 0.12; +// Doorways are looked for down to here, 0 to 1: a door the player can walk +// into is north of him. +constexpr double DOORWAY_SEARCH_BOTTOM = 0.67; +// A doorway found within this many rows of straight ahead is at the front +// row; further, the walk to the door ended short. +constexpr double DOORWAY_FRONT_ROWS = 1.5; + +// The doorway is looked for on a view this size: 4 px of 1080p per pixel. +constexpr size_t SMALL_WIDTH = 480; +constexpr size_t SMALL_HEIGHT = 270; +// A doorway's lit columns run at least this many small rows (the Mart's 12, +// the Center's 15); the buildings' lit roof bands 3 to 5. +constexpr size_t DOORWAY_COLUMN_ROWS = 8; +// A doorway has this many lit small pixels (the Mart's 260 to 350); a roof +// band's run 60 to 75. +constexpr double DOORWAY_MIN_MASS = 80; + +// Whether a pixel is the pale yellow-white of a lit doorway: the Mart's is +// (235, 243, 205), the Center's (255, 255, 185). The buildings' white roof +// bands are bluer, their orange bands and the light they throw on the +// ground outside are darker. +bool lit_doorway_pixel(uint32_t pixel){ + const int r = (pixel >> 16) & 0xff; + const int g = (pixel >> 8) & 0xff; + const int b = pixel & 0xff; + return r >= 215 && g >= 215 && b >= 150 && b <= g - 15 && r >= g - 25; +} + +// Each column of the small view: how many lit doorway pixels it has in the +// search band, and the sum of their rows. +struct LitColumns{ + std::vector count; + std::vector row_sum; +}; +LitColumns lit_columns(const ImageViewRGB32& screen){ + const ImageRGB32 small = screen.scale_to(SMALL_WIDTH, SMALL_HEIGHT); + LitColumns columns{std::vector(small.width(), 0), std::vector(small.width(), 0)}; + const size_t bottom = (size_t)(DOORWAY_SEARCH_BOTTOM * (double)small.height()); + for (size_t y = 0; y < bottom; y++){ + for (size_t x = 0; x < small.width(); x++){ + if (lit_doorway_pixel(small.pixel(x, y))){ + columns.count[x]++; + columns.row_sum[x] += (double)y; + } + } + } + return columns; +} + +// A run of lit columns: its lit pixels, and the sums of their columns and rows. +struct LitRun{ + double mass = 0; + double column_sum = 0; + double row_sum = 0; +}; +// The run of columns with at least DOORWAY_COLUMN_ROWS lit that starts at +// `x`, allowing gaps of up to 3 columns (the door's frame). Moves `x` past it. +LitRun lit_run(const LitColumns& columns, size_t& x){ + LitRun run; + size_t gap = 0; + for (; x < columns.count.size() && gap <= 3; x++){ + if (columns.count[x] < DOORWAY_COLUMN_ROWS){ + gap++; + continue; + } + run.mass += (double)columns.count[x]; + run.column_sum += (double)columns.count[x] * (double)x; + run.row_sum += columns.row_sum[x]; + gap = 0; + } + return run; +} + +// Where a building's lit doorway is across and down the screen, 0 to 1: the +// biggest run of lit columns within 0.35 of the middle (a second building's +// door may show further off). Nothing if no doorway shows. +std::optional> lit_doorway_position(const ImageViewRGB32& screen){ + const LitColumns columns = lit_columns(screen); + const double width = (double)columns.count.size(); + std::optional> best; + double best_mass = 0; + for (size_t x = 0; x < columns.count.size(); ){ + if (columns.count[x] < DOORWAY_COLUMN_ROWS){ + x++; + continue; + } + const LitRun run = lit_run(columns, x); + const double center = run.column_sum / run.mass / width; + if (run.mass < DOORWAY_MIN_MASS || std::abs(center - PLAYER_X) >= 0.35 || run.mass <= best_mass){ + continue; + } + best = {center, run.row_sum / run.mass / (double)SMALL_HEIGHT}; + best_mass = run.mass; + } + return best; +} + +// Whether the whole view is a different scene from "before". +bool scene_changed_since(VideoStream& stream, ProControllerContext& context, const VideoSnapshot& before){ + context.wait_for_all_requests(); + VideoSnapshot now = stream.video().snapshot(); + return before && now && walk_screen_change(*before.frame, *now.frame) > WALK_SCENE_CHANGE; +} + +// Moves across by "tiles", east if positive, west if negative. +void step_across(VideoStream& stream, ProControllerContext& context, double tiles){ + const Direction side = tiles > 0 ? Direction::EAST : Direction::WEST; + route_turn(stream, context, side); + const Milliseconds hold = std::max(Milliseconds(40), + Milliseconds((int64_t)(std::abs(tiles) * ROUTE_MS_PER_TILE_ACROSS.count()))); + pbf_move_left_joystick(context, {tiles > 0 ? 1.0 : -1.0, 0}, hold, 300ms); +} + +// Whether the screen now shows a Pokemon Center's inside. +bool center_inside_now(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot now = stream.video().snapshot(); + return now && pokemon_center_inside(*now.frame); +} + +// Walks north into the door straight ahead, `doorway` away. Returns whether +// we are inside. +bool walk_into_door( + VideoStream& stream, ProControllerContext& context, + const VideoSnapshot& outside, const LitDoorway& doorway, const std::string& label +){ + // Time for the rows to the door (none when it is straight ahead), the + // door opening, and a start delay. + const double rows = std::max(0.0, doorway.rows_up - 1); + const Milliseconds limit = 1500ms + Milliseconds((int64_t)(rows * ROUTE_MS_PER_TILE_UP_DOWN.count())); + // The Center's fade is too light to read as black (r126 runs 2 and 3: a + // screen change of 49 at 400 ms), so its inside, once it shows, stops + // the walk too. + const WalkStop stop = walk_until(stream, context, run_from_wild_battle, {0, 1}, limit, label + ": into the door", + [](const ImageViewRGB32& screen){ return pokemon_center_inside(screen); }).stop; + if (stop == WalkStop::TRANSITION || stop == WalkStop::ARRIVED){ + pbf_wait(context, 2000ms); + return true; + } + // The door slides open before it takes us in, and its fade is not always + // black enough to read as a transition, and may finish after the walk; a + // whole new scene says we are in. Not after a walk of several rows, which + // scrolls the view: the next look decides. + pbf_wait(context, 2000ms); + if (center_inside_now(stream, context)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; inside."); + return true; + } + if (doorway.rows_up > DOORWAY_FRONT_ROWS || !scene_changed_since(stream, context, outside)){ + return false; + } + stream.log("[Route] " + label + ": the scene changed; inside."); + return true; +} + +// Where the doorway is on screen, in the log. +void log_doorway(VideoStream& stream, const std::string& label, const LitDoorway& doorway){ + stream.log("[Route] " + label + ": doorway " + std::to_string(doorway.tiles_across) + + " tiles across, " + std::to_string(doorway.rows_up) + " rows up (at " + + std::to_string(doorway.x) + ", " + std::to_string(doorway.y) + " of the screen)."); +} + +// Near the entrance: black below the floor, either side of the step out +// of the door, and the floor lit between them: not a fade (349 in +// Jubilife at night). +bool center_entrance_view(const ImageViewRGB32& screen){ + for (const ImageFloatBox& box : {ImageFloatBox(0.02, 0.95, 0.30, 0.04), ImageFloatBox(0.65, 0.95, 0.33, 0.04)}){ + if (image_stats(extract_box_reference(screen, box)).average.sum() > 90){ + return false; + } + } + return image_stats(extract_box_reference(screen, ImageFloatBox(0.30, 0.55, 0.40, 0.15))).average.sum() > 250; +} + +// At the nurse's counter, the camera in close and the lit floor down to +// the screen's bottom (lab-B 2026-11-08: a run in whose fade went unseen +// stopped there): either side of us, the counter's red top (r 246, g 84) +// over its white front (about 200 each). Outside Oreburgh's Center at +// sunset the ground there is orange (r 218, g 134), with nothing white. +bool center_counter_view(const ImageViewRGB32& screen){ + for (double x : {0.15, 0.56}){ + const FloatPixel top = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.58, 0.12, 0.03))).average; + const FloatPixel front = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.66, 0.12, 0.03))).average; + const double front_min = std::min({front.r, front.g, front.b}); + const double front_max = std::max({front.r, front.g, front.b}); + const bool red = top.r >= 120 && top.r > 2 * top.g && top.g > top.b; + const bool white = front_min >= 120 && front_max - front_min <= 40; + if (!red || !white){ + return false; + } + } + return true; +} + +} // namespace + + +bool pokemon_center_inside(const ImageViewRGB32& screen){ + // The orange back wall along the top, in two of three places: a plant + // or a bookcase can stand in one (Jubilife's top left), and at night + // it is darker (r 160..200 in Jubilife, 254 in Sandgem by day). + size_t wall = 0; + for (double x : {0.02, 0.25, 0.65}){ + const ImageStats box = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.03, 0.08, 0.06))); + const double r = box.average.r, g = box.average.g, b = box.average.b; + wall += r >= 140 && r > g + 40 && g > b + 40; + } + return wall >= 2 && (center_entrance_view(screen) || center_counter_view(screen)); +} + +void fail_if_blacked_out(VideoStream& stream, ProControllerContext& context, const std::string& label){ + for (size_t look = 0; look < 10; look++){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (screen && indoors(*screen.frame)){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": the party fainted; the game took us home (a room shows after the battle).", stream); + } + // The blackout's text and fade are dark; the grass is not. + if (screen && image_stats(*screen.frame).average.sum() > 150){ + return; + } + pbf_wait(context, 1000ms); + } +} + +std::optional find_lit_lamp_x(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const size_t x0 = (size_t)(box.x * screen.width()), x1 = (size_t)((box.x + box.width) * screen.width()); + const size_t y0 = (size_t)(box.y * screen.height()), y1 = (size_t)((box.y + box.height) * screen.height()); + double sum = 0; + size_t count = 0; + for (size_t y = y0; y < y1 && y < screen.height(); y++){ + for (size_t x = x0; x < x1 && x < screen.width(); x++){ + const Color pixel(screen.pixel(x, y)); + if (pixel.red() > 235 && pixel.green() > 220 && pixel.blue() > 150){ + sum += x; + count++; + } + } + } + // Jubilife's lamps at night: 50 to 100 such pixels at 480 px across. + if (count * 480 * 480 < 30 * screen.width() * screen.width()){ + return std::nullopt; + } + return sum / count / screen.width(); +} + +double tiles_across_at(double screen_x){ + return (screen_x - PLAYER_X) * SCREEN_WIDTH_TILES; +} + +std::optional find_lit_doorway(const ImageViewRGB32& screen){ + // A Center's lit floor has the doorway's colour. + if (pokemon_center_inside(screen)){ + return std::nullopt; + } + const std::optional> position = lit_doorway_position(screen); + if (!position){ + return std::nullopt; + } + return LitDoorway{ + position->first, + position->second, + (position->first - PLAYER_X) * SCREEN_WIDTH_TILES, + (PLAYER_Y - position->second) / SCREEN_ROW_HEIGHT, + }; +} + +std::vector tiles_short_of_door( + VideoStream& stream, ProControllerContext& context, + const StoryWorld& grid, const TilePos& door, const std::string& label +){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + const std::optional doorway = screen ? find_lit_doorway(*screen.frame) : std::nullopt; + if (!doorway){ + stream.log("[Route] " + label + ": no lit doorway on screen; not walking again."); + return {}; + } + log_doorway(stream, label, *doorway); + if (doorway->rows_up <= DOORWAY_FRONT_ROWS){ + return {}; + } + return tiles_by_lit_doorway(*doorway, grid, door); +} + +std::vector tiles_by_lit_doorway(const LitDoorway& doorway, const StoryWorld& grid, const TilePos& door){ + const TilePos guess{door.x - (int)std::lround(doorway.tiles_across), door.y + (int)std::lround(doorway.rows_up)}; + std::vector tiles; + for (int y = guess.y - 1; y <= guess.y + 1; y++){ + for (int x = guess.x - 1; x <= guess.x + 1; x++){ + if (standable(grid, TilePos{x, y})){ + tiles.push_back(TilePos{x, y}); + } + } + } + return tiles; +} + +bool line_up_with_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + route_status(stream, label + ": lining up with the door."); + for (size_t tries = 0; tries < 6; tries++){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (screen && pokemon_center_inside(*screen.frame)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; not in front of the door."); + return false; + } + const std::optional doorway = screen ? find_lit_doorway(*screen.frame) : std::nullopt; + if (!doorway){ + stream.log("[Route] " + label + ": no lit doorway on screen."); + return false; + } + log_doorway(stream, label, *doorway); + // Steps overshoot: within a quarter tile, r128 measured 0.29, -0.28, + // 0.48, -0.67 and 0.48 tiles in turn and never settled. The door + // still takes us in from 0.4 (enter_lit_door()). + if (std::abs(doorway->tiles_across) <= 0.4){ + return true; + } + step_across(stream, context, doorway->tiles_across); + // Let a door that took us in show its inside before looking again. + pbf_wait(context, 1000ms); + } + stream.log("[Route] " + label + ": not lined up after 6 steps."); + return false; +} + +namespace{ + +// No lit doorway shows: the door may have taken us in after all, more +// slowly than we watched. Fails unless the scene changed since `last_outside`. +void confirm_inside_without_doorway(VideoStream& stream, ProControllerContext& context, const VideoSnapshot& last_outside, const std::string& label){ + pbf_wait(context, 1500ms); + if (!scene_changed_since(stream, context, last_outside)){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": no lit doorway on screen.", stream); + } + stream.log("[Route] " + label + ": the scene changed; inside."); + pbf_wait(context, 1500ms); +} + +} // namespace + +void enter_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + // Movement is not locked to tiles, and the door takes us in only when we + // are lined up with it. Its doorway is lit: find it on screen, where the + // camera keeps the player in the middle, move across by its offset, and + // walk in once it is straight ahead, looking again after every move. + route_status(stream, label + ": lining up with the door."); + VideoSnapshot last_outside; + for (size_t tries = 0; tries < 12; tries++){ + context.wait_for_all_requests(); + VideoSnapshot outside = stream.video().snapshot(); + if (!outside){ + continue; + } + // Inside a Center, its lit floor would read as another doorway. + if (pokemon_center_inside(*outside.frame)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; inside."); + return; + } + const std::optional doorway = find_lit_doorway(*outside.frame); + if (!doorway){ + confirm_inside_without_doorway(stream, context, last_outside, label); + return; + } + last_outside = outside; + log_doorway(stream, label, *doorway); + // We stand on tiles, so within half a tile is the door's column; + // a step moves about 0.7 of a reading, and at 0.25 r144a stepped + // between 0.3 and -0.4 twelve times. Try the door there, and step + // toward it if it does not take us. + if (std::abs(doorway->tiles_across) > 0.5){ + step_across(stream, context, doorway->tiles_across); + continue; + } + if (walk_into_door(stream, context, outside, *doorway, label)){ + pbf_wait(context, 1500ms); + return; + } + step_across(stream, context, doorway->tiles_across); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": not through the door after 12 tries.", stream); +} + +void heal_at_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + enter_lit_door(stream, context, label); + heal_inside_pokemon_center(stream, context, label); +} + +void heal_inside_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + talk_over(stream, context, label + ": inside the Center", {.rule = {.door = true}}); + talk_to_nurse(stream, context, label); + route_status(stream, label + ": healed; walking out."); + + leave_door_south(stream, context, run_from_wild_battle, label + ": out the door"); +} + +void talk_to_nurse( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + // The nurse's counter is straight ahead of the door: one held run north + // until the counter stops us, then A. The caller settled the door's + // fade-in (TalkOverRule::door), so the run is not lost in it. + route_status(stream, label + ": running to the nurse."); + pbf_controller_state(context, BUTTON_B, DPAD_UP, {0, 0}, {0, 0}, NURSE_RUN_HOLD); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + ShortDialogWatcher first_line(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{first_line}}) != 0){ + // No line: someone in the way, or not lined up. Walk with checks. + stream.log("[Route] " + label + ": no line after the run to the nurse; walking up with checks."); + const WalkResult walk = walk_to_talk(stream, context, run_from_wild_battle, {0, 1}, 6000ms, label + ": to the nurse"); + if (walk.stop != WalkStop::DIALOG){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": nurse not reached.", stream); + } + } + + // A through her lines; the cursor starts on "Yes". Stop once no dialogue + // has shown for a while, so A does not start the talk over. + size_t quiet = 0; + for (size_t presses = 0; presses < 30 && quiet < 2; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + quiet = 0; + pbf_press_button(context, BUTTON_A, 80ms, 700ms); + }else{ + quiet++; + } + } + context.wait_for_all_requests(); +} + + + +void move_across(VideoStream& stream, ProControllerContext& context, double tiles){ + step_across(stream, context, tiles); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h new file mode 100644 index 0000000000..399ccf31a3 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h @@ -0,0 +1,94 @@ +/* BDSP AutoStory - Pokemon Center + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Center_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Center_H + +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// After a wild battle out of doors: fails with a screenshot if the screen +// turns into a room within 10 s, which means the party fainted and the +// game took us home (r258 lost its third training battle in Route 202's +// grass, woke in Twinleaf, and walked the Sandgem plan there). Returns +// once the outside shows, not black and not a room. +void fail_if_blacked_out(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// For a player on the row below a building's door, its lit doorway on +// screen: steps across by the doorway's offset, looking again after each +// step, until it is straight ahead. Does not go in, and does not turn north +// to face it: a turn there walked us in (r127c). Returns false if no +// doorway shows, a Center's inside shows, or after 6 steps. +bool line_up_with_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// Goes in through a building's door (a Pokemon Center's or a Poke Mart's). +// Starts with its lit doorway on screen, within a few tiles: lines up with +// the doorway by sight and walks in, looking again after every move. Whole- +// tile steps sideways miss it, since movement is not locked to tiles, and +// its fade may not read as black: a new scene also counts as inside. Fails +// with a screenshot when no doorway shows, or after 12 moves. +void enter_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// Heals the party at a Pokemon Center. Starts in front of the Center with its +// lit doorway on screen, within a few tiles of it: lines up with the doorway +// by sight, walks in, talks to the nurse, accepts, and walks back out. Ends +// on the tile below the door. +void heal_at_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// heal_at_pokemon_center() from just inside the door: talks to the nurse, +// accepts, and walks back out. Ends on the tile below the door. For a caller +// that walked in on its own (from a known tile, straight north). +void heal_inside_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// At most this many tiles from a Center's door mat up to the nurse's +// counter, straight north (a bound, not measured: the counter is the wall +// that ends the run, so going on is harmless). +constexpr size_t CENTER_MAT_TO_COUNTER_TILES = 8; +// The one held run (B and up) from the door mat to the counter: long +// enough at walking pace too, before the running shoes. +constexpr Milliseconds NURSE_RUN_HOLD = + ROUTE_START_DELAY + ROUTE_MS_PER_TILE_UP_DOWN * (int64_t)CENTER_MAT_TO_COUNTER_TILES; + +// From just inside a Pokemon Center's door: walks up to the nurse, talks to + +// her and accepts. Ends at her counter, for a caller that walks out its own +// way. +void talk_to_nurse( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp new file mode 100644 index 0000000000..40fa26813b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp @@ -0,0 +1,39 @@ +/* BDSP AutoStory - Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +const StoryEvents& story_events(){ + static const StoryEvents events = []{ + StoryEvents ret; + add_lake_events(ret); + add_sandgem_events(ret); + add_jubilife_events(ret); + add_oreburgh_events(ret); + add_route204_events(ret); + return ret; + }(); + return events; +} + +StoryEvent console_event(std::function action){ + return [action = std::move(action)](StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + action(executor.stream(), executor.context()); + return Pokemon::outcome_ok(); + }; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h new file mode 100644 index 0000000000..5ed92a1f06 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h @@ -0,0 +1,102 @@ +// code-health: allow header_symbols -- story event dispatch table and per-area event population functions +/* BDSP AutoStory - Events + * + * From: https://github.com/PokemonAutomation/ + * + * The story's steps that only BDSP has, by name: a step of the table whose + * variant names one of these runs it (StoryExecutor::run()). Some are the + * scripted scenes (the Azure Flute, the lake's briefcase, the starter, + * Dawn's catching tutorial and the catch); the rest do what the shared + * steps cannot, as they ran on the console: the Poke Mart lady's Potions + * and the stairs into Jubilife (00.04c, 00.04d), a clown who wanders + * (00.05b and 00.05c), the Poketch check (00.05c, 01.01), the Gym's + * tries from the Pokemon Center (01.04) and Rock Smash + * (01.09). Each fails by throwing, with a screenshot; the executor turns + * that into a failed step. + * + * They are in files by area: _Events_Lake (00.00 to 00.04), _Sandgem + * (00.04c, 00.04d), _Jubilife (00.05b), _Oreburgh (01.04) and _Route204 + * (01.09). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Events_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Events_H + +#include +#include +#include +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_MapData.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Runs a step on the console. Returns where it left the player when it +// knows; throws OperationFailedException when it fails. +using StoryEvent = std::function; +// Map of story event names to their execution functions. +using StoryEvents = std::map; + +// Every event of the story, by name. +const StoryEvents& story_events(); + +// An event that only acts and says nothing of where it leaves us. +StoryEvent console_event(std::function action); + +// Populates lake-area story event handlers. +void add_lake_events(StoryEvents& events); +// Populates Sandgem-area story event handlers. +void add_sandgem_events(StoryEvents& events); +// Populates Jubilife-area story event handlers. +void add_jubilife_events(StoryEvents& events); +// Populates Oreburgh-area story event handlers. +void add_oreburgh_events(StoryEvents& events); +// Populates Route 204-area story event handlers. +void add_route204_events(StoryEvents& events); + +// A one-tile step along the row below a wanderer (00.05b's clowns), `east` +// +1 or -1, from its walk: the tiles it moved east (0 when he or a wall +// blocked it), or none when it left the row: through a door, into a +// dialogue. 00.05c (2026-11-11): a step's fade went unread and the search +// went on indoors, so the next checkpoint saved in the Poketch Company. +std::optional wander_row_step(const WalkResult& result, int east); +// Whether asking north from `stand` on `map` would talk to a sign or to +// someone who stands still, not the wanderer whose home is +// `wanderer_home` (00.05b, lab-B 2026-11-11: the Poketch Company clown's +// talk was "answered" in round 5 by another line). +bool asks_someone_else(const Pokemon::MapData& map, Pokemon::TilePos stand, Pokemon::TilePos wanderer_home); +// The box the Mart clown wanders, round (55, 33): PokemonSimulation's +// data/games/bdsp/maps/jubilife_city.json (decomp range 1). The story map +// leaves him out of its wanderers (the decomp hides him behind a flag). +Pokemon::TileBox jubilife_mart_clown_box(); +// A Jubilife clown's coupon (00.05b): his talk step and the Key Item's +// Bag slug. +struct ClownCoupon{ + std::string_view step; + std::string_view slug; +}; +// The three clowns' coupons: the Mart's 1, the company's 2, the TV's 3. +std::span jubilife_clown_coupons(); +// The row the Poketch Company's clown wanders, round (15, 18), as the +// Mart clown's box. +Pokemon::TileBox jubilife_company_clown_box(); +// The tiles 00.05c may start on: against the Poketch Company's sign +// (00.05b's end), and below the east-exit sign (its own end, 01.01's start). +std::pair jubilife_start_tiles(); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp new file mode 100644 index 0000000000..68d15a9618 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp @@ -0,0 +1,377 @@ +/* BDSP AutoStory - Events: Jubilife City (00.05b, 00.05c, 01.01) + * + * From: https://github.com/PokemonAutomation/ + * + * The Jubilife beats the story table cannot plan: the talk to a wandering + * clown, the check that the Poketch came, and where 00.05c starts. + * + */ + +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteInput.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_WanderWait.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// 00.05b: a talk to someone who wanders a tile either side of home along a +// row (the clowns by the Mart and the Poketch Company, WANDER_AROUND with +// x range 1). Waiting under home alone missed him for a minute (r66: he +// stood a tile east), so walk the row below his range, a whole D-pad tile +// at a time, asking north from each tile. +bool ask_north(VideoStream& stream, ProControllerContext& context){ + route_turn(stream, context, Direction::NORTH); + for (size_t ask = 0; ask < 2; ask++){ + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + return true; + } + } + return false; +} +// A clown's talk is his question, then its Yes/No box: any dialog is not +// (00.05b, lab-B 2026-11-11: the Poketch Company clown was "answered" in +// round 5 by another line, and the president gave no Poketch). Press A +// until the box's cursor shows, then A for Yes and through the coupon. +// False when the talk ends with no box: someone or something else, or a +// clown who has given his coupon already. +bool answer_coupon_question(VideoStream& stream, ProControllerContext& context, const std::string& label){ + static const ImageFloatBox YES_NO_ARROW_BOX{0.700, 0.620, 0.070, 0.120}; + SelectionArrowFinder yes_no(stream.overlay(), YES_NO_ARROW_BOX, COLOR_GREEN); + for (size_t press = 0; press < 6; press++){ + context.wait_for(1200ms); + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (screen && yes_no.detect(*screen.frame)){ + stream.log("[Talk] " + label + ": the coupon question; Yes."); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + finish_dialogue(stream, context, BUTTON_A, label); + return true; + } + if (!dialogue_opens(stream, context, 500ms)){ + stream.log("[Talk] " + label + ": the talk ended with no Yes/No box.", COLOR_ORANGE); + return false; + } + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + } + finish_dialogue(stream, context, BUTTON_B, label); + stream.log("[Talk] " + label + ": no Yes/No box in 6 presses.", COLOR_ORANGE); + return false; +} +// One tile along the row, east (+1) or west (-1). Returns the tiles moved +// east; fails the step when the walk left the row (wander_row_step()). +int step_along(VideoStream& stream, ProControllerContext& context, int east, const std::string& label){ + route_turn(stream, context, east > 0 ? Direction::EAST : Direction::WEST); + const WalkResult result = walk_tiles_dpad(stream, context, run_from_wild_battle, {east > 0 ? 1.0 : -1.0, 0}, 1, label); + const std::optional moved = wander_row_step(result, east); + if (!moved){ + fail_step(stream, std::format("{}: {} instead of a step along the row.", label, walk_stop_name(result.stop))); + } + return *moved; +} +// Walks back below his home and faces north. +void back_below_home(VideoStream& stream, ProControllerContext& context, int at, const std::string& name){ + for (size_t tries = 0; at != 0 && tries < 4; tries++){ + at += step_along(stream, context, at > 0 ? -1 : 1, "Back below " + name + "'s home"); + } + if (at != 0){ + fail_step(stream, std::format("{}: still {} tiles east of his home after 4 steps back.", name, at)); + } + route_turn(stream, context, Direction::NORTH); +} +// The talk to a wanderer from the step's declared pose, below his home. +// With "again" (00.05c), a talk with no question counts too: he may have +// given his coupon before, and the Poketch check after the president +// catches a coupon that is still missing. +Pokemon::Outcome talk_to_wanderer(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep& step){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + const bool again = Pokemon::story_arg(step.args, "again"); + std::optional> home; + if (step.pose_after){ + const Pokemon::Pose below = Pokemon::pose_of(*step.pose_after); + const std::vector tiles = Pokemon::tiles_of(below); + if (!tiles.empty()){ + home.emplace(below.map, tiles[0]); + } + } + const std::string label = "Along the row below " + step.name; + // Home, east, west, west, east: each move one tile on the D-pad. + const int moves[] = {0, 1, -1, -1, 1}; + int at = 0; + for (size_t round = 0; round < 6; round++){ + for (int move : moves){ + if (move != 0){ + at += step_along(stream, context, move, label); + } + if (home && asks_someone_else(story_map(home->first), {home->second.x + at, home->second.y}, home->second.next(Direction::NORTH))){ + stream.log(std::format("[Talk] {}: not asking from {} tiles east of home: a sign or someone else is north.", step.name, at)); + continue; + } + if (!ask_north(stream, context)){ + continue; + } + const bool coupon = answer_coupon_question(stream, context, step.name); + if (!coupon && !again){ + continue; + } + stream.log(std::format("[Talk] {}: {} in round {}, {} tiles east of home.", + step.name, coupon ? "the coupon" : "talked, no question (his coupon given before)", round + 1, at)); + back_below_home(stream, context, at, step.name); + return Pokemon::outcome_ok(); + } + } + fail_step(stream, step.name + ": no coupon question from any tile below his range."); +} + +// The console's side of a wait beside a wanderer: a D-pad tap a step, +// counted, and A; the talk counts when his Yes/No box opens (or, with +// "again", when any talk opens). +class ConsoleWanderWait : public WanderWaitBody{ +public: + ConsoleWanderWait(VideoStream& stream, ProControllerContext& context, std::string name, bool again) + : m_stream(stream) + , m_context(context) + , m_name(std::move(name)) + , m_again(again) + {} + virtual bool step(Direction direction) override{ + const std::string label = "Waiting for " + m_name; + route_turn(m_stream, m_context, direction); + const WalkResult result = walk_tiles_dpad(m_stream, m_context, run_from_wild_battle, route_stick(direction), 1, label); + const std::optional moved = wander_row_step(result, 1); + if (!moved){ + fail_step(m_stream, std::format("{}: {} instead of a one-tile step.", label, walk_stop_name(result.stop))); + } + return *moved != 0; + } + virtual void face(Direction direction) override{ + route_turn(m_stream, m_context, direction); + } + virtual bool ask() override{ + pbf_press_button(m_context, BUTTON_A, 80ms, 0ms); + m_context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(m_stream, m_context, 1500ms, {{dialog}}) != 0){ + return false; + } + return answer_coupon_question(m_stream, m_context, m_name) || m_again; + } + virtual void wait(Milliseconds time) override{ + m_context.wait_for(time); + } + +private: + VideoStream& m_stream; + ProControllerContext& m_context; + std::string m_name; + bool m_again; +}; +// The talk to a wanderer by waiting for him on the step's "stand" tiles, +// then on its "far_stand" tiles (his box's other end), in turn, from the +// tile the step before left us on. It ends on its declared tile, against +// a wall the declared way and one at the stand column's back (away from +// his box): both taps must not move. +Pokemon::Outcome wait_for_wanderer(StoryExecutor& executor, Pokemon::RunState& state, const Pokemon::StoryStep& step){ + VideoStream& stream = executor.stream(); + const Pokemon::GridPose& stand = Pokemon::story_arg(step.args, "stand"); + const Pokemon::GridPose& far_stand = Pokemon::story_arg(step.args, "far_stand"); + const std::vector start = Pokemon::tiles_of(state.pose); + if (!step.pose_after || start.size() != 1 || stand.tiles.empty() || !stand.facing){ + fail_step(stream, step.name + ": needs one start tile, stand tiles and their facing, and a declared end."); + } + if (!far_stand.tiles.empty() && !far_stand.facing){ + fail_step(stream, step.name + ": its far stand tiles need a facing."); + } + const Pokemon::Pose exit = Pokemon::pose_of(*step.pose_after); + const std::vector exit_tiles = Pokemon::tiles_of(exit); + const std::optional exit_wall = Pokemon::facing_of(exit); + if (exit_tiles.size() != 1 || !exit_wall){ + fail_step(stream, step.name + ": its declared end needs one tile and a facing."); + } + // The column's back: the way away from his box. + const Direction back = Pokemon::ALL_DIRECTIONS[((size_t)*stand.facing + 2) % 4]; + WanderWaitPlan plan{.sides = {{.stands = stand.tiles, .facing = *stand.facing}}, .exit = exit_tiles[0], .exit_walls = {*exit_wall, back}}; + if (!far_stand.tiles.empty()){ + plan.sides.push_back({.stands = far_stand.tiles, .facing = *far_stand.facing}); + } + ConsoleWanderWait body(stream, executor.context(), step.name, Pokemon::story_arg(step.args, "again")); + const WanderWaitResult result = wait_beside_wanderer(body, start[0], plan); + switch (result.end){ + case WanderWaitEnd::TALKED: + stream.log(std::format("[Talk] {}: talked from {}; now on {}.", step.name, Pokemon::to_string(result.talked), Pokemon::to_string(result.at))); + return Pokemon::outcome_ok(); + case WanderWaitEnd::NO_TALK: + fail_step(stream, std::format("{}: no talk from any stand tile; on {}.", step.name, Pokemon::to_string(result.at))); + case WanderWaitEnd::STUCK: + fail_step(stream, std::format("{}: a step on the way stayed blocked at {}.", step.name, Pokemon::to_string(result.at))); + case WanderWaitEnd::LOST: + fail_step(stream, std::format("{}: a step from {} moved to {}: not on the exit.", step.name, Pokemon::to_string(exit_tiles[0]), Pokemon::to_string(result.at))); + } + fail_step(stream, step.name + ": unknown wait end."); +} + +// 00.05c's end and 01.01's start: the Poketch shows on the screen's right +// (R toggles it). Without it, Jubilife's east-exit guard (the coord event +// on (60, 21..24)) asks about the Poketch campaign and turns us back. +void check_poketch(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + if (!ensure_poketch_visible(stream, context)){ + fail_step(stream, "No Poketch: the president gave none (a clown's coupon is missing?). " + "Jubilife's east-exit guard stops anyone without one: rerun 00.05c."); + } + stream.log("[Talk] The Poketch is on the screen."); +} + +// The Bag's Key Items pocket. +constexpr int KEY_ITEMS_POCKET = 8; + +// The coupons in the Key Items pocket, when the reader has their names in +// the game's language: whether each clown's is held, in +// jubilife_clown_coupons()' order. +std::optional> held_coupons(VideoStream& stream, ProControllerContext& context){ + std::vector slugs; + for (const ClownCoupon& coupon : jubilife_clown_coupons()){ + if (!bag_item_named(story_language(), coupon.slug)){ + stream.log(std::format("[Coupons] No name for {} in the game's language: not read.", coupon.slug), COLOR_ORANGE); + return std::nullopt; + } + slugs.emplace_back(coupon.slug); + } + return bag_has_items(stream, context, slugs, KEY_ITEMS_POCKET); +} + +// 00.05b's end: all three coupons in the Bag, or the row fails here +// rather than saving a run the president will give no Poketch. +void check_coupons(VideoStream& stream, ProControllerContext& context){ + const std::optional> held = held_coupons(stream, context); + if (!held){ + return; + } + const std::span coupons = jubilife_clown_coupons(); + for (size_t i = 0; i < coupons.size(); i++){ + if (!(*held)[i]){ + fail_step(stream, std::format("No {} in the Key Items: {} gave none.", coupons[i].slug, coupons[i].step)); + } + } + stream.log("[Coupons] All three coupons are in the Key Items."); +} + +// 00.05c starts against the Poketch Company's sign (14, 19) facing west +// (00.05b's end) or below the east-exit sign, (59, 21) facing north (its own end +// and 01.01's start, when a run gave no Poketch). A step west tells them +// apart: the sign blocks it, the street does not. +Pokemon::Outcome jubilife_start_probe(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + route_turn(stream, context, Direction::WEST); + const WalkResult result = walk_tiles_dpad(stream, context, run_from_wild_battle, {-1.0, 0}, 1, "00.05c start probe"); + const std::optional moved = wander_row_step(result, -1); + if (!moved){ + fail_step(stream, std::format("00.05c start probe: {} instead of a step west.", walk_stop_name(result.stop))); + } + const TilePos at = *moved == 0 ? jubilife_start_tiles().first : jubilife_start_tiles().second.next(Direction::WEST); + stream.log(std::format("[Talk] 00.05c starts {}: on {}.", *moved == 0 ? "at the Poketch Company's sign" : "on the street east", Pokemon::to_string(at))); + return Pokemon::outcome_ok(Pokemon::tile_pose("JubilifeCity", {at}, Direction::WEST)); +} + +} // namespace + + +std::optional wander_row_step(const WalkResult& result, int east){ + switch (result.stop){ + case WalkStop::TRANSITION: + case WalkStop::DIALOG: + return std::nullopt; + default: + return result.moved_chunks > 0 ? east : 0; + } +} + +bool asks_someone_else(const Pokemon::MapData& map, TilePos stand, TilePos wanderer_home){ + const TilePos north = stand.next(Direction::NORTH); + if (north == wanderer_home || !map.tiles.contains(north)){ + return false; + } + if (map.tiles.at(north) == Pokemon::TileKind::SIGN){ + return true; + } + for (const Pokemon::MapObject& object : map.objects){ + if (!object.wander && object.x == north.x && object.y == north.y){ + return true; + } + } + return false; +} + +Pokemon::TileBox jubilife_mart_clown_box(){ + return {.x0 = 54, .y0 = 32, .x1 = 56, .y1 = 34}; +} + +std::span jubilife_clown_coupons(){ + static constexpr ClownCoupon COUPONS[] = { + {.step = "mart_clown", .slug = "coupon-1"}, + {.step = "company_clown", .slug = "coupon-2"}, + {.step = "tv_clown", .slug = "coupon-3"}, + }; + return COUPONS; +} + +Pokemon::TileBox jubilife_company_clown_box(){ + return {.x0 = 14, .y0 = 18, .x1 = 16, .y1 = 18}; +} + +std::pair jubilife_start_tiles(){ + return {{14, 19}, {59, 21}}; +} + +void add_jubilife_events(StoryEvents& events){ + events.emplace("talk_to_wanderer", talk_to_wanderer); + events.emplace("wait_for_wanderer", wait_for_wanderer); + events.emplace("check_poketch", console_event(check_poketch)); + events.emplace("check_coupons", console_event(check_coupons)); + events.emplace("jubilife_start_probe", jubilife_start_probe); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp new file mode 100644 index 0000000000..2b4924116f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp @@ -0,0 +1,158 @@ +/* BDSP AutoStory - Events: the lake and the Pokedex (00.02 to 00.04) + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The scenes of the story's start that need scripted input: the briefcase + * at Lake Verity, the starter, Mom's and Barry's mother's lines, and Dawn's + * catching tutorial. The button sequences are + * Butters' (#1717); they ran on + * the console in his recordings, but not yet from the planned walks around + * them. + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/ImageMatchDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// 00.02: the lake's scene, from Barry's first line by the entrance to the +// starters' briefcase: B until the briefcase shows, then until it is open. +void starter_briefcase(VideoStream& stream, ProControllerContext& context){ + wait_for_dialogue(stream, context, "Barry before the starter selection"); + pbf_mash_button(context, BUTTON_A, 1000ms); + pbf_wait(context, 500ms); + std::shared_ptr briefcase = std::make_shared( + RESOURCE_PATH() + "PokemonBDSP/StarterBriefcase.png" + ); + ImageMatchWatcher detector(briefcase, {0.5, 0.1, 0.5, 0.7}, 100, true); + const int ret = run_until( + stream, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 120000ms); + }, + {{detector}} + ); + if (ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect the starters' briefcase.", stream); + } + stream.log("Detected briefcase! Mashing B for it to open."); + pbf_mash_button(context, BUTTON_B, 2000ms); +} + +// 00.02: the starter from the program's option, in the briefcase: Turtwig +// is first, Chimchar a press right, Piplup two; ZL picks, then Up and ZL +// answer "this one?" with Yes. +Pokemon::Outcome choose_starter(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + size_t rights = 0; + switch (executor.env().starter){ + case StarterChoice::TURTWIG: + rights = 0; + break; + case StarterChoice::CHIMCHAR: + rights = 1; + break; + case StarterChoice::PIPLUP: + rights = 2; + break; + } + for (size_t i = 0; i < rights; i++){ + pbf_press_dpad(context, DPAD_RIGHT, 160ms, 840ms); + } + pbf_press_button(context, BUTTON_ZL, 160ms, 240ms); + context.wait_for_all_requests(); + ShortDialogWatcher watcher(COLOR_CYAN); + int ret = wait_until(stream, context, 5000ms, {{watcher}}); + if (ret != 0){ + ShortDialogWatcher watcher_red(COLOR_RED); + ret = wait_until(stream, context, 5000ms, {{watcher_red}}); + } + stream.log(ret == 0 ? "[Starter] Confirmation dialogue detected." : "[Starter] Dialogue detection failed, proceeding anyway.", ret == 0 ? COLOR_BLACK : COLOR_ORANGE); + pbf_wait(context, 400ms); + pbf_press_dpad(context, DPAD_UP, 80ms, 400ms); + pbf_press_button(context, BUTTON_ZL, 80ms, 5000ms); + context.wait_for_all_requests(); + return Pokemon::outcome_ok(); +} + +// 00.01: BDSP puts an Azure Flute on the bedroom floor at (7, 4) when the console +// has Legends: Arceus save data, and nothing there otherwise. Facing that tile: +// A, and if text follows ("You found an Azure Flute!", then the pocket line), B +// through it. Without the ball the A does nothing and we go on. +void azure_flute_if_there(VideoStream& stream, ProControllerContext& context){ + // The route ends facing the way its last leg went (east, r15): a short tap + // north only turns us to face the tile. + pbf_press_dpad(context, DPAD_UP, 40ms, 400ms); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + for (int presses = 0; presses < 6; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 2000ms, {{dialog}}) != 0){ + return; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + } +} + + + +// 00.02 with Run Starter RNG in AutoStory: runs Starter RNG from the +// Verity Lakefront (17, 14) start save through the Starly battle and +// Rowan's scene, ending on the VerityLakefront overworld at (17, 14..15). +Pokemon::Outcome starter_rng(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + if (!executor.env().run_starter_rng){ + return Pokemon::outcome_failed("starter_rng_not_configured", "Starter RNG is not configured on StoryEnv."); + } + return executor.env().run_starter_rng(); +} + +// 00.04: Dawn's catching tutorial on Route 202: her lines and her demo +// battle, B for 70 s. +void catch_tutorial(VideoStream& stream, ProControllerContext& context){ + wait_for_dialogue(stream, context, "Dawn's catching tutorial"); + pbf_mash_button(context, BUTTON_B, 70000ms); + context.wait_for_all_requests(); + pbf_wait(context, 2000ms); +} + +} // namespace + + +void add_lake_events(StoryEvents& events){ + events.emplace("azure_flute_if_there", console_event(azure_flute_if_there)); + events.emplace("starter_briefcase", console_event(starter_briefcase)); + events.emplace("choose_starter", choose_starter); + events.emplace("starter_rng", starter_rng); + + + events.emplace("catch_tutorial", console_event(catch_tutorial)); + events.emplace("catch_pokemon", console_event(catch_1_pokemon)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp new file mode 100644 index 0000000000..6ce669a041 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -0,0 +1,146 @@ +/* BDSP AutoStory - Events: Oreburgh Gym (01.04) + * + * From: https://github.com/PokemonAutomation/ + * + * 01.04's walk into the Oreburgh Gym and up it to Roark, again from the + * Pokemon Center after each loss, as it ran on the console (2026-10-29 to + * 10-31). A loss keeps the experience, which a reload of the row's save + * would not. + * + */ + +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// 01.04 starts below Oreburgh's Center door (47, 21): 01.03's heal and +// the walk out after a loss both leave us there. The Gym's door is on the +// same street, so the D-pad taps west are the columns between the two +// doors on the map (21). A hand count of 20, from before the maps, ended +// a tile east of the Gym's door (lab-B, 2026-11-08 14:51). +size_t gym_taps_west_of_center(){ + const Pokemon::TilePos center = map_point("OreburghCity", "pokemon_center_door"); + const Pokemon::TilePos gym = map_point("OreburghCity", "gym_door"); + return static_cast(center.x - gym.x); +} + +void oreburgh_street_to_gym(VideoStream& stream, ProControllerContext& context){ + // From the Center's doorstep, straight west along its row to below the + // gym's door, then north through it, all on the D-pad (whole tiles; see + // gym_taps_west_of_center()). 01.04 always starts on the doorstep: its + // own save, or the walk out of the Center after a loss. A brightness + // check for "already in the gym" (gym 75 against street 104 by day, r74) + // read the street at night (88, game clock 00:25) as the gym and talked + // to the nurse as Roark (lab-B, 2026-11-08 14:25). + context.wait_for_all_requests(); + dpad_tiles(stream, context, DPAD_LEFT, gym_taps_west_of_center(), "west to the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + // The gym's wall stopped us: try the column west, then the one east. + dpad_tiles(stream, context, DPAD_LEFT, 1, "a column west of the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + dpad_tiles(stream, context, DPAD_RIGHT, 2, "a column east of the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + fail_step(stream, "Oreburgh: no column near the gym's door went in."); +} + +// Inside the Oreburgh Gym: straight up the middle, fighting the trainers +// who stop us, to Roark on the top tier. Returns whether Roark was beaten; +// false after a whiteout, which leaves us in the Pokemon Center. +// Walking straight up meets both gym trainers (one at the entrance, one on +// the second tier); their experience helps an under-levelled team, and +// after a whiteout they stay beaten. Butters' route goes round them. +bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context){ + // The entry camera pans for about 7.5 s (Butters' reference video). + context.wait_for(8000ms); + if (dialogue_opens(stream, context, 3000ms)){ + if (talk_or_fight(stream, context, "Gym trainer at the entrance") && in_pokemon_center(stream)){ + return false; + } + } + size_t stuck = 0; + for (size_t leg = 0; leg < 10; leg++){ + const WalkResult up = walk_until_no_wild(stream, context, {0, 1}, 8000ms, "north up the gym"); + if (up.stop == WalkStop::DIALOG){ + if (talk_or_fight(stream, context, "Gym trainer") && in_pokemon_center(stream)){ + return false; + } + continue; + } + if (up.stop != WalkStop::BLOCKED){ + continue; + } + // Roark stands at the top facing us; a wall or a stair edge says + // nothing to A. Only Roark starts a battle from our A here. The walk + // left us facing north: a turn tap that way would be a step. + pbf_press_button(context, BUTTON_A, 80ms, 200ms); + if (dialogue_opens(stream, context, 3000ms)){ + // His lines pause for more than 4 s before the battle (r210d: "only + // talked"), so the battle is expected. + // His badge speech is over when talk_or_fight() returns: the + // battle ends with its speeches (confirm_battle_overworld()). + if (talk_or_fight(stream, context, "Roark", TalkExpect::BATTLE)){ + return !in_pokemon_center(stream); + } + continue; + } + if (up.walked <= 800ms && ++stuck >= 3){ + fail_step(stream, "Oreburgh Gym: stuck on the way up, and nobody answers A."); + } + } + fail_step(stream, "Oreburgh Gym: did not reach Roark."); +} + +// 01.04: west along the row into the gym and up it to Roark, again from the Center after a loss. +void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context){ + // Losing costs some money and keeps the experience, so each try comes + // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and + // lost the third time. + bool won = false; + for (size_t attempt = 0; attempt < 5 && !won; attempt++){ + oreburgh_street_to_gym(stream, context); + won = oreburgh_gym_battles(stream, context); + if (!won){ + stream.log("[01.04] Lost in the gym (try " + std::to_string(attempt + 1) + "); back from the Center.", COLOR_ORANGE); + walk_out_of_center(stream, context, "Oreburgh"); + } + } + if (!won){ + fail_step(stream, "Oreburgh Gym: lost to Roark five times."); + } +} + +} // namespace + + +void add_oreburgh_events(StoryEvents& events){ + events.emplace("gym_until_roark_beaten", console_event(gym_until_roark_beaten)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp new file mode 100644 index 0000000000..f72eb04b39 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp @@ -0,0 +1,85 @@ +/* BDSP AutoStory - Events: the Ravaged Path (01.09) + * + * From: https://github.com/PokemonAutomation/ + * + * The Ravaged Path's Rock Smash, as it ran on the console (2026-10-31). + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +namespace{ + +// "This rock seems breakable with a hidden move. Would you like to use Rock +// Smash?" is open, from an A at a smashable rock. Its Yes/No box comes only +// once the text is advanced again (by hand, 2026-10-31: the text complete +// 1.2 s after the first A, the box 1.2 s after a second), and r237a's timed +// A, A, B put its B on that box: No. So press A until the box's cursor +// shows, then once more for Yes, and wait out the move. False if no box +// shows: whatever is open is not this prompt. +bool answer_rock_smash_prompt(VideoStream& stream, ProControllerContext& context, const std::string& label){ + static const ImageFloatBox YES_NO_ARROW_BOX{0.700, 0.620, 0.070, 0.120}; + SelectionArrowFinder yes_no(stream.overlay(), YES_NO_ARROW_BOX, COLOR_GREEN); + for (size_t press = 0; press < 5; press++){ + context.wait_for(1200ms); + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (screen && yes_no.detect(*screen.frame)){ + stream.log("[Route] " + label + ": Rock Smash offered; Yes."); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for(5000ms); + if (dialogue_opens(stream, context, 500ms)){ + finish_dialogue(stream, context, BUTTON_B, label + ": after Rock Smash"); + } + return true; + } + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + } + stream.log("[Route] " + label + ": no Yes/No box for Rock Smash.", COLOR_RED); + return false; +} + +// 01.09: Rock Smash on the rock east of us, from the tile the route ends on +// facing it. Only the prompt's Yes/No box tells the rock from someone in +// the way (r237a: the walker took one for a person and said No). +void smash_the_rock(VideoStream& stream, ProControllerContext& context){ + route_turn(stream, context, Direction::EAST); + if (!talk_with_nudges(stream, context, run_from_wild_battle, Direction::EAST, "the rock east of us", 2000ms)){ + fail_step(stream, "Ravaged Path: nothing answered facing the rock east of us."); + } + if (!answer_rock_smash_prompt(stream, context, "the rock east of us")){ + fail_step(stream, "Ravaged Path: no Rock Smash offer facing the rock east of us."); + } +} + +} // namespace + + +void add_route204_events(StoryEvents& events){ + events.emplace("smash_the_rock", console_event(smash_the_rock)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp new file mode 100644 index 0000000000..877fcdae15 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp @@ -0,0 +1,259 @@ +/* BDSP AutoStory - Events: Sandgem and Route 202 (00.04) + * + * From: https://github.com/PokemonAutomation/ + * + * 00.04's Potions from the Poke Mart lady on Route 201's east end, the + * way back to Route 202's save spot, and the stairs into Jubilife. + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// A walk_until() callback that keeps every location banner it sees. +std::function banner_keeper(std::vector& banners){ + return [&banners](const ImageViewRGB32& screen){ + const std::string read = read_location_banner(screen); + if (!read.empty() && (banners.empty() || banners.back() != read)){ + banners.push_back(read); + } + return false; + }; +} + +bool any_banner_names(const std::vector& banners, const std::string& map){ + return std::any_of(banners.begin(), banners.end(), + [&](const std::string& read){ return location_banner_matches(read, map); }); +} + +// Whether all of `tiles` are on `map`. +bool all_on_map(const StoryWorld& maps, const std::vector& tiles, const std::string& map){ + return !tiles.empty() && std::all_of(tiles.begin(), tiles.end(), [&](const TilePos& pos){ + const std::optional tile = maps.to_local(pos); + return tile && tile->map == map; + }); +} + +// North from below the gap between the signs until a wall stops us, +// nudging a tile west, twice at most, when blocked at once (below a +// sign or the Mart). Returns whether Route 202's banner showed: only +// one of the gap's columns leads out of town before a wall. +bool walk_north_through_gap(VideoStream& stream, ProControllerContext& context){ + using Pokemon::Direction; + std::vector banners; + for (size_t nudges = 0;; nudges++){ + const WalkResult north = walk_until(stream, context, run_from_wild_battle, {0, 1}, 8000ms, "North through the gap", banner_keeper(banners)); + if (north.stop == WalkStop::TRANSITION || north.stop == WalkStop::DIALOG || north.wild_battles > 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "North through the gap: " + std::string(walk_stop_name(north.stop)) + " instead of a wall.", stream); + } + // Blocked at once: the wall reads after about 800ms still. + if (north.walked > ROUTE_START_DELAY + 800ms + 2 * ROUTE_MS_PER_TILE_UP_DOWN){ + break; + } + if (nudges == 2){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "North through the gap: still blocked after two tiles west.", stream); + } + route_status(stream, "North through the gap: blocked at once, below a sign or the Mart; a tile west."); + route_turn(stream, context, Direction::WEST); + walk_for(stream, context, run_from_wild_battle, {-1, 0}, route_open_hold(Direction::WEST, 1), "A tile west"); + route_turn(stream, context, Direction::NORTH); + } + context.wait_for_all_requests(); + if (VideoSnapshot screen = stream.video().snapshot()){ + banner_keeper(banners)(*screen.frame); + } + return any_banner_names(banners, MAP_ROUTE_202); +} + +// From the trees at the east end of the doors' row, west into the gap +// between the Center's sign and the Mart's (below_sign_gap()) and +// north through it until a wall stops us. Six tiles west from column +// 29 ends on 23, and on 20 to 26 if two tiles off: north from all of +// those passes no door, and only the gap lets us through. A walk +// north that goes nowhere stood below a sign or the Mart: a tile +// west, twice at most, never reaches the Center's door at 17. The +// planner would rather go round the south of town on walls, where +// townspeople wander. Returns where the walk north may have ended: +// the gap's columns end on Sandgem's top row, bar one that runs on to +// Route 202, and its banner tells the two apart. +std::vector north_through_sign_gap(VideoStream& stream, ProControllerContext& context, const StoryWorld& maps){ + using Pokemon::Direction; + route_status(stream, "To the gap between the Center's and the Mart's signs: west 6."); + route_turn(stream, context, Direction::WEST); + walk_for(stream, context, run_from_wild_battle, {-1, 0}, route_open_hold(Direction::WEST, 6), "To the gap between the signs"); + const bool on_route = walk_north_through_gap(stream, context); + std::vector ends; + for (const TilePos& pos : map_region("SandgemTown", "below_sign_gap")){ + const TilePos end = slide_to_wall(maps, maps.to_world(MAP_SANDGEM_TOWN, pos), Direction::NORTH).stop; + if (all_on_map(maps, {end}, MAP_ROUTE_202) == on_route){ + ends.push_back(end); + } + } + route_status(stream, std::string("North through the gap: Route 202's banner ") + + (on_route ? "showed; on Route 202." : "did not show; on Sandgem Town's top row.")); + return ends; +} + +// Back up to Route 202's save spot under the ledge: it is where a +// retry may start (story_save_spots()), its walls pin it, and the +// trainers are north of it. First east to the trees that end the +// doors' row, so the way north starts from one known tile, then north +// through the gap between the signs: r123a, planned round the south +// of town on a map that had it wrong, walked into a house. +void walk_to_route_202_save_spot( + VideoStream& stream, ProControllerContext& context, const StoryWorld& maps, + std::vector at, RouteTrainers& trainers +){ + const TilePos row_end = maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "front_row_east_end")); + const TilePos save_spot = maps.to_world(MAP_ROUTE_202, map_point("Route202", "save_spot")); + if (!all_on_map(maps, at, MAP_ROUTE_202)){ + at = walk_route(stream, context, run_from_wild_battle, maps, std::move(at), trainers, RouteWalkPlan{[&](const TilePos& pos){ return pos == row_end; }, "To the trees east of the Poke Mart", {}, route_plan_costs()}); + at = north_through_sign_gap(stream, context, maps); + } + walk_route(stream, context, run_from_wild_battle, maps, std::move(at), trainers, RouteWalkPlan{[&](const TilePos& pos){ return pos == save_spot; }, "Back to Route 202", {}, route_plan_costs()}); +} + +// North into Jubilife until Dawn's scene starts (00.04d's +// jubilife_stairs). Jubilife's south edge is a wall with stairs in it, +// west of where the route may leave us (r78 stood at its east end). +// Entering starts a scene: Dawn walks over while we stand still. So +// walk north; if the wall stops us and no dialogue follows, the stairs +// are further west. The scene itself is the next step. +void up_jubilife_stairs(VideoStream& stream, ProControllerContext& context){ + for (size_t attempt = 0; attempt < 6; attempt++){ + const WalkResult walk = walk_until(stream, context, run_from_wild_battle, {0, 1}, 4000ms, "to Dawn in Jubilife"); + if (walk.stop == WalkStop::TRANSITION){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Jubilife: left the area instead of meeting Dawn.", stream); + } + ShortDialogWatcher dialog(COLOR_CYAN); + if (walk.stop == WalkStop::DIALOG || wait_until(stream, context, 6s, {{dialog}}) == 0){ + return; + } + stream.log("[00.04] Jubilife: the wall stopped us; looking for the stairs one tile west."); + route_turn(stream, context, Pokemon::Direction::WEST); + pbf_move_left_joystick(context, {-1, 0}, route_open_hold(Pokemon::Direction::WEST, 1), 200ms); + context.wait_for_all_requests(); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Jubilife: no stairs found and no Dawn after six tiles west.", stream); +} + +// Reads a sign facing `facing`: its box must open and close, and its text, +// when there is a reader for it, have `key`. A walk that stopped short of +// it is put right by walking on until the sign stops us, once. +void read_sign_on_the_way(VideoStream& stream, ProControllerContext& context, Direction facing, const std::string& sign, const std::string& key){ + for (size_t attempt = 0; attempt < 2; attempt++){ + const DialogueRead read = read_sign(stream, context, facing, sign); + if (read.opened && read.closed && (read.text.empty() || sign_text_matches(read.text, key))){ + return; + } + walk_until_stopped(stream, context, facing, 2, "On to the sign"); + } + fail_step(stream, "Sandgem: " + sign + " did not read facing " + Pokemon::direction_name(facing) + "."); +} + +// From the doors' row in front of Sandgem's Pokemon Center to the Poke Mart +// lady and back to Route 202's save spot. Every leg but three one-tile +// steps walks until something stops it, as measured on the console +// (2026-10-30, CFR "walk"): west to the Lab's sign; south a tile, west to +// the grass's edge; north to the trees, on her row; east until she stops +// us (she gives new games 10 Potions); south a tile, east to Route 201's +// sign; south a tile, east to the trees east of the Mart, north a row; then +// the planned walk to the save spot. She stands on the top row of her +// grass, a tile or two in from its west edge: missed, back west to the +// edge and again, twice. +void mart_lady(VideoStream& stream, ProControllerContext& context){ + walk_until_stopped(stream, context, Direction::WEST, 20, "West to the Lab's sign"); + read_sign_on_the_way(stream, context, Direction::WEST, "the Lab's sign", "lab"); + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the path"); + walk_until_stopped(stream, context, Direction::WEST, 28, "West along the path"); + walk_until_stopped(stream, context, Direction::NORTH, 3, "North to the trees"); + bool talked = false; + for (size_t attempt = 0; attempt < 3 && !talked; attempt++){ + if (attempt > 0){ + walk_until_stopped(stream, context, Direction::WEST, 6, "West to the grass's edge"); + } + const DialogueRead read = talk_walking(stream, context, Direction::EAST, 4, "Poke Mart lady"); + talked = read.opened && read.closed; + } + if (!talked){ + fail_step(stream, "Route 201: the Poke Mart lady did not talk."); + } + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the signs' row"); + walk_until_stopped(stream, context, Direction::EAST, 16, "East to the Route 201 sign"); + read_sign_on_the_way(stream, context, Direction::EAST, "Route 201's sign", "201"); + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the path"); + walk_until_stopped(stream, context, Direction::EAST, 40, "East along the path"); + walk_until_stopped(stream, context, Direction::NORTH, 2, "North to the front row"); + const StoryWorld& maps = story_maps(); + RouteTrainers trainers{story_trainer_sights(), [&](const std::string& name){ return fight_route_trainer(stream, context, name); }}; + walk_to_route_202_save_spot(stream, context, maps, {maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "front_row_east_end"))}, trainers); +} + +// 00.04c's potions step, from the save out of Sandgem's Center: the Poke +// Mart lady's 10, on to Route 202's save spot, where 00.04d's save goes. +// The Bag must have Potions after. +void sandgem_potions(VideoStream& stream, ProControllerContext& context){ + // Onto the doors' row first: out of the door we stand a tile to + // three below it (r261 walked west into the trees from three). + route_turn(stream, context, Direction::EAST); + walk_tiles_dpad(stream, context, run_from_wild_battle, {1, 0}, 1, "east under the Center's wall"); + const WalkResult up = walk_until(stream, context, run_from_wild_battle, {0, 1}, 3000ms, "north to the Center's front"); + if (up.stop != WalkStop::BLOCKED){ + fail_step(stream, std::string("Sandgem: north to the Center's front ended ") + walk_stop_name(up.stop) + "."); + } + mart_lady(stream, context); + if (!bag_has_item(stream, context, "potion", 1)){ + fail_step(stream, "00.04: no Potions in the Bag after the shop."); + } +} + +} // namespace + + +void add_sandgem_events(StoryEvents& events){ + events.emplace("sandgem_potions", console_event(sandgem_potions)); + events.emplace("jubilife_stairs", console_event(up_jubilife_stairs)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h new file mode 100644 index 0000000000..ada565e660 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h @@ -0,0 +1,47 @@ +/* BDSP AutoStory - Intro probe + * + * From: https://github.com/PokemonAutomation/ + * + * Telling the player is in control from how X and B change the screen, + * when X opens nothing the menu detector knows. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroProbe_H +#define PokemonAutomation_PokemonBDSP_AutoStory_IntroProbe_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace detail{ + + +// When X opens nothing the menu detector knows, the player is still in +// control if the screen held still for INTRO_STILL_TIME before X (no +// dialogue, no fade, no intro sparkles), X changed the menu's area by more +// than INTRO_X_CHANGE, and B put it back within INTRO_B_RESTORE: the menu +// opened and closed. Changes are pixel RMSD. +inline constexpr std::chrono::milliseconds INTRO_STILL_TIME = std::chrono::seconds(2); +// Maximum pixel RMSD threshold for considering the screen still before probing menu. +inline constexpr double INTRO_STILL_CHANGE = 4; +// Minimum pixel RMSD change indicating menu opened after pressing X. +inline constexpr double INTRO_X_CHANGE = 25; +// Maximum pixel RMSD difference between pre-X screen and post-B screen. +inline constexpr double INTRO_B_RESTORE = 10; +// Probe metrics for detecting menu presence via screen changes from X and B. +struct XProbe{ + double still_change; // over INTRO_STILL_TIME before X + double x_change; // from before X to after it + double b_change; // from before X to after the B that follows +}; +// Evaluates whether probe metrics indicate player is in control without menu detection. +bool intro_in_control_without_menu(const XProbe& probe); + + +} // namespace detail +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h new file mode 100644 index 0000000000..d91340e93c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h @@ -0,0 +1,111 @@ +/* BDSP AutoStory - Intro steps + * + * From: https://github.com/PokemonAutomation/ + * + * What the intro does on each screen, decided from the screen and how far + * the intro has got only, so it is tested without a console. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What the intro does on a screen. Decided from the screen and how far the intro has +// got only, so it is tested without a console. +enum class IntroStep{ + SELECT_USER, // A on the Switch's user select + FAIL_NOT_IN_GAME, // HOME is showing + FAIL_NOT_BDSP, // no BDSP screen within INTRO_TIME_BEFORE_BDSP + FAIL_PICK_MISSED, // the player's keyboard, with a pick still to make + PICK_LANGUAGE, // the language select: move to the game's language, A + PICK_LOOK, // the look select: move to the chosen look, A + ENTER_PLAYER_NAME, // type the name on the first keyboard + CANCEL_RIVAL_KEYBOARD, // X: back to the rival's name list + MOVE_TO_PRESET, // Down: off "I'll tell you..." + PICK_PRESET, // A on a preset name + CLOSE_MENU_AND_FINISH, // B: the menu opened in the bedroom, the intro is over + ADVANCE, // A: dialogue, language, appearance, cutscenes + ADVANCE_WITH_B, // B: dialogue once the rival is named + CLEAR_WITH_B, // B: an unknown screen once the rival is named + PROBE_FOR_MENU, // X, to see whether the player is in control + WAIT, // nothing: a black screen (the boot, a fade) +}; +// Progress tracking state for the new-game introductory sequence. +struct IntroProgress{ + // Picks still to make on the language select and the appearance grid. + // The first look needs no menu: A takes it. + bool language_pending = false; + bool look_pending = false; + bool player_named = false; + // A preset name was picked from the rival's list, and how many screens + // have shown since without the list coming back. "Barry, is it?" and + // its Yes/No follow, and A (Yes, where the cursor starts) confirms it; + // B would answer No and bring the list back (r4, 2026-11-01). + bool rival_picked = false; + size_t screens_after_pick = 0; + // The name is final: INTRO_SCREENS_AFTER_RIVAL_PICK screens passed + // without the list. + bool rival_named = false; + // A screen only BDSP shows has been seen (intro_screen_confirms_bdsp()). + bool game_confirmed = false; + // When the first screen that is not black showed, since the intro + // started; none yet while the game boots. + std::optional first_shown = std::nullopt; + // How long screens have shown without one only BDSP shows: the logos, + // the title, the language select. + std::chrono::milliseconds unconfirmed_for = std::chrono::milliseconds(0); + // B presses on unknown screens since the last X. + size_t b_presses = 0; +}; +// Whether `screen` (classify_intro_screen()) is one only BDSP's intro shows: +// its dialogue box, its language and look selects, the rival's name list, its main menu. The keyboard and +// the user select are the Switch's. +bool intro_screen_confirms_bdsp(std::string_view screen); + +// Decides the next controller action based on the current screen and intro progress. +IntroStep decide_intro_step(std::string_view screen, const IntroProgress& progress); + +namespace detail{ +// How long the intro presses A on unknown screens, from the first screen +// that is not black, before a BDSP screen must show: the logos, the title +// and the language select. Another game launched by mistake (r1, +// 2026-11-01: FireRed) fails here instead of being played. Black screens +// take no presses: BDSP boots on black for about 25 s (r3, 2026-11-01). +inline constexpr std::chrono::milliseconds INTRO_TIME_BEFORE_BDSP = std::chrono::seconds(60); +// How long the boot may stay black after the restart. +inline constexpr std::chrono::milliseconds INTRO_BOOT_LIMIT = std::chrono::seconds(60); +// Screens after the rival's name is picked, each taking A, before the name +// counts as confirmed and B takes over: "Barry, is it?", the box closing +// (Barry alone), the Yes/No, and a few of Rowan's lines after it. +inline constexpr size_t INTRO_SCREENS_AFTER_RIVAL_PICK = 8; +// Once the rival is named, the intro only has dialogue and cutscenes left +// before the bedroom, and screens the classifier misses (r2: a dialogue box +// without its arrow). Each unknown screen takes B, which advances dialogue, +// never starts a talk and does nothing in the bedroom; after this many, X +// checks for the menu, and only the menu ends the intro. +inline constexpr size_t INTRO_B_BEFORE_X = 5; +// X presses that opened no menu before the intro gives up: with +// INTRO_B_BEFORE_X B presses between them, about two minutes. +inline constexpr size_t INTRO_X_WITHOUT_MENU = 20; +// Counts `step` in `progress`. +void record_intro_step(IntroProgress& progress, IntroStep step); +// Notes `screen`, read `since_start` into the intro, in `progress`: the +// game is confirmed on a BDSP screen; black screens leave the clock alone. +void note_intro_screen(IntroProgress& progress, std::string_view screen, std::chrono::milliseconds since_start); +} // namespace detail + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h new file mode 100644 index 0000000000..217081dc4a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h @@ -0,0 +1,84 @@ +/* BDSP AutoStory - Lit doorway + * + * From: https://github.com/PokemonAutomation/ + * + * A building's lit doorway and the street lamps seen on the overworld, and + * the tiles the player may stand on, read off them. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_LitDoorway_H +#define PokemonAutomation_PokemonBDSP_AutoStory_LitDoorway_H + +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A building's lit doorway seen on the overworld, relative to the player, +// whom the camera keeps in the middle of the screen. +struct LitDoorway{ + double x; // centre across the screen, 0 to 1 + double y; // centre down the screen, 0 to 1 + double tiles_across; // east of the player if positive, west if negative + double rows_up; // rows north of the player's tile, roughly: 1 straight ahead +}; + +// Whether `screen` is the inside of a Pokemon Center: the orange back wall +// along the top, and either the black below the entrance mat along the +// bottom edge (seen from the door) or the nurse's counter close up. Its lit +// floor reads as a doorway (find_lit_doorway()), so this is what says we +// are in (r126 runs 2, 3). +bool pokemon_center_inside(const ImageViewRGB32& screen); + +// Finds a lit doorway (a Pokemon Center's or a Poke Mart's) in the upper +// two thirds of `screen`, the pale yellow-white of its open door. Nothing if +// none shows there, or inside a Pokemon Center, whose floor is that colour. +std::optional find_lit_doorway(const ImageViewRGB32& screen); + +// A lit street lamp's head inside `box` (0 to 1 of the screen): the mean +// x of its white core, or nothing if fewer than a lamp's worth of white +// pixels show there (by day the lamps are not lit). +std::optional find_lit_lamp_x(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// How many tiles a thing seen at screen x is across from the player +// (positive: east). +double tiles_across_at(double screen_x); + +// Moves across by `tiles`, east if positive, west if negative, facing that +// way first. +void move_across(VideoStream& stream, ProControllerContext& context, double tiles); + +// The tiles a player who sees `doorway` (the door on world tile `door`) may +// stand on: the one its offset says, give or take one each way, since the +// rows are a rough count. Only standable tiles of `grid`. +std::vector tiles_by_lit_doorway( + const LitDoorway& doorway, const StoryWorld& grid, const Pokemon::TilePos& door +); + +// For a player who walked to the row below the door on world tile `door`: +// if its lit doorway shows further up the screen than straight ahead, the +// walk ended short (someone in the way stops an open leg, and the walk +// cannot tell), and the tiles he may stand on instead, read off the +// doorway's offset, are returned to walk from again. Empty when he is at +// the front row, or no doorway shows. +std::vector tiles_short_of_door( + VideoStream& stream, ProControllerContext& context, + const StoryWorld& grid, const Pokemon::TilePos& door, const std::string& label +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp new file mode 100644 index 0000000000..e9217ef552 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp @@ -0,0 +1,606 @@ +/* BDSP AutoStory - New Game + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/ScreenMachine/ScreenMachine.h" +#include "CommonTools/ScreenMachine/StepTrace.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_NameListDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h" +#include "PokemonBDSP_AutoStory_IntroProbe.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" +#include "PokemonBDSP_AutoStory_NewGame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using namespace detail; + + +const std::vector& bdsp_language_menu(){ + static const std::vector menu{ + Language::English, Language::Japanese, + Language::Spanish, Language::Japanese, + Language::French, Language::Korean, + Language::German, Language::ChineseSimplified, + Language::Italian, Language::ChineseTraditional, + }; + return menu; +} + +namespace{ + +std::optional language_item(Language language){ + const std::vector& menu = bdsp_language_menu(); + const auto it = std::find(menu.begin(), menu.end(), language); + if (it == menu.end()){ + return std::nullopt; + } + return (size_t)(it - menu.begin()); +} + +} // namespace + +std::optional> language_pick_moves(size_t cursor, const IntroPicks& picks){ + const std::optional to = language_item(picks.game_language); + if (!to){ + return std::nullopt; + } + return menu_cursor_moves(BDSP_LANGUAGE_GRID, cursor, *to); +} +std::optional> look_pick_moves(size_t cursor, const IntroPicks& picks){ + return menu_cursor_moves(BDSP_LOOK_GRID, cursor, picks.look); +} + +namespace detail{ +IntroProgress intro_start(const IntroPicks& picks){ + IntroProgress progress; + progress.language_pending = true; + progress.look_pending = picks.look != 0; + return progress; +} +} // namespace detail + + +std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& screen){ + // The Switch's own screens come first: BDSP's dialogue detector also fires on them. + if (console != nullptr && StartGameUserSelectDetector(*console).detect(screen)){ + return "user-select"; + } + if (console != nullptr && HomeMenuDetector(*console, COLOR_RED).detect(screen)){ + return "home"; + } + if (SystemKeyboardDetector().detect(screen)){ + return "keyboard"; + } + // Before the dialogue box: its prompt sits in one. + if (language_select_cursor(screen)){ + return "language-select"; + } + if (look_select_cursor(screen)){ + return "look-select"; + } + switch (NameListDetector().selected_row(screen)){ + case -1: + break; + case 0: + return "rival-list-tell"; + default: + return "rival-list-preset"; + } + if (MenuDetector(COLOR_RED, /*skip_new_banner*/ true).detect(screen)){ + return "menu"; + } + // Includes the appearance grid, whose prompt sits in a dialogue box. + if (ShortDialogDetector().detect(screen)){ + return "dialog"; + } + // The boot and fades. After the dialogue: Rowan's first lines show on black. + if (BlackScreenDetector().detect(screen)){ + return "black"; + } + // Logos, the title, language, cutscenes, the bedroom with no menu open. + return "other"; +} + +IntroStep decide_intro_step(std::string_view screen, const IntroProgress& progress){ + if (screen == "user-select"){ + return IntroStep::SELECT_USER; + } + if (screen == "home"){ + return IntroStep::FAIL_NOT_IN_GAME; + } + if (screen == "keyboard"){ + if (!progress.player_named && (progress.language_pending || progress.look_pending)){ + return IntroStep::FAIL_PICK_MISSED; + } + return progress.player_named ? IntroStep::CANCEL_RIVAL_KEYBOARD : IntroStep::ENTER_PLAYER_NAME; + } + if (screen == "language-select"){ + // Once picked, A again only if the select still shows. + return progress.language_pending ? IntroStep::PICK_LANGUAGE : IntroStep::ADVANCE; + } + if (screen == "look-select"){ + return progress.look_pending ? IntroStep::PICK_LOOK : IntroStep::ADVANCE; + } + if (screen == "rival-list-tell"){ + return IntroStep::MOVE_TO_PRESET; + } + if (screen == "rival-list-preset"){ + return IntroStep::PICK_PRESET; + } + if (screen == "menu"){ + return IntroStep::CLOSE_MENU_AND_FINISH; + } + if (screen == "black"){ + return IntroStep::WAIT; + } + if (screen == "dialog"){ + return progress.rival_named ? IntroStep::ADVANCE_WITH_B : IntroStep::ADVANCE; + } + // The bedroom comes only after the rival is named. Before that, every unknown + // screen (logos, the title, language, the appearance preview, cutscenes) + // takes A, but only for so long before a screen only BDSP shows: past that + // it is another game. + if (progress.rival_named){ + return progress.b_presses < INTRO_B_BEFORE_X ? IntroStep::CLEAR_WITH_B : IntroStep::PROBE_FOR_MENU; + } + if (!progress.game_confirmed && progress.unconfirmed_for >= INTRO_TIME_BEFORE_BDSP){ + return IntroStep::FAIL_NOT_BDSP; + } + return IntroStep::ADVANCE; +} +bool intro_screen_confirms_bdsp(std::string_view screen){ + return screen == "dialog" || screen == "language-select" || screen == "look-select" || screen == "rival-list-tell" || screen == "rival-list-preset" || screen == "menu"; +} + +namespace detail{ +void note_intro_screen(IntroProgress& progress, std::string_view screen, std::chrono::milliseconds since_start){ + if (intro_screen_confirms_bdsp(screen)){ + progress.game_confirmed = true; + } + if (screen == "black"){ + return; + } + if (!progress.first_shown){ + progress.first_shown = since_start; + } + if (!progress.game_confirmed){ + progress.unconfirmed_for = since_start - *progress.first_shown; + } +} + +bool intro_in_control_without_menu(const XProbe& probe){ + return probe.still_change <= INTRO_STILL_CHANGE && probe.x_change > INTRO_X_CHANGE && probe.b_change <= INTRO_B_RESTORE; +} + +void record_intro_step(IntroProgress& progress, IntroStep step){ + switch (step){ + case IntroStep::ENTER_PLAYER_NAME: + progress.player_named = true; + return; + case IntroStep::MOVE_TO_PRESET: + // The list again: the name was not confirmed. + progress.rival_picked = false; + progress.screens_after_pick = 0; + return; + case IntroStep::PICK_PRESET: + progress.rival_picked = true; + progress.screens_after_pick = 0; + return; + case IntroStep::ADVANCE: + if (progress.rival_picked && ++progress.screens_after_pick >= INTRO_SCREENS_AFTER_RIVAL_PICK){ + progress.rival_named = true; + } + return; + case IntroStep::CLEAR_WITH_B: + progress.b_presses++; + return; + case IntroStep::PROBE_FOR_MENU: + progress.b_presses = 0; + return; + default: + return; + } +} +} // namespace detail + +namespace{ + +// Waits until the keyboard is gone (or, with `gone` false, has appeared). +bool wait_for_keyboard(VideoStream& stream, ProControllerContext& context, bool gone, Milliseconds timeout){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + while (current_time() < deadline){ + VideoSnapshot screen = stream.video().snapshot(); + if (screen && SystemKeyboardDetector().detect(screen) != gone){ + return true; + } + context.wait_for(100ms); + } + return false; +} + +void enter_player_name(VideoStream& stream, ProControllerContext& context, StepTrace& trace, const std::string& name){ + // Let the keyboard finish sliding in: inputs during that are dropped. + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + // B deletes a character, and closes the keyboard on an empty field, so delete only + // while there is text (e.g. after a retry). + for (size_t c = 0; c < 14; c++){ + VideoSnapshot screen = stream.video().snapshot(); + if (!screen || !system_keyboard_field_has_text(screen)){ + break; + } + trace.action("Name field holds text; deleting a character."); + pbf_press_button(context, BUTTON_B, 60ms, 250ms); + context.wait_for_all_requests(); + } + trace.action("Typing the player's name: " + name); + type_on_system_keyboard(stream, context, name); + pbf_press_button(context, BUTTON_PLUS, 80ms, 0ms); + if (!wait_for_keyboard(stream, context, true, 5s)){ + trace.fail("The keyboard stayed open after PLUS (OK)."); + } + trace.action("Name confirmed."); +} + +// A run of the intro: what its steps act on and how far it has got. +struct IntroRun{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + StepTrace& trace; + const IntroPicks& picks; + const std::string& player_name; + WallClock start; + IntroProgress progress; + size_t x_without_menu = 0; + // X opened something and B closed it, from a still screen. + bool in_control = false; +}; + +void press(IntroRun& run, Button button){ + pbf_press_button(run.context, button, 80ms, 0ms); +} + +void close_menu_and_finish(IntroRun& run){ + run.trace.action("Main menu is open: the player is in control in the bedroom. Closing it."); + press(run, BUTTON_B); + pbf_wait(run.context, 1000ms); + run.context.wait_for_all_requests(); + VideoSnapshot screen_now = run.env.console.video().snapshot(); + if (!screen_now || MenuDetector(COLOR_RED, /*skip_new_banner*/ true).detect(screen_now)){ + run.trace.fail("The main menu did not close."); + } +} + +// The menu's area, which X changes when the menu opens. +const ImageFloatBox INTRO_MENU_AREA(0.12, 0.08, 0.76, 0.56); + +double menu_area_change(const VideoSnapshot& before, const VideoSnapshot& after){ + if (!before || !after){ + return 1000; + } + return ImageMatch::pixel_RMSD( + extract_box_reference(*before.frame, INTRO_MENU_AREA), extract_box_reference(*after.frame, INTRO_MENU_AREA) + ); +} + +enum class XProbeResult{ + MENU, // the menu showed + IN_CONTROL, // something X opened and B closed, from a still screen + NOTHING, +}; +// X from a screen that held still; B after it if no menu showed. +XProbeResult x_from_still_screen(IntroRun& run){ + VideoStream& stream = run.env.console; + run.context.wait_for_all_requests(); + const VideoSnapshot first = stream.video().snapshot(); + run.context.wait_for(INTRO_STILL_TIME); + const VideoSnapshot before = stream.video().snapshot(); + press(run, BUTTON_X); + run.context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, run.context, 3s, {{menu}}) == 0){ + return XProbeResult::MENU; + } + const VideoSnapshot after_x = stream.video().snapshot(); + pbf_press_button(run.context, BUTTON_B, 80ms, 1000ms); + run.context.wait_for_all_requests(); + const VideoSnapshot after_b = stream.video().snapshot(); + const XProbe probe{menu_area_change(first, before), menu_area_change(before, after_x), menu_area_change(before, after_b)}; + run.trace.action("X probe: still " + std::to_string(probe.still_change) + ", X " + std::to_string(probe.x_change) + + ", after B " + std::to_string(probe.b_change) + "."); + return intro_in_control_without_menu(probe) ? XProbeResult::IN_CONTROL : XProbeResult::NOTHING; +} + +// X, then the menu or B presses again: whatever X opened instead, B closes. +// X opening something unrecognized from a still screen, which B closes +// again, is the player in control too. +void probe_for_menu(IntroRun& run){ + const XProbeResult result = x_from_still_screen(run); + run.in_control = result == XProbeResult::IN_CONTROL; + if (result != XProbeResult::NOTHING){ + return; // The next read sees the menu, or the in-control state, and finishes. + } + run.trace.action("X opened no menu; B again before the next X."); + if (++run.x_without_menu >= INTRO_X_WITHOUT_MENU){ + run.trace.fail("No menu after " + std::to_string(run.x_without_menu) + " X presses: the player never got control."); + } +} + +void advance(IntroRun& run){ + pbf_press_button(run.context, BUTTON_A, 60ms, 400ms); +} + +// One of the intro's two menus: its shape and its cursor reader. +struct IntroMenu{ + std::string_view what; + MenuGrid grid; + std::optional (*read)(const ImageViewRGB32& screen); +}; +const IntroMenu LANGUAGE_MENU{"language", BDSP_LANGUAGE_GRID, language_select_cursor}; +const IntroMenu LOOK_MENU{"look", BDSP_LOOK_GRID, look_select_cursor}; + +// Reads of a menu before its cursor must be on the pick. +const size_t INTRO_PICK_TRIES = 3; + +// The menu's cursor as read now; nullopt when the menu is not showing. +std::optional read_cursor(IntroRun& run, const IntroMenu& menu){ + run.context.wait_for_all_requests(); + const VideoSnapshot screen = run.env.console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return menu.read(*screen.frame); +} + +// Reads the cursor, moves it to `target` and reads it there, up to +// INTRO_PICK_TRIES times (a press dropped as the screen fades in), then +// saves a screenshot and presses A. Whether it picked: false when the menu +// went away, and the next read decides. +bool pick_on_grid(IntroRun& run, const IntroMenu& menu, size_t target){ + for (size_t attempt = 0; attempt < INTRO_PICK_TRIES; attempt++){ + const std::optional cursor = read_cursor(run, menu); + if (!cursor){ + return false; + } + run.trace.action(std::string("Picking the ") + std::string(menu.what) + ": cursor on item " + std::to_string(*cursor) + + ", the pick is item " + std::to_string(target) + "."); + if (*cursor == target){ + run.trace.screen(std::string("pick-") + std::string(menu.what)); + press(run, BUTTON_A); + pbf_wait(run.context, 500ms); + return true; + } + press_cursor_moves(run.context, *menu_cursor_moves(menu.grid, *cursor, target)); + } + run.trace.fail(std::string("The ") + std::string(menu.what) + " menu's cursor did not reach its pick in " + + std::to_string(INTRO_PICK_TRIES) + " tries."); +} + +// PICK_LANGUAGE or PICK_LOOK. +void pick_on_intro_menu(IntroRun& run, IntroStep step){ + if (step == IntroStep::PICK_LANGUAGE){ + if (pick_on_grid(run, LANGUAGE_MENU, *language_item(run.picks.game_language))){ + run.progress.language_pending = false; + } + }else if (pick_on_grid(run, LOOK_MENU, run.picks.look)){ + run.progress.look_pending = false; + } +} + +// Notes `screen` in the run's progress, decides the step for it and counts it. +IntroStep next_intro_step(IntroRun& run, std::string_view screen){ + note_intro_screen(run.progress, screen, + std::chrono::duration_cast(current_time() - run.start)); + const IntroStep step = decide_intro_step(screen, run.progress); + record_intro_step(run.progress, step); + return step; +} + +// Stops the intro for one of the FAIL_ steps. +[[noreturn]] void fail_intro(const IntroRun& run, IntroStep step){ + if (step == IntroStep::FAIL_NOT_IN_GAME){ + run.trace.fail("The Switch HOME menu is showing: the game is not running in front."); + } + if (step == IntroStep::FAIL_NOT_BDSP){ + run.trace.fail("The game in front is not BDSP: no BDSP screen in " + + std::to_string(run.progress.unconfirmed_for.count() / 1000) + " s of unknown screens."); + } + run.trace.fail(std::string("The player's name keyboard showed before the ") + + (run.progress.language_pending ? "language select" : "appearance grid") + + " was read."); +} + +// Carries out what decide_intro_step() chose for `screen`. +void act(IntroRun& run, std::string_view screen){ + const IntroStep step = next_intro_step(run, screen); + switch (step){ + case IntroStep::SELECT_USER: + // The cursor starts on the user that launched the game. + press(run, BUTTON_A); + pbf_wait(run.context, 1000ms); + return; + case IntroStep::FAIL_NOT_IN_GAME: + case IntroStep::FAIL_NOT_BDSP: + case IntroStep::FAIL_PICK_MISSED: + fail_intro(run, step); + case IntroStep::PICK_LANGUAGE: + case IntroStep::PICK_LOOK: + pick_on_intro_menu(run, step); + return; + case IntroStep::ENTER_PLAYER_NAME: + enter_player_name(run.env.console, run.context, run.trace, run.player_name); + return; + case IntroStep::CANCEL_RIVAL_KEYBOARD: + // Opened from "I'll tell you..."; inputs during the slide-in are dropped. + pbf_wait(run.context, 800ms); + press(run, BUTTON_X); + return; + case IntroStep::MOVE_TO_PRESET: + pbf_press_dpad(run.context, DPAD_DOWN, 100ms, 300ms); + return; + case IntroStep::PICK_PRESET: + press(run, BUTTON_A); + pbf_wait(run.context, 500ms); + return; + case IntroStep::CLOSE_MENU_AND_FINISH: + close_menu_and_finish(run); + return; + case IntroStep::ADVANCE: + advance(run); + return; + case IntroStep::ADVANCE_WITH_B: + case IntroStep::CLEAR_WITH_B: + pbf_press_button(run.context, BUTTON_B, 60ms, 400ms); + return; + case IntroStep::PROBE_FOR_MENU: + probe_for_menu(run); + return; + case IntroStep::WAIT: + pbf_wait(run.context, 500ms); + return; + } +} + +// After the restart BDSP boots on black for about 25 s (r3); presses then +// would count against the wrong-game guard, so none until it ends. +void wait_out_boot(VideoStream& stream, ProControllerContext& context, StepTrace& trace){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + INTRO_BOOT_LIMIT; + while (current_time() < deadline){ + VideoSnapshot screen = stream.video().snapshot(); + if (screen && !BlackScreenDetector().detect(screen)){ + trace.action("The boot's black screen ended."); + return; + } + context.wait_for(500ms); + } + trace.fail("The screen stayed black for " + std::to_string(INTRO_BOOT_LIMIT.count() / 1000) + " s after the restart."); +} + +// The machine's states: the screens classify_intro_screen() tells apart, with +// the keyboard split by whose name it asks for. Each state's step is act(). +std::vector intro_states(IntroRun& run){ + auto screen_is = [&run](const ImageViewRGB32& screen, const std::string& name){ + return classify_intro_screen(&run.env.console, screen) == name; + }; + return { + {"in-control", + [&run](const ImageViewRGB32&){ return run.in_control; }, nullptr, + [&run]{ run.trace.action("X opened and B closed the menu from a still bedroom: the player is in control."); }, + {}, 1, true}, + {"user-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "user-select"); }, nullptr, + [&run]{ act(run, "user-select"); }, {}, 3}, + {"home", + [=](const ImageViewRGB32& s){ return screen_is(s, "home"); }, nullptr, + [&run]{ act(run, "home"); }, {}, 1}, + {"player-keyboard", + [=, &run](const ImageViewRGB32& s){ return !run.progress.player_named && screen_is(s, "keyboard"); }, nullptr, + [&run]{ act(run, "keyboard"); }, {"dialog", "black", "other"}, 1}, + {"rival-keyboard", + [=, &run](const ImageViewRGB32& s){ return run.progress.player_named && screen_is(s, "keyboard"); }, nullptr, + [&run]{ act(run, "keyboard"); }, {"rival-list-tell", "dialog", "black", "other"}, 3}, + {"language-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "language-select"); }, nullptr, + [&run]{ act(run, "language-select"); }, {}, 10}, + {"look-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "look-select"); }, nullptr, + [&run]{ act(run, "look-select"); }, {}, 10}, + {"rival-list-tell", + [=](const ImageViewRGB32& s){ return screen_is(s, "rival-list-tell"); }, nullptr, + [&run]{ act(run, "rival-list-tell"); }, {"rival-list-preset", "black", "other"}, 6}, + {"rival-list-preset", + [=](const ImageViewRGB32& s){ return screen_is(s, "rival-list-preset"); }, nullptr, + [&run]{ act(run, "rival-list-preset"); }, {"dialog", "black", "other"}, 6}, + {"menu", + [=](const ImageViewRGB32& s){ return screen_is(s, "menu"); }, nullptr, + [&run]{ act(run, "menu"); }, {}, 2, true}, + {"dialog", + [=](const ImageViewRGB32& s){ return screen_is(s, "dialog"); }, nullptr, + [&run]{ act(run, "dialog"); }, {}, 400}, + // The boot is waited out before the machine starts; this is fades. + {"black", + [=](const ImageViewRGB32& s){ return screen_is(s, "black"); }, nullptr, + [&run]{ act(run, "black"); }, {}, 300, false, 60}, + {"other", + [](const ImageViewRGB32&){ return true; }, nullptr, + [&run]{ act(run, "other"); }, {}, 300, false, 60}, + }; +} + +} // namespace + + + +void play_new_game_intro( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const IntroPicks& picks, + const std::string& player_name +){ + VideoStream& stream = env.console; + StepTrace trace(stream, "Intro"); + + if (!language_pick_moves(0, picks)){ + throw UserSetupError(env.console, "BDSP's language select lacks the game's language: pick one it lists."); + } + if (!look_pick_moves(0, picks)){ + throw UserSetupError(env.console, "The player's look must be one of BDSP's eight."); + } + + // The restart below reopens the game HOME's cursor is on, which is the + // game running in front only while it runs: with none running (r1, + // 2026-11-01), closing it fails, HOME is pressed again, the cursor goes + // to the first tile, and another game was launched. So BDSP must be + // running in front when the intro starts. + context.wait_for_all_requests(); + VideoSnapshot start = stream.video().snapshot(); + if (start && HomeMenuDetector(env.console, COLOR_RED).detect(start)){ + throw UserSetupError(env.console, + "Start a new game with BDSP running in front, not from the Switch's HOME menu: " + "the intro restarts the game in front."); + } + + // Only close and reopen the game: the states below take it from the user select + // on. reset_game_from_home() waits for a loaded save's black screen to end, and a + // new game's first screen is a dialog on black, so it would sit there until its + // timeout. + trace.action("Restarting the game."); + go_home(env.console, context); + from_home_close_and_reopen_game(env.console, context, true); + + wait_out_boot(stream, context, trace); + IntroRun run{env, context, trace, picks, player_name, current_time(), intro_start(picks), 0}; + trace.action(std::string("Picks: language ") + language_data(picks.game_language).name + + ", look " + std::to_string(picks.look + 1) + "."); + const std::vector states = intro_states(run); + run_screen_machine(stream, context, states, std::chrono::minutes(15), trace); + trace.action("Intro done."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h new file mode 100644 index 0000000000..4a26600902 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h @@ -0,0 +1,95 @@ +/* BDSP AutoStory - New Game + * + * From: https://github.com/PokemonAutomation/ + * + * Plays a new game's intro (language, Professor Rowan, appearance, names) up to + * the bedroom with the player in control, which is where the Tutorial starts. + * The language and the player's look are picked from their menus by index + * (NintendoSwitch_MenuCursor.h); the menus' shapes are the data below. + * Both menus are read by their detector (PokemonBDSP_AutoStory_IntroMenuDetector.h). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_NewGame_H +#define PokemonAutomation_PokemonBDSP_AutoStory_NewGame_H + +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The language select: two columns of five buttons, English, Espanol, +// Francais, Deutsch, Italiano on the left; Japanese in kana, Japanese in +// kanji, Korean, Simplified and Traditional Chinese on the right (lab-B +// 2026-11-09). Items are numbered row by row, as MenuGrid reads them. +inline constexpr MenuGrid BDSP_LANGUAGE_GRID{2, 10}; +// The language of each item of BDSP_LANGUAGE_GRID. Japanese is there twice; +// its first item, the kana, is the one picked. +const std::vector& bdsp_language_menu(); +// The look select ("Choose your photo."): two rows of four portraits, the +// cursor on the first (lab-B 2026-11-09). PlayerModelOption's "Model N" is +// item N - 1, row by row: Lucas light, Lucas dark, Dawn dark-haired, Dawn +// dark; Lucas blond, Lucas brown, Dawn blond, Dawn brown-haired. A on it +// goes straight to the name keyboard. +inline constexpr MenuGrid BDSP_LOOK_GRID{4, 8}; + +// What the intro picks on its menus. +struct IntroPicks{ + Language game_language = Language::English; + // An item of BDSP_LOOK_GRID. + size_t look = 0; +}; +// The presses from the language select's item `cursor` to the game's +// language; nullopt when the menu lacks it or `cursor` is outside it. +std::optional> language_pick_moves(size_t cursor, const IntroPicks& picks); +// The presses from the look select's item `cursor` to the chosen look; +// nullopt when either is outside the grid. +std::optional> look_pick_moves(size_t cursor, const IntroPicks& picks); + + +// Which intro screen `screen` shows, as the intro's steps tell them apart: +// "user-select", "home", "keyboard", "language-select", "look-select", "rival-list-tell" (the rival's name list on +// "I'll tell you..."), "rival-list-preset" (on a preset name), "menu", "dialog" or +// "black" (the boot, fades) or "other". The Switch's own screens need +// `console`; without it they read as whatever else they look like. +std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& screen); + +namespace detail{ +// The progress a new intro starts from: which of `picks` need a menu. +IntroProgress intro_start(const IntroPicks& picks); +} // namespace detail + +// From the title screen or any intro screen of a game with no save, with BDSP +// running in front: from HOME it throws UserSetupError, since the restart would +// launch whatever tile HOME's cursor is on, as it does for picks no menu has. Restarts the game first, so it +// always starts from the title, waits out the black boot (up to +// INTRO_BOOT_LIMIT) without pressing anything, and fails if no BDSP screen shows within +// INTRO_TIME_BEFORE_BDSP of screens. Picks the language and the look in +// `picks`, names the player `player_name` and picks the rival's default name +// (Barry) from the list. The language and look selects are read, moved on +// and read again before their A. The player's keyboard showing with a pick +// still to make fails. Each step reads the +// screen and waits for the expected next one; an unexpected screen fails with a +// screenshot and the recent screens. +void play_new_game_intro( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const IntroPicks& picks, + const std::string& player_name +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp new file mode 100644 index 0000000000..ec14e2a0d8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp @@ -0,0 +1,506 @@ +/* BDSP AutoStory - Settings + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" +#include "PokemonBDSP_AutoStory_Settings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// How red a pixel must be to count as highlighted text or the row outline. +bool is_red(uint32_t pixel, int margin){ + const int r = (pixel >> 16) & 255, g = (pixel >> 8) & 255, b = pixel & 255; + return r > g + margin && r > b + margin; +} + +// The red pixels in `box` of the screen. +size_t red_pixels(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 image = extract_box_reference(screen, box); + size_t red = 0; + for (size_t y = 0; y < image.height(); y++){ + for (size_t x = 0; x < image.width(); x++){ + red += is_red(image.pixel(x, y), 30); + } + } + return red; +} + +// The share of near-white pixels on the settings panel, sampled every 4th pixel. +double white_share(const ImageViewRGB32& screen){ + const ImageViewRGB32 panel = extract_box_reference(screen, ImageFloatBox{0.04, 0.065, 0.93, 0.70}); + size_t white = 0, samples = 0; + for (size_t y = 0; y < panel.height(); y += 4){ + for (size_t x = 0; x < panel.width(); x += 4){ + const uint32_t p = panel.pixel(x, y); + white += ((p >> 16) & 255) > 225 && ((p >> 8) & 255) > 225 && (p & 255) > 225; + samples++; + } + } + return samples == 0 ? 0 : (double)white / samples; +} + +// The share of blue pixels in the heading bar, which the icon menu and the Bag lack. +double heading_blue_share(const ImageViewRGB32& screen){ + const ImageViewRGB32 heading = extract_box_reference(screen, ImageFloatBox{0.01, 0.005, 0.20, 0.025}); + size_t blue = 0, total = 0; + for (size_t y = 0; y < heading.height(); y++){ + for (size_t x = 0; x < heading.width(); x++){ + const uint32_t p = heading.pixel(x, y); + const int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + blue += b > r + 20 && g > r + 10; + total++; + } + } + return total == 0 ? 0 : (double)blue / total; +} + +// Where each row's value columns start and end, as fractions of the screen +// width: the same in every language (values are left-aligned in them). +struct ValueColumn{ + double left; + double right; +}; +const ValueColumn TWO_COLUMNS[] = {{0.335, 0.62}, {0.62, 0.88}}; +const ValueColumn THREE_COLUMNS[] = {{0.335, 0.52}, {0.52, 0.70}, {0.70, 0.88}}; + +// The first row's outline centre when Options opens, and how far it may be off. +constexpr double FIRST_ROW_Y = 0.157; +constexpr double ROW_Y_TOLERANCE = 0.02; +// How far apart rows are on screen. +constexpr double ROW_PITCH = 0.086; + +// The rows the story sets, top to bottom, in the game's fixed order (the +// same in every language). `columns` is the number of value columns, 0 for +// the Change Window selector, which is left as the new game has it (Type 1); +// `target` is the column to select, counted from the left. The two volume +// sliders below Show Nicknames are left as they are. +struct OptionRow{ + std::string_view name; + size_t columns; + size_t target; +}; +const OptionRow OPTION_ROWS[] = { + {"Text Speed", 3, 2}, // Slow, Normal, Fast + {"Change Window", 0, 0}, // < Type 1 > + {"Battle Effects", 2, 1}, // On, Off + {"Battle Style", 2, 1}, // Switch, Set + {"Send to Boxes", 2, 1}, // Manual, Automatic + {"Give Nicknames", 2, 1}, // Give, Don't give + {"Gyroscope", 2, 0}, // Use, Don't use + {"Autosave", 2, 1}, // On, Off + {"Show Nicknames", 2, 0}, // Show, Don't show +}; +constexpr size_t OPTION_ROW_COUNT = sizeof(OPTION_ROWS) / sizeof(OPTION_ROWS[0]); +constexpr size_t AUTOSAVE_ROW = 7; + +} // namespace + + +bool options_screen_shown(const ImageViewRGB32& screen){ + return screen && white_share(screen) > 0.70 && heading_blue_share(screen) > 0.25; +} + +std::optional options_selected_row_y(const ImageViewRGB32& screen){ + if (!screen){ + return std::nullopt; + } + // The two long red horizontal lines of the outline around the selected row. + int first = -1, last = -1; + for (size_t y = (size_t)(screen.height() * 0.08); y < screen.height() * 0.75; y++){ + size_t red = 0, total = 0; + for (size_t x = (size_t)(screen.width() * 0.08); x < screen.width() * 0.90; x += 2){ + const uint32_t p = screen.pixel(x, y); + red += ((p >> 16) & 255) > 150 && is_red(p, 40); + total++; + } + if (total && red > total * 0.70){ + if (first < 0){ + first = (int)y; + } + last = (int)y; + } + } + if (first < 0 || last - first < screen.height() * 0.04 || last - first > screen.height() * 0.12){ + return std::nullopt; + } + return (first + last) / (2.0 * screen.height()); +} + +std::optional options_highlighted_column(const ImageViewRGB32& screen, double y, size_t columns){ + const ValueColumn* layout = columns == 2 ? TWO_COLUMNS : columns == 3 ? THREE_COLUMNS : nullptr; + if (!screen || layout == nullptr){ + return std::nullopt; + } + // The band stays inside the outline, whose lines are red too. + const double band = 0.022; + std::optional highlighted; + for (size_t c = 0; c < columns; c++){ + const ImageFloatBox box{layout[c].left, y - band, layout[c].right - layout[c].left, 2 * band}; + // A short word such as "Off" has a few hundred red pixels at 1080p. + if (red_pixels(screen, box) * 10000 < screen.width() * screen.height() * 0.5){ + continue; + } + if (highlighted){ + return std::nullopt; + } + highlighted = c; + } + return highlighted; +} + + +namespace{ + +// Options fades in after A on its icon: poll instead of judging one frame. +bool wait_for_options_screen(VideoStream& stream, ProControllerContext& context, Milliseconds timeout){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + while (current_time() < deadline){ + auto frame = stream.video().snapshot(); + if (frame && options_screen_shown(frame)){ + return true; + } + context.wait_for(100ms); + } + return false; +} + +// From the overworld (or the main menu): the main menu, settled. +void open_main_menu(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + MenuDetector menu(COLOR_RED, /*skip_new_banner*/ true); + VideoOverlaySet detector_overlays(stream.overlay()); + menu.make_overlays(detector_overlays); + detector_overlays.add(COLOR_CYAN, {0.04, 0.065, 0.93, 0.70}, ""); + detector_overlays.add(COLOR_BLUE, {0.01, 0.005, 0.20, 0.025}, ""); + detector_overlays.add(COLOR_RED, {0.08, 0.08, 0.82, 0.67}, ""); + auto initial = stream.video().snapshot(); + if (!initial || !menu.detect(initial)){ + pbf_press_button(context, BUTTON_X, 80ms, 1000ms); + context.wait_for_all_requests(); + } + MenuWatcher menu_open(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{menu_open}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Main menu not detected.", stream); + } + stream.log("[Tutorial settings] Main menu detected; settling for 500ms before Options navigation."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +// From the main menu: Options. Only two icons are available at this +// initial checkpoint; verify the main menu before trying the other. +void open_options(VideoStream& stream, ProControllerContext& context){ + bool options_open = false; + for (size_t attempt = 0; attempt < 2; attempt++){ + pbf_press_dpad(context, attempt == 0 ? DPAD_RIGHT : DPAD_LEFT, 80ms, 350ms); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + if (wait_for_options_screen(stream, context, 5s)){ + options_open = true; + break; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher back_to_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{back_to_menu}}) != 0){ + break; + } + } + if (!options_open){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Options screen not detected.", stream); + } + stream.log("[Tutorial settings] Options screen detected."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +// The selected row's centre on a fresh frame of Options, retried while the +// screen fades in; fails with a screenshot after about a second. +double read_selected_row(VideoStream& stream, ProControllerContext& context, size_t row){ + for (size_t attempt = 0; attempt < 6; attempt++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const std::optional y = frame && options_screen_shown(frame) ? options_selected_row_y(frame) : std::nullopt; + if (y){ + return *y; + } + pbf_wait(context, 200ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: No selected row on Options for " + std::string(OPTION_ROWS[row].name), stream); +} + +// Options opens on its first row (Text Speed), at the top of the list: +// that row's centre. +double check_first_row(VideoStream& stream, ProControllerContext& context){ + const double y = read_selected_row(stream, context, 0); + stream.log("[Tutorial settings] First row at y=" + std::to_string(y)); + if (y < FIRST_ROW_Y - ROW_Y_TOLERANCE || y > FIRST_ROW_Y + ROW_Y_TOLERANCE){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Options did not open on its first row.", stream); + } + return y; +} + +// The mean grey-level difference of two frames over `box`, sampled every 2nd pixel. +double box_difference(const ImageViewRGB32& a, const ImageViewRGB32& b, const ImageFloatBox& box){ + const ImageViewRGB32 x = extract_box_reference(a, box); + const ImageViewRGB32 z = extract_box_reference(b, box); + const size_t width = std::min(x.width(), z.width()), height = std::min(x.height(), z.height()); + double sum = 0; + size_t count = 0; + for (size_t py = 0; py < height; py += 2){ + for (size_t px = 0; px < width; px += 2){ + const uint32_t p = x.pixel(px, py), q = z.pixel(px, py); + const int gp = ((p >> 16) & 255) + ((p >> 8) & 255) + (p & 255); + const int gq = ((q >> 16) & 255) + ((q >> 8) & 255) + (q & 255); + sum += std::abs(gp - gq) / 3.0; + count++; + } + } + return count == 0 ? 0 : sum / count; +} + +// From row `row` (outline at `y`): one press down, confirmed by the outline +// moving a row down, or, at the bottom of the view, by the list scrolling +// under it (the label under the outline changes). Never presses twice, so +// a row is never skipped. Returns the new row's centre. +double move_down(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + context.wait_for_all_requests(); + const auto before = stream.video().snapshot(); + pbf_press_dpad(context, DPAD_DOWN, 80ms, 350ms); + context.wait_for_all_requests(); + const ImageFloatBox label{0.08, y - 0.025, 0.24, 0.05}; + for (size_t check = 0; check < 15; check++){ + const auto after = stream.video().snapshot(); + const std::optional now = after && options_screen_shown(after) ? options_selected_row_y(after) : std::nullopt; + if (now && *now > y + ROW_PITCH / 2 && *now < y + ROW_PITCH * 1.5){ + return *now; + } + if (now && std::abs(*now - y) < ROW_PITCH / 4 && before && box_difference(before, after, label) > 8){ + return *now; + } + context.wait_for(100ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Cursor did not move down from " + std::string(OPTION_ROWS[row].name), stream); +} + +// The highlighted column of `row` (outline at `y`) on a fresh frame, or none. +std::optional read_column(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const std::optional column = frame ? options_highlighted_column(frame, y, OPTION_ROWS[row].columns) : std::nullopt; + stream.log("[Tutorial settings] Row " + std::to_string(row) + " (" + std::string(OPTION_ROWS[row].name) + ") y=" + + std::to_string(y) + " column=" + (column ? std::to_string(*column) : "none") + + " target=" + std::to_string(OPTION_ROWS[row].target)); + return column; +} + +// Sets the selected row `row` (outline at `y`) to its target column, a +// press at a time toward it, reading the highlight after each press. +void set_option(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + const OptionRow& option = OPTION_ROWS[row]; + if (option.columns == 0){ + stream.log("[Tutorial settings] Row " + std::to_string(row) + " (" + std::string(option.name) + ") left as it is."); + return; + } + OverlayBoxScope row_overlay(stream.overlay(), COLOR_RED, {0.07, y - 0.04, 0.84, 0.08}, ""); + OverlayBoxScope value_overlay(stream.overlay(), COLOR_GREEN, {0.335, y - 0.022, 0.545, 0.044}, ""); + size_t unread = 0; + for (size_t attempt = 0; attempt < 12; attempt++){ + const std::optional column = read_column(stream, context, row, y); + if (!column){ + // The highlight can still be fading in after a press. + if (++unread > 4){ + break; + } + pbf_wait(context, 200ms); + continue; + } + if (*column == option.target){ + stream.log("[Tutorial settings] Verified " + std::string(option.name)); + return; + } + pbf_press_dpad(context, *column < option.target ? DPAD_RIGHT : DPAD_LEFT, 80ms, 350ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Could not apply " + std::string(option.name), stream); +} + +// Confirms the settings and leaves Options (A A A A B B), back to the overworld. +void save_options(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1200ms); + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, 10s, {{overworld}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +// From the overworld: the saved Options screen again. +void reopen_options(VideoStream& stream, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher verify_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{verify_menu}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Autosave verification: Main menu not detected.", stream); + } + bool reopened = false; + for (size_t attempt = 0; attempt < 2; attempt++){ + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + if (wait_for_options_screen(stream, context, 5s)){ + reopened = true; + break; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher return_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{return_menu}}) != 0){ + break; + } + pbf_press_dpad(context, DPAD_RIGHT, 80ms, 350ms); + } + if (!reopened){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Autosave verification: Could not reopen saved Options.", stream); + } +} + +// In the saved Options: down to Autosave, a confirmed row at a time, and +// Off highlighted on three frames 200 ms apart. +void check_saved_autosave(VideoStream& stream, ProControllerContext& context){ + double y = check_first_row(stream, context); + for (size_t row = 0; row < AUTOSAVE_ROW; row++){ + y = move_down(stream, context, row, y); + } + for (size_t confirmation = 0; confirmation < 3; confirmation++){ + if (read_column(stream, context, AUTOSAVE_ROW, y) != OPTION_ROWS[AUTOSAVE_ROW].target){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Saved Autosave setting is not verified Off. Stopping before movement.", stream); + } + pbf_wait(context, 200ms); + } + stream.log("[Tutorial settings] Autosave = Off: confirmed after reopening saved Options (3 frames)."); +} + +void verify_tutorial_settings_once(VideoStream& stream, ProControllerContext& context){ + open_main_menu(stream, context); + open_options(stream, context); + double y = check_first_row(stream, context); + for (size_t row = 0; row < OPTION_ROW_COUNT; row++){ + set_option(stream, context, row, y); + if (row + 1 < OPTION_ROW_COUNT){ + y = move_down(stream, context, row, y); + } + } + save_options(stream, context, "Settings checkpoint: Did not return to the overworld after saving options."); + // Reopen the saved Options screen and independently verify Autosave. + // No route movement is permitted until this persisted-value check passes. + reopen_options(stream, context); + check_saved_autosave(stream, context); + save_options(stream, context, "Autosave verification: Could not return to the overworld."); + stream.log("[Tutorial settings] Completed: Text Speed Fast, Battle Effects Off, Battle Style Set, " + "Send to Boxes Automatic, Give Nicknames Don't give, Gyroscope Use, Autosave Off, Show Nicknames Show."); +} + +// After a failed attempt: back out of Options, save prompts, the Bag or +// the menu without moving the player or resetting/loading the game. +void back_to_overworld(VideoStream& stream, ProControllerContext& context){ + stream.log("[Tutorial settings] Returning to overworld before retry."); + context.wait_for_all_requests(); + const auto failed_frame = stream.video().snapshot(); + if (failed_frame && options_screen_shown(failed_frame)){ + // Finish the known Options save/exit sequence before observing + // state; never cancel it on an Options frame misclassified as + // overworld by the generic detector. + for (size_t press = 0; press < 4; press++){ + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + } + } + pbf_mash_button(context, BUTTON_B, 3000ms); + context.wait_for_all_requests(); + OverworldDetector overworld; + MenuDetector menu(COLOR_RED, /*skip_new_banner*/ true); + BlackScreenDetector black; + bool recovered = false; + size_t confirmations = 0; + for (size_t check = 0; check < 15 && !recovered; check++){ + const auto frame = stream.video().snapshot(); + const bool clear = frame && !options_screen_shown(frame) && + !menu.detect(frame) && !black.detect(frame) && bag_pocket_position(frame) == 0 && overworld.detect(frame); + if (clear && ++confirmations >= 3){ + recovered = true; + }else if (clear){ + pbf_wait(context, 200ms); + }else{ + confirmations = 0; + pbf_mash_button(context, BUTTON_B, 500ms); + } + context.wait_for_all_requests(); + } + stream.log(recovered ? "[Tutorial settings] Recovery confirmed outside Options/menu; retrying." + : "[Tutorial settings] Recovery not confirmed; next attempt will recheck the screen."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +} // namespace + +void verify_tutorial_settings(VideoStream& stream, ProControllerContext& context){ + // Settings-only recovery: initial attempt plus three retries. + // Never permit route movement unless the complete check succeeds. + constexpr size_t MAX_RETRIES = 3; + for (size_t attempt = 0; attempt <= MAX_RETRIES; attempt++){ + stream.log("[Tutorial settings] Attempt " + std::to_string(attempt + 1) + "/4."); + try{ + verify_tutorial_settings_once(stream, context); + return; + }catch (OperationFailedException& error){ + stream.log("[Tutorial settings] Attempt failed: " + error.message(), COLOR_ORANGE); + if (attempt == MAX_RETRIES){ + stream.log("[Tutorial settings] All three retries exhausted; stopping before movement.", COLOR_RED); + throw; + } + back_to_overworld(stream, context); + } + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h new file mode 100644 index 0000000000..7aa388b7e6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h @@ -0,0 +1,49 @@ +/* BDSP AutoStory - Settings + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Settings_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Settings_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The Options screen, read from its layout and colours only, so that it +// reads the same in every game language. + +// Whether the frame shows Options: the blue heading bar over the white panel. +bool options_screen_shown(const ImageViewRGB32& screen); + +// The centre of the selected row (its red outline), as a fraction of the +// screen height; none without exactly one outlined row. +std::optional options_selected_row_y(const ImageViewRGB32& screen); + +// Which of the `columns` value columns (2 or 3) of the row centred at `y` +// holds the highlighted (red) value, counted from the left; none when no +// column or more than one is highlighted. +std::optional options_highlighted_column(const ImageViewRGB32& screen, double y, size_t columns); + + +// From the overworld: opens Options and sets what the story needs (fast +// text, Send to Boxes automatic, no nickname prompt, Autosave off), saves +// them and returns to the overworld. Tries four times, backing out to the +// overworld between tries, and fails with a screenshot before any movement. +void verify_tutorial_settings(VideoStream& stream, ProControllerContext& context); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp new file mode 100644 index 0000000000..9871501ec0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp @@ -0,0 +1,62 @@ +/* BDSP AutoStory - Talk-over + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec){ + const WallClock deadline = current_time() + TALK_OVER_LIMIT; + TalkOver judge(spec.rule); + BdspScreenObserver eyes(spec.watch == TalkOverWatch::TRAINER ? BattleCue::TRAINER : BattleCue::WILD_MENU, spec.banner); + while (current_time() < deadline){ + context.wait_for_all_requests(); + const WallClock now = current_time(); + switch (judge.look(eyes.observe(stream.video().snapshot(), now), now)){ + case SceneMove::DONE: + stream.log(std::format("[Talk-over] {}: over; {}.", label, judge.describe())); + return TalkOverEnd::QUIET; + case SceneMove::BATTLE: + if (spec.watch == TalkOverWatch::TRAINER){ + stream.log("[Talk-over] " + label + ": a battle started."); + return TalkOverEnd::BATTLE; + } + stream.log("[Talk-over] " + label + ": in a wild battle; fleeing."); + run_from_wild_battle(stream, context); + continue; + case SceneMove::NEXT: + pbf_press_button(context, spec.next, TALK_OVER_HOLD, talk_over_release(spec.rule)); + continue; + case SceneMove::B: + pbf_press_button(context, BUTTON_B, TALK_OVER_HOLD, talk_over_release(spec.rule)); + + continue; + } + } + const long long seconds = std::chrono::duration_cast(TALK_OVER_LIMIT).count(); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + !judge.warped() + ? std::format("{}: no fade or banner within {} s, so we are not where the step goes (map mismatch).", label, seconds) + : !spec.rule.end_on_quiet + ? std::format("{}: no battle within {} s of the talk.", label, seconds) + : std::format("{}: not back on a quiet overworld within {} s.", label, seconds), + stream); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h new file mode 100644 index 0000000000..9469c3954c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h @@ -0,0 +1,89 @@ +/* BDSP AutoStory - Talk-over + * + * From: https://github.com/PokemonAutomation/ + * + * When a talk or a scene is over, judged from what the screen shows over + * time (#268's design note, slice 4): one rule for scenes, the speeches + * after a battle and a trainer's lines before one. It replaces the loops + * each of those had (settle(), settle_after_battle(), talk_or_fight()'s, + * confirm_battle_overworld()'s), which differed in how long the quiet had + * to last and whether the screen had to be still. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TalkOver_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TalkOver_H + +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/AutoStory/Pokemon_SceneJudges.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The judgement is shared (Pokemon_SceneJudges.h): it reads the screen +// events BdspScreenObserver reports. +// What to do after a look. +using Pokemon::SceneMove; +// What ends a talk-over, besides the quiet. +using Pokemon::TalkOverRule; +// When a talk or a scene is over, from looks alone. +using Pokemon::TalkOver; + +// One press is held this long, +constexpr Milliseconds TALK_OVER_HOLD = Milliseconds(80); +// then released this long before the next look. +constexpr Milliseconds TALK_OVER_RELEASE = Milliseconds(1200); +// A door's settle (TalkOverRule::door) has no text to wait out: it looks +// this often, to see its short still spell when it comes. +constexpr Milliseconds TALK_OVER_DOOR_RELEASE = Milliseconds(200); +// The release before the next look under `rule`. +inline Milliseconds talk_over_release(const TalkOverRule& rule){ + return rule.door ? TALK_OVER_DOOR_RELEASE : TALK_OVER_RELEASE; +} + +// How long a talk-over may take before the step fails. +constexpr Milliseconds TALK_OVER_LIMIT = Milliseconds(120000); + + +// What counts as a battle starting during a talk-over. +enum class TalkOverWatch{ + // A wild battle's menu; it is fled and the talk-over goes on. A black + // screen is a fade (a scene that changes map). + SCENE, + // A trainer's battle: its menu (Roark's read as STANDARD, r210d) or the + // black screen it opens with. The talk-over ends there. + TRAINER, +}; + +// How talk_over() presses and looks. +struct TalkOverSpec{ + // Pressed while a box shows: B, or A for lines that give an item. + Button next = BUTTON_B; + TalkOverRule rule = {}; + TalkOverWatch watch = TalkOverWatch::SCENE; + // The expected map's banner name for TalkOverRule::warp ("" for a fade only). + std::string banner = ""; +}; + +// How talk_over() ended. +enum class TalkOverEnd{ + QUIET, + BATTLE, // Only with TalkOverWatch::TRAINER. +}; + +// Presses through any text and scene on the console until TalkOver says it +// is over. Fails with a screenshot after TALK_OVER_LIMIT. +TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec = {}); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp new file mode 100644 index 0000000000..e8eb07f10d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -0,0 +1,300 @@ +/* BDSP AutoStory - Talks + * + * From: https://github.com/PokemonAutomation/ + * + * Talking to people and fighting the trainers among them, and the looks + * at the screen the story's events decide by. + * + */ + +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Battle.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_Talks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +[[noreturn]] void fail_step(VideoStream& stream, const std::string& message){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, message, stream); +} + +// True if a dialogue opens within `timeout`: someone who stopped us as the +// walk ended reads as BLOCKED, and their box comes up a moment later. +bool dialogue_opens(VideoStream& stream, ProControllerContext& context, Milliseconds timeout){ + context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_CYAN); + return wait_until(stream, context, timeout, {{dialog}}) == 0; +} + +bool talk_or_fight(VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect, BattleFormat format){ + const TalkOverEnd end = talk_over(stream, context, label, { + .rule = {.end_on_quiet = expect != TalkExpect::BATTLE}, + .watch = TalkOverWatch::TRAINER, + }); + if (end == TalkOverEnd::QUIET){ + route_status(stream, label + " only talked."); + return false; + } + route_status(stream, "Fighting " + label + "."); + // The battle ends with its speeches over (confirm_battle_overworld()): + // Roark's badge lines keep the Potion's menu from opening until then. + fight_story_battle(stream, context, label, format); + if (!in_pokemon_center(stream)){ + use_potion_first_pokemon(stream, context, 0.8); + } + return true; +} + +// A rough check that we stand in a Pokemon Center: its floor fills the lower +// half of the screen with warm yellow, where the gym is grey and routes are +// green. After a battle it tells a loss (a whiteout ends at the counter) +// from a win. +bool center_floor_on_screen(const ImageViewRGB32& screen, size_t* percent){ + const ImageRGB32 small = screen.scale_to(192, 108); + size_t warm = 0, total = 0; + for (size_t y = 54; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + const int r = (pixel >> 16) & 0xff, g = (pixel >> 8) & 0xff, b = pixel & 0xff; + total++; + if (r > 200 && g > 150 && b < 150 && r - b > 70){ + warm++; + } + } + } + if (percent){ + *percent = warm * 100 / total; + } + return warm * 10 > total * 3; +} + +bool pokemon_center_on_screen(const ImageViewRGB32& screen, size_t* percent){ + return center_floor_on_screen(screen, percent) && indoors(screen); +} + +bool in_pokemon_center(VideoStream& stream){ + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + size_t percent = 0; + const bool inside = pokemon_center_on_screen(*frame.frame, &percent); + stream.log("[Center check] warm floor " + std::to_string(percent) + " %, " + + (indoors(*frame.frame) ? "a room" : "outside") + ": " + + (inside ? "in a Pokemon Center." : "not in a Pokemon Center.")); + return inside; +} + +// The Oreburgh Mine's hall and corridors are dim: mean brightness 45 to 64 +// on the console, against 99 to 116 for the city and its yard (2026-10-31). +bool in_dim_cave(VideoStream& stream, uint64_t below){ + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + const ImageRGB32 small = frame.frame->scale_to(192, 108); + uint64_t sum = 0; + for (size_t y = 0; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + sum += ((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff); + } + } + const uint64_t mean = sum / (3 * 192 * 108); + stream.log("[Cave check] mean brightness " + std::to_string(mean) + "."); + return mean < below; +} + +// Mean of the three channels over a small copy of the frame, 0 to 255. +uint64_t mean_brightness(const ImageViewRGB32& screen){ + const ImageRGB32 small = screen.scale_to(192, 108); + uint64_t sum = 0; + for (size_t y = 0; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + sum += ((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff); + } + } + return sum / (3 * 192 * 108); +} + +// Fights a trainer whose dialogue has opened, with whatever starter we +// have. Someone who only talks leaves no battle to fight. +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name){ + pbf_mash_button(context, BUTTON_B, 5000ms); + context.wait_for_all_requests(); + BattleMenuWatcher battle_menu(BattleType::TRAINER, COLOR_YELLOW); + BattleDialogWatcher battle_dialog(COLOR_YELLOW); + if (wait_until(stream, context, 8s, {{battle_menu}, {battle_dialog}}) < 0){ + stream.log("[Route] " + name + ": no battle followed the talk."); + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + return false; + } + fight_trainer(stream, context, name); + context.wait_for_all_requests(); + // The next trainer may be a few tiles on: check HP now. With no + // Potion in the Bag this only looks. + use_potion_first_pokemon(stream, context, 0.8); + return true; +} + +bool sign_text_matches(const std::string& text, const std::string& key){ + std::string lower; + for (char ch : text){ + lower += (char)std::tolower((unsigned char)ch); + } + return lower.find(key) != std::string::npos; +} + +namespace{ + +// Where a sign's box shows: the bottom of the screen, as a dialogue's. The +// Lab's is grey, Route 201's green; opening either changes the area far +// more than the overworld behind it moves standing still. +const ImageFloatBox SIGN_BOX(0.19, 0.80, 0.64, 0.18); +// Its text, inside the margins. +const ImageFloatBox SIGN_TEXT(0.22, 0.82, 0.58, 0.15); +constexpr double SIGN_BOX_OPENED = 20.0; + +bool dialogue_showing(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + return screen && ShortDialogDetector().detect(*screen.frame); +} + +// After a talk opened: reads the first box, then presses `next` once +// per line, 1.5 s apart, and only while a box shows: a press after it +// has closed would start the talk over. +void finish_talk(VideoStream& stream, ProControllerContext& context, DialogueRead& read, Button next, const std::string& label){ + // The first box's text, inside its white margins. + // For the log only; a talk is checked by its box opening and closing. + VideoSnapshot screen = stream.video().snapshot(); + if (screen && story_reads_text()){ + read.text = OCR::ocr_read(story_language(), extract_box_reference(*screen.frame, SIGN_TEXT), OCR::PageSegMode::SINGLE_BLOCK); + } + stream.log("[Talk] " + label + ": the dialogue reads \"" + read.text + "\"."); + size_t quiet = 0; + // Barry's scene, to the Town Map, took more than 20 (r150a). + for (size_t presses = 0; presses < 80 && quiet < 2; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + quiet = 0; + pbf_press_button(context, next, 80ms, 0ms); + }else{ + quiet++; + } + } + read.closed = !dialogue_showing(stream, context); + stream.log("[Talk] " + label + (read.closed ? ": closed." : ": still open.")); +} + +} // namespace + +DialogueRead talk_facing(VideoStream& stream, ProControllerContext& context, Direction direction, Button next, const std::string& label){ + route_turn(stream, context, direction); + return talk_ahead(stream, context, next, label); +} + +DialogueRead talk_ahead(VideoStream& stream, ProControllerContext& context, Button next, const std::string& label){ + DialogueRead read; + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + ShortDialogWatcher opened(COLOR_CYAN); + if (wait_until(stream, context, 2000ms, {{opened}}) != 0){ + stream.log("[Talk] " + label + ": no dialogue opened."); + return read; + } + read.opened = true; + finish_talk(stream, context, read, next, label); + return read; +} + +DialogueRead talk_walking(VideoStream& stream, ProControllerContext& context, Direction direction, size_t tiles, const std::string& label){ + DialogueRead read; + const auto [x, y] = dpad_of(direction); + const bool across = direction == Direction::EAST || direction == Direction::WEST; + const Milliseconds limit = (int64_t)tiles * (across ? ROUTE_MS_PER_TILE_ACROSS : ROUTE_MS_PER_TILE_UP_DOWN) + 800ms; + const WalkResult walked = walk_to_talk(stream, context, run_from_wild_battle, {x, y}, limit, label); + if (walked.stop != WalkStop::DIALOG){ + stream.log("[Talk] " + label + ": nobody answered."); + return read; + } + read.opened = true; + finish_talk(stream, context, read, BUTTON_A, label); + return read; +} + +DialogueRead read_sign(VideoStream& stream, ProControllerContext& context, Direction facing, const std::string& label){ + DialogueRead read; + route_turn(stream, context, facing); + // Let the camera settle after the turn: its drift read as a box + // opening (r132a). + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + VideoSnapshot before = stream.video().snapshot(); + // The text types out over about a second; B before it has finished + // only finishes it (r131a). + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + context.wait_for_all_requests(); + VideoSnapshot shown = stream.video().snapshot(); + if (!before || !shown){ + return read; + } + const double opened = walk_screen_change(extract_box_reference(*before.frame, SIGN_BOX), extract_box_reference(*shown.frame, SIGN_BOX)); + read.opened = opened > SIGN_BOX_OPENED; + if (!read.opened){ + stream.log("[Sign] " + label + ": no box opened (change " + std::to_string(opened) + ")."); + return read; + } + // Tesseract read the Lab's white-on-grey text as "e R O e IO" (r131a). + // Only to say which sign, when there is a reader for the game's + // language; the box opening and closing is the check. + if (story_reads_text()){ + read.text = OCR::ocr_read(story_language(), extract_box_reference(*shown.frame, SIGN_TEXT), OCR::PageSegMode::SINGLE_BLOCK); + } + stream.log("[Sign] " + label + ": reads \"" + read.text + "\" (change " + std::to_string(opened) + ")."); + for (size_t presses = 0; presses < 3 && !read.closed; presses++){ + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + VideoSnapshot after = stream.video().snapshot(); + read.closed = after && walk_screen_change(extract_box_reference(*before.frame, SIGN_BOX), extract_box_reference(*after.frame, SIGN_BOX)) < SIGN_BOX_OPENED; + } + stream.log("[Sign] " + label + (read.closed ? ": closed." : ": still open.")); + return read; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h new file mode 100644 index 0000000000..3851a44744 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h @@ -0,0 +1,106 @@ +/* BDSP AutoStory - Talks + * + * From: https://github.com/PokemonAutomation/ + * + * Talking to people and fighting the trainers among them, and the looks + * at the screen the story's events decide by. Measured on the console + * (segment 01, 2026-10-29 to 10-31; the Jubilife talks, 2026-10-30). + * + */ + +// code-health: allow header_symbols -- dialogue and screen-detection primitives for story interactions that callers need together +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Talks_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Talks_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_Battle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Fails the step with a screenshot of the screen now. +[[noreturn]] void fail_step(VideoStream& stream, const std::string& message); + +// True if a dialogue opens within `timeout`: someone who stopped us as the +// walk ended reads as BLOCKED, and their box comes up a moment later. +bool dialogue_opens(VideoStream& stream, ProControllerContext& context, Milliseconds timeout); + +// For a dialogue that has opened: B through it. A trainer's ends in a +// battle, which is fought with the best move (Potions when HP runs low), +// followed by a Potion if HP is under half. Anyone else's ends back in the +// overworld. Returns whether there was a battle. Between a trainer's last +// line and the battle the overworld shows with no dialogue for a moment, +// so "only talked" is talk_over()'s quiet, and never comes when `expect` +// is BATTLE. `format` DOUBLES for tag and twin battles. A lost battle ends +// in the last Pokemon Center; see in_pokemon_center(). +enum class TalkExpect{ + TALK_OR_BATTLE, + BATTLE, +}; +// Handles dialogue interaction or battle sequence with an NPC or trainer. +bool talk_or_fight( + VideoStream& stream, ProControllerContext& context, const std::string& label, + TalkExpect expect = TalkExpect::TALK_OR_BATTLE, BattleFormat format = BattleFormat::SINGLES +); + +// Fights a trainer whose dialogue has opened, with whatever starter we +// have, then a Potion if HP is under half. Someone who only talks leaves +// no battle to fight; returns whether a battle followed. How the story's +// planned walks fight (RouteTrainerFight). +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name); + +// A rough check that we stand in a Pokemon Center: its floor fills the lower +// half of the screen with warm yellow, where the gym is grey and routes are +// green. After a battle it tells a loss (a whiteout ends at the counter) +// from a win. `percent`, if given, gets the warm share of the lower half. +bool center_floor_on_screen(const ImageViewRGB32& screen, size_t* percent = nullptr); +// In a Pokemon Center: its floor on screen, and a room (indoors()). Floor +// colour alone read Jubilife's yellow plaza as a Center (63 %, r79) and +// Sandgem's and Oreburgh's streets at 30 to 35 %; of 635 lab frames, the +// two together pick out exactly the Center interiors. +bool pokemon_center_on_screen(const ImageViewRGB32& screen, size_t* percent = nullptr); +// Checks whether the console stream currently shows the interior of a Pokemon Center. +bool in_pokemon_center(VideoStream& stream); +// The Oreburgh Mine's hall and corridors are dim: mean brightness 45 to 64 +// on the console, against 99 to 116 for the city and its yard (2026-10-31). +bool in_dim_cave(VideoStream& stream, uint64_t below = 85); +// Mean of the three channels over a small copy of the frame, 0 to 255. +uint64_t mean_brightness(const ImageViewRGB32& screen); + +// What a talk showed: whether a dialogue opened, whether it had closed +// after, and what OCR read in its first box. +struct DialogueRead{ + bool opened = false; + bool closed = false; + std::string text; +}; +// Whether a sign's OCR text has `key` (lower case), ignoring case. +bool sign_text_matches(const std::string& text, const std::string& key); +// Faces `direction` and presses A. If a dialogue opens, reads its first +// box, then presses `next` once per line, 1.5 s apart, while it shows. +DialogueRead talk_facing(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, Button next, const std::string& label); +// Presses A the way we face, without the turn talk_facing() taps first: a +// turn tap the way we already face is a step. +DialogueRead talk_ahead(VideoStream& stream, ProControllerContext& context, Button next, const std::string& label); +// Walks `direction`, pressing A after each short stretch, for at most +// `tiles` tiles' time until someone answers; then one A per line, 1.5 s +// apart, while the box shows. +DialogueRead talk_walking(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, size_t tiles, const std::string& label); +// Faces `facing` and presses A: whether a box opened at the bottom of +// the screen (a sign's may be green, not a dialogue's), what OCR reads +// in it, and whether B closed it. +DialogueRead read_sign(VideoStream& stream, ProControllerContext& context, Pokemon::Direction facing, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp new file mode 100644 index 0000000000..d9f4f29c57 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp @@ -0,0 +1,176 @@ +/* BDSP AutoStory - Wait beside a wanderer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonBDSP_AutoStory_WanderWait.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// Walks a straight line from `at` to `to`, a tile a step. A blocked step +// asks that way while `talked` is false, and ends the walk if the talk +// counts. False if a step stayed blocked for `tries` steps, or `to` is +// not on a line from `at`. +bool walk_line(WanderWaitBody& body, TilePos& at, TilePos to, const WanderWaitPlan& plan, size_t tries, bool* talked){ + if (at.x != to.x && at.y != to.y){ + return false; + } + while (at != to){ + const Direction direction = at.x != to.x + ? (to.x < at.x ? Direction::WEST : Direction::EAST) + : (to.y < at.y ? Direction::NORTH : Direction::SOUTH); + size_t blocked = 0; + while (!body.step(direction)){ + if (talked != nullptr && !*talked){ + body.face(direction); + if (body.ask()){ + *talked = true; + return true; + } + } + if (++blocked >= tries){ + return false; + } + body.wait(plan.blocked_wait); + } + at = at.next(direction); + } + return true; +} + +// The row to cross between columns from `at` to `to`: the one above the +// stands or the one below, whichever walk is shorter. +int crossing_row(const WanderWaitPlan& plan, const TilePos& at, const TilePos& to){ + std::optional top; + std::optional bottom; + for (const WanderWaitSide& side : plan.sides){ + for (const TilePos& stand : side.stands){ + top = top ? std::min(*top, stand.y) : stand.y; + bottom = bottom ? std::max(*bottom, stand.y) : stand.y; + } + } + const int above = top.value_or(at.y) - 1; + const int below = bottom.value_or(at.y) + 1; + const int up = std::abs(at.y - above) + std::abs(to.y - above); + const int down = std::abs(at.y - below) + std::abs(to.y - below); + return up <= down ? above : below; +} + +// Walks from `at` to `to`: along the row or the column they share, or +// between columns along a crossing row. Asks a blocked step in the row or +// column as walk_line() does: in his zone, it is him. +bool walk_to(WanderWaitBody& body, TilePos& at, TilePos to, const WanderWaitPlan& plan, size_t tries, bool* talked){ + if (at.x == to.x || at.y == to.y){ + return walk_line(body, at, to, plan, tries, talked); + } + const int row = crossing_row(plan, at, to); + if (!walk_line(body, at, {at.x, row}, plan, tries, talked)){ + return false; + } + if (talked != nullptr && *talked){ + return true; + } + return walk_line(body, at, {to.x, row}, plan, tries, nullptr) + && walk_line(body, at, to, plan, tries, talked); +} + +// Asks from where the player stands, facing `facing`. +bool ask_from_stand(WanderWaitBody& body, const WanderWaitPlan& plan, Direction facing){ + body.face(facing); + for (size_t ask = 0; ask < plan.asks; ask++){ + if (body.ask()){ + return true; + } + body.wait(plan.between_asks); + } + return false; +} + +// The way into the box from the side whose column `at` is on. +std::optional inward_from(const WanderWaitPlan& plan, const TilePos& at){ + for (const WanderWaitSide& side : plan.sides){ + if (!side.stands.empty() && side.stands[0].x == at.x){ + return side.facing; + } + } + return std::nullopt; +} + +// Walks from where the talk counted to the exit. If he stays in the way +// on the column, steps into the box beside him and goes round. +bool walk_out(WanderWaitBody& body, TilePos& at, const WanderWaitPlan& plan){ + if (walk_to(body, at, plan.exit, plan, plan.exit_tries, nullptr)){ + return true; + } + const std::optional inward = inward_from(plan, at); + if (!inward || at.x != plan.exit.x || !body.step(*inward)){ + return walk_to(body, at, plan.exit, plan, plan.blocked_tries, nullptr); + } + at = at.next(*inward); + return walk_to(body, at, plan.exit, plan, plan.blocked_tries, nullptr); +} + +// Walks `side`'s stands the plan's passes, asking from each, until the +// talk counts (`talked`, on `at`). False if a step stayed blocked. +bool wait_at_side(WanderWaitBody& body, TilePos& at, const WanderWaitPlan& plan, const WanderWaitSide& side, bool& talked){ + for (size_t pass = 0; pass < plan.passes; pass++){ + for (const TilePos& stand : side.stands){ + if (!walk_to(body, at, stand, plan, plan.blocked_tries, &talked)){ + return false; + } + if (!talked){ + talked = ask_from_stand(body, plan, side.facing); + } + if (talked){ + return true; + } + } + } + return true; +} + +} // namespace + + +WanderWaitResult wait_beside_wanderer(WanderWaitBody& body, TilePos at, const WanderWaitPlan& plan){ + bool talked = false; + TilePos talked_at = at; + for (size_t round = 0; round < plan.rounds && !talked; round++){ + for (size_t s = 0; s < plan.sides.size() && !talked; s++){ + if (!wait_at_side(body, at, plan, plan.sides[s], talked)){ + return {.end = WanderWaitEnd::STUCK, .at = at, .talked = at}; + } + talked_at = at; + } + } + if (!talked){ + return {.end = WanderWaitEnd::NO_TALK, .at = at, .talked = at}; + } + if (!walk_out(body, at, plan)){ + return {.end = WanderWaitEnd::STUCK, .at = at, .talked = talked_at}; + } + for (Direction wall : plan.exit_walls){ + if (body.step(wall)){ + return {.end = WanderWaitEnd::LOST, .at = at.next(wall), .talked = talked_at}; + } + } + return {.end = WanderWaitEnd::TALKED, .at = at, .talked = talked_at}; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h new file mode 100644 index 0000000000..437a9ab50d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h @@ -0,0 +1,105 @@ +/* BDSP AutoStory - Wait beside a wanderer + * + * From: https://github.com/PokemonAutomation/ + * + * A talk to someone who wanders, by waiting for him rather than chasing + * him: stand on an end column of his box, inside it, face in, and ask + * each time he may have come level, then try the next tile. Our tile is + * one he can no longer step on, so he comes level more often. A box wider + * than two columns has a far column he can stay on out of reach: the wait + * then takes turns between the two ends, and a side may hold tiles a step + * in from its end, toward him. The Mart clown (00.05b, 00.05c) wanders + * columns 54 to 56: he is waited for on 56 facing west, then on 54 facing + * east. The Poketch Company's clown wanders the row (14..16, 18): from + * 14 and 15 facing east, then 16 and 15 facing west. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// One end of his box to wait at: tiles on one column, in order, a tile +// apart (the list is walked again from its start: end it a tile from its +// first), and the way to face from them, into the box. +struct WanderWaitSide{ + std::vector stands; + Pokemon::Direction facing = Pokemon::Direction::WEST; +}; + +// Where and how long to wait. Between stands on one row or column the +// wait walks straight; between columns otherwise it walks round the +// stands' rows: along the row above them or the one below, whichever is +// shorter. Those rows must be open. +struct WanderWaitPlan{ + // The ends to wait at, in turn; the list is walked `rounds` times. + std::vector sides; + // Where the wait ends after the talk. A step from it toward each of + // `exit_walls` must not move: the walls there prove the tile. + Pokemon::TilePos exit; + std::vector exit_walls; + // Asks from each stand before the next one: few, so the wait steps on + // to his row instead of pressing A at nobody (lab-B 2026-11-12 11:03: + // twelve asks from (56, 32) while he stood on (55, 33)). + size_t asks = 2; + // Times a side's stands are walked before the next side. + size_t passes = 3; + // The pause after an ask that found nobody. + Milliseconds between_asks = Milliseconds(500); + // The wait for someone in the column to step out of it: a wanderer + // tries a step every 2 s or so, and may stay. + Milliseconds blocked_wait = Milliseconds(2000); + // Blocked steps on the way before giving up, and on the way out + // before walking round him. + size_t blocked_tries = 12; + size_t exit_tries = 3; + size_t rounds = 2; +}; + +// What the wait does in the game: the console's D-pad and A, or the +// simulation's. +class WanderWaitBody{ +public: + virtual ~WanderWaitBody() = default; + // One whole tile toward `direction`. False if something stopped it. + virtual bool step(Pokemon::Direction direction) = 0; + virtual void face(Pokemon::Direction direction) = 0; + // A toward the faced tile. True if the talk counted: his question. + virtual bool ask() = 0; + virtual void wait(Milliseconds time) = 0; +}; + +// How a wait ended. +enum class WanderWaitEnd{ + TALKED, // the talk counted; the player is on the plan's exit + NO_TALK, // every ask of every round found nobody to talk to + STUCK, // a step on the way stayed blocked + LOST, // an exit wall was not there: not on the exit +}; +// How a wait ended, and where. +struct WanderWaitResult{ + WanderWaitEnd end; + Pokemon::TilePos at; // where the player is, as counted + Pokemon::TilePos talked; // where the talk counted, for TALKED +}; + +// Waits for the wanderer from `at`, at the plan's sides in turn. A step +// in his box that something stops is him in the column: the wait asks +// that way too, then waits and steps again. +WanderWaitResult wait_beside_wanderer(WanderWaitBody& body, Pokemon::TilePos at, const WanderWaitPlan& plan); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp new file mode 100644 index 0000000000..a779393868 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp @@ -0,0 +1,503 @@ +/* BDSP AutoStory - Wild battles + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_ReceivePokemonDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Battle.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// Two grass rows at 240 ms each, with room to spare: the wall stops us. +constexpr Milliseconds CATCH_GRASS_WALK = 900ms; + +// Whether a wild battle has started: its black screen, menu or text. +bool wild_battle_started(VideoStream& stream, ProControllerContext& context){ + BlackScreenWatcher battle_start; + BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); + BattleDialogWatcher battle_dialog(COLOR_YELLOW); + const int ret = wait_until( + stream, + context, + 1500ms, + { + {battle_start}, + {battle_menu}, + {battle_dialog}, + } + ); + return ret >= 0; +} + +// One walk across the catch grass `direction`, to the wall; whether a +// battle started. +bool walk_catch_grass(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction){ + route_turn(stream, context, direction); + pbf_move_left_joystick(context, {0, direction == Pokemon::Direction::NORTH ? 1.0 : -1.0}, CATCH_GRASS_WALK, 200ms); + context.wait_for_all_requests(); + return wild_battle_started(stream, context); +} + +void encounter_one_wild_pokemon(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + pbf_wait(context, 1000ms); + + bool encounter_found = false; + const auto deadline = std::chrono::steady_clock::now() + 180s; + + // Walk up and down the catch grass until a battle starts. Its three rows + // sit between tree walls, so each walk ends at a wall, on the same tile + // every time, after two new grass tiles. Short sideways steps drift and + // can rock across one tile edge without entering new grass. + while (!encounter_found && std::chrono::steady_clock::now() < deadline){ + encounter_found = walk_catch_grass(stream, context, Pokemon::Direction::NORTH) || + walk_catch_grass(stream, context, Pokemon::Direction::SOUTH); + } + + if (!encounter_found){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Wild encounter: walking up and down the grass found no battle within three minutes.", stream); + } + stream.log("[AutoStory] Wild battle detected!", COLOR_GREEN); + + // Wait until the actual battle menu appears. + BattleMenuWatcher confirm_menu(BattleType::STANDARD); + if (wait_until(stream, context, 15s, {{confirm_menu}}) < 0){ + stream.log("[AutoStory] Failed to detect battle menu.", COLOR_RED); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "[AutoStory] Failed to detect battle menu.", stream); + } + + stream.log("[AutoStory] Battle menu detected.", COLOR_GREEN); + + context.wait_for_all_requests(); + pbf_wait(context, 500ms); +} + +// With the game's main menu open after a battle: B until it closes, the +// overworld or a screen without it. Whether it was seen closed. +bool close_menu_after_battle(VideoStream& stream, ProControllerContext& context){ + OverworldWatcher overworld; + MenuDetector menu_visible; + for (size_t close = 0; close < 3; close++){ + pbf_press_button(context, BUTTON_B, 80ms, 500ms); + context.wait_for_all_requests(); + if (wait_until(stream, context, 1s, {{overworld}}) == 0){ + return true; + } + BlackScreenWatcher visible(COLOR_YELLOW, + {0.1, 0.1, 0.8, 0.8}, + 100, + 10, + BlackScreenWatcher::FinderType::GONE, + 500ms); + if (wait_until(stream, context, 2s, {{visible}}) != 0){ + continue; + } + const VideoSnapshot frame = stream.video().snapshot(); + if (frame && !menu_visible.detect(frame)){ + stream.log("[Mine escape] Confirmed game menu closed; returning to navigation.", COLOR_GREEN); + return true; + } + } + stream.log("[Mine escape] Main menu detected but could not confirm it closed.", COLOR_ORANGE); + return false; +} + +bool mine_confirm_escape_via_menu(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + MenuWatcher game_menu; + BattleMenuWatcher battle_menu(battle_type, COLOR_YELLOW); + for (size_t attempt = 1; attempt <= 3; attempt++){ + // Do not accept the absence-based overworld detector before menu proof. + const int state = wait_until(stream, context, 300ms, {{game_menu}, {battle_menu}}); + if (state == 1){ + stream.log("[Mine escape] Battle menu still present; retrying Run."); + return false; + } + int detected = state; + if (state != 0){ + stream.log("[Mine escape] Main menu fallback attempt " + std::to_string(attempt) + "/3."); + detected = run_until(stream, + context, + [](ProControllerContext& controller){ + pbf_press_button(controller, BUTTON_X, 160ms, 840ms); + pbf_wait(controller, 3s); + controller.wait_for_all_requests(); + }, + {game_menu, battle_menu}); + if (detected == 1){ + return false; + } + } + if (detected != 0){ + continue; + } + stream.log("[Mine escape] Main game menu confirmed; battle has ended.", COLOR_GREEN); + // Close the confirmed menu before returning control to navigation. + return close_menu_after_battle(stream, context); + } + return false; +} + +bool mine_select_run(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + BattleMenuDetector cursor(battle_type, COLOR_YELLOW); + size_t matching_frames = 0; + for (size_t check = 0; check < 16; check++){ + context.wait_for_all_requests(); + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + const BattleMenuOption selected = battle_menu_selected_option(cursor, frame); + stream.log( + "[Mine escape] Battle cursor row=" + std::to_string(static_cast(selected) + 1) + "; target Run=4."); + if (selected == BattleMenuOption::RUN){ + if (++matching_frames >= 3){ + stream.log("[Mine escape] Run cursor confirmed.", COLOR_GREEN); + return true; + } + pbf_wait(context, 100ms); + }else{ + matching_frames = 0; + if (selected == BattleMenuOption::UNKNOWN){ + // Do not send a direction or A without a known cursor. + pbf_wait(context, 150ms); + }else{ + const DpadPosition direction = selected == BattleMenuOption::BATTLE ? DPAD_UP : DPAD_DOWN; + pbf_press_dpad(context, direction, 80ms, 250ms); + } + } + } + stream.log("[Mine escape] Could not confirm Run selection; A was not pressed.", COLOR_ORANGE); + return false; +} + +// The escape policy's game steps on the console. The exit watcher lives +// across the attempts: EndBattleWatcher needs black followed by a visible +// screen, which one attempt's waits may split. +class ConsoleWildBattleEscape : public WildBattleEscapeActions{ +public: + ConsoleWildBattleEscape(VideoStream& stream, ProControllerContext& context, BattleType battle_type) + : m_stream(stream) + , m_context(context) + , m_battle_type(battle_type) + {} + + virtual WildRunOutcome run() override; + virtual void fight() override{ + fight_wild_battle(m_stream, m_context, "Mine escape"); + } + virtual void fight_trainer() override{ + PokemonBDSP::fight_trainer(m_stream, m_context, "Mine escape"); + m_context.wait_for_all_requests(); + use_potion_first_pokemon(m_stream, m_context, 0.8); + } + virtual void note(const std::string& text) override{ + m_stream.log("[Mine escape] " + text); + } + + // After the policy gave up: whether the exit still shows within `wait`. + bool exit_within(Milliseconds wait){ + return wait_until(m_stream, m_context, wait, {{m_battle_end}}) == 0; + } + +private: + // B, then the exit (0) or the battle menu (1) within three seconds; -1 for neither. + int exit_or_menu(); + // After A on Run: whether the box under the battle says a Trainer + // battle cannot be run from. Read while its text is still up. + bool says_no_running(); + + VideoStream& m_stream; + ProControllerContext& m_context; + BattleType m_battle_type; + EndBattleWatcher m_battle_end; +}; + +int ConsoleWildBattleEscape::exit_or_menu(){ + pbf_press_button(m_context, BUTTON_B, 40ms, 200ms); + m_context.wait_for_all_requests(); + BattleMenuWatcher menu_ready(m_battle_type, COLOR_YELLOW); + return wait_until(m_stream, m_context, 3s, {{m_battle_end}, {menu_ready}}); +} + +bool ConsoleWildBattleEscape::says_no_running(){ + // The refusal's box: the bottom of the battle screen, as a dialogue's. + const ImageFloatBox text_box(0.19, 0.80, 0.64, 0.18); + pbf_press_button(m_context, BUTTON_A, 80ms, 600ms); + m_context.wait_for_all_requests(); + const VideoSnapshot frame = m_stream.video().snapshot(); + if (!frame){ + return false; + } + const std::string text = OCR::ocr_read(story_language(), extract_box_reference(frame, text_box), OCR::PageSegMode::SINGLE_BLOCK); + m_stream.log("[Mine escape] Text after Run: \"" + text + "\""); + return sign_text_matches(text, "trainer"); +} + +WildRunOutcome ConsoleWildBattleEscape::run(){ + const int ready = exit_or_menu(); + if (ready == 0){ + return WildRunOutcome::ESCAPED; + } + if (ready != 1 || !mine_select_run(m_stream, m_context, m_battle_type)){ + return WildRunOutcome::UNCERTAIN; + } + if (says_no_running()){ + return WildRunOutcome::NO_RUNNING; + } + m_stream.log("[Mine escape] Run input sent; watching for black-screen exit."); + const int result = run_until(m_stream, + m_context, + [](ProControllerContext& controller){ + pbf_mash_button(controller, BUTTON_ZL, 1000ms); + pbf_mash_button(controller, BUTTON_A, 1000ms); + pbf_mash_button(controller, BUTTON_B, 3000ms); + controller.wait_for_all_requests(); + }, + {m_battle_end}); + if (result == 0){ + return WildRunOutcome::ESCAPED; + } + // No exit after Run: the menu back means the game refused it ("Can't + // escape!": Onix's Bind in Oreburgh Mine, console B 2026-11-07). Its + // text and the foe's turn take a few seconds. + for (size_t look = 0; look < 2; look++){ + const int seen = exit_or_menu(); + if (seen == 0){ + return WildRunOutcome::ESCAPED; + } + if (seen == 1){ + return WildRunOutcome::TRAPPED; + } + } + return WildRunOutcome::UNCERTAIN; +} + +// B until the overworld shows; fails with `failure` after two minutes. +void mash_to_overworld(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + // Mash through XP / level-up / dialog screens back to overworld. + OverworldWatcher overworld; + const int cleanup_ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{overworld}} + ); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +} // namespace + +void fight_starly(VideoStream& stream, ProControllerContext& context){ + stream.log("[AutoStory] Starting Starly battle...", COLOR_BLUE); + + // Declare OUTSIDE the loop so accumulated detector state + // (trigger counts, black-screen transitions) is preserved across frames. + // STARTER type: player has no Pokeballs, so ball-count areas won't + // be white. STARTER skips that check and only verifies status bars + // and the Fight button color. + BattleMenuWatcher battle_menu(BattleType::STARTER, COLOR_YELLOW); + EndBattleWatcher end_battle; + + while (true){ + context.wait_for_all_requests(); + + // Phase 1: wait for either battle menu or battle end. + const int ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{battle_menu}, {end_battle}} + ); + if (ret == 1){ + stream.log("[AutoStory] Starly battle complete.", COLOR_GREEN); + mash_to_overworld(stream, context, "Starly battle: Overworld cleanup timed out."); + return; + } + if (ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Starly battle timed out.", stream); + } + stream.log("[AutoStory] Battle menu detected. Selecting move...", COLOR_BLUE); + // Select Fight, then move slot 1; loop back and mash B through the + // animations until the next menu or the end. + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 3000ms); + } +} + +void battle_1_pokemon(VideoStream& stream, ProControllerContext& context){ + stream.log("[Training] Finding one wild Pokemon using the catch encounter wiggle.", COLOR_BLUE); + encounter_one_wild_pokemon(stream, context); + // The best move on the menu, whatever the starter. + fight_wild_battle(stream, context, "Training"); + context.wait_for_all_requests(); + stream.log("[Training] One wild battle completed.", COLOR_GREEN); +} + +namespace{ + +// Throws the first ball in the Bag at the wild Pokemon, up to ten times, +// until its receipt shows. Returns whether the battle's exit was seen. +bool throw_until_caught(VideoStream& stream, ProControllerContext& context, EndBattleWatcher& battle_ended){ + ReceivePokemonDetector received_pokemon; + for (int throw_attempt = 0; throw_attempt < 10; throw_attempt++){ + // Open bag from battle menu (X), confirm first slot (ZL). + pbf_press_button(context, BUTTON_X, 160ms, 840ms); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_ZL, 160ms, 1000ms); + context.wait_for_all_requests(); + + // Wait to see if the battle menu comes back (ball failed) + // or if the battle ends (caught or fainted). + BattleMenuWatcher menu_back(BattleType::STANDARD, COLOR_YELLOW); + const int throw_ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{menu_back}, {received_pokemon}, {battle_ended}} + ); + + if (throw_ret == 1 || (throw_ret == 2 && received_pokemon.received())){ + stream.log("[AutoStory] Pokemon receipt verified.", COLOR_GREEN); + context.wait_for_all_requests(); + return throw_ret == 2; + } + if (throw_ret < 0 || throw_ret == 2){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch failed: No verified Pokemon receipt before battle ended or timed out.", stream); + } + stream.log( + "[AutoStory] Ball failed, retrying (attempt " + std::to_string(throw_attempt + 2) + ")...", + COLOR_ORANGE + ); + } + context.wait_for_all_requests(); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch failed: Exhausted ten ball attempts.", stream); +} + +} // namespace + +void catch_1_pokemon( + VideoStream& stream, + ProControllerContext& context +){ + stream.log("[AutoStory] Starting catch_1_pokemon helper...", COLOR_BLUE); + + encounter_one_wild_pokemon(stream, context); + + // Catch the pokemon. + stream.log("[AutoStory] Throwing Poke Ball (manual bag open)...", COLOR_BLUE); + // Preserve exit-transition state from the ball outcome through + // receipt/experience cleanup; do not recreate it between phases. + EndBattleWatcher battle_ended; + const bool exit_seen = throw_until_caught(stream, context, battle_ended); + // Receipt alone is not an overworld return: clear experience, + // automatic move learning, dex registration and nickname screens + // until the same completed-black-transition watcher used in battles fires. + if (!exit_seen){ + stream.log("[Catch cleanup] Receipt confirmed; waiting for battle exit transition."); + const int cleanup_ret = run_until(stream, context, + [](ProControllerContext& ctx){ pbf_mash_button(ctx, BUTTON_B, 120000ms); }, + {{battle_ended}}); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch cleanup: Battle exit transition did not finish.", stream); + } + } + context.wait_for_all_requests(); + stream.log("[Catch cleanup] Battle exit transition confirmed; checking stable overworld."); + confirm_battle_overworld(stream, context, "Catch cleanup"); + stream.log("[Catch cleanup] Verified receipt, completed battle exit, and four seconds of clear overworld.", COLOR_GREEN); +} + +void escape_wild_battle(VideoStream& stream, + ProControllerContext& context, + BattleType battle_type, + const WildBattleEscapePolicy& policy){ + ConsoleWildBattleEscape actions(stream, context, battle_type); + WildEscapeResult result = escape_wild_battle(actions, policy); + if (result == WildEscapeResult::FOUGHT || result == WildEscapeResult::TRAINER_FOUGHT){ + // fight_wild_battle() and fight_trainer() ended on the overworld. + return; + } + if (result == WildEscapeResult::UNCONFIRMED && actions.exit_within(10s)){ + result = WildEscapeResult::ESCAPED; + } + if (result == WildEscapeResult::ESCAPED){ + // EndBattleWatcher requires black followed by a visible screen. + stream.log("[Mine escape] Black-screen exit transition confirmed.", COLOR_GREEN); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + return; + } + // Only probe X after normal escape attempts and the final transition wait. + // Absence of battle UI by itself is not evidence of an overworld return. + stream.log("[Mine escape] Exit transition missed; trying late main-menu fallback."); + if (mine_confirm_escape_via_menu(stream, context, battle_type)){ + return; + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "escape_wild_battle: Exit transition and late main-menu fallback could not confirm escape.", + stream); +} + +// STARTER's check of the menu (its Battle button) holds for any party: +// STANDARD also reads the party's balls, which a lone starter does not +// match, so Route 201's first wild battle never saw its menu (r34). +void run_from_wild_battle(VideoStream& stream, ProControllerContext& context){ + escape_wild_battle(stream, context, BattleType::STARTER); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h new file mode 100644 index 0000000000..98a93ffb9b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h @@ -0,0 +1,59 @@ +/* BDSP AutoStory - Wild battles + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The story's wild battles: the Starly the starter is chosen against, one + * found in the grass to fight or to catch, and running from one that + * interrupts a walk. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H + +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The Starly battle at Lake Verity, from its first text to the overworld. +void fight_starly(VideoStream& stream, ProControllerContext& context); + +// Finds one wild Pokemon in the grass by walking up and down a column +// between two tree walls, and fights it with the best move. Does not heal. +void battle_1_pokemon(VideoStream& stream, ProControllerContext& context); + +// Finds one wild Pokemon the same way and catches it with the first ball +// in the Bag, up to ten throws; ends on the overworld. +void catch_1_pokemon(VideoStream& stream, ProControllerContext& context); + +// Leaves the wild battle on screen as `policy` says: Run on the battle menu +// until the exit's black screen shows, or, once the game has refused the +// escape `policy.run_refusals` times (a trapping move), fights it to its end +// with the best move. Fails with a screenshot once the policy gives up and +// the main menu does not prove the overworld either. +void escape_wild_battle( + VideoStream& stream, + ProControllerContext& context, + BattleType battle_type = BattleType::STANDARD, + const WildBattleEscapePolicy& policy = WildBattleEscapePolicy() +); + +// escape_wild_battle() for a standard wild battle, as the walker takes it +// (WildBattleEscape): how the story's walks leave the battles that +// interrupt them. +void run_from_wild_battle(VideoStream& stream, ProControllerContext& context); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp new file mode 100644 index 0000000000..adb489303f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp @@ -0,0 +1,355 @@ +/* BDSP AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Pokemon_StoryPreflight.h" +#include "Pokemon/AutoStory/Pokemon_StoryRngHandoff.h" +#include "Pokemon/AutoStory/Pokemon_StoryRun.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/Options/Pokemon_GameLanguages.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions.h" +#include "PokemonBDSP/Programs/PokemonBDSP_GameEntry.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h" +#include "PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +BDSPAutoStory_Descriptor::BDSPAutoStory_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonBDSP:AutoStory", + "Pokemon BDSP", "Auto Story", + "Programs/PokemonBDSP/AutoStory.html", + "Automatically completes BDSP story segments.", + PanelQualityStanding::OFFICIAL_BETA, + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS, + {"Tesseract"} + ) +{} + +std::unique_ptr BDSPAutoStory_Descriptor::make_stats() const{ + return std::make_unique(); +} + + +namespace{ + +// What the user does around Starter RNG when the run stops for it: the +// option's help and the run's closing message. +constexpr std::string_view STARTER_RNG_HANDOFF_TEXT = + "At the end of 00.01 save at the Verity Lakefront (17, 14), facing Lake Verity's entrance, and stop. " + "Run the Starter RNG program from that save (Start point: Verity Lakefront). " + "When it is done, close the Summary page and the menu, let Rowan's scene finish so the player stands " + "on the Verity Lakefront overworld, save, and start AutoStory at 00.03."; + +// The start and end dropdowns' entries: segments 00 and 01 joined, 00.00 +// to 01.11. The options hold a reference to it. +const StringSelectDatabase& checkpoint_database(){ + static const StringSelectDatabase database = []{ + std::vector labels; + for (const StoryCheckpoint& row : story_checkpoints()){ + labels.push_back(story_checkpoint_label(row)); + } + return Pokemon::story_checkpoint_database(labels); + }(); + return database; +} + +// The rows whose failure ends the program: the rows that save nothing and +// retry nothing, and the plan's own (Starter RNG's, which resets by itself). +std::set rows_that_stop(const StoryRunPlan& plan){ + std::set ids = plan.fatal_rows; + for (const StoryCheckpoint& row : plan.rows){ + if (row.on_failure == StoryFailure::STOP){ + ids.insert(row.id); + } + } + return ids; +} + +// What BDSP lends the shared shell: the console's preflight; a load is a +// reset from HOME, which loads the save the row made at its start; the +// engine's lines go to the console's log. +Pokemon::StoryGameHooks story_game(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + Pokemon::StoryGameHooks game; + game.preflight = [&env, &context]{ + return Pokemon::StoryPreflight::read(env.console, context); + }; + game.load_save = [&env, &context](Pokemon::RunState&){ + if (!reset_game_from_home(env, env.console, context, true, 1000ms)){ + return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); + } + return Pokemon::outcome_ok(); + }; + game.log = [&env](const std::string& line){ + env.log(line, COLOR_BLUE); + }; + game.status = [&env](const std::string& line){ + env.console.overlay().add_log(line); + }; + return game; +} + +// Verify mode: the checkpoint run as `policy` says, from the save the +// start loaded. Rows save at their start, never at their end, so each +// reload between runs loads that same save. Throws unless enough runs pass. +std::string verify_story_checkpoint( + const std::string& id, const Pokemon::VerifyPolicy& policy, + Pokemon::Segment& segment, Pokemon::StoryGameHooks& game, Pokemon::RunState& state +){ + Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game); + Pokemon::VerifyReport report; + try{ + report = Pokemon::verify_checkpoint(id, policy, segment, hooks, state); + }catch (OperationFailedException&){ + Pokemon::log_story_failure(game); + throw; + } + return Pokemon::finish_story_verify(id, policy, report, game); +} + +} // namespace + + +BDSPAutoStory::~BDSPAutoStory(){ + STARTER_RNG.run().remove_listener(*this); +} + +BDSPAutoStory::BDSPAutoStory() + : RUN(checkpoint_database(), Pokemon::Gen8ToGen9Languages_Database()) + , NEW_GAME_INTRO( + "Start a new game first:
" + "For a game with no save: restart it and play the intro (language, appearance, names) " + "until the player is in control in the bedroom. Only when starting at 00.00.", + LockMode::LOCK_WHILE_RUNNING, false + ) + , PLAYER_NAME(false, "Player name:
For a new game. Letters and digits.", LockMode::LOCK_WHILE_RUNNING, "Kosmos", "Kosmos") + , PLAYER_LOOK( + "Player's look:
For a new game: the look picked on the appearance grid. " + "The portraits row by row: 1 Lucas light, 2 Lucas dark, 3 Dawn dark-haired, 4 Dawn dark; " + "5 Lucas blond, 6 Lucas brown, 7 Dawn blond, 8 Dawn brown-haired." + ) + , STARTER( + "Starter Pokemon:", + { + {StarterChoice::TURTWIG, "turtwig", "Turtwig"}, + {StarterChoice::CHIMCHAR, "chimchar", "Chimchar"}, + {StarterChoice::PIPLUP, "piplup", "Piplup"}, + }, + LockMode::LOCK_WHILE_RUNNING, + StarterChoice::CHIMCHAR + ) + , STOP_FOR_STARTER_RNG( + std::format( + "Stop for Starter RNG:
Off by default. Ignored while Run Starter RNG in AutoStory is on. {}", + STARTER_RNG_HANDOFF_TEXT + ), + LockMode::LOCK_WHILE_RUNNING, + false + ) + , NOTIFICATIONS({ + &STARTER_RNG.notification_shiny(), + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(RUN); + PA_ADD_OPTION(NEW_GAME_INTRO); + PA_ADD_OPTION(PLAYER_NAME); + PA_ADD_OPTION(PLAYER_LOOK); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(STOP_FOR_STARTER_RNG); + add_option(STARTER_RNG.run(), "RUN_STARTER_RNG"); + add_option(STARTER_RNG.player_model(), "STARTER_RNG_PLAYER_MODEL"); + add_option(STARTER_RNG.max_resets(), "MAX_RESETS"); + add_option(STARTER_RNG.filters(), "STARTER_RNG_FILTERS"); + PA_ADD_OPTION(NOTIFICATIONS); + + BDSPAutoStory::on_config_value_changed(this); + STARTER_RNG.run().add_listener(*this); +} + +// Run Starter RNG takes precedence over Stop for Starter RNG: the stop +// option is greyed out while it is on, and the plan ignores it. +void BDSPAutoStory::on_config_value_changed(void*){ + const bool run_in_story = STARTER_RNG.run(); + STARTER_RNG.set_details_visible(run_in_story); + STOP_FOR_STARTER_RNG.set_visibility(run_in_story ? ConfigOptionState::DISABLED : ConfigOptionState::ENABLED); +} + +namespace{ + +// The checkpoints whose start save is skipped: the first one, unless the +// run starts a new game. After a load its save is already there (saving +// again cost ~10 s), and nothing saves before the start is confirmed. +std::set saved_before_the_run(const std::vector& checkpoints, size_t first, Pokemon::StoryStart start){ + if (Pokemon::story_first_save_allowed(start)){ + return {}; + } + return {checkpoints[first].id}; +} + +// "" when the save screen's card names `place`, the map the start row +// saved in, else why not. Opens the save screen and backs out unsaved. +std::string check_start_place(SingleSwitchProgramEnvironment& env, ProControllerContext& context, const std::string& place){ + if (place.empty()){ + return ""; + } + const std::string read = read_location_from_save_screen(env.console, context); + if (location_banner_matches(read, place)){ + return ""; + } + return std::format("The save is in \"{}\", not {}.", read, place); +} + +// Gets the game to where `row` starts through the shared start gate: +// preflight, then (for a save) the load and the save card's place. Throws a +// setup error when the preflight failed, nothing pressed, and an +// OperationFailedException when the load or place check failed, unsaved. +void start_story( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, const StoryCheckpoint& row, + Pokemon::StoryStart start, Pokemon::StoryGameHooks& game, Pokemon::RunState& state +){ + // The loaded save must be where the start row saved; a retry's reload + // in a later row is checked by that row's own start save. + game.check_save_place = [&env, &context, place = row.saves_in]{ + return check_start_place(env, context, place); + }; + const Pokemon::Outcome loaded = Pokemon::prepare_story_start(row.id, start, game, state); + game.check_save_place = nullptr; + if (loaded.failure == Pokemon::StoryPreflight::FAILURE){ + throw UserSetupError(env.console, std::format("AutoStory did not start, nothing was pressed: {}", loaded.detail)); + } + if (!loaded.succeeded()){ + Pokemon::log_story_failure(game); + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format("AutoStory could not load the save for {}: {}", row.id, Pokemon::to_string(loaded)) + ); + } +} + +// The run from `range`'s first checkpoint to its last, its counts in the +// stats. Returns the finish line; throws if it stopped short. +std::string run_story( + const Pokemon::SegmentRange& range, const Pokemon::SegmentPolicy& policy, + Pokemon::Segment& segment, Pokemon::StoryGameHooks& game, Pokemon::RunState& state, + SingleSwitchProgramEnvironment& env +){ + Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game); + Pokemon::SegmentReport report; + try{ + report = run_segment(range, policy, segment, hooks, state); + }catch (OperationFailedException&){ + Pokemon::log_story_failure(game); + throw; + } + Pokemon::AutoStoryStats& stats = env.current_stats(); + std::string done; + try{ + done = Pokemon::finish_story_run(report, range.to.value_or(""), stats, game); + }catch (OperationFailedException&){ + env.update_stats(); + throw; + } + env.update_stats(); + return done; +} + +} // namespace + +void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + const std::vector& checkpoints = story_checkpoints(); + const size_t first = RUN.start_index(); + const size_t last = RUN.end_index(); + if (first > last || last >= checkpoints.size()){ + throw UserSetupError(env.console, "The start checkpoint must not come after the end checkpoint."); + } + // The reads that use text (Location.h) take the game's language from here. + set_story_language(RUN.language()); + + StoryEnv story{env, context, STARTER, PLAYER_NAME, IntroPicks{RUN.language(), PLAYER_LOOK.index()}}; + if (STARTER_RNG.run()){ + story.run_starter_rng = make_starter_rng_hook( + env, context, STARTER_RNG, STARTER_RNG.config(RUN.language(), STARTER), NOTIFICATION_ERROR_RECOVERABLE + ); + } + StoryExecutor executor(story); + const StoryRunPlan plan = plan_story_run( + checkpoints, first, last, + StoryHandoffs{.starter_rng = Pokemon::rng_handoff_mode({.stop = STOP_FOR_STARTER_RNG, .run = STARTER_RNG.run()})} + ); + executor.set_stop_rows(rows_that_stop(plan)); + run_plan(env, context, plan, first == 0 && NEW_GAME_INTRO, executor); +} + +void BDSPAutoStory::run_plan( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const StoryRunPlan& plan, bool new_game, StoryExecutor& executor +){ + // A start with a save always loads it first, so the run never walks + // from wherever the game was left (console B, 2026-11-11: a run from + // 00.04b walked from 00.04d's position). + const Pokemon::StoryStart start = new_game ? Pokemon::StoryStart::NEW_GAME : Pokemon::StoryStart::FROM_SAVE; + if (new_game && RUN.VERIFY.policy()){ + throw UserSetupError(env.console, "Verify mode reloads the start's save between runs: turn off \"Start a new game first\"."); + } + if (plan.stop != StoryHandoff::NONE && RUN.VERIFY.policy()){ + throw UserSetupError(env.console, "Verify mode repeats the start's checkpoint, and its extra save at the end of 00.01 would overwrite the Verity Lakefront save: turn off \"Stop for Starter RNG\"."); + } + std::set saved = saved_before_the_run(plan.rows, 0, start); + Pokemon::Segment segment = make_story_segment(plan.rows, executor, saved, RUN.policy().save_mode); + Pokemon::StoryGameHooks game = story_game(env, context); + Pokemon::RunState state; + start_story(env, context, plan.rows.front(), start, game, state); + if (const std::optional verify = RUN.VERIFY.policy()){ + const std::string summary = verify_story_checkpoint(plan.rows.front().id, *verify, segment, game, state); + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, summary); + return; + } + const std::string done = run_story( + Pokemon::SegmentRange{plan.rows.front().id, plan.rows.back().id}, RUN.policy(), segment, game, state, env + ); + if (plan.stop == StoryHandoff::STARTER_RNG){ + const std::string handoff = Pokemon::report_rng_handoff_stop( + env.console, std::format("Stopped for Starter RNG. {}", STARTER_RNG_HANDOFF_TEXT), {}, + [&](const std::string& line){ env.console.overlay().add_log(line); } + ); + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, handoff); + return; + } + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, done); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h new file mode 100644 index 0000000000..391761bb2d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h @@ -0,0 +1,77 @@ +/* BDSP AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Plays Brilliant Diamond / Shining Pearl from a new game to Floaroma Town: + * the story table (PokemonBDSP_AutoStory_StoryTable.h) run by the shared + * story engine, from the start checkpoint to the end one. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_H +#define PokemonAutomation_PokemonBDSP_AutoStory_H + +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/StringOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP_AutoStory_StarterRng.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Descriptor for the BDSP AutoStory single-switch program. +class BDSPAutoStory_Descriptor : public SingleSwitchProgramDescriptor{ +public: + BDSPAutoStory_Descriptor(); + + virtual std::unique_ptr make_stats() const override; +}; + + +// Single-switch program instance executing BDSP story automation. +class BDSPAutoStory : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +public: + using Descriptor = BDSPAutoStory_Descriptor; + ~BDSPAutoStory(); + BDSPAutoStory(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + virtual void on_config_value_changed(void* object) override; + + // Starts the plan's story and runs it to its end or its stop. + void run_plan( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const StoryRunPlan& plan, bool new_game, StoryExecutor& executor + ); + + // The start and end checkpoints, attempts, checks and language. + Pokemon::StoryRunOptions RUN; + BooleanCheckBoxOption NEW_GAME_INTRO; + StringOption PLAYER_NAME; + PlayerModelOption PLAYER_LOOK; + EnumDropdownOption STARTER; + // The RNG program the run stops for (StoryHandoff), or runs in 00.02. + BooleanCheckBoxOption STOP_FOR_STARTER_RNG; + StoryStarterRngOptions STARTER_RNG; + + EventNotificationsOption NOTIFICATIONS; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h new file mode 100644 index 0000000000..1c610368b4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h @@ -0,0 +1,220 @@ +/* BDSP AutoStory Plan Golden + * + * From: https://github.com/PokemonAutomation/ + * + * The legs the planner plans for every ROUTE of the story simulation + * (PokemonBDSP_AutoStory_StorySimulation_Tests.cpp, "plans match the + * golden"): per route a header " costs= from + * to ", then one line per leg with the tiles it may end on. + * One string per line: MSVC caps a single literal at 16 KB. + * Regenerate from the test's failure log; see test_golden_plans(). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlanGolden_H +#define PokemonAutomation_PokemonBDSP_AutoStory_PlanGolden_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +// The stored plan lines, in the order the simulation plans them. +inline constexpr std::string_view BDSP_PLAN_GOLDEN[] = { + "00.01 below_the_flute costs=default from PlayerHouse2F (4, 6) to PlayerHouse2F (7, 5)", + "00.01 north 1 (to wall) -> PlayerHouse2F (4, 5)", + "00.01 east 3 (open) -> PlayerHouse2F (7, 5)", + "00.01 above_the_door costs=default from PlayerHouse1F (10, 3) (10, 4) to PlayerHouse1F (6, 9)", + "00.01 south 7 (to wall) -> PlayerHouse1F (10, 10)", + "00.01 north 1 (open) -> PlayerHouse1F (10, 9)", + "00.01 west 4 (open) -> PlayerHouse1F (6, 9)", + "00.01 short_of_barrys_door costs=default from TwinleafTown (20, 22) to TwinleafTown (10, 12)", + "00.01 west 7 (open) -> Twinleaf Town (13, 22)", + "00.01 north 10 (to wall) -> Twinleaf Town (13, 12)", + "00.01 west 3 (open) -> Twinleaf Town (10, 12)", + "00.01 below_barrys_stairs costs=default from RivalHouse1F (6, 9) to RivalHouse1F (2, 3)", + "00.01 west 4 (to wall) -> RivalHouse1F (2, 9)", + "00.01 north 6 (to wall) -> RivalHouse1F (2, 3)", + "00.01 above_barrys_door costs=default from RivalHouse1F (2, 3) to RivalHouse1F (6, 9)", + "00.01 south 6 (open) -> RivalHouse1F (2, 9)", + "00.01 east 4 (open) -> RivalHouse1F (6, 9)", + "00.01 below_the_lake_entrance costs=default from Route201 (14, 16) to VerityLakefront (17, 14)", + "00.01 west 28 (to wall) -> Verity Lakefront (18, 23)", + "00.01 north 9 (to wall) -> Verity Lakefront (18, 14)", + "00.01 west 1 (open) -> Verity Lakefront (17, 14)", + "00.02 to_the_lake_entrance costs=default from VerityLakefront (17, 14) to VerityLakefront (17, 13)", + "00.02 north 1 (open) -> Verity Lakefront (17, 13)", + "00.03 into_rowans_scene costs=into_scene from VerityLakefront (17, 14) (17, 15) to VerityLakefront (17, 21)", + "00.03 south 7 (to wall) -> Verity Lakefront (17, 21)", + "00.03 into_dawns_scene costs=into_scene from TwinleafTown (20, 22) to Route201 (63, 5)", + "00.03 west 3 (open) -> Twinleaf Town (17, 22)", + "00.03 north 34 (to wall) -> Route 201 (17, 13)", + "00.03 east 40 (to wall) -> Route 201 (57, 13)", + "00.03 west 7 (open) -> Route 201 (50, 13)", + "00.03 north 10 (to wall) -> Route 201 (50, 3)", + "00.03 east 13 (to wall) -> Route 201 (63, 3)", + "00.03 south 2 (open) -> Route 201 (63, 5)", + "00.04 onto_the_path costs=town from SandgemTown (26, 11) (27, 11) (28, 11) (26, 12) (27, 12) (28, 12) to SandgemTown (29, 12)", + "00.04 east 3 (to wall) -> Sandgem Town (29, 11) Sandgem Town (29, 12)", + "00.04 north 1 (to wall) -> Sandgem Town (29, 11)", + "00.04 south 1 (open) -> Sandgem Town (29, 12)", + "00.04 to_ledge_column costs=line from Route201 (57, 4) (58, 4) (59, 4) (60, 4) (61, 4) (62, 4) (63, 4) to Route201 (55, 5)", + "00.04 west 7 (to wall) -> Route 201 (56, 4)", + "00.04 south 1 (open) -> Route 201 (56, 5)", + "00.04 west 1 (open) -> Route 201 (55, 5)", + "00.04 along_route_201_road costs=line from Route201 (55, 14) to Route201 (15, 14)", + "00.04 west 16 (open) -> Route 201 (39, 14)", + "00.04 north 3 (open) -> Route 201 (39, 11)", + "00.04 west 24 (open) -> Route 201 (15, 11)", + "00.04 south 2 (open) -> Route 201 (15, 14)", + "00.04 into_twinleaf costs=line from Route201 (15, 14) to TwinleafTown (15, 22)", + "00.04 south 33 (open) -> Twinleaf Town (15, 22)", + "00.04 to_moms_doorstep costs=line from TwinleafTown (15, 22) to TwinleafTown (20, 22)", + "00.04 east 5 (open) -> Twinleaf Town (20, 22)", + "00.04 beside_mom costs=default from PlayerHouse1F (6, 9) to PlayerHouse1F (6, 8)", + "00.04 north 1 (open) -> PlayerHouse1F (6, 8)", + "00.04 into_moms_call costs=trigger from PlayerHouse1F (11, 8) to PlayerHouse1F (6, 9)", + "00.04 west 1 (open) -> PlayerHouse1F (10, 8)", + "00.04 south 1 (open) -> PlayerHouse1F (10, 9)", + "00.04 west 4 (open) -> PlayerHouse1F (6, 9)", + "00.04 short_of_the_catch_tutorial costs=default from TwinleafTown (6, 22) (6, 23) to Route202 (22, 27)", + "00.04 north 19 (to wall) -> Twinleaf Town (6, 4)", + "00.04 east 8 (open) -> Twinleaf Town (14, 4)", + "00.04 north 16 (to wall) -> Route 201 (14, 13)", + "00.04 east 43 (to wall) -> Route 201 (57, 13)", + "00.04 west 7 (open) -> Route 201 (50, 13)", + "00.04 north 7 (open) -> Route 201 (50, 6)", + "00.04 east 43 (to wall) -> Sandgem Town (29, 13)", + "00.04 north 2 (to wall) -> Sandgem Town (29, 11)", + "00.04 west 5 (open) -> Sandgem Town (24, 11)", + "00.04 north 19 (to wall) -> Route 202 (25, 24)", + "00.04 west 3 (to wall) -> Route 202 (22, 24)", + "00.04 south 3 (open) -> Route 202 (22, 27)", + "00.04b to_catch_grass costs=default 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) to Route202 (18, 25) (19, 25) (20, 25) (18, 26) (19, 26) (20, 26) (18, 27) (19, 27) (20, 27)", + "00.04b 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)", + "00.04b east 8 (to wall) -> Route 202 (23, 25)", + "00.04b west 3 (open) -> Route 202 (20, 25)", + "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) to SandgemTown (29, 5)", + "00.04b south 2 (to wall) -> Route 202 (18, 27) Route 202 (19, 27) Route 202 (20, 27)", + "00.04b east 12 (to wall) -> Route 202 (30, 27)", + "00.04b south 10 (to wall) -> Sandgem Town (29, 5)", + "00.04d north_to_jubilife costs=default from Route202 (30, 24) to 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)", + "00.04d south 2 (open) -> Route 202 (30, 26)", + "00.04d west 27 (to wall) -> Route 202 (3, 26)", + "00.04d north 12 (to wall) -> Route 202 (3, 14)", + "00.04d east 14 (to wall) -> Route 202 (17, 14)", + "00.04d south 8 (to wall) -> Route 202 (17, 22)", + "00.04d east 13 (to wall) -> Route 202 (30, 22)", + "00.04d north 18 (to wall) -> Route 202 (30, 4)", + "00.04d south 1 (open) -> Route 202 (30, 5)", + "00.04d west 12 (open) -> Route 202 (18, 5)", + "00.04d north 2 (open) -> Route 202 (18, 3)", + "00.05 to_the_school costs=town from JubilifeCity (52, 41) to JubilifeCity (40, 41)", + "00.05 west 12 (open) -> Jubilife City (40, 41)", + "00.05b below_the_presidents_trigger costs=town from JubilifeCity (40, 41) to JubilifeCity (46, 41)", + "00.05b east 6 (open) -> Jubilife City (46, 41)", + "00.05b past_the_president costs=default from JubilifeCity (46, 40) to JubilifeCity (48, 40)", + "00.05b east 2 (open) -> Jubilife City (48, 40)", + "00.05b below_the_mart_clown costs=town from JubilifeCity (48, 40) to JubilifeCity (56, 36)", + "00.05b north 4 (open) -> Jubilife City (48, 36)", + "00.05b east 8 (to wall) -> Jubilife City (56, 36)", + "00.05b below_the_tv_clown costs=town from JubilifeCity (56, 31) to JubilifeCity (36, 17)", + "00.05b west 8 (open) -> Jubilife City (48, 31)", + "00.05b north 7 (open) -> Jubilife City (48, 24)", + "00.05b west 12 (open) -> Jubilife City (36, 24)", + "00.05b north 7 (to wall) -> Jubilife City (36, 17)", + "00.05b below_the_company_clown costs=town from JubilifeCity (36, 17) to JubilifeCity (15, 19)", + "00.05b south 4 (open) -> Jubilife City (36, 21)", + "00.05b west 21 (open) -> Jubilife City (15, 21)", + "00.05b north 2 (open) -> Jubilife City (15, 19)", + "00.05b beside_the_company_sign costs=line from JubilifeCity (14, 19) to JubilifeCity (14, 19)", + "00.05c beside_the_president costs=town from JubilifeCity (14, 19) to JubilifeCity (45, 35)", + "00.05c south 4 (open) -> Jubilife City (14, 23)", + "00.05c east 18 (open) -> Jubilife City (32, 23)", + "00.05c south 12 (open) -> Jubilife City (32, 35)", + "00.05c east 13 (to wall) -> Jubilife City (45, 35)", + "00.05c below_the_east_exit_sign costs=line from JubilifeCity (45, 35) to JubilifeCity (59, 21)", + "00.05c north 9 (open) -> Jubilife City (45, 26)", + "00.05c east 14 (to wall) -> Jubilife City (59, 26)", + "00.05c north 5 (to wall) -> Jubilife City (59, 21)", + "01.01 west_of_barrys_trigger costs=town from JubilifeCity (59, 21) to Route203 (4, 18)", + "01.01 south 1 (open) -> Jubilife City (59, 22)", + "01.01 east 8 (open) -> Route 203 (4, 18)", + "01.02 south_to_the_cliff costs=default from Route203 (5, 17) (5, 18) (5, 19) (5, 20) to Route203 (5, 20)", + "01.02 south 3 (to wall) -> Route 203 (5, 20)", + "01.02 by_the_gate costs=default from Route203 (5, 20) to Route203 (55, 9)", + "01.02 north 2 (open) -> Route 203 (5, 18)", + "01.02 east 50 (to wall) -> Route 203 (55, 18)", + "01.02 north 9 (open) -> Route 203 (55, 9)", + "01.02 into_the_hikers_scene costs=into_scene from OreburghGate (4, 22) (5, 22) to OreburghGate (4, 22) (5, 22)", + "01.02 past_curtis costs=exit from OreburghGate (7, 22) to OreburghGate (20, 22)", + "01.02 east 13 (open) -> OreburghGate (20, 22)", + "01.02 by_the_east_mouth costs=exit from OreburghGate (20, 22) to OreburghGate (26, 22)", + "01.02 east 6 (open) -> OreburghGate (26, 22)", + "01.02 into_the_welcome_scene costs=into_scene from OreburghCity (2, 13) (3, 13) to OreburghCity (2, 13) (3, 13)", + "01.02b below_the_centers_door costs=town from OreburghCity (26, 22) to OreburghCity (47, 21)", + "01.02b east 22 (to wall) -> Oreburgh City (48, 22)", + "01.02b north 1 (to wall) -> Oreburgh City (48, 21)", + "01.02b west 1 (open) -> Oreburgh City (47, 21)", + "01.03 above_the_mine costs=town from OreburghCity (47, 21) (46, 21) to OreburghCity (46, 57)", + "01.03 east 2 (to wall) -> Oreburgh City (48, 21)", + "01.03 west 4 (open) -> Oreburgh City (44, 21)", + "01.03 south 20 (to wall) -> Oreburgh City (44, 41)", + "01.03 west 1 (open) -> Oreburgh City (43, 41)", + "01.03 south 16 (to wall) -> Oreburgh City (43, 57)", + "01.03 east 3 (open) -> Oreburgh City (46, 57)", + "01.05 east_of_barrys_trigger costs=town from OreburghCity (26, 21) (26, 22) to OreburghCity (7, 13)", + "01.05 south 5 (to wall) -> Oreburgh City (26, 26)", + "01.05 north 1 (open) -> Oreburgh City (26, 25)", + "01.05 west 14 (to wall) -> Oreburgh City (12, 25)", + "01.05 north 12 (open) -> Oreburgh City (12, 13)", + "01.05 west 5 (open) -> Oreburgh City (7, 13)", + "01.05 through_the_gate costs=default from OreburghGate (27, 22) (26, 22) to OreburghGate (5, 22)", + "01.05 north 2 (to wall) -> OreburghGate (27, 20) OreburghGate (26, 21)", + "01.05 east 1 (to wall) -> OreburghGate (27, 20) OreburghGate (27, 21)", + "01.05 south 3 (to wall) -> OreburghGate (27, 23)", + "01.05 west 19 (to wall) -> OreburghGate (8, 23)", + "01.05 north 1 (to wall) -> OreburghGate (8, 22)", + "01.05 west 3 (open) -> OreburghGate (5, 22)", + "01.06 up_jubilifes_road costs=town from Route203 (55, 9) to JubilifeCity (41, 8)", + "01.06 south 10 (to wall) -> Route 203 (55, 19)", + "01.06 west 17 (to wall) -> Route 203 (38, 19)", + "01.06 north 1 (open) -> Route 203 (38, 18)", + "01.06 west 60 (open) -> Jubilife City (41, 22)", + "01.06 north 14 (to wall) -> Jubilife City (41, 8)", + "01.06 below_rowans_trigger costs=town from JubilifeCity (41, 8) to JubilifeCity (47, 8)", + "01.06 east 6 (to wall) -> Jubilife City (47, 8)", + "01.08 west_to_the_wall costs=default from JubilifeCity (46, 5) (47, 5) to JubilifeCity (44, 5)", + "01.08 west 3 (to wall) -> Jubilife City (44, 5)", + "01.08 below_the_ravaged_path costs=town from JubilifeCity (44, 5) to Route204South (11, 3)", + "01.08 east 2 (open) -> Jubilife City (46, 5)", + "01.08 north 19 (to wall) -> Route 204 (14, 18)", + "01.08 west 4 (open) -> Route 204 (10, 18)", + "01.08 north 15 (to wall) -> Route 204 (10, 3)", + "01.08 east 1 (open) -> Route 204 (11, 3)", + "01.09 west_of_the_rock costs=default from RavagedPath (19, 50) to RavagedPath (21, 43)", + "01.09 north 7 (open) -> RavagedPath (19, 43)", + "01.09 east 2 (open) -> RavagedPath (21, 43)", + "01.09 above_the_way_out costs=default from RavagedPath (21, 43) to RavagedPath (28, 44)", + "01.09 east 8 (to wall) -> RavagedPath (29, 43)", + "01.09 south 1 (to wall) -> RavagedPath (29, 44)", + "01.09 west 1 (open) -> RavagedPath (28, 44)", + "01.10 line_1 costs=line from Route204North (20, 26) to Route204North (20, 27)", + "01.10 south 1 (open) -> Route 204 (20, 27)", + "01.10 line_2 costs=line from Route204North (20, 27) to Route204North (11, 27)", + "01.10 west 9 (open) -> Route 204 (11, 27)", + "01.10 line_3 costs=line from Route204North (11, 27) to Route204North (11, 22)", + "01.10 north 5 (open) -> Route 204 (11, 22)", + "01.10 line_4 costs=line from Route204North (11, 22) to Route204North (19, 22)", + "01.10 east 8 (open) -> Route 204 (19, 22)", + "01.10 line_5 costs=line from Route204North (19, 22) to Route204North (19, 12)", + "01.10 north 10 (open) -> Route 204 (19, 12)", + "01.10 line_6 costs=line from Route204North (19, 12) to Route204North (11, 12)", + "01.10 west 8 (open) -> Route 204 (11, 12)", +}; + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp new file mode 100644 index 0000000000..f559241a59 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp @@ -0,0 +1,1002 @@ +/* BDSP AutoStory Planned Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_route() (Utils/PokemonBDSP_AutoStory_PlannedRoute.h) on a script + * of console outcomes: each case lists the walks, presses and screen reads + * the walker must make, in order, and what the console answers. A call the + * script does not expect fails the case. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP_AutoStory_PlannedRoute_Tests.h" +#include "Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Pose; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +// One call the walker is expected to make, and what the console answers: +// `walk` for a walk, `answer` for a dialogue watch or the holding check. +struct ScriptStep{ + std::string call; + WalkResult walk; + bool answer; +}; + +ScriptStep walk_step(std::string call, WalkStop stop, Milliseconds walked, size_t moved){ + return {.call = std::move(call), .walk = {.stop = stop, .walked = walked, .wild_battles = 0, .moved_chunks = moved}, .answer = false}; +} +ScriptStep answer_step(std::string call, bool answer){ + return {.call = std::move(call), .walk = {.stop = WalkStop::LIMIT, .walked = 0ms, .wild_battles = 0, .moved_chunks = 0}, .answer = answer}; +} +ScriptStep press_step(std::string call){ + return answer_step(std::move(call), false); +} + +// What the console did on a scripted walk: the steps called so far, every +// line logged, and the legs walked (to a wall, or a number of taps). +struct Script{ + std::vector steps; + size_t next = 0; + std::vector notes = {}; + std::vector legs = {}; +}; + +// Thrown by ScriptedRouteIO: a call the script did not expect, or the +// walker failing the program. +struct ScriptMismatch{ + std::string message; +}; +struct RouteFailed{ + std::string message; +}; + +std::string describe(const RouteInput& input){ + const std::string_view kind = + input.kind == RouteInputKind::DPAD ? "D-pad" : + input.kind == RouteInputKind::STICK ? "stick" : + input.kind == RouteInputKind::BUTTON_A ? "A" : + input.kind == RouteInputKind::WAIT ? "wait" : "pause"; + const bool directed = input.kind == RouteInputKind::DPAD || input.kind == RouteInputKind::STICK; + return std::format("{}{}{} {} ms x{}", kind, directed ? " " : "", directed ? Pokemon::direction_name(input.direction) : "", + input.hold.count(), input.count); +} + +// The console as a script: each call must be the script's next step. +// look() sees no screen. +class ScriptedRouteIO : public RouteIO{ +public: + explicit ScriptedRouteIO(Script& script) + : m_script(script) + {} + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + m_script.legs.push_back({.direction = walk.direction, .tiles = 0, .ends_at_wall = true, .held = false}); + return expect("wall: " + walk.label).walk; + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string& label) override{ + m_script.legs.push_back({.direction = direction, .tiles = tiles, .ends_at_wall = false, .held = false}); + return expect(std::format("{} taps: {}", tiles, label)).walk; + } + virtual void press(const RouteInput& input) override{ + expect(describe(input)); + } + virtual RouteSight sight_within(const RouteWatch& watch) override{ + if (!watch.dialog){ + return {}; + } + return {.dialog = expect(std::format("dialog within {} ms", watch.wait.count())).answer}; + } + virtual bool holding_against_wall(Direction direction, const std::string&) override{ + return expect(std::string("holding ") + Pokemon::direction_name(direction)).answer; + } + virtual void look(const std::function&) override{} + virtual void note(const std::string& text, RouteNote kind) override{ + m_script.notes.push_back(text); + if (kind == RouteNote::FAILURE){ + throw RouteFailed{text}; + } + } + +private: + const ScriptStep& expect(const std::string& call){ + if (m_script.next >= m_script.steps.size()){ + throw ScriptMismatch{"a call after the script's end: \"" + call + "\""}; + } + const ScriptStep& step = m_script.steps[m_script.next++]; + if (step.call != call){ + throw ScriptMismatch{std::format("step {}: expected \"{}\", got \"{}\"", m_script.next, step.call, call)}; + } + return step; + } + + Script& m_script; +}; + + +// How a scripted walk ended: where it arrived, or why it failed. +struct ScriptedWalk{ + std::vector at; + std::string failure; + std::vector fights; +}; + +// What follows each talk that is fought. +enum class AfterTalk{ + BATTLE, + NO_BATTLE, +}; + +ScriptedWalk walk_script_from(const StoryWorld& maps, std::vector from, const TilePos& goal, std::vector sights, Script& script, + AfterTalk after_talk = AfterTalk::BATTLE +){ + ScriptedWalk out; + RouteTrainers trainers{ + .sights = std::move(sights), + .fight = [&out, after_talk](const std::string& trainer){ out.fights.push_back(trainer); return after_talk == AfterTalk::BATTLE; }, + }; + ScriptedRouteIO io(script); + try{ + out.at = walk_route(io, maps, std::move(from), trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = route_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + out.failure = e.message; + } + return out; +} + +ScriptedWalk walk_script(const StoryWorld& maps, const TilePos& from, const TilePos& goal, std::vector sights, Script& script, + AfterTalk after_talk = AfterTalk::BATTLE +){ + return walk_script_from(maps, {from}, goal, std::move(sights), script, after_talk); +} + +std::string joined_notes(const Script& script){ + std::string out; + for (const std::string& note : script.notes){ + out += "\n " + note; + } + return out; +} + +// The script's calls all made, or which are left. +std::string script_left(const Script& script){ + if (script.next == script.steps.size()){ + return ""; + } + return std::format("{} of {} steps called; next \"{}\".", script.next, script.steps.size(), script.steps[script.next].call); +} + +StoryWorld make_world(const std::vector& rows){ + Pokemon::MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{(int)rows[0].size(), (int)rows.size(), {}}; + for (const std::string& row : rows){ + for (char glyph : row){ + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(glyph)); + } + } + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + + +// r54: a trainer's line froze the check walk at a leg's wall; after the +// battle the walk on reached the wall, but the check still judged the +// frozen walk's DIALOG and failed. The walk on is what counts. +UnitTestResult test_wall_leg_trainer_on_the_way(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), + walk_step("wall: T: east 4 (to wall): on to the wall", WalkStop::BLOCKED, 1200ms, 0), + answer_step("dialog within 500 ms", false), + }}; + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + if (!walk.failure.empty()){ + return "failed after the walk on reached the wall: " + walk.failure; + } + if (walk.fights != std::vector{"a trainer on the way"}){ + return std::format("fought {} times, expected once.", walk.fights.size()); + } + if (walk.at != std::vector{{5, 0}}){ + return "did not arrive at (5, 0)."; + } + }catch (const ScriptMismatch& e){ + return e.message; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// lab-B 2026-11-12 06:12 (00.05c below_the_company_clown, the 6th failure +// there): the Wonder Station's woman stopped the check walk of "west 1 (to +// wall)", the fight saw no battle follow and pressed her talk to its end, +// so nothing showed when we looked again; the walk on to the wall then +// started her talk again and ran out of time. A talk the fight saw end +// with no battle plans again from where it stopped, never walks on. +UnitTestResult test_wall_leg_talk_no_battle(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), + }}; + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script, AfterTalk::NO_BATTLE); + if (!walk.failure.empty()){ + return "failed after a talk no battle followed: " + walk.failure; + } + if (walk.at != std::vector{{5, 0}}){ + return "did not plan again from the wall it stopped at, (5, 0)."; + } + }catch (const ScriptMismatch& e){ + return e.message; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// A leg to a wall that ran out of time: a check walk that also runs out +// of time counts as at the wall only if holding on there changes the +// screen less than walking (r127a ran along the wrong row). +struct WallCheckCase{ + std::string name; + std::vector check; + bool fails; +}; + +UnitTestResult test_wall_leg_limit(Logger&){ + const std::vector cases{ + {.name = "the check stops at the wall", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::BLOCKED, 1050ms, 0), + answer_step("dialog within 500 ms", false), + }, .fails = false}, + {.name = "the check runs out of time, holding on reads a wall", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 0), + answer_step("holding east", true), + answer_step("dialog within 500 ms", false), + }, .fails = false}, + {.name = "the check runs out of time, holding on still moves", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 0), + answer_step("holding east", false), + }, .fails = true}, + }; + for (const WallCheckCase& item : cases){ + Script script{.steps = {walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0)}}; + script.steps.insert(script.steps.end(), item.check.begin(), item.check.end()); + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + if (walk.failure.empty() == item.fails){ + return item.name + (item.fails ? ": went on as at the wall." : ": failed: " + walk.failure); + } + }catch (const ScriptMismatch& e){ + return item.name + ": " + e.message; + } + const std::string left = script_left(script); + if (!left.empty()){ + return item.name + ": " + left; + } + } + return true; +} + + +// Whether walking `legs` from `from` ever steps into `person`'s tile: a +// leg to a wall that stops against them, or a tap toward them. +bool walks_into(const StoryWorld& world, TilePos from, const std::vector& legs, const TilePos& person){ + for (const TileLeg& leg : legs){ + for (size_t tap = 0; leg.ends_at_wall || tap < leg.tiles; tap++){ + if (from.next(leg.direction) == person){ + return true; + } + const std::optional next = step(world, from, leg.direction); + if (!next){ + break; + } + from = *next; + } + } + return false; +} + +// Route 202 alone, at its own tiles, and Logan's sight: he stands at +// (26, 4) and looks south. +StoryWorld route202_world(){ + StoryWorld world; + world.add("Route 202", story_map("Route202"), {0, 0}); + return world; +} +std::vector logan_sight(){ + std::vector sights; + for (const TrainerSight& sight : route202_trainer_sights()){ + if (sight.name == "logan"){ + sights.push_back(sight); + } + } + return sights; +} + +// Whether the replan after the battle starts from each of `tiles`. +std::string check_replan_from(const Script& script, const std::vector& tiles){ + const auto replan = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("planning again") != std::string::npos; + }); + if (replan == script.notes.end()){ + return "never planned again after the battle."; + } + for (const std::string& tile : tiles){ + if (replan->find(tile) == std::string::npos){ + return "the replan does not start from " + tile + ": " + *replan; + } + } + return ""; +} + +// r55: Logan (Route 202, (26, 4), looking south) stopped an open leg west +// along row 5. "5 taps" were 4, under his nose at (26, 5); the replan from +// the tap count alone walked into him. The replan must start from both the +// tile the taps give and the tile where the leg enters his sight, and walk +// on from either without bumping into him. +UnitTestResult test_open_leg_known_trainer(Logger&){ + const StoryWorld world = route202_world(); + // West along row 5 from (30, 5) to (24, 5): his line is the 4th tap, + // and his dialogue comes after 3 taps that moved. + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::DIALOG, 1500ms, 3), + press_step("D-pad east 50 ms x1"), + walk_step("wall: T: east 4 (to wall)", WalkStop::BLOCKED, 2000ms, 0), + answer_step("dialog within 500 ms", false), + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::LIMIT, 1500ms, 6), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(world, {30, 5}, {24, 5}, logan_sight(), script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty() || walk.fights != std::vector{"logan"}){ + return std::format("expected to fight Logan once and go on; fought {}, failure \"{}\".", walk.fights.size(), walk.failure); + } + // The taps say (27, 5); his sight line crosses the row at (26, 5). + const std::string replan = check_replan_from(script, {"Route 202 (27, 5)", "Route 202 (26, 5)"}); + if (!replan.empty()){ + return replan; + } + const std::vector after_battle(script.legs.begin() + 1, script.legs.end()); + const TilePos logan = map_point("Route202", "trainer_logan"); + for (const TilePos& from : {TilePos{27, 5}, TilePos{26, 5}}){ + if (walks_into(world, from, after_battle, logan)){ + return "the replan walks into Logan from " + Pokemon::to_string(from) + "."; + } + } + if (walk.at != std::vector{{24, 5}}){ + return "did not arrive at (24, 5)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// 01.08's walk below the Ravaged Path (lab-B 2026-11-08 16:50): the leg +// north up Route 204 South ended at (13, 2), and the leg "west 2 (to +// wall)" to (11, 2) stopped at once, 0 chunks moved: the wandering +// youngster (the map's youngster_3, wandering (12, 2) to (14, 2)) stood on +// (12, 2). The world joins Jubilife and Route 204 South, as the step's. +const StoryWorld& ravaged_path_world(){ + static const StoryWorld* const world = story_plan_world("JubilifeCity", "Route204South"); + return *world; +} +TilePos on_route204_south(TilePos local){ + return story_to_world(ravaged_path_world(), "Route204South", local); +} + +// Routes no longer run along a wander zone, so that leg is no longer +// planned on Route 204 South: the walks below replay it on a plain room, +// from (3, 1) "west 2 (to wall)" to (1, 1), the person on (2, 1), with a +// row below to plan round them. +StoryWorld youngster_room(){ + return make_world({"#####", "#...#", "#...#", "#####"}); +} + +// The console's first walk west: blocked after 800 ms, no chunk moved. +ScriptStep west_never_moved(){ + return walk_step("wall: T: west 2 (to wall)", WalkStop::BLOCKED, 1050ms, 0); +} + +// A youngster who steps off the leg's next tile while we wait is waited +// out, on a leg of two tiles too: the walk west then reaches the wall at +// (11, 2). The wait used to need a stop at least three tiles short of the +// wall, so the person one tile ahead was talked to instead. +UnitTestResult test_short_leg_waits_for_the_way(Logger&){ + Script script{.steps = { + west_never_moved(), + press_step("wait 2500 ms x1"), + walk_step("wall: T: west 2 (to wall)", WalkStop::BLOCKED, 3000ms, 2), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(youngster_room(), {3, 1}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty() || !walk.fights.empty()){ + return std::format("expected to wait and walk on; fought {}, failure \"{}\".", walk.fights.size(), walk.failure); + } + if (walk.at != std::vector{{1, 1}}){ + return "did not arrive at (1, 1)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// Whether the replan after the youngster's talk starts from (3, 1) alone, +// with (2, 1) marked, and plans a route. +std::string check_never_moved_replan(const Script& script){ + const std::string replan = check_replan_from(script, {"(3, 1)"}); + if (!replan.empty()){ + return replan; + } + const auto replan_note = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("planning again") != std::string::npos; + }); + if (replan_note->find("(2, 1)") != std::string::npos){ + return "the replan still starts from the youngster's tile: " + *replan_note; + } + const auto marked = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("marked it") != std::string::npos; + }); + if (marked == script.notes.end() || marked->find("(2, 1)") == std::string::npos){ + return "the youngster's tile (2, 1) was not marked."; + } + const size_t plans = std::count_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("legs planned") != std::string::npos; + }); + if (plans != 2){ + return std::format("{} plans made, expected the first and the replan.", plans); + } + return ""; +} + +// The youngster stays on (12, 2) through both waits and talks without a +// battle. The console then planned from {(12, 2), (13, 2)}, one tile per +// tile the walk may have moved, and found no plan: an uncertain start +// needs a wall both tiles reach (wall_recovery_candidates()), and no wall +// is anchored from a tile someone wanders on, which both are. A walk that +// never moved stopped where it began: the replan starts from the start +// alone, with the youngster's tile marked (on youngster_room()). +UnitTestResult test_never_moved_plans_from_the_start(Logger& logger){ + const StoryWorld& world = ravaged_path_world(); + const std::vector console_from{on_route204_south({12, 2}), on_route204_south({13, 2})}; + if (!wall_recovery_candidates(world, console_from).empty()){ + return "the console's two tiles have a wall to anchor on: the cause is not the wander box."; + } + logger.log("No wall anchors from Route 204 South (12, 2) and (13, 2): both are in youngster_3's wander box."); + Script script{.steps = { + west_never_moved(), + press_step("wait 2500 ms x1"), + west_never_moved(), + press_step("wait 2500 ms x1"), + west_never_moved(), + press_step("A 80 ms x1"), + answer_step("dialog within 2000 ms", true), + }}; + ScriptedWalk walk; + std::string stopped_at; + try{ + walk = walk_script(youngster_room(), {3, 1}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + // The walk after the replan is not scripted. + stopped_at = e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return "stopped at: " + stopped_at; + } + for (const std::string& note : script.notes){ + logger.log(note); + } + const std::string replan = check_never_moved_replan(script); + return replan.empty() ? UnitTestResult(true) : UnitTestResult(replan); +} + +// lab-B 2026-11-12 01:22 (00.05c below_the_company_clown): an open leg +// west was blocked by someone the map lacks, and the taps after the wait +// opened their talk (the Global Wonder Station's woman). Taken for a +// trainer, the talk was "fought", no battle followed, and the replans +// walked into her again until the third talk failed the step. Whoever +// talks as we tap on after a block is the one in the way: their tile is +// marked, and the walk plans again from where it stopped. +UnitTestResult test_open_leg_blocker_talks(Logger& logger){ + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::BLOCKED, 2000ms, 2), + press_step("wait 2500 ms x1"), + walk_step("4 taps: T: west 6 (open): the rest", WalkStop::DIALOG, 800ms, 0), + }}; + // West along Route 202's row 5, as test_open_leg_known_trainer() walks + // it, with no trainer: the one in the way stands on (27, 5). + ScriptedWalk walk; + try{ + walk = walk_script(route202_world(), {30, 5}, {24, 5}, {}, script); + }catch (const ScriptMismatch&){ + // The walk after the replan is not scripted. + } + for (const std::string& note : script.notes){ + logger.log(note); + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return script_left(script); + } + const auto marked = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("marked it") != std::string::npos && note.find("(27, 5)") != std::string::npos; + }); + if (marked == script.notes.end()){ + return "the tile of whoever talked, (27, 5), was not marked."; + } + const std::string replan = check_replan_from(script, {"Route 202 (28, 5)"}); + return replan.empty() ? UnitTestResult(true) : UnitTestResult(replan); +} + +// lab-B 2026-11-12 01:55 (00.05c below_the_company_clown, again): with +// the woman who talked marked at (45, 24), the plan from (46, 24) and the +// tile behind it began "west 1 (to wall)" against her. Walking into her +// opened her talk again, which was fought as a trainer, and the walk on +// ran out of time short of the "wall". A person is no wall: the walk after +// the replan must not walk to a wall into the tile it marked. +UnitTestResult test_marked_talker_is_no_wall(Logger& logger){ + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::BLOCKED, 2000ms, 2), + press_step("wait 2500 ms x1"), + walk_step("4 taps: T: west 6 (open): the rest", WalkStop::DIALOG, 800ms, 0), + }}; + std::string after_replan; + try{ + walk_script(route202_world(), {30, 5}, {24, 5}, {}, script); + }catch (const ScriptMismatch& mismatch){ + after_replan = mismatch.message; + } + for (const std::string& note : script.notes){ + logger.log(note); + } + if (script.next != script.steps.size()){ + return script_left(script); + } + if (after_replan.find("\"wall: ") != std::string::npos && after_replan.find("west") != std::string::npos){ + return "after the replan the walk went to a wall west, into the one who talked: " + after_replan; + } + return UnitTestResult(true); +} + +// A leg to the wall two tiles away that runs out of time after one tile +// (one chunk moved): someone stands in it, as Route 204's youngster did on +// 01.08's "west 2 (to wall)" (lab-B 2026-11-08). The check at the wall +// allowed a tile short on legs of two or three tiles, so the stop against +// him passed as the wall and the route went on a tile off. Now it is a +// stop short of the wall: talk to whoever is there, then plan again. +UnitTestResult test_short_leg_a_tile_short_is_not_arrived(Logger&){ + const StoryWorld room = make_world({"####", "#..#", "#..#", "#..#", "####"}); + Script script{.steps = { + walk_step("wall: T: north 2 (to wall)", WalkStop::LIMIT, 1850ms, 1), + press_step("A 80 ms x1"), + answer_step("dialog within 2000 ms", false), + }}; + ScriptedWalk walk; + std::string stopped_at; + try{ + walk = walk_script(room, {1, 3}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + // The walk after the replan is not scripted. + stopped_at = e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return "went on as at the wall: " + stopped_at; + } + const bool replanned = std::any_of(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("stopped early after about 1 tiles") != std::string::npos && note.find("planning again") != std::string::npos; + }); + return replanned ? UnitTestResult(true) : UnitTestResult("no replan after the stop a tile short."); +} + + +// A leg to a wall 8 tiles away that stops far later than 8 tiles take: the +// player pushed against someone on the way, and the time counts 18 tiles +// (lab-B 2026-11-11 21:00, 00.05b: "east 8 (to wall)" stopped after +// 2800 ms against the boy below the Mart, about 5 tiles in, and the route +// went on from the wall). Not the wall: plan again from every tile of the +// leg, and a second leg to the wall pins it. +UnitTestResult test_wall_leg_held_up_is_not_at_its_wall(Logger&){ + const StoryWorld corridor = make_world({"###########", "#.........#", "###########"}); + Script script{.steps = { + walk_step("wall: T: east 8 (to wall)", WalkStop::BLOCKED, 3930ms, 5), + walk_step("wall: T: east 7 (to wall)", WalkStop::BLOCKED, 1450ms, 1), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(corridor, {1, 1}, {9, 1}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (walk.at != std::vector{{9, 1}}){ + return "did not arrive at (9, 1)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// lab-B 2026-11-12 14:50, 01.02 "south 3 (to wall)" on Route 203, straight +// after a battle: the start is one of four tiles, the player already stands +// at the wall, and the walk's three moved chunks (a turn counted as one) +// and 2000 ms read as held up. No tile of the leg past the wall fits, but +// the tile at the wall does: the leg ends there, it must not fail. +UnitTestResult test_wall_leg_at_its_wall_among_the_start_tiles(Logger&){ + const StoryWorld corridor = make_world({"###", "#.#", "#.#", "#.#", "#.#", "###"}); + Script script{.steps = { + walk_step("wall: T: south 3 (to wall)", WalkStop::BLOCKED, 2000ms, 3), + }}; + ScriptedWalk walk; + try{ + walk = walk_script_from(corridor, {{1, 1}, {1, 2}, {1, 3}, {1, 4}}, {1, 4}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (walk.at != std::vector{{1, 4}}){ + return "did not end at the wall (1, 4)."; + } + return true; +} + +// A leg to a wall in another direction than the player faces turns first, +// with a tap that only turns; the held walk then counts from its first +// step, not a turn (lab-B 2026-11-12 14:55, 01.02 south_to_the_cliff). +UnitTestResult test_wall_leg_turns_before_the_walk(Logger&){ + const StoryWorld bend = make_world({"#####", "#...#", "###.#", "###.#", "#####"}); + Script script{.steps = { + walk_step("wall: T: east 2 (to wall)", WalkStop::BLOCKED, 1900ms, 2), + answer_step("dialog within 500 ms", false), + press_step("D-pad south 50 ms x1"), + walk_step("wall: T: south 2 (to wall)", WalkStop::BLOCKED, 1900ms, 2), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(bend, {1, 1}, {3, 3}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// Facing unknown (after a battle), the first walk's chunk that changed the +// screen may be the turn: a leg to a wall that stopped with that one chunk +// has not moved, and whoever stopped it stands on the next tile. +UnitTestResult test_wall_leg_one_chunk_is_a_turn_when_facing_is_unknown(Logger&){ + const StoryWorld corridor = make_world({"###########", "#.........#", "###########"}); + Script script{.steps = { + walk_step("wall: T: east 8 (to wall)", WalkStop::BLOCKED, 800ms, 1), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(corridor, {1, 1}, {9, 1}, {}, script); + }catch (const ScriptMismatch& e){ + script.notes.push_back(e.message); + } + const bool never_moved = std::any_of(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("the first moved chunk may be the turn") != std::string::npos; + }); + return never_moved ? UnitTestResult(true) : UnitTestResult(std::format("the turn counted as a moved chunk (step {}, {} notes): {}{}", script.next, script.notes.size(), walk.failure, joined_notes(script))); +} + +// lab-B 2026-11-12 20:22, 01.02 "east 4 (to wall)": the player had left the +// Gate unseen and ran on in Oreburgh City; the leg hit its time limit still +// moving (6 moved chunks), and the "at the wall?" probe walked on 4 chunks +// more. A leg that is still moving past the farthest wall the map has is +// not where the plan has us: the walk stops, and nothing walks further. +UnitTestResult test_wall_leg_still_moving_at_limit_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 6)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// The probe after a leg that ran out of time: one that is still moving +// (4 moved chunks in 2000 ms) is not at a wall, so nothing asks the +// holding check and nothing walks on. +UnitTestResult test_wall_check_still_moving_is_unknown(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 4), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// A walk the banner of another map shows on, whatever its stop, went +// through a warp unseen. +ScriptStep through_a_warp_step(std::string call, WalkStop stop, Milliseconds walked, size_t moved){ + ScriptStep step = walk_step(std::move(call), stop, walked, moved); + step.walk.warp.banner = "Oreburgh City"; + return step; +} + +UnitTestResult test_wall_check_on_another_map_is_unknown(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + through_a_warp_step("wall: T: east 4 (to wall): at the wall?", WalkStop::BLOCKED, 1200ms, 0), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("another map") == std::string::npos){ + return "did not stop on the other map's banner: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// lab-B 2026-11-12 20:20, 01.02 past_curtis: no plan from the tiles left +// after a battle, so the walk held east "to find us again", ran through the +// Gate's east door unseen (5 moved chunks, 1850 ms against a wall 2 tiles +// away) and took itself for found at the wall. It is not found. +UnitTestResult test_find_again_still_moving_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 2 (to wall) to find us again", WalkStop::LIMIT, 1850ms, 5)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +UnitTestResult test_find_again_on_another_map_is_unknown(Logger&){ + Script script{.steps = {through_a_warp_step("wall: T: east 2 (to wall) to find us again", WalkStop::BLOCKED, 1200ms, 1)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("another map") == std::string::npos){ + return "did not stop on the other map's banner: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// The same hold when the walk does see the fade: a recovery hold toward a +// door ends in the next map, and the route does not go on as if it were +// still in the old one. +UnitTestResult test_find_again_through_a_door_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 2 (to wall) to find us again", WalkStop::TRANSITION, 1200ms, 3)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("transition") == std::string::npos){ + return "did not stop on the transition: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +struct PlannedRouteInput{ + std::string name; + std::function body; +}; + +struct PlannedRouteExpectation{ +}; + +struct PlannedRouteCase{ + PlannedRouteInput input; + PlannedRouteExpectation expectation; +}; + +class Test_PlannedRoute : public UnitTest{ +public: + explicit Test_PlannedRoute(PlannedRouteCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Planned route: " + 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: + PlannedRouteCase m_case; +}; + +} // namespace + + +const PlannedRouteCase PLANNED_ROUTE_CASES[] = { + { + .input = {"a trainer on a wall leg, then on to the wall", test_wall_leg_trainer_on_the_way}, + .expectation = {}, + }, + { + .input = {"a talk no battle followed on a wall leg plans again (lab-B 2026-11-12 06:12)", test_wall_leg_talk_no_battle}, + .expectation = {}, + }, + { + .input = {"a wall leg out of time needs the holding check", test_wall_leg_limit}, + .expectation = {}, + }, + { + .input = {"a known trainer stops an open leg", test_open_leg_known_trainer}, + .expectation = {}, + }, + { + .input = {"someone in the way who talks is marked, not fought (lab-B 2026-11-12, 00.05c)", test_open_leg_blocker_talks}, + .expectation = {}, + }, + { + .input = {"a marked talker is no wall for the replan (lab-B 2026-11-12, 00.05c)", test_marked_talker_is_no_wall}, + .expectation = {}, + }, + { + .input = {"a person a tile ahead on a short leg is waited out", test_short_leg_waits_for_the_way}, + .expectation = {}, + }, + { + .input = {"a short leg to a wall a tile short is a stop, not the wall", test_short_leg_a_tile_short_is_not_arrived}, + .expectation = {}, + }, + { + .input = {"a wall leg that never moved plans from its start", test_never_moved_plans_from_the_start}, + .expectation = {}, + }, + { + .input = {"a wall leg held up on the way is not at its wall (lab-B 2026-11-11 21:00)", test_wall_leg_held_up_is_not_at_its_wall}, + .expectation = {}, + }, + { + .input = {"a leg to a wall turns first when its way differs from the facing (lab-B 2026-11-12 14:55)", test_wall_leg_turns_before_the_walk}, + .expectation = {}, + }, + { + .input = {"one chunk of a walk with unknown facing is a turn, not a move (lab-B 2026-11-12 14:55)", test_wall_leg_one_chunk_is_a_turn_when_facing_is_unknown}, + .expectation = {}, + }, + { + .input = {"a wall leg that stood at its wall among the start tiles does not fail (lab-B 2026-11-12 14:50)", test_wall_leg_at_its_wall_among_the_start_tiles}, + .expectation = {}, + }, + { + .input = {"a wall leg still moving at its time limit is not where the plan has us (lab-B 2026-11-12 20:22)", test_wall_leg_still_moving_at_limit_is_unknown}, + .expectation = {}, + }, + { + .input = {"the at-the-wall check still moving is not at a wall (lab-B 2026-11-12 20:22)", test_wall_check_still_moving_is_unknown}, + .expectation = {}, + }, + { + .input = {"the at-the-wall check on another map's banner went through a warp (lab-B 2026-11-12 20:22)", test_wall_check_on_another_map_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again still moving is not found (lab-B 2026-11-12 20:20)", test_find_again_still_moving_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again on another map's banner went through a warp (lab-B 2026-11-12 20:20)", test_find_again_on_another_map_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again through a door ends in the next map (lab-B 2026-11-12 20:20)", test_find_again_through_a_door_is_unknown}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_PlannedRoute(UnitTestDatabase& database){ + for (const auto& item : PLANNED_ROUTE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h new file mode 100644 index 0000000000..740e5c27e8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory Planned Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// walk_route() on a script of console outcomes (a ScriptedRouteIO): what +// the walker does after a trainer stops a leg, and when a leg to a wall +// counts as at its wall. +void add_tests_AutoStory_PlannedRoute(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp new file mode 100644 index 0000000000..0c9fa562f8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp @@ -0,0 +1,647 @@ +/* BDSP AutoStory - Route Bench Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Scores the plan of every ROUTE step of the story table with the shared + * route scorer (Pokemon_RouteScorer.h), and the routes the user asked for + * on three of them, and checks that no route scores worse than + * Data/route_bench.golden.txt. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/Navigation/Pokemon_RouteScorer.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "Utils/PokemonBDSP_AutoStory_RouteChoice.h" +#include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_TableRoutes.h" +#include "PokemonBDSP_AutoStory_RouteBench_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::RouteScore; +using Pokemon::ScoredLeg; +using Pokemon::ScoredLegKind; +using Pokemon::TilePos; + +// One route to score: the plan the program walks today, or one hand-written. +struct BenchCase{ + std::string name; + const StoryWorld* world; + std::vector from; + std::vector goal; + std::vector triggers; // a scene starts here and stops the player + std::vector legs; + bool run = false; + std::optional costs; + bool planned = true; // false: no plan exists + // A human's route for this case (the user's, or a recorded run's), the + // bar the plan is measured against; none: the stand-in. + std::optional> human = std::nullopt; + std::string human_source = ""; +}; + +Pokemon::ScoreMap bench_map(const BenchCase& bench){ + return route_score_map(*bench.world, bench.triggers); +} + +Pokemon::RoutePaces bench_paces(){ + return route_score_paces(); +} + +MoveSpeed bench_speed(bool run){ + return run ? MoveSpeed::RUN : MoveSpeed::WALK; +} + +// The legs as RouteWalker walks them (route_scored_legs()). +std::vector walked_legs(const std::vector& legs, bool run){ + return route_scored_legs(legs, bench_speed(run)); +} + +RouteScore score_case(const BenchCase& bench){ + if (!bench.planned){ + return {.verdict = Pokemon::RouteVerdict::FAIL, .why = "no plan"}; + } + const std::vector& targets = bench.goal; + const std::function goal = [&targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + const Pokemon::RouteReplanner replan = [&](const std::vector& from) -> std::optional>{ + const std::optional> legs = + plan_recovered_route(*bench.world, from, goal, replan_costs(bench.costs.value_or(RobustPlanCosts()))); + if (!legs){ + return std::nullopt; + } + return walked_legs(*legs, bench.run); + }; + return Pokemon::score_route(bench_map(bench), bench.from, goal, bench.legs, bench_paces(), route_interrupt_model(), replan); +} + + +// The table's ROUTE steps from a known place, as the program plans them. +std::vector table_cases(){ + std::vector cases; + std::map seen; + for (const TableRoute& route : table_routes()){ + if (route.world == nullptr){ + continue; + } + std::string name = route.row + " " + route.step->name; + const size_t repeat = seen[name]++; + if (repeat > 0){ + name += std::format(" #{}", repeat + 1); + } + const RouteStepPlan plan = plan_table_route(route); + const bool run = route.row > std::string(RUNNING_SHOES_ROW); + const Pokemon::Pose target = Pokemon::pose_of(Pokemon::story_arg(route.step->args, "target")); + const std::vector goal = world_tiles(*route.world, target); + cases.push_back({ + .name = name, .world = route.world, .from = plan.start, + .goal = goal, .triggers = route.step->action == ROUTE_ONTO_TRIGGER ? goal : std::vector{}, + .legs = plan.legs ? walked_legs(*plan.legs, run) : std::vector{}, + .run = run, .costs = story_route_costs(route.step->action), .planned = plan.legs.has_value(), + }); + } + return cases; +} + +size_t case_index(const std::vector& cases, const std::string& name){ + for (size_t i = 0; i < cases.size(); i++){ + if (cases[i].name == name){ + return i; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No table route " + name + "."); +} + +TilePos wall_from(const StoryWorld& world, const TilePos& from, Direction direction){ + return slide_to_wall(world, from, direction).stop; +} + +ScoredLeg taps(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::TAPS, .run = true}; +} +ScoredLeg run_to_wall(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::TO_WALL, .run = true}; +} +ScoredLeg run_open(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::OPEN_HOLD, .run = true}; +} + +// 00.04 "along_route_201_road": the console tapped "west 16 (open)" along +// Route 201's road at 800 ms a tile (lab-B 2026-11-09, step 9 of 00.04). +// The user's route: run west into the funnel of columns 36 to 39, whose +// walls stop a run north on row 9; run north, then west to the wall at +// column 13, a tap east onto column 15 and taps south over the ledge. +BenchCase road_target(const BenchCase& current){ + const StoryWorld& world = *current.world; + const TilePos start = current.from.front(); + const TilePos funnel = story_to_world(world, "Route201", {38, 14}); + const TilePos row9 = wall_from(world, funnel, Direction::NORTH); + const TilePos west = wall_from(world, row9, Direction::WEST); + const TilePos goal = current.goal.front(); + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = { + run_open(Direction::WEST, (size_t)(start.x - funnel.x)), + run_to_wall(Direction::NORTH, (size_t)(funnel.y - row9.y)), + run_to_wall(Direction::WEST, (size_t)(row9.x - west.x)), + taps(Direction::EAST, (size_t)(goal.x - west.x)), + taps(Direction::SOUTH, 4), + }; + return target; +} + +// 00.04 "into_twinleaf": the console tapped "south 33 (open)" for 26 s +// (lab-B 2026-11-09, step 10 of 00.04), where the column ends at a wall. +// The user's route: run south to that wall, then tap back north. +BenchCase twinleaf_target(const BenchCase& current){ + const StoryWorld& world = *current.world; + const TilePos start = current.from.front(); + const TilePos wall = wall_from(world, start, Direction::SOUTH); + const TilePos goal = current.goal.front(); + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = {run_to_wall(Direction::SOUTH, (size_t)(wall.y - start.y))}; + if (wall.y > goal.y){ + target.legs.push_back(taps(Direction::NORTH, (size_t)(wall.y - goal.y))); + } + return target; +} + +// 00.04 "into_moms_call", to her call on (6, 9) (lab-B 2026-11-09 17:29, +// step 17 of 00.04). The user's route: down to row 9, whose wall row 10 +// does not stop us, so a tap; then run west along row 9 until her call +// stops us on (6, 9). +BenchCase moms_call_target(const BenchCase& current){ + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = {taps(Direction::WEST, 1), taps(Direction::SOUTH, 1), run_to_wall(Direction::WEST, 4)}; + return target; +} + +// 00.04 "into_twinleaf", lab-B run 4 (2026-11-09 18:50): "south 33 (open)" +// was blocked after 22 taps, counted at Twinleaf (15, 11), by someone on +// (15, 12). The plan from that one tile, "west 1 (to wall)", walked six +// tiles west, and the walk ended beside the house, east of its door. A +// blocked count can be a tile off (blocked_stop_tiles()): both cases are +// scored from every tile it gives, the blocker on the map. "counted tile" +// is the plan walk_route() made then, from the count alone; the other is +// the plan it makes now. +const StoryWorld& run4_world(const StoryWorld& world){ + static const StoryWorld blocked = [&world]{ + StoryWorld out = world; + out.block(story_to_world(world, "TwinleafTown", {15, 12})); + return out; + }(); + return blocked; +} +void add_run4_cases(std::vector& cases){ + const BenchCase& route = cases[case_index(cases, "00.04 into_twinleaf")]; + const StoryWorld& world = run4_world(*route.world); + const TilePos counted = story_to_world(world, "TwinleafTown", {15, 11}); + const std::vector targets = route.goal; + const auto goal = [targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + BenchCase recovery = route; + recovery.world = &world; + recovery.from = blocked_stop_tiles(world, {counted}, Direction::SOUTH); + recovery.human = std::nullopt; + const std::optional> old_plan = plan_recovered_route(world, {counted}, goal, *recovery.costs); + BenchCase old_case = recovery; + old_case.name = "00.04 into_twinleaf after run 4's block, planned from the counted tile"; + old_case.legs = old_plan ? walked_legs(*old_plan, recovery.run) : std::vector{}; + old_case.planned = old_plan.has_value(); + cases.push_back(std::move(old_case)); + const std::optional> new_plan = plan_fastest_route({ + .world = world, .from = recovery.from, .goal = goal, .costs = *recovery.costs, .speed = bench_speed(recovery.run), + }); + recovery.name = "00.04 into_twinleaf after run 4's block"; + recovery.legs = new_plan ? walked_legs(*new_plan, recovery.run) : std::vector{}; + recovery.planned = new_plan.has_value(); + cases.push_back(std::move(recovery)); +} + +// A wild battle on a leg to a wall needs no new plan: RouteWalker walks +// to the wall again. On an open leg it loses the count of tiles. 00.04d +// north_to_jubilife's plan with the route's own costs crosses Route 202's +// grass on "west 12 (taps)", and each battle there needs a replan; +// plan_fastest_route() picks the plan with open legs dear in grass +// (replan_costs()), which crosses it on legs to a wall. This scores the +// plan the route's own costs give. +void add_own_costs_case(std::vector& cases){ + BenchCase own = cases[case_index(cases, "00.04d north_to_jubilife")]; + const std::vector targets = own.goal; + const auto goal = [targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + const std::optional> legs = plan_recovered_route(*own.world, own.from, goal, *own.costs); + own.name += ", planned with the route's own costs (taps in the grass)"; + own.legs = legs ? walked_legs(*legs, own.run) : std::vector{}; + own.planned = legs.has_value(); + cases.push_back(std::move(own)); +} + +// A human run recorded on the console, as legs: replaces a case's stand-in. +struct RecordedHumanRun{ + std::string case_name; + std::vector legs; + std::string source; // the run's log +}; +// The hook for recorded human runs (a log of a human playing, turned into +// legs). None recorded yet. +std::vector recorded_human_runs(){ + return {}; +} + +// Adds `target` after the case it improves, and makes it that case's human +// route: the user described each of them. +void add_target(std::vector& cases, const std::string& name, const std::function& make){ + BenchCase& current = cases[case_index(cases, name)]; + BenchCase target = make(current); + current.human = target.legs; + current.human_source = "user's route"; + target.human = target.legs; + target.human_source = "user's route"; + cases.push_back(std::move(target)); +} + +std::vector bench_cases(){ + std::vector cases = table_cases(); + add_target(cases, "00.04 along_route_201_road", road_target); + add_target(cases, "00.04 into_twinleaf", twinleaf_target); + add_target(cases, "00.04 into_moms_call", moms_call_target); + add_run4_cases(cases); + add_own_costs_case(cases); + for (const RecordedHumanRun& run : recorded_human_runs()){ + BenchCase& bench = cases[case_index(cases, run.case_name)]; + bench.human = run.legs; + bench.human_source = "recorded: " + run.source; + } + return cases; +} + + +constexpr std::string_view BENCH_HEADER = + "BDSP AutoStory: the route bench. Each ROUTE step of the story table, its\r\n" + "plan scored by Pokemon::score_route() as RouteWalker walks it (wall legs\r\n" + "held, at a run after 00.03; open legs tapped), then the user's routes for\r\n" + "three of them (TARGET). Written by the test \"Route bench: no route scores\r\n" + "worse than Data/route_bench.golden.txt\"; rerun it with PA_UPDATE_ROUTE_BENCH=1\r\n" + "to rewrite this file. A route is worse when its verdict leaves PASS, its\r\n" + "expected or worst time grows or one of its human ratios grows past 1.00; a\r\n" + "route that did not pass is better once it passes. No story route may STRAND.\r\n" + "columns: case | expected ms (interruptions by chance) | worst ms (slowest start and\r\n" + "the costliest single interruption) | nominal ms (slowest start, none) | stops (wall\r\n" + "re-localizations) | recoveries (places an interruption may happen) | replans (of those,\r\n" + "the ones the plan cannot absorb) | slower than human (expected / human ms) | riskier\r\n" + "than human ((recoveries + 1) / (human's + 1)) | more backtracking than human ((replans +\r\n" + "tiles walked back + 1) / (human's + 1); a planned stop at a wall is not backtracking) |\r\n" + "the human's ms (the user's route, a recorded run, or the stand-in: the shortest safe\r\n" + "walk run with no stops) | verdict (why). A ratio above 1.5 is flagged \"!\".\r\n" + "\r\n"; + +// The bar a case's plan is measured against: a human's walk of it. +struct HumanBaseline{ + long long ms = 0; + size_t risk_tiles = 0; // places an interruption may happen + size_t backtracking = 0; // replans and tiles walked back (backtracking()) + std::string source; +}; + +// What the bench counts as backtracking: the replans a plan cannot absorb +// (recoveries that need a new plan) and the tiles it walks back. +size_t backtracking(const RouteScore& score, const std::vector& legs){ + return score.replans + Pokemon::tiles_walked_back(legs); +} + +// A step of the stand-in's search: tiles walked, grass and wanderer tiles +// avoided where a way round exists. +struct StandInNode{ + long long cost; + long long ms; + size_t risk; + TilePos pos; + bool operator>(const StandInNode& other) const{ + return cost > other.cost; + } +}; + +using StandInQueue = std::priority_queue, std::greater>; + +bool in_goal(const BenchCase& bench, const TilePos& pos){ + return std::find(bench.goal.begin(), bench.goal.end(), pos) != bench.goal.end(); +} + +// Queues the steps from `node` the stand-in may take: not through a warp +// or a trigger off the goal, and round grass and wanderers where it can. +void push_steps(StandInQueue& open, const BenchCase& bench, const Pokemon::ScoreMap& map, + const Pokemon::RoutePaces& paces, const StandInNode& node +){ + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + const std::optional next = map.step(node.pos, direction); + if (!next){ + continue; + } + const Pokemon::TileRisk risk = map.risk(*next); + if ((risk == Pokemon::TileRisk::WARP || risk == Pokemon::TileRisk::TRIGGER) && !in_goal(bench, *next)){ + continue; + } + const long long tiles = std::abs(next->x - node.pos.x) + std::abs(next->y - node.pos.y); + const bool risky = risk == Pokemon::TileRisk::ENCOUNTER || risk == Pokemon::TileRisk::WANDERER; + open.push({node.cost + tiles + (risky ? 1000 : 0), node.ms + tiles * paces.ms_per_tile(direction, true), + node.risk + (risky ? 1 : 0), *next}); + } +} + +// The shortest safe walk from `start` to the goal, run the whole way. +std::optional stand_in_from(const BenchCase& bench, const TilePos& start){ + const Pokemon::ScoreMap map = bench_map(bench); + const Pokemon::RoutePaces paces = bench_paces(); + StandInQueue open; + std::set> done; + open.push({0, paces.start_ms, 0, start}); + while (!open.empty() && done.size() < 100000){ + const StandInNode node = open.top(); + open.pop(); + if (!done.insert({node.pos.x, node.pos.y}).second){ + continue; + } + if (in_goal(bench, node.pos)){ + return HumanBaseline{node.ms, node.risk, 0, "stand-in"}; // no replans, no tile back + } + push_steps(open, bench, map, paces, node); + } + return std::nullopt; +} + +// For a case with no human route: the shortest safe walk from its slowest +// start, run the whole way, with no stops (as a player who sees the map +// walks). Nothing if no walk reaches the goal. +std::optional stand_in(const BenchCase& bench){ + std::optional slowest; + for (const TilePos& start : bench.from){ + const std::optional walk = stand_in_from(bench, start); + if (walk && (!slowest || slowest->ms < walk->ms)){ + slowest = walk; + } + } + return slowest; +} + +std::optional human_baseline(const BenchCase& bench){ + if (!bench.human){ + return stand_in(bench); + } + BenchCase human = bench; + human.legs = *bench.human; + const RouteScore score = score_case(human); + return HumanBaseline{score.nominal_ms, score.recoveries, backtracking(score, human.legs), bench.human_source}; +} + +// "1.23", with "!" past 1.5: a plan that far behind the human is flagged. +std::string ratio_text(double ratio){ + return std::format("{:.2f}{}", ratio, ratio > 1.5 ? "!" : ""); +} + +// slower | riskier | more backtracking than the human, and the human's time. +std::string human_columns(const BenchCase& bench, const RouteScore& score, const std::optional& human){ + if (!human || human->ms <= 0){ + return "- | - | - | no human baseline"; + } + const double slower = score.expected_ms / (double)human->ms; + const double riskier = (double)(score.recoveries + 1) / (double)(human->risk_tiles + 1); + const double back = (double)(backtracking(score, bench.legs) + 1) / (double)(human->backtracking + 1); + return std::format("{} | {} | {} | {} ms ({})", ratio_text(slower), ratio_text(riskier), ratio_text(back), + human->ms, human->source); +} + +std::string bench_line(const BenchCase& bench, const RouteScore& score){ + std::string line = std::format("{} | {} | {} | {} | {} | {} | {} | {} | {}", bench.name, (long long)(score.expected_ms + 0.5), + score.worst_ms, score.nominal_ms, score.stops, score.recoveries, score.replans, + human_columns(bench, score, human_baseline(bench)), Pokemon::verdict_name(score.verdict)); + return score.why.empty() ? line : line + " (" + score.why + ")"; +} + +// The bench's lines, each case's legs for the log, and the cases that +// strand where they must not. +struct BenchText{ + std::string lines; + std::string legs; + std::string strands; +}; +BenchText bench_text(){ + BenchText out{std::string(BENCH_HEADER), "", ""}; + for (const BenchCase& bench : bench_cases()){ + const RouteScore score = score_case(bench); + out.lines += bench_line(bench, score) + "\r\n"; + if (score.verdict == Pokemon::RouteVerdict::STRAND){ + out.strands += "\n " + bench.name + ": " + score.why; + } + out.legs += bench.name + ":"; + for (const ScoredLeg& leg : bench.legs){ + out.legs += " " + Pokemon::to_string(leg) + ";"; + } + out.legs += "\n"; + } + return out; +} + +// A bench line's columns. +std::vector bench_fields(const std::string& line){ + std::vector fields; + size_t begin = 0; + while (true){ + const size_t end = line.find(" | ", begin); + fields.push_back(line.substr(begin, end == std::string::npos ? std::string::npos : end - begin)); + if (end == std::string::npos){ + return fields; + } + begin = end + 3; + } +} + +// A bench line's expected time and whether it passed. +struct GoldenRow{ + long long expected_ms; + long long worst_ms; + bool pass; + std::vector ratios; // slower, riskier, backtracking; empty without a human +}; +// The rows of a bench text, by case name. +std::map golden_rows(const std::string& text){ + std::map rows; + size_t begin = 0; + bool body = false; // past the header's blank line + while (begin < text.size()){ + const size_t end = std::min(text.find('\n', begin), text.size()); + std::string line = text.substr(begin, end - begin); + begin = end + 1; + std::erase(line, '\r'); + const size_t first = line.find(" | "); + if (!body || first == std::string::npos){ + body = body || line.empty(); + continue; + } + const std::vector fields = bench_fields(line); + GoldenRow row{std::atoll(fields[1].c_str()), std::atoll(fields[2].c_str()), line.find(" | PASS") != std::string::npos, {}}; + for (size_t i = 7; i < 10 && i < fields.size() && fields[i] != "-"; i++){ + row.ratios.push_back(std::atof(fields[i].c_str())); + } + rows[fields[0]] = std::move(row); + } + return rows; +} + +std::string bench_golden_path(){ + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* path = std::getenv("PA_ROUTE_BENCH_GOLDEN"); + if (path != nullptr && path[0] != '\0'){ + return path; + } + const std::string self = __FILE__; + const size_t slash = self.find_last_of("/\\"); + return (slash == std::string::npos ? std::string() : self.substr(0, slash + 1)) + "Data/route_bench.golden.txt"; +} + +// Whether any of `now`'s human ratios grew from `was`'s, past the human's +// own (1.0): a plan may come up to the human's bar. +bool ratio_grew(const GoldenRow& was, const GoldenRow& now){ + for (size_t i = 0; i < std::min(was.ratios.size(), now.ratios.size()); i++){ + if (now.ratios[i] > was.ratios[i] + 0.005 && now.ratios[i] > 1.005){ + return true; + } + } + return false; +} + +// Each route of `now` that scores worse than in `golden`. +std::string worse_routes(const std::string& golden, const std::string& now){ + const std::map before = golden_rows(golden); + std::string out; + for (const auto& [name, row] : golden_rows(now)){ + const auto was = before.find(name); + // A route that did not pass has no times to keep: any pass is better. + if (was == before.end() || (!was->second.pass && row.pass)){ + continue; + } + if ((was->second.pass && !row.pass) || row.expected_ms > was->second.expected_ms || + row.worst_ms > was->second.worst_ms || ratio_grew(was->second, row) + ){ + out += std::format("\n {}: {} ms (worst {}){} -> {} ms (worst {}){}", name, + was->second.expected_ms, was->second.worst_ms, was->second.pass ? "" : " (not PASS)", + row.expected_ms, row.worst_ms, row.pass ? "" : " (not PASS)"); + } + } + return out; +} + +UnitTestResult test_route_bench(Logger& logger){ + const std::string path = bench_golden_path(); + const BenchText text = bench_text(); + const std::string& now = text.lines; + logger.log("Route bench:\n" + now); + logger.log("Route bench legs:\n" + text.legs); + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* update = std::getenv("PA_UPDATE_ROUTE_BENCH"); + if (update != nullptr && update[0] != '\0' && update[0] != '0'){ + FileIO file; + if (!file.open(path, FileMode::WRITE | FileMode::BINARY) || file.write(now) != now.size()){ + return "Could not write " + path + "."; + } + logger.log("Wrote " + path + "."); + return true; + } + if (!text.strands.empty()){ + return "Routes strand:" + text.strands; + } + std::string golden; + if (!file_to_string(path, golden)){ + return "Could not read " + path + " (set PA_ROUTE_BENCH_GOLDEN to its path)."; + } + const std::string worse = worse_routes(golden, now); + if (worse.empty()){ + return true; + } + return "Routes score worse than " + path + " (rerun with PA_UPDATE_ROUTE_BENCH=1 if meant):" + worse; +} + + +struct RouteBenchInput{ + std::string name; + UnitTestResult (*test)(Logger&); +}; + +struct RouteBenchExpectation{ + std::string_view description; +}; + +struct RouteBenchCase{ + RouteBenchInput input; + RouteBenchExpectation expectation; +}; + +class Test_AutoStoryRouteBench : public UnitTest{ +public: + explicit Test_AutoStoryRouteBench(RouteBenchCase scenario) + : UnitTest("PokemonBDSP::AutoStory - Route bench: " + scenario.input.name) + , m_case(std::move(scenario)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.test(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + RouteBenchCase m_case; +}; + +const RouteBenchCase ROUTE_BENCH_CASES[] = { + { + .input = {.name = "no route scores worse than Data/route_bench.golden.txt", .test = test_route_bench}, + .expectation = {.description = "no story route strands; every route's verdict, times and ratios at least as good as the golden file's"}, + }, +}; + +} // namespace + + +void add_tests_AutoStory_RouteBench(UnitTestDatabase& database){ + for (const auto& scenario : ROUTE_BENCH_CASES){ + database.add(scenario); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h new file mode 100644 index 0000000000..ef237aaaa4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - Route Bench Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RouteBench_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RouteBench_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The route bench: every story table route's plan scored, against a golden file. +void add_tests_AutoStory_RouteBench(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp new file mode 100644 index 0000000000..23e555d157 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp @@ -0,0 +1,480 @@ +/* BDSP AutoStory Simulated Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_route() (Utils/PokemonBDSP_AutoStory_PlannedRoute.h) on a small + * simulated console (SimulatedRouteIO): walls and obstacles stop the + * player, and timed holds can overshoot. Covers the Jubilife Center walks + * from lab-B and a fault sweep over every ROUTE step of the story table. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Pose; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +// Thrown by SimulatedRouteIO when the walker fails the program. +struct RouteFailed{ + std::string message; +}; + + +// The console as a small simulator: the player stands on one tile of +// `world`, walls and `obstacles` stop them, and every walk and tap moves +// them. Fails the case after `max_calls` calls (a walker that loops). +// code-health: allow public_methods -- a RouteIO fake: the interface's seven overrides plus four test knobs +class SimulatedRouteIO : public RouteIO{ +public: + SimulatedRouteIO(const StoryWorld& world, TilePos at, std::vector obstacles, std::vector& notes) + : m_world(world) + , m_at(at) + , m_obstacles(std::move(obstacles)) + , m_notes(notes) + {} + + const TilePos& at() const{ + return m_at; + } + size_t tiles_walked() const{ + return m_walked; + } + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + count_call(); + const MoveSpeed speed = walk.options.pace == DpadPace::RUN && m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK; + const Milliseconds per_tile = route_ms_per_tile(walk.direction, speed); + // walk_route() walks only legs to a wall here, and a walk to a wall + // ends at the wall: no overshoot (overshoot_timed_holds()). + const size_t moved = move(walk.direction, SIZE_MAX); + return {.stop = WalkStop::BLOCKED, .walked = ROUTE_START_DELAY + 800ms + per_tile * moved + per_tile / 2, + .wild_battles = 0, .moved_chunks = moved}; + } + // Fault class "a timed hold overshoots": a stick hold timed for n + // tiles goes 0..ceil(0.35 n) tiles further, unseen, until a wall stops + // it (lab-B 2026-11-11, Jubilife: runs timed for 10 tiles went about 13, + // three of three). D-pad taps are tile-exact (5 to 20 taps, every one + // moved) and walks to a wall end at the wall, so neither has a fault. + void overshoot_timed_holds(bool overshoot){ + m_overshoot = overshoot; + } + // Before the running shoes, a walk held with B is still a walk: its + // time counts walking tiles, as the walker's plan speed does. + void set_can_run(bool can_run){ + m_can_run = can_run; + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string&) override{ + count_call(); + const size_t moved = move(direction, tiles); + return {.stop = moved < tiles ? WalkStop::BLOCKED : WalkStop::LIMIT, .walked = 250ms * tiles, + .wild_battles = 0, .moved_chunks = moved}; + } + virtual void press(const RouteInput& input) override{ + // arrive() pauses for up to 3 s for a location banner the sim never + // shows: those pauses are not a walker that loops. + if (input.kind != RouteInputKind::PAUSE){ + count_call(); + } + // A tap under 100 ms only turns (route_turn_tap()). + if (input.kind == RouteInputKind::DPAD && input.hold >= 100ms){ + move(input.direction, input.count); + } + // A stick hold is timed: route_open_hold() is half a tile under n. + if (input.kind == RouteInputKind::STICK){ + const Milliseconds per_tile = route_ms_per_tile(input.direction, MoveSpeed::WALK); + const size_t tiles = (size_t)((input.hold + per_tile / 2) / per_tile); + const size_t over = m_overshoot ? (tiles * 35 + 99) / 100 : 0; + for (size_t press = 0; press < input.count; press++){ + move(input.direction, tiles + over); + } + } + } + virtual RouteSight sight_within(const RouteWatch&) override{ + count_call(); + return {}; + } + virtual bool holding_against_wall(Direction, const std::string&) override{ + count_call(); + return true; + } + virtual void look(const std::function&) override{} + virtual void note(const std::string& text, RouteNote kind) override{ + m_notes.push_back(text); + if (kind == RouteNote::FAILURE){ + throw RouteFailed{text}; + } + } + +private: + void count_call(){ + if (++m_calls > MAX_CALLS){ + throw RouteFailed{"the walker made more than " + std::to_string(MAX_CALLS) + " calls."}; + } + } + size_t move(Direction direction, size_t tiles){ + size_t moved = 0; + while (moved < tiles){ + const std::optional next = step(m_world, m_at, direction); + if (!next || std::find(m_obstacles.begin(), m_obstacles.end(), *next) != m_obstacles.end()){ + break; + } + m_at = *next; + moved++; + } + m_walked += moved; + return moved; + } + + static constexpr size_t MAX_CALLS = 300; + const StoryWorld& m_world; + TilePos m_at; + std::vector m_obstacles; + std::vector& m_notes; + size_t m_calls = 0; + size_t m_walked = 0; + bool m_overshoot = false; + bool m_can_run = true; +}; + +StoryWorld jubilife_world(){ + StoryWorld world; + world.add("Jubilife City", story_map("JubilifeCity"), {0, 0}); + return world; +} + +// lab-B 2026-11-11 11:17 (00.05 to_the_center): a walk east stopped about +// 13 tiles short of the wall, which left 15 possible tiles, (49..53, +// 41..43). Something the map lacks stood east of us; with several possible +// tiles nothing was marked, so every replan chose "east 7 (to wall)" again, +// walked 0 tiles five times and gave up on "too many plans". A walk that +// never moved from several possible tiles must route around the tiles just +// past them. +UnitTestResult test_blocked_from_several_tiles(Logger&){ + const StoryWorld world = jubilife_world(); + std::vector from; + for (int y = 41; y <= 43; y++){ + for (int x = 49; x <= 53; x++){ + from.push_back({x, y}); + } + } + const TilePos goal{52, 41}; + std::vector notes; + SimulatedRouteIO io(world, {53, 41}, {{54, 41}}, notes); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, from, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + return "failed: " + e.message; + } + if (io.at() != goal){ + return "ended at " + Pokemon::to_string(io.at()) + ", not at (52, 41)."; + } + return true; +} + +size_t tiles_of(const std::vector& legs){ + size_t tiles = 0; + for (const PlannedLeg& leg : legs){ + tiles += leg.leg.tiles; + } + return tiles; +} + +// The fewest tiles from `from` to `goal` on Jubilife, open legs exact. +size_t jubilife_shortest(const StoryWorld& world, TilePos from, TilePos goal){ + const auto plan = plan_robust_route(world, from, goal, waypoint_plan_costs()); + return plan ? tiles_of(*plan) : 0; +} + +// lab-B 2026-11-11 13:38 (00.05 to_the_center from (48, 62)): "north 20 +// (open)" walked as a held run (3175 ms) overshot, the west leg after it +// stopped 9 tiles in, the walker put walls at (54, 41..43) and "arrived" +// at (52, 41) nowhere near the Center. Timed holds overshoot by up to +// ceil(0.35 n) tiles (three runs of 10 went about 13); taps are exact. +// With every timed hold overshooting the walk still ends on the tile. +// Run (the program after the shoes, 00.05 heal_jubilife) and walk alike. +UnitTestResult test_jubilife_center_held_overshoot(Logger&){ + const StoryWorld world = jubilife_world(); + const TilePos from{48, 62}; + const TilePos goal{52, 41}; + for (const MoveSpeed speed : {MoveSpeed::RUN, MoveSpeed::WALK}){ + const std::string_view pace = speed == MoveSpeed::RUN ? "running" : "walking"; + std::vector notes; + SimulatedRouteIO io(world, from, {}, notes); + io.overshoot_timed_holds(true); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, {from}, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = speed, + }); + }catch (const RouteFailed& e){ + return std::format("{}: failed: {}", pace, e.message); + } + if (io.at() != goal){ + return std::format("{}: ended at {}, not at (52, 41).", pace, Pokemon::to_string(io.at())); + } + } + return true; +} + +// The same walk on the console: the kids at (52, 46) and (54, 46) stop a +// walk north up their columns. The walker replans from where it stopped +// and reaches the Center without a long way round. +UnitTestResult test_jubilife_center_kids(Logger&){ + const StoryWorld world = jubilife_world(); + const TilePos from{48, 62}; + const TilePos goal{52, 41}; + std::vector notes; + SimulatedRouteIO io(world, from, {{52, 46}, {54, 46}}, notes); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, {from}, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + return "failed: " + e.message; + } + if (io.at() != goal){ + return "ended at " + Pokemon::to_string(io.at()) + ", not at (52, 41)."; + } + const size_t least = jubilife_shortest(world, from, goal); + if (io.tiles_walked() * 10 > least * 13 + 40){ + return std::format("walked {} tiles where {} would do.", io.tiles_walked(), least); + } + return true; +} + +// One ROUTE step of the story table, from where the steps before it end. +struct StoryRoute{ + std::string name; + MoveSpeed speed; + Pose from; + Pose goal; + std::string costs; +}; + +// Every ROUTE step whose start the table knows: a row's start, a ROUTE's +// target or a step's pose_after. Steps after any other step start +// unknown and are left out. +void add_story_route(const std::string& row, const StoryStep& step, std::optional& at, std::vector& routes){ + if (step.kind == StoryStepKind::ROUTE){ + const Pose goal = Pokemon::pose_of(Pokemon::story_arg(step.args, "target")); + if (at){ + const MoveSpeed speed = row > std::string(RUNNING_SHOES_ROW) ? MoveSpeed::RUN : MoveSpeed::WALK; + routes.push_back({std::format("{} {}", row, step.name), speed, *at, goal, step.action}); + } + at = goal; + }else{ + at.reset(); + } + if (step.pose_after){ + at = Pokemon::pose_of(*step.pose_after); + } +} +std::vector story_routes(){ + std::vector routes; + for (const StoryCheckpoint& row : story_checkpoints()){ + std::optional at; + if (row.start){ + at = Pokemon::pose_of(*row.start); + } + for (const StoryStep& step : row.steps){ + add_story_route(row.id, step, at, routes); + } + } + return routes; +} + +// Walks `route` from each of its start tiles on the simulated console, +// every timed hold overshooting if `overshoot`. Returns the tiles walked +// from each start, or why it failed. +std::string sweep_story_route(const StoryRoute& route, bool overshoot, std::vector& walked){ + const StoryWorld* world = story_plan_world(route.from.map, route.goal.map); + const std::optional costs = story_route_costs(route.costs); + if (world == nullptr || !costs){ + return route.name + ": no world or costs."; + } + std::vector targets; + for (const TilePos& pos : Pokemon::tiles_of(route.goal)){ + targets.push_back(story_to_world(*world, route.goal.map, pos)); + } + std::vector start; + for (const TilePos& pos : Pokemon::tiles_of(route.from)){ + start.push_back(story_to_world(*world, route.from.map, pos)); + } + for (const TilePos& from : start){ + std::vector notes; + SimulatedRouteIO io(*world, from, {}, notes); + io.overshoot_timed_holds(overshoot); + io.set_can_run(route.speed == MoveSpeed::RUN); + RouteTrainers trainers{.sights = story_trainers(*world), .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, *world, start, trainers, { + .goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, + .label = route.name, + .avoid = {}, + .costs = *costs, + .speed = route.speed, + }); + }catch (const RouteFailed& e){ + std::string last; + for (size_t i = notes.size() > 4 ? notes.size() - 4 : 0; i < notes.size(); i++){ + last += "\n " + notes[i]; + } + return std::format("{} from {}{}: {}{}", route.name, Pokemon::to_string(from), overshoot ? ", holds overshooting" : "", e.message, last); + } + if (std::find(targets.begin(), targets.end(), io.at()) == targets.end()){ + return std::format("{} from {}{}: ended at {}.", route.name, Pokemon::to_string(from), overshoot ? ", holds overshooting" : "", Pokemon::to_string(io.at())); + } + walked.push_back(io.tiles_walked()); + } + return ""; +} + +// Fault sweep: every story route, walked with every timed hold +// overshooting, still ends on its target. The log line per route gives the +// most tiles walked from any of its start tiles, without the fault and with +// it. +UnitTestResult test_story_routes_holds_overshoot(Logger& logger){ + std::string failures; + const std::vector routes = story_routes(); + for (const StoryRoute& route : routes){ + std::string line = route.name + ":"; + for (bool overshoot : {false, true}){ + std::vector walked; + const std::string error = sweep_story_route(route, overshoot, walked); + if (!error.empty()){ + failures += "\n " + error; + line += " FAIL"; + continue; + } + size_t most = 0; + for (size_t tiles : walked){ + most = std::max(most, tiles); + } + line += " " + std::to_string(most); + } + logger.log("[timed holds overshoot: no, yes] " + line); + } + if (!failures.empty()){ + return std::format("{} routes swept:{}", routes.size(), failures); + } + return true; +} + + +struct SimulatedRouteInput{ + std::string name; + std::function body; +}; + +struct SimulatedRouteExpectation{ +}; + +struct SimulatedRouteCase{ + SimulatedRouteInput input; + SimulatedRouteExpectation expectation; +}; + +class Test_SimulatedRoute : public UnitTest{ +public: + explicit Test_SimulatedRoute(SimulatedRouteCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Planned route: " + 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: + SimulatedRouteCase m_case; +}; + +} // namespace + + +const SimulatedRouteCase SIMULATED_ROUTE_CASES[] = { + { + .input = {"blocked from several possible tiles routes around", test_blocked_from_several_tiles}, + .expectation = {}, + }, + { + .input = {"Jubilife Center with every timed hold overshooting", test_jubilife_center_held_overshoot}, + .expectation = {}, + }, + { + .input = {"Jubilife to the Center past the kids", test_jubilife_center_kids}, + .expectation = {}, + }, + { + .input = {"fault sweep: every story route with timed holds overshooting", test_story_routes_holds_overshoot}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_SimulatedRoute(UnitTestDatabase& database){ + for (const auto& item : SIMULATED_ROUTE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h new file mode 100644 index 0000000000..f96f5bcd3b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory Simulated Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_SimulatedRoute_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_SimulatedRoute_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// walk_route() on a simulated console (SimulatedRouteIO): the Jubilife +// Center walks with overshooting holds and people in the way, and a fault +// sweep over every ROUTE step of the story table. +void add_tests_AutoStory_SimulatedRoute(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp new file mode 100644 index 0000000000..8ceb126ce8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp @@ -0,0 +1,174 @@ +/* BDSP AutoStory: Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "PokemonBDSP_AutoStory_StarterRng.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +BdspStarter bdsp_starter_for_choice(StarterChoice choice){ + switch (choice){ + case StarterChoice::TURTWIG: + return BdspStarter::Turtwig; + case StarterChoice::CHIMCHAR: + return BdspStarter::Chimchar; + case StarterChoice::PIPLUP: + return BdspStarter::Piplup; + } + return BdspStarter::Chimchar; +} + + +StoryStarterRngOptions::StoryStarterRngOptions() + : RUN( + "Run Starter RNG in AutoStory:
Off by default. In 00.02, run Starter RNG from the Verity Lakefront " + "save using the stop conditions below, then continue the story at 00.03. " + "Takes precedence over Stop for Starter RNG. If it fails the program stops: it does not reload 00.02.", + LockMode::LOCK_WHILE_RUNNING, + false + ) + , MAX_RESETS( + "Starter RNG Maximum Resets:
0 means no limit.", + LockMode::LOCK_WHILE_RUNNING, + 0, 0, 9999 + ) + , FILTERS( + "Starter RNG Stop Conditions:
Stop Starter RNG when the starter matches any row in this table.", + Pokemon::StatsHuntMiscFeatureFlags{ + /*action*/ false, /*shiny*/ true, /*gender*/ true, /*nature*/ true + } + ) + , NOTIFICATION_SHINY( + "Shiny Starter", + true, true, ImageAttachmentMode::JPG, + {"Notifs", "Showcase"} + ) +{ + set_default_shiny_row(FILTERS); +} + +void StoryStarterRngOptions::set_details_visible(bool visible){ + const ConfigOptionState state = visible ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN; + PLAYER_MODEL.set_visibility(state); + MAX_RESETS.set_visibility(state); + FILTERS.set_visibility(state); +} + +StoryStarterRngConfig StoryStarterRngOptions::config(Language language, StarterChoice starter) const{ + return StoryStarterRngConfig{ + .language = language, + .player_model = PLAYER_MODEL.model_number(), + .starter = starter, + .max_resets = MAX_RESETS, + }; +} + + +StarterRngSettings make_starter_rng_settings(const StoryStarterRngConfig& config, const BdspRngFilterSnapshot& filters){ + StarterRngSettings settings; + settings.language = config.language; + settings.player_model = config.player_model; + settings.start_point = BdspStartPoint::Lakefront; + settings.starter = bdsp_starter_for_choice(config.starter); + settings.filters = &filters; + settings.max_resets = config.max_resets; + settings.reset_first = false; + settings.finish_battle_on_shiny = true; + settings.close_summary_after_read = true; + return settings; +} + +std::string starter_rng_failure(const StarterRngRunResult& result){ + switch (result.outcome){ + case StarterRngOutcome::HIT: + return ""; + case StarterRngOutcome::SHINY: + if (!result.settled){ + return "Caught a shiny starter, but the Starly battle or Rowan's scene did not finish: " + "stopped with the shiny in the party; the game is not on the Verity Lakefront overworld."; + } + return ""; + case StarterRngOutcome::OUT_OF_RESETS: + case StarterRngOutcome::ABANDONED: + break; + } + if (result.last_failure_reason.empty()){ + return std::format("Starter RNG stopped after {} reset(s) without hitting a target.", result.resets_used); + } + return result.last_failure_reason; +} + + +Pokemon::RngHandoffResult starter_rng_handoff_result(const StarterRngRunResult& result){ + Pokemon::RngHandoffResult handoff; + handoff.failure = starter_rng_failure(result); + if (handoff.failure.empty()){ + handoff.summary = std::format( + "after {} reset(s) on advance {}: {}.", + result.resets_used, result.target_advance, result.target.to_string() + ); + } + return handoff; +} + + +namespace{ + +// What the hook keeps between its calls: the filters, copied once so that +// edits made while running do not apply, and the aim that Starter RNG's +// calibration builds up across them. +struct StarterRngHookState{ + StarterRngHookState(StoryStarterRngOptions& options, const StoryStarterRngConfig& config) + : filters(options.filters().make_snapshot()) + , settings(make_starter_rng_settings(config, filters)) + {} + + RngAim aim; + BdspRngFilterSnapshot filters; + StarterRngSettings settings; +}; + +} // namespace + + +std::function make_starter_rng_hook( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + StoryStarterRngOptions& options, const StoryStarterRngConfig& config, + EventNotificationOption& error_notification +){ + if (options.filters().current_rows() == 0){ + throw UserSetupError( + env.console, + "Starter RNG Stop Conditions is empty, so no starter would ever match: add a row, such as a shiny of any nature." + ); + } + std::shared_ptr state = std::make_shared(options, config); + return [&env, &context, &options, &error_notification, state]() -> Pokemon::Outcome{ + uint64_t resets_logged = 0; + StarterRngAttemptIO io; + io.notification_shiny = &options.notification_shiny(); + io.notification_error = &error_notification; + io.on_reset = [&]{ + resets_logged++; + env.console.overlay().add_log(std::format("Starter RNG: try {}", resets_logged)); + }; + return Pokemon::run_rng_handoff(env.logger(), "Starter RNG", [&]{ + const StarterRngRunResult result = run_starter_rng_attempts(env, context, state->settings, io, state->aim); + return starter_rng_handoff_result(result); + }); + }; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h new file mode 100644 index 0000000000..fc2df841e0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h @@ -0,0 +1,99 @@ +/* BDSP AutoStory: Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + * Run Starter RNG in AutoStory: its options, and the action that 00.02 runs + * in place of its Lake Verity steps (the Starter RNG program's reset loop, + * PokemonBDSP_StarterRngAttempts.h, called from the story). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StarterRng_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StarterRng_H + +#include +#include +#include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/Notifications/EventNotificationOption.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Pokemon_StoryRngHandoff.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Options/PokemonBDSP_RngFilter.h" +#include "PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Maps AutoStory's starter option to Starter RNG's starter enum. +BdspStarter bdsp_starter_for_choice(StarterChoice choice); + + +// What the story tells Starter RNG about the game it plays. +struct StoryStarterRngConfig{ + Language language = Language::English; + // The player's appearance, 1 to 8: the lake's eye templates depend on it. + uint8_t player_model = 1; + StarterChoice starter = StarterChoice::CHIMCHAR; + uint16_t max_resets = 0; +}; + + +// AutoStory's options for Starter RNG. Only RUN is always shown; the rest +// show while it is on. +class StoryStarterRngOptions{ +public: + StoryStarterRngOptions(); + + void set_details_visible(bool visible); + // The options' values, for a story in `language` with `starter`. + StoryStarterRngConfig config(Language language, StarterChoice starter) const; + + BooleanCheckBoxOption& run(){ return RUN; } + PlayerModelOption& player_model(){ return PLAYER_MODEL; } + SimpleIntegerOption& max_resets(){ return MAX_RESETS; } + BdspRngFilterTable& filters(){ return FILTERS; } + EventNotificationOption& notification_shiny(){ return NOTIFICATION_SHINY; } + +private: + BooleanCheckBoxOption RUN; + PlayerModelOption PLAYER_MODEL; + SimpleIntegerOption MAX_RESETS; + BdspRngFilterTable FILTERS; + EventNotificationOption NOTIFICATION_SHINY; +}; + + +// Starter RNG's settings for the run in 00.02: from the Verity Lakefront +// save, through the Starly battle and Rowan's scene on a shiny or not, to +// the Lakefront overworld with the menu closed. +StarterRngSettings make_starter_rng_settings(const StoryStarterRngConfig& config, const BdspRngFilterSnapshot& filters); + +// Why the story cannot go on after Starter RNG ended as `result`; empty if +// it can: a starter was caught and the game stands on the Lakefront. +std::string starter_rng_failure(const StarterRngRunResult& result); + +// Starter RNG's end as the shared handoff contract has it +// (Pokemon_StoryRngHandoff.h): starter_rng_failure() and, on success, the +// resets used and the target hit. +Pokemon::RngHandoffResult starter_rng_handoff_result(const StarterRngRunResult& result); + +// The action 00.02 runs. Throws a UserSetupError now if the filter table is +// empty. The action itself throws when Starter RNG fails, so that no row +// retry reruns the whole reset loop. +std::function make_starter_rng_hook( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + StoryStarterRngOptions& options, const StoryStarterRngConfig& config, + EventNotificationOption& error_notification +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp new file mode 100644 index 0000000000..9eb9ef9fde --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp @@ -0,0 +1,93 @@ +/* BDSP AutoStory - Story actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_StoryData.h" +#include "PokemonBDSP_AutoStory_StoryActions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Outcome; +using Pokemon::RunState; +using Pokemon::StoryAction; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + + +namespace{ + +// Adds `action` for `kind`'s default and for each of its named actions in +// bdsp_story_actions(), each with that spec's args, so lint_story_checkpoint() +// checks a step's args against the action it names. +void add_with_specs(Pokemon::StoryActionTable& table, StoryStepKind kind, const StoryAction& action){ + const std::span specs = bdsp_story_actions(); + StoryAction fallback = action; + if (const Pokemon::StoryActionSpec* spec = Pokemon::find_story_action(specs, kind, "")){ + fallback.args = spec->args; + } + table.add(kind, "", std::move(fallback)); + for (const Pokemon::StoryActionSpec& spec : specs){ + // A name that is also a story event stays the event's. + if (spec.kind != kind || spec.name.empty() || story_events().contains(spec.name)){ + continue; + } + StoryAction named = action; + named.args = spec.args; + table.add(kind, spec.name, std::move(named)); + } +} + +} // namespace + + +Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound){ + Pokemon::StoryActionTable table; + using WalkMethod = Outcome (StoryWalkSteps::*)(RunState&, const StoryStep&); + const auto walk = [&](StoryStepKind kind, WalkMethod method){ + add_with_specs(table, kind, StoryAction{ + .act = [&walks, method](const StoryStep& step, RunState& state){ + return (walks.*method)(state, step); + }, + }); + }; + walk(StoryStepKind::ROUTE, &StoryWalkSteps::route); + walk(StoryStepKind::LEGS, &StoryWalkSteps::legs); + walk(StoryStepKind::STEP, &StoryWalkSteps::step); + walk(StoryStepKind::TALK, &StoryWalkSteps::talk); + walk(StoryStepKind::MASH, &StoryWalkSteps::mash); + + const auto console = [&](StoryStepKind kind, const Pokemon::StoryActionFunction& act){ + add_with_specs(table, kind, StoryAction{.act = act, .needs_console = true}); + }; + console(StoryStepKind::NEW_GAME, bound.new_game); + console(StoryStepKind::OPTIONS, bound.options); + console(StoryStepKind::ANSWER, bound.answer); + console(StoryStepKind::BATTLE, bound.battle); + console(StoryStepKind::TRAIN, bound.train); + console(StoryStepKind::HEAL, bound.heal); + console(StoryStepKind::SAVE, bound.save); + + for (const auto& item : story_events()){ + StoryAction event{.act = bound.event(item.first), .needs_console = true}; + for (const Pokemon::StoryActionSpec& spec : bdsp_story_actions()){ + if (spec.name == item.first && !spec.args.empty()){ + event.args = spec.args; + break; + } + } + table.add_any_kind(item.first, std::move(event)); + } + return table; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h new file mode 100644 index 0000000000..6204f74751 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h @@ -0,0 +1,54 @@ +/* BDSP AutoStory - Story actions + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's story steps as one action table (Pokemon_StoryActions.h), which + * the console (StoryExecutor) and the simulation (SimExecutor) each bind. + * The walking steps (ROUTE, LEGS, STEP, TALK, MASH) are the same code on + * both: StoryWalkSteps on the binding's StoryStepIO. The steps that need + * the console (the intro, the options, answers, battles, training, the + * Center, saves, and the story's events, story_events()) are the + * binding's own: played on the console, stood in for on the simulation. + * + * A GAME step runs only the event its variant names, and BDSP has no SHOP + * or READY step: lint_story_checkpoint() reports a step with neither. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryActions_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryActions_H + +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryActions.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The actions a binding does its own way, one per kind that needs the +// console. +struct StoryBoundActions{ + Pokemon::StoryActionFunction new_game = {}; + Pokemon::StoryActionFunction options = {}; + Pokemon::StoryActionFunction answer = {}; + Pokemon::StoryActionFunction battle = {}; + Pokemon::StoryActionFunction train = {}; + Pokemon::StoryActionFunction heal = {}; + Pokemon::StoryActionFunction save = {}; + // The action for the story event named `name` (story_events()). + std::function event = {}; +}; + +// BDSP's story actions: the walking steps on `walks`, the rest `bound`'s. +// `walks` must outlive the table. +Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp new file mode 100644 index 0000000000..184f8d8a02 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp @@ -0,0 +1,256 @@ +/* BDSP Authored Checkpoint Data + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_StoryData.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +Pokemon::StoryArgSpec required(std::string key, Pokemon::StoryArgType type, std::string why){ + return Pokemon::StoryArgSpec{.key = std::move(key), .type = type, .required = true, .fallback = std::nullopt, .why = std::move(why)}; +} + +Pokemon::StoryArgSpec optional(std::string key, Pokemon::StoryArgType type, Pokemon::StoryArgValue fallback, std::string why){ + return Pokemon::StoryArgSpec{.key = std::move(key), .type = type, .required = false, .fallback = std::move(fallback), .why = std::move(why)}; +} + +void add_event_actions(std::vector& actions){ + for (const auto& [name, unused] : story_events()){ + Pokemon::StoryActionSpec spec{.kind = Pokemon::StoryStepKind::GAME, .name = name, .args = {}}; + // 00.05c's start probe steps west from either save it may start on: + // the Poketch Company's sign stops it, the street does not, so it + // ends on one tile it knows. + if (name == "jubilife_start_probe"){ + spec.certainty = Pokemon::StoryCertainty{.needs = Pokemon::PoseCertainty::SET, .leaves = Pokemon::PoseEnd::WALKED}; + } + actions.push_back(std::move(spec)); + } +} + +// A hold to a wall or onto a map ends on its declared end from any start +// on the map: it is how a step pins an unsure pose. +constexpr Pokemon::StoryCertainty PINS{.needs = Pokemon::PoseCertainty::UNKNOWN, .leaves = Pokemon::PoseEnd::DECLARED}; + +// The LEGS actions, each walking `legs`. +void add_legs_actions( + std::vector& actions, + const Pokemon::StoryArgSpec& legs_arg, const Pokemon::StoryArgSpec& fight_wild +){ + using Pokemon::PoseCertainty; + using Pokemon::PoseEnd; + for (std::string_view act : {"to_wall", "to_map"}){ + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = std::string(act), .args = {legs_arg, fight_wild}, .console_only = false, .certainty = PINS}); + } + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "to_dialogue", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::UNKNOWN, .leaves = PoseEnd::GAME_SET}}); + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "one_short_ok", .args = {legs_arg, fight_wild}}); + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "counted_wall", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::SET, .leaves = PoseEnd::DECLARED}}); + // Taps counted against the map from each tile we may be on: legs each + // ended by a wall take every one of them to the declared end. + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "measured", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::SET, .leaves = PoseEnd::DECLARED}}); +} + +// The talks to someone who wanders: each walks in D-pad taps, a whole tile +// it counts, and ends on its declared tile. +void add_wanderer_talks(std::vector& actions){ + const Pokemon::StoryArgSpec again = optional("again", Pokemon::StoryArgType::BOOLEAN, false, "a talk with no coupon question counts"); + const Pokemon::StoryCertainty certainty{.needs = Pokemon::PoseCertainty::TILE, .leaves = Pokemon::PoseEnd::DECLARED}; + // Walks the row below his home, asking north. + actions.push_back({.kind = Pokemon::StoryStepKind::TALK, .name = "talk_to_wanderer", .args = {again}, + .console_only = false, .certainty = certainty}); + // Waits for him at an end column of his box, then at the other end + // (WanderWaitPlan); ends against a wall the declared way and one at + // the stand column's back. + const Pokemon::StoryArgSpec stand = required( + "stand", Pokemon::StoryArgType::GRID_POSE, "the tiles to wait on first, in order, on one column; facing into his box" + ); + const Pokemon::StoryArgSpec far_stand = optional( + "far_stand", Pokemon::StoryArgType::GRID_POSE, Pokemon::GridPose{}, + "the tiles to wait on next, on his box's other end column; facing into it" + ); + actions.push_back({.kind = Pokemon::StoryStepKind::TALK, .name = "wait_for_wanderer", .args = {stand, far_stand, again}, + .console_only = false, .certainty = certainty}); +} + +// ROUTE "exit": the room's way on is a door this way; a fight or a lost +// count on the way ends in a hold to the door, not a replan. +Pokemon::StoryArgSpec leave_arg(){ + return {.key = "leave", .type = Pokemon::StoryArgType::DIRECTION, .required = false, .fallback = std::nullopt, + .why = "the room's way on is a door this way"}; +} + +// The steps that end on a door or a banner: to_banner and door (the way to +// step), or_legs (a step, or legs if it did not move). +void add_door_steps( + std::vector& actions, + const Pokemon::StoryArgSpec& direction_arg, const Pokemon::StoryArgSpec& legs_arg +){ + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "to_banner", .args = {direction_arg}, .console_only = false, .certainty = PINS}); + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "door", .args = {direction_arg}}); + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "or_legs", .args = {direction_arg, legs_arg}}); +} + +std::vector make_bdsp_actions(){ + std::vector actions( + Pokemon::builtin_story_actions().begin(), Pokemon::builtin_story_actions().end() + ); + const Pokemon::StoryArgSpec fight_wild = optional( + "fight_wild", Pokemon::StoryArgType::BOOLEAN, false, "fight wild battles on the way instead of running" + ); + const Pokemon::StoryArgSpec route_target = required( + "target", Pokemon::StoryArgType::GRID_POSE, "where to go; with several tiles, any of them" + ); + const Pokemon::StoryArgSpec legs_arg = required( + "legs", Pokemon::StoryArgType::LEGS, "the straight legs to walk" + ); + const Pokemon::StoryArgSpec direction_arg = required( + "direction", Pokemon::StoryArgType::DIRECTION, "the way to step or face" + ); + const Pokemon::StoryArgSpec may_lose = optional( + "may_lose", Pokemon::StoryArgType::BOOLEAN, false, "losing goes on (a rival's first battle)" + ); + const Pokemon::StoryArgSpec items = required( + "items", Pokemon::StoryArgType::ITEMS, "the items and how many of each" + ); + + // No fallback: a ROUTE step without legs then fits the built-in "route". + const Pokemon::StoryArgSpec opt_legs{.key = "legs", .type = Pokemon::StoryArgType::LEGS, .required = false, + .fallback = std::nullopt, .why = "after the route, run its one leg until a dialogue starts"}; + for (auto& action : actions){ + if (action.kind == Pokemon::StoryStepKind::ROUTE){ + action.args.push_back(opt_legs); + } + } + const Pokemon::StoryArgSpec opt_avoid{.key = "avoid", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, + .fallback = std::nullopt, .why = "ground on the target's map to keep off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; + const Pokemon::StoryArgSpec opt_greeter{.key = "greeter", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, + .fallback = std::nullopt, .why = "ground on the target's map where someone walks up and talks, then walks off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; + for (std::string_view cost : {"town", "overworld", "indoors", "caves", "robust", "line", "trigger", "exit"}){ + actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = std::string(cost), .args = {route_target, fight_wild, opt_legs, opt_avoid, opt_greeter, leave_arg()}}); + } + actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = "into_scene", .args = {route_target, fight_wild, legs_arg}}); + add_legs_actions(actions, legs_arg, fight_wild); + add_door_steps(actions, direction_arg, legs_arg); + add_wanderer_talks(actions); + actions.push_back({.kind = Pokemon::StoryStepKind::MASH, .name = "a", .args = {}}); + actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "starter", .args = {may_lose}}); + actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "doubles", .args = {may_lose}}); + actions.push_back({.kind = Pokemon::StoryStepKind::SHOP, .name = "sandgem_potions", .args = {items}}); + add_event_actions(actions); + return actions; +} + +StoryFailure read_failure(std::string_view value){ + if (value == "reload_and_retry"){ + return StoryFailure::RELOAD_AND_RETRY; + } + if (value == "stop"){ + return StoryFailure::STOP; + } + throw ParseException("Unsupported BDSP failure policy: " + std::string(value)); +} + +// The file without its "metadata", which the shared reader doesn't take +// (yet): {"phase": testing, verification or reproduction}, how far the +// checkpoint is trusted to save (the engine's save policy reads it). +JsonValue without_metadata(const JsonValue& value, std::string_view source){ + JsonObject stripped; + for (const auto& [key, field] : value.to_object_throw(std::string(source))){ + if (key != "metadata"){ + stripped[key] = field.clone(); + continue; + } + const JsonObject& metadata = field.to_object_throw(std::string(source)); + for (const auto& [metadata_key, unused] : metadata){ + if (metadata_key != "phase"){ + throw ParseException(std::format("{}: unsupported metadata field {}", source, metadata_key)); + } + } + const std::string& phase = metadata.get_string_throw("phase", std::string(source)); + if (phase != "testing" && phase != "verification" && phase != "reproduction"){ + throw ParseException(std::format("{}: unknown phase {}", source, phase)); + } + } + return JsonValue(std::move(stripped)); +} + +} // namespace + +std::span bdsp_story_actions(){ + static const std::vector actions = make_bdsp_actions(); + return actions; +} + +StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source){ + StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; + std::string saves_in; + auto read_execution = [&](const JsonObject& execution, std::string_view src){ + Pokemon::validate_json_fields(execution, {"on_failure", "saves_in"}, src); + on_failure = read_failure(execution.get_string_throw("on_failure")); + saves_in = execution.get_string_throw("saves_in"); + if ((on_failure == StoryFailure::RELOAD_AND_RETRY) == saves_in.empty()){ + throw ParseException("BDSP save-card location is required exactly when the checkpoint saves/retries."); + } + }; + StoryCheckpoint result; + static_cast(result) = Pokemon::read_authored_checkpoint( + without_metadata(value, source), "bdsp", source, read_execution, bdsp_story_actions() + ); + result.on_failure = on_failure; + result.saves_in = std::move(saves_in); + return result; +} + +JsonValue authored_checkpoint_document(std::string_view file){ + for (const auto& [name, text] : bdsp_checkpoint_json()){ + if (name == file){ + return parse_json(text); + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Authored checkpoint is not embedded: " + std::string(file)); +} + +StoryCheckpoint load_authored_checkpoint(std::string_view file){ + return read_authored_checkpoint(authored_checkpoint_document(file), file); +} + +std::vector load_authored_story(){ + std::vector result; + for (const auto& [name, text] : bdsp_checkpoint_json()){ + if (name.ends_with(".plan")){ + result.push_back(read_authored_checkpoint(parse_json(text), name)); + } + } + std::sort(result.begin(), result.end(), [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }); + for (size_t c = 1; c < result.size(); c++){ + if (result[c].id == result[c - 1].id){ + throw ParseException("Two authored checkpoints are " + result[c].id + "."); + } + } + return result; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h new file mode 100644 index 0000000000..052cd044d8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h @@ -0,0 +1,47 @@ +/* BDSP Authored Checkpoint Data + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryData_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryData_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The action specifications that BDSP steps parse against. +std::span bdsp_story_actions(); + +// The embedded authored JSON files, keyed by filename without .json. +const std::vector>& bdsp_checkpoint_json(); + +// Strict authored envelope and BDSP save/failure configuration; no controller IO. +// Unknown fields/values or inconsistent save policy throw ParseException. +StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source); + +// Load an embedded authored checkpoint; a missing file throws InternalProgramError. +StoryCheckpoint load_authored_checkpoint(std::string_view file); + +// Load all embedded authored checkpoints in id order. +std::vector load_authored_story(); + +// The embedded authored checkpoint file, parsed but not read. +JsonValue authored_checkpoint_document(std::string_view file); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp new file mode 100644 index 0000000000..7dca2b0353 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp @@ -0,0 +1,232 @@ +/* BDSP Authored Checkpoint Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" +#include "PokemonBDSP_AutoStory_StoryData.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryData_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +struct StoryDataInput{ + std::string name; + void (*mutate)(JsonObject&) = nullptr; +}; + +struct StoryDataExpectation{ + bool rejected = false; +}; + +struct StoryDataCase{ + StoryDataInput input; + StoryDataExpectation expectation; +}; + +void extra(JsonObject& root){ + root["unknown"] = true; +} + +void wrong_game(JsonObject& root){ + root["game"] = "frlg"; +} + +void wrong_policy(JsonObject& root){ + root.get_object_throw("execution")["on_failure"] = "guess"; +} + +void missing_card(JsonObject& root){ + root.get_object_throw("execution")["saves_in"] = ""; +} + +void stop_with_save(JsonObject& root){ + root.get_object_throw("execution")["on_failure"] = "stop"; +} + +void bad_metadata_field(JsonObject& root){ + JsonObject meta; + meta["unknown"] = "testing"; + root["metadata"] = std::move(meta); +} + +void bad_metadata_phase(JsonObject& root){ + JsonObject meta; + meta["phase"] = "invalid"; + root["metadata"] = std::move(meta); +} + +UnitTestResult check_all_checkpoints_load(){ + const auto& table = story_checkpoints(); + if (table.size() != 22){ + return std::format("Expected 22 authored checkpoints, got {}", table.size()); + } + + const std::vector expected_ids{ + "00.00", "00.01", "00.02", "00.03", "00.04", "00.04b", "00.04c", "00.04d", + "00.05", "00.05b", "00.05c", + "01.01", "01.02", "01.02b", "01.03", "01.04", "01.05", "01.06", "01.07", "01.08", "01.09", "01.10", "01.11" + }; + + for (size_t i = 0; i < expected_ids.size(); i++){ + if (table[i].id != expected_ids[i]){ + return std::format("Checkpoint index {} mismatch: expected {}, got {}", i, expected_ids[i], table[i].id); + } + if (table[i].id == "00.00"){ + if (table[i].on_failure != StoryFailure::STOP || !table[i].saves_in.empty()){ + return "00.00 must have on_failure=stop and empty saves_in"; + } + }else{ + if (table[i].on_failure != StoryFailure::RELOAD_AND_RETRY || table[i].saves_in.empty()){ + return std::format("{} must have on_failure=reload_and_retry and non-empty saves_in", table[i].id); + } + } + if (table[i].steps.empty()){ + return std::format("{} has no steps", table[i].id); + } + } + + const StoryCheckpoint mine = load_authored_checkpoint("bdsp-mine-01.03.plan"); + if (mine.id != "01.03" || mine.on_failure != StoryFailure::RELOAD_AND_RETRY || mine.saves_in != "Oreburgh City"){ + return "Failed to load bdsp-mine-01.03.plan correctly"; + } + + return true; +} + +UnitTestResult check_parity_01_02b(){ + const auto& table = story_checkpoints(); + auto it = std::find_if(table.begin(), table.end(), [](const StoryCheckpoint& cp){ + return cp.id == "01.02b"; + }); + if (it == table.end()){ + return "01.02b checkpoint not found in table"; + } + const StoryCheckpoint& cp = *it; + if (cp.title != "Oreburgh Center heal"){ + return "01.02b title mismatch"; + } + if (cp.on_failure != StoryFailure::RELOAD_AND_RETRY || cp.saves_in != "Oreburgh City"){ + return "01.02b policy mismatch"; + } + if (cp.steps.size() != 5){ + return std::format("01.02b expected 5 steps, got {}", cp.steps.size()); + } + const std::vector expected_step_names{ + "below_barry", "barry", "barrys_lines", "below_the_centers_door", "heal_oreburgh_first" + }; + for (size_t i = 0; i < expected_step_names.size(); i++){ + if (cp.steps[i].name != expected_step_names[i]){ + return std::format("01.02b step {} name mismatch: expected {}, got {}", i, expected_step_names[i], cp.steps[i].name); + } + } + return true; +} + +UnitTestResult run_envelope_case(const StoryDataCase& test_case){ + JsonValue val = authored_checkpoint_document("bdsp-00.01.plan"); + if (test_case.input.mutate != nullptr){ + test_case.input.mutate(val.to_object_throw()); + } + try{ + StoryCheckpoint cp = read_authored_checkpoint(val, test_case.input.name); + if (test_case.expectation.rejected){ + return "Expected rejection but read_authored_checkpoint succeeded."; + } + if (cp.id != "00.01"){ + return "Loaded checkpoint id mismatch."; + } + return true; + }catch (const Exception&){ + if (test_case.expectation.rejected){ + return true; + } + return "Unexpected rejection."; + } +} + +class Test_StoryData : public UnitTest{ +public: + explicit Test_StoryData(StoryDataCase test_case) + : UnitTest("PokemonBDSP::StoryData - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (m_case.input.name == "All 22 authored checkpoints load"){ + return check_all_checkpoints_load(); + } + return run_envelope_case(m_case); + } + +private: + StoryDataCase m_case; +}; + +const StoryDataCase STORY_DATA_CASES[] = { + { + .input = {.name = "All 23 authored checkpoints load"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Parity: 01.02b checkpoint"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Envelope: valid 00.01 plan"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Envelope: extra envelope field", .mutate = extra}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: wrong game", .mutate = wrong_game}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown failure policy", .mutate = wrong_policy}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: missing save-card location for reload_and_retry", .mutate = missing_card}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: stop policy with start save", .mutate = stop_with_save}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown metadata field", .mutate = bad_metadata_field}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown metadata phase", .mutate = bad_metadata_phase}, + .expectation = {.rejected = true}, + }, +}; + +} // namespace + +void add_tests_AutoStory_StoryData(UnitTestDatabase& database){ + for (const auto& item : STORY_DATA_CASES){ + database.add(item); + } +} + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h new file mode 100644 index 0000000000..a5f20325cd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h @@ -0,0 +1,22 @@ +/* BDSP Authored Checkpoint Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryData_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryData_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +// Registers unit tests for BDSP authored story data loading and envelope validation. +void add_tests_AutoStory_StoryData(UnitTestDatabase& database); + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp new file mode 100644 index 0000000000..6176a8a58f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp @@ -0,0 +1,387 @@ +/* BDSP AutoStory - Story executor + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/FatalProgramException.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/Globals.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "Actions/PokemonBDSP_AutoStory_LitDoorway.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "Actions/PokemonBDSP_AutoStory_Actions.h" +#include "Actions/PokemonBDSP_AutoStory_Center.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "Actions/PokemonBDSP_AutoStory_NewGame.h" +#include "Actions/PokemonBDSP_AutoStory_Settings.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP_AutoStory_Walks.h" +#include "Actions/PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Outcome; +using Pokemon::Pose; +using Pokemon::RunState; +using Pokemon::StoryStep; +using Pokemon::TilePos; +using Pokemon::story_arg; + + +std::optional story_route_costs(const std::string& action){ + if (action.empty() || action == ROUTE_ONTO_TRIGGER || action == ROUTE_INTO_SCENE || action == "route" || action == "exit"){ + return route_plan_costs(); + } + if (action == "town"){ + return town_plan_costs(); + } + if (action == "robust"){ + return RobustPlanCosts(); + } + if (action == "line"){ + return waypoint_plan_costs(); + } + return std::nullopt; +} + + +namespace{ + +// "to_catch_grass" as "to catch grass", for the logs. +std::string label_of(const StoryStep& step){ + std::string label = step.name; + std::replace(label.begin(), label.end(), '_', ' '); + return label; +} + +// The screen at a failed step, kept: the retry resets the game. +void save_failure_screenshot(VideoStream& stream, size_t attempt){ + VideoSnapshot screen = stream.video().snapshot(); + if (!screen){ + return; + } + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/failures/"; + Filesystem::create_directories(Filesystem::Path(folder)); + const std::string path = std::format("{}{}-attempt{}.png", folder, now_to_filestring(), attempt); + screen.frame->save(path); + stream.log("Failure screenshot: " + path); +} + +} // namespace + + +void ConsoleStepGuard::before_step(const StoryStep& step){ + m_stream.log(std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), COLOR_BLUE); +} + +bool ConsoleStepGuard::stops_on_failure(const StoryStep&) const{ + return m_stop_rows.contains(m_checkpoint); +} + +void ConsoleStepGuard::on_failure(const StoryStep&, const OperationFailedException& error){ + // Attempts are the segment's policy; this only counts them for the + // log line the lab's run watcher reads. + const size_t attempt = m_failures[m_checkpoint]++; + m_stream.log(std::format("checkpoint_reattempt_loop: attempt {} failed: {}: {}", attempt, m_checkpoint, error.message()), COLOR_RED); + save_failure_screenshot(m_stream, attempt); +} + +void ConsoleStepGuard::on_interrupt(const StoryStep& step, const Pokemon::InterruptCheck& check){ + const bool cleared = check.outcome.succeeded(); + m_stream.log(std::format( + "[{}] {}: before it, {}: {}", m_checkpoint, step.name, + Pokemon::interrupt_kind_name(check.verdict.kind), cleared ? "cleared" : Pokemon::to_string(check.outcome) + ), cleared ? COLOR_ORANGE : COLOR_RED); +} + +void ConsoleStepGuard::on_screen(const StoryStep& step, const Pokemon::ScreenSettle& screen){ + m_stream.log(std::format("[Screen] before {}: {}", step.name, Pokemon::describe(screen))); +} + + +// The table and the guard are members, built after the base: it only +// keeps references to them until a step runs. +StoryExecutor::StoryExecutor(StoryEnv& env) + : Pokemon::TableExecutor(m_table, m_guard) + , m_env(env) + , m_io(env.env.console, env.context) + , m_walks(m_io) + , m_guard(env.env.console, m_checkpoint) + , m_table(story_action_table(m_walks, console_actions())) + , m_look(env.env.console, env.context) + , m_interrupts(story_interrupts([this](bool fresh){ return m_look.look(fresh); }, console_interrupt_actions())) +{ + set_interrupts(&m_interrupts); +} + +StoryBoundActions StoryExecutor::console_actions(){ + const auto method = [this](Outcome (StoryExecutor::*action)(RunState&, const StoryStep&)){ + return [this, action](const StoryStep& step, RunState& state){ + return (this->*action)(state, step); + }; + }; + return StoryBoundActions{ + .new_game = method(&StoryExecutor::new_game), + .options = method(&StoryExecutor::options), + .answer = method(&StoryExecutor::answer), + .battle = method(&StoryExecutor::battle), + .train = method(&StoryExecutor::train), + .heal = method(&StoryExecutor::heal), + .save = method(&StoryExecutor::save), + .event = [this](const std::string& name) -> Pokemon::StoryActionFunction{ + return [this, event = story_events().at(name)](const StoryStep& step, RunState& state){ + return event(*this, state, step); + }; + }, + }; +} + +// A wild battle is run from, a trainer who saw us fought, a dialog or a +// warp's scene pressed through to the quiet overworld. +StoryInterruptActions StoryExecutor::console_interrupt_actions(){ + const auto settle = [this](std::string label){ + return [this, label](const Pokemon::InterruptVerdict&, RunState&){ + m_io.settle(BUTTON_B, label, SettleKind::TALK, ""); + return Pokemon::outcome_ok(); + }; + }; + return StoryInterruptActions{ + .wild = [this](const Pokemon::InterruptVerdict&, RunState&){ + run_from_wild_battle(stream(), context()); + return Pokemon::outcome_ok(); + }, + .trainer = [this](const Pokemon::InterruptVerdict&, RunState&){ + m_io.fight(""); + return Pokemon::outcome_ok(); + }, + .dialog = settle("a dialog between steps"), + .warp = settle("a warp between steps"), + }; +} + +void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ + m_checkpoint = data.id; + m_walks.begin_attempt(data.id, data.steps); +} + + +Outcome StoryExecutor::new_game(RunState&, const StoryStep&){ + play_new_game_intro(m_env.env, context(), m_env.intro_picks, m_env.player_name); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::options(RunState&, const StoryStep&){ + verify_tutorial_settings(stream(), context()); + context().wait_for_all_requests(); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ + wait_for_dialogue(stream(), context(), label_of(step)); + const bool yes = story_arg(step.args, "yes"); + pbf_press_button(context(), yes ? BUTTON_A : BUTTON_B, 80ms, 1000ms); + context().wait_for_all_requests(); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::battle(RunState&, const StoryStep& step){ + // "starter": the Starly battle at the lake (00.02). It opens on its own, + // with no talk, and the starter has no Poke Balls to throw; the scene + // goes on whoever wins. + if (step.action == "starter"){ + fight_starly(stream(), context()); + return Pokemon::outcome_ok(); + } + // A loss sends us to a Pokemon Center, so ending on its floor means a + // loss - unless the floor check already passed where the battle began: + // Jubilife's yellow plaza reads as Center floor (63 %, r79 won Rowan's + // tag battle there and was called lost). + const bool may_lose = story_arg(step.args, "may_lose"); + const bool center_before = !may_lose && in_pokemon_center(stream()); + if (!talk_or_fight(stream(), context(), label_of(step), TalkExpect::BATTLE, step.action == "doubles" ? BattleFormat::DOUBLES : BattleFormat::SINGLES)){ + fail_step(stream(), label_of(step) + ": talked but did not battle."); + } + if (center_before){ + stream().log(label_of(step) + ": the floor read as a Center's before the battle too; no loss check here.", COLOR_ORANGE); + }else if (!may_lose && in_pokemon_center(stream())){ + fail_step(stream(), label_of(step) + ": lost the battle."); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::train(RunState&, const StoryStep& step){ + const int64_t battles = story_arg(step.args, "battles"); + for (int64_t battle = 0; battle < battles; battle++){ + const std::string label = std::format("Training battle {}/{}", battle + 1, battles); + route_status(stream(), label); + battle_1_pokemon(stream(), context()); + fail_if_blacked_out(stream(), context(), label); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ + // From the planned tile below the door, run in like any door. The lit- + // doorway lineup is for when the run in finds no door: people on the + // way can leave the route unsure of the column (r59, Jubilife). Below + // Sandgem's door the lineup read 0.7 tile off and stepped us off it (r48). + // With no fade, the run can have gone on in to the counter (lab-B + // 2026-11-08, Oreburgh). + route_turn(stream(), context(), Direction::NORTH); + const CenterEntry in = run_into_center(m_io, m_walks.can_run(), [this]{ + context().wait_for_all_requests(); + const VideoSnapshot screen = stream().video().snapshot(); + return screen && pokemon_center_inside(*screen.frame); + }, [&]{ + enter_lit_door(stream(), context(), label_of(step)); + }, label_of(step)); + stream().log(std::format("[{}] {}: north into the Center: {}.", m_checkpoint, step.name, + in == CenterEntry::FADED ? "faded" : in == CenterEntry::INSIDE ? "no fade, but inside" : "no fade; lined up by sight")); + talk_to_nurse(stream(), context(), label_of(step)); + // Her last line ("We hope to see you again!") can still be up, and a + // line stops the run out at once (r62): B until none shows. + ShortDialogDetector line; + for (size_t presses = 0; presses < 10; presses++){ + context().wait_for_all_requests(); + const VideoSnapshot screen = stream().video().snapshot(); + if (!screen || !line.detect(*screen.frame)){ + break; + } + pbf_press_button(context(), BUTTON_B, 80ms, 700ms); + } + // Out the same way: south until the fade. The walk out's indoor check + // read "not a room" a step from the counter and stopped inside (r50, r59). + route_turn(stream(), context(), Direction::SOUTH); + const bool out = m_io.run_into_door(Direction::SOUTH, m_walks.can_run()).any(); + stream().log(std::format("[{}] {}: south out of the Center: {}.", m_checkpoint, step.name, out ? "faded" : "no fade yet")); + if (out){ + m_io.settle(BUTTON_B, label_of(step), SettleKind::DOOR, ""); + }else{ + m_io.settle(BUTTON_B, label_of(step), SettleKind::AFTER_WARP, ""); + } + + // r143a's heal said it had walked out while still inside. + context().wait_for_all_requests(); + VideoSnapshot screen = stream().video().snapshot(); + if (screen && indoors(*screen.frame)){ + fail_step(stream(), label_of(step) + ": still inside the Pokemon Center."); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::save(RunState&, const StoryStep& step){ + if (step.action.empty()){ + save_game(m_env.env, context()); + return Pokemon::outcome_ok(); + } + // A row's start save, its action the map the card must name. With a + // map, save_game() throws only when the card names another one, having + // backed out unsaved. That stops the program: a reset would load an + // older save and run this row from wherever that is. + try{ + save_game(m_env.env, context(), step.action); + }catch (OperationFailedException& e){ + throw FatalProgramException( + ErrorReportMode::NO_ERROR_REPORT, + std::format("[{}] Not at the checkpoint's start, so not saving: {}", m_checkpoint, e.message()), + stream() + ); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::game(RunState&, const StoryStep& step){ + return Pokemon::outcome_failed("unknown_event", step.action); +} + + +Pokemon::Segment make_story_segment( + const std::vector& rows, Pokemon::StoryStepExecutor& executor, + std::set& saved, Pokemon::StorySaveMode save_mode +){ + static const StoryStep SAVE_AT_START = []{ + StoryStep step; + step.kind = Pokemon::StoryStepKind::SAVE; + step.name = "save"; + step.why = "the save a failed step resets to"; + return step; + }(); + Pokemon::Segment segment; + segment.id = "BDSP"; + for (const StoryCheckpoint& row : rows){ + segment.checkpoints.push_back(Pokemon::CheckpointSpec{ + .id = row.id, .title = row.title, .saves = false, + .build = [&row, &executor, &saved, save_mode]{ + Pokemon::Checkpoint steps = Pokemon::build_story_checkpoint(row, executor); + // Only PER_CHECKPOINT saves part-way through a run (console + // B, 2026-11-11: save mode none saved at 00.05b's start). + if (row.on_failure != StoryFailure::RELOAD_AND_RETRY || save_mode != Pokemon::StorySaveMode::PER_CHECKPOINT){ + return steps; + } + StoryStep save_at_start = SAVE_AT_START; + save_at_start.action = row.saves_in; + Pokemon::Event save = Pokemon::story_event(save_at_start, executor); + save.skip_if.push_back(Pokemon::condition("saved at the start", [&saved, id = row.id](RunState&){ + return saved.contains(id); + })); + // A save that fails is a failed first attempt: the retry + // resets and does not save again. + save.act = [&executor, &saved, id = row.id, save_at_start](RunState& state){ + saved.insert(id); + return executor.run(save_at_start, state); + }; + Pokemon::Checkpoint checkpoint; + checkpoint.id = row.id; + checkpoint.events.push_back(std::move(save)); + for (Pokemon::Event& event : steps.events){ + checkpoint.events.push_back(std::move(event)); + } + return checkpoint; + }, + }); + } + return segment; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h new file mode 100644 index 0000000000..f10ba8a0af --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h @@ -0,0 +1,242 @@ +/* BDSP AutoStory - Story executor + * +// code-health: allow header_symbols -- BDSP story executor and checkpoint execution primitives + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's way of doing the shared story steps + * (Pokemon/AutoStory/Pokemon_StorySteps.h) on the console. A route is + * planned on the joined maps that hold its start and its target + * (story_plan_world()), from the tiles the run's pose holds, and walked by + * the walker, which fights the trainers who see us and runs from wild + * battles. A step whose variant names one of the story's events + * (story_events(), PokemonBDSP_AutoStory_Events.h) runs that event, + * whatever its kind: the scripted scenes and the console-measured walks + * of segment 01. It is the console's binding of the story's action table + * (story_action_table()); the simulation binds the same table. + * + * Every step runs inside a guard (ConsoleStepGuard) that turns a failed + * console step into a failed Outcome, with a screenshot and the + * "checkpoint_reattempt_loop: attempt N failed" line the lab's run watcher + * reads; the segment then reloads the row's save and runs it again. + * Before each step it clears what came up since the step before + * (story_interrupts(), PokemonBDSP_AutoStory_StoryInterrupts.h). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryExecutor_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryExecutor_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "Pokemon/AutoStory/Pokemon_StoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP_AutoStory_StoryActions.h" +#include "PokemonBDSP_AutoStory_StoryInterrupts.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Starter Pokemon options for BDSP story automation. +enum class StarterChoice{ + TURTWIG, + CHIMCHAR, + PIPLUP, +}; + +// What a row does when one of its steps fails. +enum class StoryFailure{ + // Save before the first step; on a failure reset to that save and run + // the row again from its first step. + RELOAD_AND_RETRY, + // No save and no retry: the failure ends the program (00.00, before + // the game has anything worth saving). + STOP, +}; + +// A row of the story table: the shared checkpoint, and what a failure does. +struct StoryCheckpoint : public Pokemon::StoryCheckpoint{ + StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; + // The map the save screen's card names where the row starts ("Route + // 203"). The save at its start is refused anywhere else and the program + // stops: r76/r77 started 01.06 a walk away from its start and saved over + // the good save there. Empty for a row that does not save first. + std::string saves_in; +}; + +// What the story's steps act on. +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + StarterChoice starter = StarterChoice::CHIMCHAR; + std::string player_name; + // The new game's language and look. + IntroPicks intro_picks = {}; + std::function run_starter_rng; +}; + + +// The variant of a ROUTE step whose target is the next scene's trigger: +// the scene starts there and stops the walk, as a wall would (00.04's +// Mom's call on (6, 9)). It plans with the route costs. +inline constexpr std::string_view ROUTE_ONTO_TRIGGER = "trigger"; + +// The variant of a ROUTE step that, once at its target, runs its one leg +// ("legs") until a scene's dialogue starts. It plans with the route costs. +inline constexpr std::string_view ROUTE_INTO_SCENE = "into_scene"; + +// The plan costs a ROUTE step's variant names: "", ROUTE_ONTO_TRIGGER and +// ROUTE_INTO_SCENE the route costs, "town" town_plan_costs(), "robust" the planner's +// defaults, "line" waypoint_plan_costs() (a stretch of a line a human +// walked). Nothing for another name. +std::optional story_route_costs(const std::string& variant); + + +// What the console does around each story step: the step's log line, and +// a failed step's "checkpoint_reattempt_loop" line and screenshot. +class ConsoleStepGuard : public Pokemon::StepGuard{ +public: + // `checkpoint` names the row running now; both must outlive the guard. + ConsoleStepGuard(VideoStream& stream, const std::string& checkpoint) + : m_stream(stream) + , m_checkpoint(checkpoint) + {} + // The rows whose failure ends the program (StoryFailure::STOP). + void set_stop_rows(std::set rows){ + m_stop_rows = std::move(rows); + } + + virtual void before_step(const Pokemon::StoryStep& step) override; + virtual bool stops_on_failure(const Pokemon::StoryStep& step) const override; + virtual void on_failure(const Pokemon::StoryStep& step, const OperationFailedException& error) override; + virtual void on_interrupt(const Pokemon::StoryStep& step, const Pokemon::InterruptCheck& check) override; + virtual void on_screen(const Pokemon::StoryStep& step, const Pokemon::ScreenSettle& screen) override; + +private: + VideoStream& m_stream; + const std::string& m_checkpoint; + std::map m_failures; + std::set m_stop_rows; +}; + + +// Does BDSP's kinds of story step on the console: the story's action +// table, its rows that need the console bound to the methods below. +class StoryExecutor : public Pokemon::TableExecutor{ +public: + explicit StoryExecutor(StoryEnv& env); + + StoryEnv& env(){ + return m_env; + } + VideoStream& stream(){ + return m_env.env.console; + } + ProControllerContext& context(){ + return m_env.context; + } + // The rows whose failure ends the program (StoryFailure::STOP): a + // failed step there throws on instead of failing the step. + void set_stop_rows(std::set rows){ + m_guard.set_stop_rows(std::move(rows)); + } + // A new attempt of a row: every trainer can see us again. + virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override; + +protected: + // The row running now. + const std::string& checkpoint() const{ + return m_checkpoint; + } + // The intro of a new game, from the title screen to the bedroom. + Pokemon::Outcome new_game(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // verify_tutorial_settings(): fast text, Autosave off. + Pokemon::Outcome options(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // A for YES, B for NO, once the question's box shows. + Pokemon::Outcome answer(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // A trainer's text opens (or has) and ends in a battle, fought with the + // best move; one lost fails the step unless `may_lose`. Variant + // "doubles" for a tag battle. + Pokemon::Outcome battle(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // `battles` wild battles in the grass we stand in, with the best move. + Pokemon::Outcome train(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // In front of a Pokemon Center, its lit doorway on screen: in, the + // nurse, and out (heal_at_pokemon_center()). + Pokemon::Outcome heal(Pokemon::RunState& state, const Pokemon::StoryStep& step); + Pokemon::Outcome save(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // The event its variant names (story_events()). + Pokemon::Outcome game(Pokemon::RunState& state, const Pokemon::StoryStep& step); + +private: + StoryBoundActions console_actions(); + StoryInterruptActions console_interrupt_actions(); + + StoryEnv& m_env; + ConsoleStoryStepIO m_io; + // The steps that walk and talk, the table's rows for them: + // - ROUTE. Variant: the plan's costs ("" route, "town", "robust", "line"; "exit" is route's with "leave": + // a fight or a lost count ends in a hold to the room's door that way). + // - LEGS: each leg in D-pad taps, turning first. Variant "one_short_ok": + // a leg may end a tile short (a door lined up by sight after). Variant + // "to_wall": each leg walks until something stops it, for at most its + // tiles' time, as measured on the console; the pose is then unknown + // unless the step gives one. Variant "counted_wall": its one leg walks + // to the wall, waiting out anyone walking about in the way, and fails + // when the tiles walked are far off the map's from the step's start. + // Variant "measured": each leg in taps; the tiles of the step's start + // whose taps the map lets move as many as moved are where we are. + // - STEP: one D-pad step `direction`, turning first: onto a scene's + // trigger or into a door. Variant "to_banner": one tap into a cave + // mouth, then wait for pose_after's map's location banner. Variant + // "or_legs": into a door, and with no fade, the step's legs onto the + // door's tile and into it again. + // - TALK: A at what we face; a dialogue must open. + // - MASH: a dialogue opens (or has), then B through it to the overworld; + // variant "a": A through it, for lines that give an item. + StoryWalkSteps m_walks; + std::string m_checkpoint; + std::map m_run_counts; + ConsoleStepGuard m_guard; + // The story's actions, its console rows bound to this executor. + Pokemon::StoryActionTable m_table; + // What is cleared between steps, looked for on the screen. + ConsoleScreenLook m_look; + Pokemon::StoryInterrupts m_interrupts; +}; + + +// `rows` (kept alive by the caller) as one segment run in `save_mode`. In +// PER_CHECKPOINT a row that reloads on failure saves first, unless `saved` +// says it has in this run: a retry starts from that save. Other modes never +// save at a row's start. `executor` and `saved` must outlive the segment. +Pokemon::Segment make_story_segment( + const std::vector& rows, Pokemon::StoryStepExecutor& executor, + std::set& saved, Pokemon::StorySaveMode save_mode +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.cpp new file mode 100644 index 0000000000..3a9edd889a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.cpp @@ -0,0 +1,78 @@ +/* BDSP AutoStory - Story interrupts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonTools/Async/InferenceRoutines.h" +#include "PokemonBDSP_AutoStory_StoryInterrupts.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::InterruptKind; +using Pokemon::InterruptPose; +using Pokemon::StoryStepKind; + + +bool presses_through_text(StoryStepKind kind){ + switch (kind){ + case StoryStepKind::MASH: + case StoryStepKind::ANSWER: + case StoryStepKind::BATTLE: + case StoryStepKind::GAME: + case StoryStepKind::NEW_GAME: + return true; + default: + return false; + } +} + +Pokemon::StoryInterrupts story_interrupts(Pokemon::ScreenLookFunction look, const StoryInterruptActions& actions){ + Pokemon::StoryInterrupts out; + out.look = std::move(look); + out.handlers.add(InterruptKind::WILD_ENCOUNTER, {.handle = actions.wild, .pose = InterruptPose::UNCHANGED}); + out.handlers.add(InterruptKind::TRAINER_SIGHT, {.handle = actions.trainer, .pose = InterruptPose::UNCHANGED}); + out.handlers.add(InterruptKind::DIALOG, {.handle = actions.dialog, .pose = InterruptPose::UNCHANGED}); + out.handlers.add(InterruptKind::WARP, {.handle = actions.warp, .pose = InterruptPose::LOST}); + for (StoryStepKind step : {StoryStepKind::MASH, StoryStepKind::ANSWER, StoryStepKind::BATTLE, StoryStepKind::GAME, StoryStepKind::NEW_GAME}){ + out.handlers.starts_on(step, InterruptKind::DIALOG); + } + out.handlers.starts_on(StoryStepKind::BATTLE, InterruptKind::TRAINER_SIGHT); + // The story's wild battles (the Starly at the lake) are the step's. + out.handlers.starts_on(StoryStepKind::BATTLE, InterruptKind::WILD_ENCOUNTER); + out.handlers.starts_on(StoryStepKind::TRAIN, InterruptKind::WILD_ENCOUNTER); + for (StoryStepKind step : {StoryStepKind::GAME, StoryStepKind::NEW_GAME}){ + for (InterruptKind kind : {InterruptKind::WILD_ENCOUNTER, InterruptKind::TRAINER_SIGHT, InterruptKind::WARP}){ + out.handlers.starts_on(step, kind); + } + } + return out; +} + + +ConsoleScreenLook::ConsoleScreenLook(ConsoleHandle& console, ProControllerContext& context) + : m_stream(console) + , m_context(context) + , m_observer(BattleCue::WILD_MENU, "") + , m_eyes(console, m_observer) + , m_watcher(m_eyes) + , m_start(current_time()) +{} + +Pokemon::ScreenLook ConsoleScreenLook::look(bool fresh){ + if (fresh){ + m_watcher.reset(); + m_start = current_time(); + } + wait_until(m_stream, m_context, BETWEEN_STEPS_LOOK, {m_watcher}); + return {.log = m_watcher.log(), .window = {.start = m_start, .end = current_time()}}; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h new file mode 100644 index 0000000000..000352fcbd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h @@ -0,0 +1,80 @@ +/* BDSP AutoStory - Story interrupts + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's interrupts between story steps (Pokemon_StoryInterrupts.h): a + * wild battle is run from, a trainer who saw us is fought, a dialog is + * pressed through, and a warp is waited out, the pose then lost. The + * console (ConsoleScreenLook and the executor's console actions) and the + * simulation bind the same table. + * + * The steps that press through text (MASH, ANSWER, BATTLE, GAME, + * NEW_GAME) start on a dialog: one showing before them is theirs. A + * BATTLE and a GAME step start on a trainer's lines too, TRAIN on a wild + * battle, and a GAME step (a measured script) and NEW_GAME (the title + * screen) on anything. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryInterrupts_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryInterrupts_H + +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "Pokemon/AutoStory/Pokemon_StoryInterrupts.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What clears each kind of interrupt, as a binding does it. +struct StoryInterruptActions{ + Pokemon::InterruptAction wild = {}; + Pokemon::InterruptAction trainer = {}; + Pokemon::InterruptAction dialog = {}; + Pokemon::InterruptAction warp = {}; +}; + +// BDSP's interrupts: `look` judged with the shared knobs, `actions` for +// each kind, and the steps that start on one. +Pokemon::StoryInterrupts story_interrupts(Pokemon::ScreenLookFunction look, const StoryInterruptActions& actions); + +// Whether a step of `kind` presses through any text showing when it +// starts; a step of another kind is stopped by it (a walk, a talk). +bool presses_through_text(Pokemon::StoryStepKind kind); + + +// How long the console looks at the screen before a step: a few frames, +// for what came up after the step before had settled. +constexpr Milliseconds BETWEEN_STEPS_LOOK = Milliseconds(300); + +// The console's look between steps: BDSP's screen events for +// BETWEEN_STEPS_LOOK, everything showing in it judged. The Switch's HOME +// menu comes first (Pokemon::SwitchHomeObserver): BDSP's detectors read it +// as a quiet overworld (lab-B 2026-11-09 15:31). +class ConsoleScreenLook{ +public: + ConsoleScreenLook(ConsoleHandle& console, ProControllerContext& context); + + Pokemon::ScreenLook look(bool fresh); + +private: + VideoStream& m_stream; + ProControllerContext& m_context; + BdspScreenObserver m_observer; + Pokemon::SwitchHomeObserver m_eyes; + Pokemon::ScreenEventWatcher m_watcher; + WallClock m_start; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.cpp new file mode 100644 index 0000000000..7b1134d774 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.cpp @@ -0,0 +1,373 @@ +/* BDSP AutoStory Story Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The story's walks on its maps: Route 202's catch grass and the way into + * Sandgem, Sandgem's Center, the Oreburgh Gym's column, and the route + * records traced on their maps. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_RouteRecords.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StorySpots.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_StoryMaps_Tests.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::StoryStep; +using Pokemon::TileKind; +using Pokemon::TilePos; + +StoryWorld alone(const std::string& json_map){ + StoryWorld world; + world.add(json_map, story_map(json_map), {0, 0}); + return world; +} + +// 00.04's training walks up and down the catch patch, three grass rows +// between tree rows, and needs the walk there to end in its grass: r253 +// and r254 loaded the catch's save in the pocket behind the trees at +// (21, 29), planned from the catch spot instead, and walked up and down +// the lawn east of the grass for three minutes without a battle. +UnitTestResult test_route202_catch_grass(Logger& logger){ + const StoryWorld route = alone("Route202"); + for (int x = 15; x <= 20; x++){ + const WallHit up = slide_to_wall(route, {x, 27}, Direction::NORTH); + const WallHit down = slide_to_wall(route, {x, 25}, Direction::SOUTH); + if (!(up.stop == TilePos{x, 25}) || !(down.stop == TilePos{x, 27})){ + return std::format("Catch patch column {}: north from row 27 stops at {}, south from row 25 at {}; expected rows 25 and 27, between tree rows.", + x, Pokemon::to_string(up.stop), Pokemon::to_string(down.stop)); + } + } + const StoryWorld& maps = story_maps(); + const std::vector area = story_after_training_area(); + const auto in_grass = [&](const TilePos& pos){ + return maps.tile(pos) == TileKind::TALL_GRASS && std::find(area.begin(), area.end(), pos) != area.end(); + }; + // As walk_route() plans: the route costs, else the default costs. + // From the whole area the catch's save may stand in, and from the + // pocket behind the trees alone, where r252 saved. + const std::vector>> starts{ + {"the catch's save area", story_catch_save_area()}, + {"the pocket behind the trees", {maps.to_world(MAP_ROUTE_202, {21, 29})}}, + }; + for (const auto& [name, from] : starts){ + std::optional> legs = plan_robust_route(maps, from, in_grass, route_plan_costs()); + if (!legs){ + legs = plan_robust_route(maps, from, in_grass, RobustPlanCosts()); + } + if (!legs || legs->empty()){ + return "From " + name + " to the catch grass: no plan."; + } + for (const TilePos& pos : legs->back().after){ + if (!in_grass(pos)){ + return "From " + name + " to the catch grass: the walk may end on " + describe(maps, pos) + ", not in the grass."; + } + } + logger.log(std::format("From {} to the catch grass: {} legs, ending in the grass.", name, legs->size())); + } + return true; +} + +// The table's step `name` in row `row`. +const StoryStep& table_step(std::string_view row, std::string_view name){ + for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + for (const StoryStep& step : checkpoint.steps){ + if (checkpoint.id == row && step.name == name){ + return step; + } + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No step " + std::string(name) + " in " + std::string(row)); +} + +// 00.04b's way to Sandgem's Pokemon Center from the road's last row, +// wherever on it the run in stopped (lab-B 2026-11-09 20:09: two columns +// short): west to the row's wall and south onto the Center's sign, the +// table's walls, then its taps round the sign to below the door, each +// open, the door straight north of where they end. +UnitTestResult test_sandgem_center_approach(Logger& logger){ + const StoryWorld town = alone("SandgemTown"); + const TilePos sign = map_point("SandgemTown", "pokemon_center_sign"); + const TilePos door = map_point("SandgemTown", "pokemon_center_door"); + const TilePos entry = map_point("SandgemTown", "route_202_entry_stop"); + if (town.tile(sign) != TileKind::SIGN || town.tile(door) != TileKind::DOOR){ + return std::format("The Center's sign {} and door {} are not a sign and a door.", + Pokemon::to_string(sign), Pokemon::to_string(door)); + } + const StoryStep& walls = table_step("00.04b", "above_center_sign"); + const StoryStep& taps = table_step("00.04b", "below_center_door"); + const auto& walls_legs = Pokemon::story_arg>(walls.args, "legs"); + const auto& taps_legs = Pokemon::story_arg>(taps.args, "legs"); + const TilePos above_sign = sign.next(Direction::NORTH); + for (TilePos start = entry; standable(town, start); start = start.next(Direction::WEST)){ + TilePos pos = start; + for (const Pokemon::StoryLeg& leg : walls_legs){ + pos = slide_to_wall(town, pos, leg.direction).stop; + } + if (!(pos == above_sign)){ + return std::format("From {} the walls end at {}, not above the sign {}.", + Pokemon::to_string(start), Pokemon::to_string(pos), Pokemon::to_string(sign)); + } + } + TilePos pos = above_sign; + for (const Pokemon::StoryLeg& leg : taps_legs){ + for (int tap = 0; tap < leg.tiles; tap++){ + const std::optional next = step(town, pos, leg.direction); + if (!next){ + return std::format("{} from {} is blocked.", Pokemon::direction_name(leg.direction), Pokemon::to_string(pos)); + } + pos = *next; + } + } + if (!(pos == door.next(Direction::SOUTH))){ + return std::format("The taps end at {}, not below the door {}.", Pokemon::to_string(pos), Pokemon::to_string(door)); + } + logger.log(std::format("Sandgem: from the road's last row by walls to {}, {} tap legs to below the door {}.", + Pokemon::to_string(above_sign), taps_legs.size(), Pokemon::to_string(door))); + return true; +} + +// After training, 00.04 walks from wherever the training left us into +// Sandgem, to where the road stops above the Mart, with the default costs +// (heal_at_sandgem_center()): from the whole area and from each of its +// tiles alone, a plan must exist and end there. +UnitTestResult test_training_to_sandgem(Logger& logger){ + const StoryWorld& maps = story_maps(); + const TilePos entry = maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "route_202_entry_stop")); + const std::vector area = story_after_training_area(); + std::vector> starts{area}; + for (const TilePos& pos : area){ + starts.push_back({pos}); + } + for (const std::vector& from : starts){ + const auto legs = plan_robust_route(maps, from, [&](const TilePos& pos){ return pos == entry; }, RobustPlanCosts()); + const std::string name = from.size() == 1 ? describe(maps, from[0]) : "the whole training area"; + if (!legs || legs->empty()){ + return "From " + name + " into Sandgem: no plan."; + } + if (legs->back().after.size() != 1 || !(legs->back().after[0] == entry)){ + return "From " + name + " into Sandgem: the plan does not end on " + describe(maps, entry) + "."; + } + logger.log(std::format("From {} into Sandgem: {} legs.", name, legs->size())); + } + return true; +} + +// The Oreburgh Gym's inside: the column up from the entrance runs to Roark +// with nothing in the way but the two Youngsters' sight lines, which is +// what 01.04's "north until a trainer stops us" relies on. +UnitTestResult test_oreburgh_gym_column(Logger& logger){ + const StoryWorld gym = alone("OreburghGym"); + const TilePos entrance = map_point("OreburghGym", "entrance"); + const TilePos roark = map_point("OreburghGym", "roark"); + if (!standable(gym, entrance) || gym.tile(entrance.next(Direction::SOUTH)) != TileKind::DOOR){ + return std::string("The entrance tile is not standable above the door."); + } + if (!gym.occupied(roark)){ + return std::string("Nobody stands on Roark's tile."); + } + const WallHit up = slide_to_wall(gym, entrance, Direction::NORTH); + if (!(up.wall == roark)){ + return "North from the entrance stops at " + Pokemon::to_string(up.stop) + ", not in front of Roark."; + } + logger.log(std::format("Oreburgh Gym: north from the entrance walks {} tiles to Roark.", up.tiles)); + for (const TrainerSight& trainer : oreburgh_gym_trainer_sights()){ + if (!gym.occupied(trainer.pos)){ + return trainer.name + " does not stand on their tile."; + } + const std::vector seen = sight_tiles(gym, trainer); + const bool sees_column = std::ranges::any_of(seen, [&](const TilePos& pos){ + return pos.x == entrance.x && pos.y < entrance.y && pos.y > roark.y; + }); + if (!sees_column){ + return trainer.name + " does not see the column up to Roark."; + } + } + return true; +} + +// Oreburgh Mine B1F (the entrance hall): +// The hallway connects Oreburgh City above with the Coal Room below. +// Row 1 contains warp doors (11..13, 1) leading to the city. +// Row 21 contains warp doors (11..13, 21) leading to the coal room. +// Column 12 is unobstructed south from landing (12, 1) to warp door (12, 21). +// Column 11 is unobstructed north from landing (11, 21) to warp door (11, 1). +UnitTestResult test_oreburgh_mine_hall(Logger& logger){ + const StoryWorld hall = alone("OreburghMineB1F"); + for (int x = 11; x <= 13; x++){ + if (hall.tile({x, 1}) != TileKind::DOOR){ + return std::format("OreburghMineB1F ({}, 1) is not a DOOR tile (exit to city).", x); + } + if (hall.tile({x, 21}) != TileKind::DOOR){ + return std::format("OreburghMineB1F ({}, 21) is not a DOOR tile (entrance to coal room).", x); + } + } + for (int y = 2; y <= 20; y++){ + if (!standable(hall, {12, y})){ + return std::format("OreburghMineB1F column 12 row {} is blocked; expected clear path to coal room.", y); + } + if (!standable(hall, {11, y})){ + return std::format("OreburghMineB1F column 11 row {} is blocked; expected clear path to city exit.", y); + } + } + logger.log("OreburghMineB1F: columns 11 and 12 are clear between row 1 and row 21 warp doors."); + return true; +} + +// 01.04: the D-pad taps west from below Oreburgh's Center door end below +// the Gym's door, where the step north goes in. Red with 20 taps: they ended +// on (27, 21), a tile east of the door (lab-B, 2026-11-08 14:51). +UnitTestResult test_oreburgh_center_to_gym_taps(Logger& logger){ + const RouteRecords::RouteRecord* record = nullptr; + for (const RouteRecords::RouteRecord& item : RouteRecords::all()){ + if (item.checkpoint == "01.04" && item.map_file == "OreburghCity"){ + record = &item; + } + } + if (record == nullptr || !record->start || record->legs.empty()){ + return "No 01.04 record on Oreburgh City with a start tile and legs."; + } + const StoryWorld city = alone("OreburghCity"); + const RouteRecords::RouteLeg& west = record->legs.front(); + const TilePos end = trace_legs(city, *record->start, {{west.direction, west.tiles, false}}).back(); + const TilePos doorstep = map_point("OreburghCity", "gym_doorstep"); + if (end != doorstep){ + return std::format("{} taps west end on ({}, {}), not below the Gym's door ({}, {}).", west.tiles, end.x, end.y, doorstep.x, doorstep.y); + } + logger.log(std::format("Oreburgh: {} taps west from below the Center's door reach the Gym's doorstep.", west.tiles)); + return true; +} + +// Each traceable record with a start tile starts on ground, and its legs +// counted in whole tiles trace on its map up to the first leg that ends +// some other way. +UnitTestResult test_route_records(Logger& logger){ + for (const RouteRecords::RouteRecord& record : RouteRecords::all()){ + const std::string name = record.checkpoint + " " + record.title; + const StoryWorld world = alone(record.map_file); + if (!record.traceable || !record.start){ + continue; + } + if (!standable(world, *record.start)){ + return name + ": starts on " + Pokemon::to_string(*record.start) + ", where nobody can stand."; + } + std::vector counted; + for (const RouteRecords::RouteLeg& leg : record.legs){ + if (leg.end != RouteRecords::LegEnd::COUNT || !leg.exact){ + break; + } + counted.push_back({leg.direction, leg.tiles, false}); + } + try{ + const TilePos end = trace_legs(world, *record.start, counted).back(); + logger.log(std::format("{}: {} counted legs to {}.", name, counted.size(), describe(world, end))); + }catch (InternalProgramError& e){ + return name + ": " + e.message(); + } + } + 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 - " + 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 = {"Route 202: the catch patch and the walk to it", test_route202_catch_grass}, + .expectation = {}, + }, + { + .input = {"Sandgem Town: from the road's last row by walls to below the Center's door", test_sandgem_center_approach}, + .expectation = {}, + }, + { + .input = {"Route 202: from every tile of the training area into Sandgem", test_training_to_sandgem}, + .expectation = {}, + }, + { + .input = {"Oreburgh Gym: the column up to Roark", test_oreburgh_gym_column}, + .expectation = {}, + }, + { + .input = {"Oreburgh Mine B1F: the entrance hall corridor and warps", test_oreburgh_mine_hall}, + .expectation = {}, + }, + { + .input = {"Oreburgh: 01.04's taps from the Center's door to the Gym's", test_oreburgh_center_to_gym_taps}, + .expectation = {}, + }, + { + .input = {"Route records: counted legs trace on their maps", test_route_records}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_StoryMaps(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_StoryMaps_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h new file mode 100644 index 0000000000..c0ed36bd5b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h @@ -0,0 +1,24 @@ +/* BDSP AutoStory Story Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryMaps_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryMaps_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Offline checks of the story's walks on its maps: Route 202, Sandgem, the Oreburgh Gym and the route records. +void add_tests_AutoStory_StoryMaps(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp new file mode 100644 index 0000000000..4531c88fb4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp @@ -0,0 +1,313 @@ +/* BDSP AutoStory - Story places + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// The JSON maps' names and the location banners'. Both halves of Route +// 204 show "Route 204"; no world holds both. +const std::map& banner_names(){ + static const std::map names{ + {"TwinleafTown", "Twinleaf Town"}, + {"Route201", MAP_ROUTE_201}, + {"VerityLakefront", "Verity Lakefront"}, + {"SandgemTown", MAP_SANDGEM_TOWN}, + {"Route202", MAP_ROUTE_202}, + {"JubilifeCity", MAP_JUBILIFE_CITY}, + {"Route203", MAP_ROUTE_203}, + {"OreburghCity", MAP_OREBURGH_CITY}, + {"Route204South", "Route 204"}, + {"Route204North", "Route 204"}, + {"FloaromaTown", MAP_FLOAROMA_TOWN}, + }; + return names; +} + +const std::string& banner_name(const std::string& map){ + const auto& names = banner_names(); + if (auto iter = names.find(map); iter != names.end()){ + return iter->second; + } + return map; +} + +// `maps` (JSON names) in one world, each with its tile (0,0) at the corner +// of its decomp rectangle (devtools/maps/bdsp_maps.py), so maps that meet +// in the game meet here. +StoryWorld joined(const std::vector>& maps){ + StoryWorld world; + for (const auto& [map, corner] : maps){ + world.add(banner_name(std::string(map)), story_map(std::string(map)), corner); + } + return world; +} + +// TODO(#114 multi-map view): MapData has no joined view of maps yet. +// Twinleaf to Route 202 in one world, at the decomp's world tiles (its +// cells are 32 tiles square; Route 201's map starts 7 rows into its cell +// and Route 202's a column west of its own). +const StoryWorld& south_world(){ + static const StoryWorld world = joined({ + {"TwinleafTown", {96, 864}}, + {"Route201", {96, 839}}, + {"VerityLakefront", {64, 832}}, + {"SandgemTown", {160, 832}}, + {"Route202", {159, 800}}, + }); + return world; +} + +// Jubilife without its east column: Route 203's map starts on that column +// (world x 191, its column 0), and two maps may not both hold open ground. +// Jubilife's tiles keep their coordinates. +Pokemon::MapData jubilife_west_of_route_203(){ + const Pokemon::MapData& city = story_map("JubilifeCity"); + Pokemon::MapData cut; + cut.name = city.name; + cut.source = city.source; + cut.tiles = Pokemon::Grid{city.tiles.width - 1, city.tiles.height, {}}; + for (int y = 0; y < cut.tiles.height; y++){ + for (int x = 0; x < cut.tiles.width; x++){ + cut.tiles.cells.push_back(city.tiles.at({x, y})); + } + } + for (const Pokemon::MapObject& object : city.objects){ + if (cut.tiles.contains({object.x, object.y})){ + cut.objects.push_back(object); + } + } + return cut; +} + +// 01.06: Route 203 west into Jubilife (decomp rectangles (191, 740) and +// (128, 736)): Route 203's rows 17..20 are Jubilife's street, rows 21..24. +const StoryWorld& route_203_world(){ + static const StoryWorld world = []{ + StoryWorld world = joined({{"Route203", {191, 740}}}); + world.add(MAP_JUBILIFE_CITY, jubilife_west_of_route_203(), {128, 736}); + return world; + }(); + return world; +} + +// 01.08: Jubilife north onto Route 204's south half (160, 704), whose street +// (12..15, 31) carries on Jubilife's (44..47, 0). +const StoryWorld& route_204_south_world(){ + static const StoryWorld world = joined({{"JubilifeCity", {128, 736}}, {"Route204South", {160, 704}}}); + return world; +} + +// 01.10: Route 204's north half (160, 672) up into Floaroma (160, 640). +const StoryWorld& route_204_north_world(){ + static const StoryWorld world = joined({{"Route204North", {160, 672}}, {"FloaromaTown", {160, 640}}}); + return world; +} + +// The Ravaged Path with the rock 01.09 smashes, (22, 43), open: no route +// before the smash can use it, since it only leads east, where the rocks +// shut everything else off. +Pokemon::MapData ravaged_path_smashed(){ + Pokemon::MapData map = story_map("RavagedPath"); + Pokemon::cell_at(map_point("RavagedPath", "rocks.13"), map.tiles) = Pokemon::TileKind::OPEN; + return map; +} + +// The interiors (houses, the lab, the Gym, the Gate, the mine's two floors, +// the Ravaged Path), each its own world, named by its JSON map. +const std::map& room_worlds(){ + static const std::map rooms = []{ + std::map ret; + for (std::string_view room : {"PlayerHouse2F", "PlayerHouse1F", "RivalHouse1F", "RivalHouse2F", "RowansLab", "OreburghGym", "OreburghGate", "OreburghMineB1F", "OreburghMine"}){ + ret[std::string(room)].add(std::string(room), story_map(std::string(room)), {0, 0}); + } + ret["RavagedPath"].add("RavagedPath", ravaged_path_smashed(), {0, 0}); + return ret; + }(); + return rooms; +} + +// A world and the JSON maps it holds. +struct PlanWorld{ + const StoryWorld* world; + std::set maps; +}; + +// In order: the first that holds both ends of a walk plans it. Jubilife +// alone comes before the worlds that join it to a route, so 00.05's walks +// plan on the city alone. +const std::vector& plan_worlds(){ + static const std::vector worlds = []{ + std::vector ret{ + {&south_world(), {"TwinleafTown", "Route201", "VerityLakefront", "SandgemTown", "Route202"}}, + {&jubilife_maps(), {"JubilifeCity"}}, + {&oreburgh_city_maps(), {"OreburghCity"}}, + {&route_203_world(), {"Route203", "JubilifeCity"}}, + {&route_204_south_world(), {"JubilifeCity", "Route204South"}}, + {&route_204_north_world(), {"Route204North", "FloaromaTown"}}, + }; + for (const auto& [name, world] : room_worlds()){ + ret.push_back({&world, {name}}); + } + return ret; + }(); + return worlds; +} + +// Route 203's trainers, from the Diamond decomp's events (zone event +// narc_0329): where each stands on the JSON map, the way they look and +// how far. The one by the Gate wanders in BDSP and is off the map. +std::vector route_203_trainer_sights(){ + const auto at = [](std::string_view name){ + for (const Pokemon::MapObject& object : story_map("Route203").objects){ + if (object.name == name){ + return TilePos{object.x, object.y}; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string("Route 203 has no ") + std::string(name) + "."); + }; + return { + {.name = "Youngster (west)", .pos = at("youngster_1"), .facing = Direction::WEST, .range = 2}, + {.name = "Lass (above row 18)", .pos = at("lass_2"), .facing = Direction::WEST, .range = 5}, + {.name = "Youngster (above row 18)", .pos = at("youngster_3"), .facing = Direction::SOUTH, .range = 4}, + {.name = "Lass (by the Gate)", .pos = at("lass_4"), .facing = Direction::SOUTH, .range = 5}, + }; +} + +std::vector oreburgh_gate_trainer_sights(){ + const auto at = [](std::string_view name){ + for (const Pokemon::MapObject& object : story_map("OreburghGate").objects){ + if (object.name == name){ + return TilePos{object.x, object.y}; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string("Oreburgh Gate has no ") + std::string(name) + "."); + }; + return { + {.name = "Camper Curtis", .pos = at("camper_1"), .facing = Direction::NORTH, .range = 4}, + {.name = "Picnicker Diana", .pos = at("picnicker_0"), .facing = Direction::SOUTH, .range = 2}, + }; +} + +// The trainers of the JSON map `map`, in its own tiles. +std::vector map_trainers(const std::string& map){ + if (map == "Route202"){ + return route202_trainer_sights(); + } + if (map == "Route203"){ + return route_203_trainer_sights(); + } + if (map == "Route204South"){ + return route204_south_trainer_sights(); + } + if (map == "Route204North"){ + return route204_north_trainer_sights(); + } + return {}; +} + +} // namespace + + +const std::string& story_banner_name(const std::string& map){ + return banner_name(map); +} + +std::string story_arrival_banner(const std::string& from, const std::string& to){ + // The named places, by the place each map is part of: the mine's two floors are one. + static const std::map caves{ + {"OreburghGate", "OreburghGate"}, + {"OreburghMine", "OreburghMine"}, + {"OreburghMineB1F", "OreburghMine"}, + {"RavagedPath", "RavagedPath"}, + }; + const auto place = [](const std::string& map) -> std::string{ + if (banner_names().contains(map)){ + return banner_name(map); + } + const auto cave = caves.find(map); + return cave == caves.end() ? "" : cave->second; + }; + const std::string there = place(to); + return there.empty() || there == place(from) ? "" : story_banner_name(to); +} + +const StoryWorld* story_plan_world(const std::string& from, const std::string& to){ + for (const PlanWorld& world : plan_worlds()){ + if (world.maps.contains(from) && world.maps.contains(to)){ + return world.world; + } + } + return nullptr; +} + +TilePos story_to_world(const StoryWorld& world, const std::string& map, const TilePos& local){ + return world.to_world(banner_name(map), local); +} + +std::optional story_to_local(const StoryWorld& world, const std::string& map, const TilePos& pos){ + const std::optional tile = world.to_local(pos); + if (!tile || tile->map != banner_name(map)){ + return std::nullopt; + } + return tile->pos; +} + +std::optional story_to_local(const StoryWorld& world, const TilePos& pos){ + const std::optional tile = world.to_local(pos); + if (!tile){ + return std::nullopt; + } + for (const PlanWorld& plan : plan_worlds()){ + if (plan.world != &world){ + continue; + } + for (const std::string& map : plan.maps){ + if (banner_name(map) == tile->map){ + return MapTile{map, tile->pos}; + } + } + } + return std::nullopt; +} + +std::vector story_trainers(const StoryWorld& world){ + std::vector sights; + for (const PlanWorld& plan : plan_worlds()){ + if (plan.world != &world){ + continue; + } + for (const std::string& map : plan.maps){ + for (TrainerSight sight : map_trainers(map)){ + sight.pos = story_to_world(world, map, sight.pos); + sights.push_back(std::move(sight)); + } + } + } + return sights; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h new file mode 100644 index 0000000000..0e84a18163 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h @@ -0,0 +1,60 @@ +/* BDSP AutoStory - Story places + * + * From: https://github.com/PokemonAutomation/ + * + * Where the story's steps walk: poses are tiles of the JSON maps + * ("Route202"), named by map_point() and map_region(). A walk from one map + * to another is planned on a story world that holds both: the outdoors + * from Twinleaf to Route 202, Jubilife, Oreburgh, Route 203 into Jubilife, + * Jubilife up Route 204's south half, Route 204's north half into + * Floaroma, and each interior on its own. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryPlaces_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryPlaces_H + +#include +#include +#include +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The story world a walk from `from` to `to` is planned on, or nothing if +// no world holds both. Its maps are named as the game's location banners +// name them ("Route 202"), which the walker reads. +const StoryWorld* story_plan_world(const std::string& from, const std::string& to); + +// The location banner of the JSON map `map` ("Route 204" for both of its +// halves), or `map` itself for one no banner list names. +const std::string& story_banner_name(const std::string& map); +// The banner (story_banner_name()) a warp from the JSON map `from` onto +// `to` shows, or "" if none: arriving in another town, route or cave shows +// its name (out of the Gym, Oreburgh City's), arriving in a house, a lab, +// a school or another floor of the same cave shows none. +std::string story_arrival_banner(const std::string& from, const std::string& to); + +// The tile `local` of the map `map` in `world`. +Pokemon::TilePos story_to_world(const StoryWorld& world, const std::string& map, const Pokemon::TilePos& local); + +// The world tile `pos` as a tile of `map`, or nothing if it is not on `map`. +std::optional story_to_local(const StoryWorld& world, const std::string& map, const Pokemon::TilePos& pos); +// The world tile `pos` as a tile of whichever JSON map of `world` holds +// it, or nothing if none does. +std::optional story_to_local(const StoryWorld& world, const Pokemon::TilePos& pos); + +// The trainers a walk on `world` may meet (Routes 202 to 204's), in its tiles. +std::vector story_trainers(const StoryWorld& world); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp new file mode 100644 index 0000000000..109cff58db --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp @@ -0,0 +1,323 @@ +/* BDSP AutoStory - Story step IO + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/PrettyPrint.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "PokemonBDSP/Inference/PokemonBDSP_TransitionWatchers.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LocationBannerWatcher.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "Actions/PokemonBDSP_AutoStory_TalkOver.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_Walks.h" +#include "Actions/PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +namespace{ + +DpadPosition dpad_toward(Direction direction){ + return + direction == Direction::NORTH ? DPAD_UP : + direction == Direction::SOUTH ? DPAD_DOWN : + direction == Direction::WEST ? DPAD_LEFT : DPAD_RIGHT; +} + +// A door run's fade watch (DOOR_FADE_RULE) that fires only from `from` on. +// It keeps a small copy of each frame for the scene comparison, and the +// fade it saw. +class DoorFadeFrom : public VisualInferenceCallback{ +public: + explicit DoorFadeFrom(WallClock from) + : VisualInferenceCallback("DoorFadeFrom") + , m_watch(DOOR_FADE_RULE) + , m_from(from) + {} + virtual void make_overlays(VideoOverlaySet& items) const override{ + items.add(COLOR_RED, {0.1, 0.1, 0.8, 0.8}); + } + virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override{ + const Pokemon::TransitionSample sample = Pokemon::transition_sample(VideoSnapshot(frame.scale_to(192, 108), timestamp)); + m_fade = m_watch.push(sample); + return m_fade != WarpFade::NONE && timestamp >= m_from; + } + WarpFade fade() const{ + return m_fade; + } + const WarpFadeWatch& watch() const{ + return m_watch; + } + +private: + WarpFadeWatch m_watch; + WallClock m_from; + WarpFade m_fade = WarpFade::NONE; +}; + +// How one hold toward a door ended: run_until()'s index (0 the fade, 1 a +// line, 2 a wild battle, 3 the far map's banner; -1 time up), the fade +// and how it showed, when the screen last went dark, and the banner read. +struct DoorHoldEnd{ + int ret; + WarpFade fade; + std::string how; + WallClock went_dark; + std::string banner; +}; + +DoorHoldEnd hold_into_door(VideoStream& stream, ProControllerContext& context, DpadPosition dpad, bool run, Milliseconds hold, Milliseconds dark_from, const std::string& banner){ + DoorFadeFrom fade(current_time() + dark_from); + ShortDialogWatcher dialog(COLOR_YELLOW); + BattleMenuWatcher battle(BattleType::STANDARD, COLOR_YELLOW); + LocationBannerWatcher far_banner(banner); + std::vector watchers{{fade}, {dialog}, {battle}}; + if (!banner.empty()){ + watchers.emplace_back(far_banner); + } + const int ret = run_until(stream, context, [&](ProControllerContext& context){ + if (run){ + ssf_press_button(context, BUTTON_B, 0ms, hold, 0ms); + } + pbf_press_dpad(context, dpad, hold, 0ms); + }, watchers); + return { + .ret = ret, + .fade = ret == 0 ? fade.fade() : WarpFade::NONE, + .how = fade.watch().how(), + .went_dark = fade.watch().went_dark().value_or(WallClock::max()), + .banner = ret == 3 ? far_banner.seen() : "", + }; +} + +// Wild battles a run into a door flees and runs on after. +constexpr size_t DOOR_RUN_BATTLES = 3; + +// Holds the D-pad toward a door (with B after the shoes) for `timeout` +// until its fade (DOOR_FADE_RULE) or a line shows. Returns what it saw. +// A fade whose hold fell between two frames shows at the scene after it, +// so a missed 50 ms fade no longer runs the whole timeout. +// A line stops it: holding on under Mom's "Oh, Kosmos!" wasted 12 s (r45). +// Dark in the first `dark_from` of running is not the door (lab-B +// 2026-11-08: dark at 1.8 s, halfway down the mine's entrance hall). A wild +// battle on the way is fled, and the run goes on for the time it had left: +// only the running counts, up to the battle's fade. +// The far map's banner `banner` shows after the fade, so it ends the run +// only when the fade went unseen (#268 slice 1). +WarpSeen run_into_door(VideoStream& stream, ProControllerContext& context, Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from, const std::string& banner){ + Milliseconds left = timeout; + for (size_t battles = 0;; battles++){ + const WallClock start = current_time(); + const DoorHoldEnd end = hold_into_door(stream, context, dpad_toward(direction), run, left, dark_from, banner); + if (end.ret == 2){ + const WallClock stopped = std::min(end.went_dark, current_time()); + const Milliseconds ran = std::chrono::duration_cast(stopped - start) - ROUTE_START_DELAY; + const Milliseconds counted = std::clamp(ran, 0ms, left); + left -= counted; + dark_from -= std::min(dark_from, counted); + stream.log(std::format("[Story] run_into_door: wild battle after {} ms of running; fleeing.", counted.count())); + run_from_wild_battle(stream, context); + if (battles < DOOR_RUN_BATTLES && left > 0ms){ + continue; + } + return {}; + } + if (end.ret == 0){ + stream.log(std::format("[Story] run_into_door: {} detected ({}).", warp_fade_name(end.fade), end.how)); + }else if (end.ret == 1){ + stream.log("[Story] run_into_door: dialog detected."); + }else if (end.ret == 3){ + stream.log("[Story] run_into_door: no fade seen, but the banner \"" + end.banner + "\" read."); + }else if (end.went_dark != WallClock::max()){ + // Dark only before dark_from: early, or the door's fade itself. + stream.log(std::format("[Story] run_into_door: timed out; dark {} ms in, before dark_from {} ms.", + std::chrono::duration_cast(end.went_dark - start).count(), dark_from.count())); + }else{ + stream.log("[Story] run_into_door: timed out without detecting fade."); + } + return {.fade = end.fade, .banner = end.banner}; + } +} + +// What the screen showed after a tap toward a door or a cave mouth. +enum class TapLook{ + NOTHING, + DARK, + BANNER, +}; + +// The mean of r, g and b over the screen, 0 to 255; 255 with no frame. +double brightness_of(const VideoSnapshot& screen){ + return screen ? image_average(*screen.frame).sum() / 3 : 255; +} + +void keep_banner_frame(const ImageViewRGB32& frame, const std::string& folder, const std::string& banner){ + Filesystem::create_directories(Filesystem::Path(folder)); + frame.save(folder + now_to_filestring() + "-" + normalize_location_name(banner) + ".png"); +} +void log_new_banner_read(VideoStream& stream, const std::string& read, std::string& last_read, const std::string& banner, WallClock start){ + if (read.empty() || read == last_read){ + return; + } + stream.log(std::format("[Banner] read \"{}\" after {} ms, looking for \"{}\".", + read, std::chrono::duration_cast(current_time() - start).count(), banner)); + last_read = read; +} + +// Looks for the location banner `banner` for up to `wait`, and with +// `fade` for the fade, each frame to `fade`'s watch (TAP_FADE_RULE), which +// follows the taps from before the first. Logs each distinct banner read +// and how the wait ended: the Oreburgh Gate's entry missed its banner twice +// (lab-B, 2026-11-08 15:31 and 15:39) though a hand probe saw it 1.5 s +// after the fade, and nothing said why. +TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, WarpFadeWatch* fade){ + const WallClock start = current_time(); + const WallClock deadline = start + wait; + std::string last_read; + LocationBannerReads reads; + double darkest = 255; + // Every 500 ms of a long wait (the one after a fade) a frame goes to + // Screenshots/AutoStory/banner/, to see what the game showed. + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/banner/"; + WallClock next_frame = start; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (!fade && screen && current_time() >= next_frame){ + keep_banner_frame(*screen.frame, folder, banner); + next_frame += 500ms; + } + const std::string read = screen ? reads.read(*screen.frame, current_time()) : ""; + log_new_banner_read(stream, read, last_read, banner, start); + if (screen && location_banner_matches(read, banner)){ + return TapLook::BANNER; + } + darkest = std::min(darkest, brightness_of(screen)); + if (fade != nullptr && screen && fade->push(Pokemon::transition_sample(screen)) != WarpFade::NONE){ + return TapLook::DARK; + } + if (current_time() >= deadline){ + stream.log(std::format("[Banner] no \"{}\" in {} ms; darkest frame {:.0f}, last read \"{}\".", + banner, wait.count(), darkest, last_read)); + return TapLook::NOTHING; + } + context.wait_for(100ms); + } +} + +} // namespace + + +ConsoleStoryStepIO::ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context) + : m_stream(stream) + , m_context(context) + , m_escape(run_from_wild_battle) + , m_route(stream, context, m_escape) +{} + +bool ConsoleStoryStepIO::fight(const std::string& trainer){ + if (trainer.find("Twins") != std::string::npos){ + talk_or_fight(m_stream, m_context, trainer, TalkExpect::BATTLE, BattleFormat::DOUBLES); + return true; + } + return fight_route_trainer(m_stream, m_context, trainer); +} + +void ConsoleStoryStepIO::walk_until_stopped(Direction direction, size_t tiles, const std::string& label){ + PokemonBDSP::walk_until_stopped(m_stream, m_context, direction, tiles, label); +} + +bool ConsoleStoryStepIO::run_until_map(Direction direction, Milliseconds limit, const std::string& map, const std::string& label){ + const auto [x, y] = dpad_of(direction); + const WalkResult walk = walk_until_dpad(m_stream, m_context, run_from_wild_battle, {x, y}, limit, label, { + .arrived = [&](const ImageViewRGB32& screen){ return location_banner_matches(read_location_banner(screen), map); }, + .pace = DpadPace::RUN, + }); + m_stream.log(std::format("[Story] {}: ran {} to {}'s banner: {}.", label, direction_name(direction), map, walk_stop_name(walk.stop))); + return walk.stop == WalkStop::ARRIVED; +} + +WarpSeen ConsoleStoryStepIO::run_into_door(Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from, const std::string& banner){ + return PokemonBDSP::run_into_door(m_stream, m_context, direction, run, timeout, dark_from, banner); +} + +// A held walk can carry on past a fade that goes unseen: out of the Gym it +// walked on south (r219g), and through the Oreburgh Gate's west mouth on +// west into a pocket by Route 203's pond (r75). Taps during the fade are +// eaten, so a fade ends the taps; the banner after it is evidence, not +// required (#268 slice 1). The watch starts before the first tap: a walk +// before that slid onto the warp unseen may have left its banner up +// (lab-B 2026-11-08 15:31). +std::optional ConsoleStoryStepIO::tap_to_banner(Direction direction, const std::string& map, size_t max_taps){ + const std::string& banner = story_banner_name(map); + m_context.wait_for_all_requests(); + WarpFadeWatch watch(TAP_FADE_RULE); + if (look_after_tap(m_stream, m_context, banner, 0ms, &watch) == TapLook::BANNER){ + m_stream.log("[Banner] \"" + banner + "\" shows before the first tap: through already."); + return 0; + } + for (size_t tap = 1; tap <= max_taps; tap++){ + pbf_press_dpad(m_context, dpad_toward(direction), 160ms, 0ms); + const TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, &watch); + if (look == TapLook::DARK){ + m_stream.log("[Banner] the fade: " + watch.how() + "."); + } + if (look == TapLook::DARK && look_after_tap(m_stream, m_context, banner, 8000ms, nullptr) != TapLook::BANNER){ + m_stream.log("[Banner] a fade, no \"" + banner + "\" banner after it: counting the fade.", COLOR_ORANGE); + } + if (look != TapLook::NOTHING){ + // No fixed wait: the step settles after it (talk_over()). + return tap; + } + } + return std::nullopt; +} + +// One rule for scenes and map changes (talk_over()): press through the +// text, and keep pressing B while the screen is quiet, until the overworld +// is quiet. A step or scene that ends on another map (AFTER_WARP) must fade on + +// the way, and only the quiet after the fade counts: r28 "went out" a tile +// beside the door and walked a whole route in the house. The far map's +// banner `banner`, if it has one, counts as the fade: a fade the run +// missed is over, but its banner comes after it (#268 slice 1). +void ConsoleStoryStepIO::settle(Button button, const std::string& label, SettleKind kind, const std::string& banner){ + const TalkOverRule rule{.warp = kind == SettleKind::AFTER_WARP, .door = kind == SettleKind::DOOR}; + talk_over(m_stream, m_context, label, {.next = button, .rule = rule, .banner = banner}); +} + + + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h new file mode 100644 index 0000000000..35d3d5feb9 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h @@ -0,0 +1,126 @@ +/* BDSP AutoStory - Story step IO + * + * From: https://github.com/PokemonAutomation/ + * + * What the story's walking steps (StoryWalkSteps) do on the console beyond + * the route walker's RouteIO: fights, walks until stopped, runs into doors + * and the settle after a scene. Tests run the steps on a simulation; + * ConsoleStoryStepIO is the console. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryStepIO_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryStepIO_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// How a door run takes a warp's fade (WarpFadeWatch), on the console and +// the simulation alike: the partial tier's dark (a doorway's dim fade +// frames) held 50 ms, white held 60 ms, as its black and white watchers +// did (lab-B to 2026-11-08), or a fade whose hold it did not see, at the +// other scene after the fall. +constexpr WarpFadeRule DOOR_FADE_RULE{.partial_dark = true, .dark_for = Milliseconds(50), .white_for = Milliseconds(60)}; +// How a tap step, looking every 100 ms, takes one: the uniform tier at +// once, as fade_of() did, or a fade whose hold it did not see. +constexpr WarpFadeRule TAP_FADE_RULE{}; + + +// How a settle (StoryStepIO::settle()) ends. +enum class SettleKind{ + // Text and scenes pressed through, then the overworld quiet. + TALK, + // As TALK, but a warp (a map change) must come first: a fade, or the + // far map's banner. + AFTER_WARP, + // After a door or a way through with no text to come: the fade back + // up and a short still spell around the player, people walking about + // ignored (TalkOverRule::door). + DOOR, +}; + + +// Abstract interface for story step input/output operations. +class StoryStepIO{ +public: + virtual ~StoryStepIO() = default; + + // The route walker's console: walks, presses, dialogue watches, the log. + virtual RouteIO& route() = 0; + // Fights a trainer whose dialogue has opened (fight_route_trainer()): + // Route 204's Twins as a pair (01.10), everyone else alone. + // Returns whether a battle followed. + virtual bool fight(const std::string& trainer) = 0; + // walk_until_stopped(): `direction` until something stops us, for at + // most `tiles` tiles' time. Fails on a door or a dialogue. + virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) = 0; + // Runs `direction` on the D-pad until the location banner of `map` (a + // JSON map's name) shows, for at most `limit`. Returns whether it showed. + virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) = 0; + // Holds the D-pad toward a door (running with `run`) for `timeout` until + // its fade (DOOR_FADE_RULE), `banner` (a map's banner name, "" for + // none) reads, or a line shows. A dark screen in the first `dark_from` + // of running is not the door's fade, and a wild battle on the way is + // fled, the run going on for the time left. Returns what it saw of the + // warp: the fade or the banner, either one alone (#268 slice 1). + virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), + Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") = 0; + // D-pad taps `direction` one at a time, each looked at, toward a door + // or cave mouth, until its fade (TAP_FADE_RULE: uniform, so a dim cave + // floor is none) or the location banner of `map` (a JSON map's name), for at + // most `max_taps`; after a fade, no more taps. Either one alone is the + // way through. The watch starts before the first tap: a banner up then + // is 0 taps. Returns the taps it took, or nothing if neither showed. + virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) = 0; + // Presses through any text until the overworld ends as `kind` says; + // for SettleKind::AFTER_WARP, the warp is a fade or the banner `banner` + // ("" for a fade only). Fails otherwise. + virtual void settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) = 0; +}; + + +// The console. +class ConsoleStoryStepIO : public StoryStepIO{ +public: + ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context); + + virtual RouteIO& route() override{ + return m_route; + } + virtual bool fight(const std::string& trainer) override; + virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; + virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) override; + virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), + Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") override; + virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) override; + virtual void settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) override; + + +private: + + VideoStream& m_stream; + ProControllerContext& m_context; + WildBattleEscape m_escape; + ConsoleRouteIO m_route; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp new file mode 100644 index 0000000000..80a864315c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp @@ -0,0 +1,102 @@ +/* BDSP AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + * The story from a new game to Floaroma, 00.00 to 01.11, as rows of the + * shared story steps: plain data loaded from the embedded authored plans. + * + */ + +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_StoryData.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// 00.02 is the Lake Verity starter and Starly battle checkpoint, which Run +// Starter RNG in AutoStory replaces with the game event STARTER_RNG_ACTION. +constexpr std::string_view STARTER_RNG_CHECKPOINT_ROW = "00.02"; +constexpr std::string_view STARTER_RNG_ACTION = "starter_rng"; + +// `row` with a save appended at its end, where the next checkpoint starts: +// in BDSP rows normally save at their start, so a run that stops after +// 00.01 for Starter RNG makes 00.02's Lakefront save before stopping. As +// every start save does, it checks that the save card names `saves_in` +// (the next row's), and stops the program if not. +StoryCheckpoint with_handoff_save(StoryCheckpoint row, const std::string& saves_in){ + Pokemon::StoryStep save; + save.kind = Pokemon::StoryStepKind::SAVE; + save.name = "save_for_starter_rng"; + save.action = saves_in; + save.why = "the save at the Verity Lakefront where Starter RNG starts"; + if (!row.steps.empty()){ + const Pokemon::StoryStep& last = row.steps.back(); + if (last.pose_after){ + save.pose_after = last.pose_after; + }else if (last.kind == Pokemon::StoryStepKind::ROUTE){ + const Pokemon::GridPose& target = Pokemon::story_arg(last.args, "target"); + save.pose_after = Pokemon::TileDeclared{ + .map = target.map, .tiles = target.tiles, .facing = target.facing + }; + } + } + row.steps.push_back(std::move(save)); + return row; +} + +// `row` (00.02) with its Lake Verity steps replaced by the in-program +// Starter RNG action, keeping 00.02's start save at VerityLakefront (17, 14) +// and its end pose at VerityLakefront (17, 14..15). +StoryCheckpoint with_starter_rng_step(StoryCheckpoint row){ + Pokemon::StoryStep step; + step.kind = Pokemon::StoryStepKind::GAME; + step.name = std::string(STARTER_RNG_ACTION); + step.action = std::string(STARTER_RNG_ACTION); + step.why = "Starter RNG at Lake Verity through the Starly battle and Rowan's scene"; + if (!row.steps.empty()){ + step.pose_after = row.steps.back().pose_after; + } + row.steps = {std::move(step)}; + return row; +} + +} // namespace + + +const std::vector& story_checkpoints(){ + static const std::vector checkpoints = load_authored_story(); + return checkpoints; +} + +StoryRunPlan plan_story_run( + const std::vector& checkpoints, size_t first, size_t last, const StoryHandoffs& handoffs +){ + StoryRunPlan plan; + for (size_t c = first; c <= last && c < checkpoints.size(); c++){ + const StoryCheckpoint& row = checkpoints[c]; + if (row.id == STARTER_RNG_HANDOFF_ROW && handoffs.starter_rng == Pokemon::RngHandoffMode::STOP){ + const std::string saves_in = c + 1 < checkpoints.size() ? checkpoints[c + 1].saves_in : std::string(); + plan.rows.push_back(with_handoff_save(row, saves_in)); + plan.stop = StoryHandoff::STARTER_RNG; + return plan; + } + if (row.id == STARTER_RNG_CHECKPOINT_ROW && handoffs.starter_rng == Pokemon::RngHandoffMode::RUN){ + plan.rows.push_back(with_starter_rng_step(row)); + plan.fatal_rows.insert(row.id); + continue; + } + plan.rows.push_back(row); + } + return plan; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h new file mode 100644 index 0000000000..39f744058c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h @@ -0,0 +1,83 @@ +/* BDSP AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + * The story from a new game to Floaroma, 00.00 to 01.11, as rows of the + * shared story steps (Pokemon/AutoStory/Pokemon_StorySteps.h): plain + * data, as FRLG's table is, so the tests plan its routes and run it + * against a fake game. Each row but 00.00 saves at its start, which is + * where the row before it ends, and only if the save screen's card names + * the row's map (saves_in). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryTable_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryTable_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryRngHandoff.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Checkpoints 00.00 to 01.11, in order: segment 00 (the intro to the +// Poketch) and segment 01 (Jubilife to Floaroma). +const std::vector& story_checkpoints(); + +// Mom gives the Running Shoes in this row's step: from then on, B held +// with the D-pad runs (r24, r27). +inline constexpr std::string_view RUNNING_SHOES_ROW = "00.03"; +// Story step name in which Mom gives the Running Shoes. +inline constexpr std::string_view RUNNING_SHOES_STEP = "rowan_and_dawn"; + +// 00.01 ends at the Verity Lakefront (17, 14) facing north, where 00.02 and +// Starter RNG (Start point: Verity Lakefront) start. +inline constexpr std::string_view STARTER_RNG_HANDOFF_ROW = "00.01"; + +// An upstream RNG program the user may run part-way through the story. +enum class StoryHandoff{ + NONE, + // The run stops after 00.01 saves at the Verity Lakefront (17, 14), + // facing Lake Verity's entrance, where Starter RNG starts. + STARTER_RNG, +}; + +// The handoffs a run stops for or runs in-program (AutoStory's options). +struct StoryHandoffs{ + // STOP: the run stops after 00.01 for the user's Starter RNG. RUN: 00.02 + // runs it in the program. The mode of the options: Pokemon::rng_handoff_mode(). + Pokemon::RngHandoffMode starter_rng = Pokemon::RngHandoffMode::OFF; +}; + +// What a run from table row `first` to `last` does. +struct StoryRunPlan{ + // The checkpoints to run, in order. Copies, not pointers: a row the plan + // changes (the handoff save) must not change the table, and the run's + // steps capture these rows, which outlive the call. + std::vector rows; + // NONE: the run ends after `rows`. STARTER_RNG: the run stops after + // `rows`, the last of which (00.01) saves at the Verity Lakefront. + StoryHandoff stop = StoryHandoff::NONE; + // Rows whose failure ends the program though they save at their start: + // a failed 00.02 with Run Starter RNG, whose reset loop is the retry. + std::set fatal_rows; +}; + +// The rows of `table` from `first` to `last`, both inclusive, as a +// run with `handoffs` takes them: a run that reaches 00.01 with +// `handoffs.starter_rng` STOP saves at the Verity Lakefront at the end of +// 00.01 and stops there for Starter RNG. +StoryRunPlan plan_story_run(const std::vector& table, size_t first, size_t last, const StoryHandoffs& handoffs = {}); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp new file mode 100644 index 0000000000..c15cbd44ae --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp @@ -0,0 +1,1072 @@ +/* BDSP AutoStory - Story table Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_TableRoutes.h" +#include "PokemonBDSP_AutoStory.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryTable_Tests.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; +using Pokemon::story_arg; + +// The actions each kind does itself, besides the story's events. +const std::map>& built_in_actions(){ + static const std::map> actions{ + {StoryStepKind::ROUTE, {"", "route", "town", "robust", "line", "exit", std::string(ROUTE_ONTO_TRIGGER), std::string(ROUTE_INTO_SCENE)}}, + {StoryStepKind::LEGS, {"", "legs", "one_short_ok", "to_wall", "to_dialogue", "to_map", "counted_wall", "measured"}}, + {StoryStepKind::STEP, {"", "face", "to_banner", "tap", "or_legs", "door"}}, {StoryStepKind::MASH, {"", "a"}}, + {StoryStepKind::BATTLE, {"", "fight", "doubles", "starter"}}, + {StoryStepKind::TALK, {"", "talk_to_wanderer", "wait_for_wanderer"}}, + {StoryStepKind::SHOP, {"", "sandgem_potions"}}, + }; + return actions; +} + +// The table: unique ids in order, each row lints, and every step's variant +// is one its kind does or one of the story's events. +UnitTestResult test_story_table_lints(Logger&){ + const StoryEvents& events = story_events(); + std::set ids; + std::string previous; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (!ids.insert(row.id).second || row.id <= previous){ + return row.id + ": repeated or out of order."; + } + previous = row.id; + if ((row.on_failure == StoryFailure::RELOAD_AND_RETRY) == row.saves_in.empty()){ + return row.id + ": a row names the map its start save is made in (saves_in) exactly when it saves first."; + } + const std::vector problems = Pokemon::lint_story_checkpoint(row); + if (!problems.empty()){ + return row.id + ": " + problems[0]; + } + for (const Pokemon::StoryStep& step : row.steps){ + const auto built_in = built_in_actions().find(step.kind); + const bool known = events.contains(step.action) || + (step.kind != StoryStepKind::GAME && step.action.empty()) || + (built_in != built_in_actions().end() && built_in->second.contains(step.action)); + if (!known){ + return std::format("{} {}: no event or {} way named \"{}\".", + row.id, step.name, Pokemon::story_step_kind_name(step.kind), step.action); + } + } + } + if (!ids.contains("00.00") || !ids.contains("00.05c") || story_checkpoints().front().on_failure != StoryFailure::STOP){ + return "Expected 00.00 (which stops on a failure) to 00.05c."; + } + return true; +} + +// Where a row's last step says it leaves the player: the end of its scene, +// or where its route (or heal) goes. None for a step that does not say. +std::optional end_pose(const StoryCheckpoint& row){ + if (row.steps.empty()){ + return std::nullopt; + } + const Pokemon::StoryStep& last = row.steps.back(); + if (last.pose_after){ + return Pokemon::pose_of(*last.pose_after); + } + if (last.kind == StoryStepKind::ROUTE){ + return Pokemon::pose_of(story_arg(last.args, "target")); + } + return std::nullopt; +} + +// Each row starts where the row before it ends: every tile its start allows +// is one the last row's end allows, on the same map. Checked where the last +// step says where it ends. +UnitTestResult test_story_table_rows_join(Logger&){ + const std::vector& rows = story_checkpoints(); + for (size_t i = 1; i < rows.size(); i++){ + const std::optional end = end_pose(rows[i - 1]); + const std::optional start = rows[i].start ? std::make_optional(Pokemon::pose_of(*rows[i].start)) : std::nullopt; + if (!end || !start){ + continue; + } + const std::vector ends = Pokemon::tiles_of(*end); + bool joins = start->map == end->map; + for (const TilePos& pos : Pokemon::tiles_of(*start)){ + joins = joins && std::find(ends.begin(), ends.end(), pos) != ends.end(); + } + if (!joins){ + return std::format("{} starts at {}, but {} ends at {}.", + rows[i].id, Pokemon::to_string(*start), rows[i - 1].id, Pokemon::to_string(*end)); + } + } + return true; +} + +struct LadyLeg{ + Direction direction; + size_t tiles; // 0: walk until the wall + TilePos end; // world tile + std::string name; +}; + +// 00.04c's Potions: the Mart lady's walk (mart_lady()) is walks until +// stopped, so on the map each leg ends at its wall: from a tile to three +// below the Center's door, a tap east and north to the Center's front, +// west to the Lab's sign, a row south and west to her grass's edge, north +// to the trees on her row (she stands east of there), back a row south and +// east to Route 201's sign, a row south and east to the trees east of the +// Mart, and north to the front row's end. +std::string follow_mart_lady(const StoryWorld& maps, const std::vector& legs, TilePos at, Logger& logger){ + for (const LadyLeg& leg : legs){ + if (leg.tiles == 0){ + at = slide_to_wall(maps, at, leg.direction).stop; + }else{ + const std::optional next = PokemonBDSP::step(maps, at, leg.direction); + if (!next){ + return leg.name + ": blocked at " + describe(maps, at) + "."; + } + at = *next; + } + // The tap east lands on whichever row we stood on. + if (&leg != &legs.front() && !(at == leg.end)){ + return leg.name + ": ends at " + describe(maps, at) + ", not " + describe(maps, leg.end) + "."; + } + logger.log(leg.name + ": " + describe(maps, at)); + } + return ""; +} + +UnitTestResult test_mart_lady_walk(Logger& logger){ + const StoryWorld& maps = story_maps(); + const auto sandgem = [&](const TilePos& pos){ return maps.to_world(MAP_SANDGEM_TOWN, pos); }; + const auto route201 = [&](const TilePos& pos){ return maps.to_world(MAP_ROUTE_201, pos); }; + const std::vector legs{ + {Direction::EAST, 1, sandgem({18, 0}), "a tap east under the Center's wall"}, + {Direction::NORTH, 0, sandgem({18, 11}), "north to the Center's front"}, + {Direction::WEST, 0, sandgem({6, 11}), "west to the Lab's sign"}, + {Direction::SOUTH, 1, sandgem({6, 12}), "a row south onto the path"}, + {Direction::WEST, 0, route201({42, 5}), "west to her grass's edge"}, + {Direction::NORTH, 0, route201({42, 3}), "north to the trees, on her row"}, + {Direction::SOUTH, 1, route201({42, 4}), "a row south onto the signs' row"}, + {Direction::EAST, 0, route201(map_point("Route201", "border_sign").next(Direction::WEST)), "east to Route 201's sign"}, + {Direction::SOUTH, 1, route201({54, 5}), "a row south onto the path"}, + {Direction::EAST, 0, sandgem({29, 12}), "east to the trees east of the Mart"}, + {Direction::NORTH, 0, sandgem(map_point("SandgemTown", "front_row_east_end")), "north to the front row's end"}, + }; + // Out of the Center's door we stand a tile to three below it (r261). + for (int below = 1; below <= 3; below++){ + const std::string error = follow_mart_lady(maps, legs, sandgem({17, 10 + below}), logger); + if (!error.empty()){ + return std::format("From {} below the door: {}", below, error); + } + } + return true; +} + +// 01.10 on Route 204's north half: the line a human walked (2026-10-31), +// corner to corner, from the Ravaged Path's exit into Floaroma. Each +// stretch must plan as the one straight tap leg the human took, on the map +// as it is: a detour here means the map or the line changed. +UnitTestResult test_route204_north_line(Logger& logger){ + const StoryWorld& route = route204_north_maps(); + const std::vector waypoints = map_region("Route204North", "floaroma_waypoints"); + struct Expected{ Direction direction; size_t tiles; }; + const std::vector expected{ + {Direction::SOUTH, 1}, {Direction::WEST, 9}, {Direction::NORTH, 5}, {Direction::EAST, 8}, + {Direction::NORTH, 10}, {Direction::WEST, 8}, {Direction::NORTH, 7}, + }; + if (waypoints.size() != expected.size()){ + return std::format("Route 204 North: {} waypoints, expected {}.", waypoints.size(), expected.size()); + } + std::vector at{map_point("Route204North", "cave_exit")}; + for (size_t i = 0; i < waypoints.size(); i++){ + const TilePos goal = waypoints[i]; + const std::string name = std::format("Route 204 North stretch {} to {}", i + 1, describe(route, goal)); + const auto plan = plan_robust_route(route, at, [&](const TilePos& pos){ return pos == goal; }, waypoint_plan_costs()); + if (!plan){ + return name + ": no plan."; + } + if (plan->size() != 1){ + return std::format("{}: {} legs, expected the one straight leg.", name, plan->size()); + } + const TileLeg& leg = (*plan)[0].leg; + logger.log(name + ": " + to_string(leg)); + if (leg.ends_at_wall || leg.direction != expected[i].direction || leg.tiles != expected[i].tiles){ + return std::format("{}: planned {}, expected an open leg of {} tiles.", name, to_string(leg), expected[i].tiles); + } + if ((*plan)[0].after.size() != 1 || !((*plan)[0].after[0] == goal)){ + return name + ": the leg does not end exactly on the waypoint."; + } + at = {goal}; + } + if (!in_floaroma_path(at[0])){ + return "Route 204 North: the last waypoint is not in the path into Floaroma."; + } + return true; +} + +// 01.10's line 5, "north 10 (open)" to (19, 12), after Aroma Lady Taylor's +// battle (lab-B 2026-11-08 18:15:47): we may stand anywhere on (19, 12..20). +// Column 19 is open floor from row 7 to 13 below the wall on row 6, so north +// to that wall and 5 south reach the goal; the console's plan, "south 12 (to +// wall) to (19, 24), north 12 (open) to (19, 12)", pinned the wall with the +// fewest held tiles and walked back twice as far. +UnitTestResult test_line_5_recovery_pins_the_wall_nearest_the_goal(Logger& logger){ + const StoryWorld& world = *story_plan_world("Route204North", "Route204North"); + std::vector from; + for (int y = 12; y <= 20; y++){ + from.push_back(story_to_world(world, "Route204North", {19, y})); + } + const TilePos goal = story_to_world(world, "Route204North", {19, 12}); + const auto legs = plan_recovered_route(world, from, [&](const TilePos& pos){ return pos == goal; }, *story_route_costs("line")); + if (!legs || legs->empty()){ + return "no plan from (19, 12..20)."; + } + for (const PlannedLeg& planned : *legs){ + logger.log("01.10 line 5 recovery: " + to_string(planned)); + } + const PlannedLeg& first = legs->front(); + const TilePos wall = story_to_world(world, "Route204North", {19, 7}); + if (first.leg.direction != Direction::NORTH || !first.leg.ends_at_wall || first.after != std::vector{wall}){ + return "the recovery starts " + to_string(first) + ", not north to the wall at (19, 7)."; + } + return true; +} + +// Why `planned` may slide into a door or a warp, or "": an open leg (not to +// a wall) that may end on the tile before one, walking toward it. A part of +// a tile too far takes the warp (lab-B 2026-11-08 15:31 to 15:54: 01.05's +// open walk west to (2, 13), beside the Oreburgh Gate's mouth, slid into +// the Gate, and the tap after it waited for a banner already gone). The +// maps draw every warp as a door tile, cave mouths too. +std::string slides_into_door(const StoryWorld& world, const PlannedLeg& planned){ + if (planned.leg.ends_at_wall){ + return ""; + } + for (const TilePos& pos : planned.after){ + const TilePos ahead = pos.next(planned.leg.direction); + if (world.tile(ahead) == Pokemon::TileKind::DOOR){ + return std::format("the open leg {} may end on {}, the tile before the door {}", + to_string(planned.leg), describe(world, pos), describe(world, ahead)); + } + } + return ""; +} + +// A ROUTE step of the table from a known place: the world it plans on and +// its plan, or nothing if it finds none. +struct PlannedStoryRoute{ + std::string name; // " " + const StoryWorld* world; + std::vector from; + std::optional> legs; +}; + +// Every ROUTE step of the table that starts from a known place, planned as +// walk_route() plans it. Where a row's steps leave the player is followed +// from its start through each step's target or pose_after; a route from an +// unknown place is logged, not planned. +std::vector plan_story_routes(Logger& logger){ + std::vector routes; + for (const TableRoute& route : table_routes()){ + if (route.world == nullptr){ + logger.log(std::format("{} {}: not checked, from {}.", route.row, route.step->name, + route.from ? Pokemon::to_string(*route.from) : "nowhere known")); + continue; + } + RouteStepPlan plan = plan_table_route(route); + routes.push_back({route.row + " " + route.step->name, route.world, std::move(plan.start), std::move(plan.legs)}); + } + return routes; +} + +// No ROUTE step of the table plans an open leg that ends before a door it +// walks toward: a door is gone through by a step that runs or taps into it +// from where the route ends on a wall or off its line. +UnitTestResult test_story_routes_stop_short_of_doors(Logger& logger){ + std::vector found; + size_t checked = 0; + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + checked += route.legs ? 1 : 0; + for (const PlannedLeg& planned : route.legs ? *route.legs : std::vector{}){ + const std::string why = slides_into_door(*route.world, planned); + if (!why.empty()){ + found.push_back(std::format("{}: {}.", route.name, why)); + } + } + } + logger.log(std::format("{} routes planned.", checked)); + std::string out; + for (const std::string& line : found){ + out += "\n " + line; + } + return found.empty() ? UnitTestResult(true) : UnitTestResult("Open legs that end before a door they walk toward:" + out); +} + +// The tiles a planned leg walks over from `from`, `from` first: to the wall, +// or its tiles' worth of steps. +std::vector walked_tiles(const StoryWorld& world, const TilePos& from, const TileLeg& leg){ + std::vector tiles{from}; + size_t walked = 0; + while (leg.ends_at_wall || walked < leg.tiles){ + const std::optional next = step(world, tiles.back(), leg.direction); + if (!next){ + break; + } + walked += (size_t)(std::abs(next->x - tiles.back().x) + std::abs(next->y - tiles.back().y)); + tiles.push_back(*next); + } + return tiles; +} + +// The most tiles of `zone` one leg of `legs` walks over, from any tile the +// player may be on, and that leg. +std::pair most_zone_tiles( + const StoryWorld& world, std::vector from, const std::vector& legs, + const std::function& zone +){ + std::pair most{0, ""}; + for (const PlannedLeg& planned : legs){ + for (const TilePos& pos : from){ + const std::vector tiles = walked_tiles(world, pos, planned.leg); + const size_t in_zone = (size_t)std::count_if(tiles.begin(), tiles.end(), zone); + if (in_zone > most.first){ + most = {in_zone, to_string(planned.leg) + " from " + describe(world, pos)}; + } + } + from = planned.after; + } + return most; +} + +// Where a planned leg from `from` runs along a zone where someone wanders, +// or "": two zone tiles in a row on a straight leg are tiles of one zone. +std::string along_wander_zone(const StoryWorld& world, const TilePos& from, const TileLeg& leg){ + const std::vector tiles = walked_tiles(world, from, leg); + for (size_t i = 1; i < tiles.size(); i++){ + if (wandering_hazard(world, tiles[i - 1]) && wandering_hazard(world, tiles[i])){ + return std::format("{} from {} runs along a wander zone at {}", to_string(leg), describe(world, from), describe(world, tiles[i])); + } + } + return ""; +} + +// Every ROUTE step of the table from a known place has a plan, and no leg +// of it runs along a zone where someone wanders: plan_robust_route() never +// plans one, so a step with no other way finds no plan here. +UnitTestResult test_story_routes_keep_off_wander_zones(Logger& logger){ + std::string out; + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + if (!route.legs){ + out += "\n " + route.name + ": no plan."; + continue; + } + std::vector from = route.from; + for (const PlannedLeg& planned : *route.legs){ + for (const TilePos& pos : from){ + const std::string along = along_wander_zone(*route.world, pos, planned.leg); + out += along.empty() ? "" : "\n " + route.name + ": " + along + "."; + } + from = planned.after; + } + } + return out.empty() ? UnitTestResult(true) : UnitTestResult("ROUTE steps:" + out); +} + +// No leg of 00.05b's or 00.05c's routes enters a tile where someone +// wanders, from any tile the player may be on: on lab-B (2026-11-11 21:00) +// the boy below the Mart (51, 33..35) stopped 00.05b's "east 8 (to wall)" +// along row 34 three tiles short; row 36 below him is open. The Mart +// clown's box counts too: the routes reach column 56 and leave it at +// (56, 36) and (56, 31), outside it, and wait_for_wanderer walks the +// column between. +UnitTestResult test_jubilife_routes_cross_no_wander_zone(Logger& logger){ + std::string out; + size_t checked = 0; + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + if (!route.name.starts_with("00.05b ") && !route.name.starts_with("00.05c ")){ + continue; + } + if (!route.legs){ + out += "\n " + route.name + ": no plan."; + continue; + } + checked++; + const StoryWorld& world = *route.world; + const Pokemon::TileBox clown = jubilife_mart_clown_box(); + const auto [most, leg] = most_zone_tiles(world, route.from, *route.legs, [&](const TilePos& pos){ + const std::optional local = story_to_local(world, "JubilifeCity", pos); + const bool clowns = local && local->x >= clown.x0 && local->x <= clown.x1 && local->y >= clown.y0 && local->y <= clown.y1; + return clowns || wandering_hazard(world, pos); + }); + if (most > 0){ + out += std::format("\n {}: {} enters {} tile(s) where someone wanders.", route.name, leg, most); + } + } + logger.log(std::format("{} Jubilife routes checked.", checked)); + if (checked == 0){ + return "No 00.05b or 00.05c route planned from a known place."; + } + return out.empty() ? UnitTestResult(true) : UnitTestResult("ROUTE steps:" + out); +} + +// Where the walk from `x` on row 22 walks the plaza, or "". +// The Wonder Station's plaza on JubilifeCity, as measured on lab-B +// (2026-11-12): its round wall and the woman by it stop walks west on +// rows 21 to 24 from x 52 and on row 26 from x 43 (taps from (58, 26) +// moved 14 tiles clean to (44, 26), then stopped by her). Row 26 east of +// her is open: the way west goes south round her. +constexpr Pokemon::TileBox WONDER_STATION_PLAZA[] = { + {.x0 = 40, .y0 = 20, .x1 = 52, .y1 = 25}, + {.x0 = 40, .y0 = 26, .x1 = 43, .y1 = 27}, +}; + +std::string company_clown_plaza_tiles(Logger& logger, TableRoute route, int x){ + route.from = Pokemon::tile_pose("JubilifeCity", {{x, 21}}, Direction::WEST); + const RouteStepPlan plan = plan_table_route(route); + if (!plan.legs){ + return std::format("no plan from ({}, 21).", x); + } + const StoryWorld& world = *route.world; + for (const PlannedLeg& planned : *plan.legs){ + logger.log(std::format("00.05c beside_the_president from ({}, 21): {}", x, to_string(planned))); + } + const auto plaza = [&](const TilePos& pos){ + const std::optional local = story_to_local(world, "JubilifeCity", pos); + return local && std::any_of(std::begin(WONDER_STATION_PLAZA), std::end(WONDER_STATION_PLAZA), [&](const Pokemon::TileBox& box){ + return local->x >= box.x0 && local->x <= box.x1 && local->y >= box.y0 && local->y <= box.y1; + }); + }; + const auto [most, leg] = most_zone_tiles(world, plan.start, *plan.legs, plaza); + return most == 0 ? "" : std::format("the leg {} walks {} tile(s) of the plaza.", leg, most); +} + +// 00.05c's first walk (to the president since its clown re-talks went; +// to the Poketch Company's clown before) from the street +// (57..58, 21), where the checkpoint starts on a rerun from 01.01's start, +// keeps off the plaza (WONDER_STATION_PLAZA). BDSP put the Global Wonder Station +// there, a round wall with people round it that pret's Diamond map lacks: +// on lab-B (2026-11-12 01:22 and 01:55) the walk west from (57, 22) was +// stopped at (50, 22), (51, 21) and (49, 23), and talked to the woman at +// (45, 24) until the step failed. +UnitTestResult test_company_clown_from_the_street_keeps_off_the_plaza(Logger& logger){ + for (const TableRoute& route : table_routes()){ + if (route.row != "00.05c" || route.step->name != "beside_the_president" || route.world == nullptr){ + continue; + } + std::string out; + for (int x : {57, 58}){ + const std::string found = company_clown_plaza_tiles(logger, route, x); + out += found.empty() ? "" : "\n " + found; + } + return out.empty() ? UnitTestResult(true) : UnitTestResult("00.05c beside_the_president:" + out); + } + return "00.05c beside_the_president: not in the table, or from nowhere known."; +} + +// 00.05c's replan on lab-B (2026-11-12 02:52): the walk west on row 24 +// talked to the woman it took for (34, 24), marked her tile off the map, +// and from (35, 24) and (36, 24), with (43, 26) marked as a wall, found no +// plan at all, with the route costs or the default ones. +UnitTestResult test_company_clown_after_the_woman_on_row_24(Logger& logger){ + for (TableRoute route : table_routes()){ + if (route.row != "00.05c" || route.step->name != "beside_the_president" || route.world == nullptr){ + continue; + } + StoryWorld world = *route.world; + world.block(story_to_world(world, "JubilifeCity", {43, 26})); + world.uncover(story_to_world(world, "JubilifeCity", {34, 24})); + route.world = &world; + route.from = Pokemon::tile_pose("JubilifeCity", {{35, 24}, {36, 24}}, Direction::WEST); + const RouteStepPlan plan = plan_table_route(route); + if (!plan.legs){ + return "00.05c beside_the_president: no plan from (35, 24), (36, 24)."; + } + for (const PlannedLeg& planned : *plan.legs){ + logger.log("00.05c beside_the_president from (35, 24), (36, 24): " + to_string(planned)); + } + return UnitTestResult(true); + } + return "00.05c beside_the_president: not in the table, or from nowhere known."; +} + +// 00.05c's end below the east-exit sign (59, 21) facing north, on the +// planned route from the Mart's street (lab-B 2026-11-12 13:55: a "measured" +// walk inferred its start and tapped against the hedges). Each leg is taps +// (a whole tile each, a turn tap first) or a wall's end, and none steps on +// the guard's trigger column 60. +UnitTestResult test_main_road_ends_below_the_east_exit_sign(Logger& logger){ + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + if (route.name != "00.05c below_the_east_exit_sign"){ + continue; + } + if (!route.legs || route.legs->empty()){ + return "00.05c below_the_east_exit_sign: no plan."; + } + const StoryWorld& world = *route.world; + const auto guard_column = [&](const TilePos& pos){ + const std::optional local = story_to_local(world, "JubilifeCity", pos); + return local && local->x >= 60; + }; + const auto [most, leg] = most_zone_tiles(world, route.from, *route.legs, guard_column); + if (most > 0){ + return std::format("the leg {} steps on {} tiles of column 60 or east.", leg, most); + } + const PlannedLeg& last = route.legs->back(); + const auto on_the_tile = [&](const TilePos& pos){ + const std::optional local = story_to_local(world, "JubilifeCity", pos); + return local && *local == TilePos{59, 21}; + }; + if (last.after.size() != 1 || !on_the_tile(last.after.front()) || last.leg.direction != Direction::NORTH || to_string(last).find("to wall") == std::string::npos){ + return "00.05c below_the_east_exit_sign does not end on (59, 21) facing north against the sign."; + } + return true; + } + return "00.05c below_the_east_exit_sign is not a ROUTE step from a known place."; +} + +// A leg whose end has a wall (or the maps' edge) right after it in the +// leg's direction is a leg to the wall: held until the wall stops the +// player, which localizes, not tapped to a tile and left open (lab-B, +// 2026-11-12 14:24: 00.05c's "east 14 (open)" to (59, 26) was a run into +// the wall past it). +UnitTestResult test_legs_ending_at_a_wall_are_wall_legs(Logger& logger){ + std::vector found; + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + for (const PlannedLeg& planned : route.legs ? *route.legs : std::vector{}){ + const bool at_wall_end = !planned.after.empty() && std::all_of(planned.after.begin(), planned.after.end(), [&](const TilePos& pos){ + return at_wall(*route.world, pos, planned.leg.direction); + }); + if (!planned.leg.ends_at_wall && at_wall_end){ + found.push_back(std::format("{}: {}.", route.name, to_string(planned))); + } + } + } + std::string out; + for (const std::string& line : found){ + out += "\n " + line; + } + return found.empty() ? UnitTestResult(true) : UnitTestResult("open legs that end at a wall:" + out); +} + +// After a tapped leg the walk knows its place exactly: a leg to a wall +// from it is not followed by a leg straight back (00.05c's "east 19 +// (open)", "south 15 (to wall)", then "north 1"). Run to a wall and back is +// fine from the start or after a wall (00.04 into_twinleaf, south 37). +UnitTestResult test_no_wall_and_back_after_taps(Logger& logger){ + std::vector found; + for (const PlannedStoryRoute& route : plan_story_routes(logger)){ + if (!route.legs){ + continue; + } + const std::vector& legs = *route.legs; + for (size_t i = 2; i < legs.size(); i++){ + const bool back = (int)legs[i].leg.direction == ((int)legs[i - 1].leg.direction + 2) % 4; + const TileLeg& taps = legs[i - 2].leg; + if (!taps.ends_at_wall && !taps.held && legs[i - 1].leg.ends_at_wall && back){ + found.push_back(std::format("{}: {}, {} then {}.", route.name, to_string(legs[i - 2]), to_string(legs[i - 1]), to_string(legs[i]))); + } + } + } + std::string out; + for (const std::string& line : found){ + out += "\n " + line; + } + return found.empty() ? UnitTestResult(true) : UnitTestResult("a wall and back after a tapped leg:" + out); +} + +// The route plan dump: every table ROUTE step's legs, in a golden file +// beside the story data, so a planner or table change shows its plans' diff +// in review. + +constexpr std::string_view ROUTE_PLANS_HEADER = + "BDSP AutoStory: the legs each ROUTE step of the story table plans.\n" + "Planned as the console's RouteWalker plans them: plan_recovered_route() on\n" + "story_plan_world(), from the tiles the row's steps before it leave the\n" + "player on, with the step's costs, then the default costs if those find none.\n" + "Written by the StoryTable test \"every ROUTE step plans as\n" + "Data/route_plans.golden.txt says\". After a planner or table change, run\n" + "that test with PA_UPDATE_ROUTE_PLANS=1 to rewrite this file\n" + "(PA_ROUTE_PLANS_GOLDEN= reads or writes another copy), and review\n" + "its diff.\n" + "turns: direction changes between legs. open: legs that stop in the open\n" + "(tapped, not held to a wall). localization: the wall holds at the start\n" + "that only bring several start tiles to one (plan_recovered_route()'s\n" + "anchor); 0 from one start tile.\n"; + +std::string tile_list(const StoryWorld& world, const std::vector& tiles){ + std::string out; + for (const TilePos& pos : tiles){ + out += (out.empty() ? "" : ", ") + describe(world, pos); + } + return out; +} + +// plan_recovered_route()'s wall holds from several start tiles: the legs up +// to where the tiles the player may be on first become one. +size_t localization_legs(const std::vector& start, const std::vector& legs){ + if (start.size() < 2){ + return 0; + } + for (size_t i = 0; i < legs.size(); i++){ + if (legs[i].after.size() == 1){ + return i + 1; + } + } + return 0; +} + +std::string plan_totals(const RouteStepPlan& plan){ + const std::vector& legs = *plan.legs; + size_t turns = 0; + size_t open = 0; + for (size_t i = 0; i < legs.size(); i++){ + turns += i > 0 && legs[i].leg.direction != legs[i - 1].leg.direction ? 1 : 0; + open += legs[i].leg.ends_at_wall ? 0 : 1; + } + return std::format(" totals: {} legs, {} turns, {} open, {} localization\n", + legs.size(), turns, open, localization_legs(plan.start, legs)); +} + +std::string dump_plan(const StoryWorld& world, const RouteStepPlan& plan){ + std::string out = " start candidates: " + tile_list(world, plan.start) + "\n"; + if (!plan.legs){ + return out + " NO PLAN\n"; + } + if (plan.default_costs){ + out += " the step's costs found no plan; planned with the default costs\n"; + } + for (size_t i = 0; i < plan.legs->size(); i++){ + const PlannedLeg& planned = (*plan.legs)[i]; + out += std::format(" {}. {} -> {}\n", i + 1, to_string(planned.leg), tile_list(world, planned.after)); + } + return out + plan_totals(plan); +} + +std::string dump_route(const TableRoute& route){ + const Pokemon::StoryStep& step = *route.step; + std::string out = std::format("== {} {} (costs: {})\n", route.row, step.name, step.action.empty() ? "route" : step.action); + out += " from: " + (route.from ? Pokemon::to_string(*route.from) : std::string("unknown")) + "\n"; + const Pokemon::Pose target = Pokemon::pose_of(story_arg(step.args, "target")); + out += " target: " + Pokemon::to_string(target) + "\n"; + if (route.world == nullptr){ + return out + (route.from && !Pokemon::tiles_of(*route.from).empty() + ? " not planned: no story world joins the two maps\n" + : " not planned: the start is unknown (a LEGS or STEP step before it does not say where it ends)\n"); + } + return out + dump_plan(*route.world, plan_table_route(route)); +} + +std::string route_plans_text(){ + std::string out = std::string(ROUTE_PLANS_HEADER); + for (const TableRoute& route : table_routes()){ + out += "\n" + dump_route(route); + } + return out; +} + +// The golden file, in the Data folder beside this file's. +std::string route_plans_golden_path(){ + const char* path = std::getenv("PA_ROUTE_PLANS_GOLDEN"); + if (path != nullptr && path[0] != '\0'){ + return path; + } + const std::string self = __FILE__; + const size_t slash = self.find_last_of("/\\"); + return (slash == std::string::npos ? std::string() : self.substr(0, slash + 1)) + "Data/route_plans.golden.txt"; +} + +// `text` without carriage returns or a byte order mark. +std::string plain_lines(const std::string& text){ + std::string out = text.starts_with("\xef\xbb\xbf") ? text.substr(3) : text; + std::erase(out, '\r'); + return out; +} + +// `text` split into the header and one block per "== " step line. +std::vector plan_blocks(const std::string& text){ + std::vector blocks{""}; + size_t begin = 0; + while (begin < text.size()){ + const size_t end = std::min(text.find('\n', begin), text.size() - 1) + 1; + const std::string line = text.substr(begin, end - begin); + if (line.starts_with("== ")){ + blocks.push_back(""); + } + blocks.back() += line; + begin = end; + } + return blocks; +} + +// Each step whose block changed, as "-" (golden) and "+" (planned) blocks. +std::string plan_differences(const std::string& golden, const std::string& planned){ + const std::vector expected = plan_blocks(golden); + const std::vector actual = plan_blocks(planned); + std::string out; + size_t changed = 0; + for (size_t i = 0; i < std::max(expected.size(), actual.size()); i++){ + const std::string& was = i < expected.size() ? expected[i] : ""; + const std::string& now = i < actual.size() ? actual[i] : ""; + if (was != now && changed++ < 5){ + out += "\n--- golden:\n" + was + "+++ planned:\n" + now; + } + } + return changed == 0 ? "" : std::format("{} block(s) differ, the first ones:{}", changed, out); +} + +// Every table ROUTE step plans the legs the golden file lists. +UnitTestResult test_route_plans_golden(Logger& logger){ + const std::string path = route_plans_golden_path(); + const std::string planned = route_plans_text(); + const char* update = std::getenv("PA_UPDATE_ROUTE_PLANS"); + if (update != nullptr && update[0] != '\0' && update[0] != '0'){ + std::string crlf; + for (char ch : planned){ + crlf += ch == '\n' ? std::string("\r\n") : std::string(1, ch); + } + FileIO file; + if (!file.open(path, FileMode::WRITE | FileMode::BINARY) || file.write(crlf) != crlf.size()){ + return "Could not write " + path + "."; + } + logger.log("Wrote " + path + "."); + return true; + } + std::string golden; + if (!file_to_string(path, golden)){ + return "Could not read " + path + " (set PA_ROUTE_PLANS_GOLDEN to its path)."; + } + const std::string differences = plan_differences(plain_lines(golden), planned); + if (differences.empty()){ + return true; + } + return "The route plans differ from " + path + ". If the change is meant, rerun with PA_UPDATE_ROUTE_PLANS=1 " + "and review the file's diff. " + differences; +} + + +// plan_story_run() with Starter RNG handoff: off leaves the range unchanged; +// on stops after 00.01 with a save at the Verity Lakefront (17, 14) facing +// north, where 00.02 and Starter RNG start; starting past 00.01 runs on. +UnitTestResult test_plan_story_run_starter_rng(Logger&){ + const std::vector& table = story_checkpoints(); + if (table.size() < 3){ + return std::format("Expected the story table to reach 00.02, got {} rows.", table.size()); + } + const StoryRunPlan full = plan_story_run(table, 0, table.size() - 1, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::OFF}); + if (full.stop != StoryHandoff::NONE || full.rows.size() != table.size()){ + return "With Starter RNG handoff off, expected the full table with no stop."; + } + const StoryRunPlan handoff = plan_story_run(table, 0, table.size() - 1, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::STOP}); + if (handoff.stop != StoryHandoff::STARTER_RNG || handoff.rows.size() != 2 || + handoff.rows[0].id != "00.00" || handoff.rows[1].id != "00.01" + ){ + return "Expected a 00.00..01.11 run with Starter RNG handoff to stop after 00.01."; + } + const StoryCheckpoint& lakefront_row = handoff.rows.back(); + const std::vector problems = Pokemon::lint_story_checkpoint(lakefront_row); + if (!problems.empty()){ + return std::format("00.01 with Starter RNG handoff save: {}", problems[0]); + } + if (lakefront_row.steps.empty() || lakefront_row.steps.back().kind != StoryStepKind::SAVE || + lakefront_row.steps.back().name != "save_for_starter_rng" + ){ + return "Expected 00.01 with Starter RNG handoff to end with save_for_starter_rng."; + } + // The save card is checked as every other start save is: it must name + // 00.02's map. + if (lakefront_row.steps.back().action != table[2].saves_in || table[2].saves_in.empty()){ + return std::format( + "Expected the handoff save to check the card for \"{}\", got \"{}\".", + table[2].saves_in, lakefront_row.steps.back().action + ); + } + const std::optional end = end_pose(lakefront_row); + const std::optional next_start = table[2].start + ? std::make_optional(Pokemon::pose_of(*table[2].start)) + : std::nullopt; + if (!end || !next_start || end->map != "VerityLakefront" || + Pokemon::to_string(*end) != Pokemon::to_string(*next_start) + ){ + return "Expected 00.01's Starter RNG handoff save to end at 00.02's VerityLakefront (17, 14) start pose."; + } + const StoryRunPlan from_0002 = plan_story_run(table, 2, 4, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::STOP}); + if (from_0002.stop != StoryHandoff::NONE || from_0002.rows.size() != 3 || from_0002.rows.front().id != "00.02"){ + return "Expected a run starting at 00.02 to pass through without stopping for 00.01's handoff."; + } + return true; +} + +// plan_story_run() with Run Starter RNG in AutoStory: 00.02 runs the +// starter_rng event after its Verity Lakefront start save and ends where +// 00.03 starts; the handoff stop is not taken. +UnitTestResult test_plan_story_run_in_program(Logger&){ + const std::vector& table = story_checkpoints(); + if (table.size() < 4){ + return std::format("Expected the story table to reach 00.03, got {} rows.", table.size()); + } + const StoryRunPlan in_program = plan_story_run( + table, 0, table.size() - 1, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::RUN} + ); + if (in_program.stop != StoryHandoff::NONE || in_program.rows.size() != table.size()){ + return "With Run Starter RNG in AutoStory on, expected the full table with no handoff stop."; + } + const StoryCheckpoint& rng_row = in_program.rows[2]; + const std::vector rng_problems = Pokemon::lint_story_checkpoint(rng_row); + if (!rng_problems.empty()){ + return std::format("00.02 with Run Starter RNG in AutoStory: {}", rng_problems[0]); + } + if (rng_row.id != "00.02" || rng_row.saves_in != "Verity Lakefront" || rng_row.steps.size() != 1 || + rng_row.steps[0].kind != StoryStepKind::GAME || rng_row.steps[0].action != "starter_rng" || + !story_events().contains(rng_row.steps[0].action) + ){ + return "Expected 00.02 with Run Starter RNG in AutoStory to run the starter_rng game event after its Verity Lakefront start save."; + } + const std::optional rng_end = end_pose(rng_row); + const std::optional row0003_start = table[3].start + ? std::make_optional(Pokemon::pose_of(*table[3].start)) + : std::nullopt; + if (!rng_end || !row0003_start || Pokemon::to_string(*rng_end) != Pokemon::to_string(*row0003_start)){ + return "Expected 00.02's starter_rng step to end at 00.03's VerityLakefront (17, 14..15) start pose."; + } + if (bdsp_starter_for_choice(StarterChoice::TURTWIG) != BdspStarter::Turtwig || + bdsp_starter_for_choice(StarterChoice::CHIMCHAR) != BdspStarter::Chimchar || + bdsp_starter_for_choice(StarterChoice::PIPLUP) != BdspStarter::Piplup + ){ + return "Expected StarterChoice to map 1-to-1 to BdspStarter."; + } + return true; +} + + +// The Starter RNG run's settings: the player's model, the starter and the +// reset limit come from AutoStory's options, and the run ends where 00.03 +// starts (Lakefront overworld, menu closed). +UnitTestResult test_starter_rng_settings(Logger&){ + BdspRngFilterTable table("Filters", Pokemon::StatsHuntMiscFeatureFlags{false, true, true, true}); + set_default_shiny_row(table); + const BdspRngFilterSnapshot filters = table.make_snapshot(); + const StoryStarterRngConfig config{ + .language = Language::French, .player_model = 5, .starter = StarterChoice::PIPLUP, .max_resets = 7 + }; + const StarterRngSettings settings = make_starter_rng_settings(config, filters); + if (settings.player_model != 5){ + return std::format("Expected the story's player model 5 to reach Starter RNG, got {}.", settings.player_model); + } + if (settings.starter != BdspStarter::Piplup || settings.language != Language::French || settings.max_resets != 7){ + return "Expected the starter, language and reset limit to come from the story's options."; + } + if (settings.filters != &filters || settings.start_point != BdspStartPoint::Lakefront){ + return "Expected the filter snapshot and the Verity Lakefront start point."; + } + if (settings.reset_first || !settings.finish_battle_on_shiny || !settings.close_summary_after_read){ + return "Expected a run that starts from the loaded save and leaves the Lakefront overworld on a shiny or not."; + } + return true; +} + +// What Starter RNG ending means for the story: a starter in hand and the +// game on the Lakefront lets it go on; anything else stops it with a reason. +UnitTestResult test_starter_rng_failure(Logger&){ + StarterRngRunResult result; + result.outcome = StarterRngOutcome::HIT; + if (!starter_rng_failure(result).empty()){ + return "Expected a hit to let the story go on."; + } + result.outcome = StarterRngOutcome::SHINY; + if (!starter_rng_failure(result).empty()){ + return "Expected a settled shiny to let the story go on."; + } + result.settled = false; + if (starter_rng_failure(result).empty()){ + return "Expected a shiny that did not settle (the game is mid-scene) to stop the story."; + } + for (const StarterRngOutcome outcome : {StarterRngOutcome::ABANDONED, StarterRngOutcome::OUT_OF_RESETS}){ + StarterRngRunResult failed; + failed.outcome = outcome; + failed.resets_used = 3; + if (starter_rng_failure(failed).empty()){ + return "Expected a run with no starter to stop the story."; + } + } + return true; +} + +// A failed Run Starter RNG ends the program: 00.02's row retry would rerun +// the whole reset loop. Without it no row is fatal on top of the table's. +UnitTestResult test_starter_rng_row_is_fatal(Logger&){ + const std::vector& table = story_checkpoints(); + const StoryRunPlan in_program = plan_story_run( + table, 0, 3, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::RUN} + ); + if (in_program.fatal_rows != std::set{"00.02"}){ + return "Expected 00.02 to be fatal when Run Starter RNG in AutoStory is on."; + } + const StoryRunPlan plain = plan_story_run(table, 0, 3, StoryHandoffs{.starter_rng = Pokemon::RngHandoffMode::STOP}); + const StoryRunPlan off = plan_story_run(table, 0, 3); + if (!plain.fatal_rows.empty() || !off.fatal_rows.empty()){ + return "Expected no fatal rows unless Run Starter RNG in AutoStory is on."; + } + return true; +} + + +struct AutoStoryStoryTableInput{ + std::string name; + std::function body; +}; + +struct AutoStoryStoryTableExpectation{ +}; + +struct AutoStoryStoryTableCase{ + AutoStoryStoryTableInput input; + AutoStoryStoryTableExpectation expectation; +}; + +class Test_AutoStoryStoryTable : public UnitTest{ +public: + explicit Test_AutoStoryStoryTable(AutoStoryStoryTableCase test_case) + : UnitTest("PokemonBDSP::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryStoryTableCase m_case; +}; + +} // namespace + + +const AutoStoryStoryTableCase AUTO_STORY_STORY_TABLE_CASES[] = { + { + .input = {"Story table: rows lint, variants known", test_story_table_lints}, + .expectation = {}, + }, + { + .input = {"Story table: each row starts where the last one ends", test_story_table_rows_join}, + .expectation = {}, + }, + { + .input = {"00.04: the walk to the Poke Mart lady and back", test_mart_lady_walk}, + .expectation = {}, + }, + { + .input = {"Route 204 North: the line into Floaroma", test_route204_north_line}, + .expectation = {}, + }, + { + .input = {"01.10: line 5's recovery pins the wall nearest its goal (lab-B, 2026-11-08)", test_line_5_recovery_pins_the_wall_nearest_the_goal}, + .expectation = {}, + }, + { + .input = {"Story table: no open leg ends before a door it walks toward (lab-B, 2026-11-08)", test_story_routes_stop_short_of_doors}, + .expectation = {}, + }, + { + .input = {"Story table: every ROUTE step plans as Data/route_plans.golden.txt says", test_route_plans_golden}, + .expectation = {}, + }, + { + .input = {"Story table: every route plans, and none runs along a wander zone", test_story_routes_keep_off_wander_zones}, + .expectation = {}, + }, + { + .input = {"00.05c: the plan to below the east-exit sign is taps and a wall's end, off the guard's column (lab-B, 2026-11-12 13:55)", test_main_road_ends_below_the_east_exit_sign}, + .expectation = {}, + }, + { + .input = {"a leg that ends at a wall is planned as a leg to the wall (00.05c east 14 to (59, 26), lab-B 2026-11-12 14:24)", test_legs_ending_at_a_wall_are_wall_legs}, + .expectation = {}, + }, + { + .input = {"no run to a wall and back after a tapped leg (00.05c east 19, south 15, north 1, lab-B 2026-11-12 14:24)", test_no_wall_and_back_after_taps}, + .expectation = {}, + }, + { + .input = {"00.05b, 00.05c: no leg enters a wander zone (lab-B, 2026-11-11)", test_jubilife_routes_cross_no_wander_zone}, + .expectation = {}, + }, + { + .input = {"00.05c: from the street (57..58, 21), the walk keeps off the Wonder Station's plaza (lab-B, 2026-11-12)", test_company_clown_from_the_street_keeps_off_the_plaza}, + .expectation = {}, + }, + { + .input = {"00.05c: a plan from (35, 24), (36, 24) past the woman on row 24 (lab-B, 2026-11-12 02:52)", test_company_clown_after_the_woman_on_row_24}, + .expectation = {}, + }, + { + .input = {"Starter RNG handoff: stops after 00.01 saved at the Verity Lakefront", test_plan_story_run_starter_rng}, + .expectation = {}, + }, + { + .input = {"Run Starter RNG: 00.02 runs the starter_rng event", test_plan_story_run_in_program}, + .expectation = {}, + }, + { + .input = {"Run Starter RNG: settings come from the story's options", test_starter_rng_settings}, + .expectation = {}, + }, + { + .input = {"Run Starter RNG: what lets the story go on", test_starter_rng_failure}, + .expectation = {}, + }, + { + .input = {"Run Starter RNG: a failed 00.02 ends the program", test_starter_rng_row_is_fatal}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_STORY_TABLE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h new file mode 100644 index 0000000000..cff4508962 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - StoryTable Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryTable_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryTable_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Tests that need no console: the story table: rows lint, every step's variant is known, and the walks its events plan. +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp new file mode 100644 index 0000000000..c922a0af02 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -0,0 +1,783 @@ +/* BDSP AutoStory - Story walk steps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/Navigation/Pokemon_LegTrace.h" +#include "Pokemon/Navigation/Pokemon_MapData.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::story_arg; +using Pokemon::Outcome; +using Pokemon::Pose; +using Pokemon::RunState; +using Pokemon::StoryStep; +using Pokemon::TilePos; + + +namespace{ + +// The leg records of a step: its legs are this event's, and one still +// open when the step ends is written with its end unknown. +class EventTrace{ +public: + EventTrace(Pokemon::LegTracer& legs, const std::string& row, const std::string& step) + : m_legs(legs) + { + m_legs.begin_event(row, step); + } + ~EventTrace(){ + try{ + m_legs.end_event(); + }catch (...){} + } + EventTrace(const EventTrace&) = delete; + EventTrace& operator=(const EventTrace&) = delete; + +private: + Pokemon::LegTracer& m_legs; +}; + +// `tiles` of `map` as the route lines name them: by the story world's +// map names, where the map has a world. +std::vector traced_tiles(const std::string& map, const std::vector& tiles){ + const StoryWorld* world = story_plan_world(map, map); + std::vector out; + for (const TilePos& pos : tiles){ + const std::optional local = world == nullptr ? std::nullopt : world->to_local(story_to_world(*world, map, pos)); + out.push_back(local ? Pokemon::RouteTile{local->map, local->pos} : Pokemon::RouteTile{map, pos}); + } + return out; +} + +} // namespace + + +namespace{ + +// "to_catch_grass" as "to catch grass", for the logs. +std::string label_of(const StoryStep& step){ + std::string label = step.name; + std::replace(label.begin(), label.end(), '_', ' '); + return label; +} + +// Fails the step (fail_step() on the console). +[[noreturn]] void fail(RouteIO& io, const std::string& message){ + io.note(message, RouteNote::FAILURE); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "RouteIO::note() returned from a failure: " + message); +} + +// A trainer every pass of a row fights, by that row. +struct FoughtTrainer{ + std::string_view row; + std::string_view name; +}; +// 01.02's walk east along Route 203's row 18 crosses his sight down +// column 31 (route_203_trainer_sights()). +const FoughtTrainer FOUGHT_TRAINERS[] = { + {"01.02", "Youngster (above row 18)"}, +}; + +} // namespace + + +std::vector row_trainers(const StoryWorld& world, const std::string& row){ + std::vector sights = story_trainers(world); + std::erase_if(sights, [&row](const TrainerSight& sight){ + return std::any_of(std::begin(FOUGHT_TRAINERS), std::end(FOUGHT_TRAINERS), [&](const FoughtTrainer& fought){ + return row > std::string(fought.row) && sight.name == fought.name; + }); + }); + return sights; +} + +CenterEntry run_into_center( + StoryStepIO& io, bool run, + const std::function& inside, const std::function& line_up, + const std::string& label +){ + // Every way in settles before the walk to the nurse: the inside can + // still fade in whether or not the run saw the fade. + CenterEntry entry = CenterEntry::LINED_UP; + if (io.run_into_door(Direction::NORTH, run).any()){ + entry = CenterEntry::FADED; + }else if (inside()){ + entry = CenterEntry::INSIDE; + }else{ + line_up(); + } + io.settle(BUTTON_B, std::format("{}: into the Center", label), SettleKind::DOOR, ""); + return entry; +} + +TappedEnd tapped_end(const StoryWorld& world, const std::string& map, const TilePos& pos, const Pokemon::StoryLeg& leg){ + const size_t open = slide_to_wall(world, story_to_world(world, map, pos), leg.direction).tiles; + TappedEnd tapped{.end = pos, .moved = std::min(open, (size_t)leg.tiles)}; + for (size_t i = 0; i < tapped.moved; i++){ + tapped.end = tapped.end.next(leg.direction); + } + return tapped; +} + +std::vector story_route_tiles(const StoryWorld& world, const StoryStep& step, std::string_view key){ + if (!step.args.contains(std::string(key))){ + return {}; + } + const std::string& map = story_arg(step.args, "target").map; + std::vector tiles; + for (const std::vector& row : story_arg>>(step.args, std::string(key))){ + if (row.size() != 2 && row.size() != 4){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{}: a {} row is [x, y] or [x0, y0, x1, y1].", step.name, key)); + } + const size_t last = row.size() - 2; + for (int64_t y = row[1]; y <= row[last + 1]; y++){ + for (int64_t x = row[0]; x <= row[last]; x++){ + tiles.push_back(story_to_world(world, map, {(int)x, (int)y})); + } + } + } + return tiles; +} + +std::optional measured_ends(const Pose& from, const StoryStep& step){ + const StoryWorld* world = story_plan_world(from.map, from.map); + const auto& legs_list = story_arg>(step.args, "legs"); + if (world == nullptr || legs_list.empty()){ + return std::nullopt; + } + std::vector at = Pokemon::tiles_of(from); + for (const Pokemon::StoryLeg& leg : legs_list){ + std::vector next; + for (const TilePos& pos : at){ + const TilePos end = tapped_end(*world, from.map, pos, leg).end; + if (std::find(next.begin(), next.end(), end) == next.end()){ + next.push_back(end); + } + } + at = std::move(next); + } + return Pokemon::tile_pose(from.map, at, legs_list.back().direction); +} + +void StoryWalkSteps::begin_attempt(const std::string& row, const std::vector& steps){ + m_row = row; + // A row starts from its save: after the shoes' row, the save has them. + m_can_run = row > std::string(RUNNING_SHOES_ROW); + m_trainers.clear(); + m_warp = {}; + m_scene_next.clear(); + m_scene_open = false; + // A route right before a scene, or before a plain step onto the scene's + // trigger (the decomp's tile, which the console may have elsewhere). + const auto onto_trigger = [](const StoryStep& step){ + return step.kind == Pokemon::StoryStepKind::STEP && step.action.empty() && !step.pose_after; + }; + const auto route = [](const StoryStep& step){ + return step.kind == Pokemon::StoryStepKind::ROUTE; + }; + for (size_t c = 1; c < steps.size(); c++){ + if (steps[c].kind != Pokemon::StoryStepKind::MASH){ + continue; + } + if (route(steps[c - 1])){ + m_scene_next.insert(steps[c - 1].name); + } + if (c >= 2 && onto_trigger(steps[c - 1]) && route(steps[c - 2])){ + m_scene_next.insert(steps[c - 2].name); + } + } +} + +void StoryWalkSteps::turn(Direction direction){ + m_route.note(std::string("[Route] Turning ") + direction_name(direction) + ".", RouteNote::LOG); + m_route.press(route_turn_tap(direction)); +} + +void StoryWalkSteps::log(const std::string& text){ + m_route.note(text, RouteNote::LOG); +} + +Pose StoryWalkSteps::walk_to(const Pose& from, const Pose& goal, const StoryStep& step){ + const StoryWorld* maps = story_plan_world(from.map, goal.map); + if (maps == nullptr){ + fail(m_route, std::format("{}: no maps join {} and {}.", step.name, from.map, goal.map)); + } + const std::optional costs = story_route_costs(step.action); + if (!costs){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown route costs: " + step.action); + } + std::vector start; + for (const TilePos& pos : Pokemon::tiles_of(from)){ + start.push_back(story_to_world(*maps, from.map, pos)); + } + std::vector targets; + for (const TilePos& pos : Pokemon::tiles_of(goal)){ + targets.push_back(story_to_world(*maps, goal.map, pos)); + } + // The ones fought stay off m_trainers until the next attempt. + std::vector& sights = m_trainers.try_emplace(maps, row_trainers(*maps, m_row)).first->second; + RouteTrainers trainers{ + sights, + [this](const std::string& trainer){ return m_io.fight(trainer); } + }; + const RouteWalkPlan plan{ + [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, + label_of(step), story_route_tiles(*maps, step, "avoid"), *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK, m_scene_next.contains(step.name), + step.action == ROUTE_ONTO_TRIGGER, story_route_tiles(*maps, step, "greeter"), + step.args.contains("leave") ? std::optional(story_arg(step.args, "leave")) : std::nullopt, + }; + RouteEnd end = walk_route_to_warp(m_route, *maps, start, trainers, plan); + sights = std::move(trainers.sights); + m_warp = std::move(end.warp); + m_scene_open = end.scene; + std::vector local; + for (const TilePos& pos : end.tiles){ + const std::optional tile = story_to_local(*maps, goal.map, pos); + if (!tile){ + fail(m_route, std::format("{}: the walk ended off {}.", step.name, goal.map)); + } + local.push_back(*tile); + } + return Pokemon::tile_pose(goal.map, local, Pokemon::facing_of(goal)); +} + +Outcome StoryWalkSteps::route(RunState& state, const StoryStep& step){ + const EventTrace trace(m_legs, m_row, step.name); + // A route before this one left the room through its door (RouteWalkPlan:: + // leave_toward): the way here is walked, and the step after the routes + // takes the warp as done. + if (step.args.contains("leave") && m_warp.any()){ + log(std::format("[{}] {}: already through the door the walk before left by.", m_row, step.name)); + return Pokemon::outcome_ok(state.pose); + } + m_warp = {}; + if (Pokemon::tiles_of(state.pose).empty()){ + return Pokemon::outcome_failed("no_pose", Pokemon::to_string(state.pose)); + } + const GridPose& target = story_arg(step.args, "target"); + const Pose end = walk_to(state.pose, Pokemon::pose_of(target), step); + const auto legs_list = step.args.contains("legs") ? story_arg>(step.args, "legs") : std::vector{}; + // The next step's scene started on the way: its step plays it. + if (legs_list.empty() || m_scene_open){ + return Pokemon::outcome_ok(end); + } + return run_to_dialogue(step); +} + +// The run of a ROUTE step with a leg, after its route: on toward the leg's +// direction until a dialogue starts, wherever on the run it starts (a +// scene's trigger: r35/r36 raced Dawn's to an exact tile and banner), for +// the leg's tiles at most. A wild battle on the way is run from and the run +// goes on. No dialogue fails the step. The scene's lines are the next step's. +// The leg's tiles are timed at the slowest pace of the player's speed (a +// walk before the Running Shoes): a run past the trigger's tile is ended by +// its dialogue anyway, where one cut short fails (lab-B 2026-11-09, 00.03). +Outcome StoryWalkSteps::run_to_dialogue(const StoryStep& step){ + const auto& legs_list = story_arg>(step.args, "legs"); + const Pokemon::StoryLeg& leg = legs_list.front(); + const MoveSpeed speed = m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK; + m_legs.begin_leg({.direction = leg.direction, .tiles = (size_t)leg.tiles, .end_kind = Pokemon::LegEndKind::OPEN}); + const WalkResult walk = m_route.walk_to_wall({ + .direction = leg.direction, + .limit = route_limit_for_tiles(speed, leg.tiles), + .label = label_of(step), + .options = {.arrived = nullptr, .pace = m_can_run ? DpadPace::RUN : DpadPace::WALK}, + }); + log(std::format("[{}] {}: ran {} to a dialogue: {} moved chunks, {} tiles at most, {}, {} wild battle(s).", m_row, step.name, + direction_name(leg.direction), walk.moved_chunks, leg.tiles, walk_stop_name(walk.stop), walk.wild_battles)); + m_legs.end_leg({}, walk.stop == WalkStop::DIALOG ? Pokemon::LegEndKnown::DIALOGUE : Pokemon::LegEndKnown::UNKNOWN); + if (walk.stop == WalkStop::DIALOG){ + return Pokemon::outcome_ok(); + } + // A scene can lock us before its first line: Dawn walks over first (r37). + if (dialog_within(m_route, 10000ms, COLOR_CYAN)){ + return Pokemon::outcome_ok(); + } + return Pokemon::outcome_failed("no_dialogue", std::format("{}: no dialogue in {} tiles {}", step.name, leg.tiles, direction_name(leg.direction))); +} + +// A LEGS step "to_map": runs its one leg until the location banner of +// pose_after's map shows, wherever it shows. A straight run along an open +// row needs no tile count (r41, r42: Sandgem's row 11 took us into the lab). +Outcome StoryWalkSteps::leg_to_map(const StoryStep& step){ + const auto& legs_list = story_arg>(step.args, "legs"); + const Pokemon::StoryLeg& leg = legs_list.front(); + const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; + turn(leg.direction); + const bool arrived = m_io.run_until_map(leg.direction, Milliseconds(250) * leg.tiles + 2000ms, map, step.name); + return arrived ? Pokemon::outcome_ok() : Pokemon::outcome_failed("no_banner", map); +} + +namespace{ +// Taps toward a door with no table door ahead of it. +constexpr size_t TAPS_TO_BANNER = 30; +// How many taps `direction` a tap step from `from` takes at most: the +// tiles to the door its map has ahead of every tile of `from`, and two +// more; TAPS_TO_BANNER where the map has none. A step through already, +// its banner gone, then stops a few tiles on (lab-B 2026-11-08: 01.05's +// walk slid into the Oreburgh Gate, whose row 22 runs 24 tiles on). +size_t taps_to_banner(const Pose& from, Direction direction){ + size_t most = 0; + try{ + const Pokemon::MapData& map = story_map(from.map); + for (const TilePos& pos : Pokemon::tiles_of(from)){ + const size_t tiles = Pokemon::tiles_to_door(map, pos, direction); + TilePos door = pos; + for (size_t i = 0; i < tiles; i++){ + door = door.next(direction); + } + if (tiles == 0 || map.tiles.at(door) != Pokemon::TileKind::DOOR){ + return TAPS_TO_BANNER; + } + most = std::max(most, tiles); + } + }catch (...){ + return TAPS_TO_BANNER; + } + return most == 0 ? TAPS_TO_BANNER : most + 2; +} +} // namespace + +// A STEP "to_banner": D-pad taps toward a door or cave mouth until the fade +// and pose_after's map's location banner. No banner fails the step right +// there; a banner before any tap means the walk before took us through. +Outcome StoryWalkSteps::tap_to_banner(const Pose& from, const StoryStep& step){ + const Direction direction = story_arg(step.args, "direction"); + const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; + // After the way through, the far side may still fade in, and a walk + // then goes nowhere (lab-B 2026-11-10, 00.04b's Center): settle, as + // after a door. + if (through_already(step)){ + m_io.settle(BUTTON_B, label_of(step), SettleKind::DOOR, ""); + return Pokemon::outcome_ok(); + } + const size_t limit = taps_to_banner(from, direction); + const std::optional taps = m_io.tap_to_banner(direction, map, limit); + if (!taps){ + return Pokemon::outcome_failed("no_banner", std::format("no fade and no {} banner in {} taps {}", + map, limit, direction_name(direction))); + } + if (*taps == 0){ + log(std::format("[{}] {}: {}'s banner before any tap: through already.", m_row, step.name, map)); + }else{ + log(std::format("[{}] {}: through to {} after {} taps {}.", m_row, step.name, map, *taps, direction_name(direction))); + } + m_io.settle(BUTTON_B, label_of(step), SettleKind::DOOR, ""); + return Pokemon::outcome_ok(); +} + + +// A STEP "or_legs" whose run into the door saw nothing (`seen`): the run +// stopped beside the door's column, against the wall it is in. The step's +// legs, a tap a tile, put us on the door's tile, then the run goes again +// (00.03: from (5, 10) east onto the mat (6, 10), then south). A tap onto +// the tile that is itself the way through ends it. `seen` itself for +// another variant, or when the run saw the warp. +WarpSeen StoryWalkSteps::door_from_beside(const StoryStep& step, const WarpSeen& seen, Milliseconds timeout, const std::string& banner){ + if (seen.any() || step.action != "or_legs"){ + return seen; + } + const auto& legs_list = story_arg>(step.args, "legs"); + for (const Pokemon::StoryLeg& leg : legs_list){ + turn(leg.direction); + const WalkResult walk = m_route.walk_taps(leg.direction, (size_t)leg.tiles, step.name); + log(std::format("[{}] {}: no fade; {} {} taps {}, {}.", m_row, step.name, leg.tiles, + direction_name(leg.direction), walk.moved_chunks, walk_stop_name(walk.stop))); + if (walk.warp.any()){ + return walk.warp; + } + } + const Direction direction = story_arg(step.args, "direction"); + const WarpSeen again = m_io.run_into_door(direction, m_can_run, timeout, Milliseconds(0), banner); + log(std::format("[{}] {}: {} through the door again: {}.", m_row, step.name, + direction_name(direction), again.any() ? describe(again) : "nothing yet")); + return again; +} + +// The route before ran on through this step's warp (walk_route_to_warp()): +// the step is done, unless the banner it read names another map. +bool StoryWalkSteps::through_already(const StoryStep& step){ + const WarpSeen through = std::exchange(m_warp, {}); + if (!through.any()){ + return false; + } + const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; + if (!through.banner.empty() && !location_banner_matches(through.banner, story_banner_name(map))){ + fail(m_route, std::format("{}: the walk before went through a warp to \"{}\", not {}.", step.name, through.banner, map)); + } + log(std::format("[{}] {}: through already; the walk before saw {}.", m_row, step.name, describe(through))); + return true; +} + +namespace{ +// A run into a door: how long to hold, and for how long of it a dark screen +// is not yet the door's fade (run_into_door()). +struct DoorRun{ + Milliseconds timeout; + Milliseconds dark_from; +}; +// Dark before this share of the run's time to the door is not the door: +// lab-B 2026-11-08 09:27:33 read dark at 1.8 s of the 4050 ms run down the +// mine's entrance hall (3250 ms to the mouth), stopped there, and the next +// step walked on through the mouth. +constexpr double DOOR_DARK_FROM_SHARE = 0.85; +// The shortest hold: the one-tile step onto the mine's warp faded 3.35 s +// after its hold began (lab-B 2026-11-07 12:19 and 12:27); a 1500 ms hold +// gave up just before the fade (2026-11-08 09:27:16) and settle() pressed B +// 15 s before it called the overworld calm. +constexpr Milliseconds DOOR_MIN_HOLD = 4500ms; +DoorRun door_run(const std::string& map_name, const Pose& pose, Direction direction, bool can_run){ + try{ + const Pokemon::MapData& map = story_map(map_name); + const auto tiles_list = Pokemon::tiles_of(pose); + if (!tiles_list.empty()){ + const size_t tiles = Pokemon::tiles_to_door(map, tiles_list.front(), direction); + if (tiles > 0){ + const Milliseconds per_tile = route_ms_per_tile(direction, can_run ? MoveSpeed::RUN : MoveSpeed::WALK); + const Milliseconds to_door = ROUTE_START_DELAY + per_tile * (int64_t)tiles; + return { + .timeout = std::max(DOOR_MIN_HOLD, to_door + 800ms), + .dark_from = std::chrono::duration_cast(to_door * DOOR_DARK_FROM_SHARE), + }; + } + } + }catch (...){} + return {.timeout = 12000ms, .dark_from = 0ms}; +} +enum class CountedEndPolicy{ EXACT, ONE_SHORT }; +std::vector counted_ends(const std::vector& starts, const Pokemon::StoryLeg& leg, CountedEndPolicy policy){ + std::vector ends; + for (TilePos at : starts){ + for (int i = 0; i < leg.tiles; i++){ + if (policy == CountedEndPolicy::ONE_SHORT && i + 1 == leg.tiles){ + ends.push_back(at); + } + at = at.next(leg.direction); + } + ends.push_back(at); + } + return ends; +} +} // namespace + +Pose StoryWalkSteps::recover_tapped_leg(const Pose& from, const std::vector& starts, + const std::vector& goals, Direction facing, const Pokemon::StoryLeg& leg, const StoryStep& step, + const std::optional& moved){ + const StoryWorld* world = story_plan_world(from.map, from.map); + if (world == nullptr){ + fail(m_route, "interrupted leg has no recovery world: " + from.map); + } + std::vector global; + for (const TilePos& at : starts){ + global.push_back(story_to_world(*world, from.map, at)); + } + std::vector possible; + // Placed by the leg's screen events: a tile or so from where the taps + // left us. Else anywhere along the leg. + const std::vector along = moved + ? tiles_moved(*world, global, leg.direction, *moved) + : interrupted_leg_positions(*world, global, leg.direction, (size_t)leg.tiles); + for (const TilePos& at : along){ + const auto local = story_to_local(*world, from.map, at); + if (!local){ + fail(m_route, "interrupted leg crossed an unconfirmed map transition"); + } + possible.push_back(*local); + } + StoryStep recovery = step; + recovery.name += "_recovery"; + recovery.action.clear(); + const Pose found = walk_to(Pokemon::tile_pose(from.map, possible), Pokemon::tile_pose(from.map, goals, facing), recovery); + // The route back can approach from another direction. Establish the + // step's facing before publishing that pose to the story runner. + turn(facing); + return found; +} + +// Counted legs retain their normal path. An observed encounter invalidates +// the exact pose; localize from every legal prefix and re-plan to the end of +// the step, skipping the legs left. A battle that may have begun before +// the leg (WalkOrigin::MAY_PRECEDE_LEG) found us on the tile the leg before +// ended on, since a battle starts on a tile stepped onto: the same prefixes +// hold. A re-plan to the end of the interrupted +// leg ended on an open tile, its last leg overshot a row, and the taps after +// went on a row off (lab-B 2026-11-08 11:55); a step that ends in a corner +// ends its re-plan on wall clips. +Outcome StoryWalkSteps::tap_legs(const Pose& from, const StoryStep& step){ + const CountedEndPolicy policy = step.action == "one_short_ok" ? CountedEndPolicy::ONE_SHORT : CountedEndPolicy::EXACT; + const bool one_short_ok = policy == CountedEndPolicy::ONE_SHORT; + std::vector at = Pokemon::tiles_of(from); + const auto& legs_list = story_arg>(step.args, "legs"); + for (auto leg = legs_list.begin(); leg != legs_list.end(); ++leg){ + turn(leg->direction); + const size_t tiles = (size_t)leg->tiles; + const auto next = counted_ends(at, *leg, policy); + m_legs.begin_leg({.direction = leg->direction, .tiles = tiles, .end_kind = Pokemon::LegEndKind::TILE, + .ends = traced_tiles(from.map, next)}); + const WalkResult walk = m_route.walk_taps(leg->direction, tiles, step.name); + log(std::format("[{}] {}: taps {} moved {} of {} tiles.", m_row, step.name, direction_name(leg->direction), walk.moved_chunks, tiles)); + if (walk.wild_battles > 0){ + if (walk.stop != WalkStop::LIMIT && walk.stop != WalkStop::BLOCKED){ + return Pokemon::outcome_failed("interrupted_leg_unconfirmed", step.name); + } + const LegBattlePlace place = place_leg_battle(walk, tiles); + m_legs.end_leg({}, Pokemon::LegEndKnown::UNKNOWN, "recovery"); + m_route.note(std::format("[{}] {}", m_row, leg_battle_line(step.name, walk, place)), RouteNote::LOG); + std::vector ends = next; + for (auto rest = std::next(leg); rest != legs_list.end(); ++rest){ + ends = counted_ends(ends, *rest, policy); + } + return Pokemon::outcome_ok(recover_tapped_leg(from, at, ends, legs_list.back().direction, *leg, step, place.moved)); + } + if (walk.moved_chunks + (one_short_ok ? 1 : 0) < tiles){ + return Pokemon::outcome_failed("short_leg", std::format("{} {} moved {}", direction_name(leg->direction), tiles, walk.moved_chunks)); + } + at = next; + m_legs.end_leg(traced_tiles(from.map, at), Pokemon::LegEndKnown::COUNTED); + } + return Pokemon::outcome_ok(Pokemon::tile_pose(from.map, at, legs_list.back().direction)); +} + +// A LEGS step "measured": each leg tapped, its taps that moved counted +// against the map's from each tile the player may stand on; the tiles +// whose count matches are where we are. A wall a tap bumps tells us +// where we are, where a fixed leg from a guessed tile walks on astray +// (00.03, lab-B 2026-11-09: a row low, the taps crossed the door's mat). +Outcome StoryWalkSteps::measured_legs(const Pose& from, const StoryStep& step){ + const StoryWorld* world = story_plan_world(from.map, from.map); + std::vector at = Pokemon::tiles_of(from); + if (world == nullptr || at.empty()){ + return Pokemon::outcome_failed("measured", "no map or tiles: " + Pokemon::to_string(from)); + } + const auto& legs_list = story_arg>(step.args, "legs"); + for (const Pokemon::StoryLeg& leg : legs_list){ + turn(leg.direction); + const size_t tiles = (size_t)leg.tiles; + m_legs.begin_leg({.direction = leg.direction, .tiles = tiles, .end_kind = Pokemon::LegEndKind::TILE}); + const WalkResult walk = m_route.walk_taps(leg.direction, tiles, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_leg_unconfirmed", step.name); + } + std::vector next; + for (const TilePos& pos : at){ + const TappedEnd tapped = tapped_end(*world, from.map, pos, leg); + if (tapped.moved == walk.moved_chunks){ + next.push_back(tapped.end); + } + } + log(std::format("[{}] {}: taps {} moved {} of {}: {} of {} tiles fit.", m_row, step.name, + direction_name(leg.direction), walk.moved_chunks, tiles, next.size(), at.size())); + if (next.empty()){ + return Pokemon::outcome_failed("measured", std::format("{} moved {}: no tile of {} fits", + direction_name(leg.direction), walk.moved_chunks, Pokemon::to_string(from))); + } + at = std::move(next); + m_legs.end_leg(traced_tiles(from.map, at), Pokemon::LegEndKnown::COUNTED); + } + return Pokemon::outcome_ok(Pokemon::tile_pose(from.map, at, legs_list.back().direction)); +} + +// A LEGS step "counted_wall": its one leg held to the wall, its tiles +// counted against the map's from each tile the table may have us on +// (walk_counted_wall_leg()). The leg's tiles are the table's reading of +// the map, and must be among them. +Outcome StoryWalkSteps::counted_wall(const Pose& from, const StoryStep& step){ + const auto& legs_list = story_arg>(step.args, "legs"); + const StoryWorld* world = story_plan_world(from.map, from.map); + const std::vector starts = Pokemon::tiles_of(from); + if (legs_list.size() != 1 || world == nullptr || starts.empty()){ + return Pokemon::outcome_failed("counted_wall", std::format("{} legs from {}", legs_list.size(), Pokemon::to_string(from))); + } + const Pokemon::StoryLeg& leg = legs_list.front(); + size_t fewest = SIZE_MAX; + size_t most = 0; + for (const TilePos& pos : starts){ + const size_t tiles = slide_to_wall(*world, story_to_world(*world, from.map, pos), leg.direction).tiles; + fewest = std::min(fewest, tiles); + most = std::max(most, tiles); + } + if ((size_t)leg.tiles < fewest || (size_t)leg.tiles > most){ + return Pokemon::outcome_failed("map_mismatch", + std::format("{} {} where the map has {} to {}", direction_name(leg.direction), leg.tiles, fewest, most)); + } + turn(leg.direction); + m_legs.begin_leg({.direction = leg.direction, .tiles = (size_t)leg.tiles, .end_kind = Pokemon::LegEndKind::WALL}); + walk_counted_wall_leg(m_route, {.direction = leg.direction, .fewest = fewest, .most = most, + .speed = m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK, .label = label_of(step)}); + m_legs.end_leg({}, Pokemon::LegEndKnown::WALL); + return Pokemon::outcome_ok(); +} + +Outcome StoryWalkSteps::legs(RunState& state, const StoryStep& step){ + const EventTrace trace(m_legs, m_row, step.name); + m_warp = {}; + m_scene_open = false; + if (step.action == "counted_wall"){ + return counted_wall(state.pose, step); + } + if (step.action == "measured"){ + return measured_legs(state.pose, step); + } + if (step.action == "to_wall"){ + const auto& legs_list = story_arg>(step.args, "legs"); + // The walls end these legs; the step's pose_after says where. + for (const Pokemon::StoryLeg& leg : legs_list){ + m_legs.begin_leg({.direction = leg.direction, .tiles = (size_t)leg.tiles, .end_kind = Pokemon::LegEndKind::WALL}); + m_io.walk_until_stopped(leg.direction, (size_t)leg.tiles, label_of(step)); + m_legs.end_leg({}, Pokemon::LegEndKnown::WALL); + } + return Pokemon::outcome_ok(); + } + if (step.action == "to_map"){ + return leg_to_map(step); + } + if (step.action == "to_dialogue"){ + return run_to_dialogue(step); + } + if (step.action != "one_short_ok" && !step.action.empty() && step.action != "legs"){ + return Pokemon::outcome_failed("unknown_action", step.action); + } + return tap_legs(state.pose, step); +} + +Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ + const EventTrace trace(m_legs, m_row, step.name); + const Direction direction = story_arg(step.args, "direction"); + if (step.action == "to_banner"){ + turn(direction); + return tap_to_banner(state.pose, step); + } + if (step.action == "tap"){ + // No separate turn tap first: at 1 tile from the door, a turn tap + // steps onto the warp, causing walk_taps to take an extra step on the far map. + const WalkResult walk = m_route.walk_taps(direction, 1, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_step", step.name); + } + m_io.settle(BUTTON_B, label_of(step), SettleKind::TALK, ""); + return Pokemon::outcome_ok(); + } + // A step through a door: walk (or run) on until the screen goes dark, + // from next to the door or from anywhere in its line (r43: tapping 8 tiles + // down the lab took 7 s), then settle for any scene on arrival (Mom's, + // 00.01; Dawn's tour, 00.04). Without the fade, settle must see it itself + // (r33 was outside, "no fade"). No separate turn tap first: at 1 tile + // from the door, a 50 ms turn tap can step in before run_into_door() watches. + // A fade or the far map's banner is the way through, either one (#268 + // slice 1); with neither yet, settle() waits for one. + if (step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map){ + // A "door" step has no text on the far side: its settle is the door's + // (the fade back up and a short still spell), not B through a cave's + // ambient motion for 15 s (lab-B 2026-11-12 21:57, 01.03's mine). + const SettleKind arrived = step.action == "door" ? SettleKind::DOOR : SettleKind::TALK; + if (through_already(step)){ + m_io.settle(BUTTON_B, label_of(step), arrived, ""); + return Pokemon::outcome_ok(); + } + const std::string banner = story_arrival_banner(state.pose.map, Pokemon::grid_pose_of(*step.pose_after).map); + const DoorRun door = door_run(state.pose.map, state.pose, direction, m_can_run); + WarpSeen seen = m_io.run_into_door(direction, m_can_run, door.timeout, door.dark_from, banner); + log(std::format("[{}] {}: {} through the door (timeout {} ms, dark from {} ms): {}.", m_row, step.name, + direction_name(direction), door.timeout.count(), door.dark_from.count(), seen.any() ? describe(seen) : "nothing yet")); + seen = door_from_beside(step, seen, door.timeout, banner); + m_io.settle(BUTTON_B, label_of(step), seen.any() ? arrived : SettleKind::AFTER_WARP, banner); + return Pokemon::outcome_ok(); + } + return one_step(state, step); +} + +// A plain D-pad step: onto a scene's trigger, mostly. Not taken when the +// route before ran into that scene already (RouteEnd::scene): it holds us +// where it started. +Outcome StoryWalkSteps::one_step(const RunState& state, const StoryStep& step){ + if (std::exchange(m_scene_open, false)){ + log(std::format("[{}] {}: its scene started on the walk before; no step.", m_row, step.name)); + return Pokemon::outcome_ok(state.pose); + } + const Direction direction = story_arg(step.args, "direction"); + turn(direction); + const WalkResult walk = m_route.walk_taps(direction, 1, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_step", step.name); + } + log(std::format("[{}] {}: a step {}, {}.", m_row, step.name, direction_name(direction), walk_stop_name(walk.stop))); + std::vector at; + for (const TilePos& pos : Pokemon::tiles_of(state.pose)){ + at.push_back(pos.next(direction)); + } + return Pokemon::outcome_ok(Pokemon::tile_pose(state.pose.map, at, direction)); +} + +Outcome StoryWalkSteps::talk(RunState& state, const StoryStep& step){ + m_warp = {}; + m_scene_open = false; + // Face whom we talk to: a route ends facing its last leg, not its goal + // (r40 reached Mom facing north). They block the tile, so the tap only turns. + if (const std::optional facing = Pokemon::facing_of(state.pose)){ + turn(*facing); + } + m_route.press({.kind = RouteInputKind::BUTTON_A, .direction = Direction::NORTH, .hold = 80ms, .release = 200ms, + .count = 1, .end = RouteInputEnd::DONE}); + // Roark's first line in the mine comes after the camera zooms in on + // us, which can take some seconds (r98; the old event waited 8 s). + if (!dialog_within(m_route, 8000ms, COLOR_YELLOW)){ + fail(m_route, label_of(step) + ": nobody answered."); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryWalkSteps::mash(RunState& state, const StoryStep& step){ + m_warp = {}; + m_scene_open = false; + // The step before may have played the scene already (a door step settles + // its arrival scene), so its first line is not required. + if (!dialog_within(m_route, 15000ms, COLOR_CYAN)){ + m_route.note(label_of(step) + ": no line within 15 s; the step before played it.", RouteNote::WARNING); + } + m_io.settle(step.action == "a" ? BUTTON_A : BUTTON_B, label_of(step), + step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map && + story_plan_world(state.pose.map, Pokemon::grid_pose_of(*step.pose_after).map) == nullptr ? SettleKind::AFTER_WARP : SettleKind::TALK, ""); + if (m_row == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP){ + m_can_run = true; + } + return Pokemon::outcome_ok(); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h new file mode 100644 index 0000000000..194c9db7cd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -0,0 +1,158 @@ +/* BDSP AutoStory - Story walk steps + * + * From: https://github.com/PokemonAutomation/ + * + * The story steps that walk and talk (ROUTE, LEGS, STEP, TALK, MASH), on a + * StoryStepIO: StoryExecutor runs them on the console, the story's + * simulation tests on a simulated player. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryWalkSteps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryWalkSteps_H + +#include +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryEvent.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Pokemon_StoryPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegTraceIO.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// How a run north into a Pokemon Center's door got us in. +enum class CenterEntry{ + FADED, // its fade showed + INSIDE, // no fade, but the inside shows: the run went on in, as + // far as the nurse's counter (lab-B 2026-11-08, Oreburgh) + LINED_UP, // no fade and no inside: lined up with the lit doorway +}; + +// Runs north into the Pokemon Center's door ahead, running with `run`. +// After its fade, waits until the inside is a quiet overworld (settle()): +// a walk while it still fades in goes nowhere (lab-B 2026-11-10, 00.04b). +// Without a fade, `inside` says whether the Center's inside shows, and if +// not, `line_up` lines up with the lit doorway and walks in (or fails). +CenterEntry run_into_center( + StoryStepIO& io, bool run, + const std::function& inside, const std::function& line_up, + const std::string& label +); + +// Where a leg's taps end, and how many of them moved. +struct TappedEnd{ + Pokemon::TilePos end; + size_t moved = 0; +}; +// Where `leg`'s taps from `pos` (a tile of `map`) end on `world`: each tap +// moves a tile until a wall stops them. +TappedEnd tapped_end(const StoryWorld& world, const std::string& map, const Pokemon::TilePos& pos, const Pokemon::StoryLeg& leg); +// The tiles a "measured" LEGS step may end on from `from`, one a tile of +// it; the step measures which. Nothing if `from`'s map has no world. +std::optional measured_ends(const Pokemon::Pose& from, const Pokemon::StoryStep& step); +// The trainers of `world` who can see us in row `row`: story_trainers(), +// less those an earlier row fights, whom its save and every later one's +// keep beaten. +std::vector row_trainers(const StoryWorld& world, const std::string& row); +// The world tiles a ROUTE step's `key` rows name on its target's map, +// [x, y] a tile and [x0, y0, x1, y1] a box. "avoid": ground the maps leave +// open that the walk keeps off (BDSP's Global Wonder Station in Jubilife's +// plaza, which pret's Diamond map lacks). "greeter": where someone walks up +// and talks (RouteWalkPlan::greeter). None without the arg. Throws +// InternalProgramError for a row of another length. +std::vector story_route_tiles(const StoryWorld& world, const Pokemon::StoryStep& step, std::string_view key); + + +// Executes walking and movement-related story steps via StoryStepIO. +class StoryWalkSteps{ +public: + explicit StoryWalkSteps(StoryStepIO& io) + : m_io(io) + , m_legs(legs_to_log(io.route())) + , m_route(io.route(), m_legs) + {} + + // A new attempt of the row `row`: every trainer can see us again, and + // the pace is the row's save's (a run after the Running Shoes). With + // the row's `steps`, a route followed by a scene, or by a plain step onto + // the scene's trigger, hands a dialogue that starts on the way to that + // scene (RouteWalkPlan::scene_next). + void begin_attempt(const std::string& row, const std::vector& steps = {}); + // Whether the player has the Running Shoes: the pace routes are timed at. + bool can_run() const{ + return m_can_run; + } + + // Walks the planned route to `step.target`. Variant: the plan's costs. + // With a leg, then runs on toward its direction until a dialogue starts, + // for its tiles at most (run_to_dialogue()). + Pokemon::Outcome route(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // See StoryExecutor::legs(). + Pokemon::Outcome legs(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // One D-pad step, or a run through a door when the step changes map. + Pokemon::Outcome step(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // A at what we face; a dialogue must open. + Pokemon::Outcome talk(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // Text to a quiet overworld; the Running Shoes' scene gives the shoes. + Pokemon::Outcome mash(Pokemon::RunState& state, const Pokemon::StoryStep& step); + +private: + Pokemon::Pose walk_to(const Pokemon::Pose& from, const Pokemon::Pose& goal, const Pokemon::StoryStep& step); + // After a wild battle on `leg`'s taps from `starts`: walks to one of + // `goals` from wherever the leg could have left us, then faces `facing`. + Pokemon::Pose recover_tapped_leg(const Pokemon::Pose& from, + const std::vector& starts, const std::vector& goals, + Pokemon::Direction facing, const Pokemon::StoryLeg& leg, const Pokemon::StoryStep& step, + const std::optional& moved); + Pokemon::Outcome tap_legs(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + Pokemon::Outcome counted_wall(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + Pokemon::Outcome measured_legs(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + Pokemon::Outcome run_to_dialogue(const Pokemon::StoryStep& step); + Pokemon::Outcome leg_to_map(const Pokemon::StoryStep& step); + Pokemon::Outcome one_step(const Pokemon::RunState& state, const Pokemon::StoryStep& step); + Pokemon::Outcome tap_to_banner(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + bool through_already(const Pokemon::StoryStep& step); + WarpSeen door_from_beside(const Pokemon::StoryStep& step, const WarpSeen& seen, Milliseconds timeout, const std::string& banner); + void turn(Pokemon::Direction direction); + void log(const std::string& text); + + StoryStepIO& m_io; + // The leg records of the walks, written to m_io's route log. + Pokemon::LegTracer m_legs; + // m_io's route walker console, telling m_legs of each leg walked. + LegTraceRouteIO m_route; + std::string m_row; + bool m_can_run = false; + // The trainers who can still see us on each story_plan_world() world, + // in its tiles, from its first walk this attempt; all of them again at + // each attempt. + std::map> m_trainers; + // The warp the last route ran on through at its end (walk_route_to_warp()), + // for the step after it, which goes through that warp. + WarpSeen m_warp; + // The row's steps followed by a scene (begin_attempt()). + std::set m_scene_next; + // The last route ended in the scene of the step after it, or after the + // plain step onto its trigger (RouteEnd::scene); that step is not taken. + bool m_scene_open = false; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp new file mode 100644 index 0000000000..51c5fb16cd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp @@ -0,0 +1,364 @@ +/* BDSP AutoStory Story World Tests + * + * From: https://github.com/PokemonAutomation/ + * + * StoryWorld on small synthetic maps: what each tile does to a step, maps + * joined at offsets, and tiles blocked or taken off the maps. Also the + * story's own worlds: their connected maps step across every map edge + * where the world grid does. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryWorld_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::MapData; +using Pokemon::TilePos; + +// A map of these rows, with a person standing still on each `standing` +// tile and one wandering on each `wandering` tile. +MapData make_map( + const std::vector& rows, + const std::vector& standing = {}, + const std::vector& wandering = {} +){ + MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{(int)rows[0].size(), (int)rows.size(), {}}; + for (const std::string& row : rows){ + for (char glyph : row){ + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(glyph)); + } + } + for (const TilePos& pos : standing){ + map.objects.push_back({ + .name = "standing", .kind = Pokemon::ObjectKind::NPC, .x = pos.x, .y = pos.y, + .facing = std::nullopt, .wander = std::nullopt, + .trainer = std::nullopt, .hide_flag = std::nullopt, + }); + } + for (const TilePos& pos : wandering){ + map.objects.push_back({ + .name = "wandering", .kind = Pokemon::ObjectKind::NPC, .x = pos.x, .y = pos.y, + .facing = std::nullopt, .wander = Pokemon::TileBox{pos.x, pos.y, pos.x, pos.y}, + .trainer = std::nullopt, .hide_flag = std::nullopt, + }); + } + return map; +} + +StoryWorld make_world(const MapData& map){ + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + + +// One step from (1, 1) of a 3 x 3 map whose middle tile (the step's +// target) is `target`, and whether the player is held at a wall there. +struct StepCase{ + std::string name; + std::vector rows; + std::vector standing = {}; + std::vector wandering = {}; + TilePos from = {}; + Direction direction = Direction::NORTH; + std::optional lands = std::nullopt; + bool at_wall = false; +}; + +UnitTestResult test_story_world_steps(Logger&){ + const std::vector cases{ + {.name = "open ground", .rows = {".", "."}, .from = {0, 1}, .lands = TilePos{0, 0}}, + {.name = "tall grass", .rows = {"~", "."}, .from = {0, 1}, .lands = TilePos{0, 0}}, + {.name = "a wall", .rows = {"#", "."}, .from = {0, 1}, .at_wall = true}, + {.name = "water", .rows = {"w", "."}, .from = {0, 1}, .at_wall = true}, + {.name = "a sign", .rows = {"S", "."}, .from = {0, 1}, .at_wall = true}, + {.name = "a door is not a wall", .rows = {"D", "."}, .from = {0, 1}}, + {.name = "off the map is not a wall", .rows = {"."}, .from = {0, 0}}, + {.name = "a south ledge hops south", .rows = {".", "v", "."}, .direction = Direction::SOUTH, .lands = TilePos{0, 2}}, + {.name = "a south ledge stops north", .rows = {".", "v", "."}, .from = {0, 2}, .at_wall = true}, + {.name = "a west ledge hops west", .rows = {".<."}, .from = {2, 0}, .direction = Direction::WEST, .lands = TilePos{0, 0}}, + {.name = "a west ledge stops east", .rows = {".<."}, .direction = Direction::EAST, .at_wall = true}, + {.name = "a ledge onto a wall stops", .rows = {".", "v", "#"}, .direction = Direction::SOUTH, .at_wall = true}, + {.name = "a person standing still is a wall", .rows = {".", "."}, .standing = {{0, 0}}, .from = {0, 1}, .at_wall = true}, + {.name = "a wanderer does not block", .rows = {".", "."}, .wandering = {{0, 0}}, .from = {0, 1}, .lands = TilePos{0, 0}}, + }; + for (const StepCase& item : cases){ + const StoryWorld world = make_world(make_map(item.rows, item.standing, item.wandering)); + const std::optional lands = step(world, item.from, item.direction); + if (lands != item.lands){ + return std::format("{}: lands on {}, expected {}.", item.name, + lands ? Pokemon::to_string(*lands) : "nothing", item.lands ? Pokemon::to_string(*item.lands) : "nothing"); + } + if (at_wall(world, item.from, item.direction) != item.at_wall){ + return std::format("{}: at_wall() is {}, expected {}.", item.name, !item.at_wall, item.at_wall); + } + } + { + // Mom talks when bumped (r29): she stops a walk, but is no wall. + MapData map = make_map({".", "."}, {{0, 0}}); + map.name = "PlayerHouse1F"; + map.objects[0].name = "mom_0"; + const StoryWorld world = make_world(map); + if (at_wall(world, {0, 1}, Direction::NORTH) || step(world, {0, 1}, Direction::NORTH).has_value()){ + return "Mom: no wall to stop against, and no step onto her."; + } + } + return true; +} + + +// Two maps joined: tiles of each at their offsets, the gap between them +// off the maps, and a shared wall border allowed where nothing else is. +UnitTestResult test_story_world_join(Logger&){ + StoryWorld world; + world.add("West", make_map({"##", ".#"}), {0, 0}); + world.add("East", make_map({"#.", "#~"}), {1, -1}); + const std::vector> described{ + {{0, 2}, "West (0, 1)"}, + {{2, 0}, "East (1, 0)"}, + {{1, 1}, "West (1, 0)"}, // the shared wall: the first map holds it + {{2, 2}, "world (2, 2)"}, + }; + for (const auto& [pos, text] : described){ + if (describe(world, pos) != text){ + return std::format("{}: described as \"{}\", expected \"{}\".", Pokemon::to_string(pos), describe(world, pos), text); + } + } + if (!covered(world, {0, 2}) || covered(world, {3, 0}) || covered(world, {0, 3}) || covered(world, {2, 2}) || !standable(world, {2, 1})){ + return std::string("The joined world is not 3 x 3 with East's grass at (2, 1) and nothing at (2, 2)."); + } + if (!(world.to_world("East", {0, 0}) == TilePos{1, 0})){ + return "East (0, 0) is world " + Pokemon::to_string(world.to_world("East", {0, 0})) + ", expected (1, 0)."; + } + try{ + world.add("Overlap", make_map({"."}), {0, 1}); + return std::string("Adding a map over open ground did not throw."); + }catch (InternalProgramError&){} + try{ + world.to_world("North", {0, 0}); + return std::string("to_world() of an unknown map did not throw."); + }catch (InternalProgramError&){} + return true; +} + + +// block() and uncover() change what a tile does, and survive a map added +// later that moves the world's origin. +UnitTestResult test_story_world_block_uncover(Logger&){ + StoryWorld world; + world.add("Road", make_map({"...."}), {0, 0}); + world.block({1, 0}); + world.uncover({2, 0}); + if (step(world, {0, 0}, Direction::EAST) || !at_wall(world, {0, 0}, Direction::EAST)){ + return std::string("A blocked tile is not a wall."); + } + if (covered(world, {2, 0}) || at_wall(world, {3, 0}, Direction::WEST)){ + return std::string("An uncovered tile is still on the map, or a wall."); + } + world.add("North", make_map({"#"}), {0, -1}); + const TilePos blocked = world.to_world("Road", {1, 0}); + const TilePos uncovered = world.to_world("Road", {2, 0}); + if (!world.occupied(blocked) || covered(world, uncovered) || !standable(world, world.to_world("Road", {3, 0}))){ + return std::string("Adding a map lost the blocked or uncovered tile."); + } + return true; +} + + +struct StoryWorldInput{ + std::string name; + std::function body; +}; + +struct StoryWorldExpectation{ +}; + +struct StoryWorldCase{ + StoryWorldInput input; + StoryWorldExpectation expectation; +}; + +class Test_StoryWorld : public UnitTest{ +public: + explicit Test_StoryWorld(StoryWorldCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Story world: " + 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: + StoryWorldCase m_case; +}; + +} // namespace + + +const StoryWorldCase STORY_WORLD_CASES[] = { + { + .input = {"what each tile does to a step", test_story_world_steps}, + .expectation = {}, + }, + { + .input = {"maps joined at offsets", test_story_world_join}, + .expectation = {}, + }, + { + .input = {"blocked and uncovered tiles", test_story_world_block_uncover}, + .expectation = {}, + }, +}; + +namespace{ + +// Whether stepping `direction` from `from` lands on the same map and tile +// on the world grid as on the connected maps (or nowhere on both). +bool steps_agree(const StoryWorld& world, const Pokemon::MapPlace& from, Direction direction){ + const Pokemon::ConnectedMaps connected = connected_maps(world); + const std::string& name = connected.map(from.map).name; + const std::optional grid = world.to_local(world.to_world(name, from.tile).next(direction)); + const std::optional place = connected.neighbour(from, direction); + if (grid && place){ + return grid->map == connected.map(place->map).name && grid->pos == place->tile; + } + return !grid && !place; +} + +// The first step off a map's edge, from any of its tiles, that lands +// somewhere else on the connected maps than on the world grid, or nothing. +std::optional edge_mismatch(const StoryWorld& world){ + const Pokemon::ConnectedMaps connected = connected_maps(world); + for (size_t index = 0; index < connected.size(); index++){ + const Pokemon::Grid& tiles = connected.map(index).tiles; + for (int cell = 0; cell < tiles.width * tiles.height; cell++){ + const TilePos from{cell % tiles.width, cell / tiles.width}; + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + if (tiles.contains(from.next(direction)) || steps_agree(world, {index, from}, direction)){ + continue; + } + return std::format("{} {} {}: the grid and the connected maps disagree.", + connected.map(index).name, Pokemon::to_string(from), Pokemon::direction_name(direction)); + } + } + } + return std::nullopt; +} + +struct ConnectedWorldInput{ + std::string_view name; + // The story's plan world between two places (story_plan_world()), or, + // if `world` is set, that world. + std::string_view plan_from = {}; + std::string_view plan_to = {}; + const StoryWorld& (*world)() = nullptr; +}; +struct ConnectedWorldExpectation{ + // Connections over all the maps: two for each pair that meets. + size_t connections = 0; +}; +struct ConnectedWorldCase{ + ConnectedWorldInput input; + ConnectedWorldExpectation expectation; +}; + +class Test_ConnectedStoryWorld : public UnitTest{ +public: + explicit Test_ConnectedStoryWorld(ConnectedWorldCase test_case) + : UnitTest(std::format("PokemonBDSP::AutoStory - Story world as connected maps: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ConnectedWorldInput& input = m_case.input; + const StoryWorld* found = input.world != nullptr + ? &input.world() + : story_plan_world(std::string(input.plan_from), std::string(input.plan_to)); + if (found == nullptr){ + return std::format("No story plan world from {} to {}.", input.plan_from, input.plan_to); + } + const StoryWorld& world = *found; + if (std::optional error = edge_mismatch(world)){ + return *error; + } + const Pokemon::ConnectedMaps connected = connected_maps(world); + size_t connections = 0; + for (size_t index = 0; index < connected.size(); index++){ + connections += connected.map(index).connections.size(); + } + if (connections != m_case.expectation.connections){ + return std::format("{} connections, expected {}.", connections, m_case.expectation.connections); + } + return true; + } + +private: + ConnectedWorldCase m_case; +}; + +const ConnectedWorldCase CONNECTED_WORLD_CASES[] = { + { + .input = {.name = "Route 201, Sandgem and Route 202", .world = story_maps}, + .expectation = {.connections = 4}, + }, + { + .input = {.name = "Twinleaf to Route 202", .plan_from = "TwinleafTown", .plan_to = "Route202"}, + .expectation = {.connections = 8}, + }, + { + .input = {.name = "Route 203 into Jubilife", .plan_from = "Route203", .plan_to = "JubilifeCity"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Jubilife up Route 204's south half", .plan_from = "JubilifeCity", .plan_to = "Route204South"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Route 204's north half into Floaroma", .plan_from = "Route204North", .plan_to = "FloaromaTown"}, + .expectation = {.connections = 2}, + }, + { + .input = {.name = "Jubilife alone", .world = jubilife_maps}, + .expectation = {.connections = 0}, + }, +}; + +} // namespace + +void add_tests_AutoStory_StoryWorld(UnitTestDatabase& database){ + for (const auto& item : STORY_WORLD_CASES){ + database.add(item); + } + for (const ConnectedWorldCase& item : CONNECTED_WORLD_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h new file mode 100644 index 0000000000..de4006d340 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h @@ -0,0 +1,24 @@ +/* BDSP AutoStory Story World Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryWorld_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryWorld_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Offline checks of the story world: tile rules, joins, blocked and uncovered tiles. +void add_tests_AutoStory_StoryWorld(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp new file mode 100644 index 0000000000..126aa0c968 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp @@ -0,0 +1,117 @@ +/* BDSP AutoStory - Table Routes + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "Utils/PokemonBDSP_AutoStory_RouteChoice.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" +#include "PokemonBDSP_AutoStory_TableRoutes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +namespace{ + +using Pokemon::story_arg; + +std::optional pose_after_step(const std::optional& at, const Pokemon::StoryStep& step){ + if (step.kind == StoryStepKind::ROUTE){ + return Pokemon::pose_of(story_arg(step.args, "target")); + } + if (step.pose_after){ + return Pokemon::pose_of(*step.pose_after); + } + return step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP ? std::nullopt : at; +} + +// A ROUTE step after a "measured" LEGS step plans from the one tile the +// taps measured: one TableRoute for each tile of `at`. +void add_measured_routes(std::vector& routes, const std::string& row, const Pokemon::StoryStep& step, const Pokemon::Pose& at){ + const Pokemon::Pose target = Pokemon::pose_of(story_arg(step.args, "target")); + for (const TilePos& pos : Pokemon::tiles_of(at)){ + const Pokemon::Pose one = Pokemon::tile_pose(at.map, {pos}, Pokemon::facing_of(at)); + routes.push_back({row, &step, one, story_plan_world(at.map, target.map)}); + } +} + +} // namespace + + +std::vector table_routes(){ + std::vector routes; + for (const StoryCheckpoint& row : story_checkpoints()){ + std::optional at = row.start ? std::optional(Pokemon::pose_of(*row.start)) : std::nullopt; + bool measured = false; + for (const Pokemon::StoryStep& step : row.steps){ + const bool known = at && !Pokemon::tiles_of(*at).empty(); + if (step.kind == StoryStepKind::ROUTE && known && std::exchange(measured, false)){ + add_measured_routes(routes, row.id, step, *at); + }else if (step.kind == StoryStepKind::ROUTE){ + const Pokemon::Pose target = Pokemon::pose_of(story_arg(step.args, "target")); + routes.push_back({row.id, &step, at, known ? story_plan_world(at->map, target.map) : nullptr}); + } + measured = false; + if (step.kind == StoryStepKind::LEGS && step.action == "measured" && known && !step.pose_after){ + at = measured_ends(*at, step); + measured = at.has_value(); + continue; + } + at = pose_after_step(at, step); + } + } + return routes; +} + +std::vector world_tiles(const StoryWorld& world, const Pokemon::Pose& pose){ + std::vector tiles; + for (const TilePos& pos : Pokemon::tiles_of(pose)){ + tiles.push_back(story_to_world(world, pose.map, pos)); + } + return tiles; +} + +RouteStepPlan plan_table_route(const TableRoute& route){ + // As walk_route() plans: the step's avoided tiles off the map, but + // where it starts, and a wall in a zone stays a wall (a leg that ends + // against it is a leg to the wall). + StoryWorld world = *route.world; + const std::vector from = world_tiles(world, *route.from); + for (const TilePos& pos : story_route_tiles(world, *route.step, "avoid")){ + if (world.tile(pos) != Pokemon::TileKind::WALL && std::find(from.begin(), from.end(), pos) == from.end()){ + world.uncover(pos); + } + } + const Pokemon::Pose target = Pokemon::pose_of(story_arg(route.step->args, "target")); + const std::vector targets = world_tiles(world, target); + const auto goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }; + RouteStepPlan plan{.start = from}; + const MoveSpeed speed = route.row > std::string(RUNNING_SHOES_ROW) ? MoveSpeed::RUN : MoveSpeed::WALK; + plan.legs = plan_fastest_route({ + .world = world, .from = plan.start, .goal = goal, .costs = *story_route_costs(route.step->action), .speed = speed, + .goal_is_trigger = route.step->action == ROUTE_ONTO_TRIGGER, + }); + if (!plan.legs){ + plan.legs = plan_recovered_route(world, plan.start, goal, RobustPlanCosts()); + plan.default_costs = plan.legs.has_value(); + } + return plan; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.h new file mode 100644 index 0000000000..57092298bf --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.h @@ -0,0 +1,64 @@ +/* BDSP AutoStory - Table Routes + * + * From: https://github.com/PokemonAutomation/ + * + * The story table's ROUTE steps, each from where the steps before it in + * its row leave the player, and the legs the program plans for them: what + * the route tests and the route bench check. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TableRoutes_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TableRoutes_H + +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A ROUTE step of the table, and where the steps before it in its row leave +// the player: the row's start, then each step's target or pose_after. A LEGS +// or STEP step that does not say where it ends leaves the start unknown. +struct TableRoute{ + std::string row; + const Pokemon::StoryStep* step; + std::optional from; + // The world the step is planned on; none if the start is unknown or no + // world joins its map and the target's. + const StoryWorld* world; +}; + +// Every ROUTE step of the story table, in table order. +std::vector table_routes(); + +// The world tiles of `pose` on `world`. +std::vector world_tiles(const StoryWorld& world, const Pokemon::Pose& pose); + +// The legs a table ROUTE step plans, as walk_route() plans them: the step's +// costs, then the default ones; no legs if neither plans. +struct RouteStepPlan{ + std::vector start; + std::optional> legs; + bool default_costs = false; // the step's own costs found no plan +}; + +// Plans `route`, which must have a world. +RouteStepPlan plan_table_route(const TableRoute& route); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp new file mode 100644 index 0000000000..44956f86c6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp @@ -0,0 +1,599 @@ +/* BDSP AutoStory Walk Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "Utils/PokemonBDSP_AutoStory_LegBattle.h" +#include "Utils/PokemonBDSP_AutoStory_Location.h" +#include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "Utils/PokemonBDSP_AutoStory_Walk.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" +#include "Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP_AutoStory_Walk_Tests.h" +#include "Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "Utils/PokemonBDSP_AutoStory_WalkScreen.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// A turn must not step: on the console D-pad taps of 40 to 80 ms only +// turned, a 160 ms D-pad tap stepped a whole tile, and a 100 ms stick tap +// stepped about 0.6 tile, off the tile grid (r49, r248a). So route_turn() +// taps the D-pad, once, for at most 80 ms. +UnitTestResult test_route_turn_tap(Logger&){ + constexpr Milliseconds LONGEST_TURN(80); + for (Pokemon::Direction direction : {Pokemon::Direction::NORTH, Pokemon::Direction::SOUTH, Pokemon::Direction::EAST, Pokemon::Direction::WEST}){ + const RouteInput tap = route_turn_tap(direction); + const std::string name = std::string("turning ") + Pokemon::direction_name(direction); + if (tap.kind != RouteInputKind::DPAD){ + return name + ": not a D-pad tap; a stick tap moves part of a tile."; + } + if (tap.direction != direction || tap.count != 1){ + return name + ": expected one tap that way."; + } + if (tap.hold > LONGEST_TURN){ + return name + ": a " + std::to_string(tap.hold.count()) + " ms tap; over 80 ms may step."; + } + } + if (ROUTE_TURN_TAP > LONGEST_TURN){ + return "ROUTE_TURN_TAP is " + std::to_string(ROUTE_TURN_TAP.count()) + " ms; over 80 ms may step."; + } + return true; +} + +// WarpFadeWatch on synthetic frames 33 ms apart, each a brightness and a +// texture (stddev a channel). Two scenes differ here when their +// brightness does. +struct FadeFrame{ + double brightness; + double stddev; +}; +struct WarpFadeCase{ + std::string name; + WarpFadeRule rule; + std::vector frames; + // The frame (1-based) the fade shows on, 0 for none, and how. + size_t seen_at; + WarpFade fade; +}; + +UnitTestResult test_warp_fade_watch(Logger&){ + constexpr WarpFadeRule DOOR{.partial_dark = true, .dark_for = Milliseconds(50), .white_for = Milliseconds(60)}; + constexpr WarpFadeRule TAP{}; + constexpr FadeFrame STREET{104, 20}; + constexpr FadeFrame ROOM{130, 20}; + constexpr FadeFrame FALL{55, 10}; + constexpr FadeFrame BLACK{7, 1}; + const std::vector cases{ + {.name = "a door's fade, its hold seen", .rule = DOOR, .frames = {STREET, FALL, BLACK, BLACK, BLACK, ROOM}, .seen_at = 4, .fade = WarpFade::BLACK}, + {.name = "a tap's fade, its hold seen", .rule = TAP, .frames = {STREET, FALL, BLACK, ROOM}, .seen_at = 3, .fade = WarpFade::BLACK}, + {.name = "a short fade, its hold between two frames", .rule = TAP, .frames = {STREET, FALL, ROOM}, .seen_at = 3, .fade = WarpFade::BLACK}, + {.name = "a door's short fade, its hold between two frames", .rule = DOOR, .frames = {STREET, FALL, ROOM}, .seen_at = 3, .fade = WarpFade::BLACK}, + {.name = "a dip back to the same street", .rule = TAP, .frames = {STREET, FALL, STREET}, .seen_at = 0, .fade = WarpFade::NONE}, + {.name = "a dim cave floor (46) is no fade", .rule = TAP, .frames = {STREET, {46, 20}, {46, 20}, {46, 20}}, .seen_at = 0, .fade = WarpFade::NONE}, + {.name = "a cave mouth's dim fade (55)", .rule = TAP, .frames = {STREET, {80, 10}, {55, 1}, ROOM}, .seen_at = 3, .fade = WarpFade::BLACK}, + {.name = "a cave exit's white, its hold between two frames", .rule = TAP, .frames = {STREET, {180, 10}, ROOM}, .seen_at = 3, .fade = WarpFade::WHITE}, + {.name = "a cave exit's white", .rule = DOOR, .frames = {STREET, {250, 1}, {250, 1}, {250, 1}}, .seen_at = 4, .fade = WarpFade::WHITE}, + }; + for (const WarpFadeCase& item : cases){ + WarpFadeWatch watch(item.rule, [](const Pokemon::TransitionSample& before, const Pokemon::TransitionSample& after){ + return after.middle.average.r != before.middle.average.r ? Pokemon::SceneChange::DIFFERENT : Pokemon::SceneChange::SAME; + }); + size_t seen_at = 0; + WarpFade fade = WarpFade::NONE; + for (size_t i = 0; i < item.frames.size() && seen_at == 0; i++){ + const FadeFrame& frame = item.frames[i]; + fade = watch.push({ + .time = WallClock() + Milliseconds(33 * (int64_t)i), + .middle = ImageStats(FloatPixel(frame.brightness, frame.brightness, frame.brightness), FloatPixel(frame.stddev, frame.stddev, frame.stddev), 1), + }); + seen_at = fade == WarpFade::NONE ? 0 : i + 1; + } + if (seen_at != item.seen_at || fade != item.fade){ + return item.name + ": " + warp_fade_name(fade) + " at frame " + std::to_string(seen_at) + ", expected " + + warp_fade_name(item.fade) + " at " + std::to_string(item.seen_at) + "."; + } + } + return true; +} + +// indoors() decides when leave_door_south() is out: r257 walked on south +// past Sandgem's road into a house after leaving the Center, because the +// evening tint made the outside as warm as a room. Synthetic screens: the +// evening outside (warm all over), a room (warm, black along the bottom), +// a fade (black all over), and warm with a thin dark edge (a shadow). +struct IndoorsCase{ + std::string name; + uint32_t fill; + double dark_bottom; // how much of the screen's height, from the bottom, is black + bool indoors; +}; + +UnitTestResult test_indoors_any_light(Logger&){ + const std::vector cases{ + {.name = "evening outside, orange all over", .fill = 0xffe0a040, .dark_bottom = 0, .indoors = false}, + {.name = "a room: orange, black below its floor", .fill = 0xffe0a040, .dark_bottom = 0.10, .indoors = true}, + {.name = "a fade: black all over", .fill = 0xff000000, .dark_bottom = 0, .indoors = false}, + {.name = "orange with a thin shadow at the bottom", .fill = 0xffe0a040, .dark_bottom = 0.01, .indoors = false}, + {.name = "daytime grass", .fill = 0xff60b040, .dark_bottom = 0.10, .indoors = false}, + }; + for (const IndoorsCase& item : cases){ + ImageRGB32 screen(1920, 1080); + const size_t dark_from = (size_t)(1080 * (1 - item.dark_bottom)); + for (size_t y = 0; y < 1080; y++){ + for (size_t x = 0; x < 1920; x++){ + screen.pixel(x, y) = y >= dark_from ? 0xff000000 : item.fill; + } + } + if (indoors(screen) != item.indoors){ + return item.name + (item.indoors ? ": read as outside." : ": read as inside."); + } + } + return true; +} + +// door_fade_look(): out of a door is a dark look, then a bright one that is +// not a room. r259 turned round in Sandgem's lit Center, read "not a room" +// once at full brightness, and walked east inside it as if outside. +struct DoorLook{ + double brightness; + bool room; +}; +struct DoorCase{ + std::string name; + std::vector looks; + // The look (1-based) that reads out, or 0 for none. + size_t out_at; +}; + +UnitTestResult test_door_fade(Logger&){ + const std::vector cases{ + {.name = "r259: a bright look in the Center that misses the room", .looks = {{458, true}, {440, false}, {457, true}}, .out_at = 0}, + {.name = "the fade, then the town", .looks = {{458, true}, {300, true}, {60, false}, {310, false}}, .out_at = 4}, + {.name = "the fade, then the room again", .looks = {{458, true}, {60, false}, {420, true}}, .out_at = 0}, + {.name = "the fade, still dim", .looks = {{60, false}, {150, false}, {320, false}}, .out_at = 3}, + }; + for (const DoorCase& item : cases){ + DoorFadeWatch watch; + size_t out_at = 0; + for (size_t i = 0; i < item.looks.size() && out_at == 0; i++){ + if (door_fade_look(watch, item.looks[i].brightness, item.looks[i].room)){ + out_at = i + 1; + } + } + if (out_at != item.out_at){ + return item.name + ": out at look " + std::to_string(out_at) + ", expected " + std::to_string(item.out_at) + "."; + } + } + return true; +} + +// A fade is uniform (fade_of(): the shared watchers' stddev test), dark or +// white; a floor is textured. The Oreburgh Gate's floor, about 46, is no +// fade: read as one, the tap into the Gate from Oreburgh, already +// through, waited 8 s for a banner gone (lab-B, 2026-11-08 15:31 to +// 15:54). A cave mouth's dim fade (45 to 64 measured) is one. The stddevs +// are a model; the Gate's frames on the console are to check them. +UnitTestResult test_fade_of(Logger&){ + struct Case{ + std::string name; + double brightness; + double stddev; + WarpFade fade; + }; + const std::vector cases{ + {.name = "inside the Gate, its floor", .brightness = 46, .stddev = 20, .fade = WarpFade::NONE}, + {.name = "Oreburgh's street at night", .brightness = 88, .stddev = 20, .fade = WarpFade::NONE}, + {.name = "a door's black", .brightness = 7, .stddev = 1, .fade = WarpFade::BLACK}, + {.name = "a cave mouth's dim fade", .brightness = 64, .stddev = 2, .fade = WarpFade::BLACK}, + {.name = "a grey screen, neither dark nor white", .brightness = 120, .stddev = 1, .fade = WarpFade::NONE}, + {.name = "a cave exit's white", .brightness = 250, .stddev = 1, .fade = WarpFade::WHITE}, + {.name = "a bright textured screen", .brightness = 250, .stddev = 20, .fade = WarpFade::NONE}, + }; + for (const Case& item : cases){ + const ImageStats stats(FloatPixel(item.brightness, item.brightness, item.brightness), FloatPixel(item.stddev, item.stddev, item.stddev), 1); + const WarpFade fade = fade_of(stats); + if (fade != item.fade){ + return item.name + ": " + warp_fade_name(fade) + ", expected " + warp_fade_name(item.fade) + "."; + } + } + return true; +} + + +UnitTestResult test_location_banner(Logger&){ + struct Case{ + std::string read; + std::string expected; + bool match; + }; + const Case cases[] = { + {"sandgemtown", "Sandgem Town", true}, + {"andgemltown", "Sandgem Town", true}, // r109's OCR + {"Route 202", "Route 202", true}, + {"route2o2", "Route 202", false}, // a digit read as a letter is not trusted + {"route201", "Route 202", false}, + {"jubilifecity", "Sandgem Town", false}, + {"twinleaftown", "Sandgem Town", false}, + {"", "Sandgem Town", false}, + }; + for (const Case& c : cases){ + if (location_banner_matches(c.read, c.expected) != c.match){ + return std::string("Banner \"") + c.read + "\" for " + c.expected + + (c.match ? " should match." : " should not match."); + } + } + return true; +} + + +UnitTestResult test_indoors(Logger& logger){ + struct Case{ std::string file; bool inside; }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c6.png", true}, + {"PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c4.png", true}, + {"PokemonBDSP/AutoStory/JubilifeCenterInsideUpper-20261030-r151a.png", true}, + {"PokemonBDSP/AutoStory/SandgemCenterInside-20261030-053555.png", true}, + {"PokemonBDSP/AutoStory/JubilifeSchoolInside-20261030.png", true}, + {"PokemonBDSP/AutoStory/JubilifeCenterDoorstep-20261030-c5.png", false}, + {"PokemonBDSP/AutoStory/JubilifeSchoolDoorstep-20261030.png", false}, + {"PokemonBDSP/AutoStory/JubilifeSchoolDoorstep2-20261030.png", false}, + {"PokemonBDSP/AutoStory/JubilifeCenterFrontDoor-20261030-q2.png", false}, + {"PokemonBDSP/AutoStory/SandgemCenterBelowSign-20261030-053527.png", false}, + {"PokemonBDSP/AutoStory/SandgemMartDoorUpRight-20261030-023443.png", false}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const bool inside = indoors(ImageRGB32(path)); + logger.log(std::string(c.file) + ": " + (inside ? "indoors" : "outside")); + if (inside != c.inside){ + return std::string(c.file) + ": expected " + (c.inside ? "indoors" : "outside") + "."; + } + } + return true; +} + + +UnitTestResult test_location_banner_showing(Logger& logger){ + struct Case{ std::string file; bool banner; }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/JubilifeCenterDoorstep-20261030-c5.png", true}, + {"PokemonBDSP/AutoStory/JubilifeSchoolDoorstep-20261030.png", true}, + {"PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c6.png", false}, + {"PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c4.png", false}, + {"PokemonBDSP/AutoStory/JubilifeCenterInsideUpper-20261030-r151a.png", false}, + {"PokemonBDSP/AutoStory/JubilifeCenterFrontDoor-20261030-q2.png", false}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const bool banner = location_banner_showing(ImageRGB32(path)); + logger.log(std::string(c.file) + ": banner " + (banner ? "shows" : "none")); + if (banner != c.banner){ + return std::string(c.file) + ": expected " + (c.banner ? "a banner" : "no banner") + "."; + } + } + return true; +} + + + +// walk_view_shift(): a step scrolls the whole view; scenery that moves on +// its own does not. Synthetic 384x216 screens of 8-pixel blocks in mixed +// colours, as grass and flowers are, with one change each. +uint32_t scenery_at(int x, int y){ + const uint32_t block = (uint32_t)((x >> 3) * 73856093u) ^ (uint32_t)((y >> 3) * 19349663u); + const uint32_t hashed = block * 2654435761u; + return 0xff000000 | (hashed & 0x00ffffff); +} +ImageRGB32 scenery(int shift_x, int shift_y){ + ImageRGB32 screen(384, 216); + for (int y = 0; y < 216; y++){ + for (int x = 0; x < 384; x++){ + screen.pixel(x, y) = scenery_at(x + shift_x + 1000, y + shift_y + 1000); + } + } + return screen; +} +// Each colour channel of the pixels in [x0, x1) x [y0, y1) times `scale`, +// or set to `fill` if given. +void paint(ImageRGB32& screen, int x0, int y0, int x1, int y1, double scale, uint32_t fill = 0){ + for (int y = y0; y < y1; y++){ + for (int x = x0; x < x1; x++){ + uint32_t p = screen.pixel(x, y); + if (fill != 0){ + screen.pixel(x, y) = fill; + continue; + } + uint32_t out = 0xff000000; + for (int shift = 0; shift < 24; shift += 8){ + out |= (uint32_t)(((p >> shift) & 0xff) * scale) << shift; + } + screen.pixel(x, y) = out; + } + } +} + +UnitTestResult test_view_shift(Logger&){ + const ImageRGB32 before = scenery(0, 0); + struct Case{ + std::string name; + ImageRGB32 after; + bool scrolled; + }; + std::vector cases; + cases.push_back({"the same view", scenery(0, 0), false}); + cases.push_back({"a step east: the view scrolls 16 pixels west", scenery(16, 0), true}); + cases.push_back({"a step south", scenery(0, 12), true}); + { + // r83, r84: a cloud's shadow over the top of the view. + ImageRGB32 shadow = scenery(0, 0); + paint(shadow, 0, 0, 384, 90, 0.6); + cases.push_back({"a cloud shadow over the top third", std::move(shadow), false}); + } + { + // Jubilife's TV screens, the mine's conveyors. + ImageRGB32 tv = scenery(0, 0); + paint(tv, 20, 20, 110, 80, 0, 0xffffffff); + cases.push_back({"a TV lights up in a corner", std::move(tv), false}); + } + { + // A turn changes only the player's sprite. + ImageRGB32 turned = scenery(0, 0); + paint(turned, 175, 75, 210, 150, 0, 0xffff4040); + cases.push_back({"the player turns", std::move(turned), false}); + } + for (const Case& item : cases){ + const ViewShift shift = walk_view_shift(before, item.after); + if (shift.scrolled() != item.scrolled){ + return std::format("{}: {} (shift {}, {}; difference {:.1f} unshifted, {:.1f} shifted).", + item.name, item.scrolled ? "did not read as a scroll" : "read as a scroll", + shift.dx, shift.dy, shift.unshifted, shift.shifted); + } + } + const ViewShift east = walk_view_shift(before, scenery(16, 0)); + if (east.dx != -8 || east.dy != 0){ + return std::format("A 16-pixel scroll west read as ({}, {}) on the half-size copy, expected (-8, 0).", east.dx, east.dy); + } + return true; +} + +// pokemon_center_on_screen(): the Center's warm floor in a room. r79 read +// Jubilife's yellow plaza as a Center and called a won battle lost. +UnitTestResult test_pokemon_center_on_screen(Logger&){ + constexpr uint32_t FLOOR = 0xfff0c860; // the Center's warm yellow + constexpr uint32_t WALL = 0xffe08030; // its orange back wall + constexpr uint32_t GRASS = 0xff60b040; + struct Case{ + std::string name; + uint32_t top; + uint32_t bottom; + double dark_bottom; // how much of the screen's height, from the bottom, is black + bool center; + }; + const std::vector cases{ + {"a Center: wall over floor, black below it", WALL, FLOOR, 0.15, true}, + {"Jubilife's plaza: yellow paving, no black edge", FLOOR, FLOOR, 0, false}, + {"a street: grass over yellow paving", GRASS, FLOOR, 0, false}, + {"a grey gym in a room", 0xff808080, 0xff909090, 0.15, false}, + }; + for (const Case& item : cases){ + ImageRGB32 screen(1920, 1080); + const size_t dark_from = (size_t)(1080 * (1 - item.dark_bottom)); + for (size_t y = 0; y < 1080; y++){ + for (size_t x = 0; x < 1920; x++){ + screen.pixel(x, y) = y >= dark_from ? 0xff000000 : y < 540 ? item.top : item.bottom; + } + } + if (pokemon_center_on_screen(screen) != item.center){ + return item.name + (item.center ? ": not read as a Center." : ": read as a Center."); + } + } + return true; +} + +// 01.02's walk east beats the Youngster above Route 203's row 18; the rows +// after it, from saves made since, do not meet him again. +UnitTestResult test_row_trainers(Logger&){ + const StoryWorld* world = story_plan_world("Route203", "JubilifeCity"); + if (world == nullptr){ + return std::string("no world joins Route 203 and Jubilife"); + } + const auto has_youngster = [world](const std::string& row){ + const std::vector sights = row_trainers(*world, row); + return std::any_of(sights.begin(), sights.end(), [](const TrainerSight& sight){ + return sight.name == "Youngster (above row 18)"; + }); + }; + if (!has_youngster("01.02")){ + return std::string("01.02 fights the Youngster: he must be on its trainers"); + } + if (has_youngster("01.06")){ + return std::string("01.06 starts from a save after 01.02 beat the Youngster"); + } + return true; +} + +// A change of the whole screen is a warp only on a fade or a banner read; a +// battle's swirl before its menu is not one, and waits for the battle +// (lab-B 2026-11-12 21:58, 01.03's mine: a Geodude on west 7 ended the leg). +UnitTestResult test_whole_screen_change(Logger&){ + struct Case{ std::string_view name; bool battle; bool evidence; WholeScreenChange want; }; + const Case cases[] = { + {"a cave encounter's swirl, no menu yet, no fade, no banner", false, false, WholeScreenChange::UNCLASSIFIED}, + {"the swirl, then the battle's menu", true, false, WholeScreenChange::BATTLE}, + {"a door's fade", false, true, WholeScreenChange::WARP}, + {"a door's banner", false, true, WholeScreenChange::WARP}, + {"a battle that opens from a fade", true, true, WholeScreenChange::BATTLE}, + }; + for (const Case& c : cases){ + if (classify_whole_screen_change({.battle_showed = c.battle, .fade_or_banner_read = c.evidence}) != c.want){ + return std::string(c.name) + ": wrong class."; + } + } + return true; +} + +struct AutoStoryWalkInput{ + std::string name; + std::function body; +}; + +struct AutoStoryWalkExpectation{ +}; + +struct AutoStoryWalkCase{ + AutoStoryWalkInput input; + AutoStoryWalkExpectation expectation; +}; + +// place_leg_battle() on synthetic walks of 10 taps, their events recorded +// as record_tap_battle() records the console's. +struct LegBattleCase{ + std::string name; + size_t battles; + size_t tap; // the tap (from 1) that set the battle off + size_t moved_after; // taps that moved after it was run from + bool settled; // false: a white fade with no battle yet + std::optional expected; +}; +UnitTestResult test_leg_battle_place(Logger& logger){ + const LegBattleCase CASES[] = { + {"mid-leg, the taps went on", 1, 3, 7, true, TileSpan{9, 10}}, + {"seen after the hold", 1, 6, 0, true, TileSpan{5, 7}}, + {"on the first tap", 1, 1, 0, true, TileSpan{0, 2}}, + {"before the walk", 1, 0, 0, true, TileSpan{0, 1}}, + {"two battles", 2, 3, 0, true, std::nullopt}, + {"unsettled: a white fade, no battle yet", 1, 4, 0, false, std::nullopt}, + }; + for (const LegBattleCase& item : CASES){ + WalkResult walk{.stop = WalkStop::LIMIT, .walked = Milliseconds(8000), .wild_battles = item.battles}; + walk.moved_after_battle = item.moved_after; + walk.events.record({}, walk.started); + if (item.settled){ + record_tap_battle(walk, item.tap); + }else{ + walk.events.record({Pokemon::ScreenEvent::FADE_WHITE}, walk.started + Milliseconds(800 * (item.tap - 1) + 500)); + } + const LegBattlePlace place = place_leg_battle(walk, 10); + logger.log(leg_battle_line(item.name, walk, place)); + const bool same = place.moved.has_value() == item.expected.has_value() && + (!place.moved || (place.moved->min == item.expected->min && place.moved->max == item.expected->max)); + if (!same){ + return item.name + ": " + leg_battle_line(item.name, walk, place); + } + } + return true; +} + +class Test_AutoStoryWalk : public UnitTest{ +public: + explicit Test_AutoStoryWalk(AutoStoryWalkCase test_case) + : UnitTest("PokemonBDSP::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryWalkCase m_case; +}; + +} // namespace + + +const AutoStoryWalkCase AUTO_STORY_WALK_CASES[] = { + { + .input = {"a whole-screen change ends a walk as a warp only on a fade or a banner; a swirl waits for the battle", test_whole_screen_change}, + .expectation = {}, + }, + { + .input = {"a turn is a D-pad tap too short to step", test_route_turn_tap}, + .expectation = {}, + }, + { + .input = {"indoors() under the evening tint", test_indoors_any_light}, + .expectation = {}, + }, + { + .input = {"out of a door needs its fade", test_door_fade}, + .expectation = {}, + }, + { + .input = {"a fade is uniform, dark or white; a dim floor is none (lab-B, 2026-11-08)", test_fade_of}, + .expectation = {}, + }, + { + .input = {"a warp's fade, followed frame by frame, its hold seen or not", test_warp_fade_watch}, + .expectation = {}, + }, + { + .input = {"location banner match", test_location_banner}, + .expectation = {}, + }, + { + .input = {"indoors", test_indoors}, + .expectation = {}, + }, + { + .input = {"location banner showing", test_location_banner_showing}, + .expectation = {}, + }, + { + .input = {"a Pokemon Center is its floor in a room, not a yellow plaza", test_pokemon_center_on_screen}, + .expectation = {}, + }, + { + .input = {"a step scrolls the view; moving scenery does not", test_view_shift}, + .expectation = {}, + }, + { + .input = {"a wild battle on an open leg's taps, placed by its screen events", test_leg_battle_place}, + .expectation = {}, + }, + { + .input = {"a row's trainers leave out those an earlier row beat", test_row_trainers}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Walk(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_WALK_CASES){ + database.add(item); + } +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h new file mode 100644 index 0000000000..6532456aa1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h @@ -0,0 +1,27 @@ +/* BDSP AutoStory Walk Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Walk_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Walk_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// The walker's screen reads: indoors() on synthetic screens and +// screenshots, the door's fade, and the location banner. +void add_tests_AutoStory_Walk(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp new file mode 100644 index 0000000000..2f08d38751 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp @@ -0,0 +1,248 @@ +/* BDSP AutoStory - Walks + * + * From: https://github.com/PokemonAutomation/ + * + * Walks measured on the console that the planner does not make: straight + * holds and D-pad taps, walks until something stops us, and the ways in + * and out of doors. + * + */ + +#include +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "Actions/PokemonBDSP_AutoStory_TalkOver.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "Actions/PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_Walks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +std::pair dpad_of(Direction direction){ + switch (direction){ + case Direction::NORTH: + return {0, 1}; + case Direction::EAST: + return {1, 0}; + case Direction::SOUTH: + return {0, -1}; + case Direction::WEST: + return {-1, 0}; + } + return {0, 0}; +} + +void walk_until_stopped(VideoStream& stream, ProControllerContext& context, Direction direction, size_t tiles, const std::string& label){ + const bool across = direction == Direction::EAST || direction == Direction::WEST; + const auto [x, y] = dpad_of(direction); + // Twice every tile's time, and time for the wall to read as still: it + // stops at the wall anyway. One tile's time each fell short once the + // turn stopped stepping us ahead (r57: west to Jubilife's sign ended + // on the time, a column short). + const Milliseconds limit = ROUTE_START_DELAY + 800ms + + (int64_t)tiles * 2 * (across ? ROUTE_MS_PER_TILE_ACROSS : ROUTE_MS_PER_TILE_UP_DOWN); + // Turn first: the first press of a new way only turns us, the screen + // holds still meanwhile, and that read as a wall two tiles into the + // walk west to the Lab's sign (r132a). + route_turn(stream, context, direction); + const WalkResult walked = walk_until(stream, context, run_from_wild_battle, {x, y}, limit, label); + stream.log("[Walk] " + label + ": stopped on " + walk_stop_name(walked.stop) + "."); + if (walked.stop == WalkStop::TRANSITION || walked.stop == WalkStop::DIALOG){ + fail_step(stream, label + ": " + walk_stop_name(walked.stop) + " instead of a stop."); + } + // The leg was measured to end against a wall, so holding toward it + // for every tile's time leaves us there whatever the screen shows: + // people walking or a TV screen nearby kept it changing and the stop + // went unseen (r162a, r167a, r171a). Seeing the stop only ends the + // hold early; a wrong place is caught by the next gate (a sign, a + // door, someone who answers). + if (walked.stop == WalkStop::LIMIT){ + stream.log("[Walk] " + label + ": held for " + std::to_string(tiles) + + " tiles' time without seeing the stop; taking the wall as reached."); + } +} + +void hold_for(VideoStream& stream, ProControllerContext& context, Direction direction, Milliseconds hold, const std::string& label){ + const auto [x, y] = dpad_of(direction); + const WalkResult walk = walk_for(stream, context, run_from_wild_battle, {x, y}, hold, label); + if (walk.stop == WalkStop::DIALOG || walk.stop == WalkStop::TRANSITION){ + fail_step(stream, label + ": " + walk_stop_name(walk.stop) + "."); + } +} + +void tap_tiles(VideoStream& stream, ProControllerContext& context, Direction direction, size_t tiles, const std::string& label){ + const auto [x, y] = dpad_of(direction); + stream.log("[Walk] " + label + ": " + std::to_string(tiles) + " taps."); + for (size_t i = 0; i < tiles; i++){ + pbf_move_left_joystick(context, {x, y}, 160ms, 400ms); + } + pbf_wait(context, 800ms); + context.wait_for_all_requests(); +} + +void nudge(VideoStream& stream, ProControllerContext& context, Direction direction, const std::string& label){ + route_turn(stream, context, direction); + walk_for(stream, context, run_from_wild_battle, {direction == Direction::EAST ? 1.0 : -1.0, 0.0}, 80ms, label); +} + +bool door_north_by_scene(VideoStream& stream, ProControllerContext& context, const std::string& label){ + context.wait_for_all_requests(); + VideoSnapshot before = stream.video().snapshot(); + const WalkResult up = walk_until(stream, context, run_from_wild_battle, {0, 1}, 1200ms, label); + context.wait_for(1500ms); + if (up.stop == WalkStop::TRANSITION){ + return true; + } + // The School's fade is not always black enough to read as one: r146b + // went in (changes 156, 261) and read "limit". A whole new scene is + // through, as leave_door_south() counts it. + VideoSnapshot after = stream.video().snapshot(); + const bool through = before && after && walk_screen_change(*before.frame, *after.frame) > WALK_SCENE_CHANGE; + if (through){ + stream.log("[Route] " + label + ": a new scene; through the door."); + } + return through; +} + +// One tile south, for a player facing another way. +void step_south(VideoStream& stream, ProControllerContext& context){ + route_turn(stream, context, Direction::SOUTH); + pbf_move_left_joystick(context, {0, -1}, route_open_hold(Direction::SOUTH, 1), 200ms); + context.wait_for_all_requests(); +} + +// Holds the stick toward `dir` for `tiles` tiles, turning first. +void hold_tiles(VideoStream& stream, ProControllerContext& context, Direction dir, size_t tiles){ + route_turn(stream, context, dir); + const double x = dir == Direction::EAST ? 1 : dir == Direction::WEST ? -1 : 0; + const double y = dir == Direction::NORTH ? 1 : dir == Direction::SOUTH ? -1 : 0; + pbf_move_left_joystick(context, {x, y}, route_open_hold(dir, tiles), 200ms); + context.wait_for_all_requests(); +} + +WalkResult walk_dir( + VideoStream& stream, ProControllerContext& context, Direction dir, Milliseconds limit, const std::string& label, + const std::function& arrived +){ + const double x = dir == Direction::EAST ? 1 : dir == Direction::WEST ? -1 : 0; + const double y = dir == Direction::NORTH ? 1 : dir == Direction::SOUTH ? -1 : 0; + return walk_until(stream, context, run_from_wild_battle, {x, y}, limit, label, arrived); +} + +// `taps` D-pad taps toward `dpad`, settling between them. On the D-pad the +// player moves a whole tile per tap (measured above), so counted legs do +// not drift the way stick holds do. +void dpad_tiles(VideoStream& stream, ProControllerContext& context, DpadPosition dpad, size_t taps, const std::string& label){ + stream.log("[Walk] " + label + ": " + std::to_string(taps) + " D-pad taps."); + // No turn tap: a 160 ms tap is a whole step from any facing, so N taps + // are N tiles (r80: the old 160 ms "turn" tap walked one tile too many, + // two tiles west in the Ravaged Path, not one). Turning only matters to + // talk to someone, and they block like a wall, so a tap toward them turns. + size_t moved = 0; + // Count only taps that moved the view: one after the turn was lost and + // the leg ended a tile short (r215d). A tile's scroll changes the view by + // about 100; standing still, under 15. A tap that does not move is someone + // in the way: people walk about these streets (r220g: blocked on tap 9 + // for 12 s while a passer-by stood in our row). Wait for them to move on, + // up to 30 s, before giving up. + const auto deadline = current_time() + std::chrono::milliseconds(taps * 1000 + 30000); + while (moved < taps && current_time() < deadline){ + context.wait_for_all_requests(); + const VideoSnapshot before = stream.video().snapshot(); + pbf_press_dpad(context, dpad, 160ms, 700ms); + context.wait_for_all_requests(); + const VideoSnapshot after = stream.video().snapshot(); + const double change = before && after ? walk_screen_change(*before.frame, *after.frame) : 100; + if (!before || !after || tap_moved(*before.frame, *after.frame, 30)){ + moved++; + continue; + } + stream.log("[Walk] " + label + ": tap " + std::to_string(moved + 1) + " did not move (view change " + std::to_string(change) + "); someone in the way? Waiting."); + if (dialogue_opens(stream, context, 300ms)){ + talk_or_fight(stream, context, label + ": someone in the way"); + } + context.wait_for(1500ms); + } + if (moved < taps){ + fail_step(stream, label + ": only " + std::to_string(moved) + " of " + std::to_string(taps) + " D-pad taps moved us."); + } +} + +// Through a door straight north of us: tap up to face it, hold up into it, +// and wait for its dark fade (by hand: brightness 7, about 1 s after). +bool dpad_through_door_north(VideoStream& stream, ProControllerContext& context, const std::string& label){ + pbf_press_dpad(context, DPAD_UP, 160ms, 300ms); + pbf_press_dpad(context, DPAD_UP, 300ms, 0ms); + context.wait_for_all_requests(); + const auto start = current_time(); + while (current_time() - start < 5000ms){ + context.wait_for(100ms); + const VideoSnapshot frame = stream.video().snapshot(); + if (frame && mean_brightness(*frame.frame) < 25){ + stream.log("[Route] " + label + ": the screen went dark; through the door."); + context.wait_for(2000ms); + return true; + } + } + stream.log("[Route] " + label + ": no dark screen after the D-pad step north."); + return false; +} + +// For a player inside a Pokemon Center after a whiteout, at the counter: +// out through the door. The door is straight south of the nurse in most +// Centers; in Oreburgh's it was two tiles east of where we stood (by hand). +void walk_out_of_center(VideoStream& stream, ProControllerContext& context, const std::string& label){ + finish_dialogue(stream, context, BUTTON_B, label + ": the nurse"); + const int shifts[] = {0, 2, -4}; + for (int shift : shifts){ + if (shift != 0){ + hold_tiles(stream, context, shift > 0 ? Direction::EAST : Direction::WEST, (size_t)std::abs(shift)); + } + // A tile at a time, looking after each, so we stop on the doorstep: + // walk_until() looks only between 400 ms holds, and r205d held on + // through the fade and stepped a tile further south. + bool out = false; + for (size_t tile = 0; tile < 10 && !out; tile++){ + // D-pad taps: whole tiles (the first only turns us from the nurse). + pbf_press_dpad(context, DPAD_DOWN, 160ms, 0ms); + context.wait_for(1500ms); + if (dialogue_opens(stream, context, 100ms)){ + finish_dialogue(stream, context, BUTTON_B, label + ": someone in the Center"); + } + const VideoSnapshot frame = stream.video().snapshot(); + size_t percent = 100; + if (frame){ + center_floor_on_screen(*frame.frame, &percent); + } + out = percent < 30; + } + if (out){ + // "No floor" also reads true on the door's black fade, and the game + // takes no input until it ends: the first D-pad taps after it were + // lost and the gym's door ended a tile away (r215d, r216d; a shot + // per press by hand showed the black frame). Settle: the fade + // back up and a quiet overworld, not a fixed wait. + talk_over(stream, context, std::format("{}: out of the Center", label), {.rule = {.door = true}}); + + return; + } + } + fail_step(stream, label + ": could not find the Pokemon Center's door."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h new file mode 100644 index 0000000000..a24689d60e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h @@ -0,0 +1,82 @@ +/* BDSP AutoStory - Walks + * + * From: https://github.com/PokemonAutomation/ + * + * Walks measured on the console that the planner does not make: straight + * holds and D-pad taps, walks until something stops us, and the ways in + * and out of doors. Each fails the step with a screenshot when the walk + * ends where it should not (a dialogue or a door on a walk to a wall). + * + */ + +// code-health: allow header_symbols -- movement primitives measured on console that are used across story executor and event scripts +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Walks_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Walks_H + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The stick's (x, y) for `direction`: y is up the screen. +std::pair dpad_of(Pokemon::Direction direction); + +// Walks `direction` until something stops us, for at most `tiles` tiles' +// time, turning first. Holding on for the whole time counts as stopped: +// the leg was measured to end against a wall. Fails on a transition or a +// dialogue. +void walk_until_stopped(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, size_t tiles, const std::string& label); +// Holds the stick `direction` for `hold` from standing still (200 ms is a +// row, turning included; 160 ms a tile across, facing that way already). +// Fails on a transition or a dialogue. +void hold_for(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, Milliseconds hold, const std::string& label); +// `tiles` one-tile taps `direction` in a row, facing that way already, +// without waiting for the screen to settle in between: six of these moved +// exactly six tiles on the console, where six single holds moved seven +// (2026-10-30). +void tap_tiles(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, size_t tiles, const std::string& label); +// Half a tile `direction` (EAST or WEST), facing it first. +void nudge(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, const std::string& label); +// Walks north; true if a door took us through, by its fade or by a whole +// new scene (the Trainers' School's fade is not always black, r146b). +bool door_north_by_scene(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// One tile south, for a player facing another way. +void step_south(VideoStream& stream, ProControllerContext& context); +// Holds the stick toward `dir` for `tiles` tiles, turning first. +void hold_tiles(VideoStream& stream, ProControllerContext& context, Pokemon::Direction dir, size_t tiles); +// walk_until() toward `dir`. +WalkResult walk_dir( + VideoStream& stream, ProControllerContext& context, Pokemon::Direction dir, Milliseconds limit, const std::string& label, + const std::function& arrived = nullptr +); + +// `taps` D-pad taps toward `dpad`, settling between them. On the D-pad the +// player moves a whole tile per tap, so counted legs do not drift the way +// stick holds do. The first tap toward a new way only turns the player, so +// turn first. +void dpad_tiles(VideoStream& stream, ProControllerContext& context, DpadPosition dpad, size_t taps, const std::string& label); +// Through a door straight north of us: tap up to face it, hold up into it, +// and wait for its dark fade (by hand: brightness 7, about 1 s after). +bool dpad_through_door_north(VideoStream& stream, ProControllerContext& context, const std::string& label); +// For a player inside a Pokemon Center after a whiteout, at the counter: +// out through the door. The door is straight south of the nurse in most +// Centers; in Oreburgh's it was two tiles east of where we stood (by hand). +void walk_out_of_center(VideoStream& stream, ProControllerContext& context, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/README.md b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/README.md new file mode 100644 index 0000000000..cf77edffe0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/README.md @@ -0,0 +1,16 @@ +# BDSP authored checkpoint pilot + +`bdsp-mine-01.03.plan.json` is the authoritative definition of the mine/Roark checkpoint. `oreburgh_mine_to_roark()` loads it into the existing story table. Edit this file here when changing the route; PokemonSimulation consumes an explicitly pinned Arduino revision for independent analysis. Its checked-in pilot plan is a frozen regression snapshot, not a second authoring source. + +The shared `pokesim-authored-checkpoint/1` envelope contains `checkpoint.id`, `title`, `start`, `events` in that order. BDSP validates `execution.on_failure` and `execution.saves_in`. The common envelope and resource lookup live in `Pokemon/AutoStory/Pokemon_AuthoredCheckpointJson.*`. Bookkeeping defaults and game executors are unchanged. + +## Validation and iteration + +1. Native registered tests run the actual JSON-backed table through the existing engine/fake game, including retry exhaustion before healing. Data tests check parsing, table wiring, event IDs/order, policy/card values, and map-derived Center/Roark anchors. The table-wiring comparison is not an independent check against the removed C++ authoring list. +2. Linux CI pins a simulator revision reachable from its main branch and replays **this checkout's JSON** against nine independent scenario expectations. There is no byte-equality requirement against a second plan. A route change may still require revising a scenario expectation when its expected behavior intentionally changes; such failures are meaningful regression feedback, not a requirement to mirror JSON or repin simulator code for every edit. + +A lab route edit therefore touches the Arduino plan and applicable tests/evidence. The next build re-embeds it automatically. CMake rejects malformed JSON syntax before launching the app; native tests perform full reader/policy validation. Run tests before deploying: CMake's syntax preflight alone cannot validate all semantics. The shared formatter writes authored JSON; Git normalization rules are unchanged. Whitespace and line-ending changes do not affect replay results. C++ retains the repository CRLF convention. + +Coordinates remain literal data tied to the pinned map. The map-anchor test catches drift in the Center exit and Roark approach; it does not validate every tile or replace map review. See [the checkpoint evidence](bdsp-mine-01.03.md) for the warp columns, counted hall movement, wall choices and unresolved observations. + +The native fake uses declared scene/door fallbacks and is not a detector/timing oracle. Python uses scripted transition/dialogue/scene/heal evidence and NPC-clearance windows. Missing warp links, native planner-cost parity, queued movement and live recovery remain gaps. Python's diagnostic policy is stop/no-save; the native program retains its start-save/reload policy. No map correction, console, profile or save is part of this authoring migration. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.00.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.00.plan.json new file mode 100644 index 0000000000..6b40e409c8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.00.plan.json @@ -0,0 +1,40 @@ +{ + "checkpoint": { + "events": [ + { + "declared_after": { + "facing": "north", + "map": "PlayerHouse2F", + "tiles": [ + [ + 4, + 6 + ] + ] + }, + "id": "intro", + "kind": "new_game", + "skip_if": [ + "save_loaded" + ], + "why": "language, appearance, the player's name, Barry's default name" + }, + { + "id": "settings", + "kind": "options", + "why": "fast text, Send to Boxes automatic, no nickname prompt, Autosave off" + } + ], + "id": "00.00", + "title": "New game: the intro and the settings" + }, + "execution": { + "on_failure": "stop", + "saves_in": "" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.01.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.01.plan.json new file mode 100644 index 0000000000..e6fd8afa09 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.01.plan.json @@ -0,0 +1,523 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "PlayerHouse2F", + "tiles": [ + [ + 7, + 5 + ] + ] + } + }, + "id": "below_the_flute", + "kind": "route", + "why": "below the Azure Flute's tile" + }, + { + "action": "azure_flute_if_there", + "declared_after": { + "facing": "north", + "map": "PlayerHouse2F", + "tiles": [ + [ + 7, + 5 + ] + ] + }, + "id": "azure_flute_if_there", + "kind": "game_action", + "why": "take the Azure Flute if this save has one" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 1 + }, + { + "direction": "east", + "tiles": 1 + } + ] + }, + "id": "onto_the_flute_tile", + "kind": "legs", + "why": "up onto (7, 4), then east onto the top of the stairs' mat (8, 4)" + }, + { + "args": { + "direction": "east" + }, + "declared_after": { + "map": "PlayerHouse1F", + "tiles": [ + [ + 10, + 3 + ], + [ + 10, + 4 + ] + ] + }, + "id": "down_the_stairs", + "kind": "step", + "why": "onto the stairs" + }, + { + "declared_after": { + "map": "PlayerHouse1F", + "tiles": [ + [ + 10, + 3 + ], + [ + 10, + 4 + ] + ] + }, + "id": "mom_calls", + "kind": "scene", + "why": "Mom calls the player over as they come down; we stay put" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "south", + "map": "PlayerHouse1F", + "tiles": [ + [ + 6, + 9 + ] + ] + } + }, + "id": "above_the_door", + "kind": "route", + "why": "past Mom at (7, 8) to above the door" + }, + { + "args": { + "direction": "south" + }, + "declared_after": { + "facing": "south", + "map": "TwinleafTown", + "tiles": [ + [ + 20, + 22 + ] + ] + }, + "id": "out_of_the_house", + "kind": "step", + "why": "onto the door's tile (6, 10), and out" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "TwinleafTown", + "tiles": [ + [ + 10, + 12 + ] + ] + } + }, + "id": "short_of_barrys_door", + "kind": "route", + "why": "a tile east of Barry's doorstep" + }, + { + "args": { + "direction": "west" + }, + "id": "barrys_door", + "kind": "step", + "why": "the trigger on his doorstep (9, 12)" + }, + { + "action": "a", + "declared_after": { + "facing": "north", + "map": "TwinleafTown", + "tiles": [ + [ + 9, + 13 + ] + ] + }, + "id": "barry_bursts_out", + "kind": "scene", + "why": "Barry runs out, bumps into us and runs back in" + }, + { + "action": "a", + "declared_after": { + "facing": "north", + "map": "TwinleafTown", + "tiles": [ + [ + 9, + 13 + ] + ] + }, + "id": "barry_goes_home", + "kind": "scene", + "why": "his second lines (Butters waited for two)" + }, + { + "args": { + "direction": "north" + }, + "declared_after": { + "facing": "north", + "map": "RivalHouse1F", + "tiles": [ + [ + 6, + 10 + ] + ] + }, + "id": "into_barrys_house", + "kind": "step", + "why": "past (9, 12) into his door" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 1 + } + ] + }, + "id": "off_barrys_mat", + "kind": "legs", + "why": "off the door's mat, which no route starts on" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "east", + "map": "RivalHouse1F", + "tiles": [ + [ + 2, + 3 + ] + ] + } + }, + "id": "below_barrys_stairs", + "kind": "route", + "why": "past his mother at (4, 6) to the top of the stairs' mat" + }, + { + "args": { + "direction": "east" + }, + "declared_after": { + "map": "RivalHouse2F", + "tiles": [ + [ + 3, + 4 + ] + ] + }, + "id": "up_barrys_stairs", + "kind": "step", + "why": "onto the stairs" + }, + { + "action": "a", + "declared_after": { + "facing": "north", + "map": "RivalHouse2F", + "tiles": [ + [ + 3, + 5 + ] + ] + }, + "id": "barry_runs_to_the_lake", + "kind": "scene", + "why": "Barry's lines; he runs off to the lake" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 1 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "RivalHouse2F", + "tiles": [ + [ + 3, + 4 + ], + [ + 4, + 4 + ], + [ + 5, + 4 + ], + [ + 3, + 5 + ], + [ + 4, + 5 + ], + [ + 5, + 5 + ], + [ + 3, + 6 + ], + [ + 4, + 6 + ], + [ + 5, + 6 + ] + ] + }, + "id": "off_barrys_west_wall", + "kind": "legs", + "why": "a tap east, clear of the west wall" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 3 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "RivalHouse2F", + "tiles": [ + [ + 3, + 4 + ], + [ + 4, + 4 + ], + [ + 5, + 4 + ] + ] + }, + "id": "up_to_barrys_back_wall", + "kind": "legs", + "why": "north to the room's back wall" + }, + { + "args": { + "direction": "west" + }, + "declared_after": { + "map": "RivalHouse1F", + "tiles": [ + [ + 2, + 3 + ] + ] + }, + "id": "down_barrys_stairs", + "kind": "step", + "why": "west along row 4 onto the stairs" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "south", + "map": "RivalHouse1F", + "tiles": [ + [ + 6, + 9 + ] + ] + } + }, + "id": "above_barrys_door", + "kind": "route", + "why": "back to his door" + }, + { + "args": { + "direction": "south" + }, + "declared_after": { + "facing": "south", + "map": "TwinleafTown", + "tiles": [ + [ + 9, + 12 + ] + ] + }, + "id": "out_of_barrys_house", + "kind": "step", + "why": "out of his door" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 6 + }, + { + "direction": "north", + "tiles": 18 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "Route201", + "tiles": [ + [ + 15, + 19 + ] + ] + }, + "id": "below_barrys_warning", + "kind": "legs", + "why": "along row 12, then up Twinleaf's exit to Route 201 (15, 19)" + }, + { + "args": { + "direction": "north" + }, + "id": "barrys_warning", + "kind": "step", + "why": "the trigger (14..19, 18)" + }, + { + "declared_after": { + "map": "Route201", + "tiles": [ + [ + 15, + 18 + ] + ] + }, + "id": "barry_warns_us", + "kind": "scene", + "why": "Barry's tall-grass warning; he then stands north of us at (15, 17)" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "west", + "tiles": 1 + }, + { + "direction": "north", + "tiles": 2 + } + ] + }, + "id": "away_from_barry", + "kind": "legs", + "why": "to (14, 16): row 18 west of the path is trees (r16) and (13, 17) is the 201 sign (probe-r201-sign), so clear both onto row 16" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 14 + ] + ] + } + }, + "id": "below_the_lake_entrance", + "kind": "route", + "why": "west along Route 201's lower level to the Lakefront" + } + ], + "id": "00.01", + "start": { + "facing": "north", + "map": "PlayerHouse2F", + "tiles": [ + [ + 4, + 6 + ] + ] + }, + "title": "Bedroom to the Verity Lakefront" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Twinleaf Town" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.02.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.02.plan.json new file mode 100644 index 0000000000..40663f923f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.02.plan.json @@ -0,0 +1,92 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 13 + ] + ] + } + }, + "id": "to_the_lake_entrance", + "kind": "route", + "why": "a tile short of the trigger" + }, + { + "args": { + "direction": "north" + }, + "id": "lake_entrance", + "kind": "step", + "why": "the trigger (16..17, 12): Barry hears Rowan and the two walk in" + }, + { + "action": "starter_briefcase", + "id": "starter_briefcase", + "kind": "game_action", + "why": "the lake's scene, to the open briefcase" + }, + { + "action": "choose_starter", + "id": "choose_starter", + "kind": "game_action", + "why": "the starter from the program's option" + }, + { + "action": "starter", + "args": { + "may_lose": true + }, + "id": "starly", + "kind": "battle", + "why": "the Starly battle, with the starter alone" + }, + { + "declared_after": { + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 14 + ], + [ + 17, + 15 + ] + ] + }, + "id": "after_starly", + "kind": "scene", + "why": "Rowan's lines and the walk out of the lake" + } + ], + "id": "00.02", + "start": { + "facing": "north", + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 14 + ] + ] + }, + "title": "Lake Verity: the starter and the Starly battle" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Verity Lakefront" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.03.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.03.plan.json new file mode 100644 index 0000000000..07f057f20a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.03.plan.json @@ -0,0 +1,135 @@ +{ + "checkpoint": { + "events": [ + { + "action": "into_scene", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 22 + } + ], + "target": { + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 21 + ] + ] + } + }, + "id": "into_rowans_scene", + "kind": "route", + "why": "south to the Lakefront's wall, then east onto Route 201 until Rowan's scene starts: 20 tiles, 22 at most" + }, + { + "declared_after": { + "facing": "east", + "map": "PlayerHouse1F", + "tiles": [ + [ + 6, + 8 + ] + ] + }, + "id": "rowan_and_dawn", + "kind": "scene", + "why": "Rowan and Dawn come back; Barry, then home for the Running Shoes" + }, + { + "action": "or_legs", + "args": { + "direction": "south", + "legs": [ + { + "direction": "east", + "tiles": 1 + } + ] + }, + "declared_after": { + "facing": "south", + "map": "TwinleafTown", + "tiles": [ + [ + 20, + 22 + ] + ] + }, + "id": "out_of_the_house_again", + "kind": "step", + "why": "south onto the door's tile (6, 10), and out; from column 5, east onto the mat first" + }, + { + "action": "into_scene", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 8 + } + ], + "target": { + "facing": "east", + "map": "Route201", + "tiles": [ + [ + 63, + 5 + ] + ] + } + }, + "id": "into_dawns_scene", + "kind": "route", + "why": "Twinleaf, then east along Route 201 to its edge and on into Sandgem until Dawn's scene starts" + }, + { + "action": "a", + "declared_after": { + "facing": "north", + "map": "RowansLab", + "tiles": [ + [ + 7, + 6 + ] + ] + }, + "id": "rowan_gives_pokedex", + "kind": "scene", + "why": "Dawn walks us to the lab: Rowan, no nickname, the Pokedex" + } + ], + "id": "00.03", + "start": { + "map": "VerityLakefront", + "tiles": [ + [ + 17, + 14 + ], + [ + 17, + 15 + ] + ] + }, + "title": "Back to Sandgem: Rowan, Dawn and the Pokedex" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Twinleaf Town" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04.plan.json new file mode 100644 index 0000000000..64013e707e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04.plan.json @@ -0,0 +1,568 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "direction": "south" + }, + "declared_after": { + "facing": "north", + "map": "SandgemTown", + "tiles": [ + [ + 26, + 11 + ], + [ + 27, + 11 + ], + [ + 28, + 11 + ], + [ + 26, + 12 + ], + [ + 27, + 12 + ], + [ + 28, + 12 + ] + ] + }, + "id": "out_of_the_lab", + "kind": "step", + "why": "out of the lab; Dawn's tour of Sandgem" + }, + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "SandgemTown", + "tiles": [ + [ + 29, + 12 + ] + ] + } + }, + "id": "onto_the_path", + "kind": "route", + "why": "to the east end of the path below the doors' row" + }, + { + "action": "to_map", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "west", + "tiles": 40 + } + ] + }, + "declared_after": { + "facing": "west", + "map": "Route201", + "tiles": [ + [ + 57, + 5 + ], + [ + 58, + 5 + ], + [ + 59, + 5 + ], + [ + 60, + 5 + ], + [ + 61, + 5 + ], + [ + 62, + 5 + ], + [ + 63, + 5 + ] + ] + }, + "id": "to_route_201", + "kind": "legs", + "why": "run west along the path until Route 201's banner" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 1 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "Route201", + "tiles": [ + [ + 57, + 4 + ], + [ + 58, + 4 + ], + [ + 59, + 4 + ], + [ + 60, + 4 + ], + [ + 61, + 4 + ], + [ + 62, + 4 + ], + [ + 63, + 4 + ] + ] + }, + "id": "to_the_signs_row", + "kind": "legs", + "why": "a tap north onto the border sign's row" + }, + { + "action": "line", + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "Route201", + "tiles": [ + [ + 55, + 5 + ] + ] + } + }, + "id": "to_ledge_column", + "kind": "route", + "why": "west to the border sign, a row south, west to ledge column 55" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "south", + "tiles": 8 + } + ] + }, + "declared_after": { + "facing": "south", + "map": "Route201", + "tiles": [ + [ + 55, + 14 + ] + ] + }, + "id": "over_the_ledge", + "kind": "legs", + "why": "south over the ledge to the lower road" + }, + { + "action": "line", + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "Route201", + "tiles": [ + [ + 15, + 14 + ] + ] + } + }, + "id": "along_route_201_road", + "kind": "route", + "why": "west along Route 201's road to the Twinleaf path" + }, + { + "action": "line", + "args": { + "fight_wild": false, + "target": { + "facing": "south", + "map": "TwinleafTown", + "tiles": [ + [ + 15, + 22 + ] + ] + } + }, + "id": "into_twinleaf", + "kind": "route", + "why": "south down column 15 into Twinleaf Town" + }, + { + "action": "line", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "TwinleafTown", + "tiles": [ + [ + 20, + 22 + ] + ] + } + }, + "id": "to_moms_doorstep", + "kind": "route", + "why": "east to Mom's house doorstep" + }, + { + "args": { + "direction": "north" + }, + "declared_after": { + "facing": "north", + "map": "PlayerHouse1F", + "tiles": [ + [ + 6, + 9 + ] + ] + }, + "id": "into_the_house", + "kind": "step", + "why": "the house's door" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "east", + "map": "PlayerHouse1F", + "tiles": [ + [ + 6, + 8 + ] + ] + } + }, + "id": "beside_mom", + "kind": "route", + "why": "west of Mom at (7, 8)" + }, + { + "id": "mom", + "kind": "talk", + "why": "Mom" + }, + { + "declared_after": { + "facing": "south", + "map": "PlayerHouse1F", + "tiles": [ + [ + 7, + 7 + ] + ] + }, + "id": "mom_heals_us", + "kind": "scene", + "why": "Mom heals us; Barry's mom gives us his parcel" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 4 + }, + { + "direction": "east", + "tiles": 6 + }, + { + "direction": "south", + "tiles": 4 + } + ] + }, + "declared_after": { + "facing": "south", + "map": "PlayerHouse1F", + "tiles": [ + [ + 11, + 8 + ] + ] + }, + "id": "into_the_east_corner", + "kind": "legs", + "why": "north, east and south to the walls, into the room's east corner" + }, + { + "action": "trigger", + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "PlayerHouse1F", + "tiles": [ + [ + 6, + 9 + ] + ] + } + }, + "id": "into_moms_call", + "kind": "route", + "why": "round Mom and west along row 9 until her call stops us on (6, 9)" + }, + { + "declared_after": { + "facing": "east", + "map": "TwinleafTown", + "tiles": [ + [ + 19, + 22 + ] + ] + }, + "id": "moms_hat", + "kind": "scene", + "why": "Mom's hat; she walks out with us" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "west", + "tiles": 16 + } + ] + }, + "declared_after": { + "facing": "west", + "map": "TwinleafTown", + "tiles": [ + [ + 6, + 22 + ], + [ + 6, + 23 + ] + ] + }, + "id": "to_twinleafs_west_wall", + "kind": "legs", + "why": "west along the row to the town's west wall" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "Route202", + "tiles": [ + [ + 22, + 27 + ] + ] + } + }, + "id": "short_of_the_catch_tutorial", + "kind": "route", + "why": "through Sandgem onto Route 202, east of its trigger column" + }, + { + "args": { + "direction": "west" + }, + "id": "catch_tutorial_trigger", + "kind": "step", + "why": "the trigger (21, 25..29), VAR_ROUTE_202_STATE 0" + }, + { + "action": "catch_tutorial", + "id": "catch_tutorial", + "kind": "game_action", + "why": "Dawn's catching tutorial" + }, + { + "action": "catch_pokemon", + "declared_after": { + "map": "Route202", + "tiles": [ + [ + 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 + ] + ] + }, + "id": "catch_pokemon", + "kind": "game_action", + "why": "a Pokemon of our own from the grass" + } + ], + "id": "00.04", + "start": { + "facing": "north", + "map": "RowansLab", + "tiles": [ + [ + 7, + 6 + ] + ] + }, + "title": "Home to Mom, then Dawn's catching tutorial on Route 202" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Sandgem Town" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04b.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04b.plan.json new file mode 100644 index 0000000000..769e54834e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04b.plan.json @@ -0,0 +1,303 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "fight_wild": false, + "target": { + "map": "Route202", + "tiles": [ + [ + 18, + 25 + ], + [ + 19, + 25 + ], + [ + 20, + 25 + ], + [ + 18, + 26 + ], + [ + 19, + 26 + ], + [ + 20, + 26 + ], + [ + 18, + 27 + ], + [ + 19, + 27 + ], + [ + 20, + 27 + ] + ] + } + }, + "id": "to_catch_grass", + "kind": "route", + "why": "into the training column's grass" + }, + { + "args": { + "battles": 3 + }, + "declared_after": { + "map": "Route202", + "tiles": [ + [ + 18, + 25 + ], + [ + 19, + 25 + ], + [ + 20, + 25 + ], + [ + 18, + 26 + ], + [ + 19, + 26 + ], + [ + 20, + 26 + ], + [ + 18, + 27 + ], + [ + 19, + 27 + ], + [ + 20, + 27 + ] + ] + }, + "id": "train", + "kind": "train", + "why": "three wild battles with the best move" + }, + { + "action": "robust", + "args": { + "fight_wild": false, + "target": { + "map": "SandgemTown", + "tiles": [ + [ + 29, + 5 + ] + ] + } + }, + "id": "into_sandgem", + "kind": "route", + "why": "south down the road to above the Mart" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "west", + "tiles": 12 + }, + { + "direction": "south", + "tiles": 6 + } + ] + }, + "declared_after": { + "facing": "south", + "map": "SandgemTown", + "tiles": [ + [ + 20, + 9 + ] + ] + }, + "id": "above_center_sign", + "kind": "legs", + "why": "west to the road's west wall, then south onto the Center's sign" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 1 + }, + { + "direction": "south", + "tiles": 2 + }, + { + "direction": "west", + "tiles": 4 + } + ] + }, + "id": "below_center_door", + "kind": "legs", + "why": "taps round the sign to below the Center's door" + }, + { + "declared_after": { + "map": "SandgemTown", + "tiles": [ + [ + 16, + 11 + ], + [ + 17, + 11 + ], + [ + 18, + 11 + ] + ] + }, + "id": "heal_sandgem", + "kind": "heal", + "why": "Sandgem Town Pokemon Center" + } + ], + "id": "00.04b", + "start": { + "map": "Route202", + "tiles": [ + [ + 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 + ] + ] + }, + "title": "After the catch: train and heal in Sandgem" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Route 202" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04c.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04c.plan.json new file mode 100644 index 0000000000..80ca68d8cf --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04c.plan.json @@ -0,0 +1,57 @@ +{ + "checkpoint": { + "events": [ + { + "action": "sandgem_potions", + "args": { + "items": [ + { + "item": "potion", + "quantity": 10 + } + ] + }, + "declared_after": { + "map": "Route202", + "tiles": [ + [ + 30, + 24 + ] + ] + }, + "id": "potions", + "kind": "shop", + "why": "Potions before Route 202's trainers: the Poke Mart lady's 10 (she stands still, as in the decomp)" + } + ], + "id": "00.04c", + "start": { + "map": "SandgemTown", + "tiles": [ + [ + 16, + 11 + ], + [ + 17, + 11 + ], + [ + 18, + 11 + ] + ] + }, + "title": "Poke Mart lady for Potions, back to Route 202's save spot" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Sandgem Town" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04d.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04d.plan.json new file mode 100644 index 0000000000..51020597df --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.04d.plan.json @@ -0,0 +1,196 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "fight_wild": false, + "target": { + "map": "Route202", + "tiles": [ + [ + 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 + ] + ] + } + }, + "id": "north_to_jubilife", + "kind": "route", + "why": "north past Route 202's trainers, fighting who sees us" + }, + { + "action": "jubilife_stairs", + "declared_after": { + "map": "JubilifeCity", + "tiles": [ + [ + 46, + 58 + ] + ] + }, + "id": "jubilife_stairs", + "kind": "game_action", + "why": "Jubilife's south edge is a wall with stairs in it, west of where the route may leave us (r78)" + }, + { + "declared_after": { + "map": "JubilifeCity", + "tiles": [ + [ + 46, + 58 + ] + ] + }, + "id": "dawn_scene", + "kind": "scene", + "why": "Dawn walks over while we stand still" + } + ], + "id": "00.04d", + "start": { + "map": "Route202", + "tiles": [ + [ + 30, + 24 + ] + ] + }, + "title": "Route 202's trainers, Dawn in Jubilife" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Route 202" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05.plan.json new file mode 100644 index 0000000000..0753479fbd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05.plan.json @@ -0,0 +1,425 @@ +{ + "checkpoint": { + "events": [ + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 4 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 43, + 51 + ], + [ + 44, + 51 + ], + [ + 45, + 51 + ], + [ + 46, + 51 + ], + [ + 47, + 51 + ], + [ + 48, + 51 + ], + [ + 49, + 51 + ], + [ + 43, + 52 + ], + [ + 44, + 52 + ], + [ + 45, + 52 + ], + [ + 46, + 52 + ], + [ + 47, + 52 + ], + [ + 48, + 52 + ], + [ + 49, + 52 + ], + [ + 44, + 53 + ], + [ + 45, + 53 + ], + [ + 46, + 53 + ], + [ + 47, + 53 + ], + [ + 48, + 53 + ], + [ + 43, + 54 + ], + [ + 44, + 54 + ], + [ + 45, + 54 + ], + [ + 46, + 54 + ], + [ + 47, + 54 + ], + [ + 48, + 54 + ], + [ + 49, + 54 + ], + [ + 43, + 55 + ], + [ + 44, + 55 + ], + [ + 45, + 55 + ], + [ + 46, + 55 + ], + [ + 47, + 55 + ], + [ + 48, + 55 + ], + [ + 49, + 55 + ], + [ + 43, + 56 + ], + [ + 44, + 56 + ], + [ + 45, + 56 + ], + [ + 46, + 56 + ], + [ + 47, + 56 + ], + [ + 48, + 56 + ], + [ + 49, + 56 + ], + [ + 43, + 57 + ], + [ + 44, + 57 + ], + [ + 45, + 57 + ], + [ + 46, + 57 + ], + [ + 47, + 57 + ], + [ + 48, + 57 + ], + [ + 49, + 57 + ] + ] + }, + "id": "north_onto_the_plaza", + "kind": "legs", + "why": "4 taps up from (46, 58) onto row 54, just below the lamps on row 53 (console, lab-B 2026-11-11); from 3 tiles off any way the start may be, it ends on rows 51 to 57" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 10 + } + ] + }, + "declared_after": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 52, + 51 + ], + [ + 52, + 52 + ], + [ + 52, + 53 + ], + [ + 52, + 54 + ], + [ + 52, + 55 + ], + [ + 52, + 56 + ], + [ + 52, + 57 + ] + ] + }, + "id": "east_to_the_wall", + "kind": "legs", + "why": "the small brick building's west wall (x 53, rows 51 to 57; on console against it from row 54) pins the column" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "west", + "tiles": 1 + } + ] + }, + "id": "off_the_wall", + "kind": "legs", + "why": "a column west, out of the way of the child north of column 52" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 20 + } + ] + }, + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 51, + 41 + ] + ] + }, + "id": "north_to_the_center", + "kind": "legs", + "why": "up column 51 to the Center's front wall: a tile west of its door (52, 40)" + }, + { + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "east", + "tiles": 1 + } + ] + }, + "id": "below_the_centers_door", + "kind": "legs", + "why": "east under the door (52, 40)" + }, + { + "id": "heal_jubilife", + "kind": "heal", + "why": "in, the nurse, out onto the doorstep (r143a: check we are out)" + }, + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 40, + 41 + ] + ] + } + }, + "id": "to_the_school", + "kind": "route", + "why": "below the School's door (40, 40)" + }, + { + "args": { + "direction": "north" + }, + "declared_after": { + "map": "TrainersSchool", + "tiles": [] + }, + "id": "into_the_school", + "kind": "step", + "why": "the School's door" + }, + { + "action": "to_wall", + "args": { + "fight_wild": false, + "legs": [ + { + "direction": "north", + "tiles": 10 + } + ] + }, + "declared_after": { + "facing": "west", + "map": "TrainersSchool", + "tiles": [] + }, + "id": "up_to_barry", + "kind": "legs", + "why": "the door's column to the back wall, beside Barry" + }, + { + "id": "barry", + "kind": "talk", + "why": "Barry, west of us at the blackboard" + }, + { + "action": "a", + "declared_after": { + "map": "TrainersSchool", + "tiles": [] + }, + "id": "barry_town_map", + "kind": "scene", + "why": "his lines to \"You obtained a Town Map!\", closed with A" + }, + { + "args": { + "direction": "south" + }, + "declared_after": { + "facing": "south", + "map": "JubilifeCity", + "tiles": [ + [ + 40, + 41 + ] + ] + }, + "id": "out_of_the_school", + "kind": "step", + "why": "straight down the door's column and out" + } + ], + "id": "00.05", + "start": { + "map": "JubilifeCity", + "tiles": [ + [ + 46, + 58 + ] + ] + }, + "title": "Jubilife: the Center, the Trainers' School and the Town Map" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Jubilife City" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05b.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05b.plan.json new file mode 100644 index 0000000000..369fbf125f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05b.plan.json @@ -0,0 +1,308 @@ +{ + "checkpoint": { + "events": [ + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 46, + 41 + ] + ] + } + }, + "id": "below_the_presidents_trigger", + "kind": "route", + "why": "east along the School's row" + }, + { + "args": { + "direction": "north" + }, + "id": "presidents_trigger", + "kind": "step", + "why": "the trigger (44..48, 40)" + }, + { + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 46, + 40 + ] + ] + }, + "id": "president_walks_up", + "kind": "scene", + "why": "he walks down to (46, 39) and sends us for the coupons" + }, + { + "args": { + "fight_wild": false, + "target": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 48, + 40 + ] + ] + } + }, + "id": "past_the_president", + "kind": "route", + "why": "east of his column" + }, + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 56, + 36 + ] + ] + } + }, + "id": "below_the_mart_clown", + "kind": "route", + "why": "along row 36, below the boy by the Mart, to the wall east of the Center" + }, + { + "action": "wait_for_wanderer", + "args": { + "far_stand": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 54, + 32 + ], + [ + 54, + 33 + ], + [ + 54, + 34 + ], + [ + 54, + 33 + ] + ] + }, + "stand": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 56, + 33 + ], + [ + 56, + 34 + ], + [ + 56, + 33 + ], + [ + 56, + 32 + ] + ] + } + }, + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 56, + 31 + ] + ] + }, + "id": "mart_clown", + "kind": "talk", + "why": "the clown by the Mart (wanders 54..56, 32..34): wait in his box's east column facing west, then its west column facing east, a D-pad tap a tile along it and two A presses a tile; 'Do Pokemon grow by defeating others?' Yes; Coupon 1. Ends north against the wall at (56, 30)" + }, + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 36, + 17 + ] + ] + } + }, + "id": "below_the_tv_clown", + "kind": "route", + "why": "west along row 31, above the boy below the Mart, and north to the TV station" + }, + { + "id": "tv_clown", + "kind": "talk", + "why": "the clown below the TV station's door" + }, + { + "action": "a", + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 36, + 17 + ] + ] + }, + "id": "tv_clowns_coupon", + "kind": "scene", + "why": "\"Can a Pokemon hold an item?\" Yes; Coupon 3" + }, + { + "action": "town", + "args": { + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 15, + 19 + ] + ] + } + }, + "id": "below_the_company_clown", + "kind": "route", + "why": "west along the long road, north beside the sign" + }, + { + "action": "wait_for_wanderer", + "args": { + "far_stand": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 16, + 18 + ], + [ + 15, + 18 + ] + ] + }, + "stand": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 14, + 18 + ], + [ + 15, + 18 + ] + ] + } + }, + "declared_after": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 14, + 19 + ] + ] + }, + "id": "company_clown", + "kind": "talk", + "why": "the clown by the Poketch Company (wanders 14..16, 18): wait in his row, from (14, 18) and (15, 18) facing east, then (16, 18) and (15, 18) facing west, a tile toward him at a time; 'Do moves have types?' Yes; Coupon 2. Ends west against the company's sign (13, 19)" + }, + { + "action": "line", + "args": { + "fight_wild": false, + "target": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 14, + 19 + ] + ] + } + }, + "declared_after": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 14, + 19 + ] + ] + }, + "id": "beside_the_company_sign", + "kind": "route", + "why": "west 1 against the Poketch Company's signboard (13, 19): the bump pins the tile the save is made on (console 2026-11-11: a save believed at (15, 19) was in the Poketch lobby)" + }, + { + "action": "check_coupons", + "id": "all_three_coupons", + "kind": "game_action", + "why": "Key Items hold Coupons 1, 2 and 3, or the row fails before its save: the president gives the Poketch for all three only" + } + ], + "id": "00.05b", + "start": { + "facing": "south", + "map": "JubilifeCity", + "tiles": [ + [ + 40, + 41 + ] + ] + }, + "title": "Jubilife: the president and the clowns' coupons" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Jubilife City" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json new file mode 100644 index 0000000000..37a8be0f66 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/StoryData/bdsp-00.05c.plan.json @@ -0,0 +1,149 @@ +{ + "checkpoint": { + "events": [ + { + "action": "jubilife_start_probe", + "id": "where_we_start", + "kind": "game_action", + "why": "a step west: the Poketch Company's sign (14, 19) blocks it (00.05b's end); below the east-exit sign (59, 21) it goes (a rerun from 01.01's start when no Poketch came)" + }, + { + "action": "town", + "args": { + "avoid": [ + [ + 40, + 20, + 52, + 25 + ], + [ + 40, + 26, + 43, + 27 + ] + ], + "fight_wild": false, + "greeter": [ + [ + 30, + 22, + 48, + 34 + ] + ], + "target": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 45, + 35 + ] + ] + } + }, + "id": "beside_the_president", + "kind": "route", + "why": "from the Poketch Company's sign (00.05b's end; 00.05b checks all three coupons are in the Bag) or the street east, round the plaza to the Poketch president" + }, + { + "id": "president", + "kind": "talk", + "why": "the Poketch Company's president at (46, 35), level with (45, 35)" + }, + { + "declared_after": { + "facing": "east", + "map": "JubilifeCity", + "tiles": [ + [ + 45, + 35 + ] + ] + }, + "id": "the_poketch", + "kind": "scene", + "why": "the coupons for the Poketch; he walks off north" + }, + { + "action": "check_poketch", + "id": "poketch", + "kind": "game_action", + "why": "the Poketch on the screen (R): without it the president had too few coupons, and Jubilife's east-exit guard (60, 21..24) turns us back in 01.01" + }, + { + "action": "line", + "args": { + "avoid": [ + [ + 40, + 20, + 52, + 25 + ], + [ + 40, + 26, + 43, + 27 + ], + [ + 60, + 18, + 70, + 30 + ] + ], + "fight_wild": false, + "target": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 59, + 21 + ] + ] + } + }, + "id": "below_the_east_exit_sign", + "kind": "route", + "why": "up the main road and along the street to below the east-exit sign (59, 21) facing north, west of the guard's trigger column 60: tapped legs (each a whole tile, a turn tap first) and legs to a wall, none a held leg ended in the open (lab-B 2026-11-12 13:55)", + "declared_after": { + "facing": "north", + "map": "JubilifeCity", + "tiles": [ + [ + 59, + 21 + ] + ] + } + } + ], + "id": "00.05c", + "start": { + "facing": "west", + "map": "JubilifeCity", + "tiles": [ + [ + 14, + 19 + ] + ] + }, + "title": "Jubilife: the clowns' coupons again, the Poketch, up to the street east to Route 203" + }, + "execution": { + "on_failure": "reload_and_retry", + "saves_in": "Jubilife City" + }, + "format": "pokesim-authored-checkpoint/1", + "game": "bdsp", + "metadata": { + "phase": "reproduction" + } +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.cpp new file mode 100644 index 0000000000..2e11e5261b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.cpp @@ -0,0 +1,77 @@ +/* BDSP RNG Aim + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/PrettyPrint.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "PokemonBDSP_RngAim.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +uint64_t aimed_advance(uint64_t press, uint64_t span, int64_t bias){ + int64_t advance = (int64_t)(press + span) + bias; + return advance < 0 ? 0 : (uint64_t)advance; +} + +int64_t press_for_advance(uint64_t target, uint64_t span, int64_t bias){ + return (int64_t)target - (int64_t)span - bias; +} + + +RngAim::RngAim(size_t minimum_samples, double threshold) + : m_minimum_samples(minimum_samples) + , m_threshold(threshold) +{} + +void RngAim::reset(){ + m_bias = 0; + m_offsets.clear(); +} + +bool RngAim::record_offset(Logger& logger, int64_t offset, bool enabled){ + m_offsets.emplace_back(offset); + if (!enabled || m_offsets.size() < m_minimum_samples){ + return false; + } + double mean = 0; + for (int64_t o : m_offsets){ + mean += (double)o; + } + mean /= (double)m_offsets.size(); + if (std::abs(mean) < m_threshold){ + return false; + } + int64_t shift = (int64_t)std::llround(mean); + if (shift == 0){ + return false; + } + m_bias += shift; + logger.log( + std::to_string(m_offsets.size()) + " measured attempt(s) average " + + tostr_fixed(mean, 2) + " advance(s) off, so the aim shifts by " + + std::to_string(shift) + " to " + std::to_string(m_bias) + ".", + COLOR_BLUE + ); + m_offsets.clear(); + return true; +} + +std::string RngAim::describe_correction() const{ + if (m_bias == 0){ + return ""; + } + return ", plus " + std::to_string(m_bias) + " measured"; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.h new file mode 100644 index 0000000000..608f448315 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.h @@ -0,0 +1,52 @@ +/* BDSP RNG Aim + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngAim_H +#define PokemonAutomation_PokemonBDSP_RngAim_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ + class Logger; +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The advance where a press will generate the target Pokemon +uint64_t aimed_advance(uint64_t press, uint64_t span, int64_t bias); + +// The press that would reach "target" through a schedule of a certain length +int64_t press_for_advance(uint64_t target, uint64_t span, int64_t bias); + + +class RngAim{ +public: + explicit RngAim(size_t minimum_samples = 3, double threshold = 1.0); + + int64_t bias() const{ return m_bias; } + + void reset(); + + bool record_offset(Logger& logger, int64_t offset, bool enabled); + + std::string describe_correction() const; + +private: + size_t m_minimum_samples; + double m_threshold; + + int64_t m_bias = 0; + std::vector m_offsets; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.cpp new file mode 100644 index 0000000000..1e427daf5a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.cpp @@ -0,0 +1,188 @@ +/* BDSP RNG Calibration + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_RngCalibration.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +NatureAdjustment bdsp_nature_adjustment(uint8_t nature, size_t stat_index){ + if (nature >= 25 || stat_index >= 5){ + return NatureAdjustment::NEUTRAL; + } + size_t raised = nature / 5; + size_t lowered = nature % 5; + if (raised == lowered){ + // Hardy, Docile, Serious, Bashful, Quirky. + return NatureAdjustment::NEUTRAL; + } + if (stat_index == raised){ + return NatureAdjustment::POSITIVE; + } + if (stat_index == lowered){ + return NatureAdjustment::NEGATIVE; + } + return NatureAdjustment::NEUTRAL; +} + + +// The nature index orders stats [Atk, Def, Spe, SpA, SpD]; +static const size_t NATURE_INDEX_ATTACK = 0; +static const size_t NATURE_INDEX_DEFENSE = 1; +static const size_t NATURE_INDEX_SPEED = 2; +static const size_t NATURE_INDEX_SPATK = 3; +static const size_t NATURE_INDEX_SPDEF = 4; + + +StatReads bdsp_expected_stats( + const BdspPokemonResult& generated, + const BaseStats& base_stats, + uint8_t level +){ + StatReads ret; + ret.hp = (int16_t)calc_stats_hp(base_stats.hp, level, generated.ivs.hp, 0); + ret.attack = (int16_t)calc_stats_nonhp( + base_stats.attack, level, generated.ivs.attack, 0, + bdsp_nature_adjustment(generated.nature, NATURE_INDEX_ATTACK) + ); + ret.defense = (int16_t)calc_stats_nonhp( + base_stats.defense, level, generated.ivs.defense, 0, + bdsp_nature_adjustment(generated.nature, NATURE_INDEX_DEFENSE) + ); + ret.spatk = (int16_t)calc_stats_nonhp( + base_stats.spatk, level, generated.ivs.spatk, 0, + bdsp_nature_adjustment(generated.nature, NATURE_INDEX_SPATK) + ); + ret.spdef = (int16_t)calc_stats_nonhp( + base_stats.spdef, level, generated.ivs.spdef, 0, + bdsp_nature_adjustment(generated.nature, NATURE_INDEX_SPDEF) + ); + ret.speed = (int16_t)calc_stats_nonhp( + base_stats.speed, level, generated.ivs.speed, 0, + bdsp_nature_adjustment(generated.nature, NATURE_INDEX_SPEED) + ); + return ret; +} + + +bool advances_between( + const Xorshift128State& from, + const Xorshift128State& to, + uint64_t search_max, + uint64_t& advances +){ + Xorshift128 rng(from); + for (uint64_t c = 0; c <= search_max; c++){ + if (rng.state() == to){ + advances = c; + return true; + } + rng.next(); + } + return false; +} + + +const BaseStats& starter_base_stats(BdspStarter starter){ + // Order: HP, Atk, Def, SpA, SpD, Spe. + static const BaseStats TURTWIG {55, 68, 64, 45, 55, 31}; + static const BaseStats CHIMCHAR{44, 58, 44, 58, 44, 61}; + static const BaseStats PIPLUP {53, 51, 53, 61, 56, 40}; + switch (starter){ + case BdspStarter::Turtwig: return TURTWIG; + case BdspStarter::Chimchar: return CHIMCHAR; + case BdspStarter::Piplup: return PIPLUP; + } + return TURTWIG; +} + + +// A reading of -1 was not taken, so it rules nothing out. +static bool stat_fits(int16_t observed, int16_t expected){ + return observed < 0 || observed == expected; +} + + +bool consistent_with( + const BdspPokemonResult& generated, + const BdspObservedStarter& observed +){ + if (observed.nature != NatureCheckerValue::UnableToDetect + && bdsp_nature_to_checker_value(generated.nature) != observed.nature + ){ + return false; + } + if (observed.gender_known && generated.gender != observed.gender){ + return false; + } + if (observed.shiny_known && (generated.shiny != BdspShiny::None) != observed.shiny){ + return false; + } + + StatReads expected = bdsp_expected_stats(generated, observed.base_stats, observed.level); + return stat_fits(observed.stats.hp, expected.hp) + && stat_fits(observed.stats.attack, expected.attack) + && stat_fits(observed.stats.defense, expected.defense) + && stat_fits(observed.stats.spatk, expected.spatk) + && stat_fits(observed.stats.spdef, expected.spdef) + && stat_fits(observed.stats.speed, expected.speed); +} + + +BdspHitIdentification identify_hit_advance( + const Xorshift128State& state, + const BdspStaticTemplate& tmpl, + uint64_t intended_advance, + uint64_t radius, + const BdspObservedStarter& observed +){ + BdspHitIdentification ret; + + uint64_t first = intended_advance > radius ? intended_advance - radius : 0; + uint64_t last = intended_advance + radius; + + BdspStaticSearcher searcher(state, tmpl, 0); + + std::vector matches = searcher.scan( + first, last, + [&](const BdspPokemonResult& candidate){ return consistent_with(candidate, observed); } + ); + ret.candidates = matches.size(); + + if (ret.candidates == 0){ + ret.failure_reason = "no advance within " + std::to_string(radius) + + " of " + std::to_string(intended_advance) + + " produces that Pokemon. Either the recovered state was wrong, or the press " + "missed by more than the search covered."; + return ret; + } + if (ret.candidates > 1){ + ret.failure_reason = std::to_string(ret.candidates) + + " advances fit what was read ("; + for (size_t c = 0; c < matches.size(); c++){ + ret.failure_reason += (c == 0 ? "" : ", ") + std::to_string(matches[c].advances); + } + ret.failure_reason += "), so which one was hit cannot be told apart here."; + return ret; + } + + ret.success = true; + ret.advance = matches[0].advances; + ret.offset = (int64_t)ret.advance - (int64_t)intended_advance; + return ret; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.h new file mode 100644 index 0000000000..8687e695de --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.h @@ -0,0 +1,79 @@ +/* BDSP RNG Calibration + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngCalibration_H +#define PokemonAutomation_PokemonBDSP_RngCalibration_H + +#include +#include +#include "Pokemon/Pokemon_BdspRng.h" +#include "Pokemon/Pokemon_NatureChecker.h" +#include "Pokemon/Pokemon_StatsCalculation.h" +#include "PokemonBDSP_StarterNavigation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +Pokemon::NatureAdjustment bdsp_nature_adjustment(uint8_t nature, size_t stat_index); + +Pokemon::StatReads bdsp_expected_stats( + const Pokemon::BdspPokemonResult& generated, + const Pokemon::BaseStats& base_stats, + uint8_t level +); + +struct BdspObservedStarter{ + Pokemon::NatureCheckerValue nature = Pokemon::NatureCheckerValue::UnableToDetect; + Pokemon::BdspGender gender = Pokemon::BdspGender::Genderless; + bool gender_known = false; + Pokemon::StatReads stats; + Pokemon::BaseStats base_stats; + uint8_t level = 5; + bool shiny = false; + bool shiny_known = false; +}; + + +struct BdspHitIdentification{ + bool success = false; + uint64_t advance = 0; + int64_t offset = 0; + size_t candidates = 0; + std::string failure_reason; +}; + + +BdspHitIdentification identify_hit_advance( + const Pokemon::Xorshift128State& state, + const Pokemon::BdspStaticTemplate& tmpl, + uint64_t intended_advance, + uint64_t radius, + const BdspObservedStarter& observed +); + + +bool advances_between( + const Pokemon::Xorshift128State& from, + const Pokemon::Xorshift128State& to, + uint64_t search_max, + uint64_t& advances +); + + +const Pokemon::BaseStats& starter_base_stats(BdspStarter starter); + +bool consistent_with( + const Pokemon::BdspPokemonResult& generated, + const BdspObservedStarter& observed +); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.cpp new file mode 100644 index 0000000000..6827c5a889 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.cpp @@ -0,0 +1,130 @@ +/* BDSP RNG Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/PrettyPrint.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP_RngExecution.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +static size_t first_free_blink(const BdspTimelineContext& timeline){ + size_t before_blinks = 1 + (timeline.plus_one_on_menu_close ? 1 : 0) + + timeline.advance_delay + timeline.pokemon_models; + size_t last_waited = before_blinks + timeline.advance_delay_2_after_events - 1; + return last_waited + 1 + timeline.advance_delay_2; +} + + +static size_t timeline_depth(const BdspTimelineContext& timeline, size_t max_schedules){ + return first_free_blink(timeline) + 4 * max_schedules + 2; +} + +void schedule_span_bounds( + const BdspTimelineContext& timeline, size_t max_schedules, + uint64_t& lowest, uint64_t& highest +){ + lowest = first_free_blink(timeline) + 1 + timeline.advances_after_accept; + highest = timeline_depth(timeline, max_schedules) - 1 + timeline.advances_after_accept; +} + + +std::vector schedule_presses( + const Pokemon::Xorshift128State& state, + const BdspTimelineContext& timeline, + const NavigationTimings& timings, + uint64_t press, + size_t max_schedules +){ + std::vector schedules; + + size_t first_free = first_free_blink(timeline); + size_t last_waited = first_free - 1 - timeline.advance_delay_2; + + Pokemon::Xorshift128 rng(state); + rng.advance(press); + BdspTimelineResult simulated = simulate_timeline( + rng, timeline, timeline_depth(timeline, max_schedules) + ); + const std::vector& at = simulated.advance_times; + if (at.size() < first_free + 2){ + return schedules; + } + + // The second press goes between the last blink it waits for and the next. + double starly = (at[last_waited] + at[last_waited + 1]) / 2; + if (starly < timings.ready_seconds){ + return schedules; + } + double earliest_select = starly + timings.move_seconds; + + for (size_t blink = first_free; + blink + 1 < at.size() && schedules.size() < max_schedules; + blink++ + ){ + double gap_start = at[blink]; + double gap_end = at[blink + 1]; + + // Centred in the gap when there is room, pushed as late as the navigation + // demands when there is not. + double select_at = std::max( + (gap_start + gap_end - timings.prompt_seconds) / 2, earliest_select + ); + double confirm_at = select_at + timings.prompt_seconds; + if (select_at - gap_start < timings.guard_seconds + || gap_end - confirm_at < timings.guard_seconds + ){ + continue; + } + + schedules.push_back(PressSchedule{ + /*span*/ blink + 1 + timeline.advances_after_accept, + /*starly_seconds*/ starly, + /*select_seconds*/ select_at, + /*confirm_seconds*/ confirm_at, + /*blinks_before_confirm*/ blink + 1 - first_free, + }); + } + + return schedules; +} + + +bool wait_until_moment(ProControllerContext& context, Logger& logger, WallClock when){ + context.wait_for_all_requests(); + + WallClock now = current_time(); + if (when < now){ + double late = std::chrono::duration_cast>( + now - when + ).count(); + logger.log( + "The moment passed " + tostr_fixed(late, 3) + " seconds ago. " + "Abandoning the attempt rather than starting late.", + COLOR_ORANGE + ); + return false; + } + + Milliseconds wait = std::chrono::duration_cast(when - now); + pbf_wait(context, wait); + logger.log("Waiting " + std::to_string(wait.count()) + " ms for the timeline.", + COLOR_BLUE); + return true; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.h new file mode 100644 index 0000000000..c5ab0d7ebe --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.h @@ -0,0 +1,59 @@ +/* BDSP RNG Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngExecution_H +#define PokemonAutomation_PokemonBDSP_RngExecution_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP_RngTimeline.h" + +namespace PokemonAutomation{ + class Logger; +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +struct PressSchedule{ + uint64_t span = 0; + double starly_seconds = 0; + double select_seconds = 0; + double confirm_seconds = 0; + uint64_t blinks_before_confirm = 0; +}; + +struct NavigationTimings{ + double ready_seconds = 0; + double move_seconds = 0; + double prompt_seconds = 0; + double guard_seconds = 0.30; +}; + + +void schedule_span_bounds( + const BdspTimelineContext& timeline, size_t max_schedules, + uint64_t& lowest, uint64_t& highest +); + +std::vector schedule_presses( + const Pokemon::Xorshift128State& state, + const BdspTimelineContext& timeline, + const NavigationTimings& timings, + uint64_t press, + size_t max_schedules +); + + +bool wait_until_moment(ProControllerContext& context, Logger& logger, WallClock when); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.cpp new file mode 100644 index 0000000000..b0d9785d0a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.cpp @@ -0,0 +1,155 @@ +/* BDSP RNG Targets + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "PokemonBDSP_RngTargets.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +static BdspStaticTemplate starter_template(){ + BdspStaticTemplate ret; + ret.species = "starter"; + ret.level = 5; + ret.guaranteed_ivs = 0; + ret.ability_kind = 3; + ret.gender_ratio = 31; // Seven males to one female. + return ret; +} + +static BdspStaticTemplate legendary_template(const char* species, uint8_t level){ + BdspStaticTemplate ret; + ret.species = species; + ret.level = level; + ret.guaranteed_ivs = 3; + ret.ability_kind = 3; + ret.gender_ratio = 255; // Genderless. + return ret; +} + + +static const BdspRngTargetInfo& target_table(BdspRngTarget target){ + static const BdspRngTargetInfo STARTER{ + "starter", "Starter", + starter_template(), + BdspTimelineContext{ + /*npcs*/ 0, + /*pokemon_models*/ 2, + /*white_delay_seconds*/ 0.0, + /*advance_delay*/ 41, + /*advance_delay_2*/ 48, + /*advance_delay_2_after_events*/ 10, + /*plus_one_on_menu_close*/ false, + /*advances_after_accept*/ 65, + }, + /*observation_npcs*/ 2, + /*has_timeline*/ true, + }; + + static const BdspRngTargetInfo DIALGA{ + "dialga", "Dialga", + legendary_template("dialga", 47), + BdspTimelineContext{1, 1, 2.0, 0, 0, 10, false}, + /*observation_npcs*/ 1, + /*has_timeline*/ true, + }; + static const BdspRngTargetInfo PALKIA{ + "palkia", "Palkia", + legendary_template("palkia", 47), + BdspTimelineContext{1, 1, 2.0, 0, 0, 10, false}, + 1, true, + }; + + static const BdspRngTargetInfo GIRATINA{ + "giratina", "Giratina", + legendary_template("giratina", 47), + BdspTimelineContext{1, 1, 2.0, 2, 0, 10, false}, + 1, true, + }; + + static const BdspRngTargetInfo REGIROCK{ + "regirock", "Regirock", + legendary_template("regirock", 30), + BdspTimelineContext{1, 1, 3.0, 3, 0, 10, false}, + 1, true, + }; + static const BdspRngTargetInfo REGICE{ + "regice", "Regice", + legendary_template("regice", 30), + BdspTimelineContext{1, 1, 3.0, 3, 0, 10, false}, + 1, true, + }; + static const BdspRngTargetInfo REGISTEEL{ + "registeel", "Registeel", + legendary_template("registeel", 30), + BdspTimelineContext{1, 1, 3.0, 3, 0, 10, false}, + 1, true, + }; + + static const BdspRngTargetInfo CRESSELIA{ + "cresselia", "Cresselia", + [](){ + BdspStaticTemplate ret = legendary_template("cresselia", 50); + ret.gender_ratio = 254; // Always female. + ret.roamer = true; + return ret; + }(), + BdspTimelineContext{}, + 1, false, + }; + + static const BdspRngTargetInfo TRAINER_ID{ + "trainer-id", "Trainer ID / SID", + BdspStaticTemplate{}, + BdspTimelineContext{}, + /*observation_npcs*/ 0, + /*has_timeline*/ false, + }; + + switch (target){ + case BdspRngTarget::Starter: return STARTER; + case BdspRngTarget::Dialga: return DIALGA; + case BdspRngTarget::Palkia: return PALKIA; + case BdspRngTarget::Giratina: return GIRATINA; + case BdspRngTarget::Regirock: return REGIROCK; + case BdspRngTarget::Regice: return REGICE; + case BdspRngTarget::Registeel: return REGISTEEL; + case BdspRngTarget::Cresselia: return CRESSELIA; + case BdspRngTarget::TrainerId: return TRAINER_ID; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown RNG target."); +} + +const BdspRngTargetInfo& bdsp_rng_target_info(BdspRngTarget target){ + return target_table(target); +} + + +const EnumDropdownDatabase& BdspRngTarget_Database(){ + static const EnumDropdownDatabase database{ + {BdspRngTarget::Starter, "starter", "Starter"}, + {BdspRngTarget::Dialga, "dialga", "Dialga"}, + {BdspRngTarget::Palkia, "palkia", "Palkia"}, + {BdspRngTarget::Giratina, "giratina", "Giratina"}, + {BdspRngTarget::Regirock, "regirock", "Regirock"}, + {BdspRngTarget::Regice, "regice", "Regice"}, + {BdspRngTarget::Registeel, "registeel", "Registeel"}, + {BdspRngTarget::Cresselia, "cresselia", "Cresselia"}, + {BdspRngTarget::TrainerId, "trainer-id", "Trainer ID / SID"}, + }; + return database; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.h new file mode 100644 index 0000000000..f3f557f881 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.h @@ -0,0 +1,52 @@ +/* BDSP RNG Targets + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngTargets_H +#define PokemonAutomation_PokemonBDSP_RngTargets_H + +#include +#include +#include "Common/Cpp/Options/EnumDropdownDatabase.h" +#include "Pokemon/Pokemon_BdspRng.h" +#include "PokemonBDSP_RngTimeline.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +enum class BdspRngTarget{ + Starter, + Dialga, + Palkia, + Giratina, + Regirock, + Regice, + Registeel, + Cresselia, + TrainerId, +}; + + +struct BdspRngTargetInfo{ + const char* slug; + const char* display_name; + Pokemon::BdspStaticTemplate pokemon; + BdspTimelineContext timeline; + uint8_t observation_npcs; + bool has_timeline; +}; + + +const BdspRngTargetInfo& bdsp_rng_target_info(BdspRngTarget target); + +const EnumDropdownDatabase& BdspRngTarget_Database(); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.cpp new file mode 100644 index 0000000000..99d0dfee73 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.cpp @@ -0,0 +1,140 @@ +/* BDSP RNG Timeline + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "PokemonBDSP_BlinkModel.h" +#include "PokemonBDSP_RngTimeline.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +namespace{ + +enum class TimelineEventType : uint8_t{ + NpcTick = 0, + PokemonBlink = 1, +}; + +struct TimelineEvent{ + double time_seconds; + TimelineEventType type; + uint8_t index; + + // Member order is the tie-break + auto operator<=>(const TimelineEvent&) const = default; +}; + +using TimelineQueue = std::priority_queue< + TimelineEvent, std::vector, std::greater +>; + +} + + +static void spend_advances( + BdspTimelineResult& result, Xorshift128& rng, + double now_seconds, uint64_t count +){ + for (uint64_t c = 0; c < count; c++){ + rng.next(); + result.advance_times.emplace_back(now_seconds); + } +} + + +BdspTimelineResult simulate_timeline( + Xorshift128 rng, + const BdspTimelineContext& context, + uint64_t max_advances +){ + BdspTimelineResult result; + double now = 0.0; + + spend_advances(result, rng, now, 1); + if (context.plus_one_on_menu_close){ + spend_advances(result, rng, now, 1); + } + + now += context.white_delay_seconds; + + spend_advances(result, rng, now, context.advance_delay); + + TimelineQueue queue; + for (uint8_t c = 0; c < context.npcs; c++){ + queue.push(TimelineEvent{now + BDSP_NPC_TICK_SECONDS, TimelineEventType::NpcTick, c}); + } + for (uint8_t c = 0; c < context.pokemon_models; c++){ + double interval = bdsp_pokemon_blink_interval(rng.next()); + result.advance_times.emplace_back(now); + queue.push(TimelineEvent{now + interval, TimelineEventType::PokemonBlink, c}); + } + + uint64_t events_handled = 0; + bool second_delay_spent = false; + + while (result.advance_times.size() < max_advances){ + if (queue.empty()){ + result.end_state = rng.state(); + return result; + } + + TimelineEvent event = queue.top(); + queue.pop(); + now = event.time_seconds; + + if (context.advance_delay_2 != 0 + && !second_delay_spent + && events_handled >= context.advance_delay_2_after_events + ){ + spend_advances(result, rng, now, context.advance_delay_2); + second_delay_spent = true; + } + + switch (event.type){ + case TimelineEventType::NpcTick: + rng.next(); + result.advance_times.emplace_back(now); + event.time_seconds += BDSP_NPC_TICK_SECONDS; + break; + + case TimelineEventType::PokemonBlink: + event.time_seconds += bdsp_pokemon_blink_interval(rng.next()); + result.advance_times.emplace_back(now); + break; + } + + queue.push(event); + events_handled++; + } + + result.end_state = rng.state(); + result.reached_target = true; + return result; +} + + +bool time_of_advance(const BdspTimelineResult& result, uint64_t advance, double& seconds){ + if (advance >= result.advance_times.size()){ + return false; + } + seconds = result.advance_times[(size_t)advance]; + return true; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.h new file mode 100644 index 0000000000..17bfffa6c7 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.h @@ -0,0 +1,57 @@ +/* BDSP RNG Timeline + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngTimeline_H +#define PokemonAutomation_PokemonBDSP_RngTimeline_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/Pokemon_Xorshift128.h" +#include "PokemonBDSP_BlinkModel.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +struct BdspTimelineContext{ + uint8_t npcs = 1; + uint8_t pokemon_models = 0; + double white_delay_seconds = 0; + uint32_t advance_delay = 0; + uint32_t advance_delay_2 = 0; + uint32_t advance_delay_2_after_events = 10; + bool plus_one_on_menu_close = false; + uint32_t advances_after_accept = 0; +}; + + +struct BdspTimelineResult{ + std::vector advance_times; + Pokemon::Xorshift128State end_state; + bool reached_target = false; + uint64_t advances() const{ return advance_times.size(); } + double duration_seconds() const{ + return advance_times.empty() ? 0.0 : advance_times.back(); + } +}; + + +BdspTimelineResult simulate_timeline( + Pokemon::Xorshift128 rng, + const BdspTimelineContext& context, + uint64_t max_advances +); + +bool time_of_advance(const BdspTimelineResult& result, uint64_t advance, double& seconds); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.cpp new file mode 100644 index 0000000000..e1eb1da88e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.cpp @@ -0,0 +1,255 @@ +/* BDSP Starter Navigation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" +#include "PokemonBDSP_StarterNavigation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +const Milliseconds AFTER_BRIEFCASE = 4000ms; +const Milliseconds PRE_BRIEFCASE_DURATION = 18200ms; +const Milliseconds PROMPT_ANIMATION = 1000ms; +const Milliseconds SCHEDULING_SLACK = 100ms; + + +uint8_t starter_cursor_steps(BdspStarter starter){ + switch (starter){ + case BdspStarter::Turtwig: return 0; + case BdspStarter::Chimchar: return 1; + case BdspStarter::Piplup: return 2; + } + return 0; +} + +const char* starter_slug(BdspStarter starter){ + switch (starter){ + case BdspStarter::Turtwig: return "turtwig"; + case BdspStarter::Chimchar: return "chimchar"; + case BdspStarter::Piplup: return "piplup"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown starter."); +} + +// behind the house to the north +void navigate_bedroom_to_skip_spot(Logger& logger, ProControllerContext& context){ + logger.log("Leaving the bedroom for the spot where advances pass quickly."); + pbf_mash_button(context, BUTTON_B, 1000ms); // exit dialogue with the Switch + pbf_wait(context, 1000ms); + pbf_press_dpad(context, DPAD_RIGHT, 1200ms, 1500ms); + pbf_press_dpad(context, DPAD_DOWN, 2300ms, 500ms); + pbf_press_dpad(context, DPAD_LEFT, 1100ms, 1000ms); + pbf_press_dpad(context, DPAD_DOWN, 750ms, 4000ms); + pbf_press_dpad(context, DPAD_LEFT, 850ms, 1000ms); + pbf_press_dpad(context, DPAD_UP, 4100ms, 1000ms); + pbf_press_dpad(context, DPAD_RIGHT, 950ms, 500ms); + context.wait_for_all_requests(); +} + + +void navigate_skip_spot_to_lakefront(Logger& logger, ProControllerContext& context){ + logger.log("Walking from the skip spot to the lakefront."); + pbf_press_dpad(context, DPAD_LEFT, 1350ms, 500ms); + pbf_press_dpad(context, DPAD_UP, 5000ms, 0ms); + + // Clearing the dialogue with Barry + pbf_mash_button(context, BUTTON_B, 5000ms); + pbf_wait(context, 1000ms); + + pbf_press_dpad(context, DPAD_UP, 600ms, 1000ms); + pbf_press_dpad(context, DPAD_LEFT, 8500ms, 1000ms); + pbf_press_dpad(context, DPAD_UP, 1950ms, 1000ms); + context.wait_for_all_requests(); +} + + +void navigate_to_lake_blinks(Logger& logger, ProControllerContext& context){ + logger.log("Walking into the lake, then advancing to the lake blink position."); + pbf_move_left_joystick(context, {0.0, 1.0}, 1600ms, 0ms); + + pbf_wait(context, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 4000ms); + pbf_press_button(context, BUTTON_A, 100ms, 2000ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 2500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 5500ms); + pbf_press_button(context, BUTTON_A, 100ms, 4500ms); + pbf_press_button(context, BUTTON_A, 100ms, 0ms); + context.wait_for_all_requests(); +} + +void navigate_to_pre_briefcase(Logger& logger, ProControllerContext& context){ + logger.log("Advancing to the pre-briefcase position."); + + // total duration equal to PRE_BRIEFCASE_DURATION (above) + pbf_wait(context, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 3500ms); + pbf_press_button(context, BUTTON_A, 100ms, 2000ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 1500ms); + pbf_press_button(context, BUTTON_A, 100ms, 5500ms); + pbf_press_button(context, BUTTON_A, 100ms, 2000ms); + pbf_press_button(context, BUTTON_A, 100ms, 0ms); + context.wait_for_all_requests(); +} + + +const Milliseconds BRIEFCASE_PRESS_TO_STARLY_READY = + 80ms + AFTER_BRIEFCASE + 100ms; +const Milliseconds SELECT_PRESS_TO_CONFIRM_READY = + 80ms + PROMPT_ANIMATION + 100ms + 200ms; + +static Milliseconds starly_press_to_select_ready(BdspStarter starter){ + return 80ms + 5500ms + 100ms + 1200ms + + starter_cursor_steps(starter) * (100ms + 600ms); +} + + +// The whole blind button press sequence +bool issue_starter_sequence( + ProControllerContext& context, + BdspStarter starter, + double starly_seconds, + double select_seconds, + double confirm_seconds, + std::string& failure_reason +){ + auto to_ms = [](double seconds){ + return std::chrono::duration_cast( + std::chrono::duration(seconds) + ); + }; + + // Where the run stands after each fixed stretch, + // measured from the moment the briefcase press is completed + Milliseconds starly_at = to_ms(starly_seconds); + Milliseconds select_at = to_ms(select_seconds); + Milliseconds confirm_at = to_ms(confirm_seconds); + Milliseconds after_dialog = BRIEFCASE_PRESS_TO_STARLY_READY; + Milliseconds after_starly = starly_at + starly_press_to_select_ready(starter); + Milliseconds after_select = select_at + SELECT_PRESS_TO_CONFIRM_READY; + + auto too_tight = [&](const char* what, Milliseconds needed, Milliseconds have){ + failure_reason = std::string(what) + " is scheduled " + + std::to_string((needed - have).count()) + + " ms before the navigation ahead of it can finish"; + return false; + }; + if (starly_at < after_dialog){ + return too_tight("the Starly press", after_dialog, starly_at); + } + if (select_at < after_starly){ + return too_tight("the selecting press", after_starly, select_at); + } + if (confirm_at < after_select){ + return too_tight("the confirming press", after_select, confirm_at); + } + + // The briefcase press + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + + // Past the briefcase dialogue + pbf_wait(context, AFTER_BRIEFCASE); + pbf_press_button(context, BUTTON_A, 100ms, 0ms); + + // The Starly press + pbf_wait(context, starly_at - after_dialog); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + + // Open the briefcase and hover over the chosen ball + pbf_wait(context, 5500ms); + pbf_press_button(context, BUTTON_A, 100ms, 0ms); + pbf_wait(context, 1200ms); + for (uint8_t c = 0; c < starter_cursor_steps(starter); c++){ + pbf_press_dpad(context, DPAD_RIGHT, 100ms, 600ms); + } + + // The selecting press, then the cursor onto "Yes" + pbf_wait(context, select_at - after_starly); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + pbf_wait(context, PROMPT_ANIMATION); + pbf_press_dpad(context, DPAD_UP, 100ms, 200ms); + + // The confirming press + pbf_wait(context, confirm_at - after_select); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + + // battle start + pbf_wait(context, 3000ms); + pbf_press_button(context, BUTTON_A, 100ms, 0ms); + + context.wait_for_all_requests(); + return true; +} + + +// Where the blind run leaves off. Feedback-driven, since nothing past the +// confirming press is timed against the RNG. +bool clear_starter_battle(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + env.log("Mashing A through the battle."); + EndBattleWatcher battle_over; + int ret = run_until( + env.console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_A, 180s); + }, + {{battle_over}} + ); + if (ret < 0){ + env.log("The battle never ended.", COLOR_RED); + return false; + } + env.log("Battle over."); + return true; +} + + +double seconds_to_move_to_starter(BdspStarter starter){ + return std::chrono::duration_cast>( + starly_press_to_select_ready(starter) - 80ms + ).count(); +} + +// kept as short as the prompt animation allows to avoid extra blinks from the starter +double seconds_from_select_to_confirm(){ + return std::chrono::duration_cast>( + SELECT_PRESS_TO_CONFIRM_READY + SCHEDULING_SLACK + ).count(); +} + + +double seconds_to_pre_briefcase(){ + return std::chrono::duration_cast>( + PRE_BRIEFCASE_DURATION + ).count(); +} + + +double seconds_from_briefcase_to_starly_ready(){ + return std::chrono::duration_cast>( + BRIEFCASE_PRESS_TO_STARLY_READY + ).count(); +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.h new file mode 100644 index 0000000000..dacda12b65 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.h @@ -0,0 +1,102 @@ +/* BDSP Starter Navigation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_StarterNavigation_H +#define PokemonAutomation_PokemonBDSP_StarterNavigation_H + +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" + +namespace PokemonAutomation{ + class Logger; +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +enum class BdspStarter{ + Turtwig, + Chimchar, + Piplup, +}; + + +uint8_t starter_cursor_steps(BdspStarter starter); +const char* starter_slug(BdspStarter starter); + + +// Measured over 14 attempts spanning 40k to 734k advances, +/-0.01% +const double BDSP_SKIP_ADVANCES_PER_MINUTE = 19653; + +// Measured over the same attempts, +/-29 +const double BDSP_SKIP_NAVIGATION_ADVANCES = 6902; + +// How far short of the target to stop quickly skipping advances +const double BDSP_SKIP_BUFFER_BASE = 750; +const double BDSP_SKIP_BUFFER_PER_ADVANCE = 0.0002; +inline uint64_t bdsp_skip_buffer(double advances_skipped){ + return (uint64_t)(BDSP_SKIP_BUFFER_BASE + BDSP_SKIP_BUFFER_PER_ADVANCE * advances_skipped); +} + + +const double BDSP_SKIP_RATE_SIGMA = 0.0001; +const double BDSP_SKIP_SCATTER_SIGMA = 67; +inline double bdsp_skip_landing_sigma(double advances_skipped){ + double from_rate = BDSP_SKIP_RATE_SIGMA * advances_skipped; + return std::sqrt(from_rate * from_rate + BDSP_SKIP_SCATTER_SIGMA * BDSP_SKIP_SCATTER_SIGMA); +} + + +inline uint64_t bdsp_lake_advances_needed(double advances_skipped){ + return bdsp_skip_buffer(advances_skipped) + + (uint64_t)(2 * bdsp_skip_landing_sigma(advances_skipped)); +} + + +void navigate_bedroom_to_skip_spot(Logger& logger, ProControllerContext& context); + +void navigate_skip_spot_to_lakefront(Logger& logger, ProControllerContext& context); + + +void navigate_to_lake_blinks(Logger& logger, ProControllerContext& context); + +void navigate_to_pre_briefcase(Logger& logger, ProControllerContext& context); + + +bool issue_starter_sequence( + ProControllerContext& context, + BdspStarter starter, + double starly_seconds, + double select_seconds, + double confirm_seconds, + std::string& failure_reason +); + + +// Mash A until the battle with the wild Starly is over. +bool clear_starter_battle(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +double seconds_to_move_to_starter(BdspStarter starter); + + +double seconds_from_select_to_confirm(); + + +double seconds_from_briefcase_to_starly_ready(); + + +double seconds_to_pre_briefcase(); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.cpp new file mode 100644 index 0000000000..1d19e14261 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.cpp @@ -0,0 +1,207 @@ +/* BDSP Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/ProgramStats/StatsTracking.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/Options/Pokemon_StatsHuntFilter.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonBDSP/Inference/Rng/PokemonBDSP_SummaryReader.h" +#include "PokemonBDSP_StarterRng.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +const size_t CALIBRATION_MIN_SAMPLES = 3; +const double CALIBRATION_THRESHOLD = 1.0; + + +StarterRng_Descriptor::StarterRng_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonBDSP:StarterRng", + STRING_POKEMON + " BDSP", "Starter RNG", + "", + "Starter RNG manipulation using blinks.", + PanelQualityStanding::OFFICIAL_BETA, + ProgramControllerClass::StandardController_RequiresPrecision, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS + ) +{} + +std::unique_ptr StarterRng_Descriptor::make_stats() const{ + return std::unique_ptr(new Stats()); +} + + +StarterRng::StarterRng() + : m_aim(CALIBRATION_MIN_SAMPLES, CALIBRATION_THRESHOLD) + , LANGUAGE( + "Game Language:", + summary_nature_languages(), + LockMode::LOCK_WHILE_RUNNING, true + ) + , START_POINT( + "Start point:
" + "Where you have saved your game. Starting from the bedroom allows the possibility to skip advances quickly.", + { + {BdspStartPoint::Lakefront, "lakefront", "Verity Lakefront"}, + {BdspStartPoint::Bedroom, "bedroom", "Bedroom"}, + }, + LockMode::LOCK_WHILE_RUNNING, + BdspStartPoint::Lakefront + ) + , STARTER( + "Starter:
", + { + {BdspStarter::Turtwig, "turtwig", "Turtwig"}, + {BdspStarter::Chimchar, "chimchar", "Chimchar"}, + {BdspStarter::Piplup, "piplup", "Piplup"}, + }, + LockMode::LOCK_WHILE_RUNNING, + BdspStarter::Turtwig + ) + , FILTERS( + "Stop Conditions:
" + "If the " + STRING_POKEMON + " matches any one of these filters, the program will " + "stop.
", + StatsHuntMiscFeatureFlags{ + /*action*/ false, /*shiny*/ true, /*gender*/ true, /*nature*/ true + } + ) + , COLLECTION_DISPLAY(true) + , MAX_RESETS( + "Maximum resets:
" + "Set this to zero to keep going until a shiny is caught.", + LockMode::LOCK_WHILE_RUNNING, + 0, 0, 1000 + ) + , MAX_TARGET_WAIT_MINUTES( + "Wait at most (minutes) for a target:
" + "Time limit for waiting at Verity Lake. Rarer targets will require longer " + "wait times.", + LockMode::LOCK_WHILE_RUNNING, + 30, 5, 1440 + ) + , MAX_SKIP_MINUTES( + "Skip at most (minutes):
" + "Time limit for skipping advances in Twinleaf Town.", + LockMode::LOCK_WHILE_RUNNING, + 30, 5, 1440 + ) + , AUTO_CALIBRATE( + "Correct the timing automatically:
" + "Shift the target advances when several attempts miss.", + LockMode::LOCK_WHILE_RUNNING, + true + ) + , USE_SOUND_DETECTION( + "Use sound detection:", + LockMode::LOCK_WHILE_RUNNING, + true + ) + , TAKE_VIDEO( + "Take Video:
" + "Record a video when the shiny is found.", + LockMode::LOCK_WHILE_RUNNING, + true + ) + , GO_HOME_WHEN_DONE(false) + , NOTIFICATION_SHINY( + "Shiny Starter", + true, true, ImageAttachmentMode::JPG, + {"Notifs", "Showcase"} + ) + , NOTIFICATIONS({ + &NOTIFICATION_SHINY, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + set_default_shiny_row(FILTERS); + + PA_ADD_OPTION(LANGUAGE); + PA_ADD_OPTION(PLAYER_MODEL); + PA_ADD_OPTION(START_POINT); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(FILTERS); + PA_ADD_OPTION(COLLECTION_DISPLAY); + PA_ADD_OPTION(STATE_DISPLAY); + PA_ADD_OPTION(TARGET_DISPLAY); + PA_ADD_OPTION(MAX_RESETS); + PA_ADD_OPTION(MAX_TARGET_WAIT_MINUTES); + PA_ADD_OPTION(MAX_SKIP_MINUTES); + PA_ADD_OPTION(AUTO_CALIBRATE); + PA_ADD_OPTION(USE_SOUND_DETECTION); + PA_ADD_OPTION(TAKE_VIDEO); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); + + START_POINT.add_listener(*this); + StarterRng::on_config_value_changed(this); +} + + +void StarterRng::on_config_value_changed(void*){ + MAX_SKIP_MINUTES.set_visibility( + START_POINT == BdspStartPoint::Bedroom + ? ConfigOptionState::ENABLED + : ConfigOptionState::HIDDEN + ); +} + + +void StarterRng::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + StarterRng_Descriptor::Stats& stats = env.current_stats(); + + require_player(env.console, context, BUTTON_B); + + m_aim.reset(); + + BdspRngFilterSnapshot filters = FILTERS.make_snapshot(); + StarterRngSettings settings{ + .language = LANGUAGE, + .player_model = PLAYER_MODEL.model_number(), + .start_point = START_POINT, + .starter = STARTER, + .wanted = [&filters](const BdspPokemonResult& result){ + return starter_wanted(filters, result); + }, + .filters = &filters, + .max_resets = MAX_RESETS, + .max_target_wait_minutes = MAX_TARGET_WAIT_MINUTES, + .max_skip_minutes = MAX_SKIP_MINUTES, + .auto_calibrate = AUTO_CALIBRATE, + .use_sound_detection = USE_SOUND_DETECTION, + .take_video = TAKE_VIDEO, + .reset_first = false, + .finish_battle_on_shiny = false, + .close_summary_after_read = false, + }; + StarterRngAttemptIO io{ + .collection_display = &COLLECTION_DISPLAY, + .state_display = &STATE_DISPLAY, + .target_display = &TARGET_DISPLAY, + .notification_shiny = &NOTIFICATION_SHINY, + .notification_error = &NOTIFICATION_ERROR_RECOVERABLE, + .stats = &stats, + }; + + run_starter_rng_attempts(env, context, settings, io, m_aim); + + GO_HOME_WHEN_DONE.run_end_of_program(context); +} + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.h new file mode 100644 index 0000000000..ba081486b2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.h @@ -0,0 +1,82 @@ +/* BDSP Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_StarterRng_H +#define PokemonAutomation_PokemonBDSP_StarterRng_H + +#include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/LanguageOCROption.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Options/PokemonBDSP_RngFilter.h" +#include "PokemonBDSP_RngAim.h" +#include "PokemonBDSP_RngDisplays.h" +#include "PokemonBDSP_StarterNavigation.h" +#include "PokemonBDSP_StarterRngAttempts.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +class StarterRng_Descriptor : public SingleSwitchProgramDescriptor{ +public: + StarterRng_Descriptor(); + + using Stats = StarterRngStats; + virtual std::unique_ptr make_stats() const override; +}; + + +class StarterRng : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +public: + using Descriptor = StarterRng_Descriptor; + StarterRng(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + virtual void on_config_value_changed(void* object) override; + +private: + RngAim m_aim; + +private: + OCR::LanguageOCROption LANGUAGE; + PlayerModelOption PLAYER_MODEL; + EnumDropdownOption START_POINT; + EnumDropdownOption STARTER; + + BdspRngFilterTable FILTERS; + + BlinkCollectionDisplay COLLECTION_DISPLAY; + RngStateDisplay STATE_DISPLAY; + RngTargetDisplay TARGET_DISPLAY; + + SimpleIntegerOption MAX_RESETS; + SimpleIntegerOption MAX_TARGET_WAIT_MINUTES; + SimpleIntegerOption MAX_SKIP_MINUTES; + + BooleanCheckBoxOption AUTO_CALIBRATE; + + BooleanCheckBoxOption USE_SOUND_DETECTION; + BooleanCheckBoxOption TAKE_VIDEO; + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + + EventNotificationOption NOTIFICATION_SHINY; + EventNotificationsOption NOTIFICATIONS; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.cpp new file mode 100644 index 0000000000..472708f58f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.cpp @@ -0,0 +1,767 @@ +/* BDSP Starter RNG Attempts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/Options/Pokemon_StatsHuntFilter.h" +#include "Pokemon/Pokemon_NatureChecker.h" +#include "Pokemon/Pokemon_Notification.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/Rng/PokemonBDSP_BlinkScenes.h" +#include "PokemonBDSP/Inference/ShinyDetection/PokemonBDSP_ShinyEncounterDetector.h" +#include "PokemonBDSP/Programs/PokemonBDSP_GameEntry.h" +#include "PokemonBDSP_BlinkRecovery.h" +#include "PokemonBDSP_RngCalibration.h" +#include "PokemonBDSP_RngExecution.h" +#include "PokemonBDSP_RngTargets.h" +#include "PokemonBDSP_RngTimeline.h" +#include "PokemonBDSP_StarterRngAttempts.h" +#include "PokemonBDSP_SummaryNavigation.h" +#include "PokemonBDSP_TargetSelection.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +namespace{ + +const size_t LAKE_BLINKS_PRESS_RETRIES = 10; + +const double GAP_GUARD_SECONDS = 0.30; + +const uint64_t HIT_SEARCH_RADIUS = 20; +const size_t MAX_SCHEDULES = 8; + +const uint64_t SKIP_ARRIVAL_OVERSHOOT_ALLOWANCE = 200000; + + +std::function resolve_wanted_predicate( + const StarterRngSettings& settings +){ + if (settings.wanted){ + return settings.wanted; + } + if (settings.filters != nullptr){ + const BdspRngFilterSnapshot* filters = settings.filters; + return [filters](const BdspPokemonResult& result){ + return starter_wanted(*filters, result); + }; + } + return [](const BdspPokemonResult& result){ + return result.shiny != BdspShiny::None; + }; +} + + +BdspSkipResult skip_advances_from_bedroom( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const StarterRngSettings& settings, + const std::function& wanted_fn, + BlinkCollectionDisplay& collection_display, + RngStateDisplay& state_display, + RngTargetDisplay& target_display +){ + BdspSkipResult skip; + const BdspRngTargetInfo& target_info = bdsp_rng_target_info(BdspRngTarget::Starter); + + // Expected wait duration at the lake after a long skip + double longest_skip = settings.max_skip_minutes * BDSP_SKIP_ADVANCES_PER_MINUTE; + double lake_minutes = bdsp_lake_advances_needed(longest_skip) + * BDSP_NPC_TICK_SECONDS / (target_info.observation_npcs * 60.0); + if (lake_minutes > settings.max_target_wait_minutes){ + throw UserSetupError(env.logger(), + "A skip of up to " + std::to_string(settings.max_skip_minutes) + + " minutes can leave up to " + + tostr_fixed(lake_minutes, 1) + " minutes of waiting at the lake, but the target " + "wait is capped at " + std::to_string(settings.max_target_wait_minutes) + + ". Raise the target wait to at least " + + std::to_string((uint16_t)std::ceil(lake_minutes)) + + " minutes, or lower the skip limit." + ); + } + + env.log("Starting in the bedroom."); + pbf_press_button(context, BUTTON_A, 100ms, 2000ms); // open dialogue box + context.wait_for_all_requests(); + + std::vector setups = bedroom_eye_templates(settings.player_model); + std::vector eyes = load_eye_templates(setups); + + std::vector> watchers; + std::vector callbacks; + make_blink_watchers(setups, eyes, watchers, callbacks); + + BlinkRecoveryConfig bedroom_config; + bedroom_config.npcs = 1; + + BlinkRecovery recovery = recover_state_from_blinks( + env, context, watchers, callbacks, collection_display, bedroom_config + ); + if (!recovery.success){ + skip.failure_reason = "could not determine the RNG state in the bedroom: " + + recovery.failure_reason; + return skip; + } + skip.state = recovery.state; + env.log("Bedroom RNG state: " + recovery.state.to_string(), COLOR_BLUE); + state_display.set_state(recovery.state, recovery.clock.anchor_advance); + state_display.set_confidence_unique(); + + // take into account remaining navigation time and the desired buffer + uint64_t soonest = recovery.clock.advance_at(current_time()) + + (uint64_t)BDSP_SKIP_NAVIGATION_ADVANCES + bdsp_skip_buffer(0); + uint64_t furthest = soonest + + (uint64_t)(settings.max_skip_minutes * BDSP_SKIP_ADVANCES_PER_MINUTE); + + BdspStaticSearcher searcher(recovery.state, target_info.pokemon, 0); + std::vector hits = searcher.scan( + soonest, furthest, + wanted_fn, + true, &context + ); + if (hits.empty()){ + skip.failure_reason = "no target within " + std::to_string(settings.max_skip_minutes) + + " minutes of skipping"; + return skip; + } + const BdspRngHit& hit = hits[0]; + skip.target_advance = hit.advances; + target_display.set_target(hit.result, hit.advances); + + uint64_t start = recovery.clock.advance_at(current_time()); + + // The buffer depends on how far the skip runs, so size it from the raw distance. + double raw_skip = (double)hit.advances - (double)start - BDSP_SKIP_NAVIGATION_ADVANCES; + uint64_t buffer = bdsp_skip_buffer(raw_skip); + double to_skip = raw_skip - (double)buffer; + if (to_skip < 0){ + to_skip = 0; + } + double wait_seconds = to_skip * 60 / BDSP_SKIP_ADVANCES_PER_MINUTE; + skip.buffer = buffer; + + env.log("Target advance " + std::to_string(hit.advances) + ": " + hit.result.to_string(), + COLOR_BLUE); + env.log("Skipping " + std::to_string((uint64_t)to_skip) + " advances from " + + std::to_string(start) + ", which is " + tostr_fixed(wait_seconds, 1) + + "s on the spot, stopping " + std::to_string(buffer) + + " advances short to avoid overshooting.", COLOR_BLUE); + + navigate_bedroom_to_skip_spot(env.logger(), context); + + WallClock leave_at = current_time() + std::chrono::duration_cast( + std::chrono::duration(wait_seconds) + ); + + while (current_time() + 2s < leave_at){ + WallClock now = current_time(); + WallClock until = std::min(leave_at - 2s, now + 180s); + pbf_wait(context, std::chrono::duration_cast(until - now)); + context.wait_for_all_requests(); + } + WallClock now = current_time(); + if (now < leave_at){ + pbf_wait(context, std::chrono::duration_cast(leave_at - now)); + context.wait_for_all_requests(); + } + + navigate_skip_spot_to_lakefront(env.logger(), context); + skip.success = true; + return skip; +} + + +void report_skip_arrival( + SingleSwitchProgramEnvironment& env, + const BdspSkipResult& skip, + const BlinkRecovery& arrival +){ + // Generous enough to catch an overshoot, and a match is unique anyway. + uint64_t search_max = skip.target_advance + SKIP_ARRIVAL_OVERSHOOT_ALLOWANCE; + uint64_t travelled = 0; + if (!advances_between(skip.state, arrival.state, search_max, travelled)){ + env.log( + "Could not place the lake state against the bedroom one, so the skip " + "cannot be measured this attempt.", + COLOR_ORANGE + ); + return; + } + + uint64_t here = travelled + arrival.clock.advance_at(current_time()); + int64_t short_by = (int64_t)skip.target_advance - (int64_t)here; + env.log( + "Skip landed on advance " + std::to_string(here) + " of " + + std::to_string(skip.target_advance) + ": " + + (short_by >= 0 + ? std::to_string(short_by) + " short, aimed for " + std::to_string(skip.buffer) + : "OVERSHOT by " + std::to_string(-short_by)), + short_by >= 0 ? COLOR_BLUE : COLOR_ORANGE + ); +} + +} // namespace + + +StarterRngStats::StarterRngStats() + : resets(m_stats["Resets"]) + , missed(m_stats["Missed"]) + , hits(m_stats["Hit"]) + , shinies(m_stats["Shiny"]) + , errors(m_stats["Errors"]) +{ + m_display_order.emplace_back("Resets"); + m_display_order.emplace_back(Stat("Missed", HIDDEN_IF_ZERO)); + m_display_order.emplace_back(Stat("Hit", HIDDEN_IF_ZERO)); + m_display_order.emplace_back(Stat("Shiny", HIDDEN_IF_ZERO)); + m_display_order.emplace_back(Stat("Errors", HIDDEN_IF_ZERO)); +} + + +bool starter_wanted( + const BdspRngFilterSnapshot& filters, + const BdspPokemonResult& pokemon +){ + auto exactly = [](uint8_t iv){ + return IvRange{(int8_t)iv, (int8_t)iv}; + }; + IvRanges ivs; + ivs.hp = exactly(pokemon.ivs.hp); + ivs.attack = exactly(pokemon.ivs.attack); + ivs.defense = exactly(pokemon.ivs.defense); + ivs.spatk = exactly(pokemon.ivs.spatk); + ivs.spdef = exactly(pokemon.ivs.spdef); + ivs.speed = exactly(pokemon.ivs.speed); + + StatsHuntGenderFilter gender = StatsHuntGenderFilter::Genderless; + switch (pokemon.gender){ + case BdspGender::Male: + gender = StatsHuntGenderFilter::Male; + break; + case BdspGender::Female: + gender = StatsHuntGenderFilter::Female; + break; + default: + break; + } + + return filters.get_action( + pokemon.shiny != BdspShiny::None, + gender, + bdsp_nature_to_checker_value(pokemon.nature), + pokemon.height, + ivs + ) != StatsHuntAction::Discard; +} + + +bool resolve_starter_rng_step( + const BdspStarterAttemptResult& attempt, + uint64_t resets_used, + uint16_t max_resets, + StarterRngRunResult& result +){ + result.resets_used = resets_used; + if (attempt.target_advance != 0){ + result.target = attempt.target; + result.target_advance = attempt.target_advance; + } + if (!attempt.failure_reason.empty()){ + result.last_failure_reason = attempt.failure_reason; + } + + if (attempt.outcome == BdspAttemptOutcome::Hit){ + result.settled = attempt.settled; + result.outcome = attempt.shiny + ? StarterRngOutcome::SHINY + : StarterRngOutcome::HIT; + return true; + } + + if (max_resets != 0 && resets_used >= max_resets){ + result.outcome = attempt.outcome == BdspAttemptOutcome::Abandoned + ? StarterRngOutcome::ABANDONED + : StarterRngOutcome::OUT_OF_RESETS; + return true; + } + + return false; +} + + +// Leaves the game as the settings ask after a shiny: battle finished and, if +// asked, the starter summary read and the menu closed. False if a step failed. +static bool settle_after_shiny( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, const StarterRngSettings& settings +){ + if (!settings.finish_battle_on_shiny){ + return true; + } + if (!clear_starter_battle(env, context)){ + return false; + } + if (settings.close_summary_after_read){ + if (!open_menu(env, context)){ + return false; + } + pbf_press_button(context, BUTTON_X, 160ms, 1000ms); + context.wait_for_all_requests(); + env.log("Closed the menu."); + } + return true; +} + +BdspStarterAttemptResult run_starter_rng_attempt( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const StarterRngSettings& settings, + const StarterRngAttemptIO& io, + RngAim& aim +){ + BlinkCollectionDisplay fallback_collection(true); + RngStateDisplay fallback_state; + RngTargetDisplay fallback_target; + + BlinkCollectionDisplay& collection_display = io.collection_display != nullptr + ? *io.collection_display + : fallback_collection; + RngStateDisplay& state_display = io.state_display != nullptr + ? *io.state_display + : fallback_state; + RngTargetDisplay& target_display = io.target_display != nullptr + ? *io.target_display + : fallback_target; + + collection_display.reset(); + state_display.reset(); + target_display.reset(); + + auto record_miss = [&](){ + if (io.stats != nullptr){ + io.stats->missed++; + } + if (io.on_miss){ + io.on_miss(); + } + }; + auto record_hit = [&](){ + if (io.stats != nullptr){ + io.stats->hits++; + } + if (io.on_hit){ + io.on_hit(); + } + }; + auto record_shiny = [&](){ + if (io.stats != nullptr){ + io.stats->shinies++; + } + if (io.on_shiny){ + io.on_shiny(); + } + }; + auto record_error = [&](){ + if (io.stats != nullptr){ + io.stats->errors++; + } + if (io.on_error){ + io.on_error(); + } + }; + + auto abandon = [&](const std::string& reason) -> BdspStarterAttemptResult{ + env.log("Abandoning this attempt: " + reason, COLOR_ORANGE); + target_display.set_note(reason); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Abandoned, + .failure_reason = reason, + }; + }; + + std::function wanted_fn = resolve_wanted_predicate(settings); + const BdspRngTargetInfo& target_info = bdsp_rng_target_info(BdspRngTarget::Starter); + + BdspSkipResult skip; + if (settings.start_point == BdspStartPoint::Bedroom){ + skip = skip_advances_from_bedroom( + env, context, settings, wanted_fn, + collection_display, state_display, target_display + ); + if (!skip.success){ + return abandon(skip.failure_reason); + } + } + + navigate_to_lake_blinks(env.logger(), context); + + std::vector setups = lake_eye_templates(settings.player_model); + std::vector eyes = load_eye_templates(setups); + + // The rival's dialogue box being up is what says the pair have stopped walking. + ShortDialogDetector dialog; + BlinkSceneConfig scene; + scene.frame_filter = [&dialog](const ImageViewRGB32& frame){ + return dialog.detect(frame); + }; + scene.press_retries = LAKE_BLINKS_PRESS_RETRIES; + + if (!wait_for_blink_scene(env, context, setups, eyes, scene, "Lake blinks")){ + return abandon("never reached the lake blink position"); + } + + // Now blink watchers can be created safely + std::vector> watchers; + std::vector callbacks; + make_blink_watchers(setups, eyes, watchers, callbacks); + + BlinkRecoveryConfig blink_config; + blink_config.npcs = target_info.observation_npcs; + + BlinkRecovery recovery = recover_state_from_blinks( + env, context, watchers, callbacks, collection_display, blink_config + ); + if (!recovery.success){ + return abandon("could not determine the RNG state: " + recovery.failure_reason); + } + + uint64_t seed0 = 0; + uint64_t seed1 = 0; + xorshift128_state_to_seed_pair(recovery.state, seed0, seed1); + env.log("RNG state: " + recovery.state.to_string(), COLOR_BLUE); + env.log("PokeFinder seeds: " + tostr_hex_padded(16, seed0) + + " " + tostr_hex_padded(16, seed1), COLOR_BLUE); + env.log("Tick fitted at " + tostr_default(recovery.clock.tick_seconds) + "s over " + + std::to_string(recovery.events) + " rolls.", COLOR_BLUE); + env.log("Anchored on advance " + std::to_string(recovery.clock.anchor_advance) + + "; now at advance " + std::to_string(recovery.clock.advance_at(current_time())) + + ".", COLOR_BLUE); + state_display.set_state(recovery.state, recovery.clock.anchor_advance); + state_display.set_confidence_unique(); + + if (skip.success){ + report_skip_arrival(env, skip, recovery); + } + + const BdspTimelineContext& timeline = target_info.timeline; + + NavigationTimings timings; + timings.ready_seconds = seconds_from_briefcase_to_starly_ready(); + timings.move_seconds = seconds_to_move_to_starter(settings.starter) + 0.25; // small buffer + timings.prompt_seconds = seconds_from_select_to_confirm(); + timings.guard_seconds = GAP_GUARD_SECONDS; + + BdspStaticSearcher searcher(recovery.state, target_info.pokemon, 0); + + // A press cannot be sooner than the walk to the pre-briefcase position takes + double lead_seconds = seconds_to_pre_briefcase() + 15; + uint64_t lead_advances = (uint64_t)( + lead_seconds * recovery.clock.npcs / recovery.clock.tick_seconds + ) + 1; + + TargetSearchRequest search; + search.state = recovery.state; + search.timeline = timeline; + search.timings = timings; + search.pokemon = target_info.pokemon; + search.npcs = recovery.clock.npcs; + search.tick_seconds = recovery.clock.tick_seconds; + search.bias = aim.bias(); + search.first_press = recovery.clock.advance_at(current_time()) + lead_advances; + search.window_advances = (uint64_t)( + (double)settings.max_target_wait_minutes * 60 * recovery.clock.npcs / recovery.clock.tick_seconds + ); + search.max_schedules = MAX_SCHEDULES; + search.wanted = wanted_fn; + + TargetSelectionResult selection = select_target(search); + + if (selection.matches_found != 0){ + env.log( + std::to_string(selection.matches_found) + " matching advance(s) in " + + std::to_string(selection.advances_scanned), + COLOR_BLUE + ); + } + if (!selection.success){ + return abandon(selection.failure_reason); + } + + uint64_t press_advance = selection.press_advance; + uint64_t target_advance = selection.target_advance; + const PressSchedule& schedule = selection.schedule; + const BdspPokemonResult& target = selection.target; + + double wait_seconds = std::chrono::duration_cast>( + recovery.clock.time_of_advance(press_advance) - current_time() + ).count(); + env.log("Target advance " + std::to_string(target_advance) + ": " + target.to_string(), + COLOR_BLUE); + env.log("Pressing at advance " + std::to_string(press_advance) + ", in " + + tostr_default(wait_seconds) + "s.", COLOR_BLUE); + env.log("Schedule spans " + std::to_string(schedule.span) + " advances, confirming after " + + std::to_string(schedule.blinks_before_confirm) + " Starly blink(s), " + + tostr_default(schedule.confirm_seconds) + "s in" + + aim.describe_correction() + ".", COLOR_BLUE); + target_display.set_target(target, target_advance); + + hold_and_reanchor( + env, context, watchers, callbacks, recovery, blink_config, + press_advance, lead_seconds, state_display + ); + + navigate_to_pre_briefcase(env.logger(), context); + + // FIRST PRESS, centred in the advance rather than aimed right at the beginning of it + WallClock briefcase_time = recovery.clock.middle_of_advance(press_advance); + + while (current_time() + 3s < briefcase_time){ + state_display.set_advances(recovery.clock.advance_at(current_time())); + pbf_wait(context, 1000ms); + context.wait_for_all_requests(); + } + + state_display.set_advances(press_advance); + + env.log("Timeline starts at advance " + std::to_string(press_advance) + + ". Blind from here: Starly press at " + tostr_default(schedule.starly_seconds) + + "s, selecting at " + tostr_default(schedule.select_seconds) + + "s, confirming at " + tostr_default(schedule.confirm_seconds) + + "s, on advance " + std::to_string(target_advance - timeline.advances_after_accept) + + ".", COLOR_BLUE); + + std::string sequence_failure; + if (!wait_until_moment(context, env.logger(), briefcase_time)){ + return abandon("the briefcase press moment passed before it could be sent"); + } + if (!issue_starter_sequence( + context, settings.starter, + schedule.starly_seconds, schedule.select_seconds, schedule.confirm_seconds, + sequence_failure + )){ + return abandon(sequence_failure); + } + env.log("Timeline finished; the starter should be in hand.", COLOR_BLUE); + + state_display.set_advances(target_advance); + + // Check what was actually obtained. + DoublesShinyDetection wild; + ShinyDetectionResult own; + detect_shiny_battle( + env, env.console, context, + wild, own, + io.notification_error != nullptr ? *io.notification_error : EventNotificationOption_NOOP(), + YOUR_POKEMON, + 30s, + settings.use_sound_detection + ); + bool shiny_known = own.shiny_type != ShinyType::UNKNOWN; + + if (is_likely_shiny(own.shiny_type)){ + record_shiny(); + if (io.notification_shiny != nullptr){ + send_program_notification( + env, *io.notification_shiny, + COLOR_STAR_SHINY, "Shiny Starter", + {{"Advance", std::to_string(target_advance)}, {"Details", target.to_string()}}, + "", own.get_best_screenshot() + ); + } + if (settings.take_video){ + pbf_wait(context, 5000ms); + pbf_press_button(context, BUTTON_CAPTURE, 2000ms, 5000ms); + context.wait_for_all_requests(); + } + const bool settled = settle_after_shiny(env, context, settings); + if (!settled){ + record_error(); + } + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = true, + .target = target, + .target_advance = target_advance, + .settled = settled, + }; + } + + BdspObservedStarter observed; + if (!clear_starter_battle(env, context) + || !read_observed_pokemon( + env, context, settings.language, + starter_base_stats(settings.starter), 5, false, shiny_known, observed + ) + ){ + record_error(); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Unverifiable, + .shiny = false, + .target = target, + .target_advance = target_advance, + .failure_reason = "could not verify the obtained starter", + }; + } + + if (settings.close_summary_after_read){ + if (!close_starter_summary(env, context)){ + record_error(); + } + } + + BdspHitIdentification hit = identify_hit_advance( + recovery.state, target_info.pokemon, target_advance, HIT_SEARCH_RADIUS, observed + ); + if (!hit.success){ + env.log("Could not tell which advance that was: " + hit.failure_reason, COLOR_ORANGE); + if (consistent_with(target, observed)){ + env.log("What was read matches the target, so it counts as a hit.", COLOR_BLUE); + record_hit(); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = false, + .target = target, + .target_advance = target_advance, + }; + } + env.log("What was read is not the target, so it counts as a miss.", COLOR_ORANGE); + record_miss(); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Missed, + .shiny = false, + .target = target, + .target_advance = target_advance, + .failure_reason = hit.failure_reason, + }; + } + if (aim.record_offset(env.logger(), hit.offset, settings.auto_calibrate)){ + target_display.set_correction(aim.bias()); + } + + if (hit.offset == 0){ + env.log("Landed on advance " + std::to_string(target_advance) + ", as aimed.", + COLOR_BLUE); + record_hit(); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = false, + .target = target, + .target_advance = target_advance, + }; + } + + env.log( + "Landed on advance " + std::to_string(hit.advance) + " instead of " + + std::to_string(target_advance) + " — " + + (hit.offset > 0 ? "late by " : "early by ") + + std::to_string(hit.offset > 0 ? hit.offset : -hit.offset) + " advance(s).", + COLOR_ORANGE + ); + record_miss(); + + BdspPokemonResult actual = searcher.generate(hit.advance); + if (wanted_fn(actual)){ + env.log("It passes the filter anyway, so this one will do. Stopping.", COLOR_BLUE); + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = false, + .target = actual, + .target_advance = hit.advance, + }; + } + return BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Missed, + .shiny = false, + .target = target, + .target_advance = target_advance, + }; +} + + +StarterRngRunResult run_starter_rng_attempts( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const StarterRngSettings& settings, + const StarterRngAttemptIO& io, + RngAim& aim +){ + StarterRngRunResult result; + bool reset = settings.reset_first; + uint64_t resets = 0; + BdspStarterAttemptResult last_attempt; + + while (true){ + if (settings.max_resets != 0 && resets >= settings.max_resets){ + env.log("Reached the reset limit.", COLOR_ORANGE); + resolve_starter_rng_step(last_attempt, resets, settings.max_resets, result); + break; + } + resets++; + + bool started = true; + if (reset){ + go_home(env.console, context); + if (!reset_game_from_home( + env, env.console, context, + ConsoleSettings::instance().TOLERATE_SYSTEM_UPDATE_MENU_FAST + )){ + started = false; + } + } + + if (started){ + reset = true; + last_attempt = run_starter_rng_attempt(env, context, settings, io, aim); + }else{ + last_attempt = BdspStarterAttemptResult{ + .outcome = BdspAttemptOutcome::Abandoned, + .failure_reason = "failed to reset the game from the Home screen", + }; + } + + if (io.stats != nullptr){ + io.stats->resets++; + } + if (io.on_reset){ + io.on_reset(); + } + env.update_stats(); + + if (last_attempt.outcome == BdspAttemptOutcome::Hit){ + resolve_starter_rng_step(last_attempt, resets, settings.max_resets, result); + break; + } + if (!last_attempt.failure_reason.empty()){ + result.last_failure_reason = last_attempt.failure_reason; + } + result.resets_used = resets; + if (last_attempt.target_advance != 0){ + result.target = last_attempt.target; + result.target_advance = last_attempt.target_advance; + } + } + + env.update_stats(); + return result; +} + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h new file mode 100644 index 0000000000..1e88d9b277 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h @@ -0,0 +1,216 @@ +/* BDSP Starter RNG Attempts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_StarterRngAttempts_H +#define PokemonAutomation_PokemonBDSP_StarterRngAttempts_H + +#include +#include +#include +#include "CommonFramework/Language.h" +#include "CommonFramework/Notifications/EventNotificationOption.h" +#include "CommonFramework/ProgramStats/StatsTracking.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/Pokemon_BdspRng.h" +#include "Pokemon/Pokemon_Xorshift128.h" +#include "PokemonBDSP/Options/PokemonBDSP_RngFilter.h" +#include "PokemonBDSP_RngAim.h" +#include "PokemonBDSP_RngDisplays.h" +#include "PokemonBDSP_StarterNavigation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +enum class BdspStartPoint{ + Lakefront, + Bedroom, +}; + + +struct BdspSkipResult{ + bool success = false; + Pokemon::Xorshift128State state; + uint64_t target_advance = 0; + uint64_t buffer = 0; + std::string failure_reason; +}; + + +enum class BdspAttemptOutcome{ + Abandoned, + Missed, + Hit, + Unverifiable, +}; + + +struct StarterRngStats : public StatsTracker{ + StarterRngStats(); + + std::atomic& resets; + std::atomic& missed; + std::atomic& hits; + std::atomic& shinies; + std::atomic& errors; +}; + + +// Evaluates whether a generated starter satisfies the filter snapshot. +// Takes a snapshot rather than the table because scanning millions of +// advances cannot afford cloning the table on every query. +bool starter_wanted( + const BdspRngFilterSnapshot& filters, + const Pokemon::BdspPokemonResult& pokemon +); + + +// Settings for a Starter RNG attempt loop. +// +// In-game state traversal after the starter is chosen and the wild Starly +// battle begins at Lake Verity: +// - Shiny hit (`Outcome::SHINY`): +// `detect_shiny_battle(...)` finishes at the start of the Starly battle. +// * When `finish_battle_on_shiny == false` (standalone `StarterRng` default): +// returns immediately while still inside the Starly battle at Lake Verity. +// * When `finish_battle_on_shiny == true` (for AutoStory): +// calls `clear_starter_battle(env, context)`, which mashes A until +// `EndBattleWatcher` detects the black-screen battle exit transition, and +// when `close_summary_after_read == true` also advances through Rowan's +// post-battle scene (`open_menu`) to `VerityLakefront (17, 14..15)` and +// closes the X menu so both `SHINY` and `HIT` end on the `VerityLakefront` +// overworld ready for checkpoint `00.03`. +// - Non-shiny starter (`clear_starter_battle` + `read_observed_pokemon`): +// 1. `clear_starter_battle(env, context)` mashes A until `EndBattleWatcher` +// detects the end of the Starly battle. +// 2. `read_observed_pokemon(...)` calls `open_menu(env, context)`, which +// mashes `B, B, X` until `MenuWatcher` detects the overworld X menu. +// Because the X menu cannot open during the post-battle Lake Verity +// cutscene or the warp out of the lake, `open_menu` advances through +// all of Rowan's and Barry's post-battle dialogue (`after_starly`) and +// the warp back to `VerityLakefront (17, 14..15)`, then opens the X menu +// once the player has overworld control on `VerityLakefront`. +// 3. `open_starter_summary(env, context)` selects `POKEMON` (one entry left +// on the 2-icon pre-Pokedex X menu), opens the starter's Summary screen, +// reads Nature & Gender on the 2nd tab (Trainer Memo), and presses Right +// to read the 6 stats on the 3rd tab (Skills/Stats). +// * When `close_summary_after_read == false` (standalone `StarterRng` default): +// leaves the game on the 3rd tab of the starter's Summary screen. +// * When `close_summary_after_read == true` (for AutoStory): +// calls `close_starter_summary(env, context)` after reading the summary, +// which presses B back to the X menu and X to close it, leaving the +// player standing on the `VerityLakefront` overworld at `(17, 14..15)` +// (with `after_starly` already completed, ready for checkpoint `00.03`). +struct StarterRngSettings{ + Language language = Language::English; + uint8_t player_model = 1; + BdspStartPoint start_point = BdspStartPoint::Lakefront; + BdspStarter starter = BdspStarter::Turtwig; + + // Predicate for target selection and hit verification. If `wanted` is + // empty and `filters` is non-null, `starter_wanted(*filters, ...)` is used. + std::function wanted; + const BdspRngFilterSnapshot* filters = nullptr; + + uint16_t max_resets = 0; + uint16_t max_target_wait_minutes = 30; + uint16_t max_skip_minutes = 30; + + bool auto_calibrate = true; + bool use_sound_detection = true; + bool take_video = true; + + bool reset_first = false; + bool finish_battle_on_shiny = false; + bool close_summary_after_read = false; +}; + + +struct StarterRngAttemptIO{ + BlinkCollectionDisplay* collection_display = nullptr; + RngStateDisplay* state_display = nullptr; + RngTargetDisplay* target_display = nullptr; + EventNotificationOption* notification_shiny = nullptr; + EventNotificationOption* notification_error = nullptr; + StarterRngStats* stats = nullptr; + + std::function on_reset; + std::function on_miss; + std::function on_hit; + std::function on_shiny; + std::function on_error; +}; + + +enum class StarterRngOutcome{ + HIT, + SHINY, + OUT_OF_RESETS, + ABANDONED, +}; + + +struct StarterRngRunResult{ + using Outcome = StarterRngOutcome; + using enum StarterRngOutcome; + + Outcome outcome = Outcome::ABANDONED; + Pokemon::BdspPokemonResult target{}; + uint64_t target_advance = 0; + uint64_t resets_used = 0; + std::string last_failure_reason; + // False when a shiny was caught but the game was not left where + // `finish_battle_on_shiny` and `close_summary_after_read` promise: the + // caller must not go on from it. + bool settled = true; +}; + + +struct BdspStarterAttemptResult{ + BdspAttemptOutcome outcome = BdspAttemptOutcome::Abandoned; + bool shiny = false; + Pokemon::BdspPokemonResult target{}; + uint64_t target_advance = 0; + std::string failure_reason; + // See StarterRngRunResult::settled. + bool settled = true; +}; + + +// Pure helper that maps the latest attempt result and reset count to a +// finished `StarterRngRunResult` when the loop should terminate, or returns +// `false` when the loop should continue to the next reset. +bool resolve_starter_rng_step( + const BdspStarterAttemptResult& attempt, + uint64_t resets_used, + uint16_t max_resets, + StarterRngRunResult& result +); + + +BdspStarterAttemptResult run_starter_rng_attempt( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const StarterRngSettings& settings, + const StarterRngAttemptIO& io, + RngAim& aim +); + + +StarterRngRunResult run_starter_rng_attempts( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const StarterRngSettings& settings, + const StarterRngAttemptIO& io, + RngAim& aim +); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.cpp new file mode 100644 index 0000000000..c561e57bc1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.cpp @@ -0,0 +1,362 @@ +/* BDSP Starter RNG Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonBDSP_RngAim.h" +#include "PokemonBDSP_RngCalibration.h" +#include "PokemonBDSP_StarterNavigation.h" +#include "PokemonBDSP_StarterRngAttempts.h" +#include "PokemonBDSP_StarterRng_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +namespace{ + +struct StarterTableRow{ + BdspStarter starter; + std::string slug; + uint8_t cursor_steps; + BaseStats base_stats; + double move_seconds; +}; + +const std::vector STARTER_TABLE_ROWS = { + { + .starter = BdspStarter::Turtwig, + .slug = "turtwig", + .cursor_steps = 0, + .base_stats = {55, 68, 64, 45, 55, 31}, + .move_seconds = 6.8, + }, + { + .starter = BdspStarter::Chimchar, + .slug = "chimchar", + .cursor_steps = 1, + .base_stats = {44, 58, 44, 58, 44, 61}, + .move_seconds = 7.5, + }, + { + .starter = BdspStarter::Piplup, + .slug = "piplup", + .cursor_steps = 2, + .base_stats = {53, 51, 53, 61, 56, 40}, + .move_seconds = 8.2, + }, +}; + + +UnitTestResult test_starter_tables(Logger&){ + for (const StarterTableRow& row : STARTER_TABLE_ROWS){ + const std::string got_slug = starter_slug(row.starter); + if (got_slug != row.slug){ + return std::format("starter_slug mismatch: got {}, expected {}", got_slug, row.slug); + } + const uint8_t got_steps = starter_cursor_steps(row.starter); + if (got_steps != row.cursor_steps){ + return std::format( + "starter_cursor_steps({}) = {}, expected {}", + row.slug, got_steps, row.cursor_steps + ); + } + const BaseStats& stats = starter_base_stats(row.starter); + if (stats.hp != row.base_stats.hp + || stats.attack != row.base_stats.attack + || stats.defense != row.base_stats.defense + || stats.spatk != row.base_stats.spatk + || stats.spdef != row.base_stats.spdef + || stats.speed != row.base_stats.speed + ){ + return std::format( + "starter_base_stats({}) = {}/{}/{}/{}/{}/{}, expected {}/{}/{}/{}/{}/{}", + row.slug, + stats.hp, stats.attack, stats.defense, stats.spatk, stats.spdef, stats.speed, + row.base_stats.hp, row.base_stats.attack, row.base_stats.defense, + row.base_stats.spatk, row.base_stats.spdef, row.base_stats.speed + ); + } + const double move_s = seconds_to_move_to_starter(row.starter); + if (std::abs(move_s - row.move_seconds) > 1e-9){ + return std::format( + "seconds_to_move_to_starter({}) = {}, expected {}", + row.slug, move_s, row.move_seconds + ); + } + } + return true; +} + + +UnitTestResult test_skip_buffer_and_landing_math(Logger&){ + if (bdsp_skip_buffer(0.0) != 750){ + return std::format("bdsp_skip_buffer(0) = {}, expected 750", bdsp_skip_buffer(0.0)); + } + if (bdsp_skip_buffer(100000.0) != 770){ + return std::format("bdsp_skip_buffer(100000) = {}, expected 770", bdsp_skip_buffer(100000.0)); + } + + const double sigma0 = bdsp_skip_landing_sigma(0.0); + if (std::abs(sigma0 - 67.0) > 1e-9){ + return std::format("bdsp_skip_landing_sigma(0) = {}, expected 67.0", sigma0); + } + + const uint64_t lake0 = bdsp_lake_advances_needed(0.0); + if (lake0 != 750 + 2 * 67){ + return std::format("bdsp_lake_advances_needed(0) = {}, expected {}", lake0, 750 + 2 * 67); + } + + // At 1,000,000 advances skipped: buffer = 750 + 200 = 950, + // sigma = sqrt(100^2 + 67^2) = sqrt(14489) ~ 120.37, 2*sigma = 240, total = 1190. + const uint64_t lake1m = bdsp_lake_advances_needed(1000000.0); + if (lake1m != 1190){ + return std::format("bdsp_lake_advances_needed(1e6) = {}, expected 1190", lake1m); + } + return true; +} + + +UnitTestResult test_aim_and_calibration(Logger& logger){ + if (aimed_advance(1000, 200, 0) != 1200){ + return "aimed_advance(1000, 200, 0) != 1200"; + } + if (aimed_advance(1000, 200, 3) != 1203){ + return "aimed_advance(1000, 200, 3) != 1203"; + } + if (press_for_advance(1203, 200, 3) != 1000){ + return "press_for_advance(1203, 200, 3) != 1000"; + } + + RngAim aim(3, 1.0); + if (aim.bias() != 0){ + return "initial bias != 0"; + } + if (aim.record_offset(logger, 2, true) || aim.record_offset(logger, 2, true)){ + return "should not adjust before 3 samples"; + } + if (!aim.record_offset(logger, 2, true)){ + return "expected adjustment on 3rd sample of +2"; + } + if (aim.bias() != 2){ + return std::format("bias after three +2 offsets = {}, expected 2", aim.bias()); + } + return true; +} + + +// A shiny whose post-battle scene did not finish still ends the loop (the +// shiny must not be reset away), and says so. +UnitTestResult test_shiny_unsettled(Logger&){ + BdspStarterAttemptResult shiny{ + .outcome = BdspAttemptOutcome::Hit, .shiny = true, .target_advance = 9, .settled = false + }; + StarterRngRunResult result; + if (!resolve_starter_rng_step(shiny, 1, 3, result) || result.outcome != StarterRngOutcome::SHINY){ + return "expected a shiny to end the loop"; + } + if (result.settled){ + return "expected the unsettled shiny to say that the game is not settled"; + } + shiny.settled = true; + StarterRngRunResult settled; + if (!resolve_starter_rng_step(shiny, 1, 3, settled) || !settled.settled){ + return "expected a settled shiny to be settled"; + } + return true; +} + +UnitTestResult test_attempt_loop_transitions(Logger&){ + BdspPokemonResult sample_target{}; + sample_target.ec = 0x12345678; + sample_target.pid = 0x87654321; + sample_target.nature = 3; + + // 1. Miss when below max_resets continues the loop. + { + StarterRngRunResult result; + BdspStarterAttemptResult miss{ + .outcome = BdspAttemptOutcome::Missed, + .shiny = false, + .target = sample_target, + .target_advance = 1500, + }; + if (resolve_starter_rng_step(miss, 1, 3, result)){ + return "expected loop to continue on miss 1 of 3"; + } + if (result.resets_used != 1 || result.target_advance != 1500){ + return "expected resets_used = 1 and target_advance = 1500 after miss"; + } + } + + // 2. Non-shiny hit stops with Outcome::HIT. + { + StarterRngRunResult result; + BdspStarterAttemptResult hit{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = false, + .target = sample_target, + .target_advance = 1505, + }; + if (!resolve_starter_rng_step(hit, 2, 3, result)){ + return "expected loop to terminate on non-shiny hit"; + } + if (result.outcome != StarterRngOutcome::HIT + || result.resets_used != 2 + || result.target_advance != 1505 + || result.target.pid != sample_target.pid + ){ + return "unexpected result fields on non-shiny hit"; + } + } + + // 3. Shiny hit stops with Outcome::SHINY. + { + StarterRngRunResult result; + BdspStarterAttemptResult shiny_hit{ + .outcome = BdspAttemptOutcome::Hit, + .shiny = true, + .target = sample_target, + .target_advance = 2048, + }; + if (!resolve_starter_rng_step(shiny_hit, 1, 0, result)){ + return "expected loop to terminate on shiny hit"; + } + if (result.outcome != StarterRngOutcome::SHINY + || result.resets_used != 1 + || result.target_advance != 2048 + ){ + return "unexpected result fields on shiny hit"; + } + } + + // 4. Miss reaching max_resets stops with Outcome::OUT_OF_RESETS. + { + StarterRngRunResult result; + BdspStarterAttemptResult miss{ + .outcome = BdspAttemptOutcome::Missed, + .shiny = false, + .target = sample_target, + .target_advance = 1600, + }; + if (!resolve_starter_rng_step(miss, 3, 3, result)){ + return "expected loop to terminate when resets_used == max_resets"; + } + if (result.outcome != StarterRngOutcome::OUT_OF_RESETS || result.resets_used != 3){ + return "expected OUT_OF_RESETS on miss at reset limit"; + } + } + + // 5. Abandoned attempt reaching max_resets stops with Outcome::ABANDONED and preserves reason. + { + StarterRngRunResult result; + BdspStarterAttemptResult abandoned{ + .outcome = BdspAttemptOutcome::Abandoned, + .shiny = false, + .failure_reason = "no matching target within the allowed wait time", + }; + if (!resolve_starter_rng_step(abandoned, 1, 1, result)){ + return "expected loop to terminate on abandoned single attempt"; + } + if (result.outcome != StarterRngOutcome::ABANDONED + || result.resets_used != 1 + || result.last_failure_reason != "no matching target within the allowed wait time" + ){ + return "expected ABANDONED with failure_reason preserved"; + } + } + + return true; +} + + +// The default filter row: a table that has it stops for a shiny of any +// nature and gender, and no other starter; an empty table never stops, which +// is why a caller must seed it. +UnitTestResult test_default_shiny_row(Logger&){ + BdspRngFilterTable empty("Filters", Pokemon::StatsHuntMiscFeatureFlags{false, true, true, true}); + BdspPokemonResult shiny{}; + shiny.shiny = BdspShiny::Star; + shiny.nature = 7; + BdspPokemonResult plain{}; + plain.nature = 7; + if (starter_wanted(empty.make_snapshot(), shiny)){ + return "an empty filter table must not match a shiny"; + } + + BdspRngFilterTable table("Filters", Pokemon::StatsHuntMiscFeatureFlags{false, true, true, true}); + set_default_shiny_row(table); + if (table.current_rows() != 1){ + return std::format("expected one default row, got {}", table.current_rows()); + } + const BdspRngFilterSnapshot snapshot = table.make_snapshot(); + if (!starter_wanted(snapshot, shiny)){ + return "the default row must match a shiny of any nature"; + } + shiny.nature = 20; + if (!starter_wanted(snapshot, shiny)){ + return "the default row must match a shiny of another nature"; + } + if (starter_wanted(snapshot, plain)){ + return "the default row must not match a non-shiny"; + } + return true; +} + + +struct StarterRngTestCase{ + std::string name; + std::function body; +}; + +class Test_StarterRng : public UnitTest{ +public: + explicit Test_StarterRng(StarterRngTestCase test_case) + : UnitTest("PokemonBDSP::StarterRng - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + StarterRngTestCase m_case; +}; + +const StarterRngTestCase STARTER_RNG_CASES[] = { + {"per-starter tables and timings", test_starter_tables}, + {"bedroom skip buffer and landing math", test_skip_buffer_and_landing_math}, + {"aim offset and calibration bias", test_aim_and_calibration}, + {"attempt loop outcome resolution", test_attempt_loop_transitions}, + {"default filter row is shiny of any nature", test_default_shiny_row}, + {"a shiny that did not settle ends the loop unsettled", test_shiny_unsettled}, +}; + +} // namespace + + +void add_tests_StarterRng(UnitTestDatabase& database){ + for (const StarterRngTestCase& test_case : STARTER_RNG_CASES){ + database.add(test_case); + } +} + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h new file mode 100644 index 0000000000..be4c4fa2dd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h @@ -0,0 +1,23 @@ +/* BDSP Starter RNG Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_StarterRng_Tests_H +#define PokemonAutomation_PokemonBDSP_StarterRng_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +void add_tests_StarterRng(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.cpp new file mode 100644 index 0000000000..3d9737b4ee --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.cpp @@ -0,0 +1,185 @@ +/* BDSP Summary Navigation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/Pokemon_NatureChecker.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Inference/Rng/PokemonBDSP_SummaryReader.h" +#include "PokemonBDSP_SummaryNavigation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +const size_t MENU_ATTEMPTS = 3; + + +bool open_menu(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + env.log("Mashing B through dialogue, trying the menu as we go."); + MenuWatcher menu(COLOR_RED, true); + int ret = run_until( + env.console, context, + [](ProControllerContext& context){ + for (size_t c = 0; c < 40; c++){ + pbf_press_button(context, BUTTON_B, 100ms, 700ms); + pbf_press_button(context, BUTTON_B, 100ms, 700ms); + pbf_press_button(context, BUTTON_X, 100ms, 1200ms); + } + }, + {{menu}} + ); + if (ret < 0){ + env.log("Never got back to the overworld menu.", COLOR_RED); + return false; + } + env.log("Menu is open."); + return true; +} + + +bool open_starter_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + for (size_t attempt = 0; attempt < MENU_ATTEMPTS; attempt++){ + pbf_wait(context, 1200ms); + context.wait_for_all_requests(); + + env.log("Moving one entry left, then opening it."); + pbf_press_dpad(context, DPAD_LEFT, 160ms, 840ms); + pbf_press_button(context, BUTTON_A, 160ms, 1500ms); + + pbf_press_button(context, BUTTON_A, 100ms, 1000ms); + context.wait_for_all_requests(); + + SelectionArrowFinder arrow(env.console, {0.360, 0.110, 0.200, 0.290}, COLOR_RED); + if (wait_until(env.console, context, 10s, {{arrow}}) >= 0){ + break; + } + if (attempt + 1 >= MENU_ATTEMPTS){ + env.log("Never reached the party from the menu.", COLOR_RED); + return false; + } + env.log("That was not the party. Backing out and trying again.", COLOR_ORANGE); + bool back_at_menu = false; + for (size_t c = 0; c < 5 && !back_at_menu; c++){ + pbf_press_button(context, BUTTON_B, 160ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, true); + back_at_menu = wait_until(env.console, context, 2s, {{menu}}) >= 0; + } + if (!back_at_menu){ + env.log("Backing out never returned to the menu.", COLOR_RED); + return false; + } + } + + env.log("Opening the summary."); + pbf_press_button(context, BUTTON_A, 100ms, 2000ms); + context.wait_for_all_requests(); + + env.log("Moving to the Trainer Memo page."); + pbf_press_dpad(context, DPAD_RIGHT, 100ms, 1500ms); + context.wait_for_all_requests(); + return true; +} + + +bool navigate_to_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + return open_menu(env, context) + && open_starter_summary(env, context); +} + + +bool close_starter_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + env.log("Backing out of the summary screen to the overworld."); + bool back_at_menu = false; + for (size_t c = 0; c < 6 && !back_at_menu; c++){ + pbf_press_button(context, BUTTON_B, 160ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, true); + back_at_menu = wait_until(env.console, context, 2s, {{menu}}) >= 0; + } + if (!back_at_menu){ + env.log("Never returned to the X menu from the summary.", COLOR_ORANGE); + return false; + } + pbf_press_button(context, BUTTON_X, 160ms, 1000ms); + context.wait_for_all_requests(); + env.log("Closed the menu."); + return true; +} + + +bool read_observed_pokemon( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + Language language, + const Pokemon::BaseStats& base_stats, + uint8_t level, + bool shiny, + bool shiny_known, + BdspObservedStarter& observed +){ + if (!navigate_to_summary(env, context)){ + env.log("Could not reach the summary, so this attempt says nothing about timing.", + COLOR_ORANGE); + return false; + } + + SummaryReader reader; + + // 2nd page: nature and gender + { + VideoSnapshot memo = env.console.video().snapshot(); + observed.nature = reader.read_nature(env.logger(), language, memo); + Pokemon::BdspGender gender = reader.read_gender(env.logger(), memo); + observed.gender = gender; + observed.gender_known = gender != Pokemon::BdspGender::Genderless; + } + // 3rd page: stats + { + pbf_press_dpad(context, DPAD_RIGHT, 100ms, 1500ms); + context.wait_for_all_requests(); + VideoSnapshot skills = env.console.video().snapshot(); + observed.stats = reader.read_stats(env.logger(), skills); + } + observed.base_stats = base_stats; + observed.level = level; + observed.shiny = shiny; + observed.shiny_known = shiny_known; + + auto stat = [](int16_t value){ + return value < 0 ? std::string("?") : std::to_string(value); + }; + bool nature_read = observed.nature != Pokemon::NatureCheckerValue::UnableToDetect; + env.log( + "Read from the summary: nature " + + (nature_read + ? Pokemon::NATURE_CHECKER_VALUE_STRINGS().get_string(observed.nature) + : std::string("UNREAD")) + + ", gender " + + (observed.gender_known + ? std::string(Pokemon::bdsp_gender_name(observed.gender)) + : std::string("UNREAD")) + + ", stats " + stat(observed.stats.hp) + "/" + stat(observed.stats.attack) + + "/" + stat(observed.stats.defense) + "/" + stat(observed.stats.spatk) + + "/" + stat(observed.stats.spdef) + "/" + stat(observed.stats.speed) + ".", + nature_read && observed.gender_known ? COLOR_BLUE : COLOR_ORANGE + ); + return true; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.h new file mode 100644 index 0000000000..0c1933c3d5 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.h @@ -0,0 +1,44 @@ +/* BDSP Summary Navigation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_SummaryNavigation_H +#define PokemonAutomation_PokemonBDSP_SummaryNavigation_H + +#include "CommonFramework/Language.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/Pokemon_StatsCalculation.h" +#include "PokemonBDSP_RngCalibration.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +bool open_menu(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +bool open_starter_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +bool navigate_to_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +// Backs out of the Pokemon summary screen (3rd page -> party -> X menu -> overworld). +bool close_starter_summary(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +bool read_observed_pokemon( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + Language language, + const Pokemon::BaseStats& base_stats, + uint8_t level, + bool shiny, + bool shiny_known, + BdspObservedStarter& observed +); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.cpp new file mode 100644 index 0000000000..39420aa7fc --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.cpp @@ -0,0 +1,122 @@ +/* BDSP Target Selection + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonBDSP_RngAim.h" +#include "PokemonBDSP_TargetSelection.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +TargetSelectionResult select_target(const TargetSearchRequest& request){ + TargetSelectionResult result; + + if (!request.wanted){ + result.failure_reason = "No filter was supplied, so nothing can be searched for."; + return result; + } + + BdspStaticSearcher searcher(request.state, request.pokemon, 0); + + // How many advances the scene can spend between the press and the generation + uint64_t lowest_span = 0; + uint64_t highest_span = 0; + schedule_span_bounds(request.timeline, request.max_schedules, lowest_span, highest_span); + + int64_t last_press = (int64_t)(request.first_press + request.window_advances); + + // One sweep, walking the RNG forward rather than jumping to each advance. + uint64_t scan_from = (uint64_t)std::max( + 0, (int64_t)request.first_press + (int64_t)lowest_span + request.bias + ); + uint64_t scan_to = (uint64_t)std::max( + 0, last_press + (int64_t)highest_span + request.bias + ); + std::map wanted_targets; + for (const BdspRngHit& hit : searcher.scan(scan_from, scan_to, request.wanted)){ + wanted_targets.emplace(hit.advances, hit.result); + } + + result.advances_scanned = scan_to - scan_from + 1; + result.matches_found = wanted_targets.size(); + + if (wanted_targets.empty()){ + result.failure_reason = "no matches found within " + + std::to_string(result.advances_scanned) + " advances."; + return result; + } + + uint8_t press_step = request.npcs == 0 ? 1 : request.npcs; + std::set candidate_presses; + for (const auto& item : wanted_targets){ + int64_t lowest_press = std::max( + press_for_advance(item.first, highest_span, request.bias), + (int64_t)request.first_press + ); + int64_t highest_press = std::min( + press_for_advance(item.first, lowest_span, request.bias), + last_press + ); + for (int64_t press = lowest_press; press <= highest_press; press++){ + if (press % press_step != 0){ + continue; + } + candidate_presses.insert((uint64_t)press); + } + } + + double seconds_per_advance = request.tick_seconds / (double)press_step; + bool found = false; + double best_seconds = 0; + for (uint64_t press : candidate_presses){ + double press_at_seconds = (double)(press - request.first_press) * seconds_per_advance; + if (found && press_at_seconds >= best_seconds){ + break; + } + for (const PressSchedule& option : schedule_presses( + request.state, request.timeline, request.timings, press, request.max_schedules + )){ + auto hit = wanted_targets.find(aimed_advance(press, option.span, request.bias)); + if (hit == wanted_targets.end()){ + continue; + } + if (!found || press_at_seconds + option.confirm_seconds < best_seconds){ + found = true; + best_seconds = press_at_seconds + option.confirm_seconds; + result.press_advance = press; + result.schedule = option; + result.target = hit->second; + } + // the first hit is the best this press has to offer + break; + } + } + if (!found){ + // Around one advance in fifty is unreachable due to rapid blinks + result.failure_reason = std::to_string(wanted_targets.size()) + + " matching advance(s) in range, but none reachable"; + return result; + } + + result.target_advance = aimed_advance( + result.press_advance, result.schedule.span, request.bias + ); + result.success = true; + return result; +} + + + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.h b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.h new file mode 100644 index 0000000000..7c1fb044d0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.h @@ -0,0 +1,57 @@ +/* BDSP Target Selection + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_TargetSelection_H +#define PokemonAutomation_PokemonBDSP_TargetSelection_H + +#include +#include +#include +#include +#include "Pokemon/Pokemon_BdspRng.h" +#include "Pokemon/Pokemon_Xorshift128.h" +#include "PokemonBDSP_RngExecution.h" +#include "PokemonBDSP_RngTimeline.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +struct TargetSearchRequest{ + Pokemon::Xorshift128State state; + BdspTimelineContext timeline; + NavigationTimings timings; + Pokemon::BdspStaticTemplate pokemon; + uint8_t npcs = 1; + double tick_seconds = BDSP_NPC_TICK_SECONDS; + int64_t bias = 0; + uint64_t first_press = 0; + uint64_t window_advances = 0; + size_t max_schedules = 8; + std::function wanted; +}; + + +struct TargetSelectionResult{ + bool success = false; + uint64_t press_advance = 0; + uint64_t target_advance = 0; + PressSchedule schedule; + Pokemon::BdspPokemonResult target; + size_t matches_found = 0; + uint64_t advances_scanned = 0; + std::string failure_reason; +}; + + +TargetSelectionResult select_target(const TargetSearchRequest& request); + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index c89c937b0d..c3dd4a032e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1764,6 +1764,43 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Resources/Pokemon_PokemonSlugs.h Source/PokemonBDSP/Inference/PokemonBDSP_NameListDetector.cpp Source/PokemonBDSP/Inference/PokemonBDSP_NameListDetector.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp + Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp @@ -1796,11 +1833,52 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h @@ -1913,6 +1991,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Options/PokemonBDSP_LearnMove.h Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h + Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp + Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h Source/PokemonBDSP/Options/PokemonBDSP_ShortcutDirection.cpp Source/PokemonBDSP/Options/PokemonBDSP_ShortcutDirection.h Source/PokemonBDSP/PokemonBDSP_Panels.cpp @@ -1983,12 +2063,34 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_BlinkRecovery.h Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_IntroSeedFinder.cpp Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_IntroSeedFinder.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngAim.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngCalibration.h Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngDisplays.cpp Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngDisplays.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngExecution.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTargets.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_RngTimeline.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterNavigation.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StateReidentifier.cpp Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StateReidentifier.h Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StateSolver.cpp Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StateSolver.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_SummaryNavigation.h + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.cpp + Source/PokemonBDSP/Programs/RngManipulation/PokemonBDSP_TargetSelection.h Source/PokemonBDSP/Programs/ShinyHunting/PokemonBDSP_LegendaryReset.cpp Source/PokemonBDSP/Programs/ShinyHunting/PokemonBDSP_LegendaryReset.h Source/PokemonBDSP/Programs/ShinyHunting/PokemonBDSP_ShinyHunt-Fishing.cpp From 6ddf4878a1490b549995f19f775171b29c29eefc Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 08:42:10 -0800 Subject: [PATCH 2/3] BDSP NewGame: type the name with core's FastCodeEntry::type_on_system_keyboard autostory/engine (core #62) moved type_on_system_keyboard out of SystemKeyboardDetector.h into FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp index e9217ef552..295446edbf 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp @@ -18,6 +18,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" @@ -255,7 +256,7 @@ void enter_player_name(VideoStream& stream, ProControllerContext& context, StepT context.wait_for_all_requests(); } trace.action("Typing the player's name: " + name); - type_on_system_keyboard(stream, context, name); + FastCodeEntry::type_on_system_keyboard(stream, context, name); pbf_press_button(context, BUTTON_PLUS, 80ms, 0ms); if (!wait_for_keyboard(stream, context, true, 5s)){ trace.fail("The keyboard stayed open after PLUS (OK)."); From 347c6ece1e9ce3757878c3cb8fa1775aea9773ad Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 17:07:19 -0800 Subject: [PATCH 3/3] BDSP AutoStory tests: the menu cursor's tests register in the shared runner #470 registers them with the shared test runner, so BDSP's action tests no longer add them too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index 7099390a16..7071227319 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -23,7 +23,6 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/Logging/Logger.h" -#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" @@ -792,8 +791,6 @@ void add_tests_AutoStory_Actions(UnitTestDatabase& database){ } // The battle actions' own tests. add_tests_AutoStory_BattleActions(database); - // The menu cursor's own tests, with those of the intro, its first user. - add_tests_MenuCursor(database); }