Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::NavGrid Class Reference

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>

Public Member Functions

 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 Public Attributes

static constexpr std::size_t max_cells = std::size_t{1} << 20U
 Most cells a grid can have (one million).
 

Detailed Description

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:

const auto map = render::TileMap::load(core::asset_path("maps/level.tmj"));
ecs::NavGrid grid{map.width, map.height,
Vec2{map.tile_size.width, map.tile_size.height}};
grid.mark_tiles(map.layer("path")->tiles, ecs::Painted::Walkable);
ecs::NavPath path{.from = entrance, .to = exit};
path.update(grid); // every tick: finds the path again if a cell changed
grid.set_walkable(cell, false); // a tower is built: the next update() goes around it
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.

Constructor & Destructor Documentation

◆ 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
columnsNumber of columns, above 0.
rowsNumber of rows, above 0.
cell_sizeSize of a cell in logical pixels, above 0, such as the map's tile size.
originPosition of the top-left corner of the grid.
Exceptions
std::invalid_argumentIf a dimension or cell_size is not positive, or the grid would have more than max_cells cells.

Member Function Documentation

◆ cell_at()

std::optional< Cell > aegis2d::ecs::NavGrid::cell_at ( Vec2  position) const
noexcept

Cell under a position.

Parameters
positionPosition 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
cellAny 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
cellAny cell.
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
cellCell of the grid.
Returns
The cost, 1 unless set_cost() changed it.
Exceptions
std::out_of_rangeIf 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
fromStart cell.
toEnd 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
tilesGlobal tile id of each cell, row by row from the top-left, 0 for an empty one: render::MapLayer::tiles.
paintedWhether the painted cells are the walkable or the blocked ones.
Exceptions
std::invalid_argumentIf 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
cellCell of the grid.
costAt least 1; 1 is a plain cell.
Exceptions
std::out_of_rangeIf the cell is outside the grid.
std::invalid_argumentIf 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
cellCell of the grid.
walkabletrue to let paths go through it.
Exceptions
std::out_of_rangeIf 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
cellAny cell.
Returns
false for a blocked cell or one outside the grid.

Member Data Documentation

◆ 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: