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.66 by root, Fri Nov 6 13:03:34 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
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 5 *
8 * Deliantra is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 9 * option) any later version.
12 * 10 *
13 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 14 * GNU General Public License for more details.
17 * 15 *
18 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * 19 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 20 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 21 */
23 22
24#include <global.h> 23#include <global.h>
25#include <cmath> 24#include <cmath>
26 25
27#define SEE_IN_DARK_RADIUS 3 26#define SEE_IN_DARK_RADIUS 2
27#define MAX_VISION 10 // maximum visible radius
28 28
29// los flags 29// los flags
30enum { 30enum {
31 FLG_XI = 0x01, // we have an x-parent 31 FLG_XI = 0x01, // we have an x-parent
32 FLG_YI = 0x02, // we have an y-parent 32 FLG_YI = 0x02, // we have an y-parent
120 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED; 120 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
121 121
122 // now reset the los area and also add blocked flags 122 // now reset the los area and also add blocked flags
123 // which supposedly is faster than doing it inside the 123 // which supposedly is faster than doing it inside the
124 // spiral path algorithm below, except when very little 124 // spiral path algorithm below, except when very little
125 // area is visible, in which case it is slower, evening 125 // area is visible, in which case it is slower. which evens
126 // out los calculation times between large and small los maps. 126 // out los calculation times between large and small los maps.
127 // apply_lights also iterates over this area, maybe these 127 // apply_lights also iterates over this area, maybe these
128 // two passes could be combined somehow. 128 // two passes could be combined somehow.
129 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y) 129 unordered_mapwalk (pl->viewpoint, -half_x, -half_y, half_x, half_y)
130 { 130 {
131 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; 131 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
132 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; 132 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
133 } 133 }
134 } 134 }
268 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); 268 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
269 } 269 }
270 } 270 }
271} 271}
272 272
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 */ 273/* radius, distance => lightness adjust */
289static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; 274static 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]; 275static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
291 276
292static struct los_init 277static struct los_init
293{ 278{
294 los_init () 279 los_init ()
295 { 280 {
302 { 287 {
303 // max intensity 288 // max intensity
304 int intensity = min (LOS_MAX, abs (radius) + 1); 289 int intensity = min (LOS_MAX, abs (radius) + 1);
305 290
306 // actual intensity 291 // actual intensity
307 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); 292 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
308 293
309 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 294 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
310 ? min (3, intensity) 295 ? min (3, intensity)
311 : LOS_MAX - intensity; 296 : LOS_MAX - intensity;
312 } 297 }
313 298
314 /* for general vision */ 299 /* for general vision */
315 for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) 300 for (int radius = 0; radius <= MAX_VISION; ++radius)
316 for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2; ++distance) 301 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
317 vision_atten [radius][distance] = distance <= radius ? 3 : 4; 302 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
318 } 303 }
319} los_init; 304} los_init;
320 305
306// the following functions cannot be static, due to c++ stupidity :/
307namespace {
308 // brighten area, ignore los
321sint8 309 sint8
310 los_brighten_nolos (sint8 b, sint8 l)
311 {
312 return min (b, l);
313 }
314
315 // brighten area, but respect los
316 sint8
322los_brighten (sint8 b, sint8 l) 317 los_brighten (sint8 b, sint8 l)
323{ 318 {
324 return b == LOS_BLOCKED ? b : min (b, l); 319 return b == LOS_BLOCKED ? b : min (b, l);
325} 320 }
326 321
322 // darken area, respect los
327sint8 323 sint8
328los_darken (sint8 b, sint8 l) 324 los_darken (sint8 b, sint8 l)
329{ 325 {
330 return max (b, l); 326 return max (b, l);
331} 327 }
328};
332 329
333template<sint8 change_it (sint8, sint8)> 330template<sint8 change_it (sint8, sint8)>
334static void 331static void
335apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) 332apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table)
336{ 333{
356 * mark those squares specially. 353 * mark those squares specially.
357 */ 354 */
358static void 355static void
359apply_lights (player *pl) 356apply_lights (player *pl)
360{ 357{
361 object *op = pl->observe; 358 object *op = pl->viewpoint;
362 int darklevel = op->map->darklevel (); 359 int darklevel = op->map->darklevel ();
363 360
364 int half_x = pl->ns->mapx / 2; 361 int half_x = pl->ns->mapx / 2;
365 int half_y = pl->ns->mapy / 2; 362 int half_y = pl->ns->mapy / 2;
366 363
367 int pass2 = 0; // negative lights have an extra pass 364 int pass2 = 0; // negative lights have an extra pass
368 365
369 maprect *rects = pl->observe->map->split_to_tiles ( 366 maprect *rects = pl->viewpoint->map->split_to_tiles (
370 pl->observe->x - half_x - MAX_LIGHT_RADIUS, 367 pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS,
371 pl->observe->y - half_y - MAX_LIGHT_RADIUS, 368 pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS,
372 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, 369 pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1,
373 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 370 pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1
374 ); 371 );
372
373 /* If the player can see in the dark, increase light/vision radius */
374 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
375 375
376 if (!darklevel) 376 if (!darklevel)
377 pass2 = 1; 377 pass2 = 1;
378 else 378 else
379 { 379 {
397 397
398 if (expect_false (light)) 398 if (expect_false (light))
399 if (light < 0) 399 if (light < 0)
400 pass2 = 1; 400 pass2 = 1;
401 else 401 else
402 {
403 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]); 404 apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
405 }
403 } 406 }
404 407
405 /* grant some vision to the player, based on the darklevel */ 408 /* grant some vision to the player, based on outside, outdoor, and darklevel */
406 { 409 {
407 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 410 int light;
408 411
409 /* If the player can see in the dark, lower the darklevel for him */ 412 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
410 if (op->flag [FLAG_SEE_IN_DARK]) 413 light = MAX_DARKNESS - op->map->darkness;
411 light += SEE_IN_DARK_RADIUS; 414 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
415 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
416 else // outdoor and darkness <= 0 => start wide and decrease quickly
417 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
418
419 light = clamp (light + bonus, 0, MAX_VISION);
412 420
413 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 421 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
414 } 422 }
415 } 423 }
424
425 // when we fly high, we have some minimum viewable area around us, like x-ray
426 if (op->move_type & MOVE_FLY_HIGH)
427 apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]);
416 428
417 // possibly do 2nd pass for rare negative glow radii 429 // possibly do 2nd pass for rare negative glow radii
418 // for effect, those are always considered to be stronger than anything else 430 // for effect, those are always considered to be stronger than anything else
419 // but they can't darken a place completely 431 // but they can't darken a place completely
420 if (pass2) 432 if (pass2)
424 mapspace &ms = m->at (nx, ny); 436 mapspace &ms = m->at (nx, ny);
425 ms.update (); 437 ms.update ();
426 sint8 light = ms.light; 438 sint8 light = ms.light;
427 439
428 if (expect_false (light < 0)) 440 if (expect_false (light < 0))
441 {
442 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]); 443 apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
444 }
430 } 445 }
431} 446}
432 447
433/* blinded_sight() - sets all viewable squares to blocked except 448/* blinded_sight() - sets all viewable squares to blocked except
434 * for the one the central one that the player occupies. A little 449 * for the one the central one that the player occupies. A little
451 if (ob->flag [FLAG_REMOVED])//D really needed? 466 if (ob->flag [FLAG_REMOVED])//D really needed?
452 return; 467 return;
453 468
454 if (ob->flag [FLAG_WIZLOOK]) 469 if (ob->flag [FLAG_WIZLOOK])
455 clear_los (0); 470 clear_los (0);
456 else if (observe->flag [FLAG_BLIND]) /* player is blind */ 471 else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */
457 { 472 {
458 clear_los (); 473 clear_los ();
459 blinded_sight (this); 474 blinded_sight (this);
460 } 475 }
461 else 476 else
463 clear_los (); 478 clear_los ();
464 calculate_los (this); 479 calculate_los (this);
465 apply_lights (this); 480 apply_lights (this);
466 } 481 }
467 482
468 if (observe->flag [FLAG_XRAYS]) 483 if (viewpoint->flag [FLAG_XRAYS])
469 for (int dx = -2; dx <= 2; dx++) 484 for (int dx = -2; dx <= 2; dx++)
470 for (int dy = -2; dy <= 2; dy++) 485 for (int dy = -2; dy <= 2; dy++)
471 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); 486 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
472} 487}
473 488
512 * check as a safety 527 * check as a safety
513 */ 528 */
514 if (!pl->ob || !pl->ob->map || !pl->ns) 529 if (!pl->ob || !pl->ob->map || !pl->ns)
515 continue; 530 continue;
516 531
517 /* Same map is simple case - see if pl is close enough. 532 rv_vector rv;
518 * Note in all cases, we did the check for same map first, 533
519 * and then see if the player is close enough and update 534 get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv);
520 * los if that is the case. If the player is on the
521 * corresponding map, but not close enough, then the
522 * player can't be on another map that may be closer,
523 * so by setting it up this way, we trim processing
524 * some.
525 */ 535
526 if (pl->ob->map == map) 536 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
527 {
528 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
529 pl->do_los = 1; 537 pl->do_los = 1;
530 }
531
532 /* Now we check to see if player is on adjacent
533 * maps to the one that changed and also within
534 * view. The tile_maps[] could be null, but in that
535 * case it should never match the pl->ob->map, so
536 * we want ever try to dereference any of the data in it.
537 *
538 * The logic for 0 and 3 is to see how far the player is
539 * from the edge of the map (height/width) - pl->ob->(x,y)
540 * and to add current position on this map - that gives a
541 * distance.
542 * For 1 and 2, we check to see how far the given
543 * coordinate (x,y) is from the corresponding edge,
544 * and then add the players location, which gives
545 * a distance.
546 */
547 else if (pl->ob->map == map->tile_map[0])
548 {
549 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2))
550 pl->do_los = 1;
551 }
552 else if (pl->ob->map == map->tile_map[2])
553 {
554 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2))
555 pl->do_los = 1;
556 }
557 else if (pl->ob->map == map->tile_map[1])
558 {
559 if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
560 pl->do_los = 1;
561 }
562 else if (pl->ob->map == map->tile_map[3])
563 {
564 if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
565 pl->do_los = 1;
566 }
567 } 538 }
568} 539}
569 540
570static const int season_darkness[5][HOURS_PER_DAY] = { 541static const int season_darkness[5][HOURS_PER_DAY] = {
571 /*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 */ 542 /*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