From 05552e37a75e04788977658375b193a121d457f2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 15:22:18 -0800 Subject: [PATCH 1/3] FRLG AutoStory: flag story routes that cross a tile twice revisited_tiles() lays a planned route out tile by tile (leg_tiles(), warp and spinner ends, Surf landings, gates as the route changes them) and lists the tiles it steps on again. The story plan test collects them per route step; a new test fails on any route that walks back over its own way, except the nine in ALLOWED_REVISITS, each with its reason. --- .../Overworld/PokemonFRLG_OverworldRoute.cpp | 43 ++++++ .../Overworld/PokemonFRLG_OverworldRoute.h | 11 ++ .../PokemonFRLG_OverworldRoute_Tests.cpp | 79 ++++++++++ .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 137 +++++++++++++++--- 4 files changed, 246 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp index 126d41dea0..65b9db66ac 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp @@ -871,6 +871,49 @@ std::vector leg_tiles( return tiles; } +std::vector revisited_tiles( + const std::vector& maps, + const MapPosition& start, const std::vector& route, + const Pokemon::GateState& start_gates +){ + Pokemon::GateState gates = start_gates; + const bool gated = has_gates(maps); + std::vector resolved; + if (gated){ + resolved = resolve_maps(maps, gates); + } + std::vector visited{start}; + std::vector revisited; + const auto visit = [&](const MapPosition& tile){ + if (std::ranges::find(visited, tile) != visited.end()){ + revisited.push_back(tile); + }else{ + visited.push_back(tile); + } + }; + MapPosition at = start; + for (const RouteStep& step : route){ + if (step.action == RouteAction::OPEN_GATE || step.action == RouteAction::PRESS_SWITCH){ + Pokemon::apply_gate_change(gates, step.gate_change); + if (gated){ + resolved = resolve_maps(maps, gates); + } + }else if (step.action == RouteAction::WALK){ + const std::vector tiles = leg_tiles(gated ? resolved : maps, at, step); + for (size_t c = 1; c < tiles.size(); c++){ + visit(tiles[c]); + } + if (!(tiles.back() == step.end)){ + visit(step.end); + } + }else if (step.action == RouteAction::SURF){ + visit(step.end); + } + at = step.end; + } + return revisited; +} + namespace{ // `step`'s tiles from `player`, on `maps` as the gates now make them. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h index 605e61f49d..d19241f396 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h @@ -64,6 +64,17 @@ std::vector leg_tiles( const MapPosition& start, const RouteStep& step ); +// The tiles `route` from `start` steps on again, in the order it does: a +// route that crosses a tile twice walks back over its own way. Counts the +// tiles leg_tiles() walks, each warp's and spinner's end, and each Surf's +// landing. The gates start as `gates`, and the route's OPEN_GATE and +// PRESS_SWITCH steps change them, as in follow_overworld_route(). +std::vector revisited_tiles( + const std::vector& maps, + const MapPosition& start, const std::vector& route, + const Pokemon::GateState& gates +); + // Replays `route` from `start` with the game's step rules and returns where // it ends. Throws InternalProgramError if a step can't be taken as planned. // A route with gates (plan_overworld_route_gated()) starts with `gates`; its diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp index 9cf254feb9..75f3b34f31 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "PokemonFRLG_OverworldMaps.h" @@ -478,7 +480,84 @@ UnitTestResult test_ground_only_elevation_left_out(){ return true; } +struct RevisitInput{ + std::vector route; +}; +struct RevisitExpectation{ + std::vector revisited; +}; +struct RevisitCase{ + std::string_view name; + RevisitInput input; + RevisitExpectation expectation; +}; + +RouteStep sign_room_walk(Direction direction, int tiles, TilePos end, bool to_wall){ + RouteStep step; + step.direction = direction; + step.tiles = tiles; + step.to_wall = to_wall; + step.end = MapPosition{"SignRoom", end}; + return step; +} + +// The routes from the sign room's (1, 3), its south-west corner. +const MapPosition SIGN_ROOM_CORNER{"SignRoom", {1, 3}}; +const MapPosition SIGN_ROOM_SOUTH_MIDDLE{"SignRoom", {2, 3}}; +const std::vector EAST_AND_BACK{ + sign_room_walk(Direction::EAST, 2, {3, 3}, true), + sign_room_walk(Direction::WEST, 1, {2, 3}, false), +}; +const std::vector EAST_THEN_NORTH{ + sign_room_walk(Direction::EAST, 2, {3, 3}, true), + sign_room_walk(Direction::NORTH, 2, {3, 1}, true), +}; +const std::vector ROUND_TO_THE_CORNER{ + sign_room_walk(Direction::NORTH, 2, {1, 1}, true), + sign_room_walk(Direction::EAST, 1, {2, 1}, false), + sign_room_walk(Direction::SOUTH, 2, {2, 3}, true), + sign_room_walk(Direction::WEST, 1, {1, 3}, true), +}; + +// revisited_tiles() on the sign room: a leg back over the one before +// crosses its tiles again; a turn the other way doesn't. +UnitTestResult test_revisited_tiles(){ + const std::vector maps{parse_map(TEST_SIGN_ROOM_JSON, "SignRoom")}; + const RevisitCase CASES[] = { + { + .name = "east to the wall, then back west", + .input = {.route = EAST_AND_BACK}, + .expectation = {.revisited = {SIGN_ROOM_SOUTH_MIDDLE}}, + }, + { + .name = "east, then north", + .input = {.route = EAST_THEN_NORTH}, + .expectation = {.revisited = {}}, + }, + { + .name = "round the room to the start", + .input = {.route = ROUND_TO_THE_CORNER}, + .expectation = {.revisited = {SIGN_ROOM_CORNER}}, + }, + }; + for (const RevisitCase& test_case : CASES){ + const std::vector revisited = revisited_tiles(maps, SIGN_ROOM_CORNER, test_case.input.route, {}); + if (revisited != test_case.expectation.revisited){ + std::string text; + for (const MapPosition& tile : revisited){ + text += std::format(" {}", to_string(tile)); + } + return std::format("{}: revisited{}:{}", test_case.name, text.empty() ? " none" : text, describe(test_case.input.route)); + } + } + return true; +} + const RouteTestCase ROUTE_CASES[] = { + { + .input = {.name = "tiles a route crosses twice", .body = test_revisited_tiles}, + .expectation = {.description = "revisited_tiles() lists the tiles a route steps on again"}, + }, { .input = {.name = "route through a warp", .body = test_route_through_warp}, .expectation = {.description = "route through a warp"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index 43fe417702..6149d98e68 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -272,6 +272,29 @@ UnitTestResult check_held_legs(const std::string& name, const std::vector tiles; +}; + +// What following the story's routes collects: the golden's lines, and the +// routes that cross a tile twice. +struct StoryPlans{ + std::vector golden; + std::vector revisits; +}; + +// Adds `route`, `step`'s plan from `start`, to `plans` if it crosses a +// tile twice. +void note_revisits(const StoryStep& step, const MapPosition& start, const std::vector& route, StoryPlans& plans){ + std::vector tiles = revisited_tiles(walk_story_maps(), start, route, walk_state().gates); + if (!tiles.empty()){ + plans.revisits.push_back(RouteRevisit{.checkpoint = {}, .step = step.name, .tiles = std::move(tiles)}); + } +} + // Walks `step`'s fixed legs from `pose` and moves `pose` to their end. UnitTestResult check_legs(StoryPose& pose, const StoryStep& step){ const std::optional> route = fixed_route(walk_maps(), MapPosition{pose.map, pose.tile}, Pokemon::story_arg>(step.args, "legs")); @@ -311,8 +334,8 @@ std::string golden_line(const RouteStep& step){ } // Plans `step` from `pose` and moves `pose` to its end; adds the plan to -// `golden`. -UnitTestResult check_route(StoryPose& pose, const StoryStep& step, std::vector& golden){ +// `plans`. +UnitTestResult check_route(StoryPose& pose, const StoryStep& step, StoryPlans& plans){ const MapPosition start{pose.map, pose.tile}; std::optional facing = pose.facing; if (!facing){ @@ -329,11 +352,12 @@ UnitTestResult check_route(StoryPose& pose, const StoryStep& step, std::vector& golden){ +UnitTestResult check_checkpoint_steps(const StoryCheckpoint& checkpoint, StoryPose& pose, StoryPlans& plans){ for (const StoryStep& step : checkpoint.steps){ if (step.kind == StoryStepKind::ROUTE || step.kind == StoryStepKind::LEGS){ - UnitTestResult result = step.kind == StoryStepKind::ROUTE ? check_route(pose, step, golden) : check_legs(pose, step); + UnitTestResult result = step.kind == StoryStepKind::ROUTE ? check_route(pose, step, plans) : check_legs(pose, step); if (result.result != UnitTestResult::PASSED){ return checkpoint.id + " " + result.message; } @@ -368,7 +392,7 @@ UnitTestResult check_checkpoint_steps(const StoryCheckpoint& checkpoint, StoryPo // goes on from where the player stands. StoryPose nurse_pose = pose; StoryStep to_nurse = step; - UnitTestResult result = check_route(nurse_pose, to_nurse, golden); + UnitTestResult result = check_route(nurse_pose, to_nurse, plans); if (result.result != UnitTestResult::PASSED){ return checkpoint.id + " " + result.message; } @@ -385,20 +409,24 @@ UnitTestResult check_checkpoint_steps(const StoryCheckpoint& checkpoint, StoryPo } // Follows `checkpoint` from its start; `pose` ends where it leaves the -// player. Adds each planned route to `golden`, after the checkpoint's id. -UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& pose, std::vector& golden){ - const size_t first = golden.size(); - UnitTestResult result = check_checkpoint_steps(checkpoint, pose, golden); - for (size_t c = first; c < golden.size(); c++){ - golden[c] = std::format("{} {}", checkpoint.id, golden[c]); +// player. Adds each planned route to `plans`, after the checkpoint's id. +UnitTestResult check_checkpoint(const StoryCheckpoint& checkpoint, StoryPose& pose, StoryPlans& plans){ + const size_t first = plans.golden.size(); + const size_t first_revisit = plans.revisits.size(); + UnitTestResult result = check_checkpoint_steps(checkpoint, pose, plans); + for (size_t c = first; c < plans.golden.size(); c++){ + plans.golden[c] = std::format("{} {}", checkpoint.id, plans.golden[c]); + } + for (size_t c = first_revisit; c < plans.revisits.size(); c++){ + plans.revisits[c].checkpoint = checkpoint.id; } return result; } // Plans every route of the story, each checkpoint from where the one // before leaves the player (or, past rows not authored yet, from its own -// start: starts_after_unauthored_rows()), and adds the plans to `golden`. -UnitTestResult chain_story_routes(std::vector& golden){ +// start: starts_after_unauthored_rows()), and adds the plans to `plans`. +UnitTestResult chain_story_routes(StoryPlans& plans){ const std::vector& checkpoints = story_checkpoints(); if (checkpoints.size() != 96 || checkpoints[0].start){ return "Expected 96 checkpoints, the first from a new game."; @@ -418,7 +446,7 @@ UnitTestResult chain_story_routes(std::vector& golden){ } pose = start; } - UnitTestResult result = check_checkpoint(checkpoint, pose, golden); + UnitTestResult result = check_checkpoint(checkpoint, pose, plans); if (result.result != UnitTestResult::PASSED){ return result; } @@ -427,8 +455,8 @@ UnitTestResult chain_story_routes(std::vector& golden){ } UnitTestResult test_routes_and_chain(){ - std::vector golden; - return chain_story_routes(golden); + StoryPlans plans; + return chain_story_routes(plans); } // Every story route's plan against the stored copy in @@ -438,11 +466,12 @@ UnitTestResult test_routes_and_chain(){ // FRLG_PLAN_GOLDEN_10 (segment 10), FRLG_PLAN_GOLDEN_11 (segment 11) and FRLG_PLAN_GOLDEN_12 (segment 12 on) // with the lines the failure lists after "GOLDEN|". UnitTestResult test_golden_plans(){ - std::vector actual; - UnitTestResult result = chain_story_routes(actual); + StoryPlans plans; + UnitTestResult result = chain_story_routes(plans); if (result.result != UnitTestResult::PASSED){ return result; } + const std::vector& actual = plans.golden; std::vector golden(std::begin(FRLG_PLAN_GOLDEN), std::end(FRLG_PLAN_GOLDEN)); golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_10), std::end(FRLG_PLAN_GOLDEN_10)); golden.insert(golden.end(), std::begin(FRLG_PLAN_GOLDEN_11), std::end(FRLG_PLAN_GOLDEN_11)); @@ -474,6 +503,61 @@ UnitTestResult test_golden_plans(){ return message; } +// Story routes that cross a tile twice on purpose, and why. Any other +// route that does walks back over its own way: re-plan it. +struct AllowedRevisit{ + std::string_view checkpoint; + std::string_view step; + std::string_view reason; +}; +const AllowedRevisit ALLOWED_REVISITS[] = { + {"12.03", "land_middle_sand_bar", "west to the wall (5, 3) on Route 21's water and back east 1: the wall places the player, which an open leg on the water can't (OPEN_WATER_STOP)"}, + {"13.01", "mansion_holes", "east to the wall (20, 6), round and back down column 19 rather than end an open leg in the Mansion's wild rooms (OPEN_WILD_STOP): a cost choice to re-weigh"}, + {"13.03", "to_elixir", "to the switch (24, 30) and back: pressing it opens the way on along row 17"}, + {"13.04", "to_key", "up column 30 to the switch (27, 6) and back down it: pressing it opens the way west along row 10"}, + {"16.02", "to_corridor", "off 9F's teleporter (22, 18) and back on, the way to 5F's (10, 20); the step off is held to the wall (18, 18) rather than one open tile (OPEN_STOP): a cost choice to re-weigh"}, + {"16.03", "to_3f_trainer", "the same teleporter bounce on 9F as 16.02's to_corridor"}, + {"19.03", "volcano_guard_approach", "north to the wall (17, 61) and back south 1: the wall places the player after 16 tiles of grass (OPEN_WILD_STOP)"}, + {"19.04b", "below_ladder", "out to Route 23 and straight back in, onto the cave mouth (11, 20), rather than end an open leg there (OPEN_WILD_STOP): a cost choice to re-weigh"}, + {"19.04c", "ladder_3f", "down 3F's ladder to 2F's (3, 3) and back up it onto (5, 2); the step off is held to the wall (3, 5) rather than one open tile (OPEN_STOP): a cost choice to re-weigh"}, +}; + +std::string describe(const RouteRevisit& revisit){ + std::string text = std::format("{} {}:", revisit.checkpoint, revisit.step); + for (const MapPosition& tile : revisit.tiles){ + text += std::format(" {}", to_string(tile)); + } + return text; +} + +// No story route crosses a tile twice, but those ALLOWED_REVISITS lists; +// and each it lists still does. +UnitTestResult test_routes_cross_no_tile_twice(){ + StoryPlans plans; + UnitTestResult result = chain_story_routes(plans); + if (result.result != UnitTestResult::PASSED){ + return result; + } + const auto same = [](const AllowedRevisit& allowed, const RouteRevisit& revisit){ + return allowed.checkpoint == revisit.checkpoint && allowed.step == revisit.step; + }; + std::string message; + for (const RouteRevisit& revisit : plans.revisits){ + if (std::ranges::none_of(ALLOWED_REVISITS, [&](const AllowedRevisit& allowed){ return same(allowed, revisit); })){ + message += std::format("\n crosses a tile again: {}", describe(revisit)); + } + } + for (const AllowedRevisit& allowed : ALLOWED_REVISITS){ + if (std::ranges::none_of(plans.revisits, [&](const RouteRevisit& revisit){ return same(allowed, revisit); })){ + message += std::format("\n allowed, but crosses no tile twice: {} {}", allowed.checkpoint, allowed.step); + } + } + if (!message.empty()){ + return std::format("Story routes that walk back over their own way:{}", message); + } + return true; +} + struct ExpectedLeg{ Direction direction; int tiles; @@ -769,8 +853,8 @@ bool grass_held(const StoryPose& pose, const StoryStep& step){ // Moves `pose` past `step`, as check_checkpoint() does. UnitTestResult advance(StoryPose& pose, const StoryStep& step){ if (step.kind == StoryStepKind::ROUTE){ - std::vector golden; - return check_route(pose, step, golden); + StoryPlans plans; + return check_route(pose, step, plans); } if (step.kind == StoryStepKind::LEGS){ return check_legs(pose, step); @@ -1265,8 +1349,9 @@ UnitTestResult test_save_place_error(){ // leg to a wall or a warp, or lands and then walks to one, which finds the // player again. UnitTestResult test_route21_water_legs(){ - std::vector plan; - UnitTestResult result = chain_story_routes(plan); + StoryPlans plans; + UnitTestResult result = chain_story_routes(plans); + const std::vector& plan = plans.golden; if (result.result != UnitTestResult::PASSED){ return result; } @@ -1589,6 +1674,10 @@ const AutoStoryTestCase AUTO_STORY_CASES[] = { .input = {.name = "story route plans match the golden", .body = test_golden_plans}, .expectation = {.description = "keeps every story route's planned steps byte-identical to the stored copy"}, }, + { + .input = {.name = "story routes cross no tile twice", .body = test_routes_cross_no_tile_twice}, + .expectation = {.description = "flags a route that walks back over its own tiles, but the ones allowed with a reason"}, + }, { .input = {.name = "step names are unique per checkpoint", .body = test_unique_names}, .expectation = {.description = "ensures step identifier uniqueness within checkpoints"}, From 550a35fe39a0b6338ae99a8337c48034b53f5c1e Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:20:51 -0800 Subject: [PATCH 2/3] Leg planner: a one-step leg off a warp pays no open stop A step can't stop part-way, and a warp's arrival is a known tile, so a leg of one step off a warp is no guess. The planner held such steps to a wall and back instead (9F's teleporter in 16.02 and 16.03, 2F's ladder in 19.04c), crossing tiles twice. Now it steps one tile off and back on. Only off a warp: a route's start or the end of another leg may be off by a tile, and single steps from walls weave round longer holds. --- .../Pokemon/Navigation/Pokemon_LegPlanner.h | 26 ++++++++++++++----- .../Navigation/Pokemon_LegPlanner_Tests.cpp | 26 +++++++++++++++++++ .../AutoStory/PokemonFRLG_AutoStory_Tests.cpp | 3 --- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h index 043b7e5306..924d410e16 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h @@ -149,7 +149,9 @@ namespace LegPlannerInternal{ // they are in the middle of a leg that could go on, whether that leg had a // wild step, whether it is one step from where a hold to a wall ended, and // whether such a step was stopped after earlier (see tap_stop_risk() at -// the top of this file). +// the top of this file); whether the leg is one step so far, and whether +// it started on a warp's arrival (at rest: whether a warp put the player +// there). template struct Node{ State state; @@ -158,6 +160,8 @@ struct Node{ bool wild = false; bool tap_from_wall = false; bool tapped = false; + bool one_step = false; + bool from_warp = false; bool operator==(const Node& other) const = default; }; @@ -166,8 +170,8 @@ struct Node{ template struct NodeHash{ size_t operator()(const Node& node) const{ - return StateHash()(node.state) * 80 + node.facing * 16 + (node.moving ? 8 : 0) + (node.wild ? 4 : 0) - + (node.tap_from_wall ? 2 : 0) + (node.tapped ? 1 : 0); + return StateHash()(node.state) * 320 + node.facing * 64 + (node.moving ? 32 : 0) + (node.wild ? 16 : 0) + + (node.tap_from_wall ? 8 : 0) + (node.tapped ? 4 : 0) + (node.one_step ? 2 : 0) + (node.from_warp ? 1 : 0); } }; @@ -180,6 +184,15 @@ struct Move{ }; // Cost incurred when stopping at a node. +// A leg of one step off a warp's arrival pays no open stop, nor a wild +// one: a step can't stop part-way, and the warp put the player on a known +// tile, so stepping off a teleporter or a ladder and back on again is one +// tile each way rather than a hold to a wall and back. What the model +// weighs past the stop it still pays. Only off a warp: a route's start +// may be where an open leg left the player, a step after another leg +// starts wherever that leg ended, and steps from walls on land weave runs +// of single tiles round longer holds (see tap_stop_risk() at the top of +// this file). template int stop_cost(const Model& model, const Node& node, const LegCosts& costs){ if (!node.moving || model.held_at(node.state, (Direction)node.facing)){ @@ -192,7 +205,7 @@ int stop_cost(const Model& model, const Node& node, const LegCosts& costs } } } - int cost = costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + int cost = node.one_step && node.from_warp ? 0 : costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); if constexpr (requires { model.open_stop_risk(node.state, Direction::NORTH); }){ cost += model.open_stop_risk(node.state, (Direction)node.facing); } @@ -283,7 +296,8 @@ void expand( const bool from_wall = current.moving && model.held_at(current.state, (Direction)current.facing); const Node next{ step->next, (int)step->facing, continues, (extends && current.wild) || step->wild, - !extends && from_wall, current.tapped || (!extends && current.tap_from_wall) + !extends && from_wall, current.tapped || (!extends && current.tap_from_wall), + !extends, step->warped || (extends ? current.from_warp : !current.moving && current.from_warp) }; int step_cost = costs.per_tile + step->extra_cost; if (current.facing != (int)direction){ @@ -382,7 +396,7 @@ std::optional>> plan_legs( using State = typename Model::State; using Node = LegPlannerInternal::Node; - const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false}; + const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false, false, false}; LegPlannerInternal::Search search; const std::optional end = LegPlannerInternal::search_goal(model, first, at_goal, goal_facing, costs, search); if (!end){ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp index eea9549166..1351a88be6 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp @@ -318,6 +318,28 @@ UnitTestResult test_wild_ends_with_leg(){ return true; } +UnitTestResult test_one_tile_open_stop(){ + // From X, east 1 and south to the bottom wall stops in the open after + // one step; east to the wall and round is 2 tiles more and a turn. A + // step off a warp's arrival can't stop part-way, so after the warp on + // the right the short way pays no open stop. From the start on X, not + // put there by a warp, it does. + const ToyGame game({ + "X...#W", + "#.#.#.", + "#...#.", + }); + const auto warped = plan(game, {5, 2}, {1, 2}, Direction::NORTH); + if (!warped || warped->size() != 3 || !(*warped)[0].warps || (*warped)[1].tiles != 1 || (*warped)[1].to_wall){ + return std::format("After the warp, expected east 1 in the open, then south:{}", warped ? describe(*warped) : " none"); + } + const auto started = plan(game, {0, 0}, {1, 2}, Direction::NORTH); + if (!started || started->empty() || !(*started)[0].to_wall){ + return std::format("From the start, expected east to the wall:{}", started ? describe(*started) : " none"); + } + return true; +} + UnitTestResult test_unreachable(){ const ToyGame game({ ".#.", @@ -402,6 +424,10 @@ const LegPlannerTestCase LEG_PLANNER_CASES[] = { .input = {.name = "a new leg is not wild", .test = test_wild_ends_with_leg}, .expectation = {.description = "clears wild status on subsequent non-grass legs"}, }, + { + .input = {.name = "a one-tile leg off a warp pays no open stop", .test = test_one_tile_open_stop}, + .expectation = {.description = "takes one step off a warp's arrival into the open over a longer way round to walls"}, + }, }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index 6149d98e68..5118cbb49e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -515,11 +515,8 @@ const AllowedRevisit ALLOWED_REVISITS[] = { {"13.01", "mansion_holes", "east to the wall (20, 6), round and back down column 19 rather than end an open leg in the Mansion's wild rooms (OPEN_WILD_STOP): a cost choice to re-weigh"}, {"13.03", "to_elixir", "to the switch (24, 30) and back: pressing it opens the way on along row 17"}, {"13.04", "to_key", "up column 30 to the switch (27, 6) and back down it: pressing it opens the way west along row 10"}, - {"16.02", "to_corridor", "off 9F's teleporter (22, 18) and back on, the way to 5F's (10, 20); the step off is held to the wall (18, 18) rather than one open tile (OPEN_STOP): a cost choice to re-weigh"}, - {"16.03", "to_3f_trainer", "the same teleporter bounce on 9F as 16.02's to_corridor"}, {"19.03", "volcano_guard_approach", "north to the wall (17, 61) and back south 1: the wall places the player after 16 tiles of grass (OPEN_WILD_STOP)"}, {"19.04b", "below_ladder", "out to Route 23 and straight back in, onto the cave mouth (11, 20), rather than end an open leg there (OPEN_WILD_STOP): a cost choice to re-weigh"}, - {"19.04c", "ladder_3f", "down 3F's ladder to 2F's (3, 3) and back up it onto (5, 2); the step off is held to the wall (3, 5) rather than one open tile (OPEN_STOP): a cost choice to re-weigh"}, }; std::string describe(const RouteRevisit& revisit){ From 12eb0ff789836082d125d6ec683a41a9233c1b23 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:50:56 -0800 Subject: [PATCH 3/3] Leg planner: free the one-step stop off a warp only for the step back The waiver applied to any one-step leg off a warp, so walks out of every door took a free step and then an extra leg (02.02 to 19.04 changed, 28 checkpoints), and 19.04b and 19.04d left Victory Road for Route 23 and came straight back in to use it. Only the step straight back onto the arrival is exact enough to skip the open stop: 9F's teleporter in 16.02 and 16.03 and 2F's ladder in 19.04c still step off and back on; every other plan is as before. Goldens updated for those three. --- .../Pokemon/Navigation/Pokemon_LegPlanner.h | 28 ++++++---- .../Navigation/Pokemon_LegPlanner_Tests.cpp | 52 ++++++++++++++++--- .../PokemonFRLG_AutoStoryPlanGolden16.h | 8 +-- .../PokemonFRLG_AutoStoryPlanGolden19.h | 6 +-- 4 files changed, 70 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h index 924d410e16..5d427ec088 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h @@ -183,18 +183,25 @@ struct Move{ Step step; }; -// Cost incurred when stopping at a node. -// A leg of one step off a warp's arrival pays no open stop, nor a wild -// one: a step can't stop part-way, and the warp put the player on a known -// tile, so stepping off a teleporter or a ladder and back on again is one -// tile each way rather than a hold to a wall and back. What the model -// weighs past the stop it still pays. Only off a warp: a route's start +// Cost incurred when stopping at a node, before stepping `next` if the +// route goes on. +// A leg of one step off a warp's arrival, followed by the step straight +// back, pays no open stop, nor a wild one: a step can't stop part-way, and +// the warp put the player on a known tile, so stepping off a teleporter or +// a ladder and back on again is one tile each way rather than a hold to a +// wall and back. What the model weighs past the stop it still pays. Only +// straight back: going on from the stop starts a leg from wherever an +// interruption left the player, and free single steps off doors would turn +// every walk out of one into an extra leg. Only off a warp: a route's start // may be where an open leg left the player, a step after another leg // starts wherever that leg ended, and steps from walls on land weave runs // of single tiles round longer holds (see tap_stop_risk() at the top of // this file). template -int stop_cost(const Model& model, const Node& node, const LegCosts& costs){ +int stop_cost( + const Model& model, const Node& node, const LegCosts& costs, + std::optional next = std::nullopt +){ if (!node.moving || model.held_at(node.state, (Direction)node.facing)){ return 0; } @@ -205,7 +212,10 @@ int stop_cost(const Model& model, const Node& node, const LegCosts& costs } } } - int cost = node.one_step && node.from_warp ? 0 : costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + const bool straight_back = next && (int)*next == (node.facing + 2) % 4; + int cost = node.one_step && node.from_warp && straight_back + ? 0 + : costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); if constexpr (requires { model.open_stop_risk(node.state, Direction::NORTH); }){ cost += model.open_stop_risk(node.state, (Direction)node.facing); } @@ -301,7 +311,7 @@ void expand( }; int step_cost = costs.per_tile + step->extra_cost; if (current.facing != (int)direction){ - step_cost += costs.per_turn + stop_cost(model, current, costs); + step_cost += costs.per_turn + stop_cost(model, current, costs, direction); } relax(next, cost + step_cost, Move{current, direction, *step}, search); } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp index 1351a88be6..f2529ab464 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" @@ -23,7 +24,8 @@ namespace{ // '#' wall, '.' floor, '"' tall grass, 'p' a person (blocks, but may walk // off, so not a wall to stop against), 'v' entered only going south, 'W' -// a warp to the tile marked 'X'. +// a warp to the tile marked 'X'. If there is a 'Y', 'X' is a teleporter +// too: stepping onto it, not arriving on it, takes the player to 'Y'. class ToyGame{ public: using State = TilePos; @@ -37,6 +39,9 @@ class ToyGame{ if (m_rows[y][x] == 'X'){ m_warp_to = {x, y}; } + if (m_rows[y][x] == 'Y'){ + m_teleport_to = TilePos{x, y}; + } } } } @@ -54,6 +59,12 @@ class ToyGame{ step.extra_cost = 5; step.wild = true; } + if (tile == 'X' && m_teleport_to){ + step.next = *m_teleport_to; + step.warped = true; + step.ends_leg = true; + step.extra_cost = 10; + } if (tile == 'W'){ step.next = m_warp_to; step.warped = true; @@ -77,6 +88,7 @@ class ToyGame{ std::vector m_rows; TilePos m_warp_to; + std::optional m_teleport_to; }; @@ -321,17 +333,18 @@ UnitTestResult test_wild_ends_with_leg(){ UnitTestResult test_one_tile_open_stop(){ // From X, east 1 and south to the bottom wall stops in the open after // one step; east to the wall and round is 2 tiles more and a turn. A - // step off a warp's arrival can't stop part-way, so after the warp on - // the right the short way pays no open stop. From the start on X, not - // put there by a warp, it does. + // step off a warp's arrival can't stop part-way, but where it leaves + // the player only matters to the step straight back: going on from + // there, the short way pays the open stop, after the warp on the right + // as from the start on X. const ToyGame game({ "X...#W", "#.#.#.", "#...#.", }); const auto warped = plan(game, {5, 2}, {1, 2}, Direction::NORTH); - if (!warped || warped->size() != 3 || !(*warped)[0].warps || (*warped)[1].tiles != 1 || (*warped)[1].to_wall){ - return std::format("After the warp, expected east 1 in the open, then south:{}", warped ? describe(*warped) : " none"); + if (!warped || warped->size() < 2 || !(*warped)[0].warps || !(*warped)[1].to_wall){ + return std::format("After the warp, expected east to the wall:{}", warped ? describe(*warped) : " none"); } const auto started = plan(game, {0, 0}, {1, 2}, Direction::NORTH); if (!started || started->empty() || !(*started)[0].to_wall){ @@ -340,6 +353,25 @@ UnitTestResult test_one_tile_open_stop(){ return true; } +UnitTestResult test_one_tile_off_warp_and_back(){ + // W puts the player on the teleporter X, facing east. Using it means + // stepping off and back on: east 1 into the open and straight back + // west, or east 2 to the wall and back. The step off can't stop + // part-way, so the short way pays no open stop. + const ToyGame game({ + "..W#Y.", + "####..", + "X..#..", + }); + const auto route = plan(game, {0, 0}, {5, 2}, Direction::EAST); + if (!route || route->size() < 3 || !(*route)[0].warps || (*route)[1].tiles != 1 || (*route)[1].to_wall + || (*route)[2].direction != Direction::WEST || (*route)[2].tiles != 1 || !(*route)[2].warps + ){ + return std::format("Expected the warp, east 1, then west 1 onto the teleporter:{}", route ? describe(*route) : " none"); + } + return true; +} + UnitTestResult test_unreachable(){ const ToyGame game({ ".#.", @@ -425,8 +457,12 @@ const LegPlannerTestCase LEG_PLANNER_CASES[] = { .expectation = {.description = "clears wild status on subsequent non-grass legs"}, }, { - .input = {.name = "a one-tile leg off a warp pays no open stop", .test = test_one_tile_open_stop}, - .expectation = {.description = "takes one step off a warp's arrival into the open over a longer way round to walls"}, + .input = {.name = "a one-tile leg off a warp, going on, pays its open stop", .test = test_one_tile_open_stop}, + .expectation = {.description = "goes round to the wall rather than stop one step off a warp's arrival and turn onward"}, + }, + { + .input = {.name = "a one-tile leg off a warp and straight back pays no open stop", .test = test_one_tile_off_warp_and_back}, + .expectation = {.description = "steps off a teleporter's arrival and back on rather than to a wall and back"}, }, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h index bee769a8b2..0d51c98791 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden16.h @@ -36,15 +36,15 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_16[] = { "16.02 walk south 1 (to wall) -> SilphCo_5F (15, 8)", "16.02 walk west 5 -> SilphCo_5F (10, 8)", "16.02 walk south 12 (warp) -> SilphCo_9F (22, 18)", - "16.02 walk west 4 (to wall) -> SilphCo_9F (18, 18)", - "16.02 walk east 4 (warp) -> SilphCo_5F (10, 20)", + "16.02 walk west 1 -> SilphCo_9F (21, 18)", + "16.02 walk east 1 (warp) -> SilphCo_5F (10, 20)", "16.02 walk east 2 (to wall) -> SilphCo_5F (12, 20)", "16.03 to_3f_trainer from SilphCo_5F (21, 21) facing east to SilphCo_3F (22, 9) facing south", "16.03 walk west 9 -> SilphCo_5F (12, 21)", "16.03 walk north 1 (to wall) -> SilphCo_5F (12, 20)", "16.03 walk west 2 (warp) -> SilphCo_9F (22, 18)", - "16.03 walk west 4 (to wall) -> SilphCo_9F (18, 18)", - "16.03 walk east 4 (warp) -> SilphCo_5F (10, 20)", + "16.03 walk west 1 -> SilphCo_9F (21, 18)", + "16.03 walk east 1 (warp) -> SilphCo_5F (10, 20)", "16.03 walk north 12 -> SilphCo_5F (10, 8)", "16.03 walk east 5 -> SilphCo_5F (15, 8)", "16.03 walk north 1 (warp) -> SilphCo_3F (4, 6)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h index 4ebda27e66..5ae468da63 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h @@ -187,9 +187,9 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_19[] = { "19.04c walk south 2 -> VictoryRoad_3F (10, 4) [wild]", "19.04c walk west 5 (to wall) -> VictoryRoad_3F (5, 4) [wild]", "19.04c walk north 2 (warp) -> VictoryRoad_2F (3, 3) [wild]", - "19.04c walk south 2 (to wall) -> VictoryRoad_2F (3, 5) [wild]", - "19.04c walk north 2 (warp) -> VictoryRoad_3F (5, 2) [wild]", - "19.04c behind_boulder_4 from VictoryRoad_3F (5, 2) facing north to VictoryRoad_3F (32, 6)", + "19.04c walk east 1 -> VictoryRoad_2F (4, 3) [wild]", + "19.04c walk west 1 (warp) -> VictoryRoad_3F (5, 2)", + "19.04c behind_boulder_4 from VictoryRoad_3F (5, 2) facing west to VictoryRoad_3F (32, 6)", "19.04c walk east 1 (to wall) -> VictoryRoad_3F (6, 2) [wild]", "19.04c walk south 5 (to wall) -> VictoryRoad_3F (6, 7) [wild]", "19.04c walk east 1 (to wall) -> VictoryRoad_3F (7, 7)",