-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
254 lines (222 loc) · 8.37 KB
/
map.cpp
File metadata and controls
254 lines (222 loc) · 8.37 KB
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
#include "map.hpp"
namespace dungeon
{
static int sign (int val)
{
if (val > 0) {
return 1;
} else if (val < 0) {
return -1;
} else {
return 0;
}
}
bool Map::CheckCornerVisibility(unsigned int cornerx, unsigned int cornery,
int signx, int signy) const
{
if (GetTile(cornerx, cornery).BlocksVisibility()) {
if (GetTile(cornerx - 1, cornery).BlocksVisibility()) {
return false;
}
if (GetTile(cornerx, cornery - 1).BlocksVisibility()) {
return false;
}
}
if (GetTile(cornerx - 1, cornery - 1).BlocksVisibility()) {
if (GetTile(cornerx, cornery - 1).BlocksVisibility()) {
return false;
}
if (GetTile(cornerx - 1, cornery).BlocksVisibility()) {
return false;
}
}
if (signx * signy == 1) {
if (GetTile(cornerx, cornery).BlocksVisibility() ||
GetTile(cornerx - 1, cornery - 1).BlocksVisibility()) {
return false;
}
} else if (signx * signy == -1) {
if (GetTile(cornerx - 1, cornery).BlocksVisibility() ||
GetTile(cornerx, cornery - 1).BlocksVisibility()) {
return false;
}
} else {
if (GetTile(cornerx - 1, cornery - 1).BlocksVisibility() ||
GetTile(cornerx - 1, cornery).BlocksVisibility() ||
GetTile(cornerx, cornery - 1).BlocksVisibility() ||
GetTile(cornerx, cornery).BlocksVisibility()) {
return false;
}
}
return true;
}
bool Map::CheckRayVisibility(unsigned int startx, unsigned int starty,
unsigned int endx, unsigned int endy) const
{
int signx = sign(static_cast<int>(endx) - static_cast<int>(startx));
int signy = sign(static_cast<int>(endy) - static_cast<int>(starty));
// Check whether the immediate neighbour in the given direction blocks.
if (signx == 0 && signy == -1 &&
GetTile(startx, starty - 1).BlocksVisibility() &&
GetTile(startx - 1, starty - 1).BlocksVisibility()) {
return false;
}
if (signx == -1 && signy == 0 &&
GetTile(startx - 1, starty).BlocksVisibility() &&
GetTile(startx - 1, starty - 1).BlocksVisibility()) {
return false;
}
if (signx == 0 && signy == 1 &&
GetTile(startx, starty).BlocksVisibility() &&
GetTile(startx - 1, starty).BlocksVisibility()) {
return false;
}
if (signx == 1 && signy == 0 &&
GetTile(startx, starty).BlocksVisibility() &&
GetTile(startx, starty - 1).BlocksVisibility()) {
return false;
}
if (signx == 1 && signy == 1 &&
GetTile(startx, starty).BlocksVisibility()) {
return false;
}
if (signx == -1 && signy == 1 &&
GetTile(startx - 1, starty).BlocksVisibility()) {
return false;
}
if (signx == -1 && signy == -1 &&
GetTile(startx - 1, starty - 1).BlocksVisibility()) {
return false;
}
if (signx == 1 && signy == -1 &&
GetTile(startx, starty - 1).BlocksVisibility()) {
return false;
}
unsigned int dx = std::abs(static_cast<int>(endx) -
static_cast<int>(startx));
unsigned int dy = std::abs(static_cast<int>(endy) -
static_cast<int>(starty));
int ny;
int nx;
int ry;
int rx;
unsigned int cellx;
unsigned int celly;
// Check vertical intersections
if (dx != 0) {
for (int x = 1; x < static_cast<int>(dx); x++) {
ny = x * dy / dx;
ry = x * dy % dx;
cellx = static_cast<int>(startx) + x * signx;
celly = static_cast<int>(starty) + ny * signy;
if (ry != 0) {
if (signy < 0) {
celly--;
}
// Check for a wall between this tile and the tile to the
// left (i.e. if either this tile or the tile to the left
// blocks vis).
if (GetTile(cellx, celly).BlocksVisibility() ||
GetTile(cellx - 1, celly).BlocksVisibility()) {
return false;
}
} else {
// The ray intersects with a corner, need special
// handling.
if (!CheckCornerVisibility(cellx, celly, signx, signy)) {
return false;
}
}
}
}
// Check horizontal intersections
if (dy != 0) {
for (int y = 1; y < static_cast<int>(dy); y++) {
nx = y * dx / dy;
rx = y * dx % dy;
cellx = static_cast<int>(startx) + nx * signx;
celly = static_cast<int>(starty) + y * signy;
if (rx != 0) {
if (signx < 0) {
cellx--;
}
// Check for a wall between this tile and the tile above it
// (i.e. if either this tile or the tile above it blocks
// vis).
if (GetTile(cellx, celly).BlocksVisibility() ||
GetTile(cellx, celly - 1).BlocksVisibility()) {
return false;
}
} else {
// The ray intersects with a corner, need special
// handling.
if (!CheckCornerVisibility(cellx, celly, signx, signy)) {
return false;
}
}
}
}
return true;
}
bool Map::IsVisible (unsigned int startx, unsigned int starty,
unsigned int endx, unsigned int endy) const
{
unsigned int corners[4][2] = {
{ 0, 0 }, // Top left
{ 1, 0 }, // Top right
{ 1, 1 }, // Bottom right
{ 0, 1 }, // Bottom left
};
for (size_t i = 0; i < 4; i++) {
for (size_t j = 0; j < 4; j++) {
bool vis = CheckRayVisibility(startx + corners[i][0],
starty + corners[i][1],
endx + corners[j][0],
endy + corners[j][1]);
if (vis) {
return true;
}
}
}
return false;
}
std::vector<Tile *> Map::GetTileNeighbours (Tile *tile, bool diags)
{
std::vector<Tile *> neighbours;
if (tile->x > 0) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x - 1, tile->y)]);
}
if (tile->x < MAP_WIDTH - 1) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x + 1, tile->y)]);
}
if (tile->y > 0) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x, tile->y - 1)]);
}
if (tile->y < MAP_HEIGHT - 1) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x, tile->y + 1)]);
}
if (diags) {
if (tile->x > 0 && tile->y > 0) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x - 1, tile->y - 1)]);
}
if (tile->x > 0 && tile->y < MAP_HEIGHT - 1) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x - 1, tile->y + 1)]);
}
if (tile->x < MAP_WIDTH - 1 && tile->y > 0) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x + 1, tile->y - 1)]);
}
if (tile->x < MAP_WIDTH - 1 && tile->y < MAP_WIDTH - 1) {
neighbours.push_back(
&m_tiles[CoordsToTileIndex(tile->x + 1, tile->y + 1)]);
}
}
return neighbours;
}
}