From 6d41230cfe5be126234965b7b2c69b6b8678eb7d Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 31 Oct 2026 21:34:59 -0700 Subject: [PATCH] BDSP navigation: tile grids and map sets TileGrid and MapSet, taken unchanged from nav/core (#66) at b50d5471a, with the exact short open legs, per-open-tile cost and search cap. Only BDSP's walker and maps use them, so they live on their own branch above the core until FRLG adopts them. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Pokemon/Navigation/Pokemon_MapSet.cpp | 102 ++++ .../Pokemon/Navigation/Pokemon_MapSet.h | 63 +++ .../Pokemon/Navigation/Pokemon_TileGrid.cpp | 503 ++++++++++++++++++ .../Pokemon/Navigation/Pokemon_TileGrid.h | 191 +++++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 5 files changed, 863 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.h diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp new file mode 100644 index 0000000000..533dc4f12f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.cpp @@ -0,0 +1,102 @@ +/* Map Set + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_MapSet.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +void MapSet::add(std::string name, const TileGrid& grid, const TilePos& offset){ + for (const Placed& placed : m_maps){ + if (placed.name == name){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Map placed twice: " + name); + } + // Map images show a border of trees past their edges, so two maps + // may share blocked tiles, but nothing else. + for (int y = 0; y < grid.height(); y++){ + for (int x = 0; x < grid.width(); x++){ + const TilePos other{offset.x + x - placed.offset.x, offset.y + y - placed.offset.y}; + if (!placed.grid.in_bounds(other)){ + continue; + } + if (grid.at({x, y}) != TileType::BLOCKED || placed.grid.at(other) != TileType::BLOCKED){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Map " + name + " overlaps " + placed.name + " at its tile " + to_string(TilePos{x, y}) + "." + ); + } + } + } + } + m_maps.push_back(Placed{std::move(name), grid, offset}); + + // Rebuild the joined grid. + TilePos low = m_maps[0].offset; + TilePos high = low; + for (const Placed& placed : m_maps){ + low.x = std::min(low.x, placed.offset.x); + low.y = std::min(low.y, placed.offset.y); + high.x = std::max(high.x, placed.offset.x + placed.grid.width()); + high.y = std::max(high.y, placed.offset.y + placed.grid.height()); + } + m_origin = low; + std::vector rows(high.y - low.y, std::string(high.x - low.x, tile_char(TileType::UNKNOWN))); + for (const Placed& placed : m_maps){ + const std::vector map_rows = placed.grid.rows(); + for (size_t y = 0; y < map_rows.size(); y++){ + rows[placed.offset.y - low.y + y].replace(placed.offset.x - low.x, map_rows[y].size(), map_rows[y]); + } + } + m_world.emplace(rows); +} + +const TileGrid& MapSet::world() const{ + if (!m_world){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No maps placed."); + } + return *m_world; +} + +TilePos MapSet::to_world(const std::string& map, const TilePos& local) const{ + for (const Placed& placed : m_maps){ + if (placed.name == map){ + return { + placed.offset.x - m_origin.x + local.x, + placed.offset.y - m_origin.y + local.y, + }; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown map: " + map); +} + +std::optional MapSet::to_local(const TilePos& world) const{ + for (const Placed& placed : m_maps){ + const TilePos local{ + world.x + m_origin.x - placed.offset.x, + world.y + m_origin.y - placed.offset.y, + }; + if (placed.grid.in_bounds(local)){ + return MapTile{placed.name, local}; + } + } + return std::nullopt; +} + +std::string MapSet::describe(const TilePos& world) const{ + const std::optional local = to_local(world); + if (!local){ + return "world " + to_string(world); + } + return local->map + " " + to_string(local->pos); +} + + + +} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.h new file mode 100644 index 0000000000..468213c481 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapSet.h @@ -0,0 +1,63 @@ +/* Map Set + * + * From: https://github.com/PokemonAutomation/ + * + * Several tile grids placed side by side on one world grid, so one plan + * can cross from a route into a town. + */ + +#ifndef PokemonAutomation_Pokemon_MapSet_H +#define PokemonAutomation_Pokemon_MapSet_H + +#include +#include +#include +#include "Pokemon_TileGrid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +struct MapTile{ + std::string map; + TilePos pos; +}; + + +class MapSet{ +public: + // Places `grid` with its tile (0,0) at `offset`. Offsets are relative to + // each other and may be negative. Maps may overlap only where both are + // blocked; the map placed first holds those tiles. Throws + // InternalProgramError if the name is taken or the maps conflict. + void add(std::string name, const TileGrid& grid, const TilePos& offset); + + // The maps joined on one grid. Tiles no map covers are UNKNOWN. World + // tile (0,0) is the top-left corner of all the maps together. + const TileGrid& world() const; + + // Throws InternalProgramError for an unknown map name. + TilePos to_world(const std::string& map, const TilePos& local) const; + + // The map that holds a world tile, or nothing between maps. + std::optional to_local(const TilePos& world) const; + + // "Route 201 (45,3)", or the world tile if no map holds it. + std::string describe(const TilePos& world) const; + +private: + struct Placed{ + std::string name; + TileGrid grid; + TilePos offset; + }; + std::vector m_maps; + TilePos m_origin; // offset of world tile (0,0) + std::optional m_world; +}; + + + +} +} +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.cpp new file mode 100644 index 0000000000..74901f69a7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.cpp @@ -0,0 +1,503 @@ +/* Tile Grid + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_LegPlanner.h" +#include "Pokemon_TileGrid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +char tile_char(TileType type){ + switch (type){ + case TileType::BLOCKED: return '#'; + case TileType::WALKABLE: return '.'; + case TileType::TALL_GRASS: return '"'; + case TileType::LEDGE: return 'v'; + case TileType::NPC: return 'N'; + case TileType::SIGN: return 'S'; + case TileType::DOOR: return 'D'; + case TileType::UNKNOWN: return '?'; + } + return '?'; +} + + + + +TileGrid::TileGrid(const std::vector& rows) + : m_width(rows.empty() ? 0 : (int)rows[0].size()) + , m_height((int)rows.size()) +{ + m_tiles.reserve((size_t)m_width * m_height); + for (size_t y = 0; y < rows.size(); y++){ + const std::string& row = rows[y]; + if ((int)row.size() != m_width){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Tile grid row " + std::to_string(y) + " has " + std::to_string(row.size()) + + " tiles, expected " + std::to_string(m_width) + "." + ); + } + for (size_t x = 0; x < row.size(); x++){ + switch (row[x]){ + case '#': m_tiles.push_back(TileType::BLOCKED); break; + case '.': m_tiles.push_back(TileType::WALKABLE); break; + case '"': m_tiles.push_back(TileType::TALL_GRASS); break; + case 'v': m_tiles.push_back(TileType::LEDGE); break; + case 'N': m_tiles.push_back(TileType::NPC); break; + case 'S': m_tiles.push_back(TileType::SIGN); break; + case 'D': m_tiles.push_back(TileType::DOOR); break; + case '?': m_tiles.push_back(TileType::UNKNOWN); break; + default: + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + std::string("Unknown tile '") + row[x] + "' at " + + to_string(TilePos{(int)x, (int)y}) + "." + ); + } + } + } +} + +std::vector TileGrid::rows() const{ + std::vector rows; + for (int y = 0; y < m_height; y++){ + std::string row; + for (int x = 0; x < m_width; x++){ + row += tile_char(at({x, y})); + } + rows.push_back(std::move(row)); + } + return rows; +} + +bool TileGrid::in_bounds(const TilePos& pos) const{ + return 0 <= pos.x && pos.x < m_width && 0 <= pos.y && pos.y < m_height; +} + +TileType TileGrid::at(const TilePos& pos) const{ + if (!in_bounds(pos)){ + return TileType::UNKNOWN; + } + return m_tiles[(size_t)pos.y * m_width + pos.x]; +} + +void TileGrid::set(const TilePos& pos, TileType type){ + if (in_bounds(pos)){ + m_tiles[(size_t)pos.y * m_width + pos.x] = type; + } +} + +bool TileGrid::standable(const TilePos& pos) const{ + const TileType type = at(pos); + return type == TileType::WALKABLE || type == TileType::TALL_GRASS; +} + +std::optional TileGrid::step(const TilePos& from, Direction direction) const{ + const TilePos next = from.next(direction); + if (standable(next)){ + return next; + } + if (at(next) == TileType::LEDGE && direction == Direction::SOUTH){ + const TilePos landing = next.next(direction); + if (standable(landing)){ + return landing; + } + } + return std::nullopt; +} + +bool TileGrid::at_wall(const TilePos& pos, Direction direction) const{ + const TileType next = at(pos.next(direction)); + return next != TileType::DOOR && next != TileType::UNKNOWN && !step(pos, direction).has_value(); +} + + + +WallHit walk_to_wall(const TileGrid& grid, const TilePos& from, Direction direction){ + WallHit hit; + hit.stop = from; + while (true){ + const TilePos before = hit.stop; + const std::optional next = grid.step(before, direction); + if (!next){ + hit.wall = before.next(direction); + hit.anchored = grid.at_wall(before, direction); + return hit; + } + hit.tiles += std::abs(next->x - before.x) + std::abs(next->y - before.y); + hit.stop = *next; + } +} + + + +std::string to_string(const TileLeg& leg){ + return std::string(direction_name(leg.direction)) + " " + std::to_string(leg.tiles) + + (leg.ends_at_wall ? " (to wall)" : " (open)"); +} + + +namespace{ + +// The grid's step rules for the shared leg planner. A ledge hop is one +// press that moves two tiles. +class TileGridModel{ +public: + using State = TilePos; + using StateHash = std::hash; + + TileGridModel(const TileGrid& grid, const TilePlanCosts& costs) + : m_grid(grid) + , m_costs(costs) + {} + + std::optional> press(const State& pos, Direction direction) const{ + const std::optional next = m_grid.step(pos, direction); + if (!next){ + return std::nullopt; + } + Step step; + step.next = *next; + step.facing = direction; + const int tiles = std::abs(next->x - pos.x) + std::abs(next->y - pos.y); + step.extra_cost = m_costs.per_tile * (tiles - 1); + if (m_grid.at(*next) == TileType::TALL_GRASS){ + step.extra_cost += m_costs.per_grass_tile; + } + return step; + } + bool held_at(const State& pos, Direction direction) const{ + return m_grid.at_wall(pos, direction); + } + +private: + const TileGrid& m_grid; + const TilePlanCosts& m_costs; +}; + +} + +std::optional> plan_tile_route( + const TileGrid& grid, + const TilePos& from, const TilePos& to, + const TilePlanCosts& costs +){ + if (!grid.standable(from) || !grid.standable(to)){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Route ends must be standable: " + to_string(from) + " -> " + to_string(to) + "." + ); + } + if (from == to){ + return std::vector(); + } + LegCosts leg_costs; + leg_costs.per_tile = costs.per_tile; + leg_costs.per_turn = costs.per_leg; + leg_costs.per_open_stop = costs.leg_not_at_wall; + const std::optional>> legs = plan_legs( + TileGridModel(grid, costs), from, std::nullopt, + std::function([&](const TilePos& pos){ return pos == to; }), + std::nullopt, leg_costs + ); + if (!legs){ + return std::nullopt; + } + std::vector ret; + for (const Leg& leg : *legs){ + const size_t tiles = std::abs(leg.end.x - leg.start.x) + std::abs(leg.end.y - leg.start.y); + ret.push_back(TileLeg{leg.direction, tiles, leg.to_wall}); + } + return ret; +} + +std::vector sight_tiles(const TileGrid& grid, const TrainerSight& trainer){ + std::vector tiles; + TilePos pos = trainer.pos; + for (int i = 0; i < trainer.range; i++){ + pos = pos.next(trainer.facing); + if (!grid.standable(pos)){ + break; + } + tiles.push_back(pos); + } + return tiles; +} + +std::vector> legs_crossing_sight( + const TileGrid& grid, + const TilePos& from, + const std::vector& legs, + const std::vector& trainers +){ + std::vector> sights; + for (const TrainerSight& trainer : trainers){ + sights.push_back(sight_tiles(grid, trainer)); + } + std::vector> crossings; + TilePos pos = from; + for (const TileLeg& leg : legs){ + const std::vector tiles = trace_legs(grid, pos, {leg}); + std::vector seen_by; + for (size_t t = 0; t < trainers.size(); t++){ + for (size_t i = 1; i < tiles.size(); i++){ + if (std::find(sights[t].begin(), sights[t].end(), tiles[i]) != sights[t].end()){ + seen_by.push_back(trainers[t].name); + break; + } + } + } + crossings.push_back(std::move(seen_by)); + pos = tiles.back(); + } + return crossings; +} + +std::vector trace_legs( + const TileGrid& grid, + const TilePos& from, + const std::vector& legs +){ + std::vector tiles{from}; + for (const TileLeg& leg : legs){ + size_t travelled = 0; + while (travelled < leg.tiles){ + const TilePos pos = tiles.back(); + const std::optional next = grid.step(pos, leg.direction); + if (!next){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Leg " + to_string(leg) + " is stopped at " + to_string(pos) + "." + ); + } + travelled += std::abs(next->x - pos.x) + std::abs(next->y - pos.y); + tiles.push_back(*next); + } + if (travelled != leg.tiles){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Leg " + to_string(leg) + " overshoots on a ledge hop." + ); + } + } + return tiles; +} + + + +std::string to_string(const PlannedLeg& leg){ + std::string out = to_string(leg.leg) + " to"; + for (const TilePos& pos : leg.after){ + out += " " + to_string(pos); + } + return out; +} + +namespace{ + +// Where `from` ends up after `tiles` steps in `direction`, stopping early at +// anything that stops the player. Nothing if a step would leave the map or +// enter a door: the plan cannot tell what happens then. +std::optional step_tiles( + const TileGrid& grid, TilePos from, Direction direction, int tiles, int& grass +){ + int travelled = 0; + while (travelled < tiles){ + const std::optional next = grid.step(from, direction); + if (!next){ + if (!grid.at_wall(from, direction)){ + return std::nullopt; + } + break; + } + travelled += std::abs(next->x - from.x) + std::abs(next->y - from.y); + if (grid.at(*next) == TileType::TALL_GRASS){ + grass++; + } + from = *next; + } + return from; +} + +using Spread = std::vector; + +bool spread_less(const TilePos& a, const TilePos& b){ + return a.y != b.y ? a.y < b.y : a.x < b.x; +} +void normalize(Spread& spread){ + std::sort(spread.begin(), spread.end(), spread_less); + spread.erase(std::unique(spread.begin(), spread.end()), spread.end()); +} +struct SpreadLess{ + bool operator()(const Spread& a, const Spread& b) const{ + return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), spread_less); + } +}; + +} + +std::optional> plan_robust_route( + const TileGrid& grid, + const TilePos& from, const TilePos& to, + const RobustPlanCosts& costs +){ + if (!grid.standable(from) || !grid.standable(to)){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Route ends must be standable: " + to_string(from) + " -> " + to_string(to) + "." + ); + } + return plan_robust_route(grid, std::vector{from}, [&](const TilePos& pos){ return pos == to; }, costs); +} + +std::optional> plan_robust_route( + const TileGrid& grid, + const std::vector& from, const std::function& goal, + const RobustPlanCosts& costs +){ + for (const TilePos& pos : from){ + if (!grid.standable(pos)){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, + "Route starts must be standable: " + to_string(pos) + "." + ); + } + } + // Only the start may be wider than costs.max_spread: every leg must bring + // the player within it. + const size_t max_spread = costs.max_spread; + auto reached = [&](const Spread& spread){ + return std::all_of(spread.begin(), spread.end(), goal); + }; + + struct Node{ + long long cost; + Spread parent; + PlannedLeg leg; + bool done = false; + }; + std::map nodes; + using Entry = std::pair; + auto entry_greater = [](const Entry& a, const Entry& b){ return a.first > b.first; }; + std::priority_queue, decltype(entry_greater)> queue(entry_greater); + + Spread start = from; + normalize(start); + nodes[start] = Node{0, {}, {}}; + queue.emplace(0, start); + + auto relax = [&](const Spread& spread, long long cost, Spread next, PlannedLeg leg){ + normalize(next); + if (next.size() > max_spread || next == spread){ + return; + } + auto iter = nodes.find(next); + if (iter != nodes.end() && iter->second.cost <= cost){ + return; + } + leg.after = next; + nodes[next] = Node{cost, spread, std::move(leg)}; + queue.emplace(cost, std::move(next)); + }; + + size_t searched = 0; + while (!queue.empty()){ + const Entry top = queue.top(); + queue.pop(); + const long long cost = top.first; + const Spread& spread = top.second; + Node& node = nodes[spread]; + if (node.done || node.cost != cost){ + continue; + } + node.done = true; + if (++searched > costs.max_searched){ + return std::nullopt; + } + if (reached(spread)){ + std::vector legs; + for (Spread at = spread; at != start; ){ + const Node& step = nodes[at]; + legs.push_back(step.leg); + at = step.parent; + } + std::reverse(legs.begin(), legs.end()); + return legs; + } + + for (Direction direction : ALL_DIRECTIONS){ + // To the wall: every position walks until stopped. + { + Spread next; + size_t longest = 0; + int grass = 0; + bool ok = true; + for (const TilePos& pos : spread){ + const WallHit hit = walk_to_wall(grid, pos, direction); + if (!hit.anchored){ + ok = false; + break; + } + int leg_grass = 0; + step_tiles(grid, pos, direction, (int)hit.tiles, leg_grass); + grass = std::max(grass, leg_grass); + longest = std::max(longest, hit.tiles); + next.push_back(hit.stop); + } + if (ok && longest > 0){ + const long long step = (long long)costs.per_leg + + (long long)costs.per_tile * longest + (long long)costs.per_grass_tile * grass; + relax(spread, cost + step, std::move(next), PlannedLeg{TileLeg{direction, longest, true}, {}}); + } + } + // Open: about `tiles` tiles, give or take `open_error`. + for (int tiles = 1; tiles <= costs.max_open_tiles; tiles++){ + Spread next; + int grass = 0; + bool ok = true; + for (const TilePos& pos : spread){ + const int error = tiles <= costs.exact_open_tiles ? 0 : costs.open_error + tiles / costs.open_error_per; + for (int n = std::max(1, tiles - error); n <= tiles + error; n++){ + int leg_grass = 0; + const std::optional end = step_tiles(grid, pos, direction, n, leg_grass); + if (!end){ + ok = false; + break; + } + grass = std::max(grass, leg_grass); + next.push_back(*end); + } + if (!ok){ + break; + } + } + if (!ok){ + break; + } + const long long step = (long long)costs.per_leg + costs.leg_not_at_wall + + (long long)(costs.per_tile + costs.per_open_tile) * tiles + (long long)costs.per_grass_tile * grass; + relax(spread, cost + step, std::move(next), PlannedLeg{TileLeg{direction, (size_t)tiles, false}, {}}); + } + } + } + + return std::nullopt; +} + + + +} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.h new file mode 100644 index 0000000000..49837e77e9 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_TileGrid.h @@ -0,0 +1,191 @@ +/* Tile Grid + * + * From: https://github.com/PokemonAutomation/ + * + * An overworld map as a grid of tiles, and a planner that turns a walk from + * one tile to another into straight legs. Timed stick holds drift by a tile; + * a leg that ends against a wall does not, because the player stops there + * however long the stick is held. The planner prefers such legs. + */ + +#ifndef PokemonAutomation_Pokemon_TileGrid_H +#define PokemonAutomation_Pokemon_TileGrid_H + +#include +#include +#include +#include +#include +#include "Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Legend of the ASCII rows a grid is parsed from. +enum class TileType{ + BLOCKED, // '#': trees, walls, items on the ground + WALKABLE, // '.': open ground and paths + TALL_GRASS, // '"': walkable, with wild encounters + LEDGE, // 'v': crossed only southward, hopping to the tile past it + NPC, // 'N': a standing NPC or trainer; blocks like a wall + SIGN, // 'S': a sign; blocks like a wall + DOOR, // 'D': walking into it enters a building + UNKNOWN, // '?': off the map; an exit or ground we have no map of +}; +char tile_char(TileType type); + +class TileGrid{ +public: + // Every row must have the same length and use only the legend's characters. + // Throws InternalProgramError otherwise. + explicit TileGrid(const std::vector& rows); + + // The grid as the ASCII rows it parses from. + std::vector rows() const; + + int width() const{ return m_width; } + int height() const{ return m_height; } + bool in_bounds(const TilePos& pos) const; + + // Out-of-bounds tiles read as UNKNOWN. + TileType at(const TilePos& pos) const; + + // Changes one tile, e.g. to block one found in the way on the console. + // Out-of-bounds tiles are left alone. + void set(const TilePos& pos, TileType type); + + // Whether the player can stand on this tile. + bool standable(const TilePos& pos) const; + + // Where one step from `from` lands, or nothing if the player is stopped. + // A south step onto a ledge hops to the tile past it. + std::optional step(const TilePos& from, Direction direction) const; + + // Whether a step from `pos` is refused by a tile that holds the player + // in place. Doors and unknown tiles are not walls: walking into a door + // enters the building, and unknown ground may be an exit. + bool at_wall(const TilePos& pos, Direction direction) const; + +private: + int m_width = 0; + int m_height = 0; + std::vector m_tiles; +}; + + +// Where the player stops when walking in one direction until stopped. +struct WallHit{ + TilePos stop; + size_t tiles = 0; // tiles travelled; a ledge hop counts 2 + TilePos wall; // the first tile the player could not enter + bool anchored = false; // the player is held at `stop` (see TileGrid::at_wall()) +}; +WallHit walk_to_wall(const TileGrid& grid, const TilePos& from, Direction direction); + + +// A straight run in one direction. +struct TileLeg{ + Direction direction; + size_t tiles = 0; // tiles travelled; a ledge hop counts 2 + bool ends_at_wall = false; // the next tile in `direction` stops the player +}; +std::string to_string(const TileLeg& leg); + +struct TilePlanCosts{ + int per_tile = 1; + int per_grass_tile = 4; // extra for each tall-grass tile entered + int per_leg = 8; + int leg_not_at_wall = 50; // extra for a leg that stops in the open +}; + +// Cheapest legs from `from` to `to`, moving 4 ways, or nothing if `to` +// cannot be reached. Both tiles must be standable. +std::optional> plan_tile_route( + const TileGrid& grid, + const TilePos& from, const TilePos& to, + const TilePlanCosts& costs = TilePlanCosts() +); + +// A trainer sees the tiles in a straight line in front of them, up to +// `range` tiles or the first tile the player cannot stand on. +struct TrainerSight{ + std::string name; + TilePos pos; + Direction facing; + int range = 4; +}; +std::vector sight_tiles(const TileGrid& grid, const TrainerSight& trainer); + +// For each leg, the trainers whose sight line it enters. +std::vector> legs_crossing_sight( + const TileGrid& grid, + const TilePos& from, + const std::vector& legs, + const std::vector& trainers +); + +// The tiles a list of legs visits, starting with `from`. Throws +// InternalProgramError if a leg is stopped before its end. +std::vector trace_legs( + const TileGrid& grid, + const TilePos& from, + const std::vector& legs +); + + +// A leg of a plan that allows for open legs going a tile short or long. A +// leg to a wall is walked until the player stops; an open leg is walked for +// `leg.tiles` tiles' worth of time. `after` lists every tile the player may +// stand on once the leg is done. +struct PlannedLeg{ + TileLeg leg; + std::vector after; +}; +std::string to_string(const PlannedLeg& leg); + +struct RobustPlanCosts : TilePlanCosts{ + // An open leg of n tiles may end open_error + n / open_error_per tiles + // short or long: on the console 7 tiles went 9. + int open_error = 1; + int open_error_per = 5; + // Open legs this many tiles or shorter land exactly: a short hold moves + // whole tiles. 0 keeps every open leg uncertain. + int exact_open_tiles = 0; + // Extra per tile of an open leg: its timing error grows with its length, + // so a short step after a wall beats a long open leg. + int per_open_tile = 0; + int max_open_tiles = 12; // longest open leg to consider + size_t max_spread = 5; // most tiles the player may be on after a leg + // Most sets of possible tiles to search before giving up. The search is + // over sets, not tiles, so it grows far faster than the map: all of + // Jubilife City from six start tiles ran for minutes with no plan + // (r224a, 2026-10-31); 200000 sets took 31 s there, so this gives up + // after about 3. Use town costs in towns (a whole-Jubilife plan then + // takes 1 s) and crop the map to the part a leg needs rather than + // raising this. + size_t max_searched = 20000; +}; + +// Cheapest legs that take the player from any of the tiles in `from` to tiles +// that are all `goal`, even if every open leg ends up to `open_error` tiles +// off: the walls of later legs must bring the possible positions together. +// Legs that would leave the map or enter a door are not used. Nothing if no +// such plan exists, or none is found within `costs.max_searched` sets. +std::optional> plan_robust_route( + const TileGrid& grid, + const std::vector& from, const std::function& goal, + const RobustPlanCosts& costs = RobustPlanCosts() +); +// The same, from one tile to exactly `to`. +std::optional> plan_robust_route( + const TileGrid& grid, + const TilePos& from, const TilePos& to, + const RobustPlanCosts& costs = RobustPlanCosts() +); + + + +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index c650d0494d..ce75683ac0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1438,7 +1438,11 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_Grid.h Source/Pokemon/Navigation/Pokemon_LegPlanner.h Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp + Source/Pokemon/Navigation/Pokemon_MapSet.cpp + Source/Pokemon/Navigation/Pokemon_MapSet.h Source/Pokemon/Navigation/Pokemon_Navigation_Tests.h + Source/Pokemon/Navigation/Pokemon_TileGrid.cpp + Source/Pokemon/Navigation/Pokemon_TileGrid.h Source/Pokemon/Options/Pokemon_BoxSortingTable.cpp Source/Pokemon/Options/Pokemon_BoxSortingTable.h Source/Pokemon/Options/Pokemon_EncounterBotOptions.h