This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vision.cpp
324 lines (277 loc) · 8.6 KB
/
Vision.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
/* code to determine if there is a line of sight between two points
on the map.
.....#.... In this diagram, A should be able to see D, but not B (behind
A....#B... obstacle X) - but what about C? The rules we'll use:
.........C - obstacles fill their entire square.
.........D - vision is calculated from the center of the observer's square
- an object is visible iff any part of a sub square (0.2,0.2;
0.8,0.8) can be intersected by a ray cast from the observer
......... In this diagram, B is hiding in an alcove, in a position where
..A...... A might be able to see B and not vice-versa. Can we implement
######B## some sort of peeking system?
.....###.
Well, I went with a beam-casting algorithm posted by Isaac Kuo on
rec.games.roguelike.development - my implementation's got some bugs
in the corner cases, but it'll do for now.
*/
#include "Global.h"
#include "Map.h"
#include "Hero.h"
/* computes all the squares visible by the Hero */
void
shMapLevel::computeVisibility ()
{
int slope; /* slope in v coordinate */
int u, v;
int x, y;
int corner;
int min, max; /* width of beam along v axis */
//I->debug ("**** computing vis ****");
for (x = 0; x < MAPMAXCOLUMNS; x++) {
for (y = 0; y < MAPMAXROWS; y++) {
mVisibility[x][y] = 0;
}
}
/* orthogonal first */
mVisibility[Hero.mX][Hero.mY] = 1;
if (Hero.mZ < 0) {
/* in a pit */
if (isObstacle (Hero.mX, Hero.mY))
return; /* sealed in */
if (Hero. isUnderwater ())
return;
for (x = Hero.mX-1; x <= Hero.mX+1; x++) {
for (y = Hero.mY-1; y < Hero.mY+1; y++) {
mVisibility[x][y] = 1;
}
}
return;
}
for (x = Hero.mX;
mVisibility[x][Hero.mY] = 1, x < MAPMAXCOLUMNS-1
&& !isOcclusive (x, Hero.mY);
x++);
for (x = Hero.mX;
mVisibility[x][Hero.mY] = 1, x && !isOcclusive (x, Hero.mY);
x--);
for (y = Hero.mY;
mVisibility[Hero.mX][y] = 1, y < MAPMAXROWS-1
&& !isOcclusive (Hero.mX, y);
y++);
for (y = Hero.mY;
mVisibility[Hero.mX][y] = 1, y && !isOcclusive (Hero.mX, y);
y--);
/* now each quadrant */
#define BEAMS 32
#define MAXD 100
for (slope = 1; slope < BEAMS; slope++) {
for (u = 1, v = slope, min = 0, max = BEAMS-1;
u < MAXD && min <= max;
u++, v += slope)
{
y = v / BEAMS;
x = u - y;
x += Hero.mX;
y += Hero.mY;
if (x >= MAPMAXCOLUMNS || y >= MAPMAXROWS)
break;
corner = BEAMS - v % BEAMS;
if (min < corner) {
mVisibility[x][y] = 1;
if (isOcclusive (x,y)) {
min = corner;
}
}
if (max > corner && y < MAPMAXROWS-1) {
mVisibility[x-1][y+1] = 1;
if (isOcclusive (x - 1, y + 1)) {
max = corner;
}
}
}
}
for (slope = 1; slope < BEAMS; slope++) {
for (u = 1, v = slope, min = 0, max = BEAMS-1;
u < MAXD && min <= max;
u++, v += slope)
{
y = v / BEAMS;
x = u - y;
x = Hero.mX - x;
y += Hero.mY;
if (x < 0 || y >= MAPMAXROWS)
break;
corner = BEAMS - v % BEAMS;
if (min < corner) {
mVisibility[x][y] = 1;
if (isOcclusive (x,y)) {
min = corner;
}
}
if (max > corner && y < MAPMAXROWS-1) {
mVisibility[x+1][y+1] = 1;
if (isOcclusive (x + 1, y + 1)) {
max = corner;
}
}
}
}
for (slope = 1; slope < BEAMS; slope++) {
for (u = 1, v = slope, min = 0, max = BEAMS-1;
u < MAXD && min <= max;
u++, v += slope)
{
y = v / BEAMS;
x = u - y;
x = Hero.mX - x;
y = Hero.mY - y;
if (x < 0 || y < 0)
break;
corner = BEAMS - v % BEAMS;
if (min < corner) {
mVisibility[x][y] = 1;
if (isOcclusive (x,y)) {
min = corner;
}
}
if (max > corner && y > 0) {
mVisibility[x+1][y-1] = 1;
if (isOcclusive (x + 1, y - 1)) {
max = corner;
}
}
}
}
for (slope = 1; slope < BEAMS; slope++) {
for (u = 1, v = slope, min = 0, max = BEAMS-1;
u < MAXD && min <= max;
u++, v += slope)
{
y = v / BEAMS;
x = u - y;
x = x + Hero.mX;
y = Hero.mY - y;
if (x >= MAPMAXCOLUMNS || y < 0)
break;
corner = BEAMS - v % BEAMS;
if (min < corner) {
mVisibility[x][y] = 1;
if (isOcclusive (x,y)) {
min = corner;
}
}
if (max > corner && y > 0) {
mVisibility[x-1][y-1] = 1;
if (isOcclusive (x - 1, y - 1)) {
max = corner;
}
}
}
}
/* corner peeking */
if (Hero.mY > 1) {
for (x = Hero.mX, y = Hero.mY-1;
x < MAPMAXCOLUMNS-1 && !isOcclusive (x, y) ;
x++)
{
mVisibility[x][y-1] = mVisibility[x+1][y-1] =
mVisibility[x+1][y] = 1;
}
for (x = Hero.mX, y = Hero.mY-1; x > 0 && !isOcclusive (x, y);
x--)
{
mVisibility[x][y-1] = mVisibility[x-1][y-1] =
mVisibility[x-1][y] = 1;
}
}
if (Hero.mY < MAPMAXROWS - 2) {
for (x = Hero.mX, y = Hero.mY+1;
x < MAPMAXCOLUMNS-1 && !isOcclusive (x, y) ;
x++)
{
mVisibility[x][y+1] = mVisibility[x+1][y+1] =
mVisibility[x+1][y] = 1;
}
for (x = Hero.mX, y = Hero.mY+1; x > 0 && !isOcclusive (x, y);
x--)
{
mVisibility[x][y+1] = mVisibility[x-1][y+1] =
mVisibility[x-1][y] = 1;
}
}
if (Hero.mX > 1) {
for (x = Hero.mX-1, y = Hero.mY;
y < MAPMAXROWS-1 && !isOcclusive (x, y);
y++)
{
mVisibility[x-1][y] = mVisibility[x-1][y+1] =
mVisibility[x][y+1] = 1;
}
for (x = Hero.mX-1, y = Hero.mY; y > 0 && !isOcclusive (x, y);
y--)
{
mVisibility[x-1][y] = mVisibility[x-1][y-1] =
mVisibility[x][y-1] = 1;
}
}
if (Hero.mX < MAPMAXCOLUMNS - 2) {
for (x = Hero.mX+1, y = Hero.mY;
y < MAPMAXROWS-1 && !isOcclusive (x, y);
y++)
{
mVisibility[x+1][y] = mVisibility[x+1][y+1] =
mVisibility[x][y+1] = 1;
}
for (x = Hero.mX+1, y = Hero.mY; y > 0 && !isOcclusive (x, y);
y--)
{
mVisibility[x+1][y] = mVisibility[x+1][y-1] =
mVisibility[x][y-1] = 1;
}
}
Hero.spotStuff ();
#if 0
if (Hero.hasXRayVision ()) {
int radius = 4;// + Hero.mCLevel / 4;
for (x = Hero.mX - radius; x <= Hero.mX + radius; x++) {
for (y = Hero.mY - radius; y <= Hero.mY + radius; y++) {
if (Level->isInBounds (x, y)
&& distance (&Hero, x, y) < 5 * radius)
{
mVisibility[x][y] = 1;
}
}
}
}
#endif
}
/* returns 100 if clear line of sight between the points, 0 o/w */
int
shMapLevel::existsLOS (int x1, int y1, int x2, int y2)
{
int n;
int i;
double x, y;
double dx, dy;
n = abs (x2 - x1);
i = abs (y2 - y1);
if (i > n) n = i;
dx = (double) (x2 - x1) / (double) n;
dy = (double) (y2 - y1) / (double) n;
x = x1 + 0.5;
y = y1 + 0.5;
for (i = 0; i < n; i++) {
int ix = (int) x;
int iy = (int) y;
if ((ix == x1 && iy == y1) ||
(ix == x2 && iy == y2))
{
/* we only check in between squares, not start and end */
} else if (isOcclusive (ix, iy)) {
return 0;
}
x += dx;
y += dy;
}
return 100;
}