Changed containers used by Pathfinder::astarArea.

This commit is contained in:
Felix Ableitner 2013-05-01 21:36:14 +02:00
parent f7b0d1348b
commit d9a5821e1a
2 changed files with 10 additions and 22 deletions

View file

@ -8,8 +8,8 @@
#include "Pathfinder.h"
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <Thor/Vectors.hpp>
@ -31,19 +31,21 @@ std::vector<Pathfinder::Portal*>
Pathfinder::astarArea(Area* start, Area* end) const {
assert(start);
assert(end);
auto heuristic_cost_estimate = [](Area* start, Area* end) {
return thor::length(sf::Vector2f(end->center - start->center));
};
std::unordered_set<Area*> closed;
std::unordered_map<Area*, float> openAreasEstimatedCost;
std::set<Area*> closed;
std::map<Area*, float> openAreasEstimatedCost;
// Navigated areas with previous area/portal.
std::unordered_map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal;
std::unordered_map<Area*, float> bestPathCost;
std::map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal;
std::map<Area*, float> bestPathCost;
openAreasEstimatedCost[start] = heuristic_cost_estimate(start, end);
bestPathCost[start] = 0;
while (!openAreasEstimatedCost.empty()) {
Area* current = std::min_element(openAreasEstimatedCost.begin(),
openAreasEstimatedCost.end())->first;
Area* current = openAreasEstimatedCost.begin()->first;
if (current == end) {
std::vector<Portal*> path;
auto previous = current;
@ -134,18 +136,6 @@ Pathfinder::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
return path;
}
/**
* Returns the linear distance between two areas (using their center).
*/
float
Pathfinder::heuristic_cost_estimate(Area* start, Area* end) const {
return thor::length(sf::Vector2f(end->center - start->center));
}
bool Pathfinder::Portal::operator==(const Portal& p) {
return start == p.start && end == p.end && area == p.area;
}
/**
* Inserts an area used for path finding.
*

View file

@ -24,7 +24,6 @@ public:
private:
Area* getArea(const sf::Vector2f& point) const;
float heuristic_cost_estimate(Area* start, Area* end) const;
std::vector<Portal*> astarArea(Area* start, Area* end) const;
private:
@ -38,7 +37,6 @@ private:
* Redundant data as portals are saved twice.
*/
struct Pathfinder::Portal {
bool operator==(const Portal& p);
sf::Vector2i start;
sf::Vector2i end;
Area* area;