2010-10-07 15:54:07 +02:00
|
|
|
/*
|
2011-01-01 15:01:13 +01:00
|
|
|
* Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
|
2010-10-07 15:54:07 +02:00
|
|
|
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-14 11:57:03 -05:00
|
|
|
#ifndef TRINITY_CELLIMPL_H
|
|
|
|
|
#define TRINITY_CELLIMPL_H
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2009-06-04 21:41:07 +02:00
|
|
|
#include <cmath>
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
#include "Cell.h"
|
|
|
|
|
#include "Map.h"
|
2009-09-27 02:24:25 -07:00
|
|
|
#include "Object.h"
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2011-10-18 10:53:34 -04:00
|
|
|
inline Cell::Cell(CellCoord const& p)
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
|
|
|
|
data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS;
|
2011-10-18 11:18:49 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Cell::Cell(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
CellCoord p = Trinity::ComputeCellCoord(x, y);
|
|
|
|
|
data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS;
|
2008-10-02 16:23:55 -05:00
|
|
|
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;
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-01-05 20:34:50 +02:00
|
|
|
inline CellArea Cell::CalculateCellArea(float x, float y, float radius)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2010-04-07 19:14:10 +02:00
|
|
|
if (radius <= 0.0f)
|
2011-10-19 17:40:44 +01:00
|
|
|
{
|
|
|
|
|
CellCoord center = Trinity::ComputeCellCoord(x, y).normalize();
|
|
|
|
|
return CellArea(center, center);
|
|
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2011-10-19 17:40:44 +01:00
|
|
|
CellCoord centerX = Trinity::ComputeCellCoord(x - radius, y - radius).normalize();
|
|
|
|
|
CellCoord centerY = Trinity::ComputeCellCoord(x + radius, y + radius).normalize();
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2011-10-19 17:40:44 +01:00
|
|
|
return CellArea(centerX, centerY);
|
2009-09-27 02:24:25 -07:00
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2010-01-23 22:24:41 +01:00
|
|
|
template<class T, class CONTAINER>
|
2011-10-19 17:40:44 +01:00
|
|
|
inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, float radius, float x_off, float y_off) const
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-18 11:05:32 -04:00
|
|
|
if (!standing_cell.IsCoordValid())
|
2009-09-27 02:24:25 -07:00
|
|
|
return;
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//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?
|
2009-11-21 05:01:03 -08:00
|
|
|
if (radius <= 0.0f)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(*this, visitor);
|
2009-09-27 02:24:25 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2009-10-06 18:43:21 -07:00
|
|
|
//lets limit the upper value for search radius
|
2011-08-08 13:16:03 +02:00
|
|
|
if (radius > SIZE_OF_GRIDS)
|
|
|
|
|
radius = SIZE_OF_GRIDS;
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//lets calculate object coord offsets from cell borders.
|
2010-01-05 20:34:50 +02:00
|
|
|
CellArea area = Cell::CalculateCellArea(x_off, y_off, radius);
|
2009-09-27 02:24:25 -07:00
|
|
|
//if radius fits inside standing cell
|
2009-11-21 05:01:03 -08:00
|
|
|
if (!area)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(*this, visitor);
|
2009-09-27 02:24:25 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//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...
|
2011-10-19 17:40:44 +01:00
|
|
|
if (((area.high_bound.x_coord - area.low_bound.x_coord) > 4) && ((area.high_bound.y_coord - area.low_bound.y_coord) > 4))
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-19 17:40:44 +01:00
|
|
|
VisitCircle(visitor, map, area.low_bound, area.high_bound);
|
2009-09-27 02:24:25 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//ALWAYS visit standing cell first!!! Since we deal with small radiuses
|
|
|
|
|
//it is very essential to call visitor for standing cell firstly...
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(*this, visitor);
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
// loop the cell range
|
2011-10-19 17:40:44 +01:00
|
|
|
for (uint32 x = area.low_bound.x_coord; x <= area.high_bound.x_coord; ++x)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-19 17:40:44 +01:00
|
|
|
for (uint32 y = area.low_bound.y_coord; y <= area.high_bound.y_coord; ++y)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-18 10:53:34 -04:00
|
|
|
CellCoord cellCoord(x, y);
|
2009-09-27 02:24:25 -07:00
|
|
|
//lets skip standing cell since we already visited it
|
2011-10-18 10:53:34 -04:00
|
|
|
if (cellCoord != standing_cell)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-18 10:53:34 -04:00
|
|
|
Cell r_zone(cellCoord);
|
2010-01-23 22:24:41 +01:00
|
|
|
r_zone.data.Part.nocreate = this->data.Part.nocreate;
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(r_zone, visitor);
|
2009-09-27 02:24:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2010-01-23 22:24:41 +01:00
|
|
|
template<class T, class CONTAINER>
|
2011-10-19 17:40:44 +01:00
|
|
|
inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, WorldObject const& obj, float radius) const
|
2010-01-05 20:34:50 +02:00
|
|
|
{
|
|
|
|
|
//we should increase search radius by object's radius, otherwise
|
|
|
|
|
//we could have problems with huge creatures, which won't attack nearest players etc
|
2011-10-19 17:40:44 +01:00
|
|
|
Visit(standing_cell, visitor, map, radius + obj.GetObjectSize(), obj.GetPositionX(), obj.GetPositionY());
|
2010-01-05 20:34:50 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-23 22:24:41 +01:00
|
|
|
template<class T, class CONTAINER>
|
2011-10-19 17:40:44 +01:00
|
|
|
inline void Cell::VisitCircle(TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, CellCoord const& begin_cell, CellCoord const& end_cell) const
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
|
|
|
|
//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;
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//visit central strip with constant width...
|
2009-10-17 16:20:24 -07:00
|
|
|
for (uint32 x = x_start; x <= x_end; ++x)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2009-10-17 16:20:24 -07:00
|
|
|
for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
|
2009-09-27 02:24:25 -07:00
|
|
|
{
|
2011-10-18 10:53:34 -04:00
|
|
|
CellCoord cellCoord(x, y);
|
|
|
|
|
Cell r_zone(cellCoord);
|
2010-01-23 22:24:41 +01:00
|
|
|
r_zone.data.Part.nocreate = this->data.Part.nocreate;
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(r_zone, visitor);
|
2009-09-27 02:24:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//if x_shift == 0 then we have too small cell area, which were already
|
|
|
|
|
//visited at previous step, so just return from procedure...
|
2010-04-07 19:14:10 +02:00
|
|
|
if (x_shift == 0)
|
2009-09-27 02:24:25 -07:00
|
|
|
return;
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
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...
|
2011-10-18 10:53:34 -04:00
|
|
|
CellCoord cellCoord_left(x_start - step, y);
|
|
|
|
|
Cell r_zone_left(cellCoord_left);
|
2010-01-23 22:24:41 +01:00
|
|
|
r_zone_left.data.Part.nocreate = this->data.Part.nocreate;
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(r_zone_left, visitor);
|
2009-10-17 16:20:24 -07:00
|
|
|
|
2009-09-27 02:24:25 -07:00
|
|
|
//right trapezoid cell visit
|
2011-10-18 10:53:34 -04:00
|
|
|
CellCoord cellCoord_right(x_end + step, y);
|
|
|
|
|
Cell r_zone_right(cellCoord_right);
|
2010-01-23 22:24:41 +01:00
|
|
|
r_zone_right.data.Part.nocreate = this->data.Part.nocreate;
|
2011-10-19 17:40:44 +01:00
|
|
|
map.Visit(r_zone_right, visitor);
|
2009-09-27 02:24:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-10-02 16:23:55 -05:00
|
|
|
#endif
|
2009-02-17 20:07:49 -06:00
|
|
|
|