Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main()
{
AStar::Generator generator;
// Set 2d map size.
generator.setWorldSize({25, 25});
generator.setWorldSize({25, 25}); // World is between {0, 0} and {24, 24}
// You can use a few heuristics : manhattan, euclidean or octagonal.
generator.setHeuristic(AStar::Heuristic::euclidean);
generator.setDiagonalMovement(true);
Expand Down
16 changes: 15 additions & 1 deletion source/AStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AStar::uint AStar::Node::getScore()
return G + H;
}

AStar::Generator::Generator()
AStar::Generator::Generator() noexcept : heuristic{}, direction{}, walls{}, worldSize{ 0, 0 }, directions{ 0 }
{
setDiagonalMovement(false);
setHeuristic(&Heuristic::manhattan);
Expand All @@ -52,11 +52,15 @@ void AStar::Generator::setHeuristic(HeuristicFunction heuristic_)

void AStar::Generator::addCollision(Vec2i coordinates_)
{
verify(coordinates_);

walls.push_back(coordinates_);
}

void AStar::Generator::removeCollision(Vec2i coordinates_)
{
verify(coordinates_);

auto it = std::find(walls.begin(), walls.end(), coordinates_);
if (it != walls.end()) {
walls.erase(it);
Expand All @@ -68,8 +72,18 @@ void AStar::Generator::clearCollisions()
walls.clear();
}

void AStar::Generator::verify(const Vec2i& v) const
{
if ((v.x < 0) || (v.x >= worldSize.x) || (v.y < 0) || (v.y >= worldSize.y))
{
throw std::out_of_range("Vec2i is out of range !");
}
}

AStar::CoordinateList AStar::Generator::findPath(Vec2i source_, Vec2i target_)
{
verify(source_); verify(target_);

Node *current = nullptr;
NodeSet openSet, closedSet;
openSet.insert(new Node(source_));
Expand Down
3 changes: 2 additions & 1 deletion source/AStar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ namespace AStar
bool detectCollision(Vec2i coordinates_);
Node* findNodeOnList(NodeSet& nodes_, Vec2i coordinates_);
void releaseNodes(NodeSet& nodes_);
void verify(const Vec2i&) const;

public:
Generator();
Generator() noexcept;
void setWorldSize(Vec2i worldSize_);
void setDiagonalMovement(bool enable_);
void setHeuristic(HeuristicFunction heuristic_);
Expand Down