-
Notifications
You must be signed in to change notification settings - Fork 1
/
FieldOfView.cpp
367 lines (341 loc) · 14.2 KB
/
FieldOfView.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// Created by KunstDerFuge on 3/4/18.
// C++ adaptation of Mingos' Restrictive Precise Angle Shadowcasting V1.1 from libtcod.
//
#include <iostream>
#include <SFML/Graphics/RenderWindow.hpp>
#include "FieldOfView.h"
#include "Player.h"
using namespace std;
#define MAX(a,b) ((a)<(b)?(b):(a))
#define MIN(a,b) ((a)>(b)?(b):(a))
bool FieldOfView::isVisible(long x, long y) {
return cells[mapCoordToIndex(x, y)].visible;
}
void FieldOfView::markAsVisible(long x, long y) {
cells[mapCoordToIndex(x, y)].visible = true;
}
int FieldOfView::getViewWidthTiles(int resolutionX, int tileWidth) {
int viewWidthInTiles = resolutionX / tileWidth;
return viewWidthInTiles + 2;
}
int FieldOfView::getViewHeightTiles(int resolutionY, int tileWidth) {
int viewHeightInTiles = resolutionY / tileWidth;
return viewHeightInTiles + 2;
}
FieldOfView::FieldOfView(Player* player, const sf::RenderTexture& mapWindow, int tileWidth, WorldMap* worldMap) {
start_angle = nullptr;
end_angle = nullptr;
allocated = 0;
auto mapSize = mapWindow.getSize();
this->player = player;
cout << "Window size at FOV creation: " << mapSize.x << "x" << mapSize.y << endl;
this->width = getViewWidthTiles(mapSize.x, tileWidth);
this->height = getViewHeightTiles(mapSize.y, tileWidth);
if (width % 2 == 0) --width;
if (height % 2 == 0) --height;
cout << "FOV map dimensions: " << this->width << "x" << this->height << endl;
cout << "Creating FOV: (" << width << ", " << height << ")" << endl;
cout << "FOV Top left: (" << left << ", " << top << ")" << endl;
cells = vector<Cell>(static_cast<unsigned long>(width * height));
cout << "Size of cells vector: " << cells.size() << endl;
this->worldMap = worldMap;
}
void FieldOfView::invalidate(int tileWidth, const sf::RenderTexture& mapWindow) {
auto mapSize = mapWindow.getSize();
this->width = getViewWidthTiles(mapSize.x, tileWidth);
this->height = getViewHeightTiles(mapSize.y, tileWidth);
if (width % 2 == 0) --width;
if (height % 2 == 0) --height;
cout << "FOV is now " << width << "x" << height << endl;
cells = vector<Cell>(static_cast<unsigned long>(width * height));
}
long FieldOfView::mapCoordToIndex(long x, long y) {
x -= this->left;
y -= this->top;
auto out = (y * width) + x;
if (out > cells.size())
return false;
return out;
}
void FieldOfView::update() {
long square = 0;
auto playerLocation = player->getLocation();
auto focus = player->getFocus();
this->left = focus.x - width / 2;
this->top = focus.y - height / 2;
for (long y = top; y < top + height; ++y) {
for (long x = left; x < left + width; ++x) {
cells[square].transparent = !worldMap->isOpaque(Point(x, y));
++square;
}
}
for (auto cell : cells) {
cell.visible = false;
}
int max_obstacles;
/*first, zero the FOV map */
for(long c = cells.size() - 1; c >= 0; --c) cells[c].visible = false;
/*calculate an approximated (excessive, just in case) maximum number of obstacles per octant */
max_obstacles = static_cast<int>(cells.size() / 7);
if (max_obstacles > allocated) {
allocated = max_obstacles;
delete start_angle;
delete end_angle;
start_angle = new double[max_obstacles];
end_angle = new double[max_obstacles];
}
/*set PC's position as visible */
cells[mapCoordToIndex(playerLocation.x, playerLocation.y)].visible = true;
/*compute the 4 quadrants of the map */
int centerX = static_cast<int>(playerLocation.x - left);
int centerY = static_cast<int>(playerLocation.y - top);
computeQuadrant(centerX, centerY, 1, 1);
computeQuadrant(centerX, centerY, 1, -1);
computeQuadrant(centerX, centerY, -1, 1);
computeQuadrant(centerX, centerY, -1, -1);
}
void FieldOfView::computeQuadrant(int player_x, int player_y, int dx, int dy) {
FieldOfView* m = this;
bool light_walls = true;
int max_radius = 200;
/*octant: vertical edge */
{
int iteration = 1; /*iteration of the algo for this octant */
bool done = false;
int total_obstacles = 0;
int obstacles_in_last_line = 0;
double min_angle = 0.0;
long x,y;
/*do while there are unblocked slopes left and the algo is within the map's boundaries
scan progressive lines/columns from the PC outwards */
y = player_y+dy; /*the outer slope's coordinates (first processed line) */
if (y < 0 || y >= m->height) done = true;
while(!done) {
/*process cells in the line */
auto slopes_per_cell = 1.0 / double(iteration);
double half_slopes = slopes_per_cell * 0.5;
auto processed_cell = int((min_angle + half_slopes) / slopes_per_cell);
long minx = MAX(0,player_x-iteration), maxx = MIN(m->width-1,player_x+iteration);
done = true;
for (x = player_x + (processed_cell * dx); x >= minx && x <= maxx; x+=dx) {
long c = x + (y * m->width);
/*calculate slopes per cell */
bool visible = true;
bool extended = false;
double centre_slope = (double)processed_cell * slopes_per_cell;
double start_slope = centre_slope - half_slopes;
double end_slope = centre_slope + half_slopes;
if (obstacles_in_last_line > 0 && !m->cells[c].visible) {
int idx = 0;
if ((!m->cells[c - (m->width * dy)].visible || !m->cells[c - (m->width * dy)].transparent) && (x - dx >= 0 && x - dx < m->width && (
!m->cells[c - (m->width * dy) - dx].visible || !m->cells[c - (m->width * dy) - dx].transparent))) visible = false;
else while(visible && idx < obstacles_in_last_line) {
if (start_angle[idx] > end_slope || end_angle[idx] < start_slope) {
++idx;
}
else {
if (m->cells[c].transparent) {
if (centre_slope > start_angle[idx] && centre_slope < end_angle[idx])
visible = false;
}
else {
if (start_slope >= start_angle[idx] && end_slope <= end_angle[idx])
visible = false;
else {
start_angle[idx] = MIN(start_angle[idx],start_slope);
end_angle[idx] = MAX(end_angle[idx],end_slope);
extended = true;
}
}
++idx;
}
}
}
if (visible) {
m->cells[c].visible = true;
done = false;
/*if the cell is opaque, block the adjacent slopes */
if (!m->cells[c].transparent) {
if (min_angle >= start_slope) {
min_angle = end_slope;
/* if min_angle is applied to the last cell in line, nothing more
needs to be checked. */
if (processed_cell == iteration) done = true;
}
else if (!extended) {
start_angle[total_obstacles] = start_slope;
end_angle[total_obstacles++] = end_slope;
}
if (!light_walls) m->cells[c].visible = false;
}
}
processed_cell++;
}
if (iteration == max_radius) done = true;
iteration++;
obstacles_in_last_line = total_obstacles;
y += dy;
if (y < 0 || y >= m->height) done = true;
}
}
/*octant: horizontal edge */
{
int iteration = 1; /*iteration of the algo for this octant */
bool done = false;
int total_obstacles = 0;
int obstacles_in_last_line = 0;
double min_angle = 0.0;
long x,y;
/*do while there are unblocked slopes left and the algo is within the map's boundaries
scan progressive lines/columns from the PC outwards */
x = player_x+dx; /*the outer slope's coordinates (first processed line) */
if (x < 0 || x >= m->width) done = true;
while(!done) {
/*process cells in the line */
double slopes_per_cell = 1.0 / (double)(iteration);
double half_slopes = slopes_per_cell * 0.5;
auto processed_cell = (int)((min_angle + half_slopes) / slopes_per_cell);
long miny = MAX(0,player_y-iteration), maxy = MIN(m->height-1,player_y+iteration);
done = true;
for (y = player_y + (processed_cell * dy); y >= miny && y <= maxy; y+=dy) {
long c = x + (y * m->width);
/*calculate slopes per cell */
bool visible = true;
bool extended = false;
double centre_slope = (double)processed_cell * slopes_per_cell;
double start_slope = centre_slope - half_slopes;
double end_slope = centre_slope + half_slopes;
if (obstacles_in_last_line > 0 && !m->cells[c].visible) {
int idx = 0;
if ((!m->cells[c - dx].visible || !m->cells[c - dx].transparent) && (y - dy >= 0 && y - dy < m->height && (
!m->cells[c - (m->width * dy) - dx].visible || !m->cells[c - (m->width * dy) - dx].transparent))) visible = false;
else while(visible && idx < obstacles_in_last_line) {
if (start_angle[idx] > end_slope || end_angle[idx] < start_slope) {
++idx;
}
else {
if (m->cells[c].transparent) {
if (centre_slope > start_angle[idx] && centre_slope < end_angle[idx])
visible = false;
}
else {
if (start_slope >= start_angle[idx] && end_slope <= end_angle[idx])
visible = false;
else {
start_angle[idx] = MIN(start_angle[idx],start_slope);
end_angle[idx] = MAX(end_angle[idx],end_slope);
extended = true;
}
}
++idx;
}
}
}
if (visible) {
m->cells[c].visible = true;
done = false;
/*if the cell is opaque, block the adjacent slopes */
if (!m->cells[c].transparent) {
if (min_angle >= start_slope) {
min_angle = end_slope;
/* if min_angle is applied to the last cell in line, nothing more
needs to be checked. */
if (processed_cell == iteration) done = true;
}
else if (!extended) {
start_angle[total_obstacles] = start_slope;
end_angle[total_obstacles++] = end_slope;
}
if (!light_walls) m->cells[c].visible = false;
}
}
processed_cell++;
}
if (iteration == max_radius) done = true;
iteration++;
obstacles_in_last_line = total_obstacles;
x += dx;
if (x < 0 || x >= m->width) done = true;
}
}
}
void FieldOfView::castRay(Point from, Point to) {
// Let's get Bresenheimy
// cout << "Casting from (" << from.x << ", " << from.y << ") to (" << to.x << ", " << to.y << ")" << endl;
bool swapXY = abs(to.y - from.y) > abs(to.x - from.x);
long x0, y0, x1, y1;
if (swapXY) {
x0 = from.y;
y0 = from.x;
x1 = to.y;
y1 = to.x;
} else {
x0 = from.x;
y0 = from.y;
x1 = to.x;
y1 = to.y;
}
float deltaX = x1 - x0;
float deltaY = abs(y1 - y0);
float error = deltaX / 2;
long y = y0;
int stepY = -1;
if (y0 < y1) stepY = 1;
if (x1 > x0) { // X is increasing
for (long x = x0; x <= x1; ++x) {
if (swapXY) {
markAsVisible(y, x);
error -= deltaY;
if (worldMap->isOpaque(Point(y, x))) {
return;
}
} else {
markAsVisible(x, y);
error -= deltaY;
if (worldMap->isOpaque(Point(x, y))) {
return;
}
}
if (error < 0) {
y += stepY;
error += deltaX;
}
}
} else { // X is decreasing
for (long x = x0; x >= x1; --x) {
if (swapXY) {
markAsVisible(y, x);
error -= deltaY;
if (worldMap->isOpaque(Point(y, x))) {
return;
}
} else {
markAsVisible(x, y);
error -= deltaY;
if (worldMap->isOpaque(Point(x, y))) {
return;
}
}
if (error < 0) {
y -= stepY;
error -= deltaX;
}
}
}
}
int FieldOfView::getWidth() {
return width;
}
int FieldOfView::getHeight() {
return height;
}
long FieldOfView::getTop() {
return top;
}
long FieldOfView::getLeft() {
return left;
}
bool FieldOfView::isVisible(Point location) {
return isVisible(location.x, location.y);
}