TrinityCore
Loading...
Searching...
No Matches
CellImpl.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_CELLIMPL_H
19#define TRINITY_CELLIMPL_H
20
21#include "Cell.h"
22#include "Map.h"
23#include "Object.h"
24
25inline Cell::Cell(CellCoord const& p) : data()
26{
27 data.Part.grid_x = p.x_coord / MAX_NUMBER_OF_CELLS;
28 data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS;
29 data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS;
30 data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS;
31}
32
33inline CellArea Cell::CalculateCellArea(float x, float y, float radius)
34{
35 if (radius <= 0.0f)
36 {
38 return CellArea(center, center);
39 }
40
41 CellCoord centerX = Trinity::ComputeCellCoord(x - radius, y - radius);
42 CellCoord centerY = Trinity::ComputeCellCoord(x + radius, y + radius);
43
44 return CellArea(centerX, centerY);
45}
46
47template<class T, class CONTAINER>
48inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, WorldObject const& obj, float radius) const
49{
50 //we should increase search radius by object's radius, otherwise
51 //we could have problems with huge creatures, which won't attack nearest players etc
52 Visit(standing_cell, visitor, map, obj.GetPositionX(), obj.GetPositionY(), radius + obj.GetCombatReach());
53}
54
55template<class T, class CONTAINER>
56inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, float x_off, float y_off, float radius) const
57{
58 if (!standing_cell.IsCoordValid())
59 return;
60
61 //lets limit the upper value for search radius
62 if (radius > SIZE_OF_GRIDS)
63 radius = SIZE_OF_GRIDS;
64
65 //lets calculate object coord offsets from cell borders.
66 CellArea area = Cell::CalculateCellArea(x_off, y_off, radius);
67
68 //visit all cells, found in CalculateCellArea()
69 //if radius is known to reach cell area more than 4x4 then we should call optimized VisitCircle
70 //currently this technique works with MAX_NUMBER_OF_CELLS 16 and higher, with lower values
71 //there are nothing to optimize because SIZE_OF_GRID_CELL is too big...
72 if ((area.high_bound.x_coord > (area.low_bound.x_coord + 4)) && (area.high_bound.y_coord > (area.low_bound.y_coord + 4)))
73 {
74 VisitCircle(visitor, map, area.low_bound, area.high_bound);
75 return;
76 }
77
78 // loop the cell range
79 for (uint32 x = area.low_bound.x_coord; x <= area.high_bound.x_coord; ++x)
80 {
81 for (uint32 y = area.low_bound.y_coord; y <= area.high_bound.y_coord; ++y)
82 {
83 CellCoord cellCoord(x, y);
84 Cell r_zone(cellCoord);
85 map.Visit(r_zone, visitor);
86 }
87 }
88}
89
90template<class T, class CONTAINER>
91inline void Cell::VisitCircle(TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, CellCoord const& begin_cell, CellCoord const& end_cell) const
92{
93 //here is an algorithm for 'filling' circum-squared octagon
94 uint32 x_shift = (uint32)ceilf((end_cell.x_coord - begin_cell.x_coord) * 0.3f - 0.5f);
95 //lets calculate x_start/x_end coords for central strip...
96 const uint32 x_start = begin_cell.x_coord + x_shift;
97 const uint32 x_end = end_cell.x_coord - x_shift;
98
99 //visit central strip with constant width...
100 for (uint32 x = x_start; x <= x_end; ++x)
101 {
102 for (uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
103 {
104 CellCoord cellCoord(x, y);
105 Cell r_zone(cellCoord);
106 map.Visit(r_zone, visitor);
107 }
108 }
109
110 //if x_shift == 0 then we have too small cell area, which were already
111 //visited at previous step, so just return from procedure...
112 if (x_shift == 0)
113 return;
114
115 uint32 y_start = end_cell.y_coord;
116 uint32 y_end = begin_cell.y_coord;
117 //now we are visiting borders of an octagon...
118 for (uint32 step = 1; step <= (x_start - begin_cell.x_coord); ++step)
119 {
120 //each step reduces strip height by 2 cells...
121 y_end += 1;
122 y_start -= 1;
123 for (uint32 y = y_start; y >= y_end; --y)
124 {
125 //we visit cells symmetrically from both sides, heading from center to sides and from up to bottom
126 //e.g. filling 2 trapezoids after filling central cell strip...
127 CellCoord cellCoord_left(x_start - step, y);
128 Cell r_zone_left(cellCoord_left);
129 map.Visit(r_zone_left, visitor);
130
131 //right trapezoid cell visit
132 CellCoord cellCoord_right(x_end + step, y);
133 Cell r_zone_right(cellCoord_right);
134 map.Visit(r_zone_right, visitor);
135 }
136 }
137}
138
139template<class T>
140inline void Cell::VisitGridObjects(WorldObject const* center_obj, T& visitor, float radius)
141{
142 CellCoord p(Trinity::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
143 Cell cell(p);
144
146 cell.Visit(p, gnotifier, *center_obj->GetMap(), *center_obj, radius);
147}
148
149template<class T>
150inline void Cell::VisitWorldObjects(WorldObject const* center_obj, T& visitor, float radius)
151{
152 CellCoord p(Trinity::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
153 Cell cell(p);
154
156 cell.Visit(p, gnotifier, *center_obj->GetMap(), *center_obj, radius);
157}
158
159template<class T>
160inline void Cell::VisitAllObjects(WorldObject const* center_obj, T& visitor, float radius)
161{
162 CellCoord p(Trinity::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
163 Cell cell(p);
164
166 cell.Visit(p, wnotifier, *center_obj->GetMap(), *center_obj, radius);
168 cell.Visit(p, gnotifier, *center_obj->GetMap(), *center_obj, radius);
169}
170
171template<class T>
172inline void Cell::VisitGridObjects(float x, float y, Map* map, T& visitor, float radius)
173{
175 Cell cell(p);
176
178 cell.Visit(p, gnotifier, *map, x, y, radius);
179}
180
181template<class T>
182inline void Cell::VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius)
183{
185 Cell cell(p);
186
188 cell.Visit(p, gnotifier, *map, x, y, radius);
189}
190
191template<class T>
192inline void Cell::VisitAllObjects(float x, float y, Map* map, T& visitor, float radius)
193{
195 Cell cell(p);
196
198 cell.Visit(p, wnotifier, *map, x, y, radius);
200 cell.Visit(p, gnotifier, *map, x, y, radius);
201}
202
203#endif
uint32_t uint32
Definition Define.h:154
#define MAX_NUMBER_OF_CELLS
Definition GridDefines.h:36
#define SIZE_OF_GRIDS
Definition GridDefines.h:40
Definition Map.h:227
void Visit(Cell const &cell, TypeContainerVisitor< T, CONTAINER > &visitor)
Definition Map.h:958
Map * GetMap() const
Definition Object.h:411
virtual float GetCombatReach() const
Definition Object.h:302
constexpr CellCoord ComputeCellCoord(float x, float y)
CellCoord high_bound
Definition Cell.h:34
CellCoord low_bound
Definition Cell.h:33
Definition Cell.h:38
Cell()
Definition Cell.h:39
static void VisitWorldObjects(WorldObject const *obj, T &visitor, float radius)
Definition CellImpl.h:150
void VisitCircle(TypeContainerVisitor< T, CONTAINER > &, Map &, CellCoord const &, CellCoord const &) const
Definition CellImpl.h:91
static void VisitAllObjects(WorldObject const *obj, T &visitor, float radius)
Definition CellImpl.h:160
void Visit(CellCoord const &, TypeContainerVisitor< T, CONTAINER > &visitor, Map &, WorldObject const &obj, float radius) const
Definition CellImpl.h:48
static CellArea CalculateCellArea(float x, float y, float radius)
Definition CellImpl.h:33
union Cell::@301 data
static void VisitGridObjects(WorldObject const *obj, T &visitor, float radius)
Definition CellImpl.h:140
uint32 x_coord
constexpr bool IsCoordValid() const
uint32 y_coord
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88