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 "Pathfinder.h"
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <set>
#include <unordered_map> #include <map>
#include <Thor/Vectors.hpp> #include <Thor/Vectors.hpp>
@ -31,19 +31,21 @@ std::vector<Pathfinder::Portal*>
Pathfinder::astarArea(Area* start, Area* end) const { Pathfinder::astarArea(Area* start, Area* end) const {
assert(start); assert(start);
assert(end); 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::set<Area*> closed;
std::unordered_map<Area*, float> openAreasEstimatedCost; std::map<Area*, float> openAreasEstimatedCost;
// Navigated areas with previous area/portal. // Navigated areas with previous area/portal.
std::unordered_map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal; std::map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal;
std::unordered_map<Area*, float> bestPathCost; std::map<Area*, float> bestPathCost;
openAreasEstimatedCost[start] = heuristic_cost_estimate(start, end); openAreasEstimatedCost[start] = heuristic_cost_estimate(start, end);
bestPathCost[start] = 0; bestPathCost[start] = 0;
while (!openAreasEstimatedCost.empty()) { while (!openAreasEstimatedCost.empty()) {
Area* current = std::min_element(openAreasEstimatedCost.begin(), Area* current = openAreasEstimatedCost.begin()->first;
openAreasEstimatedCost.end())->first;
if (current == end) { if (current == end) {
std::vector<Portal*> path; std::vector<Portal*> path;
auto previous = current; auto previous = current;
@ -134,18 +136,6 @@ Pathfinder::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
return path; 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. * Inserts an area used for path finding.
* *

View file

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