Fix path finding issue introduced by PR #470

master
Eduardo Bart 10 years ago
parent 64437e4e1a
commit 57a330e589

@ -655,15 +655,12 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
// as described in http://en.wikipedia.org/wiki/A*_search_algorithm // as described in http://en.wikipedia.org/wiki/A*_search_algorithm
struct Node { struct Node {
Node(const Position& pos) : cost(0), totalCost(0), steps(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection), evaluated(false) { } Node(const Position& pos) : cost(0), totalCost(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection) { }
bool operator<(const Node& other) const { return totalCost < other.totalCost; }
float cost; float cost;
float totalCost; float totalCost;
int steps;
Position pos; Position pos;
Node *prev; Node *prev;
Otc::Direction dir; Otc::Direction dir;
bool evaluated;
}; };
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret; std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
@ -788,27 +785,21 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
neighborNode->prev = currentNode; neighborNode->prev = currentNode;
neighborNode->cost = cost; neighborNode->cost = cost;
neighborNode->steps = currentNode->steps + 1;
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos); neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
neighborNode->dir = walkDir; neighborNode->dir = walkDir;
neighborNode->evaluated = false;
searchList.push_back(neighborNode); searchList.push_back(neighborNode);
} }
} }
std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; }); std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; });
auto end = std::unique(searchList.begin(), searchList.end()); auto uniq_end = std::unique(searchList.begin(), searchList.end());
searchList.erase(uniq_end, searchList.end());
currentNode->evaluated = true;
currentNode = nullptr; if(!searchList.empty()) {
for (auto begin = searchList.begin(); begin != end && !currentNode; ++begin) { currentNode = searchList.front();
Node *node = *begin; searchList.pop_front();
} else
if(!node->evaluated) currentNode = nullptr;
currentNode = node;
}
searchList.clear();
} }
if(foundNode) { if(foundNode) {
@ -827,5 +818,3 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
return ret; return ret;
} }
/* vim: set ts=4 sw=4 et: */

Loading…
Cancel
Save