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 "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
73
86
87// Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
89extern template struct TypeListContainer<GridRefManagerContainer, Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/>;
90
93
96
99
100template<uint32 LIMIT>
102{
104 : x_coord(x), y_coord(y)
105 { }
106
108 : x_coord(obj.x_coord), y_coord(obj.y_coord)
109 { }
110
112 {
113 x_coord = obj.x_coord;
114 y_coord = obj.y_coord;
115 return *this;
116 }
117
118 void dec_x(uint32 val)
119 {
120 if (x_coord > val)
121 x_coord -= val;
122 else
123 x_coord = 0;
124 }
125
126 void inc_x(uint32 val)
127 {
128 if (x_coord + val < LIMIT)
129 x_coord += val;
130 else
131 x_coord = LIMIT - 1;
132 }
133
134 void dec_y(uint32 val)
135 {
136 if (y_coord > val)
137 y_coord -= val;
138 else
139 y_coord = 0;
140 }
141
142 void inc_y(uint32 val)
143 {
144 if (y_coord + val < LIMIT)
145 y_coord += val;
146 else
147 y_coord = LIMIT - 1;
148 }
149
150 bool IsCoordValid() const
151 {
152 return x_coord < LIMIT && y_coord < LIMIT;
153 }
154
156 {
157 x_coord = std::min(x_coord, LIMIT - 1);
158 y_coord = std::min(y_coord, LIMIT - 1);
159 return *this;
160 }
161
162 uint32 GetId() const
163 {
164 return y_coord * LIMIT + x_coord;
165 }
166
167 friend bool operator==(CoordPair const& p1, CoordPair const& p2) = default;
168
171};
172
175
176namespace Trinity
177{
178 template<class RET_TYPE, int CENTER_VAL>
179 inline RET_TYPE Compute(float x, float y, float center_offset, float size)
180 {
181 // calculate and store temporary values in double format for having same result as same mySQL calculations
182 double x_offset = (double(x) - center_offset)/size;
183 double y_offset = (double(y) - center_offset)/size;
184
185 int x_val = int(x_offset + CENTER_VAL + 0.5);
186 int y_val = int(y_offset + CENTER_VAL + 0.5);
187 return RET_TYPE(x_val, y_val);
188 }
189
190 inline GridCoord ComputeGridCoord(float x, float y)
191 {
192 return Compute<GridCoord, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
193 }
194
195 inline GridCoord ComputeGridCoordSimple(float x, float y)
196 {
197 int gx = (int)(CENTER_GRID_ID - x / SIZE_OF_GRIDS);
198 int gy = (int)(CENTER_GRID_ID - y / SIZE_OF_GRIDS);
199 return GridCoord((MAX_NUMBER_OF_GRIDS - 1) - gx, (MAX_NUMBER_OF_GRIDS - 1) - gy);
200 }
201
202 inline CellCoord ComputeCellCoord(float x, float y)
203 {
204 return Compute<CellCoord, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
205 }
206
207 inline CellCoord ComputeCellCoord(float x, float y, float &x_off, float &y_off)
208 {
209 double x_offset = (double(x) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
210 double y_offset = (double(y) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
211
212 int x_val = int(x_offset + CENTER_GRID_CELL_ID + 0.5f);
213 int y_val = int(y_offset + CENTER_GRID_CELL_ID + 0.5f);
214 x_off = (float(x_offset) - float(x_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
215 y_off = (float(y_offset) - float(y_val) + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
216 return CellCoord(x_val, y_val);
217 }
218
219 inline void NormalizeMapCoord(float &c)
220 {
221 if (c > MAP_HALFSIZE - 0.5f)
222 c = MAP_HALFSIZE - 0.5f;
223 else if (c < -(MAP_HALFSIZE - 0.5f))
224 c = -(MAP_HALFSIZE - 0.5f);
225 }
226
227 inline bool IsValidMapCoord(float c)
228 {
229 return std::isfinite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5f);
230 }
231
232 inline bool IsValidMapCoord(float x, float y)
233 {
234 return IsValidMapCoord(x) && IsValidMapCoord(y);
235 }
236
237 inline bool IsValidMapCoord(float x, float y, float z)
238 {
239 return IsValidMapCoord(x, y) && IsValidMapCoord(z);
240 }
241
242 inline bool IsValidMapCoord(float x, float y, float z, float o)
243 {
244 return IsValidMapCoord(x, y, z) && std::isfinite(o);
245 }
246}
247#endif
uint32_t uint32
Definition Define.h:154
GridRefManager< Corpse > CorpseMapType
Definition GridDefines.h:65
GridRefManager< DynamicObject > DynamicObjectMapType
Definition GridDefines.h:67
GridRefManager< Creature > CreatureMapType
Definition GridDefines.h:66
TypeListContainer< GridRefManagerContainer, Player, Creature, Corpse, DynamicObject > WorldTypeMapContainer
Definition GridDefines.h:92
GridRefManager< Player > PlayerMapType
Definition GridDefines.h:69
#define SIZE_OF_GRIDS
Definition GridDefines.h:40
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
TypeListContainer< GridRefManagerContainer, GameObject, Creature, DynamicObject, Corpse, AreaTrigger, SceneObject, Conversation > GridTypeMapContainer
Definition GridDefines.h:91
GridRefManager< Conversation > ConversationMapType
Definition GridDefines.h:72
#define CENTER_GRID_CELL_OFFSET
Definition GridDefines.h:51
GridRefManager< AreaTrigger > AreaTriggerMapType
Definition GridDefines.h:70
#define MAX_NUMBER_OF_GRIDS
Definition GridDefines.h:38
Grid< WorldTypeMapContainer, GridTypeMapContainer > GridType
Definition GridDefines.h:97
#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
NGrid< MAX_NUMBER_OF_CELLS, WorldTypeMapContainer, GridTypeMapContainer > NGridType
Definition GridDefines.h:98
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
GridRefManager< SceneObject > SceneObjectMapType
Definition GridDefines.h:71
#define CENTER_GRID_OFFSET
Definition GridDefines.h:43
#define MAP_HALFSIZE
Definition GridDefines.h:58
GridMapTypeMask
Definition GridDefines.h:75
@ GRID_MAP_TYPE_MASK_PLAYER
Definition GridDefines.h:80
@ GRID_MAP_TYPE_MASK_CREATURE
Definition GridDefines.h:77
@ GRID_MAP_TYPE_MASK_CONVERSATION
Definition GridDefines.h:83
@ GRID_MAP_TYPE_MASK_SCENEOBJECT
Definition GridDefines.h:82
@ GRID_MAP_TYPE_MASK_ALL
Definition GridDefines.h:84
@ GRID_MAP_TYPE_MASK_GAMEOBJECT
Definition GridDefines.h:79
@ GRID_MAP_TYPE_MASK_DYNAMICOBJECT
Definition GridDefines.h:78
@ GRID_MAP_TYPE_MASK_CORPSE
Definition GridDefines.h:76
@ GRID_MAP_TYPE_MASK_AREATRIGGER
Definition GridDefines.h:81
GridRefManager< GameObject > GameObjectMapType
Definition GridDefines.h:68
Definition Grid.h:42
Definition NGrid.h:70
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)
void dec_x(uint32 val)
void inc_x(uint32 val)
CoordPair(const CoordPair< LIMIT > &obj)
uint32 y_coord
friend bool operator==(CoordPair const &p1, CoordPair const &p2)=default
uint32 GetId() const
void dec_y(uint32 val)
CoordPair & normalize()