33#ifndef HORIZON_ZONE_GAME_MAP_PATH_ASTAR_HPP
34#define HORIZON_ZONE_GAME_MAP_PATH_ASTAR_HPP
57 auto delta =
getDelta(source_, target_);
58 return static_cast<uint32_t
>(10 * (delta.x() + delta.y()));
63 auto delta =
getDelta(source_, target_);
64 return static_cast<uint32_t
>(10 * std::sqrt(std::pow(delta.x(), 2) + std::pow(delta.y(), 2)));
69 auto delta =
getDelta(source_, target_);
70 return 10 * (delta.x() + delta.y()) + (-6) * std::min(delta.x(), delta.y());
86 explicit Node(
MapCoords coord_, std::shared_ptr<Node> parent_ =
nullptr)
96using NodeSet = std::vector<std::shared_ptr<Node>>;
102 for (
auto node : nodes_) {
103 if (node->coordinates == coordinates_) {
111 for (
auto it = nodes_.begin(); it != nodes_.end();) {
112 it = nodes_.erase(it);
137 void setHeuristic(
const HeuristicFunction& heuristic_) {
heuristic = [heuristic_](
auto && PH1,
auto && PH2) {
return heuristic_(std::forward<
decltype(PH1)>(PH1), std::forward<
decltype(PH2)>(PH2)); }; }
142 std::shared_ptr<Node> current =
nullptr;
147 closedSet.reserve(20);
148 openSet.push_back(std::make_shared<Node>(source_));
154 auto current_it = openSet.begin();
156 current = *current_it;
158 for (
auto it = openSet.begin(); it != openSet.end(); it++) {
160 if (node->getFScore() <= current->getFScore()) {
166 if (current->coordinates == target_)
169 closedSet.push_back(current);
170 openSet.erase(current_it);
181 uint32_t totalCost = current->G + newCoordinates.
move_cost();
183 std::shared_ptr<Node> successor =
findNodeOnList(openSet, newCoordinates);
184 if (successor ==
nullptr) {
185 successor = std::make_shared<Node>(newCoordinates, current);
186 successor->G = totalCost;
187 successor->H =
heuristic(successor->coordinates, target_);
188 openSet.push_back(successor);
189 }
else if (totalCost < successor->G) {
190 successor->parent = current;
191 successor->G = totalCost;
198 while (current !=
nullptr) {
199 path.push_back(current->coordinates);
200 current = current->parent;
213 { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
214 { -1, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 }
Coordinates< MAX_CELLS_PER_MAP > MapCoords
Definition: GridDefinitions.hpp:83
#define MAX_VIEW_RANGE
Definition: Horizon.hpp:59
directions
Definition: UnitDefinitions.hpp:75
int16_t y() const
Definition: Coordinates.hpp:120
int16_t move_cost() const
Definition: Coordinates.hpp:121
int16_t x() const
Definition: Coordinates.hpp:119
void set_move_cost(int16_t move_cost)
Definition: Coordinates.hpp:122
CoordinateList direction
Definition: AStar.hpp:212
static void releaseNodes(NodeSet nodes_)
Definition: AStar.hpp:109
void setDiagonalMovement(bool enable_)
Definition: AStar.hpp:135
Generator()
Definition: AStar.hpp:117
void setCollisionDetectionFunction(CollisionDetectionFunction c)
Definition: AStar.hpp:133
CollisionDetectionFunction check_collision
Definition: AStar.hpp:211
Generator(MapCoords worldSize_, CollisionDetectionFunction c, bool diagonal_movement=true, HeuristicFunction h=&Heuristic::manhattan)
Definition: AStar.hpp:123
void setHeuristic(const HeuristicFunction &heuristic_)
Definition: AStar.hpp:137
MapCoords worldSize
Definition: AStar.hpp:216
CoordinateList findPath(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:139
uint32_t directions
Definition: AStar.hpp:217
static std::shared_ptr< Node > findNodeOnList(NodeSet &nodes_, MapCoords coordinates_)
Definition: AStar.hpp:100
void setWorldSize(MapCoords worldSize_)
Definition: AStar.hpp:131
HeuristicFunction heuristic
Definition: AStar.hpp:210
static uint32_t manhattan(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:55
static uint32_t octagonal(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:67
static MapCoords getDelta(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:52
static uint32_t euclidean(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:61
std::function< uint32_t(MapCoords, MapCoords)> HeuristicFunction
Definition: AStar.hpp:74
std::vector< std::shared_ptr< Node > > NodeSet
Definition: AStar.hpp:96
std::vector< MapCoords > CoordinateList
We use a vector because the AStar algorithm is only searching on small datasets. Other data structure...
Definition: AStar.hpp:77
std::function< bool(uint16_t x, uint16_t y)> CollisionDetectionFunction
Definition: AStar.hpp:78
Definition: Element.hpp:7
uint32_t G
Definition: AStar.hpp:82
MapCoords coordinates
Definition: AStar.hpp:83
Node(MapCoords coord_, std::shared_ptr< Node > parent_=nullptr)
Definition: AStar.hpp:86
uint32_t H
Definition: AStar.hpp:82
std::shared_ptr< Node > parent
Definition: AStar.hpp:84
uint32_t getFScore() const
Definition: AStar.hpp:93