Planeshift
pspath.h
Go to the documentation of this file.
1 /*
2  * pspath.h
3  *
4  * Copyright (C) 2007 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 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 __PSPATH_H__
20 #define __PSPATH_H__
21 
22 #include <csutil/weakref.h>
23 #include <csutil/parray.h>
24 #include <csutil/list.h>
25 #include <igeom/path.h>
26 #include <csgeom/vector3.h>
27 
28 #include <idal.h>
29 #include <psstdint.h>
30 
31 class Waypoint;
32 class psPathAnchor;
33 class psWorld;
34 struct iMovable;
35 struct iSector;
36 struct iEngine;
37 class Location;
38 class psString;
39 class psPath;
40 
49 {
50 public:
51  virtual uint32_t GetEffectID() = 0;
52 };
53 
54 
59 {
60  friend class psPathNetwork;
61  friend class psPath;
62  friend class psLinearPath;
63  friend class psPathAnchor;
64 public:
65  psPathPoint(psPath* parentPath);
66 
68  bool Load(iResultRow& row, iEngine *engine);
69 
71  bool Create(iDataConnection * db, int pathID);
72 
74  bool Remove(iDataConnection * db);
75 
77  bool UpdatePrevPointId(iDataConnection * db, int prevPointId);
78 
80  bool Adjust(iDataConnection * db, csVector3 & pos, csString sector);
81 
84  void Adjust(csVector3 & pos, csString sector);
85 
88  void Adjust(csVector3 & pos, iSector* sector);
89 
91  void SetPrevious(int previous) { prevPointId = previous; }
92 
94  int GetPreviousPointId() const { return prevPointId; }
95 
96  int GetID() const { return id; }
97  void SetID(int id) { this->id = id; }
98 
99  const csVector3& GetPosition() const { return pos; }
100  iSector * GetSector(iEngine *engine);
101  iSector * GetSector(iEngine *engine) const;
102  float GetRadius();
103  float GetRadius() const;
104 
105  void SetWaypoint(Waypoint* waypoint);
106 
107  Waypoint* GetWaypoint();
108 
111  int GetID();
112 
116  csString GetName();
117 
121  uint32_t GetEffectID(iEffectIDAllocator* allocator);
122 
125  psPath* GetPath() const;
126 
129  int GetPathIndex() const;
130 
131 private:
133  // Database data
134  int id;
135  int prevPointId;
136  csVector3 pos;
137  csString sectorName;
141  // Internal data
143  float radius;
144  csWeakRef<iSector> sector;
146  float startDistance[2];
147  Waypoint* waypoint;
148  uint32_t effectID;
149  psPath* path;
150 };
151 
152 class psPath
153 {
154  friend class psPathAnchor;
155 public:
156  typedef enum {
158  REVERSE
159  } Direction;
160 
161  typedef csPDelArray<psPathPoint> PathPointArray;
162 
163  int id;
164  csString name;
167  PathPointArray points;
168 
170  bool oneWay;
171  bool noWander;
172  bool teleport;
173 
176 
177  psPath(csString name, Waypoint * wp1, Waypoint * wp2, psString flagStr);
178  psPath(int pathID, csString name, psString flagStr);
179 
180  virtual ~psPath();
181 
183  bool Load(iDataConnection * db, iEngine *engine);
184 
186  bool Create(iDataConnection *db);
187 
189  bool Adjust(iDataConnection * db, int index, csVector3 & pos, csString sector);
190 
192  psPathPoint* AddPoint(Location * loc, bool first = false);
193 
195  psPathPoint* AddPoint(iDataConnection *db, const csVector3& pos, const char * sectorName, bool first = false);
196 
198  psPathPoint* AddPoint(const csVector3& pos, float radius, const char * sectorName, bool first = false);
199 
201  psPathPoint* InsertPoint(iDataConnection *db, int index, const csVector3& pos, const char * sectorName);
202 
204  bool RemovePoint(iDataConnection *db, int index);
205 
207  bool RemovePoint(int index);
208 
210  bool RemovePoint(iDataConnection *db, psPathPoint* point);
211 
213  bool UpdatePrevPointIndexes(iDataConnection* db);
214 
216  void SetStart(Waypoint * wp);
217 
219  void SetEnd(Waypoint * wp);
220 
222  virtual void Precalculate(psWorld * world, iEngine *engine, bool forceUpdate = false);
223 
225  virtual float Distance(psWorld * world, iEngine *engine,const csVector3& pos, const iSector* sector, int * index = NULL, float * fraction = NULL) const;
226 
228  virtual float DistancePoint(psWorld * world, iEngine *engine,const csVector3& pos, const iSector* sector, int * index = NULL, bool include_ends = false) const;
229 
231  psPathPoint* GetStartPoint(Direction direction);
232 
234  psPathPoint* GetEndPoint(Direction direction);
235 
237  Waypoint* GetStartWaypoint(Direction direction);
238 
240  Waypoint* GetEndWaypoint(Direction direction);
241 
243  csVector3 GetEndPos(Direction direction);
244 
246  float GetEndRot(Direction direction);
247 
249  iSector* GetEndSector(iEngine * engine, Direction direction);
250 
251 
253  virtual psPathAnchor* CreatePathAnchor();
254 
256  virtual int GetNumPoints () const { return (int)points.GetSize(); }
257 
259  virtual const char* GetName() const { return name.GetDataSafe(); }
260 
262  int GetID() const { return id; }
263 
265  bool Rename(iDataConnection * db,const char* name);
266 
268  void Rename(const char* name);
269 
271  virtual float GetLength(psWorld * world, iEngine *engine, int index);
272 
274  virtual float GetLength(psWorld * world, iEngine *engine);
275 
277  float CalculateIncidentAngle(csVector3& pos, csVector3& dest);
278 
280  csString GetFlags() const;
281 
283  void SetFlags(const psString & flagStr);
284 
286  bool SetFlag(iDataConnection * db, const csString &flagstr, bool enable);
287 
289  bool SetFlag(const csString &flagstr, bool enable);
290 
292  size_t FindPointsInSector(iEngine * engine, iSector *sector, csList<psPathPoint*>& list);
293 
295  psPathPoint* FindPoint(int id);
296 
298  int FindPointIndex(int id) const;
299 
301  int FindPointIndex(const psPathPoint* point) const;
302 
303  // Get Path Point by index
304  psPathPoint* GetPoint(int index);
305 
306  // Get Path Point by index
307  const psPathPoint* GetPoint(int index) const;
308 
309 protected:
311  virtual void PrecalculatePath(psWorld * world, iEngine *engine) = 0;
312 
314  virtual void GetInterpolatedPosition (int index, float fraction, csVector3& pos) = 0;
315 
317  virtual void GetInterpolatedUp (int index, float fraction, csVector3& up) = 0;
318 
320  virtual void GetInterpolatedForward (int index, float fraction, csVector3& forward) = 0;
321 };
322 
323 class psLinearPath: public psPath
324 {
325 public:
326  psLinearPath(csString name, Waypoint * wp1, Waypoint * wp2, psString flagStr);
327  psLinearPath(int pathID, csString name, psString flagStr);
328  virtual ~psLinearPath(){};
329 
330 protected:
332  virtual void PrecalculatePath(psWorld * world, iEngine *engine);
333 
335  virtual void GetInterpolatedPosition (int index, float fraction, csVector3& pos);
336 
338  virtual void GetInterpolatedUp (int index, float fraction, csVector3& up);
339 
341  virtual void GetInterpolatedForward (int index, float fraction, csVector3& forward);
342 
343 private:
344  csArray<float> dx;
345  csArray<float> dy;
346  csArray<float> dz;
347 };
348 
349 // Warning: This is BROKEN and does not work across sectors.
351 {
352 public:
353  psPathAnchor(psPath * path);
354  virtual ~psPathAnchor() {}
355 
357  virtual bool CalculateAtDistance(psWorld * world, iEngine *engine, float distance, psPath::Direction direction);
358 
360  virtual void GetInterpolatedPosition (csVector3& pos);
361 
363  virtual void GetInterpolatedUp (csVector3& up);
364 
366  virtual void GetInterpolatedForward (csVector3& forward);
367 
370  virtual bool Extrapolate(psWorld * world, iEngine *engine, float delta, psPath::Direction direction, iMovable* movable);
371 
372 
374  float GetDistance(){ return pathDistance; }
376  int GetCurrentAtIndex() { return currentAtIndex; }
378  psPath::Direction GetCurrentAtDirection() { return currentAtDirection; }
380  float GetCurrentAtFraction(){ return currentAtFraction; }
381 
382 private:
383  psPath * path;
384  // Internal non reentrant data leagal after calcuateAt operation
385  int currentAtIndex;
386  psPath::Direction currentAtDirection;
387  float currentAtFraction;
388 
389  float pathDistance;
390 };
391 
394 #endif
psWorld is in charge of managing all regions (zone map files) and loading/unloading them as needed...
Definition: psworld.h:47
virtual void GetInterpolatedUp(csVector3 &up)
Get the interpolated up vector.
void SetPrevious(int previous)
Set the previous point id.
Definition: pspath.h:91
float totalDistance
Definition: pspath.h:175
int GetPreviousPointId() const
Get the previous point id.
Definition: pspath.h:94
bool noWander
Definition: pspath.h:171
void SetID(int id)
Definition: pspath.h:97
Definition: pspath.h:152
int GetCurrentAtIndex()
Get the index to the current path point.
Definition: pspath.h:376
const char * GetName(int id)
float GetCurrentAtFraction()
Get the current fraction of the current path segment.
Definition: pspath.h:380
void SetFlag(const char *name, bool flag, uint32 filter)
Direction
Definition: pspath.h:156
virtual void GetInterpolatedPosition(csVector3 &pos)
Get the interpolated position.
A waypoint is a specified circle on the map with a name, location, and a list of waypoints it is conn...
Definition: waypoint.h:81
bool precalculationValid
Definition: pspath.h:174
virtual ~psPathAnchor()
Definition: pspath.h:354
int GetID() const
Get ID of the path.
Definition: pspath.h:262
int id
Definition: pspath.h:163
csString name
Definition: pspath.h:164
virtual const char * GetName() const
Get name of the path.
Definition: pspath.h:259
PathPointArray points
The points this path consists of including start wp and end wp.
Definition: pspath.h:167
iDataConnection * db
Global connection to the Database. Set from the psDatabase class.
bool oneWay
Flags.
Definition: pspath.h:170
virtual void GetInterpolatedForward(csVector3 &forward)
Get the interpolated forward vector.
const csVector3 & GetPosition() const
Definition: pspath.h:99
csPDelArray< psPathPoint > PathPointArray
Definition: pspath.h:161
virtual uint32_t GetEffectID()=0
bool teleport
Teleport between the waypoints.
Definition: pspath.h:172
Helper interface to allocate effect IDs.
Definition: pspath.h:48
virtual int GetNumPoints() const
Get number of points in path.
Definition: pspath.h:256
virtual ~psLinearPath()
Definition: pspath.h:328
int GetID() const
Definition: pspath.h:96
A Location is a named place on the map, located dynamically by NPCs as scripted.
Definition: location.h:48
Waypoint * start
This path start waypoint.
Definition: pspath.h:165
A network of Waypoint and psPath objects.
Definition: pspathnetwork.h:42
float GetDistance()
Get the current distance this anchor has moved along the path.
Definition: pspath.h:374
psPath::Direction GetCurrentAtDirection()
Get the current direction.
Definition: pspath.h:378
Represents a point on a path between two waypoints.
Definition: pspath.h:58
Waypoint * end
This path end waypoint.
Definition: pspath.h:166