TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GridDefines.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_GRIDDEFINES_H
20 #define TRINITY_GRIDDEFINES_H
21 
22 #include "Common.h"
23 #include "NGrid.h"
24 #include <cmath>
25 
26 // Forward class definitions
27 class Corpse;
28 class Creature;
29 class DynamicObject;
30 class GameObject;
31 class Pet;
32 class Player;
33 class AreaTrigger;
34 
35 #define MAX_NUMBER_OF_CELLS 8
36 
37 #define MAX_NUMBER_OF_GRIDS 64
38 
39 #define SIZE_OF_GRIDS 533.3333f
40 #define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
41 
42 #define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
43 
44 #define MIN_GRID_DELAY (MINUTE*IN_MILLISECONDS)
45 #define MIN_MAP_UPDATE_DELAY 50
46 
47 #define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
48 
49 #define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
50 #define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
51 
52 #define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
53 
54 #define MAP_RESOLUTION 128
55 
56 #define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
57 #define MAP_HALFSIZE (MAP_SIZE/2)
58 
59 // Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
60 typedef TYPELIST_4(Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/) AllWorldObjectTypes;
61 typedef TYPELIST_5(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/, AreaTrigger) AllGridObjectTypes;
62 typedef TYPELIST_6(Creature, GameObject, DynamicObject, Pet, Corpse, AreaTrigger) AllMapStoredObjectTypes;
63 
64 typedef GridRefManager<Corpse> CorpseMapType;
65 typedef GridRefManager<Creature> CreatureMapType;
66 typedef GridRefManager<DynamicObject> DynamicObjectMapType;
67 typedef GridRefManager<GameObject> GameObjectMapType;
70 
72 {
80 };
81 
84 
87 
88 template<uint32 LIMIT>
89 struct CoordPair
90 {
92  : x_coord(x), y_coord(y)
93  { }
94 
96  : x_coord(obj.x_coord), y_coord(obj.y_coord)
97  { }
98 
100  {
101  x_coord = obj.x_coord;
102  y_coord = obj.y_coord;
103  return *this;
104  }
105 
106  void dec_x(uint32 val)
107  {
108  if (x_coord > val)
109  x_coord -= val;
110  else
111  x_coord = 0;
112  }
113 
114  void inc_x(uint32 val)
115  {
116  if (x_coord + val < LIMIT)
117  x_coord += val;
118  else
119  x_coord = LIMIT - 1;
120  }
121 
122  void dec_y(uint32 val)
123  {
124  if (y_coord > val)
125  y_coord -= val;
126  else
127  y_coord = 0;
128  }
129 
130  void inc_y(uint32 val)
131  {
132  if (y_coord + val < LIMIT)
133  y_coord += val;
134  else
135  y_coord = LIMIT - 1;
136  }
137 
138  bool IsCoordValid() const
139  {
140  return x_coord < LIMIT && y_coord < LIMIT;
141  }
142 
144  {
145  x_coord = std::min(x_coord, LIMIT - 1);
146  y_coord = std::min(y_coord, LIMIT - 1);
147  return *this;
148  }
149 
150  uint32 GetId() const
151  {
152  return y_coord * LIMIT + x_coord;
153  }
154 
157 };
158 
159 template<uint32 LIMIT>
160 bool operator==(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
161 {
162  return (p1.x_coord == p2.x_coord && p1.y_coord == p2.y_coord);
163 }
164 
165 template<uint32 LIMIT>
166 bool operator!=(const CoordPair<LIMIT> &p1, const CoordPair<LIMIT> &p2)
167 {
168  return !(p1 == p2);
169 }
170 
173 
174 namespace Trinity
175 {
176  template<class RET_TYPE, int CENTER_VAL>
177  inline RET_TYPE Compute(float x, float y, float center_offset, float size)
178  {
179  // calculate and store temporary values in double format for having same result as same mySQL calculations
180  double x_offset = (double(x) - center_offset)/size;
181  double y_offset = (double(y) - center_offset)/size;
182 
183  int x_val = int(x_offset + CENTER_VAL + 0.5f);
184  int y_val = int(y_offset + CENTER_VAL + 0.5f);
185  return RET_TYPE(x_val, y_val);
186  }
187 
188  inline GridCoord ComputeGridCoord(float x, float y)
189  {
190  return Compute<GridCoord, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
191  }
192 
193  inline CellCoord ComputeCellCoord(float x, float y)
194  {
195  return Compute<CellCoord, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
196  }
197 
198  inline CellCoord ComputeCellCoord(float x, float y, float &x_off, float &y_off)
199  {
200  double x_offset = (double(x) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
201  double y_offset = (double(y) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
202 
203  int x_val = int(x_offset + CENTER_GRID_CELL_ID + 0.5f);
204  int y_val = int(y_offset + CENTER_GRID_CELL_ID + 0.5f);
205  x_off = (float(x_offset) - float(x_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
206  y_off = (float(y_offset) - float(y_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
207  return CellCoord(x_val, y_val);
208  }
209 
210  inline void NormalizeMapCoord(float &c)
211  {
212  if (c > MAP_HALFSIZE - 0.5f)
213  c = MAP_HALFSIZE - 0.5f;
214  else if (c < -(MAP_HALFSIZE - 0.5f))
215  c = -(MAP_HALFSIZE - 0.5f);
216  }
217 
218  inline bool IsValidMapCoord(float c)
219  {
220  return std::isfinite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5f);
221  }
222 
223  inline bool IsValidMapCoord(float x, float y)
224  {
225  return IsValidMapCoord(x) && IsValidMapCoord(y);
226  }
227 
228  inline bool IsValidMapCoord(float x, float y, float z)
229  {
230  return IsValidMapCoord(x, y) && IsValidMapCoord(z);
231  }
232 
233  inline bool IsValidMapCoord(float x, float y, float z, float o)
234  {
235  return IsValidMapCoord(x, y, z) && std::isfinite(o);
236  }
237 }
238 #endif
CoordPair(const CoordPair< LIMIT > &obj)
Definition: GridDefines.h:95
TypeMapContainer< AllGridObjectTypes > GridTypeMapContainer
Definition: GridDefines.h:85
Definition: NGrid.h:77
#define CENTER_GRID_CELL_ID
Definition: GridDefines.h:49
Definition: GridDefines.h:78
#define SIZE_OF_GRID_CELL
Definition: GridDefines.h:47
Definition: Corpse.h:49
Definition: GridDefines.h:79
typedef TYPELIST_4(Player, Creature, Corpse, DynamicObject) AllWorldObjectTypes
#define SIZE_OF_GRIDS
Definition: GridDefines.h:39
Grid< Player, AllWorldObjectTypes, AllGridObjectTypes > GridType
Definition: GridDefines.h:82
CoordPair & normalize()
Definition: GridDefines.h:143
TypeMapContainer< AllWorldObjectTypes > WorldTypeMapContainer
Definition: GridDefines.h:86
#define MAP_HALFSIZE
Definition: GridDefines.h:57
GridCoord ComputeGridCoord(float x, float y)
Definition: GridDefines.h:188
CellCoord ComputeCellCoord(float x, float y)
Definition: GridDefines.h:193
Definition: Grid.h:46
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:177
Definition: Creature.h:467
uint32 y_coord
Definition: GridDefines.h:156
uint32 x_coord
Definition: GridDefines.h:155
void inc_y(uint32 val)
Definition: GridDefines.h:130
Definition: GridDefines.h:77
void dec_y(uint32 val)
Definition: GridDefines.h:122
Definition: TypeContainer.h:86
typedef TYPELIST_6(Creature, GameObject, DynamicObject, Pet, Corpse, AreaTrigger) AllMapStoredObjectTypes
Definition: DynamicObject.h:35
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Definition: GridDefines.h:76
Definition: GridReference.h:25
typedef TYPELIST_5(GameObject, Creature, DynamicObject, Corpse, AreaTrigger) AllGridObjectTypes
bool IsValidMapCoord(float c)
Definition: GridDefines.h:218
G3D::int16 z
Definition: Vector3int16.h:46
bool operator!=(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:166
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
Definition: GridDefines.h:75
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
Definition: GridDefines.h:89
Definition: GameObject.h:880
CoordPair< LIMIT > & operator=(const CoordPair< LIMIT > &obj)
Definition: GridDefines.h:99
bool IsCoordValid() const
Definition: GridDefines.h:138
void NormalizeMapCoord(float &c)
Definition: GridDefines.h:210
#define CENTER_GRID_OFFSET
Definition: GridDefines.h:42
#define CENTER_GRID_CELL_OFFSET
Definition: GridDefines.h:50
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
Definition: GridDefines.h:172
void dec_x(uint32 val)
Definition: GridDefines.h:106
Definition: GridDefines.h:74
Definition: Common.h:172
G3D::int16 x
Definition: Vector2int16.h:37
CoordPair(uint32 x=0, uint32 y=0)
Definition: GridDefines.h:91
NGrid< MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes > NGridType
Definition: GridDefines.h:83
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
Definition: GridDefines.h:171
uint32 GetId() const
Definition: GridDefines.h:150
Definition: GridDefines.h:73
Definition: Pet.h:46
Definition: AreaTrigger.h:26
GridMapTypeMask
Definition: GridDefines.h:71
void inc_x(uint32 val)
Definition: GridDefines.h:114