Planeshift

pspath.h

Go to the documentation of this file.
00001 /*
00002  * pspath.h
00003  *
00004  * Copyright (C) 2007 Atomic Blue ([email protected], http://www.atomicblue.org) 
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 #ifndef __PSPATH_H__
00020 #define __PSPATH_H__
00021 
00022 #include <csutil/weakref.h>
00023 #include <csutil/parray.h>
00024 #include <csutil/list.h>
00025 #include <igeom/path.h>
00026 #include <csgeom/vector3.h>
00027 
00028 #include <idal.h>
00029 #include <psstdint.h>
00030 
00031 class Waypoint;
00032 class psPathAnchor;
00033 class psWorld;
00034 struct iMovable;
00035 struct iSector;
00036 struct iEngine;
00037 class Location;
00038 class psString;
00039 class psPath;
00040 
00048 class iEffectIDAllocator
00049 {
00050 public:
00051     virtual uint32_t GetEffectID() = 0;
00052 };
00053 
00054 
00058 class psPathPoint
00059 {
00060     friend class psPathNetwork;
00061     friend class psPath;
00062     friend class psLinearPath;
00063     friend class psPathAnchor;
00064 public:
00065     psPathPoint(psPath* parentPath);
00066 
00068     bool Load(iResultRow& row, iEngine *engine);
00069     
00071     bool Create(iDataConnection * db, int pathID);
00072 
00074     bool Remove(iDataConnection * db);
00075 
00077     bool UpdatePrevPointId(iDataConnection * db, int prevPointId);
00078     
00080     bool Adjust(iDataConnection * db, csVector3 & pos, csString sector);
00081     
00084     void Adjust(csVector3 & pos, csString sector);
00085     
00088     void Adjust(csVector3 & pos, iSector* sector);
00089 
00091     void SetPrevious(int previous) { prevPointId = previous; }
00092 
00094     int GetPreviousPointId() const { return prevPointId; }
00095 
00096     int GetID() const { return id; }
00097     void SetID(int id) { this->id = id; }
00098 
00099     const csVector3& GetPosition() const { return pos; }
00100     iSector * GetSector(iEngine *engine);
00101     iSector * GetSector(iEngine *engine) const;
00102     float GetRadius();
00103     float GetRadius() const;
00104 
00105     void SetWaypoint(Waypoint* waypoint);
00106 
00107     Waypoint* GetWaypoint();
00108 
00111     int GetID();
00112 
00116     csString GetName();
00117 
00121     uint32_t GetEffectID(iEffectIDAllocator* allocator);
00122 
00125     psPath* GetPath() const;
00126 
00129     int GetPathIndex() const;
00130     
00131 private:
00133     // Database data
00134     int                    id;
00135     int                    prevPointId;
00136     csVector3              pos;
00137     csString               sectorName;       
00141 
00142     // Internal data
00143     float                  radius;           
00144 
00145     csWeakRef<iSector>     sector;           
00146     float                  startDistance[2]; 
00147     Waypoint*              waypoint;         
00148     uint32_t               effectID;         
00149     psPath*                path;             
00150 };
00151 
00152 class psPath
00153 {
00154     friend class psPathAnchor;
00155 public:
00156     typedef enum {
00157         FORWARD,
00158         REVERSE
00159     } Direction;
00160 
00161     typedef csPDelArray<psPathPoint> PathPointArray;
00162             
00163     int                id;
00164     csString           name;
00165     Waypoint*          start;   
00166     Waypoint*          end;     
00167     PathPointArray     points;  
00168 
00170     bool                   oneWay;
00171     bool                   noWander;
00172     bool                   teleport; 
00173     
00174     bool                   precalculationValid;
00175     float                  totalDistance;
00176     
00177     psPath(csString name, Waypoint * wp1, Waypoint * wp2, psString flagStr);
00178     psPath(int pathID, csString name, psString flagStr);
00179     
00180     virtual ~psPath();
00181 
00183     bool Load(iDataConnection * db, iEngine *engine);
00184 
00186     bool Create(iDataConnection *db);
00187 
00189     bool Adjust(iDataConnection * db, int index, csVector3 & pos, csString sector);
00190 
00192     psPathPoint* AddPoint(Location * loc, bool first = false);
00193 
00195     psPathPoint* AddPoint(iDataConnection *db, const csVector3& pos, const char * sectorName, bool first = false);
00196     
00198     psPathPoint* AddPoint(const csVector3& pos, float radius, const char * sectorName, bool first = false);
00199 
00201     psPathPoint* InsertPoint(iDataConnection *db, int index, const csVector3& pos, const char * sectorName);
00202 
00204     bool RemovePoint(iDataConnection *db, int index);
00205 
00207     bool RemovePoint(int index);
00208     
00210     bool RemovePoint(iDataConnection *db, psPathPoint* point);
00211 
00213     bool UpdatePrevPointIndexes(iDataConnection* db);
00214     
00216     void SetStart(Waypoint * wp);
00217 
00219     void SetEnd(Waypoint * wp);
00220 
00222     virtual void Precalculate(psWorld * world, iEngine *engine, bool forceUpdate = false);
00223 
00225     virtual float Distance(psWorld * world, iEngine *engine,const csVector3& pos, const iSector* sector, int * index = NULL, float * fraction = NULL) const;
00226 
00228     virtual float DistancePoint(psWorld * world, iEngine *engine,const csVector3& pos, const iSector* sector, int * index = NULL, bool include_ends = false) const;
00229 
00231     psPathPoint* GetStartPoint(Direction direction);
00232     
00234     psPathPoint* GetEndPoint(Direction direction);
00235 
00237     Waypoint* GetStartWaypoint(Direction direction);
00238     
00240     Waypoint* GetEndWaypoint(Direction direction);
00241     
00243     csVector3 GetEndPos(Direction direction);
00244     
00246     float GetEndRot(Direction direction);
00247     
00249     iSector* GetEndSector(iEngine * engine, Direction direction);
00250 
00251     
00253     virtual psPathAnchor* CreatePathAnchor();
00254 
00256     virtual int GetNumPoints () const { return (int)points.GetSize(); }
00257 
00259     virtual const char* GetName() const { return name.GetDataSafe(); }
00260 
00262     int GetID() const { return id; }
00263 
00265     bool Rename(iDataConnection * db,const char* name);
00266     
00268     void Rename(const char* name);
00269 
00271     virtual float GetLength(psWorld * world, iEngine *engine, int index);
00272 
00274     virtual float GetLength(psWorld * world, iEngine *engine);
00275 
00277     float CalculateIncidentAngle(csVector3& pos, csVector3& dest);
00278     
00280     csString GetFlags() const;
00281 
00283     void SetFlags(const psString & flagStr);
00284 
00286     bool SetFlag(iDataConnection * db, const csString &flagstr, bool enable);
00287 
00289     bool SetFlag(const csString &flagstr, bool enable);
00290 
00292     size_t FindPointsInSector(iEngine * engine, iSector *sector, csList<psPathPoint*>& list);
00293 
00295     psPathPoint* FindPoint(int id);
00296 
00298     int FindPointIndex(int id) const;
00299 
00301     int FindPointIndex(const psPathPoint* point) const;
00302     
00303     // Get Path Point by index
00304     psPathPoint* GetPoint(int index);
00305 
00306     // Get Path Point by index
00307     const psPathPoint* GetPoint(int index) const;
00308     
00309 protected:
00311     virtual void PrecalculatePath(psWorld * world, iEngine *engine) = 0;
00312 
00314     virtual void GetInterpolatedPosition (int index, float fraction, csVector3& pos) = 0;
00315 
00317     virtual void GetInterpolatedUp (int index, float fraction, csVector3& up) = 0;
00318     
00320     virtual void GetInterpolatedForward (int index, float fraction, csVector3& forward) = 0;
00321 };
00322 
00323 class psLinearPath: public psPath
00324 {
00325 public:
00326     psLinearPath(csString name, Waypoint * wp1, Waypoint * wp2, psString flagStr);
00327     psLinearPath(int pathID, csString name, psString flagStr);
00328     virtual ~psLinearPath(){};
00329 
00330 protected:
00332     virtual void PrecalculatePath(psWorld * world, iEngine *engine);
00333 
00335     virtual void GetInterpolatedPosition (int index, float fraction, csVector3& pos);
00336 
00338     virtual void GetInterpolatedUp (int index, float fraction, csVector3& up);
00339     
00341     virtual void GetInterpolatedForward (int index, float fraction, csVector3& forward);
00342     
00343 private:
00344     csArray<float> dx;
00345     csArray<float> dy;
00346     csArray<float> dz;
00347 };
00348 
00349 // Warning: This is BROKEN and does not work across sectors.
00350 class psPathAnchor
00351 {
00352 public:
00353     psPathAnchor(psPath * path);
00354     virtual ~psPathAnchor() {}
00355 
00357     virtual bool CalculateAtDistance(psWorld * world, iEngine *engine, float distance, psPath::Direction direction);
00358 
00360     virtual void GetInterpolatedPosition (csVector3& pos);
00361 
00363     virtual void GetInterpolatedUp (csVector3& up);
00364     
00366     virtual void GetInterpolatedForward (csVector3& forward);
00367 
00370     virtual bool Extrapolate(psWorld * world, iEngine *engine, float delta, psPath::Direction direction, iMovable* movable);
00371 
00372 
00374     float GetDistance(){ return pathDistance; }
00376     int GetCurrentAtIndex() { return currentAtIndex; }
00378     psPath::Direction GetCurrentAtDirection() { return currentAtDirection; }
00380     float GetCurrentAtFraction(){ return currentAtFraction; }
00381 
00382 private:
00383     psPath * path;
00384     // Internal non reentrant data leagal after calcuateAt operation
00385     int                    currentAtIndex;
00386     psPath::Direction      currentAtDirection;
00387     float                  currentAtFraction;
00388 
00389     float                  pathDistance;    
00390 };
00391 
00394 #endif