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.55 by root, Tue Jan 6 19:17:06 2009 UTC vs.
Revision 1.61 by root, Tue May 5 04:51:56 2009 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
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
270 } 271 }
271} 272}
272 273
273/* radius, distance => lightness adjust */ 274/* radius, distance => lightness adjust */
274static 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];
275static 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];
276 277
277static struct los_init 278static struct los_init
278{ 279{
279 los_init () 280 los_init ()
280 { 281 {
287 { 288 {
288 // max intensity 289 // max intensity
289 int intensity = min (LOS_MAX, abs (radius) + 1); 290 int intensity = min (LOS_MAX, abs (radius) + 1);
290 291
291 // actual intensity 292 // actual intensity
292 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); 293 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
293 294
294 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 295 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
295 ? min (3, intensity) 296 ? min (3, intensity)
296 : LOS_MAX - intensity; 297 : LOS_MAX - intensity;
297 } 298 }
298 299
299 /* for general vision */ 300 /* for general vision */
300 for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) 301 for (int radius = 0; radius <= MAX_VISION; ++radius)
301 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)
302 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;
303 } 304 }
304} los_init; 305} los_init;
305 306
306sint8 307sint8
307los_brighten (sint8 b, sint8 l) 308los_brighten (sint8 b, sint8 l)
355 pl->observe->x - half_x - MAX_LIGHT_RADIUS, 356 pl->observe->x - half_x - MAX_LIGHT_RADIUS,
356 pl->observe->y - half_y - MAX_LIGHT_RADIUS, 357 pl->observe->y - half_y - MAX_LIGHT_RADIUS,
357 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, 358 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1,
358 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 359 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1
359 ); 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;
360 364
361 if (!darklevel) 365 if (!darklevel)
362 pass2 = 1; 366 pass2 = 1;
363 else 367 else
364 { 368 {
382 386
383 if (expect_false (light)) 387 if (expect_false (light))
384 if (light < 0) 388 if (light < 0)
385 pass2 = 1; 389 pass2 = 1;
386 else 390 else
391 {
392 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
387 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 }
388 } 395 }
389 396
390 /* grant some vision to the player, based on the darklevel */ 397 /* grant some vision to the player, based on outside, outdoor, and darklevel */
391 { 398 {
392 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 399 int light;
393 400
394 /* If the player can see in the dark, lower the darklevel for him */ 401 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
395 if (op->flag [FLAG_SEE_IN_DARK]) 402 light = MAX_DARKNESS - op->map->darkness;
396 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 + bonus, 0, MAX_VISION);
397 409
398 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 410 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
399 } 411 }
400 } 412 }
401 413
409 mapspace &ms = m->at (nx, ny); 421 mapspace &ms = m->at (nx, ny);
410 ms.update (); 422 ms.update ();
411 sint8 light = ms.light; 423 sint8 light = ms.light;
412 424
413 if (expect_false (light < 0)) 425 if (expect_false (light < 0))
426 {
427 light = clamp (light - bonus, 0, MAX_DARKNESS);
414 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 }
415 } 430 }
416} 431}
417 432
418/* blinded_sight() - sets all viewable squares to blocked except 433/* blinded_sight() - sets all viewable squares to blocked except
419 * 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
487 * 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.
488 */ 503 */
489void 504void
490update_all_los (const maptile *map, int x, int y) 505update_all_los (const maptile *map, int x, int y)
491{ 506{
492 // no need to do anything if we don't have darkness
493 if (map->darklevel () <= 0)
494 return;
495
496 map->at (x, y).invalidate (); 507 map->at (x, y).invalidate ();
497 508
498 for_all_players (pl) 509 for_all_players (pl)
499 { 510 {
500 /* Player should not have a null map, but do this 511 /* Player should not have a null map, but do this
501 * check as a safety 512 * check as a safety
502 */ 513 */
503 if (!pl->ob || !pl->ob->map || !pl->ns) 514 if (!pl->ob || !pl->ob->map || !pl->ns)
504 continue; 515 continue;
505 516
506 /* Same map is simple case - see if pl is close enough. 517 rv_vector rv;
507 * Note in all cases, we did the check for same map first, 518
508 * and then see if the player is close enough and update 519 get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv);
509 * los if that is the case. If the player is on the
510 * corresponding map, but not close enough, then the
511 * player can't be on another map that may be closer,
512 * so by setting it up this way, we trim processing
513 * some.
514 */ 520
515 if (pl->ob->map == map) 521 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
516 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
517 pl->do_los = 1; 522 pl->do_los = 1;
518
519 /* Now we check to see if player is on adjacent
520 * maps to the one that changed and also within
521 * view. The tile_maps[] could be null, but in that
522 * case it should never match the pl->ob->map, so
523 * we want ever try to dereference any of the data in it.
524 *
525 * The logic for 0 and 3 is to see how far the player is
526 * from the edge of the map (height/width) - pl->ob->(x,y)
527 * and to add current position on this map - that gives a
528 * distance.
529 * For 1 and 2, we check to see how far the given
530 * coordinate (x,y) is from the corresponding edge,
531 * and then add the players location, which gives
532 * a distance.
533 */
534 else if (pl->ob->map == map->tile_map[0])
535 {
536 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2))
537 pl->do_los = 1;
538 }
539 else if (pl->ob->map == map->tile_map[2])
540 {
541 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2))
542 pl->do_los = 1;
543 }
544 else if (pl->ob->map == map->tile_map[1])
545 {
546 if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
547 pl->do_los = 1;
548 }
549 else if (pl->ob->map == map->tile_map[3])
550 {
551 if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
552 pl->do_los = 1;
553 }
554 } 523 }
555} 524}
556 525
557static const int season_darkness[5][HOURS_PER_DAY] = { 526static const int season_darkness[5][HOURS_PER_DAY] = {
558 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 */ 527 /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines