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