Fixed possible crash
[getmangos.git] / src / game / Cell.h
blobb0ff85d892bba6e7609080b25e702a8c84e382a7
1 /*
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
19 #ifndef MANGOS_CELL_H
20 #define MANGOS_CELL_H
22 #include "GameSystem/TypeContainer.h"
23 #include "GameSystem/TypeContainerVisitor.h"
24 #include "GridDefines.h"
25 #include <cmath>
27 class Map;
29 enum District
31 UPPER_DISTRICT = 1,
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;
56 Compute(x, 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;
63 return;
66 if( x < old_x )
68 data.Part.reserved |= LEFT_DISTRICT;
69 cell.data.Part.reserved |= RIGHT_DISTRICT;
71 else if( old_x < x )
73 data.Part.reserved |= RIGHT_DISTRICT;
74 cell.data.Part.reserved |= LEFT_DISTRICT;
76 if( y < old_y )
78 data.Part.reserved |= UPPER_DISTRICT;
79 cell.data.Part.reserved |= LOWER_DISTRICT;
81 else if( old_y < y )
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
115 return CellPair(
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;
123 return *this;
126 bool operator==(const Cell &cell) const { return (data.All == cell.data.All); }
127 bool operator!=(const Cell &cell) const { return !operator==(cell); }
128 union
130 struct
132 unsigned grid_x : 6;
133 unsigned grid_y : 6;
134 unsigned cell_x : 4;
135 unsigned cell_y : 4;
136 unsigned nocreate : 1;
137 unsigned reserved : 11;
138 } Part;
139 uint32 All;
140 } data;
142 template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &) const;
145 template<class T>
146 struct MANGOS_DLL_DECL CellLock
148 const Cell& i_cell;
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)
157 ~CellLock();
158 new (this) CellLock<T>(cell);
159 return *this;
162 #endif