* Move some grid stuff.

--HG--
branch : trunk
This commit is contained in:
XTZGZoReX
2010-06-06 23:32:59 +02:00
parent 7cb74345e0
commit 1170c67a70
10 changed files with 0 additions and 0 deletions
-177
View File
@@ -1,177 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_CELL_H
#define TRINITY_CELL_H
#include <cmath>
#include "GameSystem/TypeContainer.h"
#include "GameSystem/TypeContainerVisitor.h"
#include "GridDefines.h"
class Map;
class WorldObject;
enum District
{
UPPER_DISTRICT = 1,
LOWER_DISTRICT = 1 << 1,
LEFT_DISTRICT = 1 << 2,
RIGHT_DISTRICT = 1 << 3,
CENTER_DISTRICT = 1 << 4,
UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT),
UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT),
LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT),
LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT),
ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT)
};
struct CellArea
{
CellArea() : right_offset(0), left_offset(0), upper_offset(0), lower_offset(0) {}
CellArea(int right, int left, int upper, int lower) : right_offset(right), left_offset(left), upper_offset(upper), lower_offset(lower) {}
bool operator!() const { return !right_offset && !left_offset && !upper_offset && !lower_offset; }
void ResizeBorders(CellPair& begin_cell, CellPair& end_cell) const
{
begin_cell << left_offset;
begin_cell -= lower_offset;
end_cell >> right_offset;
end_cell += upper_offset;
}
int right_offset;
int left_offset;
int upper_offset;
int lower_offset;
};
struct Cell
{
Cell() { data.All = 0; }
Cell(const Cell &cell) { data.All = cell.data.All; }
explicit Cell(CellPair const& p);
void operator|=(Cell &cell)
{
data.Part.reserved = 0;
cell.data.Part.reserved = 0;
uint32 x, y, old_x, old_y;
Compute(x, y);
cell.Compute(old_x, old_y);
if (std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1)
{
data.Part.reserved = ALL_DISTRICT;
cell.data.Part.reserved = ALL_DISTRICT;
return;
}
if (x < old_x)
{
data.Part.reserved |= LEFT_DISTRICT;
cell.data.Part.reserved |= RIGHT_DISTRICT;
}
else if (old_x < x)
{
data.Part.reserved |= RIGHT_DISTRICT;
cell.data.Part.reserved |= LEFT_DISTRICT;
}
if (y < old_y)
{
data.Part.reserved |= UPPER_DISTRICT;
cell.data.Part.reserved |= LOWER_DISTRICT;
}
else if (old_y < y)
{
data.Part.reserved |= LOWER_DISTRICT;
cell.data.Part.reserved |= UPPER_DISTRICT;
}
}
void Compute(uint32 &x, uint32 &y) const
{
x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x;
y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y;
}
bool DiffCell(const Cell &cell) const
{
return(data.Part.cell_x != cell.data.Part.cell_x ||
data.Part.cell_y != cell.data.Part.cell_y);
}
bool DiffGrid(const Cell &cell) const
{
return(data.Part.grid_x != cell.data.Part.grid_x ||
data.Part.grid_y != cell.data.Part.grid_y);
}
uint32 CellX() const { return data.Part.cell_x; }
uint32 CellY() const { return data.Part.cell_y; }
uint32 GridX() const { return data.Part.grid_x; }
uint32 GridY() const { return data.Part.grid_y; }
bool NoCreate() const { return data.Part.nocreate; }
void SetNoCreate() { data.Part.nocreate = 1; }
CellPair cellPair() const
{
return CellPair(
data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x,
data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y);
}
Cell& operator=(const Cell &cell)
{
this->data.All = cell.data.All;
return *this;
}
bool operator == (const Cell &cell) const { return (data.All == cell.data.All); }
bool operator != (const Cell &cell) const { return !operator == (cell); }
union
{
struct
{
unsigned grid_x : 6;
unsigned grid_y : 6;
unsigned cell_x : 6;
unsigned cell_y : 6;
unsigned nocreate : 1;
unsigned reserved : 7;
} Part;
uint32 All;
} data;
template<class T, class CONTAINER> void Visit(const CellPair&, TypeContainerVisitor<T, CONTAINER> &visitor, Map &) const;
template<class T, class CONTAINER> void Visit(const CellPair&, TypeContainerVisitor<T, CONTAINER> &visitor, Map &, const WorldObject&, float) const;
template<class T, class CONTAINER> void Visit(const CellPair&, TypeContainerVisitor<T, CONTAINER> &visitor, Map &, float, float, float) const;
static CellArea CalculateCellArea(const WorldObject &obj, float radius);
static CellArea CalculateCellArea(float x, float y, float radius);
private:
template<class T, class CONTAINER> void VisitCircle(TypeContainerVisitor<T, CONTAINER> &, Map &, const CellPair&, const CellPair&) const;
};
#endif
-297
View File
@@ -1,297 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_CELLIMPL_H
#define TRINITY_CELLIMPL_H
#include <cmath>
#include "Cell.h"
#include "Map.h"
#include "Object.h"
inline Cell::Cell(CellPair const& p)
{
data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS;
data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS;
data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS;
data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS;
data.Part.nocreate = 0;
data.Part.reserved = 0;
}
template<class T, class CONTAINER>
inline void
Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor<T, CONTAINER> &visitor, Map &m) const
{
if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
return;
uint16 district = (District)this->data.Part.reserved;
if (district == CENTER_DISTRICT)
{
m.Visit(*this, visitor);
return;
}
// set up the cell range based on the district
// the overloaded operators handle range checking
CellPair begin_cell = standing_cell;
CellPair end_cell = standing_cell;
switch(district)
{
case ALL_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell >> 1; end_cell += 1; // lower right
break;
}
case UPPER_LEFT_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
break;
}
case UPPER_RIGHT_DISTRICT:
{
begin_cell -= 1; // up
end_cell >> 1; // right
break;
}
case LOWER_LEFT_DISTRICT:
{
begin_cell << 1; // left
end_cell += 1; // down
break;
}
case LOWER_RIGHT_DISTRICT:
{
end_cell >> 1; end_cell += 1; // lower right
break;
}
case LEFT_DISTRICT:
{
begin_cell -= 1; // up
end_cell >> 1; end_cell += 1; // lower right
break;
}
case RIGHT_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell += 1; // down
break;
}
case UPPER_DISTRICT:
{
begin_cell << 1; begin_cell -= 1; // upper left
end_cell >> 1; // right
break;
}
case LOWER_DISTRICT:
{
begin_cell << 1; // left
end_cell >> 1; end_cell += 1; // lower right
break;
}
default:
{
assert(false);
break;
}
}
// loop the cell range
for (uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; x++)
{
for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; y++)
{
CellPair cell_pair(x,y);
Cell r_zone(cell_pair);
r_zone.data.Part.nocreate = this->data.Part.nocreate;
m.Visit(r_zone, visitor);
}
}
}
inline int CellHelper(const float radius)
{
if (radius < 1.0f)
return 0;
return (int)ceilf(radius/SIZE_OF_GRID_CELL);
}
inline CellArea Cell::CalculateCellArea(const WorldObject &obj, float radius)
{
return Cell::CalculateCellArea(obj.GetPositionX(), obj.GetPositionY(), radius);
}
inline CellArea Cell::CalculateCellArea(float x, float y, float radius)
{
if (radius <= 0.0f)
return CellArea();
//lets calculate object coord offsets from cell borders.
//TODO: add more correct/generic method for this task
const float x_offset = (x - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
const float y_offset = (y - CENTER_GRID_CELL_OFFSET)/SIZE_OF_GRID_CELL;
const float x_val = floor(x_offset + CENTER_GRID_CELL_ID + 0.5f);
const float y_val = floor(y_offset + CENTER_GRID_CELL_ID + 0.5f);
const float x_off = (x_offset - x_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
const float y_off = (y_offset - y_val + CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL;
const float tmp_diff = radius - CENTER_GRID_CELL_OFFSET;
//lets calculate upper/lower/right/left corners for cell search
int right = CellHelper(tmp_diff + x_off);
int left = CellHelper(tmp_diff - x_off);
int upper = CellHelper(tmp_diff + y_off);
int lower = CellHelper(tmp_diff - y_off);
return CellArea(right, left, upper, lower);
}
template<class T, class CONTAINER>
inline void
Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor<T, CONTAINER> &visitor, Map &m, float radius, float x_off, float y_off) const
{
if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
return;
//no jokes here... Actually placing ASSERT() here was good idea, but
//we had some problems with DynamicObjects, which pass radius = 0.0f (DB issue?)
//maybe it is better to just return when radius <= 0.0f?
if (radius <= 0.0f)
{
m.Visit(*this, visitor);
return;
}
//lets limit the upper value for search radius
if (radius > 333.0f)
radius = 333.0f;
//lets calculate object coord offsets from cell borders.
CellArea area = Cell::CalculateCellArea(x_off, y_off, radius);
//if radius fits inside standing cell
if (!area)
{
m.Visit(*this, visitor);
return;
}
CellPair begin_cell = standing_cell;
CellPair end_cell = standing_cell;
area.ResizeBorders(begin_cell, end_cell);
//visit all cells, found in CalculateCellArea()
//if radius is known to reach cell area more than 4x4 then we should call optimized VisitCircle
//currently this technique works with MAX_NUMBER_OF_CELLS 16 and higher, with lower values
//there are nothing to optimize because SIZE_OF_GRID_CELL is too big...
if (((end_cell.x_coord - begin_cell.x_coord) > 4) && ((end_cell.y_coord - begin_cell.y_coord) > 4))
{
VisitCircle(visitor, m, begin_cell, end_cell);
return;
}
//ALWAYS visit standing cell first!!! Since we deal with small radiuses
//it is very essential to call visitor for standing cell firstly...
m.Visit(*this, visitor);
// loop the cell range
for (uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x)
{
for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
{
CellPair cell_pair(x,y);
//lets skip standing cell since we already visited it
if (cell_pair != standing_cell)
{
Cell r_zone(cell_pair);
r_zone.data.Part.nocreate = this->data.Part.nocreate;
m.Visit(r_zone, visitor);
}
}
}
}
template<class T, class CONTAINER>
inline void
Cell::Visit(const CellPair& l, TypeContainerVisitor<T, CONTAINER> &visitor, Map &m, const WorldObject &obj, float radius) const
{
//we should increase search radius by object's radius, otherwise
//we could have problems with huge creatures, which won't attack nearest players etc
Visit(l, visitor, m, radius + obj.GetObjectSize(), obj.GetPositionX(), obj.GetPositionY());
}
template<class T, class CONTAINER>
inline void
Cell::VisitCircle(TypeContainerVisitor<T, CONTAINER> &visitor, Map &m, const CellPair& begin_cell, const CellPair& end_cell) const
{
//here is an algorithm for 'filling' circum-squared octagon
uint32 x_shift = (uint32)ceilf((end_cell.x_coord - begin_cell.x_coord) * 0.3f - 0.5f);
//lets calculate x_start/x_end coords for central strip...
const uint32 x_start = begin_cell.x_coord + x_shift;
const uint32 x_end = end_cell.x_coord - x_shift;
//visit central strip with constant width...
for (uint32 x = x_start; x <= x_end; ++x)
{
for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
{
CellPair cell_pair(x,y);
Cell r_zone(cell_pair);
r_zone.data.Part.nocreate = this->data.Part.nocreate;
m.Visit(r_zone, visitor);
}
}
//if x_shift == 0 then we have too small cell area, which were already
//visited at previous step, so just return from procedure...
if (x_shift == 0)
return;
uint32 y_start = end_cell.y_coord;
uint32 y_end = begin_cell.y_coord;
//now we are visiting borders of an octagon...
for (uint32 step = 1; step <= (x_start - begin_cell.x_coord); ++step)
{
//each step reduces strip height by 2 cells...
y_end += 1;
y_start -= 1;
for (uint32 y = y_start; y >= y_end; --y)
{
//we visit cells symmetrically from both sides, heading from center to sides and from up to bottom
//e.g. filling 2 trapezoids after filling central cell strip...
CellPair cell_pair_left(x_start - step, y);
Cell r_zone_left(cell_pair_left);
r_zone_left.data.Part.nocreate = this->data.Part.nocreate;
m.Visit(r_zone_left, visitor);
//right trapezoid cell visit
CellPair cell_pair_right(x_end + step, y);
Cell r_zone_right(cell_pair_right);
r_zone_right.data.Part.nocreate = this->data.Part.nocreate;
m.Visit(r_zone_right, visitor);
}
}
}
#endif
-193
View File
@@ -1,193 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_GRIDDEFINES_H
#define TRINITY_GRIDDEFINES_H
#include "Common.h"
#include "GameSystem/NGrid.h"
#include <cmath>
// Forward class definitions
class Corpse;
class Creature;
class DynamicObject;
class GameObject;
class Pet;
class Player;
#define MAX_NUMBER_OF_CELLS 8
#define MAX_NUMBER_OF_GRIDS 64
#define SIZE_OF_GRIDS 533.33333f
#define CENTER_GRID_ID (MAX_NUMBER_OF_GRIDS/2)
#define CENTER_GRID_OFFSET (SIZE_OF_GRIDS/2)
#define MIN_GRID_DELAY (MINUTE*IN_MILISECONDS)
#define MIN_MAP_UPDATE_DELAY 50
#define SIZE_OF_GRID_CELL (SIZE_OF_GRIDS/MAX_NUMBER_OF_CELLS)
#define CENTER_GRID_CELL_ID (MAX_NUMBER_OF_CELLS*MAX_NUMBER_OF_GRIDS/2)
#define CENTER_GRID_CELL_OFFSET (SIZE_OF_GRID_CELL/2)
#define TOTAL_NUMBER_OF_CELLS_PER_MAP (MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_CELLS)
#define MAP_RESOLUTION 128
#define MAP_SIZE (SIZE_OF_GRIDS*MAX_NUMBER_OF_GRIDS)
#define MAP_HALFSIZE (MAP_SIZE/2)
// 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;
typedef TYPELIST_4(GameObject, Creature/*except pets*/, DynamicObject, Corpse/*Bones*/) AllGridObjectTypes;
typedef GridRefManager<Corpse> CorpseMapType;
typedef GridRefManager<Creature> CreatureMapType;
typedef GridRefManager<DynamicObject> DynamicObjectMapType;
typedef GridRefManager<GameObject> GameObjectMapType;
typedef GridRefManager<Player> PlayerMapType;
typedef Grid<Player, AllWorldObjectTypes,AllGridObjectTypes> GridType;
typedef NGrid<MAX_NUMBER_OF_CELLS, Player, AllWorldObjectTypes, AllGridObjectTypes> NGridType;
typedef TypeMapContainer<AllGridObjectTypes> GridTypeMapContainer;
typedef TypeMapContainer<AllWorldObjectTypes> WorldTypeMapContainer;
template<const unsigned int LIMIT>
struct CoordPair
{
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) {}
bool operator == (const CoordPair<LIMIT> &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); }
bool operator != (const CoordPair<LIMIT> &obj) const { return !operator == (obj); }
CoordPair<LIMIT>& operator=(const CoordPair<LIMIT> &obj)
{
x_coord = obj.x_coord;
y_coord = obj.y_coord;
return *this;
}
void operator<<(const uint32 val)
{
if (x_coord > val)
x_coord -= val;
else
x_coord = 0;
}
void operator>>(const uint32 val)
{
if (x_coord+val < LIMIT)
x_coord += val;
else
x_coord = LIMIT - 1;
}
void operator-=(const uint32 val)
{
if (y_coord > val)
y_coord -= val;
else
y_coord = 0;
}
void operator+=(const uint32 val)
{
if (y_coord+val < LIMIT)
y_coord += val;
else
y_coord = LIMIT - 1;
}
uint32 x_coord;
uint32 y_coord;
};
typedef CoordPair<MAX_NUMBER_OF_GRIDS> GridPair;
typedef CoordPair<TOTAL_NUMBER_OF_CELLS_PER_MAP> CellPair;
namespace Trinity
{
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;
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);
}
inline GridPair ComputeGridPair(float x, float y)
{
return Compute<GridPair, CENTER_GRID_ID>(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS);
}
inline CellPair ComputeCellPair(float x, float y)
{
return Compute<CellPair, CENTER_GRID_CELL_ID>(x, y, CENTER_GRID_CELL_OFFSET, SIZE_OF_GRID_CELL);
}
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;
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);
}
inline void NormalizeMapCoord(float &c)
{
if (c > MAP_HALFSIZE - 0.5)
c = MAP_HALFSIZE - 0.5;
else if (c < -(MAP_HALFSIZE - 0.5))
c = -(MAP_HALFSIZE - 0.5);
}
inline bool IsValidMapCoord(float c)
{
return finite(c) && (std::fabs(c) <= MAP_HALFSIZE - 0.5);
}
inline bool IsValidMapCoord(float x, float y)
{
return IsValidMapCoord(x) && IsValidMapCoord(y);
}
inline bool IsValidMapCoord(float x, float y, float z)
{
return IsValidMapCoord(x,y) && finite(z);
}
inline bool IsValidMapCoord(float x, float y, float z, float o)
{
return IsValidMapCoord(x,y,z) && finite(o);
}
}
#endif
-353
View File
@@ -1,353 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "UpdateData.h"
#include "Item.h"
#include "Map.h"
#include "Transports.h"
#include "ObjectAccessor.h"
#include "CellImpl.h"
using namespace Trinity;
void
VisibleNotifier::SendToSelf()
{
// at this moment i_clientGUIDs have guids that not iterate at grid level checks
// but exist one case when this possible and object not out of range: transports
if (Transport* transport = i_player.GetTransport())
for (Transport::PlayerSet::const_iterator itr = transport->GetPassengers().begin();itr != transport->GetPassengers().end();++itr)
{
if (vis_guids.find((*itr)->GetGUID()) != vis_guids.end())
{
vis_guids.erase((*itr)->GetGUID());
i_player.UpdateVisibilityOf((*itr), i_data, i_visibleNow);
if (!(*itr)->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
(*itr)->UpdateVisibilityOf(&i_player);
}
}
for (Player::ClientGUIDs::const_iterator it = vis_guids.begin();it != vis_guids.end(); ++it)
{
i_player.m_clientGUIDs.erase(*it);
i_data.AddOutOfRangeGUID(*it);
if (IS_PLAYER_GUID(*it))
{
Player* plr = ObjectAccessor::FindPlayer(*it);
if (plr && plr->IsInWorld() && !plr->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
plr->UpdateVisibilityOf(&i_player);
}
}
if (!i_data.HasData())
return;
WorldPacket packet;
i_data.BuildPacket(&packet);
i_player.GetSession()->SendPacket(&packet);
for (std::set<Unit*>::const_iterator it = i_visibleNow.begin(); it != i_visibleNow.end(); ++it)
i_player.SendInitialVisiblePackets(*it);
}
void
VisibleChangesNotifier::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
if (iter->getSource() == &i_object)
continue;
iter->getSource()->UpdateVisibilityOf(&i_object);
if (!iter->getSource()->GetSharedVisionList().empty())
for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin();
i != iter->getSource()->GetSharedVisionList().end(); ++i)
if ((*i)->m_seer == iter->getSource())
(*i)->UpdateVisibilityOf(&i_object);
}
}
void
VisibleChangesNotifier::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
if (!iter->getSource()->GetSharedVisionList().empty())
for (SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin();
i != iter->getSource()->GetSharedVisionList().end(); ++i)
if ((*i)->m_seer == iter->getSource())
(*i)->UpdateVisibilityOf(&i_object);
}
void
VisibleChangesNotifier::Visit(DynamicObjectMapType &m)
{
for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID()))
if (Player* caster = (Player*)iter->getSource()->GetCaster())
if (caster->m_seer == iter->getSource())
caster->UpdateVisibilityOf(&i_object);
}
inline void CreatureUnitRelocationWorker(Creature* c, Unit* u)
{
if (!u->isAlive() || !c->isAlive() || c == u || u->isInFlight())
return;
if (c->HasReactState(REACT_AGGRESSIVE) && !c->hasUnitState(UNIT_STAT_SIGHTLESS))
if (c->_IsWithinDist(u, c->m_SightDistance, true) && c->IsAIEnabled)
c->AI()->MoveInLineOfSight_Safe(u);
}
void PlayerRelocationNotifier::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
Player* plr = iter->getSource();
vis_guids.erase(plr->GetGUID());
i_player.UpdateVisibilityOf(plr,i_data,i_visibleNow);
if (plr->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
continue;
plr->UpdateVisibilityOf(&i_player);
}
}
void PlayerRelocationNotifier::Visit(CreatureMapType &m)
{
bool relocated_for_ai = (&i_player == i_player.m_seer);
for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
Creature * c = iter->getSource();
vis_guids.erase(c->GetGUID());
i_player.UpdateVisibilityOf(c,i_data,i_visibleNow);
if (relocated_for_ai && !c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
CreatureUnitRelocationWorker(c, &i_player);
}
}
void CreatureRelocationNotifier::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
Player * pl = iter->getSource();
if (!pl->m_seer->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
pl->UpdateVisibilityOf(&i_creature);
CreatureUnitRelocationWorker(&i_creature, pl);
}
}
void CreatureRelocationNotifier::Visit(CreatureMapType &m)
{
if (!i_creature.isAlive())
return;
for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
Creature* c = iter->getSource();
CreatureUnitRelocationWorker(&i_creature, c);
if (!c->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
CreatureUnitRelocationWorker(c, &i_creature);
}
}
void DelayedUnitRelocation::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
Creature * unit = iter->getSource();
if (!unit->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
continue;
CreatureRelocationNotifier relocate(*unit);
TypeContainerVisitor<CreatureRelocationNotifier, WorldTypeMapContainer > c2world_relocation(relocate);
TypeContainerVisitor<CreatureRelocationNotifier, GridTypeMapContainer > c2grid_relocation(relocate);
cell.Visit(p, c2world_relocation, i_map, *unit, i_radius);
cell.Visit(p, c2grid_relocation, i_map, *unit, i_radius);
}
}
void DelayedUnitRelocation::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
Player * player = iter->getSource();
WorldObject const *viewPoint = player->m_seer;
if (!viewPoint->isNeedNotify(NOTIFY_VISIBILITY_CHANGED))
continue;
if (player != viewPoint && !viewPoint->IsPositionValid())
continue;
CellPair pair2(Trinity::ComputeCellPair(viewPoint->GetPositionX(), viewPoint->GetPositionY()));
Cell cell2(pair2);
//cell.SetNoCreate(); need load cells around viewPoint or player, that's why its commented
PlayerRelocationNotifier relocate(*player);
TypeContainerVisitor<PlayerRelocationNotifier, WorldTypeMapContainer > c2world_relocation(relocate);
TypeContainerVisitor<PlayerRelocationNotifier, GridTypeMapContainer > c2grid_relocation(relocate);
cell2.Visit(pair2, c2world_relocation, i_map, *viewPoint, i_radius);
cell2.Visit(pair2, c2grid_relocation, i_map, *viewPoint, i_radius);
relocate.SendToSelf();
}
}
void AIRelocationNotifier::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
Creature *c = iter->getSource();
CreatureUnitRelocationWorker(c, &i_unit);
if (isCreature)
CreatureUnitRelocationWorker((Creature*)&i_unit, c);
}
}
void
MessageDistDeliverer::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
Player *target = iter->getSource();
if (!target->InSamePhase(i_phaseMask))
continue;
if (target->GetExactDistSq(i_source) > i_distSq)
continue;
// Send packet to all who are sharing the player's vision
if (!target->GetSharedVisionList().empty())
{
SharedVisionList::const_iterator i = target->GetSharedVisionList().begin();
for (; i != target->GetSharedVisionList().end(); ++i)
if ((*i)->m_seer == target)
SendPacket(*i);
}
if (target->m_seer == target || target->GetVehicle())
SendPacket(target);
}
}
void
MessageDistDeliverer::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
if (!iter->getSource()->InSamePhase(i_phaseMask))
continue;
if (iter->getSource()->GetExactDistSq(i_source) > i_distSq)
continue;
// Send packet to all who are sharing the creature's vision
if (!iter->getSource()->GetSharedVisionList().empty())
{
SharedVisionList::const_iterator i = iter->getSource()->GetSharedVisionList().begin();
for (; i != iter->getSource()->GetSharedVisionList().end(); ++i)
if ((*i)->m_seer == iter->getSource())
SendPacket(*i);
}
}
}
void
MessageDistDeliverer::Visit(DynamicObjectMapType &m)
{
for (DynamicObjectMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
if (!iter->getSource()->InSamePhase(i_phaseMask))
continue;
if (iter->getSource()->GetExactDistSq(i_source) > i_distSq)
continue;
if (IS_PLAYER_GUID(iter->getSource()->GetCasterGUID()))
{
// Send packet back to the caster if the caster has vision of dynamic object
Player* caster = (Player*)iter->getSource()->GetCaster();
if (caster && caster->m_seer == iter->getSource())
SendPacket(caster);
}
}
}
/*
void
MessageDistDeliverer::VisitObject(Player* plr)
{
if (!i_ownTeamOnly || (i_source.GetTypeId() == TYPEID_PLAYER && plr->GetTeam() == ((Player&)i_source).GetTeam()))
{
SendPacket(plr);
}
}
*/
template<class T> void
ObjectUpdater::Visit(GridRefManager<T> &m)
{
for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
{
if (iter->getSource()->IsInWorld())
iter->getSource()->Update(i_timeDiff);
}
}
bool CannibalizeObjectCheck::operator()(Corpse* u)
{
// ignore bones
if (u->GetType() == CORPSE_BONES)
return false;
Player* owner = ObjectAccessor::FindPlayer(u->GetOwnerGUID());
if (!owner || i_funit->IsFriendlyTo(owner))
return false;
if (i_funit->IsWithinDistInMap(u, i_range))
return true;
return false;
}
template void ObjectUpdater::Visit<GameObject>(GameObjectMapType &);
template void ObjectUpdater::Visit<DynamicObject>(DynamicObjectMapType &);
File diff suppressed because it is too large Load Diff
@@ -1,453 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_GRIDNOTIFIERSIMPL_H
#define TRINITY_GRIDNOTIFIERSIMPL_H
#include "GridNotifiers.h"
#include "WorldPacket.h"
#include "Corpse.h"
#include "Player.h"
#include "UpdateData.h"
#include "CreatureAI.h"
#include "SpellAuras.h"
template<class T>
inline void
Trinity::VisibleNotifier::Visit(GridRefManager<T> &m)
{
for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
{
vis_guids.erase(iter->getSource()->GetGUID());
i_player.UpdateVisibilityOf(iter->getSource(),i_data,i_visibleNow);
}
}
inline void
Trinity::ObjectUpdater::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
if (iter->getSource()->IsInWorld() && !iter->getSource()->isSpiritService())
iter->getSource()->Update(i_timeDiff);
}
// SEARCHERS & LIST SEARCHERS & WORKERS
// WorldObject searchers & workers
template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(GameObjectMapType &m)
{
// already found
if (i_object)
return;
for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(PlayerMapType &m)
{
// already found
if (i_object)
return;
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(CreatureMapType &m)
{
// already found
if (i_object)
return;
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(CorpseMapType &m)
{
// already found
if (i_object)
return;
for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::WorldObjectSearcher<Check>::Visit(DynamicObjectMapType &m)
{
// already found
if (i_object)
return;
for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(CorpseMapType &m)
{
for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(GameObjectMapType &m)
{
for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::WorldObjectListSearcher<Check>::Visit(DynamicObjectMapType &m)
{
for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
// Gameobject searchers
template<class Check>
void Trinity::GameObjectSearcher<Check>::Visit(GameObjectMapType &m)
{
// already found
if (i_object)
return;
for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::GameObjectLastSearcher<Check>::Visit(GameObjectMapType &m)
{
for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
i_object = itr->getSource();
}
}
template<class Check>
void Trinity::GameObjectListSearcher<Check>::Visit(GameObjectMapType &m)
{
for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
// Unit searchers
template<class Check>
void Trinity::UnitSearcher<Check>::Visit(CreatureMapType &m)
{
// already found
if (i_object)
return;
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::UnitSearcher<Check>::Visit(PlayerMapType &m)
{
// already found
if (i_object)
return;
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::UnitLastSearcher<Check>::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
i_object = itr->getSource();
}
}
template<class Check>
void Trinity::UnitLastSearcher<Check>::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
i_object = itr->getSource();
}
}
template<class Check>
void Trinity::UnitListSearcher<Check>::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::UnitListSearcher<Check>::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
// Creature searchers
template<class Check>
void Trinity::CreatureSearcher<Check>::Visit(CreatureMapType &m)
{
// already found
if (i_object)
return;
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Check>
void Trinity::CreatureLastSearcher<Check>::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
i_object = itr->getSource();
}
}
template<class Check>
void Trinity::CreatureListSearcher<Check>::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::PlayerListSearcher<Check>::Visit(PlayerMapType &m)
{
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
if (itr->getSource()->InSamePhase(i_phaseMask))
if (i_check(itr->getSource()))
i_objects.push_back(itr->getSource());
}
template<class Check>
void Trinity::PlayerSearcher<Check>::Visit(PlayerMapType &m)
{
// already found
if (i_object)
return;
for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
{
if (!itr->getSource()->InSamePhase(i_phaseMask))
continue;
if (i_check(itr->getSource()))
{
i_object = itr->getSource();
return;
}
}
}
template<class Builder>
void Trinity::LocalizedPacketDo<Builder>::operator()(Player* p)
{
int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
uint32 cache_idx = loc_idx+1;
WorldPacket* data;
// create if not cached yet
if (i_data_cache.size() < cache_idx+1 || !i_data_cache[cache_idx])
{
if (i_data_cache.size() < cache_idx+1)
i_data_cache.resize(cache_idx+1);
data = new WorldPacket(SMSG_MESSAGECHAT, 200);
i_builder(*data,loc_idx);
i_data_cache[cache_idx] = data;
}
else
data = i_data_cache[cache_idx];
p->SendDirectMessage(data);
}
template<class Builder>
void Trinity::LocalizedPacketListDo<Builder>::operator()(Player* p)
{
int32 loc_idx = p->GetSession()->GetSessionDbLocaleIndex();
uint32 cache_idx = loc_idx+1;
WorldPacketList* data_list;
// create if not cached yet
if (i_data_cache.size() < cache_idx+1 || i_data_cache[cache_idx].empty())
{
if (i_data_cache.size() < cache_idx+1)
i_data_cache.resize(cache_idx+1);
data_list = &i_data_cache[cache_idx];
i_builder(*data_list,loc_idx);
}
else
data_list = &i_data_cache[cache_idx];
for (size_t i = 0; i < data_list->size(); ++i)
p->SendDirectMessage((*data_list)[i]);
}
#endif // TRINITY_GRIDNOTIFIERSIMPL_H
-76
View File
@@ -1,76 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "GridStates.h"
#include "GridNotifiers.h"
#include "GameSystem/Grid.h"
#include "Log.h"
void
InvalidState::Update(Map &, NGridType &, GridInfo &, const uint32 &/*x*/, const uint32 &/*y*/, const uint32 &) const
{
}
void
ActiveState::Update(Map &m, NGridType &grid, GridInfo & info, const uint32 &x, const uint32 &y, const uint32 &t_diff) const
{
// Only check grid activity every (grid_expiry/10) ms, because it's really useless to do it every cycle
info.UpdateTimeTracker(t_diff);
if (info.getTimeTracker().Passed())
{
if (grid.ActiveObjectsInGrid() == 0 && !m.ActiveObjectsNearGrid(x, y))
{
ObjectGridStoper stoper(grid);
stoper.StopN();
grid.SetGridState(GRID_STATE_IDLE);
sLog.outDebug("Grid[%u,%u] on map %u moved to IDLE state", x, y, m.GetId());
}
else
{
m.ResetGridExpiry(grid, 0.1f);
}
}
}
void
IdleState::Update(Map &m, NGridType &grid, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &) const
{
m.ResetGridExpiry(grid);
grid.SetGridState(GRID_STATE_REMOVAL);
sLog.outDebug("Grid[%u,%u] on map %u moved to REMOVAL state", x, y, m.GetId());
}
void
RemovalState::Update(Map &m, NGridType &grid, GridInfo &info, const uint32 &x, const uint32 &y, const uint32 &t_diff) const
{
if (!info.getUnloadLock())
{
info.UpdateTimeTracker(t_diff);
if (info.getTimeTracker().Passed())
{
if (!m.UnloadGrid(x, y, false))
{
sLog.outDebug("Grid[%u,%u] for map %u differed unloading due to players or active objects nearby", x, y, m.GetId());
m.ResetGridExpiry(grid);
}
}
}
}
-76
View File
@@ -1,76 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_GRIDSTATES_H
#define TRINITY_GRIDSTATES_H
#include "Map.h"
#include "Object.h"
class GridState
{
public:
#ifdef TRINITY_DEBUG
#define MAGIC_TESTVAL 0xFBE823BA
GridState() { i_Magic = MAGIC_TESTVAL; }
bool checkMagic()
{
if (i_Magic != MAGIC_TESTVAL)
{
sLog.outError("!!! GridState: Magic value gone !!!");
return false;
}
return true;
}
void setMagic() { i_Magic = MAGIC_TESTVAL; }
unsigned int i_Magic;
#endif
virtual void Update(Map &, NGridType&, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &t_diff) const = 0;
};
class InvalidState : public GridState
{
public:
void Update(Map &, NGridType &, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &t_diff) const;
};
class ActiveState : public GridState
{
public:
void Update(Map &, NGridType &, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &t_diff) const;
};
class IdleState : public GridState
{
public:
void Update(Map &, NGridType &, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &t_diff) const;
};
class RemovalState : public GridState
{
public:
void Update(Map &, NGridType &, GridInfo &, const uint32 &x, const uint32 &y, const uint32 &t_diff) const;
};
#endif
@@ -1,325 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ObjectGridLoader.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Creature.h"
#include "Vehicle.h"
#include "GameObject.h"
#include "DynamicObject.h"
#include "Corpse.h"
#include "World.h"
#include "CellImpl.h"
#include "CreatureAI.h"
class ObjectGridRespawnMover
{
public:
ObjectGridRespawnMover() {}
void Move(GridType &grid);
template<class T> void Visit(GridRefManager<T> &) {}
void Visit(CreatureMapType &m);
};
void
ObjectGridRespawnMover::Move(GridType &grid)
{
TypeContainerVisitor<ObjectGridRespawnMover, GridTypeMapContainer > mover(*this);
grid.Visit(mover);
}
void
ObjectGridRespawnMover::Visit(CreatureMapType &m)
{
// creature in unloading grid can have respawn point in another grid
// if it will be unloaded then it will not respawn in original grid until unload/load original grid
// move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
for (CreatureMapType::iterator iter = m.begin(); iter != m.end();)
{
Creature * c = iter->getSource();
++iter;
assert(!c->isPet() && "ObjectGridRespawnMover don't must be called for pets");
Cell const& cur_cell = c->GetCurrentCell();
float resp_x, resp_y, resp_z;
c->GetRespawnCoord(resp_x, resp_y, resp_z);
CellPair resp_val = Trinity::ComputeCellPair(resp_x, resp_y);
Cell resp_cell(resp_val);
if (cur_cell.DiffGrid(resp_cell))
{
c->GetMap()->CreatureRespawnRelocation(c);
// false result ignored: will be unload with other creatures at grid
}
}
}
// for loading world object at grid loading (Corpses)
class ObjectWorldLoader
{
public:
explicit ObjectWorldLoader(ObjectGridLoader& gloader)
: i_cell(gloader.i_cell), i_grid(gloader.i_grid), i_map(gloader.i_map), i_corpses (0)
{}
void Visit(CorpseMapType &m);
template<class T> void Visit(GridRefManager<T>&) { }
private:
Cell i_cell;
NGridType &i_grid;
Map* i_map;
public:
uint32 i_corpses;
};
template<class T> void addUnitState(T* /*obj*/, CellPair const& /*cell_pair*/)
{
}
template<> void addUnitState(Creature *obj, CellPair const& cell_pair)
{
Cell cell(cell_pair);
obj->SetCurrentCell(cell);
if (obj->isSpiritService())
obj->setDeathState(DEAD);
}
template <class T>
void AddObjectHelper(CellPair &cell, GridRefManager<T> &m, uint32 &count, Map* map, T *obj)
{
obj->GetGridRef().link(&m, obj);
addUnitState(obj,cell);
obj->AddToWorld();
if (obj->isActiveObject())
map->AddToActive(obj);
++count;
}
template <class T>
void LoadHelper(CellGuidSet const& guid_set, CellPair &cell, GridRefManager<T> &m, uint32 &count, Map* map)
{
for (CellGuidSet::const_iterator i_guid = guid_set.begin(); i_guid != guid_set.end(); ++i_guid)
{
T* obj = new T;
uint32 guid = *i_guid;
//sLog.outString("DEBUG: LoadHelper from table: %s for (guid: %u) Loading",table,guid);
if (!obj->LoadFromDB(guid, map))
{
delete obj;
continue;
}
AddObjectHelper(cell, m, count, map, obj);
}
}
void LoadHelper(CellCorpseSet const& cell_corpses, CellPair &cell, CorpseMapType &m, uint32 &count, Map* map)
{
if (cell_corpses.empty())
return;
for (CellCorpseSet::const_iterator itr = cell_corpses.begin(); itr != cell_corpses.end(); ++itr)
{
if (itr->second != map->GetInstanceId())
continue;
uint32 player_guid = itr->first;
Corpse *obj = ObjectAccessor::Instance().GetCorpseForPlayerGUID(player_guid);
if (!obj)
continue;
// TODO: this is a hack
// corpse's map should be reset when the map is unloaded
// but it may still exist when the grid is unloaded but map is not
// in that case map == currMap
obj->SetMap(map);
AddObjectHelper(cell, m, count, map, obj);
}
}
void
ObjectGridLoader::Visit(GameObjectMapType &m)
{
uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
CellPair cell_pair(x,y);
uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
CellObjectGuids const& cell_guids = objmgr.GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cell_id);
LoadHelper(cell_guids.gameobjects, cell_pair, m, i_gameObjects, i_map);
}
void
ObjectGridLoader::Visit(CreatureMapType &m)
{
uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
CellPair cell_pair(x,y);
uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
CellObjectGuids const& cell_guids = objmgr.GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cell_id);
LoadHelper(cell_guids.creatures, cell_pair, m, i_creatures, i_map);
}
void
ObjectWorldLoader::Visit(CorpseMapType &m)
{
uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
CellPair cell_pair(x,y);
uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
// corpses are always added to spawn mode 0 and they are spawned by their instance id
CellObjectGuids const& cell_guids = objmgr.GetCellObjectGuids(i_map->GetId(), 0, cell_id);
LoadHelper(cell_guids.corpses, cell_pair, m, i_corpses, i_map);
}
void
ObjectGridLoader::Load(GridType &grid)
{
{
TypeContainerVisitor<ObjectGridLoader, GridTypeMapContainer > loader(*this);
grid.Visit(loader);
}
{
ObjectWorldLoader wloader(*this);
TypeContainerVisitor<ObjectWorldLoader, WorldTypeMapContainer > loader(wloader);
grid.Visit(loader);
i_corpses = wloader.i_corpses;
}
}
void ObjectGridLoader::LoadN(void)
{
i_gameObjects = 0; i_creatures = 0; i_corpses = 0;
i_cell.data.Part.cell_y = 0;
for (unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
{
i_cell.data.Part.cell_x = x;
for (unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
{
i_cell.data.Part.cell_y = y;
GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> loader;
loader.Load(i_grid(x, y), *this);
}
}
sLog.outDebug("%u GameObjects, %u Creatures, and %u Corpses/Bones loaded for grid %u on map %u", i_gameObjects, i_creatures, i_corpses,i_grid.GetGridId(), i_map->GetId());
}
void ObjectGridUnloader::MoveToRespawnN()
{
for (unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
{
for (unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
{
ObjectGridRespawnMover mover;
mover.Move(i_grid(x, y));
}
}
}
void
ObjectGridUnloader::Unload(GridType &grid)
{
TypeContainerVisitor<ObjectGridUnloader, GridTypeMapContainer > unloader(*this);
grid.Visit(unloader);
}
template<class T>
void
ObjectGridUnloader::Visit(GridRefManager<T> &m)
{
while (!m.isEmpty())
{
T *obj = m.getFirst()->getSource();
// if option set then object already saved at this moment
if (!sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY))
obj->SaveRespawnTime();
///- object will get delinked from the manager when deleted
delete obj;
}
}
void
ObjectGridStoper::Stop(GridType &grid)
{
TypeContainerVisitor<ObjectGridStoper, GridTypeMapContainer > stoper(*this);
grid.Visit(stoper);
}
void
ObjectGridStoper::Visit(CreatureMapType &m)
{
// stop any fights at grid de-activation and remove dynobjects created at cast by creatures
for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
{
iter->getSource()->RemoveAllDynObjects();
if (iter->getSource()->isInCombat())
{
iter->getSource()->CombatStop();
iter->getSource()->DeleteThreatList();
iter->getSource()->AI()->EnterEvadeMode();
}
}
}
void
ObjectGridCleaner::Stop(GridType &grid)
{
TypeContainerVisitor<ObjectGridCleaner, GridTypeMapContainer > stoper(*this);
grid.Visit(stoper);
}
void
ObjectGridCleaner::Visit(CreatureMapType &m)
{
for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
iter->getSource()->CleanupsBeforeDelete();
}
template<class T>
void
ObjectGridCleaner::Visit(GridRefManager<T> &m)
{
for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
iter->getSource()->RemoveFromWorld();
}
template void ObjectGridUnloader::Visit(CreatureMapType &);
template void ObjectGridUnloader::Visit(GameObjectMapType &);
template void ObjectGridUnloader::Visit(DynamicObjectMapType &);
template void ObjectGridUnloader::Visit(CorpseMapType &);
template void ObjectGridCleaner::Visit<GameObject>(GameObjectMapType &);
template void ObjectGridCleaner::Visit<DynamicObject>(DynamicObjectMapType &);
template void ObjectGridCleaner::Visit<Corpse>(CorpseMapType &);
@@ -1,134 +0,0 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef TRINITY_OBJECTGRIDLOADER_H
#define TRINITY_OBJECTGRIDLOADER_H
#include "Utilities/TypeList.h"
#include "Platform/Define.h"
#include "GameSystem/GridLoader.h"
#include "GridDefines.h"
#include "Cell.h"
class ObjectWorldLoader;
class ObjectGridLoader
{
friend class ObjectWorldLoader;
public:
ObjectGridLoader(NGridType &grid, Map* map, const Cell &cell)
: i_cell(cell), i_grid(grid), i_map(map), i_gameObjects(0), i_creatures(0), i_corpses (0)
{}
void Load(GridType &grid);
void Visit(GameObjectMapType &m);
void Visit(CreatureMapType &m);
void Visit(CorpseMapType &) {}
void Visit(DynamicObjectMapType&) { }
void LoadN(void);
private:
Cell i_cell;
NGridType &i_grid;
Map* i_map;
uint32 i_gameObjects;
uint32 i_creatures;
uint32 i_corpses;
};
class ObjectGridUnloader
{
public:
ObjectGridUnloader(NGridType &grid) : i_grid(grid) {}
void MoveToRespawnN();
void UnloadN()
{
for (unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
{
for (unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
{
GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> loader;
loader.Unload(i_grid(x, y), *this);
}
}
}
void Unload(GridType &grid);
template<class T> void Visit(GridRefManager<T> &m);
private:
NGridType &i_grid;
};
class ObjectGridStoper
{
public:
ObjectGridStoper(NGridType &grid) : i_grid(grid) {}
void StopN()
{
for (unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
{
for (unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
{
GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> loader;
loader.Stop(i_grid(x, y), *this);
}
}
}
void Stop(GridType &grid);
void Visit(CreatureMapType &m);
template<class NONACTIVE> void Visit(GridRefManager<NONACTIVE> &) {}
private:
NGridType &i_grid;
};
class ObjectGridCleaner
{
public:
ObjectGridCleaner(NGridType &grid) : i_grid(grid) {}
void CleanN()
{
for (unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
{
for (unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
{
GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> loader;
loader.Stop(i_grid(x, y), *this);
}
}
}
void Stop(GridType &grid);
void Visit(CreatureMapType &m);
template<class T> void Visit(GridRefManager<T> &);
private:
NGridType &i_grid;
};
typedef GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> GridLoaderType;
#endif