Skip to content

Commit

Permalink
Use an unordered_map for ClosedNodeList
Browse files Browse the repository at this point in the history
Simplifies closed node list by storing Node Ids only into an unordered map, speeding up node adding and lookup, especially with unsuccessful pathfinder iterations.

Workaround for Issue #652
  • Loading branch information
bsxf-47 authored and dscharrer committed Apr 8, 2017
1 parent a6b5bff commit 807e52d
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/ai/PathFinder.cpp
Expand Up @@ -45,6 +45,7 @@ ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

#include <limits>
#include <algorithm>
#include <boost/unordered_map.hpp>

#include "graphics/GraphicsTypes.h"
#include "graphics/Math.h"
Expand Down Expand Up @@ -151,29 +152,23 @@ class PathFinder::OpenNodeList {

class PathFinder::ClosedNodeList {

typedef std::vector<Node*> NodeList;
typedef boost::unordered_map<NodeId, Node *> NodeList;
NodeList nodes;

public:

~ClosedNodeList() {
for(NodeList::iterator i = nodes.begin(); i != nodes.end(); ++i) {
delete *i;
for(boost::unordered_map<NodeId, Node *>::iterator it=nodes.begin(); it != nodes.end(); it++) {
delete (*it).second;
}
}

void add(Node * node) {
nodes.push_back(node);
nodes.insert(NodeList::value_type(node->getId(), node));
}

bool contains(NodeId id) const {
// TODO better datastructure: set of closed node ids
for(NodeList::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
if((*i)->getId() == id) {
return true;
}
}
return false;
return nodes.find(id) != nodes.end();
}

};
Expand Down Expand Up @@ -213,11 +208,11 @@ bool PathFinder::move(NodeId from, NodeId to, Result & rlist, bool stealth) cons
ClosedNodeList close;
do {

NodeId nid = node->getId();

// Put node onto close list as we have now examined this node.
close.add(node);

NodeId nid = node->getId();

// If it's the goal node then we're done.
if(nid == to) {
buildPath(*node, rlist);
Expand Down Expand Up @@ -280,6 +275,8 @@ bool PathFinder::flee(NodeId from, const Vec3f & danger, float safeDist, Result
ClosedNodeList close;
do {

NodeId nid = node->getId();

// Put node onto close list as we have now examined this node.
close.add(node);

Expand All @@ -289,8 +286,6 @@ bool PathFinder::flee(NodeId from, const Vec3f & danger, float safeDist, Result
return true;
}

NodeId nid = node->getId();

// Otherwise, generate child from current node.
for(short i(0); i < map_d[nid].nblinked; i++) {

Expand Down

0 comments on commit 807e52d

Please sign in to comment.