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,2010 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 |
30 | enum { |
30 | enum { |
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. |
37 | struct los_info |
41 | struct 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; |
… | |
… | |
124 | // spiral path algorithm below, except when very little |
128 | // spiral path algorithm below, except when very little |
125 | // area is visible, in which case it is slower. which evens |
129 | // area is visible, in which case it is slower. which evens |
126 | // out los calculation times between large and small los maps. |
130 | // out los calculation times between large and small los maps. |
127 | // apply_lights also iterates over this area, maybe these |
131 | // apply_lights also iterates over this area, maybe these |
128 | // two passes could be combined somehow. |
132 | // two passes could be combined somehow. |
129 | 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) |
130 | { |
134 | { |
131 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
135 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
132 | 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; |
133 | } |
137 | } |
134 | } |
138 | } |
… | |
… | |
268 | if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); |
272 | if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); |
269 | } |
273 | } |
270 | } |
274 | } |
271 | } |
275 | } |
272 | |
276 | |
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 | */ |
|
|
278 | int |
|
|
279 | has_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 */ |
277 | /* radius, distance => lightness adjust */ |
289 | static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; |
278 | static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; |
290 | static sint8 vision_atten[MAX_DARKNESS + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1]; |
279 | static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1]; |
291 | |
280 | |
292 | static struct los_init |
281 | static struct los_init |
293 | { |
282 | { |
294 | los_init () |
283 | los_init () |
295 | { |
284 | { |
… | |
… | |
302 | { |
291 | { |
303 | // max intensity |
292 | // max intensity |
304 | int intensity = min (LOS_MAX, abs (radius) + 1); |
293 | int intensity = min (LOS_MAX, abs (radius) + 1); |
305 | |
294 | |
306 | // actual intensity |
295 | // actual intensity |
307 | 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)); |
308 | |
297 | |
309 | light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 |
298 | light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 |
310 | ? min (3, intensity) |
299 | ? min (3, intensity) |
311 | : LOS_MAX - intensity; |
300 | : LOS_MAX - intensity; |
312 | } |
301 | } |
313 | |
302 | |
314 | /* for general vision */ |
303 | /* for general vision */ |
315 | for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) |
304 | for (int radius = 0; radius <= MAX_VISION; ++radius) |
316 | 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) |
317 | 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; |
318 | } |
307 | } |
319 | } los_init; |
308 | } los_init; |
320 | |
309 | |
|
|
310 | // the following functions cannot be static, due to c++ stupidity :/ |
|
|
311 | namespace { |
|
|
312 | // brighten area, ignore los |
321 | sint8 |
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 |
322 | los_brighten (sint8 b, sint8 l) |
321 | los_brighten (sint8 b, sint8 l) |
323 | { |
322 | { |
324 | return b == LOS_BLOCKED ? b : min (b, l); |
323 | return b == LOS_BLOCKED ? b : min (b, l); |
325 | } |
324 | } |
326 | |
325 | |
|
|
326 | // darken area, respect los |
327 | sint8 |
327 | sint8 |
328 | los_darken (sint8 b, sint8 l) |
328 | los_darken (sint8 b, sint8 l) |
329 | { |
329 | { |
330 | return max (b, l); |
330 | return max (b, l); |
331 | } |
331 | } |
|
|
332 | }; |
332 | |
333 | |
333 | template<sint8 change_it (sint8, sint8)> |
334 | template<sint8 change_it (sint8, sint8)> |
334 | static void |
335 | static void |
335 | apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) |
336 | apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) |
336 | { |
337 | { |
… | |
… | |
356 | * mark those squares specially. |
357 | * mark those squares specially. |
357 | */ |
358 | */ |
358 | static void |
359 | static void |
359 | apply_lights (player *pl) |
360 | apply_lights (player *pl) |
360 | { |
361 | { |
361 | object *op = pl->observe; |
362 | object *op = pl->viewpoint; |
362 | int darklevel = op->map->darklevel (); |
363 | int darklevel = op->map->darklevel (); |
363 | |
364 | |
364 | int half_x = pl->ns->mapx / 2; |
365 | int half_x = pl->ns->mapx / 2; |
365 | int half_y = pl->ns->mapy / 2; |
366 | int half_y = pl->ns->mapy / 2; |
366 | |
367 | |
367 | int pass2 = 0; // negative lights have an extra pass |
368 | int pass2 = 0; // negative lights have an extra pass |
368 | |
369 | |
369 | maprect *rects = pl->observe->map->split_to_tiles ( |
370 | maprect *rects = pl->viewpoint->map->split_to_tiles ( |
|
|
371 | mapwalk_buf, |
370 | pl->observe->x - half_x - MAX_LIGHT_RADIUS, |
372 | pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, |
371 | pl->observe->y - half_y - MAX_LIGHT_RADIUS, |
373 | pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, |
372 | pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, |
374 | pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, |
373 | pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 |
375 | pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1 |
374 | ); |
376 | ); |
|
|
377 | |
|
|
378 | /* If the player can see in the dark, increase light/vision radius */ |
|
|
379 | int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0; |
375 | |
380 | |
376 | if (!darklevel) |
381 | if (!darklevel) |
377 | pass2 = 1; |
382 | pass2 = 1; |
378 | else |
383 | else |
379 | { |
384 | { |
… | |
… | |
397 | |
402 | |
398 | if (expect_false (light)) |
403 | if (expect_false (light)) |
399 | if (light < 0) |
404 | if (light < 0) |
400 | pass2 = 1; |
405 | pass2 = 1; |
401 | else |
406 | else |
|
|
407 | { |
|
|
408 | 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]); |
409 | apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]); |
|
|
410 | } |
403 | } |
411 | } |
404 | |
412 | |
405 | /* grant some vision to the player, based on the darklevel */ |
413 | /* grant some vision to the player, based on outside, outdoor, and darklevel */ |
406 | { |
414 | { |
407 | int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); |
415 | int light; |
408 | |
416 | |
409 | /* If the player can see in the dark, lower the darklevel for him */ |
417 | if (!op->map->outdoor) // not outdoor, darkness becomes light radius |
410 | if (op->flag [FLAG_SEE_IN_DARK]) |
418 | light = MAX_DARKNESS - op->map->darkness; |
411 | light += SEE_IN_DARK_RADIUS; |
419 | else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius |
|
|
420 | light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0); |
|
|
421 | else // outdoor and darkness <= 0 => start wide and decrease quickly |
|
|
422 | light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2); |
|
|
423 | |
|
|
424 | light = clamp (light + bonus, 0, MAX_VISION); |
412 | |
425 | |
413 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
426 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
414 | } |
427 | } |
415 | } |
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]); |
416 | |
433 | |
417 | // possibly do 2nd pass for rare negative glow radii |
434 | // possibly do 2nd pass for rare negative glow radii |
418 | // 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 |
419 | // but they can't darken a place completely |
436 | // but they can't darken a place completely |
420 | if (pass2) |
437 | if (pass2) |
… | |
… | |
424 | mapspace &ms = m->at (nx, ny); |
441 | mapspace &ms = m->at (nx, ny); |
425 | ms.update (); |
442 | ms.update (); |
426 | sint8 light = ms.light; |
443 | sint8 light = ms.light; |
427 | |
444 | |
428 | if (expect_false (light < 0)) |
445 | if (expect_false (light < 0)) |
|
|
446 | { |
|
|
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]); |
|
|
449 | } |
430 | } |
450 | } |
431 | } |
451 | } |
432 | |
452 | |
433 | /* blinded_sight() - sets all viewable squares to blocked except |
453 | /* blinded_sight() - sets all viewable squares to blocked except |
434 | * for the one the central one that the player occupies. A little |
454 | * for the one the central one that the player occupies. A little |
… | |
… | |
451 | if (ob->flag [FLAG_REMOVED])//D really needed? |
471 | if (ob->flag [FLAG_REMOVED])//D really needed? |
452 | return; |
472 | return; |
453 | |
473 | |
454 | if (ob->flag [FLAG_WIZLOOK]) |
474 | if (ob->flag [FLAG_WIZLOOK]) |
455 | clear_los (0); |
475 | clear_los (0); |
456 | else if (observe->flag [FLAG_BLIND]) /* player is blind */ |
476 | else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */ |
457 | { |
477 | { |
458 | clear_los (); |
478 | clear_los (); |
459 | blinded_sight (this); |
479 | blinded_sight (this); |
460 | } |
480 | } |
461 | else |
481 | else |
… | |
… | |
463 | clear_los (); |
483 | clear_los (); |
464 | calculate_los (this); |
484 | calculate_los (this); |
465 | apply_lights (this); |
485 | apply_lights (this); |
466 | } |
486 | } |
467 | |
487 | |
468 | if (observe->flag [FLAG_XRAYS]) |
488 | if (viewpoint->flag [FLAG_XRAYS]) |
469 | for (int dx = -2; dx <= 2; dx++) |
489 | for (int dx = -2; dx <= 2; dx++) |
470 | for (int dy = -2; dy <= 2; dy++) |
490 | for (int dy = -2; dy <= 2; dy++) |
471 | min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); |
491 | min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); |
472 | } |
492 | } |
473 | |
493 | |
… | |
… | |
502 | * map is the map that changed, x and y are the coordinates. |
522 | * map is the map that changed, x and y are the coordinates. |
503 | */ |
523 | */ |
504 | void |
524 | void |
505 | update_all_los (const maptile *map, int x, int y) |
525 | update_all_los (const maptile *map, int x, int y) |
506 | { |
526 | { |
507 | // no need to do anything if we don't have darkness |
|
|
508 | if (map->darklevel () <= 0) |
|
|
509 | return; |
|
|
510 | |
|
|
511 | map->at (x, y).invalidate (); |
527 | map->at (x, y).invalidate (); |
512 | |
528 | |
513 | for_all_players (pl) |
529 | for_all_players (pl) |
514 | { |
530 | { |
515 | /* Player should not have a null map, but do this |
531 | /* Player should not have a null map, but do this |
516 | * check as a safety |
532 | * check as a safety |
517 | */ |
533 | */ |
518 | if (!pl->ob || !pl->ob->map || !pl->ns) |
534 | if (!pl->ob || !pl->ob->map || !pl->ns) |
519 | continue; |
535 | continue; |
520 | |
536 | |
521 | /* Same map is simple case - see if pl is close enough. |
537 | rv_vector rv; |
522 | * Note in all cases, we did the check for same map first, |
538 | |
523 | * and then see if the player is close enough and update |
539 | get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv); |
524 | * los if that is the case. If the player is on the |
|
|
525 | * corresponding map, but not close enough, then the |
|
|
526 | * player can't be on another map that may be closer, |
|
|
527 | * so by setting it up this way, we trim processing |
|
|
528 | * some. |
|
|
529 | */ |
540 | |
530 | if (pl->ob->map == map) |
541 | if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) |
531 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
532 | pl->do_los = 1; |
542 | pl->do_los = 1; |
533 | |
|
|
534 | /* Now we check to see if player is on adjacent |
|
|
535 | * maps to the one that changed and also within |
|
|
536 | * view. The tile_maps[] could be null, but in that |
|
|
537 | * case it should never match the pl->ob->map, so |
|
|
538 | * we want ever try to dereference any of the data in it. |
|
|
539 | * |
|
|
540 | * The logic for 0 and 3 is to see how far the player is |
|
|
541 | * from the edge of the map (height/width) - pl->ob->(x,y) |
|
|
542 | * and to add current position on this map - that gives a |
|
|
543 | * distance. |
|
|
544 | * For 1 and 2, we check to see how far the given |
|
|
545 | * coordinate (x,y) is from the corresponding edge, |
|
|
546 | * and then add the players location, which gives |
|
|
547 | * a distance. |
|
|
548 | */ |
|
|
549 | else if (pl->ob->map == map->tile_map[0]) |
|
|
550 | { |
|
|
551 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2)) |
|
|
552 | pl->do_los = 1; |
|
|
553 | } |
|
|
554 | else if (pl->ob->map == map->tile_map[2]) |
|
|
555 | { |
|
|
556 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2)) |
|
|
557 | pl->do_los = 1; |
|
|
558 | } |
|
|
559 | else if (pl->ob->map == map->tile_map[1]) |
|
|
560 | { |
|
|
561 | if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
562 | pl->do_los = 1; |
|
|
563 | } |
|
|
564 | else if (pl->ob->map == map->tile_map[3]) |
|
|
565 | { |
|
|
566 | if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
567 | pl->do_los = 1; |
|
|
568 | } |
|
|
569 | } |
543 | } |
570 | } |
544 | } |
571 | |
545 | |
572 | static const int season_darkness[5][HOURS_PER_DAY] = { |
546 | static const int season_darkness[5][HOURS_PER_DAY] = { |
573 | /*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 */ |
547 | /*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 */ |