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.50 by root, Tue Dec 23 22:04:17 2008 UTC vs.
Revision 1.62 by root, Mon Oct 12 14:00:57 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 it under
9 * it under the terms of the GNU General Public License as published by 9 * 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 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * 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/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24#include <global.h> 25#include <global.h>
25#include <cmath> 26#include <cmath>
27
28#define SEE_IN_DARK_RADIUS 2
29#define MAX_VISION 10 // maximum visible radius
26 30
27// los flags 31// los flags
28enum { 32enum {
29 FLG_XI = 0x01, // we have an x-parent 33 FLG_XI = 0x01, // we have an x-parent
30 FLG_YI = 0x02, // we have an y-parent 34 FLG_YI = 0x02, // we have an y-parent
99// still is basically the same algorithm. 103// still is basically the same algorithm.
100static void 104static void
101calculate_los (player *pl) 105calculate_los (player *pl)
102{ 106{
103 { 107 {
108 memset (los, 0, sizeof (los));
109
104 // we keep one line for ourselves, for the border flag 110 // we keep one line for ourselves, for the border flag
105 // so the client area is actually MAP_CLIENT_(X|Y) - 2 111 // so the client area is actually MAP_CLIENT_(X|Y) - 2
106 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2); 112 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
107 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2); 113 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
108 114
116 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED; 122 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
117 123
118 // now reset the los area and also add blocked flags 124 // now reset the los area and also add blocked flags
119 // which supposedly is faster than doing it inside the 125 // which supposedly is faster than doing it inside the
120 // spiral path algorithm below, except when very little 126 // spiral path algorithm below, except when very little
121 // area is visible, in which case it is slower, evening 127 // area is visible, in which case it is slower. which evens
122 // out los calculation times between large and small los maps. 128 // out los calculation times between large and small los maps.
123 // apply_lights also iterates over this area, maybe these 129 // apply_lights also iterates over this area, maybe these
124 // two passes could be combined somehow. 130 // two passes could be combined somehow.
125 rectangular_mapspace_iterate_begin (pl->observe, -half_x, half_x, -half_y, half_y) 131 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y)
132 {
126 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; 133 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
127 l.flags = m && m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; 134 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
128 rectangular_mapspace_iterate_end 135 }
129 } 136 }
130 137
131 q1 = 0; q2 = 0; // initialise queue, not strictly required 138 q1 = 0; q2 = 0; // initialise queue, not strictly required
132 enqueue (0, 0); // enqueue center 139 enqueue (0, 0); // enqueue center
133 140
263 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); 270 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
264 } 271 }
265 } 272 }
266} 273}
267 274
268/* returns true if op carries one or more lights
269 * This is a trivial function now days, but it used to
270 * be a bit longer. Probably better for callers to just
271 * check the op->glow_radius instead of calling this.
272 */
273int
274has_carried_lights (const object *op)
275{
276 /* op may glow! */
277 if (op->glow_radius > 0)
278 return 1;
279
280 return 0;
281}
282
283/* radius, distance => lightness adjust */ 275/* radius, distance => lightness adjust */
284static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; 276static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
285static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; 277static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
286 278
287static struct los_init 279static struct los_init
288{ 280{
289 los_init () 281 los_init ()
290 { 282 {
297 { 289 {
298 // max intensity 290 // max intensity
299 int intensity = min (LOS_MAX, abs (radius) + 1); 291 int intensity = min (LOS_MAX, abs (radius) + 1);
300 292
301 // actual intensity 293 // actual intensity
302 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); 294 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
303 295
304 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 296 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
305 ? min (3, intensity) 297 ? min (3, intensity)
306 : LOS_MAX - intensity; 298 : LOS_MAX - intensity;
307 } 299 }
308 300
309 /* for general vision */ 301 /* for general vision */
310 for (int radius = 0; radius <= MAX_DARKNESS; ++radius) 302 for (int radius = 0; radius <= MAX_VISION; ++radius)
311 for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) 303 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
312 { 304 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
313 vision_atten [radius][distance] = distance <= radius ? 3 : 4;
314 }
315 } 305 }
316} los_init; 306} los_init;
317 307
318sint8 308sint8
319los_brighten (sint8 b, sint8 l) 309los_brighten (sint8 b, sint8 l)
356apply_lights (player *pl) 346apply_lights (player *pl)
357{ 347{
358 object *op = pl->observe; 348 object *op = pl->observe;
359 int darklevel = op->map->darklevel (); 349 int darklevel = op->map->darklevel ();
360 350
361 /* If the player can see in the dark, lower the darklevel for him */
362 if (op->flag [FLAG_SEE_IN_DARK])
363 darklevel = max (0, darklevel - 2);
364
365 int half_x = pl->ns->mapx / 2; 351 int half_x = pl->ns->mapx / 2;
366 int half_y = pl->ns->mapy / 2; 352 int half_y = pl->ns->mapy / 2;
367 353
368 int pass2 = 0; // negative lights have an extra pass 354 int pass2 = 0; // negative lights have an extra pass
355
356 maprect *rects = pl->observe->map->split_to_tiles (
357 pl->observe->x - half_x - MAX_LIGHT_RADIUS,
358 pl->observe->y - half_y - MAX_LIGHT_RADIUS,
359 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1,
360 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1
361 );
362
363 /* 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;
369 365
370 if (!darklevel) 366 if (!darklevel)
371 pass2 = 1; 367 pass2 = 1;
372 else 368 else
373 { 369 {
380 * Only process the area of interest. 376 * Only process the area of interest.
381 * the basex, basey values represent the position in the op->contr->los 377 * the basex, basey values represent the position in the op->contr->los
382 * array. Its easier to just increment them here (and start with the right 378 * array. Its easier to just increment them here (and start with the right
383 * value) than to recalculate them down below. 379 * value) than to recalculate them down below.
384 */ 380 */
385 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) 381 for (maprect *r = rects; r->m; ++r)
386 if (m) 382 rect_mapwalk (r, 0, 0)
387 { 383 {
388 mapspace &ms = m->at (nx, ny); 384 mapspace &ms = m->at (nx, ny);
389 ms.update (); 385 ms.update ();
390 sint8 light = ms.light; 386 sint8 light = ms.light;
391 387
392 if (expect_false (light)) 388 if (expect_false (light))
393 if (light < 0) 389 if (light < 0)
394 pass2 = 1; 390 pass2 = 1;
395 else 391 else
392 {
393 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
396 apply_light<los_brighten> (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]); 394 apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
395 }
397 } 396 }
398 rectangular_mapspace_iterate_end
399 397
400 /* grant some vision to the player, based on the darklevel */ 398 /* grant some vision to the player, based on outside, outdoor, and darklevel */
401 { 399 {
402 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 400 int light;
401
402 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
403 light = MAX_DARKNESS - op->map->darkness;
404 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
405 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
406 else // outdoor and darkness <= 0 => start wide and decrease quickly
407 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
408
409 light = clamp (light + bonus, 0, MAX_VISION);
403 410
404 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 411 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
405 } 412 }
406 } 413 }
407 414
408 // possibly do 2nd pass for rare negative glow radii 415 // possibly do 2nd pass for rare negative glow radii
409 // for effect, those are always considered to be stronger than anything else 416 // for effect, those are always considered to be stronger than anything else
410 // but they can't darken a place completely 417 // but they can't darken a place completely
411 if (pass2) 418 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) 419 for (maprect *r = rects; r->m; ++r)
413 if (m) 420 rect_mapwalk (r, 0, 0)
414 { 421 {
415 mapspace &ms = m->at (nx, ny); 422 mapspace &ms = m->at (nx, ny);
416 ms.update (); 423 ms.update ();
417 sint8 light = ms.light; 424 sint8 light = ms.light;
418 425
419 if (expect_false (light < 0)) 426 if (expect_false (light < 0))
427 {
428 light = clamp (light - bonus, 0, MAX_DARKNESS);
420 apply_light<los_darken> (pl, dx, dy, -light, light_atten [light + MAX_LIGHT_RADIUS]); 429 apply_light<los_darken> (pl, dx - pl->observe->x, dy - pl->observe->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
430 }
421 } 431 }
422 rectangular_mapspace_iterate_end
423} 432}
424 433
425/* blinded_sight() - sets all viewable squares to blocked except 434/* blinded_sight() - sets all viewable squares to blocked except
426 * for the one the central one that the player occupies. A little 435 * 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 436 * odd that you can see yourself (and what your standing on), but
504 * check as a safety 513 * check as a safety
505 */ 514 */
506 if (!pl->ob || !pl->ob->map || !pl->ns) 515 if (!pl->ob || !pl->ob->map || !pl->ns)
507 continue; 516 continue;
508 517
509 /* Same map is simple case - see if pl is close enough. 518 rv_vector rv;
510 * Note in all cases, we did the check for same map first, 519
511 * and then see if the player is close enough and update 520 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 */ 521
518 if (pl->ob->map == map) 522 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; 523 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 } 524 }
560} 525}
561 526
562static const int season_darkness[5][HOURS_PER_DAY] = { 527static 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 */ 528 /*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 && 601 if (pl->ob->map == op->map &&
637 pl->ob->y - pl->ns->mapy / 2 <= op->y && 602 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) 603 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; 604 pl->do_los = 1;
640} 605}
606

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines