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{
103 constexpr CoordPair(uint32 x, uint32 y)
104 : x_coord(x), y_coord(y) { }
105
106 constexpr void dec_x(uint32 val)
107 {
108 if (x_coord > val)
109 x_coord -= val;
110 else
111 x_coord = 0;
112 }
113
114 constexpr 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 constexpr void dec_y(uint32 val)
123 {
124 if (y_coord > val)
125 y_coord -= val;
126 else
127 y_coord = 0;
128 }
129
130 constexpr 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 constexpr bool IsCoordValid() const
139 {
140 return x_coord < LIMIT && y_coord < LIMIT;
141 }
142
143 constexpr uint32 GetId() const
144 {
145 return y_coord * LIMIT + x_coord;
146 }
147
148 friend constexpr bool operator==(CoordPair const& p1, CoordPair const& p2) = default;
149
152};
153
156
157namespace Trinity
158{
159 template<int32 LIMIT, int32 CENTER_VAL>
160 inline constexpr CoordPair<LIMIT> Compute(float x, float y, float center_offset, float size)
161 {
162 // calculate and store temporary values in double format for having same result as same mySQL calculations
163 double x_offset = (double(x) - center_offset) / size;
164 double y_offset = (double(y) - center_offset) / size;
165
166 int32 x_val = int32(x_offset + CENTER_VAL + 0.5);
167 int32 y_val = int32(y_offset + CENTER_VAL + 0.5);
168 return CoordPair<LIMIT>(
169 x_val >= 0 ? x_val < LIMIT ? x_val : LIMIT - 1 : 0,
170 y_val >= 0 ? y_val < LIMIT ? y_val : LIMIT - 1 : 0
171 );
172 }
173
174 inline constexpr GridCoord ComputeGridCoord(float x, float y)
175 {
176 return Compute<MAX_NUMBER_OF_GRIDS, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
177 }
178
179 inline constexpr GridCoord ComputeGridCoordSimple(float x, float y)
180 {
181 int gx = (int)(CENTER_GRID_ID - x / SIZE_OF_GRIDS);
182 int gy = (int)(CENTER_GRID_ID - y / SIZE_OF_GRIDS);
183 return GridCoord((MAX_NUMBER_OF_GRIDS - 1) - gx, (MAX_NUMBER_OF_GRIDS - 1) - gy);
184 }
185
186 inline constexpr CellCoord ComputeCellCoord(float x, float y)
187 {
188 return Compute<TOTAL_NUMBER_OF_CELLS_PER_MAP, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
189 }
190
191 inline constexpr void NormalizeMapCoord(float &c)
192 {
193 if (c > MAP_HALFSIZE - 0.5f)
194 c = MAP_HALFSIZE - 0.5f;
195 else if (c < -(MAP_HALFSIZE - 0.5f))
196 c = -(MAP_HALFSIZE - 0.5f);
197 }
198
199 inline bool IsValidMapCoord(float c)
200 {
201 return std::isfinite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5f);
202 }
203
204 inline bool IsValidMapCoord(float x, float y)
205 {
206 return IsValidMapCoord(x) && IsValidMapCoord(y);
207 }
208
209 inline bool IsValidMapCoord(float x, float y, float z)
210 {
211 return IsValidMapCoord(x, y) && IsValidMapCoord(z);
212 }
213
214 inline bool IsValidMapCoord(float x, float y, float z, float o)
215 {
216 return IsValidMapCoord(x, y, z) && std::isfinite(o);
217 }
218}
219#endif
int32_t int32
Definition Define.h:150
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 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
constexpr GridCoord ComputeGridCoordSimple(float x, float y)
bool IsValidMapCoord(float c)
constexpr CoordPair< LIMIT > Compute(float x, float y, float center_offset, float size)
constexpr void NormalizeMapCoord(float &c)
constexpr CellCoord ComputeCellCoord(float x, float y)
constexpr GridCoord ComputeGridCoord(float x, float y)
friend constexpr bool operator==(CoordPair const &p1, CoordPair const &p2)=default
uint32 x_coord
constexpr void inc_y(uint32 val)
constexpr void dec_y(uint32 val)
constexpr void dec_x(uint32 val)
constexpr uint32 GetId() const
constexpr CoordPair(uint32 x, uint32 y)
constexpr bool IsCoordValid() const
uint32 y_coord
constexpr void inc_x(uint32 val)