2 * Copyright (C) 2005-2008 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"
32 LOWER_DISTRICT
= 1 << 1,
33 LEFT_DISTRICT
= 1 << 2,
34 RIGHT_DISTRICT
= 1 << 3,
35 CENTER_DISTRICT
= 1 << 4,
36 UPPER_LEFT_DISTRICT
= (UPPER_DISTRICT
| LEFT_DISTRICT
),
37 UPPER_RIGHT_DISTRICT
= (UPPER_DISTRICT
| RIGHT_DISTRICT
),
38 LOWER_LEFT_DISTRICT
= (LOWER_DISTRICT
| LEFT_DISTRICT
),
39 LOWER_RIGHT_DISTRICT
= (LOWER_DISTRICT
| RIGHT_DISTRICT
),
40 ALL_DISTRICT
= (UPPER_DISTRICT
| LOWER_DISTRICT
| LEFT_DISTRICT
| RIGHT_DISTRICT
| CENTER_DISTRICT
)
43 template<class T
> struct CellLock
;
45 struct MANGOS_DLL_DECL Cell
47 Cell() { data
.All
= 0; }
48 Cell(const Cell
&cell
) { data
.All
= cell
.data
.All
; }
49 explicit Cell(CellPair
const& p
);
51 void operator|=(Cell
&cell
)
53 data
.Part
.reserved
= 0;
54 cell
.data
.Part
.reserved
= 0;
55 uint32 x
, y
, old_x
, old_y
;
57 cell
.Compute(old_x
, old_y
);
59 if( std::abs(int(x
-old_x
)) > 1 || std::abs(int(y
-old_y
)) > 1)
61 data
.Part
.reserved
= ALL_DISTRICT
;
62 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
68 data
.Part
.reserved
|= LEFT_DISTRICT
;
69 cell
.data
.Part
.reserved
|= RIGHT_DISTRICT
;
73 data
.Part
.reserved
|= RIGHT_DISTRICT
;
74 cell
.data
.Part
.reserved
|= LEFT_DISTRICT
;
78 data
.Part
.reserved
|= UPPER_DISTRICT
;
79 cell
.data
.Part
.reserved
|= LOWER_DISTRICT
;
83 data
.Part
.reserved
|= LOWER_DISTRICT
;
84 cell
.data
.Part
.reserved
|= UPPER_DISTRICT
;
88 void Compute(uint32
&x
, uint32
&y
) const
90 x
= data
.Part
.grid_x
*MAX_NUMBER_OF_CELLS
+ data
.Part
.cell_x
;
91 y
= data
.Part
.grid_y
*MAX_NUMBER_OF_CELLS
+ data
.Part
.cell_y
;
94 inline bool DiffCell(const Cell
&cell
) const
96 return( data
.Part
.cell_x
!= cell
.data
.Part
.cell_x
||
97 data
.Part
.cell_y
!= cell
.data
.Part
.cell_y
);
100 inline bool DiffGrid(const Cell
&cell
) const
102 return( data
.Part
.grid_x
!= cell
.data
.Part
.grid_x
||
103 data
.Part
.grid_y
!= cell
.data
.Part
.grid_y
);
106 uint32
CellX() const { return data
.Part
.cell_x
; }
107 uint32
CellY() const { return data
.Part
.cell_y
; }
108 uint32
GridX() const { return data
.Part
.grid_x
; }
109 uint32
GridY() const { return data
.Part
.grid_y
; }
110 bool NoCreate() const { return data
.Part
.nocreate
; }
111 void SetNoCreate() { data
.Part
.nocreate
= 1; }
113 CellPair
cellPair() const
116 data
.Part
.grid_x
*MAX_NUMBER_OF_CELLS
+data
.Part
.cell_x
,
117 data
.Part
.grid_y
*MAX_NUMBER_OF_CELLS
+data
.Part
.cell_y
);
120 Cell
& operator=(const Cell
&cell
)
122 data
.All
= cell
.data
.All
;
126 bool operator==(const Cell
&cell
) const { return (data
.All
== cell
.data
.All
); }
127 bool operator!=(const Cell
&cell
) const { return !operator==(cell
); }
136 unsigned nocreate
: 1;
137 unsigned reserved
: 11;
142 template<class LOCK_TYPE
, class T
, class CONTAINER
> void Visit(const CellLock
<LOCK_TYPE
> &, TypeContainerVisitor
<T
, CONTAINER
> &visitor
, Map
&) const;
146 struct MANGOS_DLL_DECL CellLock
149 const CellPair
&i_cellPair
;
150 CellLock(const Cell
&c
, const CellPair
&p
) : i_cell(c
), i_cellPair(p
) {}
151 CellLock(const CellLock
<T
> &cell
) : i_cell(cell
.i_cell
), i_cellPair(cell
.i_cellPair
) {}
152 const Cell
* operator->(void) const { return &i_cell
; }
153 const Cell
* operator->(void) { return &i_cell
; }
154 operator const Cell
&(void) const { return i_cell
; }
155 CellLock
<T
>& operator=(const CellLock
<T
> &cell
)
158 new (this) CellLock
<T
>(cell
);