Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
pathfinding.hpp
Go to the documentation of this file.
1
4#pragma once
6#include <aegis2d/ecs/export.hpp>
7
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <span>
12#include <vector>
13
14namespace aegis2d::ecs {
15
17struct Cell {
19 int x = 0;
21 int y = 0;
22
24 friend constexpr bool operator==(Cell, Cell) noexcept = default;
25};
26
28enum class Painted : std::uint8_t {
32 Blocked,
33};
34
55class AEGIS2D_ECS_EXPORT NavGrid {
56public:
65 NavGrid(int columns, int rows, Vec2 cell_size, Vec2 origin = {});
66
68 static constexpr std::size_t max_cells = std::size_t{1} << 20U;
69
74 void set_walkable(Cell cell, bool walkable);
75
81 void set_cost(Cell cell, float cost);
82
88 void mark_tiles(std::span<const std::uint32_t> tiles, Painted painted);
89
93 [[nodiscard]] bool walkable(Cell cell) const;
94
99 [[nodiscard]] float cost(Cell cell) const;
100
104 [[nodiscard]] bool contains(Cell cell) const noexcept;
105
109 [[nodiscard]] std::optional<Cell> cell_at(Vec2 position) const noexcept;
110
114 [[nodiscard]] Vec2 center(Cell cell) const noexcept;
115
122 [[nodiscard]] std::vector<Vec2> find_path(Cell from, Cell to) const;
123
128 [[nodiscard]] std::uint64_t version() const noexcept;
129
132 [[nodiscard]] int columns() const noexcept;
133
136 [[nodiscard]] int rows() const noexcept;
137
138private:
139 [[nodiscard]] std::size_t index_of(Cell cell) const;
140 [[nodiscard]] std::size_t slot(Cell cell) const noexcept;
141 [[nodiscard]] Cell cell_of(std::size_t slot) const noexcept;
142
143 int columns_;
144 int rows_;
145 Vec2 cell_size_;
146 Vec2 origin_;
147 std::uint64_t version_ = 1;
148 std::vector<std::uint8_t> walkable_;
149 std::vector<float> costs_;
150};
151
156struct NavPath {
158 Cell from{};
160 Cell to{};
162 std::vector<Vec2> points{};
164 std::uint64_t version = 0;
165
169 bool update(const NavGrid& grid) {
170 if (version == grid.version()) {
171 return false;
172 }
173 points = grid.find_path(from, to);
174 version = grid.version();
175 return true;
176 }
177};
178
179} // namespace aegis2d::ecs
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 set_walkable(Cell cell, bool walkable)
Makes a cell walkable or blocked; its cost is kept.
std::uint64_t version() const noexcept
Number of changes made to the grid.
float cost(Cell cell) const
Cost of entering a cell.
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.
NavGrid(int columns, int rows, Vec2 cell_size, Vec2 origin={})
Creates a grid where every cell is walkable with a cost of 1.
std::optional< Cell > cell_at(Vec2 position) const noexcept
Cell under a position.
bool contains(Cell cell) const noexcept
Tells whether a cell is in the grid.
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.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
Painted
What the painted cells of a tile layer are, for NavGrid::mark_tiles().
Definition pathfinding.hpp:28
@ Blocked
Painted cells are blocked, empty ones can be walked (a layer of walls).
@ 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
Definition endpoint.hpp:85
Column and row of a grid cell, from the top-left one.
Definition pathfinding.hpp:17
int x
Column, from 0 on the left.
Definition pathfinding.hpp:19
friend constexpr bool operator==(Cell, Cell) noexcept=default
Compares the cells.
int y
Row, from 0 at the top.
Definition pathfinding.hpp:21
Path between two cells, found again only when the grid has changed.
Definition pathfinding.hpp:156
bool update(const NavGrid &grid)
Finds the path again if the grid changed since the last time.
Definition pathfinding.hpp:169