TrinityCore
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 "NGrid.h"
23#include <cmath>
24
25// Forward class definitions
26class Corpse;
27class Creature;
28class DynamicObject;
29class GameObject;
30class Pet;
31class Player;
32class AreaTrigger;
33class SceneObject;
34class Conversation;
35
36#define MAX_NUMBER_OF_CELLS 8
37
38#define MAX_NUMBER_OF_GRIDS 64
39
40#define SIZE_OF_GRIDS 533.3333f
41#define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
42
43#define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
44
45#define MIN_GRID_DELAY (MINUTE*IN_MILLISECONDS)
46#define MIN_MAP_UPDATE_DELAY 1
47
48#define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
49
50#define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
51#define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
52
53#define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
54
55#define MAP_RESOLUTION 128
56
57#define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
58#define MAP_HALFSIZE (MAP_SIZE/2)
59
60#define MAX_HEIGHT 100000.0f // can be use for find ground height at surface
61#define INVALID_HEIGHT -100000.0f // for check, must be equal to VMAP_INVALID_HEIGHT, real value for unknown height is VMAP_INVALID_HEIGHT_VALUE
62#define MAX_FALL_DISTANCE 250000.0f // "unlimited fall" to find VMap ground if it is available, just larger than MAX_HEIGHT - INVALID_HEIGHT
63#define DEFAULT_HEIGHT_SEARCH 50.0f // default search distance to find height at nearby locations
64
65// Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
66typedef TYPELIST_4(Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/) AllWorldObjectTypes;
67typedef TYPELIST_7(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/, AreaTrigger, SceneObject, Conversation) AllGridObjectTypes;
69
78
80{
90};
91
94
95extern template class TypeMapContainer<AllGridObjectTypes>;
96extern template class TypeMapContainer<AllWorldObjectTypes>;
97
100
103
104template<uint32 LIMIT>
106{
108 : x_coord(x), y_coord(y)
109 { }
110
112 : x_coord(obj.x_coord), y_coord(obj.y_coord)
113 { }
114
116 {
117 x_coord = obj.x_coord;
118 y_coord = obj.y_coord;
119 return *this;
120 }
121
122 void dec_x(uint32 val)
123 {
124 if (x_coord > val)
125 x_coord -= val;
126 else
127 x_coord = 0;
128 }
129
130 void inc_x(uint32 val)
131 {
132 if (x_coord + val < LIMIT)
133 x_coord += val;
134 else
135 x_coord = LIMIT - 1;
136 }
137
138 void dec_y(uint32 val)
139 {
140 if (y_coord > val)
141 y_coord -= val;
142 else
143 y_coord = 0;
144 }
145
146 void inc_y(uint32 val)
147 {
148 if (y_coord + val < LIMIT)
149 y_coord += val;
150 else
151 y_coord = LIMIT - 1;
152 }
153
154 bool IsCoordValid() const
155 {
156 return x_coord < LIMIT && y_coord < LIMIT;
157 }
158
160 {
161 x_coord = std::min(x_coord, LIMIT - 1);
162 y_coord = std::min(y_coord, LIMIT - 1);
163 return *this;
164 }
165
166 uint32 GetId() const
167 {
168 return y_coord * LIMIT + x_coord;
169 }
170
171 friend bool operator==(CoordPair const& p1, CoordPair const& p2) = default;
172
175};
176
179
180namespace Trinity
181{
182 template<class RET_TYPE, int CENTER_VAL>
183 inline RET_TYPE Compute(float x, float y, float center_offset, float size)
184 {
185 // calculate and store temporary values in double format for having same result as same mySQL calculations
186 double x_offset = (double(x) - center_offset)/size;
187 double y_offset = (double(y) - center_offset)/size;
188
189 int x_val = int(x_offset + CENTER_VAL + 0.5);
190 int y_val = int(y_offset + CENTER_VAL + 0.5);
191 return RET_TYPE(x_val, y_val);
192 }
193
194 inline GridCoord ComputeGridCoord(float x, float y)
195 {
196 return Compute<GridCoord, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
197 }
198
199 inline GridCoord ComputeGridCoordSimple(float x, float y)
200 {
201 int gx = (int)(CENTER_GRID_ID - x / SIZE_OF_GRIDS);
202 int gy = (int)(CENTER_GRID_ID - y / SIZE_OF_GRIDS);
203 return GridCoord((MAX_NUMBER_OF_GRIDS - 1) - gx, (MAX_NUMBER_OF_GRIDS - 1) - gy);
204 }
205
206 inline CellCoord ComputeCellCoord(float x, float y)
207 {
208 return Compute<CellCoord, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
209 }
210
211 inline CellCoord ComputeCellCoord(float x, float y, float &x_off, float &y_off)
212 {
213 double x_offset = (double(x) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
214 double y_offset = (double(y) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
215
216 int x_val = int(x_offset + CENTER_GRID_CELL_ID + 0.5f);
217 int y_val = int(y_offset + CENTER_GRID_CELL_ID + 0.5f);
218 x_off = (float(x_offset) - float(x_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
219 y_off = (float(y_offset) - float(y_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
220 return CellCoord(x_val, y_val);
221 }
222
223 inline void NormalizeMapCoord(float &c)
224 {
225 if (c > MAP_HALFSIZE - 0.5f)
226 c = MAP_HALFSIZE - 0.5f;
227 else if (c < -(MAP_HALFSIZE - 0.5f))
228 c = -(MAP_HALFSIZE - 0.5f);
229 }
230
231 inline bool IsValidMapCoord(float c)
232 {
233 return std::isfinite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5f);
234 }
235
236 inline bool IsValidMapCoord(float x, float y)
237 {
238 return IsValidMapCoord(x) && IsValidMapCoord(y);
239 }
240
241 inline bool IsValidMapCoord(float x, float y, float z)
242 {
243 return IsValidMapCoord(x, y) && IsValidMapCoord(z);
244 }
245
246 inline bool IsValidMapCoord(float x, float y, float z, float o)
247 {
248 return IsValidMapCoord(x, y, z) && std::isfinite(o);
249 }
250}
251#endif
uint32_t uint32
Definition: Define.h:142
GridRefManager< Corpse > CorpseMapType
Definition: GridDefines.h:70
GridRefManager< DynamicObject > DynamicObjectMapType
Definition: GridDefines.h:72
GridRefManager< Creature > CreatureMapType
Definition: GridDefines.h:71
GridRefManager< Player > PlayerMapType
Definition: GridDefines.h:74
#define SIZE_OF_GRIDS
Definition: GridDefines.h:40
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
Definition: GridDefines.h:177
TypeMapContainer< AllWorldObjectTypes > WorldTypeMapContainer
Definition: GridDefines.h:102
TypeMapContainer< AllGridObjectTypes > GridTypeMapContainer
Definition: GridDefines.h:101
GridRefManager< Conversation > ConversationMapType
Definition: GridDefines.h:77
#define CENTER_GRID_CELL_OFFSET
Definition: GridDefines.h:51
GridRefManager< AreaTrigger > AreaTriggerMapType
Definition: GridDefines.h:75
#define MAX_NUMBER_OF_GRIDS
Definition: GridDefines.h:38
#define CENTER_GRID_CELL_ID
Definition: GridDefines.h:50
#define SIZE_OF_GRID_CELL
Definition: GridDefines.h:48
#define CENTER_GRID_ID
Definition: GridDefines.h:41
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
Definition: GridDefines.h:178
GridRefManager< SceneObject > SceneObjectMapType
Definition: GridDefines.h:76
#define CENTER_GRID_OFFSET
Definition: GridDefines.h:43
typedef TYPELIST_8(Creature, GameObject, DynamicObject, Pet, Corpse, AreaTrigger, SceneObject, Conversation) AllMapStoredObjectTypes
#define MAP_HALFSIZE
Definition: GridDefines.h:58
GridMapTypeMask
Definition: GridDefines.h:80
@ GRID_MAP_TYPE_MASK_PLAYER
Definition: GridDefines.h:85
@ GRID_MAP_TYPE_MASK_CREATURE
Definition: GridDefines.h:82
@ GRID_MAP_TYPE_MASK_CONVERSATION
Definition: GridDefines.h:88
@ GRID_MAP_TYPE_MASK_SCENEOBJECT
Definition: GridDefines.h:87
@ GRID_MAP_TYPE_MASK_ALL
Definition: GridDefines.h:89
@ GRID_MAP_TYPE_MASK_GAMEOBJECT
Definition: GridDefines.h:84
@ GRID_MAP_TYPE_MASK_DYNAMICOBJECT
Definition: GridDefines.h:83
@ GRID_MAP_TYPE_MASK_CORPSE
Definition: GridDefines.h:81
@ GRID_MAP_TYPE_MASK_AREATRIGGER
Definition: GridDefines.h:86
typedef TYPELIST_7(GameObject, Creature, DynamicObject, Corpse, AreaTrigger, SceneObject, Conversation) AllGridObjectTypes
GridRefManager< GameObject > GameObjectMapType
Definition: GridDefines.h:73
typedef TYPELIST_4(Player, Creature, Corpse, DynamicObject) AllWorldObjectTypes
NGrid< MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes > NGridType
Definition: GridDefines.h:99
Grid< Player, AllWorldObjectTypes, AllGridObjectTypes > GridType
Definition: GridDefines.h:98
Definition: Corpse.h:53
Definition: Grid.h:46
Definition: NGrid.h:70
Definition: Pet.h:40
GridCoord ComputeGridCoordSimple(float x, float y)
Definition: GridDefines.h:199
bool IsValidMapCoord(float x, float y, float z, float o)
Definition: GridDefines.h:246
CellCoord ComputeCellCoord(float x, float y, float &x_off, float &y_off)
Definition: GridDefines.h:211
RET_TYPE Compute(float x, float y, float center_offset, float size)
Definition: GridDefines.h:183
void NormalizeMapCoord(float &c)
Definition: GridDefines.h:223
GridCoord ComputeGridCoord(float x, float y)
Definition: GridDefines.h:194
constexpr std::size_t size()
Definition: UpdateField.h:796
bool IsCoordValid() const
Definition: GridDefines.h:154
uint32 x_coord
Definition: GridDefines.h:173
void inc_y(uint32 val)
Definition: GridDefines.h:146
CoordPair< LIMIT > & operator=(const CoordPair< LIMIT > &obj)
Definition: GridDefines.h:115
CoordPair(uint32 x=0, uint32 y=0)
Definition: GridDefines.h:107
void dec_x(uint32 val)
Definition: GridDefines.h:122
void inc_x(uint32 val)
Definition: GridDefines.h:130
CoordPair(const CoordPair< LIMIT > &obj)
Definition: GridDefines.h:111
uint32 y_coord
Definition: GridDefines.h:174
friend bool operator==(CoordPair const &p1, CoordPair const &p2)=default
uint32 GetId() const
Definition: GridDefines.h:166
void dec_y(uint32 val)
Definition: GridDefines.h:138
CoordPair & normalize()
Definition: GridDefines.h:159