Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f185de0
BDSP AutoStory: no route ends beside a warp walking toward it
dayfine Nov 9, 2026
c19b411
BDSP AutoStory: the story sim's old steps in their own file
dayfine Nov 9, 2026
26b4ed4
BDSP AutoStory: a dim cave floor is not a tap step's fade
dayfine Nov 9, 2026
a6641e7
BDSP AutoStory: mark the tap step's fade and through-already checks a…
dayfine Nov 9, 2026
8758c35
BDSP AutoStory: dump every story ROUTE step's planned legs to a golde…
dayfine Nov 9, 2026
b73ffa5
BDSP AutoStory: the story's route plans golden file
dayfine Nov 9, 2026
12e1203
Merge bdsp/autostory-mine-floaroma-wip (with #269) into bdsp/autostor…
dayfine Nov 9, 2026
afe4c45
BDSP AutoStory: a wall leg that never moved waits, then plans from it…
dayfine Nov 9, 2026
e94a655
BDSP AutoStory: red tests for routes along wander zones and short wal…
dayfine Nov 9, 2026
814a825
BDSP AutoStory: no route runs along a wander zone; short wall legs wa…
dayfine Nov 9, 2026
b9c2d8b
BDSP AutoStory: red tests for 01.10's line-5 recovery wall and a batt…
dayfine Nov 9, 2026
f844923
BDSP AutoStory: recovery takes the cheapest whole walk; 01.10 runs in…
dayfine Nov 9, 2026
b6a554a
BDSP AutoStory: keep the story table under the length limit; line-7 c…
dayfine Nov 9, 2026
83c09c7
BDSP AutoStory: red sim test for 01.11's walk to the old woman
dayfine Nov 9, 2026
dd502a1
BDSP AutoStory: 01.11 walks west to the field's edge, then 4 taps east
dayfine Nov 9, 2026
6437c1b
Merge bdsp/autostory-old-couple-clip (#279) into bdsp/autostory-route…
dayfine Nov 9, 2026
8c9ba9b
BDSP AutoStory: regenerate the route plans golden after #277, #278 an…
dayfine Nov 9, 2026
769e8b7
BDSP AutoStory: console failure cases on the story sim (known failing…
dayfine Nov 9, 2026
7aeb9e9
Merge bdsp/autostory-mine-floaroma-wip (#279 with #285 squash-merged)…
dayfine Nov 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h"
Expand Down Expand Up @@ -46,6 +47,7 @@ void add_tests(UnitTestDatabase& database){
add_tests_AutoStory_WallBattleSim(database);
add_tests_AutoStory_RecoveryWalk(database);
add_tests_AutoStory_StorySim(database);
add_tests_AutoStory_SimFailureCases(database);
}


Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Story table Tests

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=745 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -15,6 +15,7 @@
#include <utility>
#include <vector>
#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_StorySteps.h"
Expand Down Expand Up @@ -252,20 +253,70 @@
return true;
}

// The legs a ROUTE step from `from` plans, as walk_route() plans them (the
// step's costs, then the default ones), on `world`; nothing if none.
std::optional<std::vector<PlannedLeg>> route_step_plan(const StoryWorld& world, const Pokemon::Pose& from, const Pokemon::StoryStep& step){
std::vector<TilePos> start;
for (const TilePos& pos : Pokemon::tiles_of(from)){
start.push_back(story_to_world(world, from.map, pos));
// 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<Pokemon::Pose> 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;
};

std::optional<Pokemon::Pose> pose_after_step(const std::optional<Pokemon::Pose>& at, const Pokemon::StoryStep& step){
if (step.kind == StoryStepKind::ROUTE){
return step.target;
}
if (step.pose_after){
return step.pose_after;
}
return step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP ? std::nullopt : at;
}

std::vector<TableRoute> table_routes(){
std::vector<TableRoute> routes;
for (const StoryCheckpoint& row : story_checkpoints()){
std::optional<Pokemon::Pose> at = row.start;
for (const Pokemon::StoryStep& step : row.steps){
if (step.kind == StoryStepKind::ROUTE){
const bool known = at && !Pokemon::tiles_of(*at).empty();
routes.push_back({row.id, &step, at, known ? story_plan_world(at->map, step.target->map) : nullptr});
}
at = pose_after_step(at, step);
}
}
std::vector<TilePos> targets;
for (const TilePos& pos : Pokemon::tiles_of(*step.target)){
targets.push_back(story_to_world(world, step.target->map, pos));
return routes;
}

std::vector<TilePos> world_tiles(const StoryWorld& world, const Pokemon::Pose& pose){
std::vector<TilePos> tiles;
for (const TilePos& pos : Pokemon::tiles_of(pose)){
tiles.push_back(story_to_world(world, pose.map, pos));
}
return tiles;
}

// 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<TilePos> start;
std::optional<std::vector<PlannedLeg>> legs;
bool default_costs = false; // the step's own costs found no plan
};

RouteStepPlan plan_table_route(const TableRoute& route){
const StoryWorld& world = *route.world;
const std::vector<TilePos> targets = world_tiles(world, *route.step->target);
const auto goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); };
std::optional<std::vector<PlannedLeg>> legs = plan_recovered_route(world, start, goal, *story_route_costs(step.variant));
return legs ? legs : plan_recovered_route(world, start, goal, RobustPlanCosts());
RouteStepPlan plan{.start = world_tiles(world, *route.from)};
plan.legs = plan_recovered_route(world, plan.start, goal, *story_route_costs(route.step->variant));
if (!plan.legs){
plan.legs = plan_recovered_route(world, plan.start, goal, RobustPlanCosts());
plan.default_costs = plan.legs.has_value();
}
return plan;
}

// Why `planned` may slide into a door or a warp, or "": an open leg (not to
Expand Down Expand Up @@ -303,26 +354,14 @@
// unknown place is logged, not planned.
std::vector<PlannedStoryRoute> plan_story_routes(Logger& logger){
std::vector<PlannedStoryRoute> routes;
for (const StoryCheckpoint& row : story_checkpoints()){
std::optional<Pokemon::Pose> at = row.start;
for (const Pokemon::StoryStep& step : row.steps){
const bool known = at && !Pokemon::tiles_of(*at).empty();
const StoryWorld* world = step.kind == StoryStepKind::ROUTE && known
? story_plan_world(at->map, step.target->map) : nullptr;
if (step.kind == StoryStepKind::ROUTE && world == nullptr){
logger.log(std::format("{} {}: not checked, from {}.", row.id, step.name, at ? Pokemon::to_string(*at) : "nowhere known"));
}
if (world != nullptr){
std::vector<TilePos> from;
for (const TilePos& pos : Pokemon::tiles_of(*at)){
from.push_back(story_to_world(*world, at->map, pos));
}
routes.push_back({row.id + " " + step.name, world, std::move(from), route_step_plan(*world, *at, step)});
}
at = step.kind == StoryStepKind::ROUTE ? step.target
: step.pose_after ? step.pose_after
: step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP ? std::nullopt : at;
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;
}
Expand Down Expand Up @@ -451,6 +490,175 @@
}


// 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 const char* 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=<path> 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<TilePos>& 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<TilePos>& start, const std::vector<PlannedLeg>& 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<PlannedLeg>& 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.variant.empty() ? "route" : step.variant);
out += " from: " + (route.from ? Pokemon::to_string(*route.from) : std::string("unknown")) + "\n";
out += " target: " + Pokemon::to_string(*step.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 = 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<std::string> plan_blocks(const std::string& text){
std::vector<std::string> 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<std::string> expected = plan_blocks(golden);
const std::vector<std::string> 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){

Check warning on line 633 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_route_plans_golden( Logger & logger): function_lines=27 (advisory above 25, limit 50)
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;
}


struct AutoStoryStoryTableInput{
std::string name;
std::function<UnitTestResult(Logger&)> body;
Expand Down Expand Up @@ -510,6 +718,10 @@
.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 = {},
Expand Down
Loading
Loading