* https://github.com/shirayukikitsune/otclient:
  And do not delete curly brackets.
  Meh, spacing.
  Even more fixes
  Optimizations in Map::findPath
  More fixes in Map::findPath
  Fixes heap corruption in Map::findPath
This commit is contained in:
Ahmed Samy 2014-02-11 15:55:23 +02:00
commit 96f3bcb42b
1 changed files with 9 additions and 11 deletions

View File

@ -666,12 +666,6 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
bool evaluated;
};
struct LessNode : std::binary_function<Node*, Node*, bool> {
bool operator()(Node* a, Node* b) const {
return b->totalCost < a->totalCost;
}
};
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
std::vector<Otc::Direction>& dirs = std::get<0>(ret);
Otc::PathFindResult& result = std::get<1>(ret);
@ -703,7 +697,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
}
std::unordered_map<Position, Node*, PositionHasher> nodes;
std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList;
std::deque<Node*> searchList;
Node *currentNode = new Node(startPos);
currentNode->pos = startPos;
@ -798,19 +792,23 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
neighborNode->dir = walkDir;
neighborNode->evaluated = false;
searchList.push(neighborNode);
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());
currentNode->evaluated = true;
currentNode = nullptr;
while(searchList.size() > 0 && !currentNode) {
Node *node = searchList.top();
searchList.pop();
for (auto begin = searchList.begin(); begin != end && !currentNode; ++begin) {
Node *node = *begin;
if(!node->evaluated)
currentNode = node;
}
searchList.clear();
}
if(foundNode) {