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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines