Optimize path finding upto 100x performance again
This commit is contained in:
parent
c96b717273
commit
bb514d3acb
|
@ -663,6 +663,12 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
|
||||||
Otc::Direction dir;
|
Otc::Direction dir;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct LessNode : std::binary_function<std::pair<Node*, float>, std::pair<Node*, float>, bool> {
|
||||||
|
bool operator()(std::pair<Node*, float> a, std::pair<Node*, float> b) const {
|
||||||
|
return b.second < a.second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
|
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
|
||||||
std::vector<Otc::Direction>& dirs = std::get<0>(ret);
|
std::vector<Otc::Direction>& dirs = std::get<0>(ret);
|
||||||
Otc::PathFindResult& result = std::get<1>(ret);
|
Otc::PathFindResult& result = std::get<1>(ret);
|
||||||
|
@ -694,7 +700,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_map<Position, Node*, PositionHasher> nodes;
|
std::unordered_map<Position, Node*, PositionHasher> nodes;
|
||||||
std::deque<Node*> searchList;
|
std::priority_queue<std::pair<Node*, float>, std::vector<std::pair<Node*, float>>, LessNode> searchList;
|
||||||
|
|
||||||
Node *currentNode = new Node(startPos);
|
Node *currentNode = new Node(startPos);
|
||||||
currentNode->pos = startPos;
|
currentNode->pos = startPos;
|
||||||
|
@ -779,7 +785,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
|
||||||
nodes[neighborPos] = neighborNode;
|
nodes[neighborPos] = neighborNode;
|
||||||
} else {
|
} else {
|
||||||
neighborNode = nodes[neighborPos];
|
neighborNode = nodes[neighborPos];
|
||||||
if(neighborNode->cost < cost)
|
if(neighborNode->cost <= cost)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -787,17 +793,13 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
|
||||||
neighborNode->cost = cost;
|
neighborNode->cost = cost;
|
||||||
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
|
neighborNode->totalCost = neighborNode->cost + neighborPos.distance(goalPos);
|
||||||
neighborNode->dir = walkDir;
|
neighborNode->dir = walkDir;
|
||||||
searchList.push_back(neighborNode);
|
searchList.push(std::make_pair(neighborNode, neighborNode->totalCost));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(searchList.begin(), searchList.end(), [](Node *a, Node *b) { return a->totalCost < b->totalCost; });
|
|
||||||
auto uniq_end = std::unique(searchList.begin(), searchList.end());
|
|
||||||
searchList.erase(uniq_end, searchList.end());
|
|
||||||
|
|
||||||
if(!searchList.empty()) {
|
if(!searchList.empty()) {
|
||||||
currentNode = searchList.front();
|
currentNode = searchList.top().first;
|
||||||
searchList.pop_front();
|
searchList.pop();
|
||||||
} else
|
} else
|
||||||
currentNode = nullptr;
|
currentNode = nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue