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 // 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; if(!searchList.empty()) {
currentNode = nullptr; currentNode = searchList.front();
for (auto begin = searchList.begin(); begin != end && !currentNode; ++begin) { searchList.pop_front();
Node *node = *begin; } else
currentNode = nullptr;
if(!node->evaluated)
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: */