Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
GridDefines.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SKYFIRE_GRIDDEFINES_H
7#define SKYFIRE_GRIDDEFINES_H
8
9#include "Common.h"
10#include "NGrid.h"
11#include <cmath>
12
13// Forward class definitions
14class Corpse;
15class Creature;
16class DynamicObject;
17class GameObject;
18class Pet;
19class Player;
20class AreaTrigger;
21
22#define MAX_NUMBER_OF_CELLS 8
23
24#define MAX_NUMBER_OF_GRIDS 64
25
26#define SIZE_OF_GRIDS 533.3333f
27#define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
28
29#define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
30
31#define MIN_GRID_DELAY (MINUTE*IN_MILLISECONDS)
32#define MIN_MAP_UPDATE_DELAY 50
33
34#define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
35
36#define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
37#define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
38
39#define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
40
41#define MAP_RESOLUTION 128
42
43#define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
44#define MAP_HALFSIZE (MAP_SIZE/2)
45
46// Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
47typedef TYPELIST_4(Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/) AllWorldObjectTypes;
48typedef TYPELIST_5(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/, AreaTrigger) AllGridObjectTypes;
49
56
67
70
73
74template<uint32 LIMIT>
76{
77 CoordPair(uint32 x = 0, uint32 y = 0)
78 : x_coord(x), y_coord(y)
79 { }
80
82 : x_coord(obj.x_coord), y_coord(obj.y_coord)
83 { }
84
86 {
87 x_coord = obj.x_coord;
88 y_coord = obj.y_coord;
89 return *this;
90 }
91
92 void dec_x(uint32 val)
93 {
94 if (x_coord > val)
95 x_coord -= val;
96 else
97 x_coord = 0;
98 }
99
100 void inc_x(uint32 val)
101 {
102 if (x_coord + val < LIMIT)
103 x_coord += val;
104 else
105 x_coord = LIMIT - 1;
106 }
107
108 void dec_y(uint32 val)
109 {
110 if (y_coord > val)
111 y_coord -= val;
112 else
113 y_coord = 0;
114 }
115
116 void inc_y(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
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
145template<uint32 LIMIT>
147{
148 return (p1.x_coord == p2.x_coord && p1.y_coord == p2.y_coord);
149}
150
151template<uint32 LIMIT>
153{
154 return !(p1 == p2);
155}
156
159
160namespace Skyfire
161{
162 template<class RET_TYPE, int CENTER_VAL>
163 inline RET_TYPE Compute(float x, float y, float center_offset, float size)
164 {
165 // calculate and store temporary values in double format for having same result as same mySQL calculations
166 double x_offset = (double(x) - center_offset) / size;
167 double y_offset = (double(y) - center_offset) / size;
168
169 int x_val = int(x_offset + CENTER_VAL + 0.5f);
170 int y_val = int(y_offset + CENTER_VAL + 0.5f);
171 return RET_TYPE(x_val, y_val);
172 }
173
174 inline GridCoord ComputeGridCoord(float x, float y)
175 {
177 }
178
183
184 inline CellCoord ComputeCellCoord(float x, float y, float& x_off, float& y_off)
185 {
186 double x_offset = (double(x) - CENTER_GRID_CELL_OFFSET) / SIZE_OF_GRID_CELL;
187 double y_offset = (double(y) - CENTER_GRID_CELL_OFFSET) / SIZE_OF_GRID_CELL;
188
189 int x_val = int(x_offset + CENTER_GRID_CELL_ID + 0.5f);
190 int y_val = int(y_offset + CENTER_GRID_CELL_ID + 0.5f);
191 x_off = (float(x_offset) - x_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
192 y_off = (float(y_offset) - y_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
193 return CellCoord(x_val, y_val);
194 }
195
196 inline void NormalizeMapCoord(float& c)
197 {
198 if (c > MAP_HALFSIZE - 0.5f)
199 c = MAP_HALFSIZE - 0.5f;
200 else if (c < -(MAP_HALFSIZE - 0.5f))
201 c = -(MAP_HALFSIZE - 0.5f);
202 }
203
204 inline bool IsValidMapCoord(float c)
205 {
206 return finite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5f);
207 }
208
209 inline bool IsValidMapCoord(float x, float y)
210 {
211 return IsValidMapCoord(x) && IsValidMapCoord(y);
212 }
213
214 inline bool IsValidMapCoord(float x, float y, float z)
215 {
216 return IsValidMapCoord(x, y) && finite(z);
217 }
218
219 inline bool IsValidMapCoord(float x, float y, float z, float o)
220 {
221 return IsValidMapCoord(x, y, z) && finite(o);
222 }
223}
224#endif
#define finite(X)
Definition Common.h:101
std::uint32_t uint32
Definition Define.h:77
GridRefManager< Corpse > CorpseMapType
Definition GridDefines.h:50
GridRefManager< DynamicObject > DynamicObjectMapType
Definition GridDefines.h:52
GridRefManager< Creature > CreatureMapType
Definition GridDefines.h:51
GridRefManager< Player > PlayerMapType
Definition GridDefines.h:54
#define SIZE_OF_GRIDS
Definition GridDefines.h:26
CoordPair< MAX_NUMBER_OF_GRIDS > GridCoord
TypeMapContainer< AllWorldObjectTypes > WorldTypeMapContainer
Definition GridDefines.h:72
TypeMapContainer< AllGridObjectTypes > GridTypeMapContainer
Definition GridDefines.h:71
#define CENTER_GRID_CELL_OFFSET
Definition GridDefines.h:37
GridRefManager< AreaTrigger > AreaTriggerMapType
Definition GridDefines.h:55
#define CENTER_GRID_CELL_ID
Definition GridDefines.h:36
#define SIZE_OF_GRID_CELL
Definition GridDefines.h:34
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
#define CENTER_GRID_OFFSET
Definition GridDefines.h:29
#define MAP_HALFSIZE
Definition GridDefines.h:44
GridMapTypeMask
Definition GridDefines.h:58
@ GRID_MAP_TYPE_MASK_PLAYER
Definition GridDefines.h:63
@ GRID_MAP_TYPE_MASK_CREATURE
Definition GridDefines.h:60
@ GRID_MAP_TYPE_MASK_ALL
Definition GridDefines.h:65
@ GRID_MAP_TYPE_MASK_GAMEOBJECT
Definition GridDefines.h:62
@ GRID_MAP_TYPE_MASK_DYNAMICOBJECT
Definition GridDefines.h:61
@ GRID_MAP_TYPE_MASK_CORPSE
Definition GridDefines.h:59
@ GRID_MAP_TYPE_MASK_AREATRIGGER
Definition GridDefines.h:64
GridRefManager< GameObject > GameObjectMapType
Definition GridDefines.h:53
NGrid< MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes > NGridType
Definition GridDefines.h:69
bool operator!=(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Grid< Player, AllWorldObjectTypes, AllGridObjectTypes > GridType
Definition GridDefines.h:68
#define TYPELIST_4(T1, T2, T3, T4)
Definition TypeList.h:28
#define TYPELIST_5(T1, T2, T3, T4, T5)
Definition TypeList.h:29
Definition Grid.h:34
Definition NGrid.h:65
Definition Pet.h:28
void NormalizeMapCoord(float &c)
bool IsValidMapCoord(float c)
CellCoord ComputeCellCoord(float x, float y)
GridCoord ComputeGridCoord(float x, float y)
RET_TYPE Compute(float x, float y, float center_offset, float size)
bool IsCoordValid() const
void inc_y(uint32 val)
CoordPair< LIMIT > & operator=(const CoordPair< LIMIT > &obj)
Definition GridDefines.h:85
CoordPair(uint32 x=0, uint32 y=0)
Definition GridDefines.h:77
void dec_x(uint32 val)
Definition GridDefines.h:92
void inc_x(uint32 val)
CoordPair(const CoordPair< LIMIT > &obj)
Definition GridDefines.h:81
uint32 GetId() const
void dec_y(uint32 val)
CoordPair & normalize()