Planeshift
celhpf.h
Go to the documentation of this file.
1 /*
2  Crystal Space Entity Layer
3  Copyright (C) 2010 by Leonardo Rodrigo Domingues
4  Copyright (C) 2011 by Matthieu Kraus
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public
17  License along with this library; if not, write to the Free
18  Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20 
21 #ifndef __CEL_HPF__
22 #define __CEL_HPF__
23 
24 #include <cssysdef.h>
25 #include <csgeom/math3d.h>
26 #include <csgeom/vector3.h>
27 #include <cstool/mapnode.h>
28 #include <csutil/csstring.h>
29 #include <csutil/hash.h>
30 #include <csutil/scf_implementation.h>
31 #include <iengine/sector.h>
32 #include <iutil/comp.h>
33 #include <iutil/document.h>
34 #include <iutil/objreg.h>
35 #include <ivaria/mapnode.h>
36 #include <tools/celgraph.h>
37 #include <tools/celnavmesh.h>
38 #include <tools/celhpf.h>
39 #include "celnavmesh.h"
40 
42 {
43 
44 
48 class celHPath : public scfImplementation1<celHPath, iCelHPath>
49 {
50 private:
51  csRef<iCelPath> hlPath; // High level path
52  csHash<csRef<iCelNavMesh>, csPtrKey<iSector> >& navMeshes;
53  csArray<csRef<iCelNavMeshPath> > llPaths; // Low level paths
54  size_t currentllPosition; // Current position for low level paths array
55  csRef<iMapNode> currentNode;
56  csPtrKey<iSector> currentSector;
57  csRef<iMapNode> firstNode; // Optimization for celHPath::GetFirst
58  csRef<iMapNode> lastNode; // Optimization for celHPath::GetLast
59  csArray<csSimpleRenderMesh*>* debugMeshes;
60  bool reverse;
61  float length;
62  float advanced;
63 
64  virtual bool HasNextInternal (bool rev) const;
65  virtual iMapNode* NextInternal (bool rev);
66 
67 public:
68  celHPath (csHash<csRef<iCelNavMesh>, csPtrKey<iSector> >& navMeshes);
69  virtual ~celHPath ();
70 
71  void Initialize(iCelPath* highLevelPath);
72 
73  // API
74  virtual bool HasNext () const;
75  virtual bool HasPrevious () const;
76  virtual iMapNode* Next ();
77  virtual iMapNode* Previous ();
78  virtual iMapNode* Current ();
79  virtual iMapNode* GetFirst ();
80  virtual iMapNode* GetLast ();
81  virtual void Invert ();
82  virtual void Restart ();
83  virtual float GetDistance () const;
84  virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes ();
85 };
86 
87 
88 
92 class celHNavStruct : public scfImplementation1<celHNavStruct, iCelHNavStruct>
93 {
94 private:
95  iObjectRegistry* objectRegistry;
96  csRef<iCelNavMeshParams> parameters;
97  csHash<csRef<iCelNavMesh>, csPtrKey<iSector> > navMeshes;
98  csRef<iCelGraph> hlGraph; // High level graph
99  csRef<celHPath> path;
100  csArray<csSimpleRenderMesh*>* debugMeshes;
101 
102  // Helpers for the SaveToFile method
103  void SaveParameters (iDocumentNode* node);
104  void SaveNavMeshes (iDocumentNode* node, iVFS* vfs);
105  void SaveHighLevelGraph (iDocumentNode* node1, iDocumentNode* node2);
106 
107 public:
108  celHNavStruct (const iCelNavMeshParams* params, iObjectRegistry* objectRegistry);
109  virtual ~celHNavStruct ();
110 
111  void AddNavMesh(iCelNavMesh* navMesh);
112  bool BuildHighLevelGraph();
113  void SetHighLevelGraph(iCelGraph* graph);
114 
115  // API
116  virtual iCelHPath* ShortestPath (const csVector3& from, iSector* fromSector, const csVector3& goal,
117  iSector* goalSector);
118  virtual iCelHPath* ShortestPath (iMapNode* from, iMapNode* goal);
119  virtual bool Update (const csBox3& boundingBox, iSector* sector = 0);
120  virtual bool Update (const csOBB& boundingBox, iSector* sector = 0);
121  virtual bool SaveToFile (iVFS* vfs, const char* directory);
122  virtual const iCelNavMeshParams* GetNavMeshParams () const;
123  virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes (iSector* sector = 0);
124  virtual csArray<csSimpleRenderMesh*>* GetAgentDebugMeshes (const csVector3& pos, int red, int green,
125  int blue, int alpha);
126  virtual void ResetAgentDebugMeshes ();
127 };
128 
129 
130 
134 class celHNavStructBuilder : public scfImplementation2<celHNavStructBuilder, iCelHNavStructBuilder, iComponent>
135 {
136 private:
137  iObjectRegistry* objectRegistry;
138  csRef<iCelNavMeshParams> parameters;
139  csRefArray<iSector> sectors;
140  csHash<csRef<iCelNavMeshBuilder>, csPtrKey<iSector> > builders;
141  csRef<celHNavStruct> navStruct;
142 
143  bool InstantiateNavMeshBuilders();
144 
145  // Helpers for LoadFromFile()
146  bool ParseParameters (iDocumentNode* node, iCelNavMeshParams* params);
147  bool ParseMeshes (iDocumentNode* node, csHash<csRef<iSector>, const char*>& sectors,
148  celHNavStruct* navStruct, iVFS* vfs, iCelNavMeshParams* params);
149  bool ParseGraph (iDocumentNode* node, iCelGraph* graph, csHash<csRef<iSector>, const char*>& sectors);
150 
151 public:
152  celHNavStructBuilder (iBase* parent);
153  virtual ~celHNavStructBuilder ();
154  virtual bool Initialize (iObjectRegistry*);
155 
156  // API
157  virtual bool SetSectors (csRefArray<iSector>* sectorList);
158  virtual iCelHNavStruct* BuildHNavStruct ();
159  virtual iCelHNavStruct* LoadHNavStruct (iVFS* vfs, const char* directory);
160  virtual const iCelNavMeshParams* GetNavMeshParams () const;
161  virtual void SetNavMeshParams (const iCelNavMeshParams* parameters);
162 };
163 
164 }
165 CS_PLUGIN_NAMESPACE_END(celNavMesh)
166 
167 #endif // __CEL_HPF__
void void Initialize(iObjectRegistry *object_reg)
CS_PLUGIN_NAMESPACE_BEGIN(celNavMesh)
Definition: celhpf.h:41