[9290] Some cleanups in realmd, no functional changes
[getmangos.git] / src / game / GridDefines.h
blob0f8207ba6ed103af3264517e0ee385e20cd4ad33
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_GRIDDEFINES_H
20 #define MANGOS_GRIDDEFINES_H
22 #include "Common.h"
23 #include "GameSystem/NGrid.h"
24 #include <cmath>
26 // Forward class definitions
27 class Corpse;
28 class Creature;
29 class Vehicle;
30 class DynamicObject;
31 class GameObject;
32 class Pet;
33 class Player;
35 #define MAX_NUMBER_OF_GRIDS 64
37 #define SIZE_OF_GRIDS 533.33333f
38 #define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
40 #define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
42 #define MIN_GRID_DELAY (MINUTE*IN_MILISECONDS)
43 #define MIN_MAP_UPDATE_DELAY 50
45 #define MAX_NUMBER_OF_CELLS 8
46 #define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
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)
51 #define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
53 #define MAP_RESOLUTION 128
55 #define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
56 #define MAP_HALFSIZE (MAP_SIZE/2)
58 // Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
59 typedef TYPELIST_3(Player, Creature/*pets*/, Corpse/*resurrectable*/) AllWorldObjectTypes;
60 typedef TYPELIST_4(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/) AllGridObjectTypes;
61 typedef TYPELIST_5(Creature, Pet, Vehicle, GameObject, DynamicObject) AllMapStoredObjectTypes;
63 typedef GridRefManager<Corpse> CorpseMapType;
64 typedef GridRefManager<Creature> CreatureMapType;
65 typedef GridRefManager<DynamicObject> DynamicObjectMapType;
66 typedef GridRefManager<GameObject> GameObjectMapType;
67 typedef GridRefManager<Player> PlayerMapType;
69 typedef Grid<Player, AllWorldObjectTypes,AllGridObjectTypes> GridType;
70 typedef NGrid<MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes> NGridType;
72 typedef TypeMapContainer<AllGridObjectTypes> GridTypeMapContainer;
73 typedef TypeMapContainer<AllWorldObjectTypes> WorldTypeMapContainer;
75 template<const unsigned int LIMIT>
76 struct MANGOS_DLL_DECL CoordPair
78 CoordPair(uint32 x=0, uint32 y=0) : x_coord(x), y_coord(y) {}
79 CoordPair(const CoordPair<LIMIT> &obj) : x_coord(obj.x_coord), y_coord(obj.y_coord) {}
80 bool operator==(const CoordPair<LIMIT> &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); }
81 bool operator!=(const CoordPair<LIMIT> &obj) const { return !operator==(obj); }
82 CoordPair<LIMIT>& operator=(const CoordPair<LIMIT> &obj)
84 x_coord = obj.x_coord;
85 y_coord = obj.y_coord;
86 return *this;
89 void operator<<(const uint32 val)
91 if( x_coord > val )
92 x_coord -= val;
93 else
94 x_coord = 0;
97 void operator>>(const uint32 val)
99 if( x_coord+val < LIMIT )
100 x_coord += val;
101 else
102 x_coord = LIMIT - 1;
105 void operator-=(const uint32 val)
107 if( y_coord > val )
108 y_coord -= val;
109 else
110 y_coord = 0;
113 void operator+=(const uint32 val)
115 if( y_coord+val < LIMIT )
116 y_coord += val;
117 else
118 y_coord = LIMIT - 1;
121 uint32 x_coord;
122 uint32 y_coord;
125 typedef CoordPair<MAX_NUMBER_OF_GRIDS> GridPair;
126 typedef CoordPair<TOTAL_NUMBER_OF_CELLS_PER_MAP> CellPair;
128 namespace MaNGOS
130 template<class RET_TYPE, int CENTER_VAL>
131 inline RET_TYPE Compute(float x, float y, float center_offset, float size)
133 // calculate and store temporary values in double format for having same result as same mySQL calculations
134 double x_offset = (double(x) - center_offset)/size;
135 double y_offset = (double(y) - center_offset)/size;
137 int x_val = int(x_offset+CENTER_VAL + 0.5);
138 int y_val = int(y_offset+CENTER_VAL + 0.5);
139 return RET_TYPE(x_val, y_val);
142 inline GridPair ComputeGridPair(float x, float y)
144 return Compute<GridPair, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
147 inline CellPair ComputeCellPair(float x, float y)
149 return Compute<CellPair, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
152 inline void NormalizeMapCoord(float &c)
154 if(c > MAP_HALFSIZE - 0.5)
155 c = MAP_HALFSIZE - 0.5;
156 else if(c < -(MAP_HALFSIZE - 0.5))
157 c = -(MAP_HALFSIZE - 0.5);
160 inline bool IsValidMapCoord(float c)
162 return finite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5);
165 inline bool IsValidMapCoord(float x, float y)
167 return IsValidMapCoord(x) && IsValidMapCoord(y);
170 inline bool IsValidMapCoord(float x, float y, float z)
172 return IsValidMapCoord(x,y) && finite(z);
175 inline bool IsValidMapCoord(float x, float y, float z, float o)
177 return IsValidMapCoord(x,y,z) && finite(o);
180 #endif