2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "GameSystem/TypeContainer.h"
23 #include "GameSystem/TypeContainerVisitor.h"
24 #include "GridDefines.h"
33 LOWER_DISTRICT
= 1 << 1,
34 LEFT_DISTRICT
= 1 << 2,
35 RIGHT_DISTRICT
= 1 << 3,
36 CENTER_DISTRICT
= 1 << 4,
37 UPPER_LEFT_DISTRICT
= (UPPER_DISTRICT
| LEFT_DISTRICT
),
38 UPPER_RIGHT_DISTRICT
= (UPPER_DISTRICT
| RIGHT_DISTRICT
),
39 LOWER_LEFT_DISTRICT
= (LOWER_DISTRICT
| LEFT_DISTRICT
),
40 LOWER_RIGHT_DISTRICT
= (LOWER_DISTRICT
| RIGHT_DISTRICT
),
41 ALL_DISTRICT
= (UPPER_DISTRICT
| LOWER_DISTRICT
| LEFT_DISTRICT
| RIGHT_DISTRICT
| CENTER_DISTRICT
)
44 template<class T
> struct CellLock
;
46 struct MANGOS_DLL_DECL CellArea
48 CellArea() : right_offset(0), left_offset(0), upper_offset(0), lower_offset(0) {}
49 CellArea(int right
, int left
, int upper
, int lower
) : right_offset(right
), left_offset(left
), upper_offset(upper
), lower_offset(lower
) {}
50 bool operator!() const { return !right_offset
&& !left_offset
&& !upper_offset
&& !lower_offset
; }
52 void ResizeBorders(CellPair
& begin_cell
, CellPair
& end_cell
) const
54 begin_cell
<< left_offset
;
55 begin_cell
-= lower_offset
;
56 end_cell
>> right_offset
;
57 end_cell
+= upper_offset
;
66 struct MANGOS_DLL_DECL Cell
68 Cell() { data
.All
= 0; }
69 Cell(const Cell
&cell
) { data
.All
= cell
.data
.All
; }
70 explicit Cell(CellPair
const& p
);
72 void operator|=(Cell
&cell
)
74 data
.Part
.reserved
= 0;
75 cell
.data
.Part
.reserved
= 0;
76 uint32 x
, y
, old_x
, old_y
;
78 cell
.Compute(old_x
, old_y
);
80 if( std::abs(int(x
-old_x
)) > 1 || std::abs(int(y
-old_y
)) > 1)
82 data
.Part
.reserved
= ALL_DISTRICT
;
83 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
89 data
.Part
.reserved
|= LEFT_DISTRICT
;
90 cell
.data
.Part
.reserved
|= RIGHT_DISTRICT
;
94 data
.Part
.reserved
|= RIGHT_DISTRICT
;
95 cell
.data
.Part
.reserved
|= LEFT_DISTRICT
;
99 data
.Part
.reserved
|= UPPER_DISTRICT
;
100 cell
.data
.Part
.reserved
|= LOWER_DISTRICT
;
104 data
.Part
.reserved
|= LOWER_DISTRICT
;
105 cell
.data
.Part
.reserved
|= UPPER_DISTRICT
;
109 void Compute(uint32
&x
, uint32
&y
) const
111 x
= data
.Part
.grid_x
*MAX_NUMBER_OF_CELLS
+ data
.Part
.cell_x
;
112 y
= data
.Part
.grid_y
*MAX_NUMBER_OF_CELLS
+ data
.Part
.cell_y
;
115 bool DiffCell(const Cell
&cell
) const
117 return( data
.Part
.cell_x
!= cell
.data
.Part
.cell_x
||
118 data
.Part
.cell_y
!= cell
.data
.Part
.cell_y
);
121 bool DiffGrid(const Cell
&cell
) const
123 return( data
.Part
.grid_x
!= cell
.data
.Part
.grid_x
||
124 data
.Part
.grid_y
!= cell
.data
.Part
.grid_y
);
127 uint32
CellX() const { return data
.Part
.cell_x
; }
128 uint32
CellY() const { return data
.Part
.cell_y
; }
129 uint32
GridX() const { return data
.Part
.grid_x
; }
130 uint32
GridY() const { return data
.Part
.grid_y
; }
131 bool NoCreate() const { return data
.Part
.nocreate
; }
132 void SetNoCreate() { data
.Part
.nocreate
= 1; }
134 CellPair
cellPair() const
137 data
.Part
.grid_x
*MAX_NUMBER_OF_CELLS
+data
.Part
.cell_x
,
138 data
.Part
.grid_y
*MAX_NUMBER_OF_CELLS
+data
.Part
.cell_y
);
141 Cell
& operator=(const Cell
&cell
)
143 data
.All
= cell
.data
.All
;
147 bool operator==(const Cell
&cell
) const { return (data
.All
== cell
.data
.All
); }
148 bool operator!=(const Cell
&cell
) const { return !operator==(cell
); }
157 unsigned nocreate
: 1;
158 unsigned reserved
: 7;
163 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &, TypeContainerVisitor
<T
, CONTAINER
> &visitor
, Map
&) const;
164 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &, TypeContainerVisitor
<T
, CONTAINER
> &visitor
, Map
&m
, const WorldObject
&obj
, float radius
) const;
166 static CellArea
CalculateCellArea(const WorldObject
&obj
, float radius
);
169 template<class LOCK_TYPE
, class T
, class CONTAINER
> void VisitCircle(const CellLock
<LOCK_TYPE
> &, TypeContainerVisitor
<T
, CONTAINER
> &, Map
&, const CellPair
& , const CellPair
& ) const;
173 struct MANGOS_DLL_DECL CellLock
176 const CellPair
&i_cellPair
;
177 CellLock(const Cell
&c
, const CellPair
&p
) : i_cell(c
), i_cellPair(p
) {}
178 CellLock(const CellLock
<T
> &cell
) : i_cell(cell
.i_cell
), i_cellPair(cell
.i_cellPair
) {}
179 const Cell
* operator->(void) const { return &i_cell
; }
180 const Cell
* operator->(void) { return &i_cell
; }
181 operator const Cell
&(void) const { return i_cell
; }
182 CellLock
<T
>& operator=(const CellLock
<T
> &cell
)
185 new (this) CellLock
<T
>(cell
);