Planeshift
location.h
Go to the documentation of this file.
1 /*
2  * location.h
3  *
4  * Copyright (C) 2004 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 __LOCATION_H__
20 #define __LOCATION_H__
21 
22 #include <csutil/csstring.h>
23 #include <csgeom/vector3.h>
24 #include <csgeom/box.h>
25 #include <csutil/parray.h>
26 #include <csutil/list.h>
27 #include <iengine/sector.h>
28 #include <csutil/weakref.h>
29 
30 #include "util/psconst.h"
31 #include "util/psdatabase.h"
32 
33 struct iEngine;
34 class LocationType;
35 class psWorld;
36 class iEffectIDAllocator;
37 
48 class Location
49 {
50 public:
51  int id;
52  csString name;
53  csVector3 pos;
54  float rot_angle;
55  float radius;
56  csArray<Location*> locs;
58  csString sectorName;
59 
60  csBox2 boundingBox;
61  csWeakRef<iSector> sector;
63  uint32_t effectID;
65 
68  Location();
69 
72  Location(LocationType* locationType, const char* locationName, const csVector3 &pos, iSector* sector, float radius, float rot_angle, const csString &flags);
73 
76  ~Location();
77 
80  int GetID() const
81  {
82  return id;
83  }
84 
87  void SetID(int id)
88  {
89  this->id = id;
90  }
91 
94  const csVector3 &GetPosition() const
95  {
96  return pos;
97  }
98 
102  csString GetFlags() const
103  {
104  return csString();
105  }
106 
110  bool SetFlags(const csString &flags)
111  {
112  return false;
113  }
114 
118  bool SetFlag(const csString &flag, bool enable)
119  {
120  return false;
121  }
122 
125  float GetRadius() const
126  {
127  return radius;
128  }
129 
132  bool SetRadius(iDataConnection* db, float radius);
133 
136  void SetRadius(float radius);
137 
140  float GetRotationAngle() const
141  {
142  return rot_angle;
143  }
144 
147  const char* GetTypeName() const;
148 
151  bool Load(iResultRow &row, iEngine* engine, iDataConnection* db);
152 
156  bool CreateUpdate(iDataConnection* db);
157 
160  bool Import(iDocumentNode* node, iDataConnection* db, int typeID);
161 
167  bool IsRegion()
168  {
169  return locs.GetSize() != 0;
170  }
171 
174  bool IsCircle()
175  {
176  return locs.GetSize() == 0;
177  }
178 
181  iSector* GetSector(iEngine* engine);
182 
185  iSector* GetSector(iEngine* engine) const;
186 
191  const csBox2 &GetBoundingBox() const;
192 
198  void CalculateBoundingBox();
199 
202  bool CheckWithinBounds(iEngine* engine,const csVector3 &pos,const iSector* sector);
203 
215  bool GetRandomPosition(iEngine* engine,csVector3 &pos,iSector* &sector);
216 
219  static int GetSectorID(iDataConnection* db, const char* name);
220 
223  const char* GetName() const
224  {
225  return name.GetDataSafe();
226  }
227 
230  void SetName(const csString &name)
231  {
232  this->name = name;
233  }
234 
238  uint32_t GetEffectID(iEffectIDAllocator* allocator);
239 
242  bool Adjust(iDataConnection* db, const csVector3 &pos, iSector* sector);
243 
246  bool Adjust(iDataConnection* db, const csVector3 &pos, iSector* sector, float rot_angle);
247 
250  bool Adjust(const csVector3 &pos, iSector* sector);
251 
254  bool Adjust(const csVector3 &pos, iSector* sector, float rot_angle);
255 
258  Location* Insert(iDataConnection* db, csVector3 &pos, iSector* sector);
259 
262  Location* Insert(int id, csVector3 &pos, iSector* sector);
263 
266  csString ToString() const;
267 };
268 
276 {
277 public:
278  int id;
279  csString name;
280  csArray<Location*> locs;
281 
284  LocationType();
285 
288  LocationType(int id, const csString &name);
289 
292  ~LocationType();
293 
297  bool CreateUpdate(iDataConnection* db);
298 
301  bool Load(iDocumentNode* node);
302  bool Import(iDocumentNode* node, iDataConnection* db);
303  bool ImportLocations(iDocumentNode* node, iDataConnection* db);
304 
307  bool Load(iResultRow &row, iEngine* engine, iDataConnection* db);
308 
311  void AddLocation(Location* location);
312 
315  void RemoveLocation(Location* location);
316 
319  bool CheckWithinBounds(iEngine* engine,const csVector3 &pos,const iSector* sector);
320 
333  bool GetRandomPosition(iEngine* engine,csVector3 &pos,iSector* &sector, const iSector* inSector);
334 
337  int GetID() const
338  {
339  return id;
340  }
341 
344  const char* GetName() const
345  {
346  return name.GetDataSafe();
347  }
348 };
349 
354 {
355 public:
358  LocationManager();
359 
362  virtual ~LocationManager();
363 
366  bool Load(iEngine* engine, iDataConnection* db);
367 
370  int GetNumberOfLocations() const;
371 
374  Location* GetLocation(int index);
375 
380  LocationType* FindRegion(const char* regname);
381 
386  LocationType* FindLocation(const char* typeName);
387 
392  Location* FindLocation(const char* typeName, const char* name);
393 
396  Location* FindLocation(LocationType* type, const char* name);
397 
400  Location* FindLocation(int id);
401 
404  size_t FindLocationsInSector(iEngine* engine, iSector* sector, csList<Location*> &list);
405 
408  Location* FindNearestLocation(psWorld* world, csVector3 &pos, iSector* sector, float range, float* found_range);
409 
412  Location* FindNearestLocation(psWorld* world, const char* typeName, csVector3 &pos, iSector* sector, float range, float* found_range);
413 
416  Location* FindRandomLocation(psWorld* world, const char* typeName, csVector3 &pos, iSector* sector, float range, float* found_range);
417 
420  csHash<LocationType*, csString>::GlobalIterator GetIterator();
421 
424  Location* CreateLocation(iDataConnection* db, LocationType* locationType, const char* locationName,
425  const csVector3 &pos, iSector* sector, float radius, float rot_angle,
426  const csString &flags);
427 
430  Location* CreateLocation(const char* locationTypeName, const char* locationName,
431  const csVector3 &pos, iSector* sector, float radius, float rot_angle,
432  const csString &flags);
433 
436  Location* CreateLocation(LocationType* locationType, const char* locationName,
437  const csVector3 &pos, iSector* sector, float radius, float rot_angle,
438  const csString &flags);
439 
442  LocationType* CreateLocationType(iDataConnection* db, const csString &locationName);
443 
446  LocationType* CreateLocationType(const csString &locationName);
447 
450  LocationType* CreateLocationType(int id, const csString &locationName);
451 
454  bool RemoveLocationType(iDataConnection* db, const csString &locationName);
455 
458  bool RemoveLocationType(const csString &locationName);
459 
460 private:
461  csHash<LocationType*, csString> loctypes;
462  csArray<Location*> all_locations;
463 };
464 
467 #endif
468 
void SetID(int id)
Set DB ID.
Definition: location.h:87
psWorld is in charge of managing all regions (zone map files) and loading/unloading them as needed...
Definition: psworld.h:47
const char * GetName() const
retrive the name of this location.
Definition: location.h:223
iSector * GetSector(iEngine *engine)
Return cached sector or find the sector and cache it from engine.
bool SetFlags(const csString &flags)
Set flags.
Definition: location.h:110
float GetRadius() const
Get the Radius.
Definition: location.h:125
bool Adjust(iDataConnection *db, const csVector3 &pos, iSector *sector)
Adjust the postion of this location.
int id
The DB ID of this location type.
Definition: location.h:278
csString sectorName
The sector where this location is located.
Definition: location.h:58
int GetID() const
retrive the ID of this location type.
Definition: location.h:337
bool IsCircle()
Query if this location is a circle.
Definition: location.h:174
float GetRotationAngle() const
Get the rotation angle.
Definition: location.h:140
void CalculateBoundingBox()
Function to calculate the bounding box for a location.
static int GetSectorID(iDataConnection *db, const char *name)
retrive a sector ID.
const csVector3 & GetPosition() const
Return the position of this location.
Definition: location.h:94
csArray< Location * > locs
All the location of this location type.
Definition: location.h:280
int GetID() const
Get the DB ID.
Definition: location.h:80
bool Load(iResultRow &row, iEngine *engine, iDataConnection *db)
Load a location from the DB.
uint32_t GetEffectID(iEffectIDAllocator *allocator)
Return the effect ID for this location or assign a new ID.
bool CheckWithinBounds(iEngine *engine, const csVector3 &pos, const iSector *sector)
Check if a point is within bounds of this location.
This stores a vector of positions listing a set of points defining a common type of location...
Definition: location.h:275
csArray< Location * > locs
A number of points for regions. Including self for first location in region.
Definition: location.h:56
csString name
The name of this location.
Definition: location.h:52
csVector3 pos
The positon of this location.
Definition: location.h:53
iDataConnection * db
Global connection to the Database. Set from the psDatabase class.
bool Import(iDocumentNode *node, iDataConnection *db, int typeID)
Import a location from an XML document.
Manager that manage all locations and location types.
Definition: location.h:353
bool SetFlag(const csString &flag, bool enable)
Set flag.
Definition: location.h:118
int id
The ID of this location, from the DB.
Definition: location.h:51
Location * region
Pointer to first location in a region.
Definition: location.h:64
csBox2 boundingBox
Bounding box used for boundary checks.
Definition: location.h:60
const csBox2 & GetBoundingBox() const
Return the bounding box for this location.
csWeakRef< iSector > sector
Cached sector.
Definition: location.h:61
bool SetRadius(iDataConnection *db, float radius)
Set Radius and recalculate the boudning box.
float radius
The radius of this locaiton.
Definition: location.h:55
csString name
The name of this location type.
Definition: location.h:279
void SetName(const csString &name)
Set the name.
Definition: location.h:230
Helper interface to allocate effect IDs.
Definition: pspath.h:48
Location * Insert(iDataConnection *db, csVector3 &pos, iSector *sector)
Insert a new point in a region after this location.
bool IsRegion()
Query if this location is a region.
Definition: location.h:167
~Location()
Destructor.
csString ToString() const
Create text representation.
int id_prev_loc_in_region
Prev database ID for a region.
Definition: location.h:57
csString GetFlags() const
Get flags.
Definition: location.h:102
bool CreateUpdate(iDataConnection *db)
Create or update an entry for this location in the DB.
bool GetRandomPosition(iEngine *engine, csVector3 &pos, iSector *&sector)
Get a random position in the location.
LocationType * type
Points back to location type.
Definition: location.h:62
A Location is a named place on the map, located dynamically by NPCs as scripted.
Definition: location.h:48
uint32_t effectID
When displayed in a client this is the effect id.
Definition: location.h:63
float rot_angle
Some location has an angle.
Definition: location.h:54
Location()
Constructor.
const char * GetName() const
retrive the name of this location type.
Definition: location.h:344
const char * GetTypeName() const
Get the type name of this location.