From 1170c67a70dbda204cafb72ed80402f18af3b9a6 Mon Sep 17 00:00:00 2001 From: XTZGZoReX Date: Sun, 6 Jun 2010 23:32:59 +0200 Subject: * Move some grid stuff. --HG-- branch : trunk --- src/server/game/Grids/Cell.h | 177 ++++ src/server/game/Grids/CellImpl.h | 297 ++++++ src/server/game/Grids/GridDefines.h | 193 ++++ src/server/game/Grids/GridNotifiers.cpp | 353 +++++++ src/server/game/Grids/GridNotifiers.h | 1232 ++++++++++++++++++++++++ src/server/game/Grids/GridNotifiersImpl.h | 453 +++++++++ src/server/game/Grids/GridStates.cpp | 76 ++ src/server/game/Grids/GridStates.h | 76 ++ src/server/game/Grids/ObjectGridLoader.cpp | 325 +++++++ src/server/game/Grids/ObjectGridLoader.h | 134 +++ src/server/game/Maps/Cell/Cell.h | 177 ---- src/server/game/Maps/Cell/CellImpl.h | 297 ------ src/server/game/Maps/Grid/GridDefines.h | 193 ---- src/server/game/Maps/Grid/GridNotifiers.cpp | 353 ------- src/server/game/Maps/Grid/GridNotifiers.h | 1232 ------------------------ src/server/game/Maps/Grid/GridNotifiersImpl.h | 453 --------- src/server/game/Maps/Grid/GridStates.cpp | 76 -- src/server/game/Maps/Grid/GridStates.h | 76 -- src/server/game/Maps/Grid/ObjectGridLoader.cpp | 325 ------- src/server/game/Maps/Grid/ObjectGridLoader.h | 134 --- 20 files changed, 3316 insertions(+), 3316 deletions(-) create mode 100644 src/server/game/Grids/Cell.h create mode 100644 src/server/game/Grids/CellImpl.h create mode 100644 src/server/game/Grids/GridDefines.h create mode 100644 src/server/game/Grids/GridNotifiers.cpp create mode 100644 src/server/game/Grids/GridNotifiers.h create mode 100644 src/server/game/Grids/GridNotifiersImpl.h create mode 100644 src/server/game/Grids/GridStates.cpp create mode 100644 src/server/game/Grids/GridStates.h create mode 100644 src/server/game/Grids/ObjectGridLoader.cpp create mode 100644 src/server/game/Grids/ObjectGridLoader.h delete mode 100644 src/server/game/Maps/Cell/Cell.h delete mode 100644 src/server/game/Maps/Cell/CellImpl.h delete mode 100644 src/server/game/Maps/Grid/GridDefines.h delete mode 100644 src/server/game/Maps/Grid/GridNotifiers.cpp delete mode 100644 src/server/game/Maps/Grid/GridNotifiers.h delete mode 100644 src/server/game/Maps/Grid/GridNotifiersImpl.h delete mode 100644 src/server/game/Maps/Grid/GridStates.cpp delete mode 100644 src/server/game/Maps/Grid/GridStates.h delete mode 100644 src/server/game/Maps/Grid/ObjectGridLoader.cpp delete mode 100644 src/server/game/Maps/Grid/ObjectGridLoader.h (limited to 'src') diff --git a/src/server/game/Grids/Cell.h b/src/server/game/Grids/Cell.h new file mode 100644 index 00000000000..49e0329ace6 --- /dev/null +++ b/src/server/game/Grids/Cell.h @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 + +#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 void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &) const; + template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, const WorldObject&, float) const; + template void Visit(const CellPair&, TypeContainerVisitor &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 void VisitCircle(TypeContainerVisitor &, Map &, const CellPair&, const CellPair&) const; +}; + +#endif + diff --git a/src/server/game/Grids/CellImpl.h b/src/server/game/Grids/CellImpl.h new file mode 100644 index 00000000000..d906e81a5c9 --- /dev/null +++ b/src/server/game/Grids/CellImpl.h @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 + +#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 +inline void +Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &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 +inline void +Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &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 +inline void +Cell::Visit(const CellPair& l, TypeContainerVisitor &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 +inline void +Cell::VisitCircle(TypeContainerVisitor &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 + diff --git a/src/server/game/Grids/GridDefines.h b/src/server/game/Grids/GridDefines.h new file mode 100644 index 00000000000..5269d0a094d --- /dev/null +++ b/src/server/game/Grids/GridDefines.h @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 + +// 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 CorpseMapType; +typedef GridRefManager CreatureMapType; +typedef GridRefManager DynamicObjectMapType; +typedef GridRefManager GameObjectMapType; +typedef GridRefManager PlayerMapType; + +typedef Grid GridType; +typedef NGrid NGridType; + +typedef TypeMapContainer GridTypeMapContainer; +typedef TypeMapContainer WorldTypeMapContainer; + +template +struct CoordPair +{ + CoordPair(uint32 x=0, uint32 y=0) : x_coord(x), y_coord(y) {} + CoordPair(const CoordPair &obj) : x_coord(obj.x_coord), y_coord(obj.y_coord) {} + bool operator == (const CoordPair &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); } + bool operator != (const CoordPair &obj) const { return !operator == (obj); } + CoordPair& operator=(const CoordPair &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 GridPair; +typedef CoordPair CellPair; + +namespace Trinity +{ + template + 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(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS); + } + + inline CellPair ComputeCellPair(float x, float y) + { + return Compute(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 diff --git a/src/server/game/Grids/GridNotifiers.cpp b/src/server/game/Grids/GridNotifiers.cpp new file mode 100644 index 00000000000..b10dfa8791e --- /dev/null +++ b/src/server/game/Grids/GridNotifiers.cpp @@ -0,0 +1,353 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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::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 c2world_relocation(relocate); + TypeContainerVisitor 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 c2world_relocation(relocate); + TypeContainerVisitor 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 void +ObjectUpdater::Visit(GridRefManager &m) +{ + for (typename GridRefManager::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(GameObjectMapType &); +template void ObjectUpdater::Visit(DynamicObjectMapType &); diff --git a/src/server/game/Grids/GridNotifiers.h b/src/server/game/Grids/GridNotifiers.h new file mode 100644 index 00000000000..b0abf0aae79 --- /dev/null +++ b/src/server/game/Grids/GridNotifiers.h @@ -0,0 +1,1232 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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_GRIDNOTIFIERS_H +#define TRINITY_GRIDNOTIFIERS_H + +#include "ObjectGridLoader.h" +#include "UpdateData.h" +#include + +#include "Corpse.h" +#include "Object.h" +#include "DynamicObject.h" +#include "GameObject.h" +#include "Player.h" +#include "Unit.h" +#include "CreatureAI.h" + +class Player; +//class Map; + +namespace Trinity +{ + struct VisibleNotifier + { + Player &i_player; + UpdateData i_data; + std::set i_visibleNow; + Player::ClientGUIDs vis_guids; + + VisibleNotifier(Player &player) : i_player(player), vis_guids(player.m_clientGUIDs) {} + template void Visit(GridRefManager &m); + void SendToSelf(void); + }; + + struct VisibleChangesNotifier + { + WorldObject &i_object; + + explicit VisibleChangesNotifier(WorldObject &object) : i_object(object) {} + template void Visit(GridRefManager &) {} + void Visit(PlayerMapType &); + void Visit(CreatureMapType &); + void Visit(DynamicObjectMapType &); + }; + + struct PlayerRelocationNotifier : public VisibleNotifier + { + PlayerRelocationNotifier(Player &pl) : VisibleNotifier(pl) {} + + template void Visit(GridRefManager &m) { VisibleNotifier::Visit(m); } + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct CreatureRelocationNotifier + { + Creature &i_creature; + CreatureRelocationNotifier(Creature &c) : i_creature(c) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct DelayedUnitRelocation + { + Map &i_map; + Cell &cell; + CellPair &p; + const float i_radius; + DelayedUnitRelocation(Cell &c, CellPair &pair, Map &map, float radius) : + cell(c), p(pair), i_map(map), i_radius(radius) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + void Visit(PlayerMapType &); + }; + + struct AIRelocationNotifier + { + Unit &i_unit; + bool isCreature; + explicit AIRelocationNotifier(Unit &unit) : i_unit(unit), isCreature(unit.GetTypeId() == TYPEID_UNIT) {} + template void Visit(GridRefManager &) {} + void Visit(CreatureMapType &); + }; + + struct GridUpdater + { + GridType &i_grid; + uint32 i_timeDiff; + GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) {} + + template void updateObjects(GridRefManager &m) + { + for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) + iter->getSource()->Update(i_timeDiff); + } + + void Visit(PlayerMapType &m) { updateObjects(m); } + void Visit(CreatureMapType &m){ updateObjects(m); } + void Visit(GameObjectMapType &m) { updateObjects(m); } + void Visit(DynamicObjectMapType &m) { updateObjects(m); } + void Visit(CorpseMapType &m) { updateObjects(m); } + }; + + struct MessageDistDeliverer + { + WorldObject *i_source; + WorldPacket *i_message; + uint32 i_phaseMask; + float i_distSq; + uint32 team; + Player const* skipped_receiver; + MessageDistDeliverer(WorldObject *src, WorldPacket *msg, float dist, bool own_team_only = false, Player const* skipped = NULL) + : i_source(src), i_message(msg), i_distSq(dist * dist), i_phaseMask(src->GetPhaseMask()) + , team((own_team_only && src->GetTypeId() == TYPEID_PLAYER) ? ((Player*)src)->GetTeam() : 0) + , skipped_receiver(skipped) + { + } + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(DynamicObjectMapType &m); + template void Visit(GridRefManager &) {} + + void SendPacket(Player* plr) + { + // never send packet to self + if (plr == i_source || (team && plr->GetTeam() != team) || skipped_receiver == plr) + return; + + plr->GetSession()->SendPacket(i_message); + } + }; + + struct ObjectUpdater + { + uint32 i_timeDiff; + explicit ObjectUpdater(const uint32 &diff) : i_timeDiff(diff) {} + template void Visit(GridRefManager &m); + void Visit(PlayerMapType &) {} + void Visit(CorpseMapType &) {} + void Visit(CreatureMapType &); + }; + + // SEARCHERS & LIST SEARCHERS & WORKERS + + // WorldObject searchers & workers + + template + struct WorldObjectSearcher + { + uint32 i_phaseMask; + WorldObject* &i_object; + Check &i_check; + + WorldObjectSearcher(WorldObject const* searcher, WorldObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(GameObjectMapType &m); + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(CorpseMapType &m); + void Visit(DynamicObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct WorldObjectListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + WorldObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + void Visit(CorpseMapType &m); + void Visit(GameObjectMapType &m); + void Visit(DynamicObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct WorldObjectWorker + { + uint32 i_phaseMask; + Do const& i_do; + + WorldObjectWorker(WorldObject const* searcher, Do const& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(GameObjectMapType &m) + { + for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + void Visit(CreatureMapType &m) + { + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(CorpseMapType &m) + { + for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + void Visit(DynamicObjectMapType &m) + { + for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // Gameobject searchers + + template + struct GameObjectSearcher + { + uint32 i_phaseMask; + GameObject* &i_object; + Check &i_check; + + GameObjectSearcher(WorldObject const* searcher, GameObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check GO if any (Check can change requirements at each call) + template + struct GameObjectLastSearcher + { + uint32 i_phaseMask; + GameObject* &i_object; + Check& i_check; + + GameObjectLastSearcher(WorldObject const* searcher, GameObject* & result, Check& check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result), i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct GameObjectListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + GameObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects), i_check(check) {} + + void Visit(GameObjectMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Unit searchers + + // First accepted by Check Unit if any + template + struct UnitSearcher + { + uint32 i_phaseMask; + Unit* &i_object; + Check & i_check; + + UnitSearcher(WorldObject const* searcher, Unit* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check Unit if any (Check can change requirements at each call) + template + struct UnitLastSearcher + { + uint32 i_phaseMask; + Unit* &i_object; + Check & i_check; + + UnitLastSearcher(WorldObject const* searcher, Unit* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // All accepted by Check units if any + template + struct UnitListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + UnitListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Creature searchers + + template + struct CreatureSearcher + { + uint32 i_phaseMask; + Creature* &i_object; + Check & i_check; + + CreatureSearcher(WorldObject const* searcher, Creature* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + // Last accepted by Check Creature if any (Check can change requirements at each call) + template + struct CreatureLastSearcher + { + uint32 i_phaseMask; + Creature* &i_object; + Check & i_check; + + CreatureLastSearcher(WorldObject const* searcher, Creature* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct CreatureListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + CreatureListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct CreatureWorker + { + uint32 i_phaseMask; + Do& i_do; + + CreatureWorker(WorldObject const* searcher, Do& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(CreatureMapType &m) + { + for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // Player searchers + + template + struct PlayerSearcher + { + uint32 i_phaseMask; + Player* &i_object; + Check & i_check; + + PlayerSearcher(WorldObject const* searcher, Player* & result, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} + + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerListSearcher + { + uint32 i_phaseMask; + std::list &i_objects; + Check& i_check; + + PlayerListSearcher(WorldObject const* searcher, std::list &objects, Check & check) + : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} + + void Visit(PlayerMapType &m); + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerWorker + { + uint32 i_phaseMask; + Do& i_do; + + PlayerWorker(WorldObject const* searcher, Do& _do) + : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_phaseMask)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + template + struct PlayerDistWorker + { + WorldObject const* i_searcher; + float i_dist; + Do& i_do; + + PlayerDistWorker(WorldObject const* searcher, float _dist, Do& _do) + : i_searcher(searcher), i_dist(_dist), i_do(_do) {} + + void Visit(PlayerMapType &m) + { + for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) + if (itr->getSource()->InSamePhase(i_searcher) && itr->getSource()->IsWithinDist(i_searcher,i_dist)) + i_do(itr->getSource()); + } + + template void Visit(GridRefManager &) {} + }; + + // CHECKS && DO classes + + // WorldObject check classes + class RaiseDeadObjectCheck + { + public: + RaiseDeadObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Creature* u) + { + if (i_funit->GetTypeId() != TYPEID_PLAYER || !((Player*)i_funit)->isHonorOrXPTarget(u) || + u->getDeathState() != CORPSE || u->isDeadByDefault() || u->isInFlight() || + (u->GetCreatureTypeMask() & (1 << (CREATURE_TYPE_HUMANOID-1))) == 0 || + (u->GetDisplayId() != u->GetNativeDisplayId())) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + class ExplodeCorpseObjectCheck + { + public: + ExplodeCorpseObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Player* u) + { + if (u->getDeathState() != CORPSE || u->isInFlight() || + u->HasAuraType(SPELL_AURA_GHOST) || (u->GetDisplayId() != u->GetNativeDisplayId())) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + bool operator()(Creature* u) + { + if (u->getDeathState() != CORPSE || u->isInFlight() || u->isDeadByDefault() || + (u->GetDisplayId() != u->GetNativeDisplayId()) || + (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_MECHANICAL_OR_ELEMENTAL) != 0) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + class CannibalizeObjectCheck + { + public: + CannibalizeObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} + bool operator()(Player* u) + { + if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight()) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + bool operator()(Corpse* u); + bool operator()(Creature* u) + { + if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight() || + (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_HUMANOID_OR_UNDEAD) == 0) + return false; + + return i_funit->IsWithinDistInMap(u, i_range); + } + template bool operator()(NOT_INTERESTED*) { return false; } + private: + Unit* const i_funit; + float i_range; + }; + + // WorldObject do classes + + class RespawnDo + { + public: + RespawnDo() {} + void operator()(Creature* u) const { u->Respawn(); } + void operator()(GameObject* u) const { u->Respawn(); } + void operator()(WorldObject*) const {} + void operator()(Corpse*) const {} + }; + + // GameObject checks + + class GameObjectFocusCheck + { + public: + GameObjectFocusCheck(Unit const* unit,uint32 focusId) : i_unit(unit), i_focusId(focusId) {} + bool operator()(GameObject* go) const + { + if (go->GetGOInfo()->type != GAMEOBJECT_TYPE_SPELL_FOCUS) + return false; + + if (go->GetGOInfo()->spellFocus.focusId != i_focusId) + return false; + + float dist = (go->GetGOInfo()->spellFocus.dist)/2; + + return go->IsWithinDistInMap(i_unit, dist); + } + private: + Unit const* i_unit; + uint32 i_focusId; + }; + + // Find the nearest Fishing hole and return true only if source object is in range of hole + class NearestGameObjectFishingHole + { + public: + NearestGameObjectFishingHole(WorldObject const& obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(GameObject* go) + { + if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_FISHINGHOLE && go->isSpawned() && i_obj.IsWithinDistInMap(go, i_range) && i_obj.IsWithinDistInMap(go, go->GetGOInfo()->fishinghole.radius)) + { + i_range = i_obj.GetDistance(go); + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + float i_range; + + // prevent clone + NearestGameObjectFishingHole(NearestGameObjectFishingHole const&); + }; + + class NearestGameObjectCheck + { + public: + NearestGameObjectCheck(WorldObject const& obj) : i_obj(obj), i_range(999) {} + bool operator()(GameObject* go) + { + if (i_obj.IsWithinDistInMap(go, i_range)) + { + i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + float i_range; + + // prevent clone this object + NearestGameObjectCheck(NearestGameObjectCheck const&); + }; + + // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO) + class NearestGameObjectEntryInObjectRangeCheck + { + public: + NearestGameObjectEntryInObjectRangeCheck(WorldObject const& obj,uint32 entry, float range) : i_obj(obj), i_entry(entry), i_range(range) {} + bool operator()(GameObject* go) + { + if (go->GetEntry() == i_entry && i_obj.IsWithinDistInMap(go, i_range)) + { + i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + uint32 i_entry; + float i_range; + + // prevent clone this object + NearestGameObjectEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&); + }; + + class GameObjectWithDbGUIDCheck + { + public: + GameObjectWithDbGUIDCheck(WorldObject const& obj,uint32 db_guid) : i_obj(obj), i_db_guid(db_guid) {} + bool operator()(GameObject const* go) const + { + return go->GetDBTableGUIDLow() == i_db_guid; + } + private: + WorldObject const& i_obj; + uint32 i_db_guid; + }; + + // Unit checks + + class MostHPMissingInRange + { + public: + MostHPMissingInRange(Unit const* obj, float range, uint32 hp) : i_obj(obj), i_range(range), i_hp(hp) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && u->GetMaxHealth() - u->GetHealth() > i_hp) + { + i_hp = u->GetMaxHealth() - u->GetHealth(); + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + uint32 i_hp; + }; + + class FriendlyCCedInRange + { + public: + FriendlyCCedInRange(Unit const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && + (u->isFeared() || u->isCharmed() || u->isFrozen() || u->hasUnitState(UNIT_STAT_STUNNED) || u->hasUnitState(UNIT_STAT_CONFUSED))) + { + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + }; + + class FriendlyMissingBuffInRange + { + public: + FriendlyMissingBuffInRange(Unit const* obj, float range, uint32 spellid) : i_obj(obj), i_range(range), i_spell(spellid) {} + bool operator()(Unit* u) + { + if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && + !(u->HasAura(i_spell))) + { + return true; + } + return false; + } + private: + Unit const* i_obj; + float i_range; + uint32 i_spell; + }; + + class AnyUnfriendlyUnitInObjectRangeCheck + { + public: + AnyUnfriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u)) + return true; + else + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnfriendlyNoTotemUnitInObjectRangeCheck + { + public: + AnyUnfriendlyNoTotemUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (!u->isAlive()) + return false; + + if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) + return false; + + return i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u); + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnfriendlyVisibleUnitInObjectRangeCheck + { + public: + AnyUnfriendlyVisibleUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) + : i_obj(obj), i_funit(funit), i_range(range) {} + + bool operator()(Unit* u) + { + return u->isAlive() + && i_obj->IsWithinDistInMap(u, i_range) + && !i_funit->IsFriendlyTo(u) + && u->isVisibleForOrDetect(i_funit, false); + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class CreatureWithDbGUIDCheck + { + public: + CreatureWithDbGUIDCheck(WorldObject const* obj, uint32 lowguid) : i_obj(obj), i_lowguid(lowguid) {} + bool operator()(Creature* u) + { + return u->GetDBTableGUIDLow() == i_lowguid; + } + private: + WorldObject const* i_obj; + uint32 i_lowguid; + }; + + class AnyFriendlyUnitInObjectRangeCheck + { + public: + AnyFriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && i_funit->IsFriendlyTo(u)) + return true; + else + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + class AnyUnitInObjectRangeCheck + { + public: + AnyUnitInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + WorldObject const* i_obj; + float i_range; + }; + + // Success at unit in range, range update for next check (this can be use with UnitLastSearcher to find nearest unit) + class NearestAttackableUnitInObjectRangeCheck + { + public: + NearestAttackableUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} + bool operator()(Unit* u) + { + if (u->isTargetableForAttack() && i_obj->IsWithinDistInMap(u, i_range) && + !i_funit->IsFriendlyTo(u) && u->isVisibleForOrDetect(i_funit,false)) + { + i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + + return false; + } + private: + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + + // prevent clone this object + NearestAttackableUnitInObjectRangeCheck(NearestAttackableUnitInObjectRangeCheck const&); + }; + + class AnyAoETargetUnitInObjectRangeCheck + { + public: + AnyAoETargetUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) + : i_obj(obj), i_funit(funit), i_range(range) + { + Unit const* check = i_funit; + Unit const* owner = i_funit->GetOwner(); + if (owner) + check = owner; + i_targetForPlayer = (check->GetTypeId() == TYPEID_PLAYER); + } + bool operator()(Unit* u) + { + // Check contains checks for: live, non-selectable, non-attackable flags, flight check and GM check, ignore totems + if (!u->isTargetableForAttack()) + return false; + if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) + return false; + + if ((i_targetForPlayer ? !i_funit->IsFriendlyTo(u) : i_funit->IsHostileTo(u))&& i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + bool i_targetForPlayer; + WorldObject const* i_obj; + Unit const* i_funit; + float i_range; + }; + + // do attack at call of help to friendly crearture + class CallOfHelpCreatureInRangeDo + { + public: + CallOfHelpCreatureInRangeDo(Unit* funit, Unit* enemy, float range) + : i_funit(funit), i_enemy(enemy), i_range(range) + {} + void operator()(Creature* u) + { + if (u == i_funit) + return; + + if (!u->CanAssistTo(i_funit, i_enemy, false)) + return; + + // too far + if (!u->IsWithinDistInMap(i_enemy, i_range)) + return; + + // only if see assisted creature's enemy + if (!u->IsWithinLOSInMap(i_enemy)) + return; + + if (u->AI()) + u->AI()->AttackStart(i_enemy); + } + private: + Unit* const i_funit; + Unit* const i_enemy; + float i_range; + }; + + struct AnyDeadUnitCheck + { + bool operator()(Unit* u) { return !u->isAlive(); } + }; + + struct AnyStealthedCheck + { + bool operator()(Unit* u) { return u->GetVisibility() == VISIBILITY_GROUP_STEALTH; } + }; + + // Creature checks + + class NearestHostileUnitCheck + { + public: + explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0) : me(creature) + { + m_range = (dist == 0 ? 9999 : dist); + } + bool operator()(Unit* u) + { + if (!me->IsWithinDistInMap(u, m_range)) + return false; + + if (!me->canAttack(u)) + return false; + + m_range = me->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + + private: + Creature const *me; + float m_range; + NearestHostileUnitCheck(NearestHostileUnitCheck const&); + }; + + class NearestHostileUnitInAttackDistanceCheck + { + public: + explicit NearestHostileUnitInAttackDistanceCheck(Creature const* creature, float dist = 0) : me(creature) + { + m_range = (dist == 0 ? 9999 : dist); + m_force = (dist == 0 ? false : true); + } + bool operator()(Unit* u) + { + if (!me->IsWithinDistInMap(u, m_range)) + return false; + + if (m_force) + { + if (!me->canAttack(u)) + return false; + } + else + { + if (!me->canStartAttack(u, false)) + return false; + } + + m_range = me->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + float GetLastRange() const { return m_range; } + private: + Creature const *me; + float m_range; + bool m_force; + NearestHostileUnitInAttackDistanceCheck(NearestHostileUnitInAttackDistanceCheck const&); + }; + + class AnyAssistCreatureInRangeCheck + { + public: + AnyAssistCreatureInRangeCheck(Unit* funit, Unit* enemy, float range) + : i_funit(funit), i_enemy(enemy), i_range(range) + { + } + bool operator()(Creature* u) + { + if (u == i_funit) + return false; + + if (!u->CanAssistTo(i_funit, i_enemy)) + return false; + + // too far + if (!i_funit->IsWithinDistInMap(u, i_range)) + return false; + + // only if see assisted creature + if (!i_funit->IsWithinLOSInMap(u)) + return false; + + return true; + } + private: + Unit* const i_funit; + Unit* const i_enemy; + float i_range; + }; + + class NearestAssistCreatureInCreatureRangeCheck + { + public: + NearestAssistCreatureInCreatureRangeCheck(Creature* obj, Unit* enemy, float range) + : i_obj(obj), i_enemy(enemy), i_range(range) {} + + bool operator()(Creature* u) + { + if (u == i_obj) + return false; + if (!u->CanAssistTo(i_obj,i_enemy)) + return false; + + if (!i_obj->IsWithinDistInMap(u, i_range)) + return false; + + if (!i_obj->IsWithinLOSInMap(u)) + return false; + + i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check + return true; + } + float GetLastRange() const { return i_range; } + private: + Creature* const i_obj; + Unit* const i_enemy; + float i_range; + + // prevent clone this object + NearestAssistCreatureInCreatureRangeCheck(NearestAssistCreatureInCreatureRangeCheck const&); + }; + + // Success at unit in range, range update for next check (this can be use with CreatureLastSearcher to find nearest creature) + class NearestCreatureEntryWithLiveStateInObjectRangeCheck + { + public: + NearestCreatureEntryWithLiveStateInObjectRangeCheck(WorldObject const& obj, uint32 entry, bool alive, float range) + : i_obj(obj), i_entry(entry), i_alive(alive), i_range(range) {} + + bool operator()(Creature* u) + { + if (u->GetEntry() == i_entry && u->isAlive() == i_alive && i_obj.IsWithinDistInMap(u, i_range)) + { + i_range = i_obj.GetDistance(u); // use found unit range as new range limit for next check + return true; + } + return false; + } + float GetLastRange() const { return i_range; } + private: + WorldObject const& i_obj; + uint32 i_entry; + bool i_alive; + float i_range; + + // prevent clone this object + NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&); + }; + + class AnyPlayerInObjectRangeCheck + { + public: + AnyPlayerInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} + bool operator()(Player* u) + { + if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) + return true; + + return false; + } + private: + WorldObject const* i_obj; + float i_range; + }; + + class AllFriendlyCreaturesInGrid + { + public: + AllFriendlyCreaturesInGrid(Unit const* obj) : pUnit(obj) {} + bool operator() (Unit* u) + { + if (u->isAlive() && u->GetVisibility() == VISIBILITY_ON && u->IsFriendlyTo(pUnit)) + return true; + + return false; + } + private: + Unit const* pUnit; + }; + + class AllGameObjectsWithEntryInRange + { + public: + AllGameObjectsWithEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} + bool operator() (GameObject* pGo) + { + if (pGo->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pGo,m_fRange,false)) + return true; + + return false; + } + private: + const WorldObject* m_pObject; + uint32 m_uiEntry; + float m_fRange; + }; + + class AllCreaturesOfEntryInRange + { + public: + AllCreaturesOfEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} + bool operator() (Unit* pUnit) + { + if (pUnit->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pUnit,m_fRange,false)) + return true; + + return false; + } + + private: + const WorldObject* m_pObject; + uint32 m_uiEntry; + float m_fRange; + }; + + class PlayerAtMinimumRangeAway + { + public: + PlayerAtMinimumRangeAway(Unit const* unit, float fMinRange) : pUnit(unit), fRange(fMinRange) {} + bool operator() (Player* pPlayer) + { + //No threat list check, must be done explicit if expected to be in combat with creature + if (!pPlayer->isGameMaster() && pPlayer->isAlive() && !pUnit->IsWithinDist(pPlayer,fRange,false)) + return true; + + return false; + } + + private: + Unit const* pUnit; + float fRange; + }; + + class GameObjectInRangeCheck + { + public: + GameObjectInRangeCheck(float _x, float _y, float _z, float _range) : x(_x), y(_y), z(_z), range(_range) {} + bool operator() (GameObject* go) + { + return go->IsInRange(x, y, z, range); + } + private: + float x, y, z, range; + }; + + // Player checks and do + + // Prepare using Builder localized packets with caching and send to player + template + class LocalizedPacketDo + { + public: + explicit LocalizedPacketDo(Builder& builder) : i_builder(builder) {} + + ~LocalizedPacketDo() + { + for (size_t i = 0; i < i_data_cache.size(); ++i) + delete i_data_cache[i]; + } + void operator()(Player* p); + + private: + Builder& i_builder; + std::vector i_data_cache; // 0 = default, i => i-1 locale index + }; + + // Prepare using Builder localized packets with caching and send to player + template + class LocalizedPacketListDo + { + public: + typedef std::vector WorldPacketList; + explicit LocalizedPacketListDo(Builder& builder) : i_builder(builder) {} + + ~LocalizedPacketListDo() + { + for (size_t i = 0; i < i_data_cache.size(); ++i) + for (size_t j = 0; j < i_data_cache[i].size(); ++j) + delete i_data_cache[i][j]; + } + void operator()(Player* p); + + private: + Builder& i_builder; + std::vector i_data_cache; + // 0 = default, i => i-1 locale index + }; +} +#endif diff --git a/src/server/game/Grids/GridNotifiersImpl.h b/src/server/game/Grids/GridNotifiersImpl.h new file mode 100644 index 00000000000..26a9c0bd328 --- /dev/null +++ b/src/server/game/Grids/GridNotifiersImpl.h @@ -0,0 +1,453 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 +inline void +Trinity::VisibleNotifier::Visit(GridRefManager &m) +{ + for (typename GridRefManager::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 +void Trinity::WorldObjectSearcher::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 +void Trinity::WorldObjectSearcher::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 +void Trinity::WorldObjectSearcher::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 +void Trinity::WorldObjectSearcher::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 +void Trinity::WorldObjectSearcher::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 +void Trinity::WorldObjectListSearcher::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 +void Trinity::WorldObjectListSearcher::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 +void Trinity::WorldObjectListSearcher::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 +void Trinity::WorldObjectListSearcher::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 +void Trinity::WorldObjectListSearcher::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 +void Trinity::GameObjectSearcher::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 +void Trinity::GameObjectLastSearcher::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 +void Trinity::GameObjectListSearcher::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 +void Trinity::UnitSearcher::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 +void Trinity::UnitSearcher::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 +void Trinity::UnitLastSearcher::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 +void Trinity::UnitLastSearcher::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 +void Trinity::UnitListSearcher::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 +void Trinity::UnitListSearcher::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 +void Trinity::CreatureSearcher::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 +void Trinity::CreatureLastSearcher::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 +void Trinity::CreatureListSearcher::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 +void Trinity::PlayerListSearcher::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 +void Trinity::PlayerSearcher::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 +void Trinity::LocalizedPacketDo::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 +void Trinity::LocalizedPacketListDo::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 diff --git a/src/server/game/Grids/GridStates.cpp b/src/server/game/Grids/GridStates.cpp new file mode 100644 index 00000000000..9d39531cfad --- /dev/null +++ b/src/server/game/Grids/GridStates.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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); + } + } + } +} + diff --git a/src/server/game/Grids/GridStates.h b/src/server/game/Grids/GridStates.h new file mode 100644 index 00000000000..c2a75ec45b7 --- /dev/null +++ b/src/server/game/Grids/GridStates.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 + diff --git a/src/server/game/Grids/ObjectGridLoader.cpp b/src/server/game/Grids/ObjectGridLoader.cpp new file mode 100644 index 00000000000..ab69d9a966b --- /dev/null +++ b/src/server/game/Grids/ObjectGridLoader.cpp @@ -0,0 +1,325 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 void Visit(GridRefManager &) {} + void Visit(CreatureMapType &m); +}; + +void +ObjectGridRespawnMover::Move(GridType &grid) +{ + TypeContainerVisitor 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 void Visit(GridRefManager&) { } + + private: + Cell i_cell; + NGridType &i_grid; + Map* i_map; + public: + uint32 i_corpses; +}; + +template 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 +void AddObjectHelper(CellPair &cell, GridRefManager &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 +void LoadHelper(CellGuidSet const& guid_set, CellPair &cell, GridRefManager &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 loader(*this); + grid.Visit(loader); + } + + { + ObjectWorldLoader wloader(*this); + TypeContainerVisitor 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 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 unloader(*this); + grid.Visit(unloader); +} + +template +void +ObjectGridUnloader::Visit(GridRefManager &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 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 stoper(*this); + grid.Visit(stoper); +} + +void +ObjectGridCleaner::Visit(CreatureMapType &m) +{ + for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) + iter->getSource()->CleanupsBeforeDelete(); +} + +template +void +ObjectGridCleaner::Visit(GridRefManager &m) +{ + for (typename GridRefManager::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(GameObjectMapType &); +template void ObjectGridCleaner::Visit(DynamicObjectMapType &); +template void ObjectGridCleaner::Visit(CorpseMapType &); diff --git a/src/server/game/Grids/ObjectGridLoader.h b/src/server/game/Grids/ObjectGridLoader.h new file mode 100644 index 00000000000..e890bf8d482 --- /dev/null +++ b/src/server/game/Grids/ObjectGridLoader.h @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2005-2009 MaNGOS + * + * Copyright (C) 2008-2010 Trinity + * + * 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 loader; + loader.Unload(i_grid(x, y), *this); + } + } + } + + void Unload(GridType &grid); + template void Visit(GridRefManager &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 loader; + loader.Stop(i_grid(x, y), *this); + } + } + } + + void Stop(GridType &grid); + void Visit(CreatureMapType &m); + + template void Visit(GridRefManager &) {} + 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 loader; + loader.Stop(i_grid(x, y), *this); + } + } + } + + void Stop(GridType &grid); + void Visit(CreatureMapType &m); + template void Visit(GridRefManager &); + private: + NGridType &i_grid; +}; + +typedef GridLoader GridLoaderType; +#endif + diff --git a/src/server/game/Maps/Cell/Cell.h b/src/server/game/Maps/Cell/Cell.h deleted file mode 100644 index 49e0329ace6..00000000000 --- a/src/server/game/Maps/Cell/Cell.h +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 - -#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 void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &) const; - template void Visit(const CellPair&, TypeContainerVisitor &visitor, Map &, const WorldObject&, float) const; - template void Visit(const CellPair&, TypeContainerVisitor &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 void VisitCircle(TypeContainerVisitor &, Map &, const CellPair&, const CellPair&) const; -}; - -#endif - diff --git a/src/server/game/Maps/Cell/CellImpl.h b/src/server/game/Maps/Cell/CellImpl.h deleted file mode 100644 index d906e81a5c9..00000000000 --- a/src/server/game/Maps/Cell/CellImpl.h +++ /dev/null @@ -1,297 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 - -#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 -inline void -Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &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 -inline void -Cell::Visit(const CellPair& standing_cell, TypeContainerVisitor &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 -inline void -Cell::Visit(const CellPair& l, TypeContainerVisitor &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 -inline void -Cell::VisitCircle(TypeContainerVisitor &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 - diff --git a/src/server/game/Maps/Grid/GridDefines.h b/src/server/game/Maps/Grid/GridDefines.h deleted file mode 100644 index 5269d0a094d..00000000000 --- a/src/server/game/Maps/Grid/GridDefines.h +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 - -// 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 CorpseMapType; -typedef GridRefManager CreatureMapType; -typedef GridRefManager DynamicObjectMapType; -typedef GridRefManager GameObjectMapType; -typedef GridRefManager PlayerMapType; - -typedef Grid GridType; -typedef NGrid NGridType; - -typedef TypeMapContainer GridTypeMapContainer; -typedef TypeMapContainer WorldTypeMapContainer; - -template -struct CoordPair -{ - CoordPair(uint32 x=0, uint32 y=0) : x_coord(x), y_coord(y) {} - CoordPair(const CoordPair &obj) : x_coord(obj.x_coord), y_coord(obj.y_coord) {} - bool operator == (const CoordPair &obj) const { return (obj.x_coord == x_coord && obj.y_coord == y_coord); } - bool operator != (const CoordPair &obj) const { return !operator == (obj); } - CoordPair& operator=(const CoordPair &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 GridPair; -typedef CoordPair CellPair; - -namespace Trinity -{ - template - 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(x, y, CENTER_GRID_OFFSET, SIZE_OF_GRIDS); - } - - inline CellPair ComputeCellPair(float x, float y) - { - return Compute(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 diff --git a/src/server/game/Maps/Grid/GridNotifiers.cpp b/src/server/game/Maps/Grid/GridNotifiers.cpp deleted file mode 100644 index b10dfa8791e..00000000000 --- a/src/server/game/Maps/Grid/GridNotifiers.cpp +++ /dev/null @@ -1,353 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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::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 c2world_relocation(relocate); - TypeContainerVisitor 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 c2world_relocation(relocate); - TypeContainerVisitor 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 void -ObjectUpdater::Visit(GridRefManager &m) -{ - for (typename GridRefManager::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(GameObjectMapType &); -template void ObjectUpdater::Visit(DynamicObjectMapType &); diff --git a/src/server/game/Maps/Grid/GridNotifiers.h b/src/server/game/Maps/Grid/GridNotifiers.h deleted file mode 100644 index b0abf0aae79..00000000000 --- a/src/server/game/Maps/Grid/GridNotifiers.h +++ /dev/null @@ -1,1232 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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_GRIDNOTIFIERS_H -#define TRINITY_GRIDNOTIFIERS_H - -#include "ObjectGridLoader.h" -#include "UpdateData.h" -#include - -#include "Corpse.h" -#include "Object.h" -#include "DynamicObject.h" -#include "GameObject.h" -#include "Player.h" -#include "Unit.h" -#include "CreatureAI.h" - -class Player; -//class Map; - -namespace Trinity -{ - struct VisibleNotifier - { - Player &i_player; - UpdateData i_data; - std::set i_visibleNow; - Player::ClientGUIDs vis_guids; - - VisibleNotifier(Player &player) : i_player(player), vis_guids(player.m_clientGUIDs) {} - template void Visit(GridRefManager &m); - void SendToSelf(void); - }; - - struct VisibleChangesNotifier - { - WorldObject &i_object; - - explicit VisibleChangesNotifier(WorldObject &object) : i_object(object) {} - template void Visit(GridRefManager &) {} - void Visit(PlayerMapType &); - void Visit(CreatureMapType &); - void Visit(DynamicObjectMapType &); - }; - - struct PlayerRelocationNotifier : public VisibleNotifier - { - PlayerRelocationNotifier(Player &pl) : VisibleNotifier(pl) {} - - template void Visit(GridRefManager &m) { VisibleNotifier::Visit(m); } - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct CreatureRelocationNotifier - { - Creature &i_creature; - CreatureRelocationNotifier(Creature &c) : i_creature(c) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct DelayedUnitRelocation - { - Map &i_map; - Cell &cell; - CellPair &p; - const float i_radius; - DelayedUnitRelocation(Cell &c, CellPair &pair, Map &map, float radius) : - cell(c), p(pair), i_map(map), i_radius(radius) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - void Visit(PlayerMapType &); - }; - - struct AIRelocationNotifier - { - Unit &i_unit; - bool isCreature; - explicit AIRelocationNotifier(Unit &unit) : i_unit(unit), isCreature(unit.GetTypeId() == TYPEID_UNIT) {} - template void Visit(GridRefManager &) {} - void Visit(CreatureMapType &); - }; - - struct GridUpdater - { - GridType &i_grid; - uint32 i_timeDiff; - GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) {} - - template void updateObjects(GridRefManager &m) - { - for (typename GridRefManager::iterator iter = m.begin(); iter != m.end(); ++iter) - iter->getSource()->Update(i_timeDiff); - } - - void Visit(PlayerMapType &m) { updateObjects(m); } - void Visit(CreatureMapType &m){ updateObjects(m); } - void Visit(GameObjectMapType &m) { updateObjects(m); } - void Visit(DynamicObjectMapType &m) { updateObjects(m); } - void Visit(CorpseMapType &m) { updateObjects(m); } - }; - - struct MessageDistDeliverer - { - WorldObject *i_source; - WorldPacket *i_message; - uint32 i_phaseMask; - float i_distSq; - uint32 team; - Player const* skipped_receiver; - MessageDistDeliverer(WorldObject *src, WorldPacket *msg, float dist, bool own_team_only = false, Player const* skipped = NULL) - : i_source(src), i_message(msg), i_distSq(dist * dist), i_phaseMask(src->GetPhaseMask()) - , team((own_team_only && src->GetTypeId() == TYPEID_PLAYER) ? ((Player*)src)->GetTeam() : 0) - , skipped_receiver(skipped) - { - } - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(DynamicObjectMapType &m); - template void Visit(GridRefManager &) {} - - void SendPacket(Player* plr) - { - // never send packet to self - if (plr == i_source || (team && plr->GetTeam() != team) || skipped_receiver == plr) - return; - - plr->GetSession()->SendPacket(i_message); - } - }; - - struct ObjectUpdater - { - uint32 i_timeDiff; - explicit ObjectUpdater(const uint32 &diff) : i_timeDiff(diff) {} - template void Visit(GridRefManager &m); - void Visit(PlayerMapType &) {} - void Visit(CorpseMapType &) {} - void Visit(CreatureMapType &); - }; - - // SEARCHERS & LIST SEARCHERS & WORKERS - - // WorldObject searchers & workers - - template - struct WorldObjectSearcher - { - uint32 i_phaseMask; - WorldObject* &i_object; - Check &i_check; - - WorldObjectSearcher(WorldObject const* searcher, WorldObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(GameObjectMapType &m); - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(CorpseMapType &m); - void Visit(DynamicObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct WorldObjectListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - WorldObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - void Visit(CorpseMapType &m); - void Visit(GameObjectMapType &m); - void Visit(DynamicObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct WorldObjectWorker - { - uint32 i_phaseMask; - Do const& i_do; - - WorldObjectWorker(WorldObject const* searcher, Do const& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(GameObjectMapType &m) - { - for (GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - void Visit(CreatureMapType &m) - { - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(CorpseMapType &m) - { - for (CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - void Visit(DynamicObjectMapType &m) - { - for (DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // Gameobject searchers - - template - struct GameObjectSearcher - { - uint32 i_phaseMask; - GameObject* &i_object; - Check &i_check; - - GameObjectSearcher(WorldObject const* searcher, GameObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check GO if any (Check can change requirements at each call) - template - struct GameObjectLastSearcher - { - uint32 i_phaseMask; - GameObject* &i_object; - Check& i_check; - - GameObjectLastSearcher(WorldObject const* searcher, GameObject* & result, Check& check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result), i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct GameObjectListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - GameObjectListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects), i_check(check) {} - - void Visit(GameObjectMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Unit searchers - - // First accepted by Check Unit if any - template - struct UnitSearcher - { - uint32 i_phaseMask; - Unit* &i_object; - Check & i_check; - - UnitSearcher(WorldObject const* searcher, Unit* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check Unit if any (Check can change requirements at each call) - template - struct UnitLastSearcher - { - uint32 i_phaseMask; - Unit* &i_object; - Check & i_check; - - UnitLastSearcher(WorldObject const* searcher, Unit* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // All accepted by Check units if any - template - struct UnitListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - UnitListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Creature searchers - - template - struct CreatureSearcher - { - uint32 i_phaseMask; - Creature* &i_object; - Check & i_check; - - CreatureSearcher(WorldObject const* searcher, Creature* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - // Last accepted by Check Creature if any (Check can change requirements at each call) - template - struct CreatureLastSearcher - { - uint32 i_phaseMask; - Creature* &i_object; - Check & i_check; - - CreatureLastSearcher(WorldObject const* searcher, Creature* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct CreatureListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - CreatureListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct CreatureWorker - { - uint32 i_phaseMask; - Do& i_do; - - CreatureWorker(WorldObject const* searcher, Do& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(CreatureMapType &m) - { - for (CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // Player searchers - - template - struct PlayerSearcher - { - uint32 i_phaseMask; - Player* &i_object; - Check & i_check; - - PlayerSearcher(WorldObject const* searcher, Player* & result, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_object(result),i_check(check) {} - - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerListSearcher - { - uint32 i_phaseMask; - std::list &i_objects; - Check& i_check; - - PlayerListSearcher(WorldObject const* searcher, std::list &objects, Check & check) - : i_phaseMask(searcher->GetPhaseMask()), i_objects(objects),i_check(check) {} - - void Visit(PlayerMapType &m); - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerWorker - { - uint32 i_phaseMask; - Do& i_do; - - PlayerWorker(WorldObject const* searcher, Do& _do) - : i_phaseMask(searcher->GetPhaseMask()), i_do(_do) {} - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_phaseMask)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - template - struct PlayerDistWorker - { - WorldObject const* i_searcher; - float i_dist; - Do& i_do; - - PlayerDistWorker(WorldObject const* searcher, float _dist, Do& _do) - : i_searcher(searcher), i_dist(_dist), i_do(_do) {} - - void Visit(PlayerMapType &m) - { - for (PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr) - if (itr->getSource()->InSamePhase(i_searcher) && itr->getSource()->IsWithinDist(i_searcher,i_dist)) - i_do(itr->getSource()); - } - - template void Visit(GridRefManager &) {} - }; - - // CHECKS && DO classes - - // WorldObject check classes - class RaiseDeadObjectCheck - { - public: - RaiseDeadObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Creature* u) - { - if (i_funit->GetTypeId() != TYPEID_PLAYER || !((Player*)i_funit)->isHonorOrXPTarget(u) || - u->getDeathState() != CORPSE || u->isDeadByDefault() || u->isInFlight() || - (u->GetCreatureTypeMask() & (1 << (CREATURE_TYPE_HUMANOID-1))) == 0 || - (u->GetDisplayId() != u->GetNativeDisplayId())) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - class ExplodeCorpseObjectCheck - { - public: - ExplodeCorpseObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Player* u) - { - if (u->getDeathState() != CORPSE || u->isInFlight() || - u->HasAuraType(SPELL_AURA_GHOST) || (u->GetDisplayId() != u->GetNativeDisplayId())) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - bool operator()(Creature* u) - { - if (u->getDeathState() != CORPSE || u->isInFlight() || u->isDeadByDefault() || - (u->GetDisplayId() != u->GetNativeDisplayId()) || - (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_MECHANICAL_OR_ELEMENTAL) != 0) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - class CannibalizeObjectCheck - { - public: - CannibalizeObjectCheck(Unit* funit, float range) : i_funit(funit), i_range(range) {} - bool operator()(Player* u) - { - if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight()) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - bool operator()(Corpse* u); - bool operator()(Creature* u) - { - if (i_funit->IsFriendlyTo(u) || u->isAlive() || u->isInFlight() || - (u->GetCreatureTypeMask() & CREATURE_TYPEMASK_HUMANOID_OR_UNDEAD) == 0) - return false; - - return i_funit->IsWithinDistInMap(u, i_range); - } - template bool operator()(NOT_INTERESTED*) { return false; } - private: - Unit* const i_funit; - float i_range; - }; - - // WorldObject do classes - - class RespawnDo - { - public: - RespawnDo() {} - void operator()(Creature* u) const { u->Respawn(); } - void operator()(GameObject* u) const { u->Respawn(); } - void operator()(WorldObject*) const {} - void operator()(Corpse*) const {} - }; - - // GameObject checks - - class GameObjectFocusCheck - { - public: - GameObjectFocusCheck(Unit const* unit,uint32 focusId) : i_unit(unit), i_focusId(focusId) {} - bool operator()(GameObject* go) const - { - if (go->GetGOInfo()->type != GAMEOBJECT_TYPE_SPELL_FOCUS) - return false; - - if (go->GetGOInfo()->spellFocus.focusId != i_focusId) - return false; - - float dist = (go->GetGOInfo()->spellFocus.dist)/2; - - return go->IsWithinDistInMap(i_unit, dist); - } - private: - Unit const* i_unit; - uint32 i_focusId; - }; - - // Find the nearest Fishing hole and return true only if source object is in range of hole - class NearestGameObjectFishingHole - { - public: - NearestGameObjectFishingHole(WorldObject const& obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(GameObject* go) - { - if (go->GetGOInfo()->type == GAMEOBJECT_TYPE_FISHINGHOLE && go->isSpawned() && i_obj.IsWithinDistInMap(go, i_range) && i_obj.IsWithinDistInMap(go, go->GetGOInfo()->fishinghole.radius)) - { - i_range = i_obj.GetDistance(go); - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - float i_range; - - // prevent clone - NearestGameObjectFishingHole(NearestGameObjectFishingHole const&); - }; - - class NearestGameObjectCheck - { - public: - NearestGameObjectCheck(WorldObject const& obj) : i_obj(obj), i_range(999) {} - bool operator()(GameObject* go) - { - if (i_obj.IsWithinDistInMap(go, i_range)) - { - i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - float i_range; - - // prevent clone this object - NearestGameObjectCheck(NearestGameObjectCheck const&); - }; - - // Success at unit in range, range update for next check (this can be use with GameobjectLastSearcher to find nearest GO) - class NearestGameObjectEntryInObjectRangeCheck - { - public: - NearestGameObjectEntryInObjectRangeCheck(WorldObject const& obj,uint32 entry, float range) : i_obj(obj), i_entry(entry), i_range(range) {} - bool operator()(GameObject* go) - { - if (go->GetEntry() == i_entry && i_obj.IsWithinDistInMap(go, i_range)) - { - i_range = i_obj.GetDistance(go); // use found GO range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - uint32 i_entry; - float i_range; - - // prevent clone this object - NearestGameObjectEntryInObjectRangeCheck(NearestGameObjectEntryInObjectRangeCheck const&); - }; - - class GameObjectWithDbGUIDCheck - { - public: - GameObjectWithDbGUIDCheck(WorldObject const& obj,uint32 db_guid) : i_obj(obj), i_db_guid(db_guid) {} - bool operator()(GameObject const* go) const - { - return go->GetDBTableGUIDLow() == i_db_guid; - } - private: - WorldObject const& i_obj; - uint32 i_db_guid; - }; - - // Unit checks - - class MostHPMissingInRange - { - public: - MostHPMissingInRange(Unit const* obj, float range, uint32 hp) : i_obj(obj), i_range(range), i_hp(hp) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && u->GetMaxHealth() - u->GetHealth() > i_hp) - { - i_hp = u->GetMaxHealth() - u->GetHealth(); - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - uint32 i_hp; - }; - - class FriendlyCCedInRange - { - public: - FriendlyCCedInRange(Unit const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && - (u->isFeared() || u->isCharmed() || u->isFrozen() || u->hasUnitState(UNIT_STAT_STUNNED) || u->hasUnitState(UNIT_STAT_CONFUSED))) - { - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - }; - - class FriendlyMissingBuffInRange - { - public: - FriendlyMissingBuffInRange(Unit const* obj, float range, uint32 spellid) : i_obj(obj), i_range(range), i_spell(spellid) {} - bool operator()(Unit* u) - { - if (u->isAlive() && u->isInCombat() && !i_obj->IsHostileTo(u) && i_obj->IsWithinDistInMap(u, i_range) && - !(u->HasAura(i_spell))) - { - return true; - } - return false; - } - private: - Unit const* i_obj; - float i_range; - uint32 i_spell; - }; - - class AnyUnfriendlyUnitInObjectRangeCheck - { - public: - AnyUnfriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u)) - return true; - else - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnfriendlyNoTotemUnitInObjectRangeCheck - { - public: - AnyUnfriendlyNoTotemUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (!u->isAlive()) - return false; - - if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) - return false; - - return i_obj->IsWithinDistInMap(u, i_range) && !i_funit->IsFriendlyTo(u); - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnfriendlyVisibleUnitInObjectRangeCheck - { - public: - AnyUnfriendlyVisibleUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) - : i_obj(obj), i_funit(funit), i_range(range) {} - - bool operator()(Unit* u) - { - return u->isAlive() - && i_obj->IsWithinDistInMap(u, i_range) - && !i_funit->IsFriendlyTo(u) - && u->isVisibleForOrDetect(i_funit, false); - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class CreatureWithDbGUIDCheck - { - public: - CreatureWithDbGUIDCheck(WorldObject const* obj, uint32 lowguid) : i_obj(obj), i_lowguid(lowguid) {} - bool operator()(Creature* u) - { - return u->GetDBTableGUIDLow() == i_lowguid; - } - private: - WorldObject const* i_obj; - uint32 i_lowguid; - }; - - class AnyFriendlyUnitInObjectRangeCheck - { - public: - AnyFriendlyUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range) && i_funit->IsFriendlyTo(u)) - return true; - else - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - class AnyUnitInObjectRangeCheck - { - public: - AnyUnitInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - WorldObject const* i_obj; - float i_range; - }; - - // Success at unit in range, range update for next check (this can be use with UnitLastSearcher to find nearest unit) - class NearestAttackableUnitInObjectRangeCheck - { - public: - NearestAttackableUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) : i_obj(obj), i_funit(funit), i_range(range) {} - bool operator()(Unit* u) - { - if (u->isTargetableForAttack() && i_obj->IsWithinDistInMap(u, i_range) && - !i_funit->IsFriendlyTo(u) && u->isVisibleForOrDetect(i_funit,false)) - { - i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - - return false; - } - private: - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - - // prevent clone this object - NearestAttackableUnitInObjectRangeCheck(NearestAttackableUnitInObjectRangeCheck const&); - }; - - class AnyAoETargetUnitInObjectRangeCheck - { - public: - AnyAoETargetUnitInObjectRangeCheck(WorldObject const* obj, Unit const* funit, float range) - : i_obj(obj), i_funit(funit), i_range(range) - { - Unit const* check = i_funit; - Unit const* owner = i_funit->GetOwner(); - if (owner) - check = owner; - i_targetForPlayer = (check->GetTypeId() == TYPEID_PLAYER); - } - bool operator()(Unit* u) - { - // Check contains checks for: live, non-selectable, non-attackable flags, flight check and GM check, ignore totems - if (!u->isTargetableForAttack()) - return false; - if (u->GetTypeId() == TYPEID_UNIT && ((Creature*)u)->isTotem()) - return false; - - if ((i_targetForPlayer ? !i_funit->IsFriendlyTo(u) : i_funit->IsHostileTo(u))&& i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - bool i_targetForPlayer; - WorldObject const* i_obj; - Unit const* i_funit; - float i_range; - }; - - // do attack at call of help to friendly crearture - class CallOfHelpCreatureInRangeDo - { - public: - CallOfHelpCreatureInRangeDo(Unit* funit, Unit* enemy, float range) - : i_funit(funit), i_enemy(enemy), i_range(range) - {} - void operator()(Creature* u) - { - if (u == i_funit) - return; - - if (!u->CanAssistTo(i_funit, i_enemy, false)) - return; - - // too far - if (!u->IsWithinDistInMap(i_enemy, i_range)) - return; - - // only if see assisted creature's enemy - if (!u->IsWithinLOSInMap(i_enemy)) - return; - - if (u->AI()) - u->AI()->AttackStart(i_enemy); - } - private: - Unit* const i_funit; - Unit* const i_enemy; - float i_range; - }; - - struct AnyDeadUnitCheck - { - bool operator()(Unit* u) { return !u->isAlive(); } - }; - - struct AnyStealthedCheck - { - bool operator()(Unit* u) { return u->GetVisibility() == VISIBILITY_GROUP_STEALTH; } - }; - - // Creature checks - - class NearestHostileUnitCheck - { - public: - explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0) : me(creature) - { - m_range = (dist == 0 ? 9999 : dist); - } - bool operator()(Unit* u) - { - if (!me->IsWithinDistInMap(u, m_range)) - return false; - - if (!me->canAttack(u)) - return false; - - m_range = me->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - - private: - Creature const *me; - float m_range; - NearestHostileUnitCheck(NearestHostileUnitCheck const&); - }; - - class NearestHostileUnitInAttackDistanceCheck - { - public: - explicit NearestHostileUnitInAttackDistanceCheck(Creature const* creature, float dist = 0) : me(creature) - { - m_range = (dist == 0 ? 9999 : dist); - m_force = (dist == 0 ? false : true); - } - bool operator()(Unit* u) - { - if (!me->IsWithinDistInMap(u, m_range)) - return false; - - if (m_force) - { - if (!me->canAttack(u)) - return false; - } - else - { - if (!me->canStartAttack(u, false)) - return false; - } - - m_range = me->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - float GetLastRange() const { return m_range; } - private: - Creature const *me; - float m_range; - bool m_force; - NearestHostileUnitInAttackDistanceCheck(NearestHostileUnitInAttackDistanceCheck const&); - }; - - class AnyAssistCreatureInRangeCheck - { - public: - AnyAssistCreatureInRangeCheck(Unit* funit, Unit* enemy, float range) - : i_funit(funit), i_enemy(enemy), i_range(range) - { - } - bool operator()(Creature* u) - { - if (u == i_funit) - return false; - - if (!u->CanAssistTo(i_funit, i_enemy)) - return false; - - // too far - if (!i_funit->IsWithinDistInMap(u, i_range)) - return false; - - // only if see assisted creature - if (!i_funit->IsWithinLOSInMap(u)) - return false; - - return true; - } - private: - Unit* const i_funit; - Unit* const i_enemy; - float i_range; - }; - - class NearestAssistCreatureInCreatureRangeCheck - { - public: - NearestAssistCreatureInCreatureRangeCheck(Creature* obj, Unit* enemy, float range) - : i_obj(obj), i_enemy(enemy), i_range(range) {} - - bool operator()(Creature* u) - { - if (u == i_obj) - return false; - if (!u->CanAssistTo(i_obj,i_enemy)) - return false; - - if (!i_obj->IsWithinDistInMap(u, i_range)) - return false; - - if (!i_obj->IsWithinLOSInMap(u)) - return false; - - i_range = i_obj->GetDistance(u); // use found unit range as new range limit for next check - return true; - } - float GetLastRange() const { return i_range; } - private: - Creature* const i_obj; - Unit* const i_enemy; - float i_range; - - // prevent clone this object - NearestAssistCreatureInCreatureRangeCheck(NearestAssistCreatureInCreatureRangeCheck const&); - }; - - // Success at unit in range, range update for next check (this can be use with CreatureLastSearcher to find nearest creature) - class NearestCreatureEntryWithLiveStateInObjectRangeCheck - { - public: - NearestCreatureEntryWithLiveStateInObjectRangeCheck(WorldObject const& obj, uint32 entry, bool alive, float range) - : i_obj(obj), i_entry(entry), i_alive(alive), i_range(range) {} - - bool operator()(Creature* u) - { - if (u->GetEntry() == i_entry && u->isAlive() == i_alive && i_obj.IsWithinDistInMap(u, i_range)) - { - i_range = i_obj.GetDistance(u); // use found unit range as new range limit for next check - return true; - } - return false; - } - float GetLastRange() const { return i_range; } - private: - WorldObject const& i_obj; - uint32 i_entry; - bool i_alive; - float i_range; - - // prevent clone this object - NearestCreatureEntryWithLiveStateInObjectRangeCheck(NearestCreatureEntryWithLiveStateInObjectRangeCheck const&); - }; - - class AnyPlayerInObjectRangeCheck - { - public: - AnyPlayerInObjectRangeCheck(WorldObject const* obj, float range) : i_obj(obj), i_range(range) {} - bool operator()(Player* u) - { - if (u->isAlive() && i_obj->IsWithinDistInMap(u, i_range)) - return true; - - return false; - } - private: - WorldObject const* i_obj; - float i_range; - }; - - class AllFriendlyCreaturesInGrid - { - public: - AllFriendlyCreaturesInGrid(Unit const* obj) : pUnit(obj) {} - bool operator() (Unit* u) - { - if (u->isAlive() && u->GetVisibility() == VISIBILITY_ON && u->IsFriendlyTo(pUnit)) - return true; - - return false; - } - private: - Unit const* pUnit; - }; - - class AllGameObjectsWithEntryInRange - { - public: - AllGameObjectsWithEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} - bool operator() (GameObject* pGo) - { - if (pGo->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pGo,m_fRange,false)) - return true; - - return false; - } - private: - const WorldObject* m_pObject; - uint32 m_uiEntry; - float m_fRange; - }; - - class AllCreaturesOfEntryInRange - { - public: - AllCreaturesOfEntryInRange(const WorldObject* pObject, uint32 uiEntry, float fMaxRange) : m_pObject(pObject), m_uiEntry(uiEntry), m_fRange(fMaxRange) {} - bool operator() (Unit* pUnit) - { - if (pUnit->GetEntry() == m_uiEntry && m_pObject->IsWithinDist(pUnit,m_fRange,false)) - return true; - - return false; - } - - private: - const WorldObject* m_pObject; - uint32 m_uiEntry; - float m_fRange; - }; - - class PlayerAtMinimumRangeAway - { - public: - PlayerAtMinimumRangeAway(Unit const* unit, float fMinRange) : pUnit(unit), fRange(fMinRange) {} - bool operator() (Player* pPlayer) - { - //No threat list check, must be done explicit if expected to be in combat with creature - if (!pPlayer->isGameMaster() && pPlayer->isAlive() && !pUnit->IsWithinDist(pPlayer,fRange,false)) - return true; - - return false; - } - - private: - Unit const* pUnit; - float fRange; - }; - - class GameObjectInRangeCheck - { - public: - GameObjectInRangeCheck(float _x, float _y, float _z, float _range) : x(_x), y(_y), z(_z), range(_range) {} - bool operator() (GameObject* go) - { - return go->IsInRange(x, y, z, range); - } - private: - float x, y, z, range; - }; - - // Player checks and do - - // Prepare using Builder localized packets with caching and send to player - template - class LocalizedPacketDo - { - public: - explicit LocalizedPacketDo(Builder& builder) : i_builder(builder) {} - - ~LocalizedPacketDo() - { - for (size_t i = 0; i < i_data_cache.size(); ++i) - delete i_data_cache[i]; - } - void operator()(Player* p); - - private: - Builder& i_builder; - std::vector i_data_cache; // 0 = default, i => i-1 locale index - }; - - // Prepare using Builder localized packets with caching and send to player - template - class LocalizedPacketListDo - { - public: - typedef std::vector WorldPacketList; - explicit LocalizedPacketListDo(Builder& builder) : i_builder(builder) {} - - ~LocalizedPacketListDo() - { - for (size_t i = 0; i < i_data_cache.size(); ++i) - for (size_t j = 0; j < i_data_cache[i].size(); ++j) - delete i_data_cache[i][j]; - } - void operator()(Player* p); - - private: - Builder& i_builder; - std::vector i_data_cache; - // 0 = default, i => i-1 locale index - }; -} -#endif diff --git a/src/server/game/Maps/Grid/GridNotifiersImpl.h b/src/server/game/Maps/Grid/GridNotifiersImpl.h deleted file mode 100644 index 26a9c0bd328..00000000000 --- a/src/server/game/Maps/Grid/GridNotifiersImpl.h +++ /dev/null @@ -1,453 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 -inline void -Trinity::VisibleNotifier::Visit(GridRefManager &m) -{ - for (typename GridRefManager::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 -void Trinity::WorldObjectSearcher::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 -void Trinity::WorldObjectSearcher::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 -void Trinity::WorldObjectSearcher::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 -void Trinity::WorldObjectSearcher::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 -void Trinity::WorldObjectSearcher::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 -void Trinity::WorldObjectListSearcher::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 -void Trinity::WorldObjectListSearcher::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 -void Trinity::WorldObjectListSearcher::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 -void Trinity::WorldObjectListSearcher::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 -void Trinity::WorldObjectListSearcher::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 -void Trinity::GameObjectSearcher::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 -void Trinity::GameObjectLastSearcher::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 -void Trinity::GameObjectListSearcher::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 -void Trinity::UnitSearcher::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 -void Trinity::UnitSearcher::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 -void Trinity::UnitLastSearcher::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 -void Trinity::UnitLastSearcher::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 -void Trinity::UnitListSearcher::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 -void Trinity::UnitListSearcher::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 -void Trinity::CreatureSearcher::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 -void Trinity::CreatureLastSearcher::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 -void Trinity::CreatureListSearcher::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 -void Trinity::PlayerListSearcher::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 -void Trinity::PlayerSearcher::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 -void Trinity::LocalizedPacketDo::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 -void Trinity::LocalizedPacketListDo::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 diff --git a/src/server/game/Maps/Grid/GridStates.cpp b/src/server/game/Maps/Grid/GridStates.cpp deleted file mode 100644 index 9d39531cfad..00000000000 --- a/src/server/game/Maps/Grid/GridStates.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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); - } - } - } -} - diff --git a/src/server/game/Maps/Grid/GridStates.h b/src/server/game/Maps/Grid/GridStates.h deleted file mode 100644 index c2a75ec45b7..00000000000 --- a/src/server/game/Maps/Grid/GridStates.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 - diff --git a/src/server/game/Maps/Grid/ObjectGridLoader.cpp b/src/server/game/Maps/Grid/ObjectGridLoader.cpp deleted file mode 100644 index ab69d9a966b..00000000000 --- a/src/server/game/Maps/Grid/ObjectGridLoader.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 void Visit(GridRefManager &) {} - void Visit(CreatureMapType &m); -}; - -void -ObjectGridRespawnMover::Move(GridType &grid) -{ - TypeContainerVisitor 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 void Visit(GridRefManager&) { } - - private: - Cell i_cell; - NGridType &i_grid; - Map* i_map; - public: - uint32 i_corpses; -}; - -template 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 -void AddObjectHelper(CellPair &cell, GridRefManager &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 -void LoadHelper(CellGuidSet const& guid_set, CellPair &cell, GridRefManager &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 loader(*this); - grid.Visit(loader); - } - - { - ObjectWorldLoader wloader(*this); - TypeContainerVisitor 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 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 unloader(*this); - grid.Visit(unloader); -} - -template -void -ObjectGridUnloader::Visit(GridRefManager &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 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 stoper(*this); - grid.Visit(stoper); -} - -void -ObjectGridCleaner::Visit(CreatureMapType &m) -{ - for (CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter) - iter->getSource()->CleanupsBeforeDelete(); -} - -template -void -ObjectGridCleaner::Visit(GridRefManager &m) -{ - for (typename GridRefManager::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(GameObjectMapType &); -template void ObjectGridCleaner::Visit(DynamicObjectMapType &); -template void ObjectGridCleaner::Visit(CorpseMapType &); diff --git a/src/server/game/Maps/Grid/ObjectGridLoader.h b/src/server/game/Maps/Grid/ObjectGridLoader.h deleted file mode 100644 index e890bf8d482..00000000000 --- a/src/server/game/Maps/Grid/ObjectGridLoader.h +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2005-2009 MaNGOS - * - * Copyright (C) 2008-2010 Trinity - * - * 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 loader; - loader.Unload(i_grid(x, y), *this); - } - } - } - - void Unload(GridType &grid); - template void Visit(GridRefManager &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 loader; - loader.Stop(i_grid(x, y), *this); - } - } - } - - void Stop(GridType &grid); - void Visit(CreatureMapType &m); - - template void Visit(GridRefManager &) {} - 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 loader; - loader.Stop(i_grid(x, y), *this); - } - } - } - - void Stop(GridType &grid); - void Visit(CreatureMapType &m); - template void Visit(GridRefManager &); - private: - NGridType &i_grid; -}; - -typedef GridLoader GridLoaderType; -#endif - -- cgit v1.2.3