Grid of cells that can be walked or not, each with a cost, and the shortest path between two of them.
More...
#include <aegis2d/ecs/pathfinding.hpp>
|
| | NavGrid (int columns, int rows, Vec2 cell_size, Vec2 origin={}) |
| | Creates a grid where every cell is walkable with a cost of 1.
|
| |
| void | set_walkable (Cell cell, bool walkable) |
| | Makes a cell walkable or blocked; its cost is kept.
|
| |
| void | set_cost (Cell cell, float cost) |
| | Sets the cost of entering a cell.
|
| |
| void | mark_tiles (std::span< const std::uint32_t > tiles, Painted painted) |
| | Sets every cell walkable or blocked from a tile layer.
|
| |
| bool | walkable (Cell cell) const |
| | Tells whether a cell can be walked.
|
| |
| float | cost (Cell cell) const |
| | Cost of entering a cell.
|
| |
| bool | contains (Cell cell) const noexcept |
| | Tells whether a cell is in the grid.
|
| |
| std::optional< Cell > | cell_at (Vec2 position) const noexcept |
| | Cell under a position.
|
| |
| Vec2 | center (Cell cell) const noexcept |
| | Center of a cell.
|
| |
| std::vector< Vec2 > | find_path (Cell from, Cell to) const |
| | Shortest path between two cells.
|
| |
| std::uint64_t | version () const noexcept |
| | Number of changes made to the grid.
|
| |
| int | columns () const noexcept |
| | Number of columns.
|
| |
| int | rows () const noexcept |
| | Number of rows.
|
| |
|
| static constexpr std::size_t | max_cells = std::size_t{1} << 20U |
| | Most cells a grid can have (one million).
|
| |
Grid of cells that can be walked or not, each with a cost, and the shortest path between two of them.
Every cell starts walkable with a cost of 1. A path goes from a cell to one of its four neighbours (no diagonals) and costs the sum of the costs of the cells it enters, so a cell of cost 3 (mud, a slow road) is worth a detour of up to two cells. find_path() runs A* with the Manhattan distance, which never overestimates since every cost is at least 1: the path it returns is a cheapest one.
The walkable cells come from a Tiled layer or from code, and change during the game (a tower built on a cell); version() tells when, so a NavPath is found again only then:
Vec2{map.tile_size.width, map.tile_size.height}};
path.update(grid);
grid.set_walkable(cell, false);
Grid of cells that can be walked or not, each with a cost, and the shortest path between two of them.
Definition pathfinding.hpp:55
void mark_tiles(std::span< const std::uint32_t > tiles, Painted painted)
Sets every cell walkable or blocked from a tile layer.
std::filesystem::path asset_path(const std::filesystem::path &relative)
Path of one asset.
@ Walkable
Painted cells can be walked, empty ones cannot (a layer that draws the path).
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Path between two cells, found again only when the grid has changed.
Definition pathfinding.hpp:156
Cell from
Start cell.
Definition pathfinding.hpp:158
static TileMap load(const std::filesystem::path &path)
Reads a map and its external tilesets.
◆ NavGrid()
| aegis2d::ecs::NavGrid::NavGrid |
( |
int |
columns, |
|
|
int |
rows, |
|
|
Vec2 |
cell_size, |
|
|
Vec2 |
origin = {} |
|
) |
| |
Creates a grid where every cell is walkable with a cost of 1.
- Parameters
-
| columns | Number of columns, above 0. |
| rows | Number of rows, above 0. |
| cell_size | Size of a cell in logical pixels, above 0, such as the map's tile size. |
| origin | Position of the top-left corner of the grid. |
- Exceptions
-
| std::invalid_argument | If a dimension or cell_size is not positive, or the grid would have more than max_cells cells. |
◆ cell_at()
| std::optional< Cell > aegis2d::ecs::NavGrid::cell_at |
( |
Vec2 |
position | ) |
const |
|
noexcept |
Cell under a position.
- Parameters
-
| position | Position in logical pixels, such as the mouse in the world. |
- Returns
- The cell, or nothing outside the grid.
◆ center()
| Vec2 aegis2d::ecs::NavGrid::center |
( |
Cell |
cell | ) |
const |
|
noexcept |
Center of a cell.
- Parameters
-
| cell | Any cell, even outside the grid. |
- Returns
- Its center in logical pixels.
◆ columns()
| int aegis2d::ecs::NavGrid::columns |
( |
| ) |
const |
|
noexcept |
Number of columns.
- Returns
- The value given to the constructor.
◆ contains()
| bool aegis2d::ecs::NavGrid::contains |
( |
Cell |
cell | ) |
const |
|
noexcept |
Tells whether a cell is in the grid.
- Parameters
-
- Returns
true if its column and row are in range.
◆ cost()
| float aegis2d::ecs::NavGrid::cost |
( |
Cell |
cell | ) |
const |
Cost of entering a cell.
- Parameters
-
- Returns
- The cost, 1 unless set_cost() changed it.
- Exceptions
-
| std::out_of_range | If the cell is outside the grid. |
◆ find_path()
| std::vector< Vec2 > aegis2d::ecs::NavGrid::find_path |
( |
Cell |
from, |
|
|
Cell |
to |
|
) |
| const |
Shortest path between two cells.
- Parameters
-
| from | Start cell. |
| to | End cell. |
- Returns
- The centers of the cells of a cheapest path,
from and to included (one point when they are the same); empty when either is blocked or outside the grid, or when no path joins them.
◆ mark_tiles()
| void aegis2d::ecs::NavGrid::mark_tiles |
( |
std::span< const std::uint32_t > |
tiles, |
|
|
Painted |
painted |
|
) |
| |
Sets every cell walkable or blocked from a tile layer.
- Parameters
-
| tiles | Global tile id of each cell, row by row from the top-left, 0 for an empty one: render::MapLayer::tiles. |
| painted | Whether the painted cells are the walkable or the blocked ones. |
- Exceptions
-
| std::invalid_argument | If tiles does not have one id per cell. |
◆ rows()
| int aegis2d::ecs::NavGrid::rows |
( |
| ) |
const |
|
noexcept |
Number of rows.
- Returns
- The value given to the constructor.
◆ set_cost()
| void aegis2d::ecs::NavGrid::set_cost |
( |
Cell |
cell, |
|
|
float |
cost |
|
) |
| |
Sets the cost of entering a cell.
- Parameters
-
| cell | Cell of the grid. |
| cost | At least 1; 1 is a plain cell. |
- Exceptions
-
| std::out_of_range | If the cell is outside the grid. |
| std::invalid_argument | If cost is below 1 or not finite. |
◆ set_walkable()
| void aegis2d::ecs::NavGrid::set_walkable |
( |
Cell |
cell, |
|
|
bool |
walkable |
|
) |
| |
Makes a cell walkable or blocked; its cost is kept.
- Parameters
-
| cell | Cell of the grid. |
| walkable | true to let paths go through it. |
- Exceptions
-
| std::out_of_range | If the cell is outside the grid. |
◆ version()
| std::uint64_t aegis2d::ecs::NavGrid::version |
( |
| ) |
const |
|
noexcept |
Number of changes made to the grid.
Goes up each time set_walkable(), set_cost() or mark_tiles() changes a cell, never when they leave it as it was; starts at 1.
- Returns
- The current version.
◆ walkable()
| bool aegis2d::ecs::NavGrid::walkable |
( |
Cell |
cell | ) |
const |
Tells whether a cell can be walked.
- Parameters
-
- Returns
false for a blocked cell or one outside the grid.
◆ max_cells
| constexpr std::size_t aegis2d::ecs::NavGrid::max_cells = std::size_t{1} << 20U |
|
staticconstexpr |
Most cells a grid can have (one million).
The documentation for this class was generated from the following file: