Planeshift

celhpf.h

Go to the documentation of this file.
00001 /*
00002     Crystal Space Entity Layer
00003     Copyright (C) 2010 by Leonardo Rodrigo Domingues
00004     Copyright (C) 2011 by Matthieu Kraus
00005   
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010   
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015   
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the Free
00018     Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #ifndef __CEL_HPF__
00022 #define __CEL_HPF__
00023 
00024 #include <cssysdef.h>
00025 #include <csgeom/math3d.h>
00026 #include <csgeom/vector3.h>
00027 #include <cstool/mapnode.h>
00028 #include <csutil/csstring.h>
00029 #include <csutil/hash.h>
00030 #include <csutil/scf_implementation.h>
00031 #include <iengine/sector.h>
00032 #include <iutil/comp.h>
00033 #include <iutil/document.h>
00034 #include <iutil/objreg.h>
00035 #include <ivaria/mapnode.h>
00036 #include <tools/celgraph.h>
00037 #include <tools/celnavmesh.h>
00038 #include <tools/celhpf.h>
00039 #include "celnavmesh.h"
00040 
00041 CS_PLUGIN_NAMESPACE_BEGIN(celNavMesh)
00042 {
00043 
00044 
00048 class celHPath : public scfImplementation1<celHPath, iCelHPath>
00049 {
00050 private:
00051   csRef<iCelPath> hlPath; // High level path
00052   csHash<csRef<iCelNavMesh>, csPtrKey<iSector> >& navMeshes;
00053   csArray<csRef<iCelNavMeshPath> > llPaths; // Low level paths
00054   size_t currentllPosition; // Current position for low level paths array
00055   csRef<iMapNode> currentNode;
00056   csPtrKey<iSector> currentSector;
00057   csRef<iMapNode> firstNode; // Optimization for celHPath::GetFirst
00058   csRef<iMapNode> lastNode; // Optimization for celHPath::GetLast
00059   csArray<csSimpleRenderMesh*>* debugMeshes;
00060   bool reverse;
00061   float length;
00062   float advanced;
00063 
00064   virtual bool HasNextInternal (bool rev) const;
00065   virtual iMapNode* NextInternal (bool rev);
00066 
00067 public:
00068   celHPath (csHash<csRef<iCelNavMesh>, csPtrKey<iSector> >& navMeshes);
00069   virtual ~celHPath ();
00070 
00071   void Initialize(iCelPath* highLevelPath);
00072 
00073   // API
00074   virtual bool HasNext () const;
00075   virtual bool HasPrevious () const;
00076   virtual iMapNode* Next ();
00077   virtual iMapNode* Previous ();
00078   virtual iMapNode* Current ();
00079   virtual iMapNode* GetFirst ();
00080   virtual iMapNode* GetLast ();
00081   virtual void Invert ();
00082   virtual void Restart ();
00083   virtual float GetDistance () const;
00084   virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes ();
00085 };
00086 
00087 
00088 
00092 class celHNavStruct : public scfImplementation1<celHNavStruct, iCelHNavStruct>
00093 {
00094 private:
00095   iObjectRegistry* objectRegistry;
00096   csRef<iCelNavMeshParams> parameters;
00097   csHash<csRef<iCelNavMesh>, csPtrKey<iSector> > navMeshes;
00098   csRef<iCelGraph> hlGraph; // High level graph
00099   csRef<celHPath> path;
00100   csArray<csSimpleRenderMesh*>* debugMeshes;
00101 
00102   // Helpers for the SaveToFile method
00103   void SaveParameters (iDocumentNode* node);
00104   void SaveNavMeshes (iDocumentNode* node, iVFS* vfs);
00105   void SaveHighLevelGraph (iDocumentNode* node1, iDocumentNode* node2);
00106 
00107 public:
00108   celHNavStruct (const iCelNavMeshParams* params, iObjectRegistry* objectRegistry);
00109   virtual ~celHNavStruct ();
00110 
00111   void AddNavMesh(iCelNavMesh* navMesh);
00112   bool BuildHighLevelGraph();
00113   void SetHighLevelGraph(iCelGraph* graph);
00114 
00115   // API
00116   virtual iCelHPath* ShortestPath (const csVector3& from, iSector* fromSector, const csVector3& goal,
00117                                    iSector* goalSector);
00118   virtual iCelHPath* ShortestPath (iMapNode* from, iMapNode* goal);
00119   virtual bool Update (const csBox3& boundingBox, iSector* sector = 0);
00120   virtual bool Update (const csOBB& boundingBox, iSector* sector = 0);
00121   virtual bool SaveToFile (iVFS* vfs, const char* directory);
00122   virtual const iCelNavMeshParams* GetNavMeshParams () const;
00123   virtual csArray<csSimpleRenderMesh*>* GetDebugMeshes (iSector* sector = 0);
00124   virtual csArray<csSimpleRenderMesh*>* GetAgentDebugMeshes (const csVector3& pos, int red, int green, 
00125                                                            int blue, int alpha);
00126   virtual void ResetAgentDebugMeshes ();
00127 };
00128 
00129 
00130 
00134 class celHNavStructBuilder : public scfImplementation2<celHNavStructBuilder, iCelHNavStructBuilder, iComponent>
00135 {
00136 private:
00137   iObjectRegistry* objectRegistry;
00138   csRef<iCelNavMeshParams> parameters;
00139   csRefArray<iSector> sectors;
00140   csHash<csRef<iCelNavMeshBuilder>, csPtrKey<iSector> > builders;
00141   csRef<celHNavStruct> navStruct;
00142 
00143   bool InstantiateNavMeshBuilders();
00144 
00145   // Helpers for LoadFromFile()
00146   bool ParseParameters (iDocumentNode* node, iCelNavMeshParams* params);
00147   bool ParseMeshes (iDocumentNode* node, csHash<csRef<iSector>, const char*>& sectors, 
00148                     celHNavStruct* navStruct, iVFS* vfs, iCelNavMeshParams* params);
00149   bool ParseGraph (iDocumentNode* node, iCelGraph* graph, csHash<csRef<iSector>, const char*>& sectors);
00150 
00151 public:
00152   celHNavStructBuilder (iBase* parent);
00153   virtual ~celHNavStructBuilder ();
00154   virtual bool Initialize (iObjectRegistry*);
00155 
00156   // API
00157   virtual bool SetSectors (csRefArray<iSector>* sectorList);
00158   virtual iCelHNavStruct* BuildHNavStruct ();
00159   virtual iCelHNavStruct* LoadHNavStruct (iVFS* vfs, const char* directory);
00160   virtual const iCelNavMeshParams* GetNavMeshParams () const;
00161   virtual void SetNavMeshParams (const iCelNavMeshParams* parameters);
00162 };
00163 
00164 }
00165 CS_PLUGIN_NAMESPACE_END(celNavMesh)
00166 
00167 #endif // __CEL_HPF__