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.50 by root, Tue Dec 23 22:04:17 2008 UTC vs.
Revision 1.58 by root, Thu Jan 8 22:35:00 2009 UTC

22 */ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#include <cmath> 25#include <cmath>
26 26
27#define SEE_IN_DARK_RADIUS 2
28#define MAX_VISION 10 // maximum visible radius
29
27// los flags 30// los flags
28enum { 31enum {
29 FLG_XI = 0x01, // we have an x-parent 32 FLG_XI = 0x01, // we have an x-parent
30 FLG_YI = 0x02, // we have an y-parent 33 FLG_YI = 0x02, // we have an y-parent
31 FLG_BLOCKED = 0x04, // this space blocks the view 34 FLG_BLOCKED = 0x04, // this space blocks the view
99// still is basically the same algorithm. 102// still is basically the same algorithm.
100static void 103static void
101calculate_los (player *pl) 104calculate_los (player *pl)
102{ 105{
103 { 106 {
107 memset (los, 0, sizeof (los));
108
104 // we keep one line for ourselves, for the border flag 109 // we keep one line for ourselves, for the border flag
105 // so the client area is actually MAP_CLIENT_(X|Y) - 2 110 // so the client area is actually MAP_CLIENT_(X|Y) - 2
106 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2); 111 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
107 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2); 112 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
108 113
116 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED; 121 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
117 122
118 // now reset the los area and also add blocked flags 123 // now reset the los area and also add blocked flags
119 // which supposedly is faster than doing it inside the 124 // which supposedly is faster than doing it inside the
120 // spiral path algorithm below, except when very little 125 // spiral path algorithm below, except when very little
121 // area is visible, in which case it is slower, evening 126 // area is visible, in which case it is slower. which evens
122 // out los calculation times between large and small los maps. 127 // out los calculation times between large and small los maps.
123 // apply_lights also iterates over this area, maybe these 128 // apply_lights also iterates over this area, maybe these
124 // two passes could be combined somehow. 129 // two passes could be combined somehow.
125 rectangular_mapspace_iterate_begin (pl->observe, -half_x, half_x, -half_y, half_y) 130 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y)
131 {
126 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; 132 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
127 l.flags = m && m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; 133 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
128 rectangular_mapspace_iterate_end 134 }
129 } 135 }
130 136
131 q1 = 0; q2 = 0; // initialise queue, not strictly required 137 q1 = 0; q2 = 0; // initialise queue, not strictly required
132 enqueue (0, 0); // enqueue center 138 enqueue (0, 0); // enqueue center
133 139
263 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); 269 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
264 } 270 }
265 } 271 }
266} 272}
267 273
268/* returns true if op carries one or more lights
269 * This is a trivial function now days, but it used to
270 * be a bit longer. Probably better for callers to just
271 * check the op->glow_radius instead of calling this.
272 */
273int
274has_carried_lights (const object *op)
275{
276 /* op may glow! */
277 if (op->glow_radius > 0)
278 return 1;
279
280 return 0;
281}
282
283/* radius, distance => lightness adjust */ 274/* radius, distance => lightness adjust */
284static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; 275static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
285static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; 276static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
286 277
287static struct los_init 278static struct los_init
288{ 279{
289 los_init () 280 los_init ()
290 { 281 {
305 ? min (3, intensity) 296 ? min (3, intensity)
306 : LOS_MAX - intensity; 297 : LOS_MAX - intensity;
307 } 298 }
308 299
309 /* for general vision */ 300 /* for general vision */
310 for (int radius = 0; radius <= MAX_DARKNESS; ++radius) 301 for (int radius = 0; radius <= MAX_VISION; ++radius)
311 for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) 302 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
312 { 303 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
313 vision_atten [radius][distance] = distance <= radius ? 3 : 4;
314 }
315 } 304 }
316} los_init; 305} los_init;
317 306
318sint8 307sint8
319los_brighten (sint8 b, sint8 l) 308los_brighten (sint8 b, sint8 l)
356apply_lights (player *pl) 345apply_lights (player *pl)
357{ 346{
358 object *op = pl->observe; 347 object *op = pl->observe;
359 int darklevel = op->map->darklevel (); 348 int darklevel = op->map->darklevel ();
360 349
361 /* If the player can see in the dark, lower the darklevel for him */
362 if (op->flag [FLAG_SEE_IN_DARK])
363 darklevel = max (0, darklevel - 2);
364
365 int half_x = pl->ns->mapx / 2; 350 int half_x = pl->ns->mapx / 2;
366 int half_y = pl->ns->mapy / 2; 351 int half_y = pl->ns->mapy / 2;
367 352
368 int pass2 = 0; // negative lights have an extra pass 353 int pass2 = 0; // negative lights have an extra pass
354
355 maprect *rects = pl->observe->map->split_to_tiles (
356 pl->observe->x - half_x - MAX_LIGHT_RADIUS,
357 pl->observe->y - half_y - MAX_LIGHT_RADIUS,
358 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1,
359 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1
360 );
361
362 /* If the player can see in the dark, increase light/vision radius */
363 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
369 364
370 if (!darklevel) 365 if (!darklevel)
371 pass2 = 1; 366 pass2 = 1;
372 else 367 else
373 { 368 {
380 * Only process the area of interest. 375 * Only process the area of interest.
381 * the basex, basey values represent the position in the op->contr->los 376 * the basex, basey values represent the position in the op->contr->los
382 * array. Its easier to just increment them here (and start with the right 377 * array. Its easier to just increment them here (and start with the right
383 * value) than to recalculate them down below. 378 * value) than to recalculate them down below.
384 */ 379 */
385 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) 380 for (maprect *r = rects; r->m; ++r)
386 if (m) 381 rect_mapwalk (r, 0, 0)
387 { 382 {
388 mapspace &ms = m->at (nx, ny); 383 mapspace &ms = m->at (nx, ny);
389 ms.update (); 384 ms.update ();
390 sint8 light = ms.light; 385 sint8 light = ms.light;
391 386
392 if (expect_false (light)) 387 if (expect_false (light))
393 if (light < 0) 388 if (light < 0)
394 pass2 = 1; 389 pass2 = 1;
395 else 390 else
391 {
392 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
396 apply_light<los_brighten> (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]); 393 apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
394 }
397 } 395 }
398 rectangular_mapspace_iterate_end
399 396
400 /* grant some vision to the player, based on the darklevel */ 397 /* grant some vision to the player, based on outside, outdoor, and darklevel */
401 { 398 {
402 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 399 int light;
400
401 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
402 light = MAX_DARKNESS - op->map->darkness;
403 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
404 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
405 else // outdoor and darkness <= 0 => start wide and decrease quickly
406 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
407
408 light = clamp (light, 0, MAX_VISION);
403 409
404 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 410 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
405 } 411 }
406 } 412 }
407 413
408 // possibly do 2nd pass for rare negative glow radii 414 // possibly do 2nd pass for rare negative glow radii
409 // for effect, those are always considered to be stronger than anything else 415 // for effect, those are always considered to be stronger than anything else
410 // but they can't darken a place completely 416 // but they can't darken a place completely
411 if (pass2) 417 if (pass2)
412 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) 418 for (maprect *r = rects; r->m; ++r)
413 if (m) 419 rect_mapwalk (r, 0, 0)
414 { 420 {
415 mapspace &ms = m->at (nx, ny); 421 mapspace &ms = m->at (nx, ny);
416 ms.update (); 422 ms.update ();
417 sint8 light = ms.light; 423 sint8 light = ms.light;
418 424
419 if (expect_false (light < 0)) 425 if (expect_false (light < 0))
426 {
427 light = clamp (light - bonus, 0, MAX_DARKNESS);
420 apply_light<los_darken> (pl, dx, dy, -light, light_atten [light + MAX_LIGHT_RADIUS]); 428 apply_light<los_darken> (pl, dx - pl->observe->x, dy - pl->observe->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
429 }
421 } 430 }
422 rectangular_mapspace_iterate_end
423} 431}
424 432
425/* blinded_sight() - sets all viewable squares to blocked except 433/* blinded_sight() - sets all viewable squares to blocked except
426 * for the one the central one that the player occupies. A little 434 * for the one the central one that the player occupies. A little
427 * odd that you can see yourself (and what your standing on), but 435 * odd that you can see yourself (and what your standing on), but
494 * map is the map that changed, x and y are the coordinates. 502 * map is the map that changed, x and y are the coordinates.
495 */ 503 */
496void 504void
497update_all_los (const maptile *map, int x, int y) 505update_all_los (const maptile *map, int x, int y)
498{ 506{
507 // no need to do anything if we don't have darkness
508 if (map->darklevel () <= 0)
509 return;
510
499 map->at (x, y).invalidate (); 511 map->at (x, y).invalidate ();
500 512
501 for_all_players (pl) 513 for_all_players (pl)
502 { 514 {
503 /* Player should not have a null map, but do this 515 /* Player should not have a null map, but do this
514 * player can't be on another map that may be closer, 526 * player can't be on another map that may be closer,
515 * so by setting it up this way, we trim processing 527 * so by setting it up this way, we trim processing
516 * some. 528 * some.
517 */ 529 */
518 if (pl->ob->map == map) 530 if (pl->ob->map == map)
519 {
520 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) 531 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
521 pl->do_los = 1; 532 pl->do_los = 1;
522 }
523 533
524 /* Now we check to see if player is on adjacent 534 /* Now we check to see if player is on adjacent
525 * maps to the one that changed and also within 535 * maps to the one that changed and also within
526 * view. The tile_maps[] could be null, but in that 536 * view. The tile_maps[] could be null, but in that
527 * case it should never match the pl->ob->map, so 537 * case it should never match the pl->ob->map, so
636 if (pl->ob->map == op->map && 646 if (pl->ob->map == op->map &&
637 pl->ob->y - pl->ns->mapy / 2 <= op->y && 647 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
638 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) 648 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)
639 pl->do_los = 1; 649 pl->do_los = 1;
640} 650}
651

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines