ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/los.C
(Generate patch)

Comparing deliantra/server/common/los.C (file contents):
Revision 1.48 by root, Tue Dec 23 06:58:23 2008 UTC vs.
Revision 1.49 by root, Tue Dec 23 22:03:06 2008 UTC

19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * 20 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 22 */
23 23
24/* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */ 24#include <bench.h>//D
25
26#include <global.h> 25#include <global.h>
27#include <cmath> 26#include <cmath>
28 27
29static void expand_lighted_sight (object *op); 28// los flags
30
31enum { 29enum {
32 LOS_XI = 0x01, 30 FLG_XI = 0x01, // we have an x-parent
33 LOS_YI = 0x02, 31 FLG_YI = 0x02, // we have an y-parent
32 FLG_BLOCKED = 0x04, // this space blocks the view
33 FLG_QUEUED = 0x80 // already queued in queue, or border
34}; 34};
35 35
36struct los_info 36struct los_info
37{ 37{
38 uint8 flags; // FLG_xxx
39 uint8 culled; // culled from "tree"
40 uint8 visible;
41 uint8 pad0;
42
38 sint8 xo, yo; // obscure angle 43 sint8 xo, yo; // obscure angle
39 sint8 xe, ye; // angle deviation 44 sint8 xe, ye; // angle deviation
40 uint8 culled; // culled from "tree"
41 uint8 queued; // already queued
42 uint8 visible;
43 uint8 flags; // LOS_XI/YI
44}; 45};
45 46
46// temporary storage for the los algorithm, 47// temporary storage for the los algorithm,
47// one los_info for each lightable map space 48// one los_info for each lightable map space
48static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; 49static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y];
75enqueue (sint8 dx, sint8 dy, uint8 flags = 0) 76enqueue (sint8 dx, sint8 dy, uint8 flags = 0)
76{ 77{
77 sint8 x = LOS_X0 + dx; 78 sint8 x = LOS_X0 + dx;
78 sint8 y = LOS_Y0 + dy; 79 sint8 y = LOS_Y0 + dy;
79 80
80 if (x < 0 || x >= MAP_CLIENT_X) return;
81 if (y < 0 || y >= MAP_CLIENT_Y) return;
82
83 los_info &l = los[x][y]; 81 los_info &l = los[x][y];
84 82
85 l.flags |= flags; 83 l.flags |= flags;
86 84
87 if (l.queued) 85 if (l.flags & FLG_QUEUED)
88 return; 86 return;
89 87
90 l.queued = 1; 88 l.flags |= FLG_QUEUED;
91 89
92 queue[q1].x = dx; 90 queue[q1].x = dx;
93 queue[q1].y = dy; 91 queue[q1].y = dy;
94 92
95 q1 = (q1 + 1) & (QUEUE_LENGTH - 1); 93 q1 = (q1 + 1) & (QUEUE_LENGTH - 1);
101// which has been simplified and changed considerably, but 99// which has been simplified and changed considerably, but
102// still is basically the same algorithm. 100// still is basically the same algorithm.
103static void 101static void
104calculate_los (player *pl) 102calculate_los (player *pl)
105{ 103{
106 int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2; 104 {
105 // we keep one line for ourselves, for the border flag
106 // so the client area is actually MAP_CLIENT_(X|Y) - 2
107 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
108 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
107 109
108 memset (los, 0, sizeof (los)); 110 // create borders, the corners are not touched
111 for (int dx = -half_x; dx <= half_x; ++dx)
112 los [dx + LOS_X0][LOS_Y0 - (half_y + 1)].flags =
113 los [dx + LOS_X0][LOS_Y0 + (half_y + 1)].flags = FLG_QUEUED;
114
115 for (int dy = -half_y; dy <= half_y; ++dy)
116 los [LOS_X0 - (half_x + 1)][dy + LOS_Y0].flags =
117 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
118
119 // now reset the los area and also add blocked flags
120 // which supposedly is faster than doing it inside the
121 // spiral path algorithm below, except when very little
122 // area is visible, in which case it is slower, evening
123 // out los calculation times between large and small los maps.
124 // apply_lights also iterates over this area, maybe these
125 // two passes could be combined somehow.
126 rectangular_mapspace_iterate_begin (pl->observe, -half_x, half_x, -half_y, half_y)
127 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
128 l.flags = m && m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
129 rectangular_mapspace_iterate_end
130 }
109 131
110 q1 = 0; q2 = 0; // initialise queue, not strictly required 132 q1 = 0; q2 = 0; // initialise queue, not strictly required
111 enqueue (0, 0); // enqueue center 133 enqueue (0, 0); // enqueue center
112 134
113 // treat the origin specially 135 // treat the origin specially
126 q2 = (q2 + 1) & (QUEUE_LENGTH - 1); 148 q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
127 149
128 sint8 x = LOS_X0 + dx; 150 sint8 x = LOS_X0 + dx;
129 sint8 y = LOS_Y0 + dy; 151 sint8 y = LOS_Y0 + dy;
130 152
131 //int distance = idistance (dx, dy); if (distance > max_radius) continue;//D
132 int distance = 0;//D
133
134 los_info &l = los[x][y]; 153 los_info &l = los[x][y];
135 154
136 if (expect_true (l.flags & (LOS_XI | LOS_YI))) 155 if (expect_true (l.flags & (FLG_XI | FLG_YI)))
137 { 156 {
138 l.culled = 1; 157 l.culled = 1;
158 l.xo = l.yo = l.xe = l.ye = 0;
139 159
140 // check contributing spaces, first horizontal 160 // check contributing spaces, first horizontal
141 if (expect_true (l.flags & LOS_XI)) 161 if (expect_true (l.flags & FLG_XI))
142 { 162 {
143 los_info *xi = &los[x - sign (dx)][y]; 163 los_info *xi = &los[x - sign (dx)][y];
144 164
145 // don't cull unless obscured 165 // don't cull unless obscured
146 l.culled &= !xi->visible; 166 l.culled &= !xi->visible;
171 } 191 }
172 } 192 }
173 } 193 }
174 194
175 // check contributing spaces, last vertical, identical structure 195 // check contributing spaces, last vertical, identical structure
176 if (expect_true (l.flags & LOS_YI)) 196 if (expect_true (l.flags & FLG_YI))
177 { 197 {
178 los_info *yi = &los[x][y - sign (dy)]; 198 los_info *yi = &los[x][y - sign (dy)];
179 199
180 // don't cull unless obscured 200 // don't cull unless obscured
181 l.culled &= !yi->visible; 201 l.culled &= !yi->visible;
205 l.xo = yi->xo; 225 l.xo = yi->xo;
206 } 226 }
207 } 227 }
208 } 228 }
209 229
210 // check whether this space blocks the view 230 if (l.flags & FLG_BLOCKED)
211 maptile *m = pl->observe->map;
212 sint16 nx = pl->observe->x + dx;
213 sint16 ny = pl->observe->y + dy;
214
215 if (expect_true (!xy_normalise (m, nx, ny))
216 || expect_false (m->at (nx, ny).flags () & P_BLOCKSVIEW))
217 { 231 {
218 l.xo = l.xe = abs (dx); 232 l.xo = l.xe = abs (dx);
219 l.yo = l.ye = abs (dy); 233 l.yo = l.ye = abs (dy);
220 234
221 // we obscure dependents, but might be visible 235 // we obscure dependents, but might be visible
222 // copy the los from the square towards the player, 236 // copy the los from the square towards the player,
223 // so outward diagonal corners are lit. 237 // so outward diagonal corners are lit.
224 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED; 238 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED;
239
225 l.visible = false; 240 l.visible = false;
226 } 241 }
227 else 242 else
228 { 243 {
229 // we are not blocked, so calculate visibility, by checking 244 // we are not blocked, so calculate visibility, by checking
230 // whether we are inside or outside the shadow 245 // whether we are inside or outside the shadow
231 l.visible = (l.xe <= 0 || l.xe > l.xo) 246 l.visible = (l.xe <= 0 || l.xe > l.xo)
232 && (l.ye <= 0 || l.ye > l.yo); 247 && (l.ye <= 0 || l.ye > l.yo);
233 248
234 pl->los[x][y] = l.culled ? LOS_BLOCKED 249 pl->los[x][y] = l.culled ? LOS_BLOCKED
235 : l.visible ? max (0, 2 - max_radius + distance) 250 : l.visible ? 0
236 : 3; 251 : 3;
237 } 252 }
238 253
239 } 254 }
240 255
241 // Expands by the unit length in each component's current direction. 256 // Expands by the unit length in each component's current direction.
242 // If a component has no direction, then it is expanded in both of its 257 // If a component has no direction, then it is expanded in both of its
243 // positive and negative directions. 258 // positive and negative directions.
244 if (!l.culled) 259 if (!l.culled)
245 { 260 {
246 if (dx >= 0) enqueue (dx + 1, dy, LOS_XI); 261 if (dx >= 0) enqueue (dx + 1, dy, FLG_XI);
247 if (dx <= 0) enqueue (dx - 1, dy, LOS_XI); 262 if (dx <= 0) enqueue (dx - 1, dy, FLG_XI);
248 if (dy >= 0) enqueue (dx, dy + 1, LOS_YI); 263 if (dy >= 0) enqueue (dx, dy + 1, FLG_YI);
249 if (dy <= 0) enqueue (dx, dy - 1, LOS_YI); 264 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
250 } 265 }
251 } 266 }
252} 267}
253 268
254/* returns true if op carries one or more lights 269/* returns true if op carries one or more lights
272 287
273static struct los_init 288static struct los_init
274{ 289{
275 los_init () 290 los_init ()
276 { 291 {
292 assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two",
293 !(QUEUE_LENGTH & (QUEUE_LENGTH - 1))));
294
277 /* for lights */ 295 /* for lights */
278 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius) 296 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius)
279 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance) 297 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance)
280 { 298 {
281 // max intensity 299 // max intensity
345 if (op->flag [FLAG_SEE_IN_DARK]) 363 if (op->flag [FLAG_SEE_IN_DARK])
346 darklevel = max (0, darklevel - 2); 364 darklevel = max (0, darklevel - 2);
347 365
348 int half_x = pl->ns->mapx / 2; 366 int half_x = pl->ns->mapx / 2;
349 int half_y = pl->ns->mapy / 2; 367 int half_y = pl->ns->mapy / 2;
350
351 int min_x = op->x - half_x - MAX_LIGHT_RADIUS;
352 int min_y = op->y - half_y - MAX_LIGHT_RADIUS;
353 int max_x = op->x + half_x + MAX_LIGHT_RADIUS;
354 int max_y = op->y + half_y + MAX_LIGHT_RADIUS;
355 368
356 int pass2 = 0; // negative lights have an extra pass 369 int pass2 = 0; // negative lights have an extra pass
357 370
358 if (!darklevel) 371 if (!darklevel)
359 pass2 = 1; 372 pass2 = 1;
360 else 373 else
361 { 374 {
362 /* first, make everything totally dark */ 375 /* first, make everything totally dark */
363 for (int dx = -half_x; dx <= half_x; dx++) 376 for (int dx = -half_x; dx <= half_x; dx++)
364 for (int dy = -half_x; dy <= half_y; dy++) 377 for (int dy = -half_x; dy <= half_y; dy++)
365 if (pl->los[dx + LOS_X0][dy + LOS_Y0] != LOS_BLOCKED)
366 pl->los[dx + LOS_X0][dy + LOS_Y0] = LOS_MAX; 378 max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX);
367 379
368 /* 380 /*
369 * Only process the area of interest. 381 * Only process the area of interest.
370 * the basex, basey values represent the position in the op->contr->los 382 * the basex, basey values represent the position in the op->contr->los
371 * array. Its easier to just increment them here (and start with the right 383 * array. Its easier to just increment them here (and start with the right
372 * value) than to recalculate them down below. 384 * value) than to recalculate them down below.
373 */ 385 */
374 for (int x = min_x; x <= max_x; x++) 386 rectangular_mapspace_iterate_begin (pl->observe, -half_x - MAX_LIGHT_RADIUS, half_x + MAX_LIGHT_RADIUS, -half_y - MAX_LIGHT_RADIUS, half_y + MAX_LIGHT_RADIUS)
375 for (int y = min_y; y <= max_y; y++) 387 if (m)
376 { 388 {
377 maptile *m = pl->observe->map;
378 sint16 nx = x;
379 sint16 ny = y;
380
381 if (!xy_normalise (m, nx, ny))
382 continue;
383
384 mapspace &ms = m->at (nx, ny); 389 mapspace &ms = m->at (nx, ny);
385 ms.update (); 390 ms.update ();
386 sint8 light = ms.light; 391 sint8 light = ms.light;
387 392
388 if (expect_false (light)) 393 if (expect_false (light))
389 if (light < 0) 394 if (light < 0)
390 pass2 = 1; 395 pass2 = 1;
391 else 396 else
392 apply_light<los_brighten> (pl, x - op->x, y - op->y, light, light_atten [light + MAX_LIGHT_RADIUS]); 397 apply_light<los_brighten> (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]);
393 } 398 }
399 rectangular_mapspace_iterate_end
394 400
395 /* grant some vision to the player, based on the darklevel */ 401 /* grant some vision to the player, based on the darklevel */
396 { 402 {
397 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 403 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS);
398 404
402 408
403 // possibly do 2nd pass for rare negative glow radii 409 // possibly do 2nd pass for rare negative glow radii
404 // for effect, those are always considered to be stronger than anything else 410 // for effect, those are always considered to be stronger than anything else
405 // but they can't darken a place completely 411 // but they can't darken a place completely
406 if (pass2) 412 if (pass2)
407 for (int x = min_x; x <= max_x; x++) 413 rectangular_mapspace_iterate_begin (pl->observe, -half_x - MAX_LIGHT_RADIUS, half_x + MAX_LIGHT_RADIUS, -half_y - MAX_LIGHT_RADIUS, half_y + MAX_LIGHT_RADIUS)
408 for (int y = min_y; y <= max_y; y++) 414 if (m)
409 { 415 {
410 maptile *m = pl->observe->map;
411 sint16 nx = x;
412 sint16 ny = y;
413
414 if (!xy_normalise (m, nx, ny))
415 continue;
416
417 mapspace &ms = m->at (nx, ny); 416 mapspace &ms = m->at (nx, ny);
418 ms.update (); 417 ms.update ();
419 sint8 light = ms.light; 418 sint8 light = ms.light;
420 419
421 if (expect_false (light < 0)) 420 if (expect_false (light < 0))
422 apply_light<los_darken> (pl, x - op->x, y - op->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); 421 apply_light<los_darken> (pl, dx, dy, -light, light_atten [light + MAX_LIGHT_RADIUS]);
423 } 422 }
423 rectangular_mapspace_iterate_end
424} 424}
425 425
426/* blinded_sight() - sets all viewable squares to blocked except 426/* blinded_sight() - sets all viewable squares to blocked except
427 * for the one the central one that the player occupies. A little 427 * for the one the central one that the player occupies. A little
428 * odd that you can see yourself (and what your standing on), but 428 * odd that you can see yourself (and what your standing on), but
442player::update_los () 442player::update_los ()
443{ 443{
444 if (ob->flag [FLAG_REMOVED])//D really needed? 444 if (ob->flag [FLAG_REMOVED])//D really needed?
445 return; 445 return;
446 446
447 clear_los ();
448
449 if (ob->flag [FLAG_WIZLOOK]) 447 if (ob->flag [FLAG_WIZLOOK])
450 memset (los, 0, sizeof (los)); 448 clear_los (0);
451 else if (observe->flag [FLAG_BLIND]) /* player is blind */ 449 else if (observe->flag [FLAG_BLIND]) /* player is blind */
450 {
451 clear_los ();
452 blinded_sight (this); 452 blinded_sight (this);
453 }
453 else 454 else
454 { 455 {
456 clear_los ();
455 calculate_los (this); 457 calculate_los (this);
456 apply_lights (this); 458 apply_lights (this);
457 } 459 }
458 460
459 if (observe->flag [FLAG_XRAYS]) 461 if (observe->flag [FLAG_XRAYS])
460 for (int dx = -2; dx <= 2; dx++) 462 for (int dx = -2; dx <= 2; dx++)
461 for (int dy = -2; dy <= 2; dy++) 463 for (int dy = -2; dy <= 2; dy++)
462 min_it (los[dx + LOS_X0][dy + LOS_X0], 1); 464 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
463} 465}
464 466
465/* update all_map_los is like update_all_los below, 467/* update all_map_los is like update_all_los below,
466 * but updates everyone on the map, no matter where they 468 * but updates everyone on the map, no matter where they
467 * are. This generally should not be used, as a per 469 * are. This generally should not be used, as a per
618{ 620{
619 for_all_players (pl) 621 for_all_players (pl)
620 if (pl->ob->map == op->map && 622 if (pl->ob->map == op->map &&
621 pl->ob->y - pl->ns->mapy / 2 <= op->y && 623 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
622 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x) 624 pl->ob->y + pl->ns->mapy / 2 >= op->y && pl->ob->x - pl->ns->mapx / 2 <= op->x && pl->ob->x + pl->ns->mapx / 2 >= op->x)
623 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_X0] = 0; 625 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0;
624} 626}
625 627
626/* 628/*
627 * make_sure_not_seen: The object which is supposed to be visible through 629 * make_sure_not_seen: The object which is supposed to be visible through
628 * walls has just been removed from the map, so update the los of any 630 * walls has just been removed from the map, so update the los of any

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines