Horizon Official Technical Documentation
Horizon::Zone::AStar::Generator Class Reference

#include <AStar.hpp>

+ Collaboration diagram for Horizon::Zone::AStar::Generator:

Public Member Functions

 Generator ()
 
 Generator (MapCoords worldSize_, CollisionDetectionFunction c, bool diagonal_movement=true, HeuristicFunction h=&Heuristic::manhattan)
 
void setWorldSize (MapCoords worldSize_)
 
void setCollisionDetectionFunction (CollisionDetectionFunction c)
 
void setDiagonalMovement (bool enable_)
 
void setHeuristic (const HeuristicFunction &heuristic_)
 
CoordinateList findPath (MapCoords source_, MapCoords target_)
 

Static Private Member Functions

static std::shared_ptr< NodefindNodeOnList (NodeSet &nodes_, MapCoords coordinates_)
 
static void releaseNodes (NodeSet nodes_)
 

Private Attributes

HeuristicFunction heuristic
 
CollisionDetectionFunction check_collision
 
CoordinateList direction
 
MapCoords worldSize
 
uint32_t directions {0}
 

Constructor & Destructor Documentation

◆ Generator() [1/2]

Horizon::Zone::AStar::Generator::Generator ( )
inline
118 {
121 }
void setDiagonalMovement(bool enable_)
Definition: AStar.hpp:135
void setHeuristic(const HeuristicFunction &heuristic_)
Definition: AStar.hpp:137
static uint32_t manhattan(MapCoords source_, MapCoords target_)
Definition: AStar.hpp:55

References Horizon::Zone::AStar::Heuristic::manhattan(), setDiagonalMovement(), and setHeuristic().

+ Here is the call graph for this function:

◆ Generator() [2/2]

Horizon::Zone::AStar::Generator::Generator ( MapCoords  worldSize_,
CollisionDetectionFunction  c,
bool  diagonal_movement = true,
HeuristicFunction  h = &Heuristic::manhattan 
)
inline
124 {
125 setWorldSize(worldSize_);
126 setCollisionDetectionFunction(std::move(c));
127 setDiagonalMovement(diagonal_movement);
128 setHeuristic(h);
129 }
void setCollisionDetectionFunction(CollisionDetectionFunction c)
Definition: AStar.hpp:133
void setWorldSize(MapCoords worldSize_)
Definition: AStar.hpp:131

References setCollisionDetectionFunction(), setDiagonalMovement(), setHeuristic(), and setWorldSize().

+ Here is the call graph for this function:

Member Function Documentation

◆ findNodeOnList()

static std::shared_ptr< Node > Horizon::Zone::AStar::Generator::findNodeOnList ( NodeSet nodes_,
MapCoords  coordinates_ 
)
inlinestaticprivate
101 {
102 for (auto node : nodes_) {
103 if (node->coordinates == coordinates_) {
104 return node;
105 }
106 }
107 return nullptr;
108 }

Referenced by findPath().

+ Here is the caller graph for this function:

◆ findPath()

CoordinateList Horizon::Zone::AStar::Generator::findPath ( MapCoords  source_,
MapCoords  target_ 
)
inline
140 {
141 CoordinateList path;
142 std::shared_ptr<Node> current = nullptr;
143 NodeSet openSet, closedSet;
144 int searchStep = 0;
145
146 openSet.reserve(20);
147 closedSet.reserve(20);
148 openSet.push_back(std::make_shared<Node>(source_));
149
150 if (check_collision(target_.x(), target_.y()))
151 return path;
152
153 while (!openSet.empty() && searchStep < MAX_VIEW_RANGE) {
154 auto current_it = openSet.begin();
155
156 current = *current_it;
157
158 for (auto it = openSet.begin(); it != openSet.end(); it++) {
159 auto node = *it;
160 if (node->getFScore() <= current->getFScore()) {
161 current = node;
162 current_it = it;
163 }
164 }
165
166 if (current->coordinates == target_)
167 break;
168
169 closedSet.push_back(current);
170 openSet.erase(current_it);
171
172 for (uint32_t i = 0; i < directions; ++i) {
173 MapCoords newCoordinates(current->coordinates + direction[i]);
174
175 if (check_collision(newCoordinates.x(), newCoordinates.y())
176 || findNodeOnList(closedSet, newCoordinates)) {
177 continue;
178 }
179
180 newCoordinates.set_move_cost((i < 4) ? 10 : 14);
181 uint32_t totalCost = current->G + newCoordinates.move_cost();
182
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;
192 }
193 }
194
195 searchStep++;
196 }
197
198 while (current != nullptr) {
199 path.push_back(current->coordinates);
200 current = current->parent;
201 }
202
203 releaseNodes(openSet);
204 releaseNodes(closedSet);
205
206 return path;
207 }
#define MAX_VIEW_RANGE
Definition: Horizon.hpp:59
int16_t y() const
Definition: Coordinates.hpp:120
int16_t x() const
Definition: Coordinates.hpp:119
CoordinateList direction
Definition: AStar.hpp:212
static void releaseNodes(NodeSet nodes_)
Definition: AStar.hpp:109
CollisionDetectionFunction check_collision
Definition: AStar.hpp:211
uint32_t directions
Definition: AStar.hpp:217
static std::shared_ptr< Node > findNodeOnList(NodeSet &nodes_, MapCoords coordinates_)
Definition: AStar.hpp:100
HeuristicFunction heuristic
Definition: AStar.hpp:210
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

References check_collision, direction, directions, findNodeOnList(), heuristic, MAX_VIEW_RANGE, Coordinates< MAX_COORDINATES >::move_cost(), releaseNodes(), Coordinates< MAX_COORDINATES >::set_move_cost(), Coordinates< MAX_COORDINATES >::x(), and Coordinates< MAX_COORDINATES >::y().

+ Here is the call graph for this function:

◆ releaseNodes()

static void Horizon::Zone::AStar::Generator::releaseNodes ( NodeSet  nodes_)
inlinestaticprivate
110 {
111 for (auto it = nodes_.begin(); it != nodes_.end();) {
112 it = nodes_.erase(it);
113 }
114 }

Referenced by findPath().

+ Here is the caller graph for this function:

◆ setCollisionDetectionFunction()

void Horizon::Zone::AStar::Generator::setCollisionDetectionFunction ( CollisionDetectionFunction  c)
inline
133{ check_collision = c; };

References check_collision.

Referenced by Generator().

+ Here is the caller graph for this function:

◆ setDiagonalMovement()

void Horizon::Zone::AStar::Generator::setDiagonalMovement ( bool  enable_)
inline
135{ directions = (enable_ ? 8 : 4); }
directions
Definition: UnitDefinitions.hpp:75

Referenced by Generator().

+ Here is the caller graph for this function:

◆ setHeuristic()

void Horizon::Zone::AStar::Generator::setHeuristic ( const HeuristicFunction heuristic_)
inline
137{ heuristic = [heuristic_](auto && PH1, auto && PH2) { return heuristic_(std::forward<decltype(PH1)>(PH1), std::forward<decltype(PH2)>(PH2)); }; }

References heuristic.

Referenced by BOOST_AUTO_TEST_CASE(), and Generator().

+ Here is the caller graph for this function:

◆ setWorldSize()

void Horizon::Zone::AStar::Generator::setWorldSize ( MapCoords  worldSize_)
inline
131{ worldSize = worldSize_; }
MapCoords worldSize
Definition: AStar.hpp:216

References worldSize.

Referenced by Generator().

+ Here is the caller graph for this function:

Member Data Documentation

◆ check_collision

CollisionDetectionFunction Horizon::Zone::AStar::Generator::check_collision
private

◆ direction

CoordinateList Horizon::Zone::AStar::Generator::direction
private
Initial value:
= {
{ 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
{ -1, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 }
}

Referenced by findPath().

◆ directions

uint32_t Horizon::Zone::AStar::Generator::directions {0}
private

Referenced by findPath().

◆ heuristic

HeuristicFunction Horizon::Zone::AStar::Generator::heuristic
private

Referenced by findPath(), and setHeuristic().

◆ worldSize

MapCoords Horizon::Zone::AStar::Generator::worldSize
private

Referenced by setWorldSize().


The documentation for this class was generated from the following file: