… | |
… | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * |
20 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | #include <bench.h>//D |
|
|
25 | #include <global.h> |
24 | #include <global.h> |
26 | #include <cmath> |
25 | #include <cmath> |
|
|
26 | |
|
|
27 | #define SEE_IN_DARK_RADIUS 3 |
27 | |
28 | |
28 | // los flags |
29 | // los flags |
29 | enum { |
30 | enum { |
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. |
101 | static void |
102 | static void |
102 | calculate_los (player *pl) |
103 | calculate_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 | |
… | |
… | |
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, evening |
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->observe, -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 | |
… | |
… | |
281 | return 0; |
285 | return 0; |
282 | } |
286 | } |
283 | |
287 | |
284 | /* radius, distance => lightness adjust */ |
288 | /* radius, distance => lightness adjust */ |
285 | static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; |
289 | static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; |
286 | static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; |
290 | static sint8 vision_atten[MAX_DARKNESS + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1]; |
287 | |
291 | |
288 | static struct los_init |
292 | static struct los_init |
289 | { |
293 | { |
290 | los_init () |
294 | los_init () |
291 | { |
295 | { |
… | |
… | |
306 | ? min (3, intensity) |
310 | ? min (3, intensity) |
307 | : LOS_MAX - intensity; |
311 | : LOS_MAX - intensity; |
308 | } |
312 | } |
309 | |
313 | |
310 | /* for general vision */ |
314 | /* for general vision */ |
311 | for (int radius = 0; radius <= MAX_DARKNESS; ++radius) |
315 | for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) |
312 | for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) |
316 | for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2; ++distance) |
313 | { |
|
|
314 | vision_atten [radius][distance] = distance <= radius ? 3 : 4; |
317 | vision_atten [radius][distance] = distance <= radius ? 3 : 4; |
315 | } |
|
|
316 | } |
318 | } |
317 | } los_init; |
319 | } los_init; |
318 | |
320 | |
319 | sint8 |
321 | sint8 |
320 | los_brighten (sint8 b, sint8 l) |
322 | los_brighten (sint8 b, sint8 l) |
… | |
… | |
357 | apply_lights (player *pl) |
359 | apply_lights (player *pl) |
358 | { |
360 | { |
359 | object *op = pl->observe; |
361 | object *op = pl->observe; |
360 | int darklevel = op->map->darklevel (); |
362 | int darklevel = op->map->darklevel (); |
361 | |
363 | |
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 | |
|
|
366 | int half_x = pl->ns->mapx / 2; |
364 | int half_x = pl->ns->mapx / 2; |
367 | int half_y = pl->ns->mapy / 2; |
365 | int half_y = pl->ns->mapy / 2; |
368 | |
366 | |
369 | int pass2 = 0; // negative lights have an extra pass |
367 | int pass2 = 0; // negative lights have an extra pass |
|
|
368 | |
|
|
369 | maprect *rects = pl->observe->map->split_to_tiles ( |
|
|
370 | pl->observe->x - half_x - MAX_LIGHT_RADIUS, |
|
|
371 | pl->observe->y - half_y - MAX_LIGHT_RADIUS, |
|
|
372 | pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, |
|
|
373 | pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 |
|
|
374 | ); |
370 | |
375 | |
371 | if (!darklevel) |
376 | if (!darklevel) |
372 | pass2 = 1; |
377 | pass2 = 1; |
373 | else |
378 | else |
374 | { |
379 | { |
… | |
… | |
381 | * Only process the area of interest. |
386 | * Only process the area of interest. |
382 | * the basex, basey values represent the position in the op->contr->los |
387 | * 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 |
388 | * array. Its easier to just increment them here (and start with the right |
384 | * value) than to recalculate them down below. |
389 | * value) than to recalculate them down below. |
385 | */ |
390 | */ |
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) |
391 | for (maprect *r = rects; r->m; ++r) |
387 | if (m) |
392 | rect_mapwalk (r, 0, 0) |
388 | { |
393 | { |
389 | mapspace &ms = m->at (nx, ny); |
394 | mapspace &ms = m->at (nx, ny); |
390 | ms.update (); |
395 | ms.update (); |
391 | sint8 light = ms.light; |
396 | sint8 light = ms.light; |
392 | |
397 | |
393 | if (expect_false (light)) |
398 | if (expect_false (light)) |
394 | if (light < 0) |
399 | if (light < 0) |
395 | pass2 = 1; |
400 | pass2 = 1; |
396 | else |
401 | else |
397 | apply_light<los_brighten> (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]); |
402 | apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]); |
398 | } |
403 | } |
399 | rectangular_mapspace_iterate_end |
|
|
400 | |
404 | |
401 | /* grant some vision to the player, based on the darklevel */ |
405 | /* grant some vision to the player, based on the darklevel */ |
402 | { |
406 | { |
403 | int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); |
407 | int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); |
|
|
408 | |
|
|
409 | /* If the player can see in the dark, lower the darklevel for him */ |
|
|
410 | if (op->flag [FLAG_SEE_IN_DARK]) |
|
|
411 | light += SEE_IN_DARK_RADIUS; |
404 | |
412 | |
405 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
413 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
406 | } |
414 | } |
407 | } |
415 | } |
408 | |
416 | |
409 | // possibly do 2nd pass for rare negative glow radii |
417 | // possibly do 2nd pass for rare negative glow radii |
410 | // for effect, those are always considered to be stronger than anything else |
418 | // for effect, those are always considered to be stronger than anything else |
411 | // but they can't darken a place completely |
419 | // but they can't darken a place completely |
412 | if (pass2) |
420 | 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) |
421 | for (maprect *r = rects; r->m; ++r) |
414 | if (m) |
422 | rect_mapwalk (r, 0, 0) |
415 | { |
423 | { |
416 | mapspace &ms = m->at (nx, ny); |
424 | mapspace &ms = m->at (nx, ny); |
417 | ms.update (); |
425 | ms.update (); |
418 | sint8 light = ms.light; |
426 | sint8 light = ms.light; |
419 | |
427 | |
420 | if (expect_false (light < 0)) |
428 | if (expect_false (light < 0)) |
421 | 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]); |
422 | } |
430 | } |
423 | rectangular_mapspace_iterate_end |
|
|
424 | } |
431 | } |
425 | |
432 | |
426 | /* blinded_sight() - sets all viewable squares to blocked except |
433 | /* blinded_sight() - sets all viewable squares to blocked except |
427 | * for the one the central one that the player occupies. A little |
434 | * 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 |
435 | * odd that you can see yourself (and what your standing on), but |
… | |
… | |
495 | * map is the map that changed, x and y are the coordinates. |
502 | * map is the map that changed, x and y are the coordinates. |
496 | */ |
503 | */ |
497 | void |
504 | void |
498 | update_all_los (const maptile *map, int x, int y) |
505 | update_all_los (const maptile *map, int x, int y) |
499 | { |
506 | { |
|
|
507 | // no need to do anything if we don't have darkness |
|
|
508 | if (map->darklevel () <= 0) |
|
|
509 | return; |
|
|
510 | |
500 | map->at (x, y).invalidate (); |
511 | map->at (x, y).invalidate (); |
501 | |
512 | |
502 | for_all_players (pl) |
513 | for_all_players (pl) |
503 | { |
514 | { |
504 | /* Player should not have a null map, but do this |
515 | /* Player should not have a null map, but do this |
… | |
… | |
515 | * player can't be on another map that may be closer, |
526 | * player can't be on another map that may be closer, |
516 | * so by setting it up this way, we trim processing |
527 | * so by setting it up this way, we trim processing |
517 | * some. |
528 | * some. |
518 | */ |
529 | */ |
519 | if (pl->ob->map == map) |
530 | if (pl->ob->map == map) |
520 | { |
|
|
521 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - 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)) |
522 | pl->do_los = 1; |
532 | pl->do_los = 1; |
523 | } |
|
|
524 | |
533 | |
525 | /* Now we check to see if player is on adjacent |
534 | /* Now we check to see if player is on adjacent |
526 | * maps to the one that changed and also within |
535 | * maps to the one that changed and also within |
527 | * view. The tile_maps[] could be null, but in that |
536 | * view. The tile_maps[] could be null, but in that |
528 | * case it should never match the pl->ob->map, so |
537 | * case it should never match the pl->ob->map, so |
… | |
… | |
637 | if (pl->ob->map == op->map && |
646 | if (pl->ob->map == op->map && |
638 | pl->ob->y - pl->ns->mapy / 2 <= op->y && |
647 | 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) |
648 | 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; |
649 | pl->do_los = 1; |
641 | } |
650 | } |
|
|
651 | |