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.51 by root, Wed Dec 24 01:37:23 2008 UTC vs.
Revision 1.67 by root, Sun Nov 29 17:41:07 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
33 FLG_BLOCKED = 0x04, // this space blocks the view 33 FLG_BLOCKED = 0x04, // this space blocks the view
34 FLG_QUEUED = 0x80 // already queued in queue, or border 34 FLG_QUEUED = 0x80 // already queued in queue, or border
35}; 35};
36 36
37// it is important for performance reasons that this structure
38// has a size easily computable by the cpu (*8 is perfect).
39// it is possible to move culled and visible into flags, at
40// some speed loss.
37struct los_info 41struct los_info
38{ 42{
39 uint8 flags; // FLG_xxx 43 uint8 flags; // FLG_xxx
40 uint8 culled; // culled from "tree" 44 uint8 culled; // culled from "tree"
41 uint8 visible; 45 uint8 visible;
81 85
82 los_info &l = los[x][y]; 86 los_info &l = los[x][y];
83 87
84 l.flags |= flags; 88 l.flags |= flags;
85 89
86 if (l.flags & FLG_QUEUED) 90 if (expect_false (l.flags & FLG_QUEUED))
87 return; 91 return;
88 92
89 l.flags |= FLG_QUEUED; 93 l.flags |= FLG_QUEUED;
90 94
91 queue[q1].x = dx; 95 queue[q1].x = dx;
101// still is basically the same algorithm. 105// still is basically the same algorithm.
102static void 106static void
103calculate_los (player *pl) 107calculate_los (player *pl)
104{ 108{
105 { 109 {
110 memset (los, 0, sizeof (los));
111
106 // we keep one line for ourselves, for the border flag 112 // we keep one line for ourselves, for the border flag
107 // so the client area is actually MAP_CLIENT_(X|Y) - 2 113 // so the client area is actually MAP_CLIENT_(X|Y) - 2
108 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2); 114 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
109 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2); 115 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
110 116
118 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED; 124 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
119 125
120 // now reset the los area and also add blocked flags 126 // now reset the los area and also add blocked flags
121 // which supposedly is faster than doing it inside the 127 // which supposedly is faster than doing it inside the
122 // spiral path algorithm below, except when very little 128 // spiral path algorithm below, except when very little
123 // area is visible, in which case it is slower, evening 129 // area is visible, in which case it is slower. which evens
124 // out los calculation times between large and small los maps. 130 // out los calculation times between large and small los maps.
125 // apply_lights also iterates over this area, maybe these 131 // apply_lights also iterates over this area, maybe these
126 // two passes could be combined somehow. 132 // two passes could be combined somehow.
127 rectangular_mapspace_iterate_begin (pl->observe, -half_x, half_x, -half_y, half_y) 133 unordered_mapwalk (pl->viewpoint, -half_x, -half_y, half_x, half_y)
134 {
128 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; 135 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
129 l.flags = m && m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; 136 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
130 rectangular_mapspace_iterate_end 137 }
131 } 138 }
132 139
133 q1 = 0; q2 = 0; // initialise queue, not strictly required 140 q1 = 0; q2 = 0; // initialise queue, not strictly required
134 enqueue (0, 0); // enqueue center 141 enqueue (0, 0); // enqueue center
135 142
265 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); 272 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
266 } 273 }
267 } 274 }
268} 275}
269 276
270/* returns true if op carries one or more lights
271 * This is a trivial function now days, but it used to
272 * be a bit longer. Probably better for callers to just
273 * check the op->glow_radius instead of calling this.
274 */
275int
276has_carried_lights (const object *op)
277{
278 /* op may glow! */
279 if (op->glow_radius > 0)
280 return 1;
281
282 return 0;
283}
284
285/* radius, distance => lightness adjust */ 277/* radius, distance => lightness adjust */
286static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; 278static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
287static sint8 vision_atten[MAX_DARKNESS + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1]; 279static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
288 280
289static struct los_init 281static struct los_init
290{ 282{
291 los_init () 283 los_init ()
292 { 284 {
299 { 291 {
300 // max intensity 292 // max intensity
301 int intensity = min (LOS_MAX, abs (radius) + 1); 293 int intensity = min (LOS_MAX, abs (radius) + 1);
302 294
303 // actual intensity 295 // actual intensity
304 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); 296 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
305 297
306 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 298 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
307 ? min (3, intensity) 299 ? min (3, intensity)
308 : LOS_MAX - intensity; 300 : LOS_MAX - intensity;
309 } 301 }
310 302
311 /* for general vision */ 303 /* for general vision */
312 for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) 304 for (int radius = 0; radius <= MAX_VISION; ++radius)
313 for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2; ++distance) 305 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
314 vision_atten [radius][distance] = distance <= radius ? 3 : 4; 306 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
315 } 307 }
316} los_init; 308} los_init;
317 309
310// the following functions cannot be static, due to c++ stupidity :/
311namespace {
312 // brighten area, ignore los
318sint8 313 sint8
314 los_brighten_nolos (sint8 b, sint8 l)
315 {
316 return min (b, l);
317 }
318
319 // brighten area, but respect los
320 sint8
319los_brighten (sint8 b, sint8 l) 321 los_brighten (sint8 b, sint8 l)
320{ 322 {
321 return b == LOS_BLOCKED ? b : min (b, l); 323 return b == LOS_BLOCKED ? b : min (b, l);
322} 324 }
323 325
326 // darken area, respect los
324sint8 327 sint8
325los_darken (sint8 b, sint8 l) 328 los_darken (sint8 b, sint8 l)
326{ 329 {
327 return max (b, l); 330 return max (b, l);
328} 331 }
332};
329 333
330template<sint8 change_it (sint8, sint8)> 334template<sint8 change_it (sint8, sint8)>
331static void 335static void
332apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) 336apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table)
333{ 337{
353 * mark those squares specially. 357 * mark those squares specially.
354 */ 358 */
355static void 359static void
356apply_lights (player *pl) 360apply_lights (player *pl)
357{ 361{
358 object *op = pl->observe; 362 object *op = pl->viewpoint;
359 int darklevel = op->map->darklevel (); 363 int darklevel = op->map->darklevel ();
360 364
361 int half_x = pl->ns->mapx / 2; 365 int half_x = pl->ns->mapx / 2;
362 int half_y = pl->ns->mapy / 2; 366 int half_y = pl->ns->mapy / 2;
363 367
364 int pass2 = 0; // negative lights have an extra pass 368 int pass2 = 0; // negative lights have an extra pass
369
370 maprect *rects = pl->viewpoint->map->split_to_tiles (
371 pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS,
372 pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS,
373 pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1,
374 pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1
375 );
376
377 /* If the player can see in the dark, increase light/vision radius */
378 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
365 379
366 if (!darklevel) 380 if (!darklevel)
367 pass2 = 1; 381 pass2 = 1;
368 else 382 else
369 { 383 {
376 * Only process the area of interest. 390 * Only process the area of interest.
377 * the basex, basey values represent the position in the op->contr->los 391 * the basex, basey values represent the position in the op->contr->los
378 * array. Its easier to just increment them here (and start with the right 392 * array. Its easier to just increment them here (and start with the right
379 * value) than to recalculate them down below. 393 * value) than to recalculate them down below.
380 */ 394 */
381 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) 395 for (maprect *r = rects; r->m; ++r)
382 if (m) 396 rect_mapwalk (r, 0, 0)
383 { 397 {
384 mapspace &ms = m->at (nx, ny); 398 mapspace &ms = m->at (nx, ny);
385 ms.update (); 399 ms.update ();
386 sint8 light = ms.light; 400 sint8 light = ms.light;
387 401
388 if (expect_false (light)) 402 if (expect_false (light))
389 if (light < 0) 403 if (light < 0)
390 pass2 = 1; 404 pass2 = 1;
391 else 405 else
406 {
407 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
392 apply_light<los_brighten> (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]); 408 apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
409 }
393 } 410 }
394 rectangular_mapspace_iterate_end
395 411
396 /* grant some vision to the player, based on the darklevel */ 412 /* grant some vision to the player, based on outside, outdoor, and darklevel */
397 { 413 {
398 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 414 int light;
399 415
400 /* If the player can see in the dark, lower the darklevel for him */ 416 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
401 if (op->flag [FLAG_SEE_IN_DARK]) 417 light = MAX_DARKNESS - op->map->darkness;
402 light += SEE_IN_DARK_RADIUS; 418 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
419 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
420 else // outdoor and darkness <= 0 => start wide and decrease quickly
421 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
422
423 light = clamp (light + bonus, 0, MAX_VISION);
403 424
404 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 425 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
405 } 426 }
406 } 427 }
428
429 // when we fly high, we have some minimum viewable area around us, like x-ray
430 if (op->move_type & MOVE_FLY_HIGH)
431 apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]);
407 432
408 // possibly do 2nd pass for rare negative glow radii 433 // possibly do 2nd pass for rare negative glow radii
409 // for effect, those are always considered to be stronger than anything else 434 // for effect, those are always considered to be stronger than anything else
410 // but they can't darken a place completely 435 // but they can't darken a place completely
411 if (pass2) 436 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) 437 for (maprect *r = rects; r->m; ++r)
413 if (m) 438 rect_mapwalk (r, 0, 0)
414 { 439 {
415 mapspace &ms = m->at (nx, ny); 440 mapspace &ms = m->at (nx, ny);
416 ms.update (); 441 ms.update ();
417 sint8 light = ms.light; 442 sint8 light = ms.light;
418 443
419 if (expect_false (light < 0)) 444 if (expect_false (light < 0))
445 {
446 light = clamp (light - bonus, 0, MAX_DARKNESS);
420 apply_light<los_darken> (pl, dx, dy, -light, light_atten [light + MAX_LIGHT_RADIUS]); 447 apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
448 }
421 } 449 }
422 rectangular_mapspace_iterate_end
423} 450}
424 451
425/* blinded_sight() - sets all viewable squares to blocked except 452/* blinded_sight() - sets all viewable squares to blocked except
426 * for the one the central one that the player occupies. A little 453 * 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 454 * odd that you can see yourself (and what your standing on), but
443 if (ob->flag [FLAG_REMOVED])//D really needed? 470 if (ob->flag [FLAG_REMOVED])//D really needed?
444 return; 471 return;
445 472
446 if (ob->flag [FLAG_WIZLOOK]) 473 if (ob->flag [FLAG_WIZLOOK])
447 clear_los (0); 474 clear_los (0);
448 else if (observe->flag [FLAG_BLIND]) /* player is blind */ 475 else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */
449 { 476 {
450 clear_los (); 477 clear_los ();
451 blinded_sight (this); 478 blinded_sight (this);
452 } 479 }
453 else 480 else
455 clear_los (); 482 clear_los ();
456 calculate_los (this); 483 calculate_los (this);
457 apply_lights (this); 484 apply_lights (this);
458 } 485 }
459 486
460 if (observe->flag [FLAG_XRAYS]) 487 if (viewpoint->flag [FLAG_XRAYS])
461 for (int dx = -2; dx <= 2; dx++) 488 for (int dx = -2; dx <= 2; dx++)
462 for (int dy = -2; dy <= 2; dy++) 489 for (int dy = -2; dy <= 2; dy++)
463 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); 490 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
464} 491}
465 492
504 * check as a safety 531 * check as a safety
505 */ 532 */
506 if (!pl->ob || !pl->ob->map || !pl->ns) 533 if (!pl->ob || !pl->ob->map || !pl->ns)
507 continue; 534 continue;
508 535
509 /* Same map is simple case - see if pl is close enough. 536 rv_vector rv;
510 * Note in all cases, we did the check for same map first, 537
511 * and then see if the player is close enough and update 538 get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv);
512 * los if that is the case. If the player is on the
513 * corresponding map, but not close enough, then the
514 * player can't be on another map that may be closer,
515 * so by setting it up this way, we trim processing
516 * some.
517 */ 539
518 if (pl->ob->map == map) 540 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
519 {
520 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
521 pl->do_los = 1; 541 pl->do_los = 1;
522 }
523
524 /* Now we check to see if player is on adjacent
525 * maps to the one that changed and also within
526 * view. The tile_maps[] could be null, but in that
527 * case it should never match the pl->ob->map, so
528 * we want ever try to dereference any of the data in it.
529 *
530 * The logic for 0 and 3 is to see how far the player is
531 * from the edge of the map (height/width) - pl->ob->(x,y)
532 * and to add current position on this map - that gives a
533 * distance.
534 * For 1 and 2, we check to see how far the given
535 * coordinate (x,y) is from the corresponding edge,
536 * and then add the players location, which gives
537 * a distance.
538 */
539 else if (pl->ob->map == map->tile_map[0])
540 {
541 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2))
542 pl->do_los = 1;
543 }
544 else if (pl->ob->map == map->tile_map[2])
545 {
546 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2))
547 pl->do_los = 1;
548 }
549 else if (pl->ob->map == map->tile_map[1])
550 {
551 if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
552 pl->do_los = 1;
553 }
554 else if (pl->ob->map == map->tile_map[3])
555 {
556 if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
557 pl->do_los = 1;
558 }
559 } 542 }
560} 543}
561 544
562static const int season_darkness[5][HOURS_PER_DAY] = { 545static const int season_darkness[5][HOURS_PER_DAY] = {
563 /*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 */ 546 /*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 */
636 if (pl->ob->map == op->map && 619 if (pl->ob->map == op->map &&
637 pl->ob->y - pl->ns->mapy / 2 <= op->y && 620 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) 621 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; 622 pl->do_los = 1;
640} 623}
624

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines