1 | /* |
---|
2 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
3 | * |
---|
4 | * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_CELL_H |
---|
22 | #define TRINITY_CELL_H |
---|
23 | |
---|
24 | #include "GameSystem/TypeContainer.h" |
---|
25 | #include "GameSystem/TypeContainerVisitor.h" |
---|
26 | #include "GridDefines.h" |
---|
27 | #include <cmath> |
---|
28 | |
---|
29 | class Map; |
---|
30 | |
---|
31 | enum District |
---|
32 | { |
---|
33 | UPPER_DISTRICT = 1, |
---|
34 | LOWER_DISTRICT = 1 << 1, |
---|
35 | LEFT_DISTRICT = 1 << 2, |
---|
36 | RIGHT_DISTRICT = 1 << 3, |
---|
37 | CENTER_DISTRICT = 1 << 4, |
---|
38 | UPPER_LEFT_DISTRICT = (UPPER_DISTRICT | LEFT_DISTRICT), |
---|
39 | UPPER_RIGHT_DISTRICT = (UPPER_DISTRICT | RIGHT_DISTRICT), |
---|
40 | LOWER_LEFT_DISTRICT = (LOWER_DISTRICT | LEFT_DISTRICT), |
---|
41 | LOWER_RIGHT_DISTRICT = (LOWER_DISTRICT | RIGHT_DISTRICT), |
---|
42 | ALL_DISTRICT = (UPPER_DISTRICT | LOWER_DISTRICT | LEFT_DISTRICT | RIGHT_DISTRICT | CENTER_DISTRICT) |
---|
43 | }; |
---|
44 | |
---|
45 | template<class T> struct CellLock; |
---|
46 | |
---|
47 | struct TRINITY_DLL_DECL Cell |
---|
48 | { |
---|
49 | Cell() { data.All = 0; } |
---|
50 | Cell(const Cell &cell) { data.All = cell.data.All; } |
---|
51 | explicit Cell(CellPair const& p); |
---|
52 | |
---|
53 | void operator|=(Cell &cell) |
---|
54 | { |
---|
55 | data.Part.reserved = 0; |
---|
56 | cell.data.Part.reserved = 0; |
---|
57 | uint32 x, y, old_x, old_y; |
---|
58 | Compute(x, y); |
---|
59 | cell.Compute(old_x, old_y); |
---|
60 | |
---|
61 | if( std::abs(int(x-old_x)) > 1 || std::abs(int(y-old_y)) > 1) |
---|
62 | { |
---|
63 | data.Part.reserved = ALL_DISTRICT; |
---|
64 | cell.data.Part.reserved = ALL_DISTRICT; |
---|
65 | return; |
---|
66 | } |
---|
67 | |
---|
68 | if( x < old_x ) |
---|
69 | { |
---|
70 | data.Part.reserved |= LEFT_DISTRICT; |
---|
71 | cell.data.Part.reserved |= RIGHT_DISTRICT; |
---|
72 | } |
---|
73 | else if( old_x < x ) |
---|
74 | { |
---|
75 | data.Part.reserved |= RIGHT_DISTRICT; |
---|
76 | cell.data.Part.reserved |= LEFT_DISTRICT; |
---|
77 | } |
---|
78 | if( y < old_y ) |
---|
79 | { |
---|
80 | data.Part.reserved |= UPPER_DISTRICT; |
---|
81 | cell.data.Part.reserved |= LOWER_DISTRICT; |
---|
82 | } |
---|
83 | else if( old_y < y ) |
---|
84 | { |
---|
85 | data.Part.reserved |= LOWER_DISTRICT; |
---|
86 | cell.data.Part.reserved |= UPPER_DISTRICT; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | void Compute(uint32 &x, uint32 &y) const |
---|
91 | { |
---|
92 | x = data.Part.grid_x*MAX_NUMBER_OF_CELLS + data.Part.cell_x; |
---|
93 | y = data.Part.grid_y*MAX_NUMBER_OF_CELLS + data.Part.cell_y; |
---|
94 | } |
---|
95 | |
---|
96 | inline bool DiffCell(const Cell &cell) const |
---|
97 | { |
---|
98 | return( data.Part.cell_x != cell.data.Part.cell_x || |
---|
99 | data.Part.cell_y != cell.data.Part.cell_y ); |
---|
100 | } |
---|
101 | |
---|
102 | inline bool DiffGrid(const Cell &cell) const |
---|
103 | { |
---|
104 | return( data.Part.grid_x != cell.data.Part.grid_x || |
---|
105 | data.Part.grid_y != cell.data.Part.grid_y ); |
---|
106 | } |
---|
107 | |
---|
108 | uint32 CellX() const { return data.Part.cell_x; } |
---|
109 | uint32 CellY() const { return data.Part.cell_y; } |
---|
110 | uint32 GridX() const { return data.Part.grid_x; } |
---|
111 | uint32 GridY() const { return data.Part.grid_y; } |
---|
112 | bool NoCreate() const { return data.Part.nocreate; } |
---|
113 | void SetNoCreate() { data.Part.nocreate = 1; } |
---|
114 | |
---|
115 | CellPair cellPair() const |
---|
116 | { |
---|
117 | return CellPair( |
---|
118 | data.Part.grid_x*MAX_NUMBER_OF_CELLS+data.Part.cell_x, |
---|
119 | data.Part.grid_y*MAX_NUMBER_OF_CELLS+data.Part.cell_y); |
---|
120 | } |
---|
121 | |
---|
122 | Cell& operator=(const Cell &cell) |
---|
123 | { |
---|
124 | this->data.All = cell.data.All; |
---|
125 | return *this; |
---|
126 | } |
---|
127 | |
---|
128 | bool operator==(const Cell &cell) const { return (data.All == cell.data.All); } |
---|
129 | bool operator!=(const Cell &cell) const { return !operator==(cell); } |
---|
130 | union |
---|
131 | { |
---|
132 | struct |
---|
133 | { |
---|
134 | unsigned grid_x : 6; |
---|
135 | unsigned grid_y : 6; |
---|
136 | unsigned cell_x : 4; |
---|
137 | unsigned cell_y : 4; |
---|
138 | unsigned nocreate : 1; |
---|
139 | unsigned reserved : 11; |
---|
140 | } Part; |
---|
141 | uint32 All; |
---|
142 | } data; |
---|
143 | |
---|
144 | template<class LOCK_TYPE, class T, class CONTAINER> void Visit(const CellLock<LOCK_TYPE> &, TypeContainerVisitor<T, CONTAINER> &visitor, Map &) const; |
---|
145 | }; |
---|
146 | |
---|
147 | template<class T> |
---|
148 | struct TRINITY_DLL_DECL CellLock |
---|
149 | { |
---|
150 | const Cell& i_cell; |
---|
151 | const CellPair &i_cellPair; |
---|
152 | CellLock(const Cell &c, const CellPair &p) : i_cell(c), i_cellPair(p) {} |
---|
153 | CellLock(const CellLock<T> &cell) : i_cell(cell.i_cell), i_cellPair(cell.i_cellPair) {} |
---|
154 | const Cell* operator->(void) const { return &i_cell; } |
---|
155 | const Cell* operator->(void) { return &i_cell; } |
---|
156 | operator const Cell &(void) const { return i_cell; } |
---|
157 | CellLock<T>& operator=(const CellLock<T> &cell) |
---|
158 | { |
---|
159 | this->~CellLock(); |
---|
160 | new (this) CellLock<T>(cell); |
---|
161 | return *this; |
---|
162 | } |
---|
163 | }; |
---|
164 | #endif |
---|