diff --git a/ReadMe.md b/ReadMe.md index 0647f42..02b02c0 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -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); diff --git a/source/AStar.cpp b/source/AStar.cpp index 7bf471a..4205570 100644 --- a/source/AStar.cpp +++ b/source/AStar.cpp @@ -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); @@ -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); @@ -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_)); diff --git a/source/AStar.hpp b/source/AStar.hpp index d006820..3d86b2c 100644 --- a/source/AStar.hpp +++ b/source/AStar.hpp @@ -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_);