diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-10-02 10:30:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-02 10:30:12 +0200 |
commit | b4c1d40869066e50d50dfb1bdb4dd3f68b99cd91 (patch) | |
tree | dba63796951aa892a1f7fcf9ba0698fea415570d /core/math | |
parent | e98eb0c113aa08e12d3a5b9b64a7cec182bed2ab (diff) | |
parent | 40562a67c86d21a8ad411c0711889c519697e150 (diff) |
Merge pull request #21492 from Maykeye/astar
Changed A* exit condition, added 2 tests for it
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/a_star.cpp | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index e4f93289e9..451c45cade 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -250,14 +250,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { n->distance = _compute_cost(begin_point->id, n->id) * n->weight_scale; n->last_pass = pass; open_list.add(&n->list); - - if (end_point == n) { - found_route = true; - break; - } } - while (!found_route) { + while (true) { if (open_list.first() == NULL) { // No path found @@ -277,13 +272,16 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { cost += _estimate_cost(p->id, end_point->id); if (cost < least_cost) { - least_cost_point = E; least_cost = cost; } } Point *p = least_cost_point->self(); + if (p == end_point) { + found_route = true; + break; + } for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) { @@ -295,7 +293,6 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { // Already visited, is this cheaper? if (e->distance > distance) { - e->prev_point = p; e->distance = distance; } @@ -306,18 +303,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { e->distance = distance; e->last_pass = pass; // Mark as used open_list.add(&e->list); - - if (e == end_point) { - // End reached; stop algorithm - found_route = true; - break; - } } } - if (found_route) - break; - open_list.remove(least_cost_point); } |