-
Notifications
You must be signed in to change notification settings - Fork 224
/
map_region.cpp
271 lines (233 loc) · 5.4 KB
/
map_region.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "map_region.h"
#include "basemap.h"
#include "position.h"
#include "tile.h"
//**************** Tile Location **********************
TileLocation::TileLocation() :
tile(nullptr),
position(0, 0, 0),
spawn_count(0),
waypoint_count(0),
house_exits(nullptr)
{
////
}
TileLocation::~TileLocation()
{
delete tile;
delete house_exits;
}
int TileLocation::size() const
{
if(tile)
return tile->size();
return spawn_count + waypoint_count + (house_exits? 1 : 0);
}
bool TileLocation::empty() const
{
return size() == 0;
}
HouseExitList* TileLocation::createHouseExits()
{
if(!house_exits)
house_exits = new HouseExitList();
return house_exits;
}
//**************** Floor **********************
Floor::Floor(int sx, int sy, int z)
{
sx = sx & ~3;
sy = sy & ~3;
for(int i = 0; i < rme::MapLayers; ++i) {
locs[i].position.x = sx + (i >> 2);
locs[i].position.y = sy + (i & 3);
locs[i].position.z = z;
}
}
//**************** QTreeNode **********************
QTreeNode::QTreeNode(BaseMap& map) :
map(map),
visible(0),
isLeaf(false)
{
// Doesn't matter if we're leaf or node
for(int i = 0; i < rme::MapLayers; ++i)
child[i] = nullptr;
}
QTreeNode::~QTreeNode()
{
if(isLeaf) {
for(int i = 0; i < rme::MapLayers; ++i)
delete array[i];
} else {
for(int i = 0; i < rme::MapLayers; ++i)
delete child[i];
}
}
QTreeNode* QTreeNode::getLeaf(int x, int y)
{
QTreeNode* node = this;
uint32_t cx = x, cy = y;
while(node) {
if(node->isLeaf) {
return node;
} else {
uint32_t index = ((cx & 0xC000) >> 14) | ((cy & 0xC000) >> 12);
if(node->child[index]) {
node = node->child[index];
cx <<= 2;
cy <<= 2;
} else {
return nullptr;
}
}
}
return nullptr;
}
QTreeNode* QTreeNode::getLeafForce(int x, int y)
{
QTreeNode* node = this;
uint32_t cx = x, cy = y;
int level = 6;
while(node) {
uint32_t index = ((cx & 0xC000) >> 14) | ((cy & 0xC000) >> 12);
QTreeNode*& qt = node->child[index];
if(qt) {
if(qt->isLeaf)
return qt;
} else {
if(level == 0) {
qt = newd QTreeNode(map);
qt->isLeaf = true;
return qt;
} else {
qt = newd QTreeNode(map);
}
}
node = node->child[index];
cx <<= 2;
cy <<= 2;
level -= 1;
}
return nullptr;
}
Floor* QTreeNode::createFloor(int x, int y, int z)
{
ASSERT(isLeaf);
if(!array[z])
array[z] = newd Floor(x, y, z);
return array[z];
}
bool QTreeNode::isVisible(bool underground)
{
return testFlags(visible, underground + 1);
}
bool QTreeNode::isRequested(bool underground)
{
if(underground) {
return testFlags(visible, 4);
} else {
return testFlags(visible, 8);
}
}
void QTreeNode::clearVisible(uint32_t u)
{
if(isLeaf)
visible &= u;
else
for(int i = 0; i < rme::MapLayers; ++i)
if(child[i])
child[i]->clearVisible(u);
}
bool QTreeNode::isVisible(uint32_t client, bool underground)
{
if(underground) {
return testFlags(visible >> rme::MapLayers, static_cast<uint64_t>(1) << client);
} else {
return testFlags(visible, static_cast<uint64_t>(1) << client);
}
}
void QTreeNode::setVisible(bool underground, bool value)
{
if(underground) {
if(value)
visible |= 2;
else
visible &= ~2;
} else { // overground
if(value)
visible |= 1;
else
visible &= 1;
}
}
void QTreeNode::setRequested(bool underground, bool r)
{
if(r)
visible |= (underground? 4 : 8);
else
visible &= ~(underground? 4 : 8);
}
void QTreeNode::setVisible(uint32_t client, bool underground, bool value)
{
if(value)
visible |= (1 << client << (underground ? rme::MapLayers : 0));
else
visible &= ~(1 << client << (underground ? rme::MapLayers : 0));
}
TileLocation* QTreeNode::getTile(int x, int y, int z)
{
ASSERT(isLeaf);
Floor* f = array[z];
if(!f)
return nullptr;
return &f->locs[(x & 3) * 4 + (y & 3)];
}
TileLocation* QTreeNode::createTile(int x, int y, int z)
{
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
return &f->locs[(x & 3) * 4 + (y & 3)];
}
Tile* QTreeNode::setTile(int x, int y, int z, Tile* newtile)
{
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
int offset_x = x & 3;
int offset_y = y & 3;
TileLocation* tmp = &f->locs[offset_x*4+offset_y];
Tile* oldtile = tmp->tile;
tmp->tile = newtile;
if(newtile && !oldtile)
++map.tilecount;
else if(oldtile && !newtile)
--map.tilecount;
return oldtile;
}
void QTreeNode::clearTile(int x, int y, int z)
{
ASSERT(isLeaf);
Floor* f = createFloor(x, y, z);
int offset_x = x & 3;
int offset_y = y & 3;
TileLocation* tmp = &f->locs[offset_x*4+offset_y];
delete tmp->tile;
tmp->tile = map.allocator(tmp);
}