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.52 by root, Fri Dec 26 10:36:42 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 3 27#define SEE_IN_DARK_RADIUS 2
28#define MAX_VISION 10 // maximum visible radius
28 29
29// los flags 30// los flags
30enum { 31enum {
31 FLG_XI = 0x01, // we have an x-parent 32 FLG_XI = 0x01, // we have an x-parent
32 FLG_YI = 0x02, // we have an y-parent 33 FLG_YI = 0x02, // we have an y-parent
120 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;
121 122
122 // now reset the los area and also add blocked flags 123 // now reset the los area and also add blocked flags
123 // which supposedly is faster than doing it inside the 124 // which supposedly is faster than doing it inside the
124 // spiral path algorithm below, except when very little 125 // spiral path algorithm below, except when very little
125 // area is visible, in which case it is slower, evening 126 // area is visible, in which case it is slower. which evens
126 // out los calculation times between large and small los maps. 127 // out los calculation times between large and small los maps.
127 // apply_lights also iterates over this area, maybe these 128 // apply_lights also iterates over this area, maybe these
128 // two passes could be combined somehow. 129 // two passes could be combined somehow.
129 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y) 130 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y)
130 { 131 {
268 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); 269 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
269 } 270 }
270 } 271 }
271} 272}
272 273
273/* returns true if op carries one or more lights
274 * This is a trivial function now days, but it used to
275 * be a bit longer. Probably better for callers to just
276 * check the op->glow_radius instead of calling this.
277 */
278int
279has_carried_lights (const object *op)
280{
281 /* op may glow! */
282 if (op->glow_radius > 0)
283 return 1;
284
285 return 0;
286}
287
288/* radius, distance => lightness adjust */ 274/* radius, distance => lightness adjust */
289static 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];
290static sint8 vision_atten[MAX_DARKNESS + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1]; 276static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
291 277
292static struct los_init 278static struct los_init
293{ 279{
294 los_init () 280 los_init ()
295 { 281 {
310 ? min (3, intensity) 296 ? min (3, intensity)
311 : LOS_MAX - intensity; 297 : LOS_MAX - intensity;
312 } 298 }
313 299
314 /* for general vision */ 300 /* for general vision */
315 for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) 301 for (int radius = 0; radius <= MAX_VISION; ++radius)
316 for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2; ++distance) 302 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
317 vision_atten [radius][distance] = distance <= radius ? 3 : 4; 303 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
318 } 304 }
319} los_init; 305} los_init;
320 306
321sint8 307sint8
322los_brighten (sint8 b, sint8 l) 308los_brighten (sint8 b, sint8 l)
370 pl->observe->x - half_x - MAX_LIGHT_RADIUS, 356 pl->observe->x - half_x - MAX_LIGHT_RADIUS,
371 pl->observe->y - half_y - MAX_LIGHT_RADIUS, 357 pl->observe->y - half_y - MAX_LIGHT_RADIUS,
372 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, 358 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1,
373 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 359 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1
374 ); 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;
375 364
376 if (!darklevel) 365 if (!darklevel)
377 pass2 = 1; 366 pass2 = 1;
378 else 367 else
379 { 368 {
397 386
398 if (expect_false (light)) 387 if (expect_false (light))
399 if (light < 0) 388 if (light < 0)
400 pass2 = 1; 389 pass2 = 1;
401 else 390 else
391 {
392 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
402 apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, 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 }
403 } 395 }
404 396
405 /* grant some vision to the player, based on the darklevel */ 397 /* grant some vision to the player, based on outside, outdoor, and darklevel */
406 { 398 {
407 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 399 int light;
408 400
409 /* If the player can see in the dark, lower the darklevel for him */ 401 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
410 if (op->flag [FLAG_SEE_IN_DARK]) 402 light = MAX_DARKNESS - op->map->darkness;
411 light += SEE_IN_DARK_RADIUS; 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);
412 409
413 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 410 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
414 } 411 }
415 } 412 }
416 413
424 mapspace &ms = m->at (nx, ny); 421 mapspace &ms = m->at (nx, ny);
425 ms.update (); 422 ms.update ();
426 sint8 light = ms.light; 423 sint8 light = ms.light;
427 424
428 if (expect_false (light < 0)) 425 if (expect_false (light < 0))
426 {
427 light = clamp (light - bonus, 0, MAX_DARKNESS);
429 apply_light<los_darken> (pl, dx - pl->observe->x, dy - pl->observe->y, -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 }
430 } 430 }
431} 431}
432 432
433/* blinded_sight() - sets all viewable squares to blocked except 433/* blinded_sight() - sets all viewable squares to blocked except
434 * 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
502 * 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.
503 */ 503 */
504void 504void
505update_all_los (const maptile *map, int x, int y) 505update_all_los (const maptile *map, int x, int y)
506{ 506{
507 // no need to do anything if we don't have darkness
508 if (map->darklevel () <= 0)
509 return;
510
507 map->at (x, y).invalidate (); 511 map->at (x, y).invalidate ();
508 512
509 for_all_players (pl) 513 for_all_players (pl)
510 { 514 {
511 /* Player should not have a null map, but do this 515 /* Player should not have a null map, but do this
522 * player can't be on another map that may be closer, 526 * player can't be on another map that may be closer,
523 * so by setting it up this way, we trim processing 527 * so by setting it up this way, we trim processing
524 * some. 528 * some.
525 */ 529 */
526 if (pl->ob->map == map) 530 if (pl->ob->map == map)
527 {
528 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))
529 pl->do_los = 1; 532 pl->do_los = 1;
530 }
531 533
532 /* Now we check to see if player is on adjacent 534 /* Now we check to see if player is on adjacent
533 * maps to the one that changed and also within 535 * maps to the one that changed and also within
534 * view. The tile_maps[] could be null, but in that 536 * view. The tile_maps[] could be null, but in that
535 * case it should never match the pl->ob->map, so 537 * case it should never match the pl->ob->map, so

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines