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.62 by root, Mon Oct 12 14:00:57 2009 UTC vs.
Revision 1.73 by root, Mon Oct 29 23:55:52 2012 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,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 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 it under 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the 7 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your 8 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see 17 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 18 * <http://www.gnu.org/licenses/>.
21 * 19 *
22 * 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>
23 */ 21 */
24 22
25#include <global.h> 23#include <global.h>
26#include <cmath> 24#include <cmath>
34 FLG_YI = 0x02, // we have an y-parent 32 FLG_YI = 0x02, // we have an y-parent
35 FLG_BLOCKED = 0x04, // this space blocks the view 33 FLG_BLOCKED = 0x04, // this space blocks the view
36 FLG_QUEUED = 0x80 // already queued in queue, or border 34 FLG_QUEUED = 0x80 // already queued in queue, or border
37}; 35};
38 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.
39struct los_info 41struct los_info
40{ 42{
41 uint8 flags; // FLG_xxx 43 uint8 flags; // FLG_xxx
42 uint8 culled; // culled from "tree" 44 uint8 culled; // culled from "tree"
43 uint8 visible; 45 uint8 visible;
83 85
84 los_info &l = los[x][y]; 86 los_info &l = los[x][y];
85 87
86 l.flags |= flags; 88 l.flags |= flags;
87 89
88 if (l.flags & FLG_QUEUED) 90 if (expect_false (l.flags & FLG_QUEUED))
89 return; 91 return;
90 92
91 l.flags |= FLG_QUEUED; 93 l.flags |= FLG_QUEUED;
92 94
93 queue[q1].x = dx; 95 queue[q1].x = dx;
126 // spiral path algorithm below, except when very little 128 // spiral path algorithm below, except when very little
127 // area is visible, in which case it is slower. which evens 129 // area is visible, in which case it is slower. which evens
128 // out los calculation times between large and small los maps. 130 // out los calculation times between large and small los maps.
129 // apply_lights also iterates over this area, maybe these 131 // apply_lights also iterates over this area, maybe these
130 // two passes could be combined somehow. 132 // two passes could be combined somehow.
131 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y) 133 unordered_mapwalk (mapwalk_buf, pl->viewpoint, -half_x, -half_y, half_x, half_y)
132 { 134 {
133 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; 135 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
134 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; 136 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
135 } 137 }
136 } 138 }
303 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) 305 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
304 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4; 306 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
305 } 307 }
306} los_init; 308} los_init;
307 309
310// the following functions cannot be static, due to c++ stupidity :/
311namespace {
312 // brighten area, ignore los
308sint8 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
309los_brighten (sint8 b, sint8 l) 321 los_brighten (sint8 b, sint8 l)
310{ 322 {
311 return b == LOS_BLOCKED ? b : min (b, l); 323 return b == LOS_BLOCKED ? b : min (b, l);
312} 324 }
313 325
326 // darken area, respect los
314sint8 327 sint8
315los_darken (sint8 b, sint8 l) 328 los_darken (sint8 b, sint8 l)
316{ 329 {
317 return max (b, l); 330 return max (b, l);
318} 331 }
332};
319 333
320template<sint8 change_it (sint8, sint8)> 334template<sint8 change_it (sint8, sint8)>
321static void 335static void
322apply_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)
323{ 337{
343 * mark those squares specially. 357 * mark those squares specially.
344 */ 358 */
345static void 359static void
346apply_lights (player *pl) 360apply_lights (player *pl)
347{ 361{
348 object *op = pl->observe; 362 object *op = pl->viewpoint;
349 int darklevel = op->map->darklevel (); 363 int darklevel = op->map->darklevel ();
350 364
351 int half_x = pl->ns->mapx / 2; 365 int half_x = pl->ns->mapx / 2;
352 int half_y = pl->ns->mapy / 2; 366 int half_y = pl->ns->mapy / 2;
353 367
354 int pass2 = 0; // negative lights have an extra pass 368 int pass2 = 0; // negative lights have an extra pass
355 369
356 maprect *rects = pl->observe->map->split_to_tiles ( 370 maprect *rects = pl->viewpoint->map->split_to_tiles (
371 mapwalk_buf,
357 pl->observe->x - half_x - MAX_LIGHT_RADIUS, 372 pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS,
358 pl->observe->y - half_y - MAX_LIGHT_RADIUS, 373 pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS,
359 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, 374 pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1,
360 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 375 pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1
361 ); 376 );
362 377
363 /* If the player can see in the dark, increase light/vision radius */ 378 /* If the player can see in the dark, increase light/vision radius */
364 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0; 379 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
365 380
389 if (light < 0) 404 if (light < 0)
390 pass2 = 1; 405 pass2 = 1;
391 else 406 else
392 { 407 {
393 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); 408 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
394 apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]); 409 apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
395 } 410 }
396 } 411 }
397 412
398 /* grant some vision to the player, based on outside, outdoor, and darklevel */ 413 /* grant some vision to the player, based on outside, outdoor, and darklevel */
399 { 414 {
409 light = clamp (light + bonus, 0, MAX_VISION); 424 light = clamp (light + bonus, 0, MAX_VISION);
410 425
411 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 426 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
412 } 427 }
413 } 428 }
429
430 // when we fly high, we have some minimum viewable area around us, like x-ray
431 if (op->move_type & MOVE_FLY_HIGH)
432 apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]);
414 433
415 // possibly do 2nd pass for rare negative glow radii 434 // possibly do 2nd pass for rare negative glow radii
416 // for effect, those are always considered to be stronger than anything else 435 // for effect, those are always considered to be stronger than anything else
417 // but they can't darken a place completely 436 // but they can't darken a place completely
418 if (pass2) 437 if (pass2)
424 sint8 light = ms.light; 443 sint8 light = ms.light;
425 444
426 if (expect_false (light < 0)) 445 if (expect_false (light < 0))
427 { 446 {
428 light = clamp (light - bonus, 0, MAX_DARKNESS); 447 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]); 448 apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
430 } 449 }
431 } 450 }
432} 451}
433 452
434/* blinded_sight() - sets all viewable squares to blocked except 453/* blinded_sight() - sets all viewable squares to blocked except
452 if (ob->flag [FLAG_REMOVED])//D really needed? 471 if (ob->flag [FLAG_REMOVED])//D really needed?
453 return; 472 return;
454 473
455 if (ob->flag [FLAG_WIZLOOK]) 474 if (ob->flag [FLAG_WIZLOOK])
456 clear_los (0); 475 clear_los (0);
457 else if (observe->flag [FLAG_BLIND]) /* player is blind */ 476 else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */
458 { 477 {
459 clear_los (); 478 clear_los ();
460 blinded_sight (this); 479 blinded_sight (this);
461 } 480 }
462 else 481 else
464 clear_los (); 483 clear_los ();
465 calculate_los (this); 484 calculate_los (this);
466 apply_lights (this); 485 apply_lights (this);
467 } 486 }
468 487
469 if (observe->flag [FLAG_XRAYS]) 488 if (viewpoint->flag [FLAG_XRAYS])
470 for (int dx = -2; dx <= 2; dx++) 489 for (int dx = -2; dx <= 2; dx++)
471 for (int dy = -2; dy <= 2; dy++) 490 for (int dy = -2; dy <= 2; dy++)
472 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); 491 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
473} 492}
474 493
515 if (!pl->ob || !pl->ob->map || !pl->ns) 534 if (!pl->ob || !pl->ob->map || !pl->ns)
516 continue; 535 continue;
517 536
518 rv_vector rv; 537 rv_vector rv;
519 538
520 get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv); 539 get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv);
521 540
522 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) 541 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
523 pl->do_los = 1; 542 pl->do_los = 1;
524 } 543 }
525} 544}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines