Some cleanup of grid system.

This commit is contained in:
megamage
2011-10-22 09:36:05 -04:00
parent 3b4ff9ee51
commit 80a18b9e56
2 changed files with 24 additions and 42 deletions
+16 -24
View File
@@ -77,7 +77,6 @@ class GRID_OBJECT_TYPES
class NGrid class NGrid
{ {
public: public:
typedef Grid<ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES> GridType; typedef Grid<ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES> GridType;
NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true) NGrid(uint32 id, int32 x, int32 y, time_t expiry, bool unload = true)
: i_gridId(id), i_x(x), i_y(y), i_cellstate(GRID_STATE_INVALID), i_GridObjectDataLoaded(false) : i_gridId(id), i_x(x), i_y(y), i_cellstate(GRID_STATE_INVALID), i_GridObjectDataLoaded(false)
@@ -85,13 +84,13 @@ class NGrid
i_GridInfo = GridInfo(expiry, unload); i_GridInfo = GridInfo(expiry, unload);
} }
const GridType& operator()(unsigned short x, unsigned short y) const GridType& GetGridType(const uint32 x, const uint32 y)
{ {
ASSERT(x < N && y < N); ASSERT(x < N && y < N);
return i_cells[x][y]; return i_cells[x][y];
} }
GridType& operator()(unsigned short x, unsigned short y) GridType const& GetGridType(const uint32 x, const uint32 y) const
{ {
ASSERT(x < N && y < N); ASSERT(x < N && y < N);
return i_cells[x][y]; return i_cells[x][y];
@@ -123,12 +122,22 @@ class NGrid
template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) template<class SPECIFIC_OBJECT> void AddWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{ {
getGridType(x, y).AddWorldObject(obj); GetGridType(x, y).AddWorldObject(obj);
} }
template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj) template<class SPECIFIC_OBJECT> void RemoveWorldObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{ {
getGridType(x, y).RemoveWorldObject(obj); GetGridType(x, y).RemoveWorldObject(obj);
}
template<class SPECIFIC_OBJECT> void AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{
GetGridType(x, y).AddGridObject(obj);
}
template<class SPECIFIC_OBJECT> void RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{
GetGridType(x, y).RemoveGridObject(obj);
} }
// Visit all Grids (cells) in NGrid (grid) // Visit all Grids (cells) in NGrid (grid)
@@ -137,14 +146,14 @@ class NGrid
{ {
for (uint32 x = 0; x < N; ++x) for (uint32 x = 0; x < N; ++x)
for (uint32 y = 0; y < N; ++y) for (uint32 y = 0; y < N; ++y)
getGridType(x, y).Visit(visitor); GetGridType(x, y).Visit(visitor);
} }
// Visit a single Grid (cell) in NGrid (grid) // Visit a single Grid (cell) in NGrid (grid)
template<class T, class TT> template<class T, class TT>
void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor<T, TypeMapContainer<TT> > &visitor) void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor<T, TypeMapContainer<TT> > &visitor)
{ {
getGridType(x, y).Visit(visitor); GetGridType(x, y).Visit(visitor);
} }
unsigned int ActiveObjectsInGrid(void) const unsigned int ActiveObjectsInGrid(void) const
@@ -156,24 +165,7 @@ class NGrid
return count; return count;
} }
template<class SPECIFIC_OBJECT> bool AddGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{
return getGridType(x, y).AddGridObject(obj);
}
template<class SPECIFIC_OBJECT> bool RemoveGridObject(const uint32 x, const uint32 y, SPECIFIC_OBJECT *obj)
{
return getGridType(x, y).RemoveGridObject(obj);
}
private: private:
GridType& getGridType(const uint32 x, const uint32 y)
{
ASSERT(x < N && y < N);
return i_cells[x][y];
}
uint32 i_gridId; uint32 i_gridId;
GridInfo i_GridInfo; GridInfo i_GridInfo;
GridReference<NGrid<N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES> > i_Reference; GridReference<NGrid<N, ACTIVE_OBJECT, WORLD_OBJECT_TYPES, GRID_OBJECT_TYPES> > i_Reference;
+8 -18
View File
@@ -238,9 +238,9 @@ void Map::AddToGrid(T* obj, Cell const& cell)
{ {
NGridType* grid = getNGrid(cell.GridX(), cell.GridY()); NGridType* grid = getNGrid(cell.GridX(), cell.GridY());
if (obj->m_isWorldObject) if (obj->m_isWorldObject)
(*grid)(cell.CellX(), cell.CellY()).template AddWorldObject<T>(obj); grid->GetGridType(cell.CellX(), cell.CellY()).template AddWorldObject<T>(obj);
else else
(*grid)(cell.CellX(), cell.CellY()).template AddGridObject<T>(obj); grid->GetGridType(cell.CellX(), cell.CellY()).template AddGridObject<T>(obj);
} }
template<> template<>
@@ -248,9 +248,9 @@ void Map::AddToGrid(Creature* obj, Cell const& cell)
{ {
NGridType* grid = getNGrid(cell.GridX(), cell.GridY()); NGridType* grid = getNGrid(cell.GridX(), cell.GridY());
if (obj->m_isWorldObject) if (obj->m_isWorldObject)
(*grid)(cell.CellX(), cell.CellY()).AddWorldObject(obj); grid->GetGridType(cell.CellX(), cell.CellY()).AddWorldObject(obj);
else else
(*grid)(cell.CellX(), cell.CellY()).AddGridObject(obj); grid->GetGridType(cell.CellX(), cell.CellY()).AddGridObject(obj);
obj->SetCurrentCell(cell); obj->SetCurrentCell(cell);
} }
@@ -260,9 +260,9 @@ void Map::RemoveFromGrid(T* obj, Cell const& cell)
{ {
NGridType* grid = getNGrid(cell.GridX(), cell.GridY()); NGridType* grid = getNGrid(cell.GridX(), cell.GridY());
if (obj->m_isWorldObject) if (obj->m_isWorldObject)
(*grid)(cell.CellX(), cell.CellY()).template RemoveWorldObject<T>(obj); grid->GetGridType(cell.CellX(), cell.CellY()).template RemoveWorldObject<T>(obj);
else else
(*grid)(cell.CellX(), cell.CellY()).template RemoveGridObject<T>(obj); grid->GetGridType(cell.CellX(), cell.CellY()).template RemoveGridObject<T>(obj);
} }
template<class T> template<class T>
@@ -283,27 +283,17 @@ void Map::SwitchGridContainers(T* obj, bool on)
NGridType *ngrid = getNGrid(cell.GridX(), cell.GridY()); NGridType *ngrid = getNGrid(cell.GridX(), cell.GridY());
ASSERT(ngrid != NULL); ASSERT(ngrid != NULL);
GridType &grid = (*ngrid)(cell.CellX(), cell.CellY()); GridType &grid = ngrid->GetGridType(cell.CellX(), cell.CellY());
if (on) if (on)
{ {
grid.RemoveGridObject<T>(obj); grid.RemoveGridObject<T>(obj);
grid.AddWorldObject<T>(obj); grid.AddWorldObject<T>(obj);
/*if (!grid.RemoveGridObject<T>(obj, obj->GetGUID())
|| !grid.AddWorldObject<T>(obj, obj->GetGUID()))
{
ASSERT(false);
}*/
} }
else else
{ {
grid.RemoveWorldObject<T>(obj); grid.RemoveWorldObject<T>(obj);
grid.AddGridObject<T>(obj); grid.AddGridObject<T>(obj);
/*if (!grid.RemoveWorldObject<T>(obj, obj->GetGUID())
|| !grid.AddGridObject<T>(obj, obj->GetGUID()))
{
ASSERT(false);
}*/
} }
obj->m_isWorldObject = on; obj->m_isWorldObject = on;
} }
@@ -387,7 +377,7 @@ bool Map::EnsureGridLoaded(const Cell &cell)
loader.LoadN(); loader.LoadN();
// Add resurrectable corpses to world object list in grid // Add resurrectable corpses to world object list in grid
sObjectAccessor->AddCorpsesToGrid(GridCoord(cell.GridX(), cell.GridY()), (*grid)(cell.CellX(), cell.CellY()), this); sObjectAccessor->AddCorpsesToGrid(GridCoord(cell.GridX(), cell.GridY()), grid->GetGridType(cell.CellX(), cell.CellY()), this);
return true; return true;
} }