Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NavPath Class Reference

#include <navpath.h>

Public Member Functions

bool isAbsolute () const
 
bool isEvaluated () const
 
bool isComplete () const
 
bool isCurrentDependant () const
 
bool isTargetDependant () const
 
std::string getDescription () const
 
void setVisible (bool vis)
 
void setColor (GFXColor col)
 
void setName (std::string n)
 
bool getVisible () const
 
GFXColor getColor () const
 
std::string getName () const
 
bool setSourceNode (PathNode *node)
 
bool setDestinationNode (PathNode *node)
 
PathNodegetSourceNode ()
 
PathNodegetDestinationNode ()
 
const PathNodegetSourceNode () const
 
const PathNodegetDestinationNode () const
 
unsigned getAbsoluteSource () const
 
unsigned getAbsoluteDestination () const
 
const std::list< unsigned > * getAllPoints () const
 
std::list< unsigned > * getAllPoints ()
 
void addDependant (NavPath *dependant)
 
void removeDependant (NavPath *dependant)
 
const std::set< NavPath * > * getDependants () const
 
std::set< NavPath * > * getDependants ()
 
std::vector< NavPath * > getRequiredPaths () const
 
bool checkForCycles () const
 
bool evaluate ()
 
void removeOldPath ()
 
void addNewPath ()
 
bool update ()
 
bool isNeighborPath (unsigned system, unsigned neighbor)
 
 NavPath ()
 
 ~NavPath ()
 

Protected Attributes

bool visible
 
std::string name
 
GFXColor color
 
PathNodesource
 
PathNodedestination
 
std::list< unsigned > path
 
std::map< unsigned, std::pair
< unsigned, unsigned > > 
pathNeighbors
 
std::set< NavPath * > dependants
 
TopoColor topoColor
 
unsigned topoTime
 
bool updated
 

Friends

class PathManager
 

Detailed Description

Definition at line 47 of file navpath.h.

Constructor & Destructor Documentation

NavPath::NavPath ( )

Definition at line 503 of file navpath.cpp.

References color, destination, name, source, and visible.

504 {
505  name = "New Path";
506  visible = true;
507  color = GFXColor( 1, 0, 0 );
508  source = NULL;
509  destination = NULL;
510 }
NavPath::~NavPath ( )

Definition at line 512 of file navpath.cpp.

References destination, getDependants(), PathNode::getRequiredPath(), i, removeDependant(), removeOldPath(), and source.

513 {
514  removeOldPath();
515  if (source) {
516  if ( source->getRequiredPath() )
518  delete source;
519  source = NULL;
520  }
521  if (destination) {
522  if ( destination->getRequiredPath() )
524  delete source;
525  source = NULL;
526  }
527  set< NavPath* > *depList = getDependants();
528  for (std::set< NavPath* >::iterator i = depList->begin(); i != depList->end(); ++i) {
529  if ( (*i)->source->getRequiredPath() == this ) {
530  delete (*i)->source;
531  (*i)->source = NULL;
532  }
533  if ( (*i)->destination->getRequiredPath() == this ) {
534  delete (*i)->destination;
535  (*i)->destination = NULL;
536  }
537  }
538 }

Member Function Documentation

void NavPath::addDependant ( NavPath dependant)

Definition at line 230 of file navpath.cpp.

References dependants.

Referenced by setDestinationNode(), and setSourceNode().

231 {
232  if (dependant == NULL)
233  return;
234  dependants.insert( dependant );
235 }
void NavPath::addNewPath ( )

Definition at line 458 of file navpath.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::AccessNavSystem(), i, path, and pathNeighbors.

Referenced by update().

459 {
461 
462  int i;
463  unsigned system;
464  //Inscribe new path
465  //*************************
466 
467  list< unsigned >::iterator aux;
468  for (list< unsigned >::iterator iter = path.begin(); iter != path.end(); ++iter) {
469  if ( (*iter) != path.front() )
470  pathNeighbors[(*iter)].first = ( *--(aux = iter) );
471  if ( (*iter) != path.back() )
472  pathNeighbors[(*iter)].second = ( *++(aux = iter) );
473  systemIter[*iter].part_of_path = true;
474  systemIter[*iter].paths.insert( this );
475  }
476 }
bool NavPath::checkForCycles ( ) const

Definition at line 266 of file navpath.cpp.

References getRequiredPaths(), i, and v.

Referenced by setDestinationNode(), and setSourceNode().

267 {
268  const NavPath *v;
269  vector< NavPath* >neighbors;
270  unsigned i;
271  deque< const NavPath* >pathStack;
272  pathStack.push_back( this );
273  bool cycle = false;
274  while (!pathStack.empty() && !cycle) {
275  v = pathStack.back();
276  pathStack.pop_back();
277  neighbors = v->getRequiredPaths();
278  for (i = 0; i < neighbors.size(); ++i) {
279  if (neighbors[i] == this) {
280  cycle = true;
281  break;
282  }
283  pathStack.push_back( neighbors[i] );
284  }
285  }
286  return cycle;
287 }
bool NavPath::evaluate ( )

Definition at line 289 of file navpath.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::AccessNavSystem(), destination, fprintf, VegaConfig::getVariable(), i, index, PathNode::initSearchQueue(), isAbsolute(), isComplete(), PathNode::isDestination(), XMLSupport::parse_int(), path, NavigationSystem::CachedSystemIterator::size(), source, and vs_config.

Referenced by update().

290 {
292  path.clear();
293  static size_t max_size = XMLSupport::parse_int( vs_config->getVariable( "graphics", "nav_max_search_size", "16384" ) );
294  if ( !isComplete() )
295  return false;
296  if ( isAbsolute() ) {
297  //Using a double-rooted BFS search
298  unsigned originIndex = source->initSearchQueue().front();
299  unsigned destIndex = destination->initSearchQueue().front();
300  if (originIndex == destIndex) {
301  path.push_back( originIndex );
302  return true;
303  }
304  vector< unsigned >prev( systemIter.size() );
305  vector< unsigned >visited( systemIter.size(), 0 );
306  deque< unsigned > oriFront, destFront;
307  bool found = false;
308  unsigned midNode;
309  unsigned midNodePrevOri;
310  unsigned midNodePrevDest;
311  midNodePrevOri = midNodePrevDest = 0; //to shut up a warning about possibly used uninitialized --chuck_starchaser
312  bool oriTurn = true;
313  deque< unsigned > *front;
314  unsigned visitMark;
315  if ( originIndex >= visited.size() || destIndex >= visited.size() ) {
316  fprintf( stderr, "(previously) FATAL error with nav system, referencing value too big %d %d with visited size %d\n",
317  (int) originIndex, (int) destIndex, (int) visited.size() );
318  return false;
319  }
320  oriFront.push_back( originIndex );
321  visited[originIndex] = 1;
322  destFront.push_back( destIndex );
323  visited[destIndex] = 2;
324  while (oriFront.size() < max_size && destFront.size() < max_size && !oriFront.empty() && !destFront.empty()
325  && !found) {
326  //stay within memory bounds in case something goes wrong (it has, unfortunately on occasion)
327  if (oriTurn) {
328  front = &oriFront;
329  visitMark = 1;
330  } else {
331  front = &destFront;
332  visitMark = 2;
333  }
334  unsigned index = front->front();
335  front->pop_front();
336  for (unsigned adjs = 0; adjs < systemIter[index].GetDestinationSize(); ++adjs) {
337  unsigned adjIndex = systemIter[index].GetDestinationIndex( adjs );
338  if ( systemIter[adjIndex].isDrawable() ) {
339  if (visited[adjIndex] == 0) {
340  visited[adjIndex] = visitMark;
341  prev[adjIndex] = index;
342  front->push_back( adjIndex );
343  } else if (visited[adjIndex] != visitMark) {
344  midNode = adjIndex;
345  if (oriTurn) {
346  midNodePrevDest = prev[adjIndex];
347  midNodePrevOri = index;
348  } else {
349  midNodePrevOri = prev[adjIndex];
350  midNodePrevDest = index;
351  }
352  found = true;
353  break;
354  }
355  }
356  }
357  oriTurn = !oriTurn;
358  }
359  if (found) {
360  unsigned index = midNodePrevOri;
361  while (index != originIndex) {
362  path.push_front( index );
363  index = prev[index];
364  if (path.size() >= max_size) {
365  //this prevents some odd "out of memory" crashes we were getting where there might have been a loop in the path somehow
366  path.clear();
367  found = false;
368  return false;
369  }
370  }
371  path.push_front( originIndex );
372  if (destIndex == midNode) {
373  path.push_back( destIndex );
374  } else {
375  path.push_back( midNode );
376 
377  unsigned index = midNodePrevDest;
378  while (index != destIndex) {
379  path.push_back( index );
380  index = prev[index];
381  if (path.size() >= max_size) {
382  //this prevents some odd "out of memory" crashes we were getting where there might have been a loop in the path somehow
383  path.clear();
384  found = false;
385  return false;
386  }
387  }
388  path.push_back( destIndex );
389  }
390  }
391  return found;
392  } else {
393  //Using single-rooted BFS search
394  vector< unsigned >prev( systemIter.size() );
395  vector< bool > visited( systemIter.size(), false );
396  deque< unsigned > frontier = source->initSearchQueue();
397  set< unsigned > origins;
398  bool found = false;
399  unsigned index, destIndex = 0;
400  for (unsigned i = 0; i < frontier.size(); ++i) {
401  index = frontier.front();
402  frontier.pop_front();
403  visited[index] = true;
404  frontier.push_back( index );
405  origins.insert( index );
406  if ( destination->isDestination( index ) ) {
407  path.push_back( index );
408  return true;
409  }
410  }
411  while (frontier.size() < max_size && !frontier.empty() && !found) {
412  index = frontier.front();
413  frontier.pop_front();
414  for (unsigned adjs = 0; adjs < systemIter[index].GetDestinationSize(); ++adjs) {
415  unsigned adjIndex = systemIter[index].GetDestinationIndex( adjs );
416  if ( !visited[adjIndex] && systemIter[adjIndex].isDrawable() ) {
417  visited[adjIndex] = true;
418  prev[adjIndex] = index;
419  frontier.push_back( adjIndex );
420  if ( destination->isDestination( adjIndex ) ) {
421  found = true;
422  destIndex = adjIndex;
423  break;
424  }
425  }
426  }
427  }
428  if (found) {
429  index = destIndex;
430  path.push_front( index );
431  do {
432  index = prev[index];
433  path.push_front( index );
434  if (path.size() >= max_size) {
435  //this prevents some odd "out of memory" crashes we were getting where there might have been a loop in the path somehow
436  path.clear();
437  found = false;
438  return false;
439  }
440  } while ( !origins.count( index ) ); //While the index is not an origin
441  }
442  return found;
443  }
444 }
unsigned NavPath::getAbsoluteDestination ( ) const

Definition at line 215 of file navpath.cpp.

References path.

Referenced by ChainPathNode::initSearchQueue(), and ChainPathNode::isDestination().

216 {
217  return path.back();
218 }
unsigned NavPath::getAbsoluteSource ( ) const

Definition at line 210 of file navpath.cpp.

References path.

Referenced by ChainPathNode::initSearchQueue(), and ChainPathNode::isDestination().

211 {
212  return path.front();
213 }
const std::list< unsigned > * NavPath::getAllPoints ( ) const

Definition at line 220 of file navpath.cpp.

References path.

Referenced by ChainPathNode::initSearchQueue(), and ChainPathNode::isDestination().

221 {
222  return &path;
223 }
std::list< unsigned > * NavPath::getAllPoints ( )

Definition at line 225 of file navpath.cpp.

References path.

226 {
227  return &path;
228 }
GFXColor NavPath::getColor ( ) const

Definition at line 148 of file navpath.cpp.

References color.

149 {
150  return color;
151 }
const std::set< NavPath * > * NavPath::getDependants ( ) const

Definition at line 244 of file navpath.cpp.

References dependants.

Referenced by PathManager::dfsVisit(), PathManager::updateDependants(), and ~NavPath().

245 {
246  return &dependants;
247 }
std::set< NavPath * > * NavPath::getDependants ( )

Definition at line 249 of file navpath.cpp.

References dependants.

250 {
251  return &dependants;
252 }
string NavPath::getDescription ( ) const

Definition at line 99 of file navpath.cpp.

References destination, PathNode::getDescription(), getName(), getVisible(), isComplete(), isEvaluated(), and source.

Referenced by NavComputer::updateDescription().

100 {
101  string temp;
102 
103  temp = getName();
104  temp += "#n#";
105  temp += "----------#n#";
106  temp += "Visible: ";
107  temp += (getVisible() ? "True" : "False");
108  temp += "#n#";
109  if (source) {
110  temp += "Source:#n#";
111  temp += source->getDescription();
112  temp += "#n#";
113  }
114  if (destination) {
115  temp += "Destination:#n#";
116  temp += destination->getDescription();
117  temp += "#n#";
118  }
119  if (!source || !destination)
120  temp += "#n#INCOMPLETE#n#";
121  else if ( !isComplete() )
122  temp += "#n#PATH CHAIN IS UNSOLVED BEFORE THIS POINT#n#";
123  else if ( !isEvaluated() )
124  temp += "#n#PATH NOT FOUND#n#";
125  return temp;
126 }
PathNode* NavPath::getDestinationNode ( )
inline

Definition at line 74 of file navpath.h.

References destination.

75  {
76  return destination;
77  }
const PathNode* NavPath::getDestinationNode ( ) const
inline

Definition at line 82 of file navpath.h.

References destination.

83  {
84  return destination;
85  }
string NavPath::getName ( ) const

Definition at line 153 of file navpath.cpp.

References name.

Referenced by getDescription(), and ChainPathNode::getDescription().

154 {
155  return name;
156 }
std::vector< NavPath * > NavPath::getRequiredPaths ( ) const

Definition at line 254 of file navpath.cpp.

References destination, PathNode::getRequiredPath(), and source.

Referenced by checkForCycles().

255 {
256  std::vector< NavPath* >temp;
257  if (source)
258  if ( source->getRequiredPath() )
259  temp.push_back( source->getRequiredPath() );
260  if (destination)
261  if ( destination->getRequiredPath() )
262  temp.push_back( destination->getRequiredPath() );
263  return temp;
264 }
PathNode* NavPath::getSourceNode ( )
inline

Definition at line 70 of file navpath.h.

References source.

71  {
72  return source;
73  }
const PathNode* NavPath::getSourceNode ( ) const
inline

Definition at line 78 of file navpath.h.

References source.

79  {
80  return source;
81  }
bool NavPath::getVisible ( ) const

Definition at line 143 of file navpath.cpp.

References visible.

Referenced by NavComputer::actionShowPath(), and getDescription().

144 {
145  return visible;
146 }
bool NavPath::isAbsolute ( ) const

Definition at line 55 of file navpath.cpp.

References destination, PathNode::isAbsolute(), isComplete(), and source.

Referenced by evaluate().

56 {
57  if ( !isComplete() )
58  return false;
59  if ( !source->isAbsolute() || !destination->isAbsolute() )
60  return false;
61  return true;
62 }
bool NavPath::isComplete ( ) const

Definition at line 64 of file navpath.cpp.

References destination, PathNode::getRequiredPath(), isEvaluated(), and source.

Referenced by evaluate(), getDescription(), and isAbsolute().

65 {
66  if (!source || !destination)
67  return false;
68  if ( source->getRequiredPath() )
69  if ( !source->getRequiredPath()->isEvaluated() )
70  return false;
73  return false;
74  return true;
75 }
bool NavPath::isCurrentDependant ( ) const

Definition at line 77 of file navpath.cpp.

References destination, PathNode::isCurrentDependant(), and source.

78 {
79  if (source)
80  if ( source->isCurrentDependant() )
81  return true;
82  if (destination)
84  return true;
85  return false;
86 }
bool NavPath::isEvaluated ( ) const
inline

Definition at line 51 of file navpath.h.

References path.

Referenced by getDescription(), and isComplete().

52  {
53  return path.size() != 0;
54  }
bool NavPath::isNeighborPath ( unsigned  system,
unsigned  neighbor 
)

Definition at line 478 of file navpath.cpp.

References i, path, and pathNeighbors.

479 {
480  map< unsigned, pair< unsigned, unsigned > >::iterator i = pathNeighbors.find( system );
481  if ( i == pathNeighbors.end() )
482  return false;
483  if ( system != path.front() )
484  if ( (*i).second.first == neighbor )
485  return true;
486  if ( system != path.back() )
487  if ( (*i).second.second == neighbor )
488  return true;
489  return false;
490 }
bool NavPath::isTargetDependant ( ) const

Definition at line 88 of file navpath.cpp.

References destination, PathNode::isTargetDependant(), and source.

89 {
90  if (source)
91  if ( source->isTargetDependant() )
92  return true;
93  if (destination)
95  return true;
96  return false;
97 }
void NavPath::removeDependant ( NavPath dependant)

Definition at line 237 of file navpath.cpp.

References dependants.

Referenced by setDestinationNode(), setSourceNode(), and ~NavPath().

238 {
239  if (dependant == NULL)
240  return;
241  dependants.erase( dependant );
242 }
void NavPath::removeOldPath ( )

Definition at line 446 of file navpath.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::AccessNavSystem(), i, path, and pathNeighbors.

Referenced by update(), and ~NavPath().

447 {
449  //Erase old path
450  //*************************
451  for (list< unsigned >::iterator i = path.begin(); i != path.end(); ++i)
452  if ( systemIter[*i].paths.erase( this ) ) //This erases this path from the list of paths in system
453  if ( systemIter[*i].paths.empty() )
454  systemIter[*i].part_of_path = false;
455  pathNeighbors.clear();
456 }
void NavPath::setColor ( GFXColor  col)

Definition at line 133 of file navpath.cpp.

References color.

134 {
135  color = col;
136 }
bool NavPath::setDestinationNode ( PathNode node)

Definition at line 185 of file navpath.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::AccessNavSystem(), addDependant(), checkForCycles(), destination, PathNode::getRequiredPath(), NavigationSystem::pathman, removeDependant(), and PathManager::updateSpecificPath().

Referenced by NavComputer::actionDestination(), and NavComputer::init().

186 {
187  if (!node)
188  return false;
189  PathNode *oldNode = destination;
190  destination = node;
191  if ( checkForCycles() ) {
192  destination = oldNode;
193  delete node;
194  node = NULL;
195  return false;
196  } else {
197  if (oldNode)
198  if ( oldNode->getRequiredPath() )
199  oldNode->getRequiredPath()->removeDependant( this );
200  if ( node->getRequiredPath() )
201  node->getRequiredPath()->addDependant( this );
202  if (oldNode)
203  delete oldNode;
204  oldNode = NULL;
206  return true;
207  }
208 }
void NavPath::setName ( std::string  n)

Definition at line 138 of file navpath.cpp.

References name.

Referenced by NavComputer::actionRenameConfirmed(), and NavComputer::init().

139 {
140  name = n;
141 }
bool NavPath::setSourceNode ( PathNode node)

Definition at line 158 of file navpath.cpp.

References _Universe, Universe::AccessCockpit(), Cockpit::AccessNavSystem(), addDependant(), checkForCycles(), PathNode::getRequiredPath(), PathNode::isSourceable(), NavigationSystem::pathman, removeDependant(), source, and PathManager::updateSpecificPath().

Referenced by NavComputer::actionSource(), PathManager::addPath(), and NavComputer::init().

159 {
160  if (!node)
161  return false;
162  if ( !node->isSourceable() )
163  return false;
164  PathNode *oldNode = source;
165  source = node;
166  if ( checkForCycles() ) {
167  source = oldNode;
168  delete node;
169  node = NULL;
170  return false;
171  } else {
172  if (oldNode)
173  if ( oldNode->getRequiredPath() )
174  oldNode->getRequiredPath()->removeDependant( this );
175  if ( node->getRequiredPath() )
176  node->getRequiredPath()->addDependant( this );
177  if (oldNode)
178  delete oldNode;
179  oldNode = NULL;
181  return true;
182  }
183 }
void NavPath::setVisible ( bool  vis)

Definition at line 128 of file navpath.cpp.

References visible.

Referenced by NavComputer::actionShowPath().

129 {
130  visible = vis;
131 }
bool NavPath::update ( )

Definition at line 492 of file navpath.cpp.

References addNewPath(), evaluate(), and removeOldPath().

Referenced by PathManager::updateSpecificPath().

493 {
494  removeOldPath();
495  if ( evaluate() ) {
496  addNewPath();
497  return true;
498  } else {
499  return false;
500  }
501 }

Friends And Related Function Documentation

friend class PathManager
friend

Definition at line 109 of file navpath.h.

Member Data Documentation

GFXColor NavPath::color
protected

Definition at line 113 of file navpath.h.

Referenced by getColor(), NavPath(), and setColor().

std::set< NavPath* > NavPath::dependants
protected

Definition at line 118 of file navpath.h.

Referenced by addDependant(), getDependants(), and removeDependant().

std::string NavPath::name
protected

Definition at line 112 of file navpath.h.

Referenced by getName(), NavPath(), and setName().

std::list< unsigned > NavPath::path
protected
std::map< unsigned, std::pair< unsigned, unsigned > > NavPath::pathNeighbors
protected

Definition at line 117 of file navpath.h.

Referenced by addNewPath(), isNeighborPath(), and removeOldPath().

TopoColor NavPath::topoColor
protected

Definition at line 120 of file navpath.h.

Referenced by PathManager::dfsVisit().

unsigned NavPath::topoTime
protected

Definition at line 121 of file navpath.h.

Referenced by PathManager::dfsVisit().

bool NavPath::updated
protected

Definition at line 122 of file navpath.h.

Referenced by PathManager::updateSpecificPath().

bool NavPath::visible
protected

Definition at line 111 of file navpath.h.

Referenced by getVisible(), NavPath(), and setVisible().


The documentation for this class was generated from the following files: