Fix path finding issue introduced by PR #470

This commit is contained in:
Eduardo Bart 2014-02-11 19:28:01 -02:00
parent 64437e4e1a
commit 57a330e589
1 changed files with 8 additions and 19 deletions

View File

@ -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
struct Node {
Node(const Position& pos) : cost(0), totalCost(0), steps(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection), evaluated(false) { }
bool operator<(const Node& other) const { return totalCost < other.totalCost; }
Node(const Position& pos) : cost(0), totalCost(0), pos(pos), prev(nullptr), dir(Otc::InvalidDirection) { }
float cost;
float totalCost;
int steps;
Position pos;
Node *prev;
Otc::Direction dir;
bool evaluated;
};
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->cost = cost;
neighborNode->steps = currentNode->steps + 1;
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
neighborNode->dir = walkDir;
neighborNode->evaluated = false;
searchList.push_back(neighborNode);
}
}
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;
if(!searchList.empty()) {
currentNode = searchList.front();
searchList.pop_front();
} else
currentNode = nullptr;
for (auto begin = searchList.begin(); begin != end && !currentNode; ++begin) {
Node *node = *begin;
if(!node->evaluated)
currentNode = node;
}
searchList.clear();
}
if(foundNode) {
@ -827,5 +818,3 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
return ret;
}
/* vim: set ts=4 sw=4 et: */