Files
TCPlayerbotCore/src/server/game/Grids/GridDefines.h
T

192 lines
6.1 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
#ifndef TRINITY_GRIDDEFINES_H
#define TRINITY_GRIDDEFINES_H
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
2010-06-08 13:05:47 -06:00
#include "NGrid.h"
2008-10-02 16:23:55 -05:00
#include <cmath>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Forward class definitions
class Corpse;
class Creature;
class DynamicObject;
class GameObject;
class Pet;
class Player;
2009-10-17 15:51:44 -07:00
#define MAX_NUMBER_OF_CELLS 8
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define MAX_NUMBER_OF_GRIDS 64
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define SIZE_OF_GRIDS 533.33333f
#define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
2009-10-17 15:51:44 -07:00
#define MIN_GRID_DELAY (MINUTE*IN_MILLISECONDS)
2008-10-02 16:23:55 -05:00
#define MIN_MAP_UPDATE_DELAY 50
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
2009-10-17 15:51:44 -07:00
#define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
2008-10-02 16:23:55 -05:00
#define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
2009-10-17 15:51:44 -07:00
#define MAP_RESOLUTION 128
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
#define MAP_HALFSIZE (MAP_SIZE/2)
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Creature used instead pet to simplify *::Visit templates (not required duplicate code for Creature->Pet case)
typedef TYPELIST_4(Player, Creature/*pets*/, Corpse/*resurrectable*/, DynamicObject/*farsight target*/) AllWorldObjectTypes;
2008-10-02 16:23:55 -05:00
typedef TYPELIST_4(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/) AllGridObjectTypes;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
typedef GridRefManager<Corpse> CorpseMapType;
typedef GridRefManager<Creature> CreatureMapType;
typedef GridRefManager<DynamicObject> DynamicObjectMapType;
typedef GridRefManager<GameObject> GameObjectMapType;
typedef GridRefManager<Player> PlayerMapType;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
typedef Grid<Player, AllWorldObjectTypes,AllGridObjectTypes> GridType;
typedef NGrid<MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes> NGridType;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
typedef TypeMapContainer<AllGridObjectTypes> GridTypeMapContainer;
typedef TypeMapContainer<AllWorldObjectTypes> WorldTypeMapContainer;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template<const unsigned int LIMIT>
2010-02-05 00:31:28 +01:00
struct CoordPair
2008-10-02 16:23:55 -05:00
{
CoordPair(uint32 x=0, uint32 y=0) : x_coord(x), y_coord(y) {}
CoordPair(const CoordPair<LIMIT> &obj) : x_coord(obj.x_coord), y_coord(obj.y_coord) {}
2010-04-07 23:25:02 +02:00
bool operator == (const CoordPair<LIMIT> &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); }
2010-04-08 08:20:08 +02:00
bool operator != (const CoordPair<LIMIT> &obj) const { return !operator == (obj); }
2008-10-02 16:23:55 -05:00
CoordPair<LIMIT>& operator=(const CoordPair<LIMIT> &obj)
{
x_coord = obj.x_coord;
y_coord = obj.y_coord;
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void operator<<(const uint32 val)
{
2010-04-07 22:59:46 +02:00
if (x_coord > val)
2008-10-02 16:23:55 -05:00
x_coord -= val;
2009-02-24 20:05:29 -06:00
else
x_coord = 0;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void operator>>(const uint32 val)
{
2010-04-07 22:59:46 +02:00
if (x_coord+val < LIMIT)
2008-10-02 16:23:55 -05:00
x_coord += val;
2009-02-24 20:05:29 -06:00
else
x_coord = LIMIT - 1;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void operator-=(const uint32 val)
{
2010-04-07 22:59:46 +02:00
if (y_coord > val)
2008-10-02 16:23:55 -05:00
y_coord -= val;
2009-02-24 20:05:29 -06:00
else
y_coord = 0;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void operator+=(const uint32 val)
{
2010-04-07 22:59:46 +02:00
if (y_coord+val < LIMIT)
2008-10-02 16:23:55 -05:00
y_coord += val;
2009-02-24 20:05:29 -06:00
else
y_coord = LIMIT - 1;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 x_coord;
uint32 y_coord;
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
typedef CoordPair<MAX_NUMBER_OF_GRIDS> GridPair;
typedef CoordPair<TOTAL_NUMBER_OF_CELLS_PER_MAP> CellPair;
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
namespace Trinity
2008-10-02 16:23:55 -05:00
{
template<class RET_TYPE, int CENTER_VAL>
inline RET_TYPE Compute(float x, float y, float center_offset, float size)
{
// calculate and store temporary values in double format for having same result as same mySQL calculations
double x_offset = (double(x) - center_offset)/size;
double y_offset = (double(y) - center_offset)/size;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
int x_val = int(x_offset+CENTER_VAL + 0.5);
int y_val = int(y_offset+CENTER_VAL + 0.5);
return RET_TYPE(x_val, y_val);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline GridPair ComputeGridPair(float x, float y)
{
return Compute<GridPair, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline CellPair ComputeCellPair(float x, float y)
{
return Compute<CellPair, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
}
2009-10-17 15:51:44 -07:00
inline CellPair ComputeCellPair(float x, float y, float &x_off, float &y_off)
{
double x_offset = (double(x) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
double y_offset = (double(y) - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
2009-10-17 15:51:44 -07:00
2009-01-06 11:11:35 -06:00
int x_val = int(x_offset + CENTER_GRID_CELL_ID + 0.5);
int y_val = int(y_offset + CENTER_GRID_CELL_ID + 0.5);
x_off = (float(x_offset) - x_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
y_off = (float(y_offset) - y_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
return CellPair(x_val, y_val);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline void NormalizeMapCoord(float &c)
{
2010-04-07 19:14:10 +02:00
if (c > MAP_HALFSIZE - 0.5)
2008-10-02 16:23:55 -05:00
c = MAP_HALFSIZE - 0.5;
2010-04-07 19:14:10 +02:00
else if (c < -(MAP_HALFSIZE - 0.5))
2008-10-02 16:23:55 -05:00
c = -(MAP_HALFSIZE - 0.5);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline bool IsValidMapCoord(float c)
{
return finite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline bool IsValidMapCoord(float x, float y)
{
return IsValidMapCoord(x) && IsValidMapCoord(y);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline bool IsValidMapCoord(float x, float y, float z)
{
return IsValidMapCoord(x,y) && finite(z);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline bool IsValidMapCoord(float x, float y, float z, float o)
{
return IsValidMapCoord(x,y,z) && finite(o);
}
}
#endif