Planeshift
pathfind.h
Go to the documentation of this file.
1 /*
2 * pathfind.h
3 *
4 * Copyright (C) 2005 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation (version 2
9 * of the License).
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 
19 #ifndef ___PATHFIND_HEADER___
20 #define ___PATHFIND_HEADER___
21 
22 #ifdef USE_WALKPOLY
23 
24 //=============================================================================
25 // Crystal Space Includes
26 //=============================================================================
27 #include <csgeom/vector3.h>
28 #include <csgeom/box.h>
29 #include <csutil/hash.h>
30 #include <iutil/objreg.h>
31 #include <csutil/array.h>
32 #include <csutil/csstring.h>
33 #include <csutil/list.h>
34 
39 class psANode;
40 class psWalkPoly;
41 class psWalkPolyMap;
42 class psAMap;
43 struct iVFS;
44 struct iSector;
45 struct iCollection;
46 struct iEngine;
47 struct iDocumentNode;
48 
49 /***************************************************************************/
52 class psPFMap
53 {
54 public:
55  csString name;
56  iCollection* region;
57  psWalkPolyMap* wpMap;
58  psAMap* aMap;
59 };
60 
61 /***************************************************************************/
64 class psPFMaps
65 {
66 public:
67  psPFMaps(iObjectRegistry* objReg);
68  psPFMap* GetRegionBySector(const csString &sectorName);
69  psPFMap* GetRegionBySector(iSector* sector);
70  psPFMap* FindRegionByName(const csString &regionName);
71 
72 protected:
73  iCollection* GetCSRegionOfSector(const csString &sectorName);
74 
75  csList<psPFMap*> regions;
76 
78  csHash<psPFMap*, csString> regionMap;
79 
80  iObjectRegistry* objReg;
81  csRef<iEngine> engine;
82 };
83 
84 /***************************************************************************/
89 class psAEdge
90 {
91 public:
92  psAEdge(psANode* neighbour, float cost);
93  psANode* neighbour;
94  float cost;
96 };
97 
98 /* Empty subclass that unites psACluster and psAHierarchyNode
99 class psAHierarchyNode
100 {
101 };*/
102 
103 /*************************************************************/
107 class psACluster
108 {
109 public:
110  bool Contains(psANode* node);
111  bool AddNode(psANode* node);
112  void DeleteNode(psANode* node);
113 
114  void GrowClusterFrom(psANode* start, int level, csList <psANode*> &unassigned);
115 
116 protected:
117 
118  int CalcFreeBorderSize(int level);
119 
120  csList <psANode*> parts;
121  csList <psANode*> exits;
122 };
123 
124 /*******************************************************************************/
130 enum psAState {AState_open, AState_closed, AState_unknown};
131 
132 
133 /************************************************************************/
141 class psANode
142 {
143 public:
144  psANode();
145  ~psANode();
146  psANode(const csVector3 &point);
147  psANode(const csVector3 &point, iSector* sector);
148 
151  void EnsureNodeIsInited(int currARunNum);
152 
153  void SetBestPrev(psANode* bestPrev, float cost);
154  void CalcHeur(psANode* goal);
155  void AddEdge(psANode* neighbour, float cost, int level, bool bidirectional=true);
156  void DeleteEdges(psANode* neighbour);
157 
158  void ConnectToPoly(psWalkPoly* poly, bool bidirectional);
159 
160  bool ShouldBePruned(psAMap &map);
161 
162  void SaveToString(csString &str);
163 
164  //wpMap can be NULL
165  void LoadBasicsFromXML(iDocumentNode* node, psWalkPolyMap* wpMap, iEngine* engine);
166  void LoadEdgesFromXML(iDocumentNode* node, psAMap &map);
167 
168  void DumpJS();
169 
170  psWalkPoly* GetSomePoly();
171 
172  //works on 0th level only !
173  void DeleteConnectionsFromNeighbours();
174  void RestoreConnectionsFromNeighbours();
175 
176  //on 0th level
177  float GetEdgeCost(psANode* neighbour);
178 
179  csVector3 point;
180  iSector* sector;
181  csArray<csArray<psAEdge> > edges;
182  psACluster* cluster;
183 
184  int id;
185  psWalkPoly* poly1, * poly2;
186  static int nextID;
187 
188  /* The following variables are temporary A* state valid for one A* run only */
189  int ARunNum;
190  psAState state;
191  psANode* prevInOpen, * nextInOpen;
192  psANode* bestPrev;
193  float bestCost;
194  float heur;
195  float total;
196 };
197 
198 class psAPath
199 {
200 public:
201  psAPath();
202  psAPath(psPFMaps* regions);
203 
204  void SetMaps(psPFMaps* maps);
205 
206  void AddNodeToFront(psANode* node);
207 
208  // resets
209  void SetDest(const csVector3 &dest);
210  csVector3 GetDest()
211  {
212  return dest;
213  }
214 
215  void CalcLocalDest(const csVector3 &currPoint, iSector* currSector, csVector3 &localDest);
216 
217  void Dump();
218  void ResetNodes();
219 
221  float CalcCost();
222 
223 protected:
224 
225  csList<psANode*>::Iterator FindDirectNode(const csVector3 &currPoint);
226  void InsertSubpath(psAPath &subpath);
227  csString GetRegionNameOfSector(const csString &sectorName);
228  psANode* GetNthNode(int n);
229  void RandomizeFirst(const csVector3 &currPoint);
230 
231  bool CalcLocalDestFromLocalPath(const csVector3 &currPoint, csVector3 &localDest);
232 
233  psANode* FindNearbyANode(const csVector3 &pos, iSector* sector);
234 
235  csVector3 dest;
236  csList<psANode*> nodes;
237  psPFMaps* maps;
238 };
239 
241 class psAMap
242 {
243 public:
244  psAMap(iObjectRegistry* objReg);
245  psAMap();
246 
247  void SetObjReg(iObjectRegistry* objReg)
248  {
249  this->objReg=objReg;
250  }
251  void AddNode(psANode* node);
252 
253  void PruneSuperfluous();
254 
257  void BuildHierarchy();
258 
262  bool RunA(psANode* start, psANode* goal, int level, int maxExpands, psAPath &path);
263 
264  void DumpJS();
265 
266  bool LoadFromString(const csString &str, psWalkPolyMap* wpMap);
267  bool LoadFromFile(const csString &path, psWalkPolyMap* wpMap);
268  void SaveToString(csString &str);
269  bool SaveToFile(const csString &path);
270 
271  psANode* FindNode(int id);
272 
273 protected:
274  csList <psANode*> nodes;
275  csList <psACluster*> clusters;
276  iObjectRegistry* objReg;
277  csRef<iVFS> vfs;
278 };
279 
282 #endif
283 
284 #endif