TrinityCore
Loading...
Searching...
No Matches
GridDefines.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 TRINITY_GRIDDEFINES_H
19#define TRINITY_GRIDDEFINES_H
20
21#include "Common.h"
22#include "ObjectGuid.h"
23#include "NGrid.h"
24#include <cmath>
25
26// Forward class definitions
27class Corpse;
28class Creature;
29class DynamicObject;
30class GameObject;
31class Pet;
32class Player;
33
34#define MAX_NUMBER_OF_CELLS 8
35
36#define MAX_NUMBER_OF_GRIDS 64
37
38#define SIZE_OF_GRIDS 533.3333f
39#define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
40
41#define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
42
43#define MIN_GRID_DELAY (MINUTE*IN_MILLISECONDS)
44#define MIN_MAP_UPDATE_DELAY 1
45
46#define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
47
48#define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
49#define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
50
51#define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
52
53#define MAP_RESOLUTION 128
54
55#define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
56#define MAP_HALFSIZE (MAP_SIZE/2)
57
58// Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
59typedef TYPELIST_4(Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/) AllWorldObjectTypes;
60typedef TYPELIST_4(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/) AllGridObjectTypes;
61typedef TYPELIST_5(Creature, GameObject, DynamicObject, Pet, Corpse) AllMapStoredObjectTypes;
62
68
78
81
82extern template class TypeMapContainer<AllGridObjectTypes>;
83extern template class TypeMapContainer<AllWorldObjectTypes>;
84
87
90
91template<uint32 LIMIT>
93{
95 : x_coord(x), y_coord(y)
96 { }
97
99 : x_coord(obj.x_coord), y_coord(obj.y_coord)
100 { }
101
103 {
104 x_coord = obj.x_coord;
105 y_coord = obj.y_coord;
106 return *this;
107 }
108
109 void dec_x(uint32 val)
110 {
111 if (x_coord > val)
112 x_coord -= val;
113 else
114 x_coord = 0;
115 }
116
117 void inc_x(uint32 val)
118 {
119 if (x_coord + val < LIMIT)
120 x_coord += val;
121 else
122 x_coord = LIMIT - 1;
123 }
124
125 void dec_y(uint32 val)
126 {
127 if (y_coord > val)
128 y_coord -= val;
129 else
130 y_coord = 0;
131 }
132
133 void inc_y(uint32 val)
134 {
135 if (y_coord + val < LIMIT)
136 y_coord += val;
137 else
138 y_coord = LIMIT - 1;
139 }
140
141 bool IsCoordValid() const
142 {
143 return x_coord < LIMIT && y_coord < LIMIT;
144 }
145
147 {
148 x_coord = std::min(x_coord, LIMIT - 1);
149 y_coord = std::min(y_coord, LIMIT - 1);
150 return *this;
151 }
152
153 uint32 GetId() const
154 {
155 return y_coord * LIMIT + x_coord;
156 }
157
158 friend bool operator==(CoordPair const& p1, CoordPair const& p2) = default;
159
162};
163
166
167namespace Trinity
168{
169 template<class RET_TYPE, int CENTER_VAL>
170 inline RET_TYPE Compute(float x, float y, float center_offset, float size)
171 {
172 // calculate and store temporary values in double format for having same result as same mySQL calculations
173 double x_offset = (double(x) - center_offset)/size;
174 double y_offset = (double(y) - center_offset)/size;
175
176 int x_val = int(x_offset + CENTER_VAL + 0.5);
177 int y_val = int(y_offset + CENTER_VAL + 0.5);
178 return RET_TYPE(x_val, y_val);
179 }
180
181 inline GridCoord ComputeGridCoord(float x, float y)
182 {
183 return Compute<GridCoord, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
184 }
185
186 inline GridCoord ComputeGridCoordSimple(float x, float y)
187 {
188 int gx = (int)(CENTER_GRID_ID - x / SIZE_OF_GRIDS);
189 int gy = (int)(CENTER_GRID_ID - y / SIZE_OF_GRIDS);
190 return GridCoord((MAX_NUMBER_OF_GRIDS - 1) - gx, (MAX_NUMBER_OF_GRIDS - 1) - gy);
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
uint32_t uint32
Definition Define.h:133
GridRefManager< Corpse > CorpseMapType
Definition GridDefines.h:63
GridRefManager< DynamicObject > DynamicObjectMapType
Definition GridDefines.h:65
GridRefManager< Creature > CreatureMapType
Definition GridDefines.h:64
GridRefManager< Player > PlayerMapType
Definition GridDefines.h:67
#define SIZE_OF_GRIDS
Definition GridDefines.h:38
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
TypeMapContainer< AllWorldObjectTypes > WorldTypeMapContainer
Definition GridDefines.h:89
TypeMapContainer< AllGridObjectTypes > GridTypeMapContainer
Definition GridDefines.h:88
#define CENTER_GRID_CELL_OFFSET
Definition GridDefines.h:49
#define MAX_NUMBER_OF_GRIDS
Definition GridDefines.h:36
#define CENTER_GRID_CELL_ID
Definition GridDefines.h:48
#define SIZE_OF_GRID_CELL
Definition GridDefines.h:46
#define CENTER_GRID_ID
Definition GridDefines.h:39
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
#define CENTER_GRID_OFFSET
Definition GridDefines.h:41
#define MAP_HALFSIZE
Definition GridDefines.h:56
GridMapTypeMask
Definition GridDefines.h:70
@ GRID_MAP_TYPE_MASK_PLAYER
Definition GridDefines.h:75
@ GRID_MAP_TYPE_MASK_CREATURE
Definition GridDefines.h:72
@ GRID_MAP_TYPE_MASK_ALL
Definition GridDefines.h:76
@ GRID_MAP_TYPE_MASK_GAMEOBJECT
Definition GridDefines.h:74
@ GRID_MAP_TYPE_MASK_DYNAMICOBJECT
Definition GridDefines.h:73
@ GRID_MAP_TYPE_MASK_CORPSE
Definition GridDefines.h:71
GridRefManager< GameObject > GameObjectMapType
Definition GridDefines.h:66
NGrid< MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes > NGridType
Definition GridDefines.h:86
Grid< Player, AllWorldObjectTypes, AllGridObjectTypes > GridType
Definition GridDefines.h:85
#define TYPELIST_4(T1, T2, T3, T4)
Definition TypeList.h:40
#define TYPELIST_5(T1, T2, T3, T4, T5)
Definition TypeList.h:41
Definition Grid.h:46
Definition NGrid.h:73
Definition Pet.h:40
GridCoord ComputeGridCoordSimple(float x, float y)
bool IsValidMapCoord(float c)
RET_TYPE Compute(float x, float y, float center_offset, float size)
void NormalizeMapCoord(float &c)
GridCoord ComputeGridCoord(float x, float y)
CellCoord ComputeCellCoord(float x, float y)
bool IsCoordValid() const
uint32 x_coord
void inc_y(uint32 val)
CoordPair< LIMIT > & operator=(const CoordPair< LIMIT > &obj)
CoordPair(uint32 x=0, uint32 y=0)
Definition GridDefines.h:94
void dec_x(uint32 val)
void inc_x(uint32 val)
CoordPair(const CoordPair< LIMIT > &obj)
Definition GridDefines.h:98
uint32 y_coord
friend bool operator==(CoordPair const &p1, CoordPair const &p2)=default
uint32 GetId() const
void dec_y(uint32 val)
CoordPair & normalize()