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.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef __NAVPATH_H__
23 #define __NAVPATH_H__
24 
25 #include <vector>
26 #include <deque>
27 #include <list>
28 #include <set>
29 #include <map>
30 #include <string>
31 #include "criteria.h"
32 
33 #include "gfxlib.h"
34 
36 
37 //*******************************************************************//
39 //NavPath Class //
41 //*******************************************************************//
42 
43 class NavPath;
44 class PathManager;
45 class PathNode;
46 
47 class NavPath
48 {
49 public:
50  bool isAbsolute() const;
51  bool isEvaluated() const
52  {
53  return path.size() != 0;
54  }
55  bool isComplete() const;
56  bool isCurrentDependant() const;
57  bool isTargetDependant() const;
58 
59  std::string getDescription() const;
60 
61  void setVisible( bool vis );
62  void setColor( GFXColor col );
63  void setName( std::string n );
64  bool getVisible() const;
65  GFXColor getColor() const;
66  std::string getName() const;
67 
68  bool setSourceNode( PathNode *node );
69  bool setDestinationNode( PathNode *node );
71  {
72  return source;
73  }
75  {
76  return destination;
77  }
78  const PathNode * getSourceNode() const
79  {
80  return source;
81  }
82  const PathNode * getDestinationNode() const
83  {
84  return destination;
85  }
86  unsigned getAbsoluteSource() const;
87  unsigned getAbsoluteDestination() const;
88  const std::list< unsigned > * getAllPoints() const;
89  std::list< unsigned > * getAllPoints();
90 
91  void addDependant( NavPath *dependant );
92  void removeDependant( NavPath *dependant );
93  const std::set< NavPath* > * getDependants() const;
94  std::set< NavPath* > * getDependants();
95 
96  std::vector< NavPath* >getRequiredPaths() const;
97  bool checkForCycles() const;
98  bool evaluate();
99  void removeOldPath();
100  void addNewPath();
101  bool update();
102 
103  bool isNeighborPath( unsigned system, unsigned neighbor );
104 
105  NavPath();
106  ~NavPath();
107 
108 protected:
109  friend class PathManager;
110 
111  bool visible;
112  std::string name;
116  std::list< unsigned >path;
117  std::map< unsigned, std::pair< unsigned, unsigned > >pathNeighbors;
118  std::set< NavPath* > dependants;
119 
121  unsigned topoTime;
122  bool updated;
123 };
124 
125 //*******************************************************************//
127 //PathManager Class //
129 //*******************************************************************//
130 
132 {
133 public:
134  void addPath();
135  bool removePath( NavPath *path );
136  void showAll();
137  void showNone();
138 
140  bool updateSpecificPath( NavPath *path );
141  void updatePaths( UpdateType type = ALL );
142  void updateDependants( NavPath *parent );
143 
144  PathManager();
145  ~PathManager();
146 
147 protected:
148  friend class NavPath;
149  friend class NavComputer;
150 
151  std::vector< NavPath* >paths;
152  std::list< NavPath* > topoOrder;
153 
154  void DFS();
155  void dfsVisit( NavPath *path );
156  unsigned topoTime;
157 };
158 
159 //*******************************************************************//
161 //PathNode Class //
163 //*******************************************************************//
164 
165 class PathNode
166 {
167 public:
168  virtual bool isAbsolute() const = 0;
169 //Desc: True IFF this node can dereference into one absolute system.
170  virtual bool isSourceable() const = 0;
171 //Desc True IFF this node can be used as a source in a path finding algorithm
172 
173  virtual bool isCurrentDependant() const
174  {
175  return false;
176  }
177  virtual bool isTargetDependant() const
178  {
179  return false;
180  }
181 
183  {
184  return NULL;
185  }
186 //Desc: Returns the list of paths this node is dependant on.
187  virtual std::string getDescription() const = 0;
188 //Desc: Returns a textual description of the node
189 
190  virtual std::deque< unsigned >initSearchQueue() const = 0;
191 //Desc: Returns a deque that may be used to start a search
192  virtual bool isDestination( unsigned index ) const = 0;
193 //Desc: True IFF the system index is equivalent to the system
194 //specified in the node
195 
196  virtual PathNode * clone() const = 0;
197  PathNode() {}
198  virtual ~PathNode() {}
199 };
200 
201 //*******************************************************************//
203 //AbsolutePathNode Class //
205 //*******************************************************************//
206 
208 {
209 public:
210  bool isAbsolute() const
211  {
212  return true;
213  }
214  bool isSourceable() const
215  {
216  return true;
217  }
218  std::string getDescription() const;
219 
220  std::deque< unsigned >initSearchQueue() const;
221  bool isDestination( unsigned index ) const
222  {
223  return index == system;
224  }
225 
226  unsigned getSystemIndex()
227  {
228  return system;
229  }
230 //Desc: Gets the system referenced by this node
231 
232  PathNode * clone() const
233  {
234  return new AbsolutePathNode( system );
235  }
237  {
238  system = index;
239  }
241 
242 protected:
243  unsigned system;
244 };
245 
246 //*******************************************************************//
248 //CurrentPathNode Class //
250 //*******************************************************************//
251 
252 class CurrentPathNode : public PathNode
253 {
254 public:
255  bool isAbsolute() const
256  {
257  return true;
258  }
259  bool isSourceable() const
260  {
261  return true;
262  }
263  std::string getDescription() const
264  {
265  return "Current System";
266  }
267 
268  bool isCurrentDependant() const
269  {
270  return true;
271  }
272 
273  std::deque< unsigned >initSearchQueue() const;
274  bool isDestination( unsigned index ) const;
275 
276  PathNode * clone() const
277  {
278  return new CurrentPathNode();
279  }
282 };
283 
284 //*******************************************************************//
286 //TargetPathNode Class //
288 //*******************************************************************//
289 
290 class TargetPathNode : public PathNode
291 {
292 public:
293  bool isAbsolute() const
294  {
295  return true;
296  }
297  bool isSourceable() const
298  {
299  return true;
300  }
301  std::string getDescription() const
302  {
303  return "Target System";
304  }
305 
306  bool isTargetDependant() const
307  {
308  return true;
309  }
310 
311  std::deque< unsigned >initSearchQueue() const;
312  bool isDestination( unsigned index ) const;
313 
314  PathNode * clone() const
315  {
316  return new TargetPathNode();
317  }
320 };
321 
322 //*******************************************************************//
324 //CriteriaPathNode Class //
326 //*******************************************************************//
327 
329 {
330 public:
331  bool isAbsolute() const
332  {
333  return false;
334  }
335  bool isSourceable() const
336  {
337  return false;
338  }
339  std::string getDescription() const;
340 
341  std::deque< unsigned >initSearchQueue() const
342  {
343  std::deque< unsigned >temp;
344  return temp;
345  }
346  bool isDestination( unsigned index ) const;
347 
349  {
350  return criteria;
351  }
352 
353  PathNode * clone() const;
356 
357 private:
358  CriteriaRoot *criteria;
359 };
360 
361 //*******************************************************************//
363 //ChainPathNode Class //
365 //*******************************************************************//
366 
367 class ChainPathNode : public PathNode
368 {
369 public:
370  bool isAbsolute() const
371  {
372  return type == ALL_POINTS ? false : true;
373  }
374  bool isSourceable() const
375  {
376  return true;
377  }
378  std::string getDescription() const;
379 
381  {
382  return supplierPath;
383  }
384  std::deque< unsigned >initSearchQueue() const;
385  bool isDestination( unsigned index ) const;
386 
387  void setSupplierPath( NavPath *supplier )
388  {
389  supplierPath = supplier;
390  }
392  {
393  return supplierPath;
394  }
396  void setPartType( PartType part )
397  {
398  type = part;
399  }
401  {
402  return type;
403  }
404 
405  PathNode * clone() const
406  {
407  return new ChainPathNode( supplierPath, type );
408  }
410  {
411  supplierPath = NULL;
412  }
413  ChainPathNode( NavPath *supplier, PartType part )
414  {
415  supplierPath = supplier;
416  type = part;
417  }
419 
420 private:
421  NavPath *supplierPath;
422  PartType type;
423 };
424 
425 #endif //__NAVPATH_H__
426