From e2f4e3a35487fbad583f99a548f6b99b43ea0e37 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 03:54:15 -0800 Subject: [PATCH 1/2] Robust leg planner: held open legs and per_open_wild_tile (off by default) RobustLegCosts::hold_open_legs plans open legs that may end off as holds (RobustLeg::held) that pay no per_open_tile; a held leg whose possible ends past the shortest enter tall grass, a wander place or hop a ledge is not used, and held legs are never joined. per_open_wild_tile adds to each wild tile an open leg enters. open_leg() now steps each place once for all its lengths (step_prefixes). Same code as on bdsp/start-certainty. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Navigation/Pokemon_RobustLegPlanner.h | 109 ++++++++++++++++-- .../Pokemon_RobustLegPlanner_Tests.cpp | 58 +++++++++- 2 files changed, 156 insertions(+), 11 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner.h index 6c527a19fb..a6fb75f8d0 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner.h @@ -62,6 +62,10 @@ struct RobustLeg{ size_t tiles = 0; // tiles travelled; a ledge hop counts 2 bool to_wall = false; std::vector after; + // An open leg held for its tiles' time instead of tapped a tile at a + // time (RobustLegCosts::hold_open_legs). A held leg is never joined + // with the next one the same way: each is its own hold. + bool held = false; }; @@ -82,6 +86,16 @@ struct RobustLegCosts{ int exact_open_tiles = 0; // Extra per tile of an open leg: its timing error grows with its length. int per_open_tile = 0; + // Open legs that may end off (longer than exact_open_tiles) are held + // for their time (RobustLeg::held), not tapped, and pay no + // per_open_tile: no such leg is planned whose possible ends past its + // shortest enter a wild or wander place or hop a ledge, so a long or + // short hold meets nothing a tap would not have. + bool hold_open_legs = false; + // Extra per wild place an open leg enters, on top of per_wild_tile: a + // wild battle loses an open leg's count, while a leg to a wall just + // walks to the wall again. + int per_open_wild_tile = 0; int max_open_tiles = 12; // longest open leg to consider size_t max_spread = 5; // most places the player may be on after a leg // Most sets of places to search before giving up. The search is over @@ -196,6 +210,57 @@ std::optional> step_tiles(const Model& model, typ return stepped; } +// step_tiles() for each count from 1 to `most`, in one walk: [n - 1] is +// step_tiles(model, from, direction, n). +template +std::vector>> step_prefixes( + const Model& model, typename Model::State from, Direction direction, int most +){ + std::vector>> out; + Stepped stepped{from}; + int travelled = 0; + bool in_wander = model.wander(from); + while ((int)out.size() < most){ + const auto next = model.step(from, direction); + if (!next){ + stepped.end = from; + std::optional> last; + if (model.held_at(from, direction)){ + last = stepped; + } + out.resize((size_t)most, last); + break; + } + travelled += (int)model.step_tiles(from, *next); + stepped.wild += model.wild(*next) ? 1 : 0; + const bool next_wander = model.wander(*next); + if (next_wander){ + stepped.wander++; + stepped.along_wander |= in_wander; + } + in_wander = next_wander; + from = *next; + stepped.end = from; + while ((int)out.size() < std::min(travelled, most)){ + out.push_back(stepped); + } + } + return out; +} + +// Whether a held leg's possible ends, `ends` in order from the shortest, +// meet nothing past the shortest that a tap would not: no wild or wander +// place, no ledge hop. +template +bool held_ends_safe(const Model& model, const std::vector& ends){ + for (size_t i = 1; i < ends.size(); i++){ + if (model.step_tiles(ends[i - 1], ends[i]) > 1 || model.wild(ends[i]) || model.wander(ends[i])){ + return false; + } + } + return true; +} + // Sorts a spread and drops repeated places, so equal sets compare equal. template void normalize(std::vector& spread){ @@ -216,6 +281,9 @@ template struct Expansion{ long long cost = 0; RobustLeg leg; + // A held leg that may end somewhere unsafe (held_ends_safe()): not to + // be used, though a longer one may be. + bool unsafe = false; }; // To the wall: every place walks until stopped. Nothing unless each is @@ -255,40 +323,58 @@ std::optional> to_wall_leg( } // Open: about `tiles` tiles, give or take the error. Nothing if any -// possible end is stopped where the player is not held. +// possible end is stopped where the player is not held. A held leg whose +// ends may be unsafe is marked so (Expansion::unsafe). template std::optional> open_leg( const Model& model, const std::vector& spread, Direction direction, int tiles, const RobustLegCosts& costs ){ - Expansion expansion; + using State = typename Model::State; + Expansion expansion; int wild = 0; int wander = 0; const int error = open_leg_error(tiles, costs); + const bool held = costs.hold_open_legs && error > 0; for (const auto& place : spread){ + std::vector ends; + const auto prefixes = step_prefixes(model, place, direction, tiles + error); for (int n = std::max(1, tiles - error); n <= tiles + error; n++){ - const auto stepped = step_tiles(model, place, direction, n); + const auto& stepped = prefixes[(size_t)n - 1]; if (!stepped || stepped->along_wander){ return std::nullopt; } wild = std::max(wild, stepped->wild); wander = std::max(wander, stepped->wander); - expansion.leg.after.push_back(stepped->end); + ends.push_back(stepped->end); } + if (held && !held_ends_safe(model, ends)){ + expansion.unsafe = true; + } + expansion.leg.after.insert(expansion.leg.after.end(), ends.begin(), ends.end()); } + const int per_open_tile = held ? 0 : costs.per_open_tile; expansion.cost = (long long)costs.per_leg + costs.leg_not_at_wall - + (long long)(costs.per_tile + costs.per_open_tile) * tiles - + (long long)costs.per_wild_tile * wild + (long long)costs.per_wander_tile * wander; + + (long long)(costs.per_tile + per_open_tile) * tiles + + (long long)(costs.per_wild_tile + costs.per_open_wild_tile) * wild + (long long)costs.per_wander_tile * wander; expansion.leg.direction = direction; expansion.leg.tiles = (size_t)tiles; + expansion.leg.held = held; return expansion; } +// Whether `next` walks on as one hold with `prev`: both open and tapped, +// the same way. +template +bool joins(const RobustLeg& prev, const RobustLeg& next){ + return prev.direction == next.direction && !prev.to_wall && !prev.held && !next.held; +} + // Joins an open leg with the next one the same way: they are one hold. template std::vector> consolidate(const std::vector>& legs){ std::vector> consolidated; for (const RobustLeg& leg : legs){ - if (!consolidated.empty() && consolidated.back().direction == leg.direction && !consolidated.back().to_wall){ + if (!consolidated.empty() && joins(consolidated.back(), leg)){ consolidated.back().tiles += leg.tiles; consolidated.back().to_wall = leg.to_wall; consolidated.back().after = leg.after; @@ -355,12 +441,15 @@ class Search{ if (auto wall = to_wall_leg(m_model, spread, direction, m_costs)){ relax(spread, cost, std::move(*wall)); } + // A held leg unsafe at one length may be safe at a longer one. for (int tiles = 1; tiles <= m_costs.max_open_tiles; tiles++){ auto open = open_leg(m_model, spread, direction, tiles, m_costs); if (!open){ break; } - relax(spread, cost, std::move(*open)); + if (!open->unsafe){ + relax(spread, cost, std::move(*open)); + } } } } @@ -375,7 +464,7 @@ class Search{ } if (spread != m_start){ const RobustLeg& prev = m_nodes.at(spread).leg; - if (expansion.leg.direction == prev.direction && !prev.to_wall){ + if (joins(prev, expansion.leg)){ expansion.cost -= m_costs.per_leg; } } @@ -443,7 +532,7 @@ long long robust_legs_cost( if (expansion){ cost = expansion->cost; } - if (previous != nullptr && previous->direction == leg.direction && !previous->to_wall){ + if (previous != nullptr && RobustLegPlannerInternal::joins(*previous, leg)){ cost -= costs.per_leg; } total += cost; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp index 6333649e2c..65eca83138 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp @@ -94,7 +94,8 @@ std::string describe(const std::optional& legs){ } std::string text; for (const RobustLeg& leg : *legs){ - text += std::format("{}{} {} {}:", text.empty() ? "" : "; ", direction_name(leg.direction), leg.tiles, leg.to_wall ? "to wall" : "open"); + text += std::format("{}{} {} {}:", text.empty() ? "" : "; ", direction_name(leg.direction), leg.tiles, + leg.to_wall ? "to wall" : leg.held ? "held" : "open"); for (const TilePos& pos : leg.after){ text += " " + to_string(pos); } @@ -103,6 +104,8 @@ std::string describe(const std::optional& legs){ } constexpr RobustLegCosts EXACT{.exact_open_tiles = 12}; +// Open legs held for their time, a tile short or long at any length. +constexpr RobustLegCosts HELD{.open_error = 1, .open_error_per = 0, .hold_open_legs = true}; struct PlanInput{ @@ -228,6 +231,26 @@ const PlanCase PLAN_CASES[] = { .input = {.name = "a walk that is never stopped is not a wall", .rows = {"...."}, .wraps = true, .from = {{0, 0}}, .goal = {2, 0}, .costs = EXACT}, .expectation = {.legs = "east 2 open: (2, 0)"}, }, + { + .input = {.name = "an open leg that may end off is held", .rows = {".....", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = HELD}, + .expectation = {.legs = "east 1 held: (1, 0) (2, 0); south 1 to wall: (1, 1) (2, 1); east 2 to wall: (3, 1)"}, + }, + { + .input = {.name = "a held leg is refused when its long end lands in tall grass", .rows = {"..\"..", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = HELD}, + .expectation = {.legs = "TBD"}, + }, + { + .input = {.name = "a held leg is refused when its long end lands where someone wanders", .rows = {"..w..", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = HELD}, + .expectation = {.legs = "TBD"}, + }, + { + .input = {.name = "a held leg is refused when its long end hops a ledge", .rows = {"..", "..", "v.", "..", "#."}, .from = {{0, 0}}, .goal = {0, 3}, .costs = HELD}, + .expectation = {.legs = "TBD"}, + }, + { + .input = {.name = "open legs are tapped while hold_open_legs is off", .rows = {".....", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = {.open_error = 1, .open_error_per = 0}}, + .expectation = {.legs = "TBD"}, + }, }; @@ -290,6 +313,39 @@ const CostCase COST_CASES[] = { }, .expectation = {.cost = 8 + 50 + 3}, }, + { + .input = { + .name = "a held leg pays no per_open_tile", .rows = {"........"}, .from = {0, 0}, + .legs = {{Direction::EAST, 3, false, {{2, 0}, {3, 0}, {4, 0}}, true}}, + .costs = {.open_error = 1, .open_error_per = 0, .per_open_tile = 2, .hold_open_legs = true}, + }, + .expectation = {.cost = 8 + 50 + 3}, + }, + { + .input = { + .name = "held legs the same way are not joined", .rows = {"........"}, .from = {0, 0}, + .legs = { + {Direction::EAST, 2, false, {{1, 0}, {2, 0}, {3, 0}}, true}, + {Direction::EAST, 2, false, {{2, 0}, {3, 0}, {4, 0}, {5, 0}}, true}, + }, + .costs = HELD, + }, + .expectation = {.cost = 2 * (8 + 50 + 2)}, + }, + { + .input = { + .name = "each grass tile of an open leg costs per_open_wild_tile more", .rows = {".\"\"...."}, .from = {0, 0}, + .legs = {{Direction::EAST, 3, false, {{3, 0}}}}, .costs = {.exact_open_tiles = 12, .per_open_wild_tile = 10}, + }, + .expectation = {.cost = 8 + 50 + 3 + 2 * (4 + 10)}, + }, + { + .input = { + .name = "per_open_wild_tile leaves a wall leg's grass alone", .rows = {".\"\"#"}, .from = {0, 0}, + .legs = {{Direction::EAST, 2, true, {{2, 0}}}}, .costs = {.per_open_wild_tile = 10}, + }, + .expectation = {.cost = 8 + 2 + 2 * 4}, + }, }; } // namespace From 6bf2fece9319f1fcaf3f5b7028810615ae48afde Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 03:55:57 -0800 Subject: [PATCH 2/2] Robust leg planner tests: held legs refused at grass, wander and ledge ends; held costs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Navigation/Pokemon_RobustLegPlanner_Tests.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp index 65eca83138..b9866592b5 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RobustLegPlanner_Tests.cpp @@ -237,19 +237,23 @@ const PlanCase PLAN_CASES[] = { }, { .input = {.name = "a held leg is refused when its long end lands in tall grass", .rows = {"..\"..", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = HELD}, - .expectation = {.legs = "TBD"}, + .expectation = {.legs = "none"}, }, { .input = {.name = "a held leg is refused when its long end lands where someone wanders", .rows = {"..w..", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = HELD}, - .expectation = {.legs = "TBD"}, + .expectation = {.legs = "none"}, + }, + { + .input = {.name = "a held leg south lines up a wall leg", .rows = {".##", "...", "...", ".##", ".##"}, .from = {{0, 0}}, .goal = {2, 2}, .costs = HELD}, + .expectation = {.legs = "south 1 held: (0, 1) (0, 2); east 2 to wall: (2, 1) (2, 2); south 1 to wall: (2, 2)"}, }, { - .input = {.name = "a held leg is refused when its long end hops a ledge", .rows = {"..", "..", "v.", "..", "#."}, .from = {{0, 0}}, .goal = {0, 3}, .costs = HELD}, - .expectation = {.legs = "TBD"}, + .input = {.name = "a held leg is refused when its long end hops a ledge", .rows = {".##", "...", "v..", ".##", ".##"}, .from = {{0, 0}}, .goal = {2, 2}, .costs = HELD}, + .expectation = {.legs = "none"}, }, { .input = {.name = "open legs are tapped while hold_open_legs is off", .rows = {".....", "#...#"}, .from = {{0, 0}}, .goal = {3, 1}, .costs = {.open_error = 1, .open_error_per = 0}}, - .expectation = {.legs = "TBD"}, + .expectation = {.legs = "east 1 open: (1, 0) (2, 0); south 1 to wall: (1, 1) (2, 1); east 2 to wall: (3, 1)"}, }, };