Planeshift

celgraph.h

Go to the documentation of this file.
00001 /*    Crystal Space Entity Layer
00002     Copyright (C) 2006 by Jorrit Tyberghein
00003   
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008   
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013   
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef __CEL_TOOLS_CELGRAPH__
00020 #define __CEL_TOOLS_CELGRAPH__
00021 
00022 #include "csutil/csobject.h"
00023 #include "csutil/util.h"
00024 #include "csutil/hash.h"
00025 #include "csutil/redblacktree.h"
00026 #include "csutil/weakrefarr.h"
00027 #include "csutil/stringarray.h"
00028 #include "csutil/eventhandlers.h"
00029 #include "iutil/comp.h"
00030 #include "iutil/eventh.h"
00031 #include "iutil/eventq.h"
00032 #include "ivaria/conin.h"
00033 #include "ivaria/conout.h"
00034 
00035 #include "tools/celgraph.h"
00036 
00037 #include <stdio.h>
00038 #include <string.h>
00039 
00043 class celEdge : public scfImplementation1<
00044                    celEdge, iCelEdge>
00045 {
00046  private:
00047   csRef<iCelNode> successor;
00048   bool state;
00049   float weight;
00050 
00051  public:
00052   celEdge ();
00053   virtual ~celEdge ();
00054   virtual void SetState (bool open);
00055   virtual void SetSuccessor (iCelNode* node);
00056   virtual bool GetState ();
00057   virtual iCelNode* GetSuccessor ();
00058   virtual float GetWeight () const;
00059   virtual void SetWeight (float weight);
00060 };
00061 
00062 class celNode : public scfImplementation1<
00063                    celNode, iCelNode>
00064 {
00065  private:
00066   csRefArray<iCelEdge> edges;
00067   csRef<iMapNode> map_node;
00068   csRef<iCelNode> parent;
00069   float heuristic;
00070   float cost;
00071   csString name;
00072 
00073  public:
00074   celNode ();
00075   virtual ~celNode ();
00076   virtual size_t AddSuccessor (iCelNode* node, bool state);
00077   virtual void SetMapNode (iMapNode* node);
00078   virtual void SetParent (iCelNode* par);
00079   virtual void SetName (const char* n);
00080   virtual void Heuristic (float cost, iCelNode* goal);
00081   virtual iMapNode* GetMapNode ();
00082   virtual csVector3 GetPosition ();
00083   virtual const char* GetName ();
00084   virtual iCelNode* GetParent () {return parent;}
00085   virtual csArray<iCelNode*> GetSuccessors ();
00086   virtual csArray<iCelNode*> GetAllSuccessors ();
00087   virtual float GetHeuristic () {return heuristic;};
00088   virtual float GetCost () {return cost;};
00089   virtual size_t GetEdgeCount ()
00090   { return edges.GetSize(); }
00091   virtual iCelEdge* GetEdge (size_t idx)
00092   { return edges.Get(idx); }
00093   virtual void RemoveEdge (size_t idx);
00094   virtual size_t AddSuccessor (iCelNode* node, bool state, float weight);
00095   virtual csRefArray<iCelEdge>* GetEdges ();
00096 };
00097 
00102 class celPath : public scfImplementationExt2<
00103 celPath, csObject, iCelPath, iComponent>
00104 {
00105  private:
00106   iObjectRegistry* object_reg;
00107   size_t cur_node;
00108   csRefArray<iMapNode> nodes;
00109   
00110  public:
00111   celPath (iBase* parent);
00112   virtual ~celPath ();
00113   virtual iObject* QueryObject () { return this; }
00114   virtual void AddNode (iMapNode* node);
00115   virtual void InsertNode (size_t pos, iMapNode* node);
00116   virtual iMapNode* Next ();
00117   virtual iMapNode* Previous ();
00118   virtual iMapNode* Current ();
00119   virtual csVector3 CurrentPosition ();
00120   virtual iSector* CurrentSector ();
00121   virtual bool HasNext ();
00122   virtual bool HasPrevious ();
00123   virtual void Restart ();
00124   virtual void Clear ();
00125   virtual bool Initialize (iObjectRegistry* object_reg);
00126   virtual iMapNode* GetFirst ();
00127   virtual iMapNode* GetLast ();
00128   virtual void Invert ();
00129   virtual size_t GetNodeCount ()
00130   { return nodes.GetSize(); }
00131 };
00132 
00136 class celGraph : public scfImplementationExt2<
00137   celGraph, csObject, iCelGraph, iComponent>
00138 {
00139 private:
00140   iObjectRegistry* object_reg;
00141    csRefArray <iCelNode> nodes;
00142 
00143 public: 
00144   celGraph (iBase* parent);
00145   virtual ~celGraph ();
00146   virtual iObject* QueryObject () { return this; }
00147   virtual bool Initialize (iObjectRegistry* object_reg);
00148   virtual iCelNode* CreateNode (const char *name, csVector3 &pos);
00149   virtual size_t AddNode (iCelNode* node);
00150   virtual void AddEdge (iCelNode* from, iCelNode* to, bool state);
00151   virtual bool AddEdgeByNames (const char* from, const char* to, bool state);
00152   virtual iCelNode* GetClosest (csVector3 position);
00153   virtual bool ShortestPath (iCelNode* from, iCelNode* goal, iCelPath* path);
00154   virtual iCelNode* RandomPath (iCelNode* from, int distance, iCelPath* path);
00155   virtual size_t GetNodeCount ()
00156   { return nodes.GetSize(); }
00157   virtual iCelNode* GetNode (size_t idx)
00158   { return nodes.Get(idx); }
00159   virtual void RemoveNode (size_t idx);
00160   virtual void RemoveEdge (iCelNode* from, size_t idx);
00161   virtual size_t AddEdge (iCelNode* from, iCelNode* to, bool state, float weight);
00162   virtual iCelNode* CreateEmptyNode (size_t& index);
00163   virtual bool ShortestPath2 (iCelNode* from, iCelNode* goal, iCelPath* path);
00164 };
00165 
00166 #endif //__CEL_TOOLS_CELGRAPH__ 
00167