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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 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 | * |
6 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * 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 |
8 | * 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 |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
10 | * option) any later version. |
12 | * |
11 | * |
13 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
17 | * |
16 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
17 | * 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 |
18 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
21 | * |
20 | * |
22 | * 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> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include <global.h> |
24 | #include <global.h> |
26 | #include <cmath> |
25 | #include <cmath> |
… | |
… | |
34 | FLG_YI = 0x02, // we have an y-parent |
33 | FLG_YI = 0x02, // we have an y-parent |
35 | FLG_BLOCKED = 0x04, // this space blocks the view |
34 | FLG_BLOCKED = 0x04, // this space blocks the view |
36 | FLG_QUEUED = 0x80 // already queued in queue, or border |
35 | FLG_QUEUED = 0x80 // already queued in queue, or border |
37 | }; |
36 | }; |
38 | |
37 | |
|
|
38 | // it is important for performance reasons that this structure |
|
|
39 | // has a size easily computable by the cpu (*8 is perfect). |
|
|
40 | // it is possible to move culled and visible into flags, at |
|
|
41 | // some speed loss. |
39 | struct los_info |
42 | struct los_info |
40 | { |
43 | { |
41 | uint8 flags; // FLG_xxx |
44 | uint8 flags; // FLG_xxx |
42 | uint8 culled; // culled from "tree" |
45 | uint8 culled; // culled from "tree" |
43 | uint8 visible; |
46 | uint8 visible; |
… | |
… | |
49 | |
52 | |
50 | // temporary storage for the los algorithm, |
53 | // temporary storage for the los algorithm, |
51 | // one los_info for each lightable map space |
54 | // one los_info for each lightable map space |
52 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
55 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
53 | |
56 | |
54 | struct point |
57 | struct point8 |
55 | { |
58 | { |
56 | sint8 x, y; |
59 | sint8 x, y; |
57 | }; |
60 | }; |
58 | |
61 | |
59 | // minimum size, but must be a power of two |
62 | // minimum size, but must be a power of two |
60 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
63 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
61 | |
64 | |
62 | // a queue of spaces to calculate |
65 | // a queue of spaces to calculate |
63 | static point queue [QUEUE_LENGTH]; |
66 | static point8 queue [QUEUE_LENGTH]; |
64 | static int q1, q2; // queue start, end |
67 | static int q1, q2; // queue start, end |
65 | |
68 | |
66 | /* |
69 | /* |
67 | * Clears/initialises the los-array associated to the player |
70 | * Clears/initialises the los-array associated to the player |
68 | * controlling the object. |
71 | * controlling the object. |
… | |
… | |
83 | |
86 | |
84 | los_info &l = los[x][y]; |
87 | los_info &l = los[x][y]; |
85 | |
88 | |
86 | l.flags |= flags; |
89 | l.flags |= flags; |
87 | |
90 | |
88 | if (l.flags & FLG_QUEUED) |
91 | if (expect_false (l.flags & FLG_QUEUED)) |
89 | return; |
92 | return; |
90 | |
93 | |
91 | l.flags |= FLG_QUEUED; |
94 | l.flags |= FLG_QUEUED; |
92 | |
95 | |
93 | queue[q1].x = dx; |
96 | queue[q1].x = dx; |
… | |
… | |
126 | // spiral path algorithm below, except when very little |
129 | // spiral path algorithm below, except when very little |
127 | // area is visible, in which case it is slower. which evens |
130 | // area is visible, in which case it is slower. which evens |
128 | // out los calculation times between large and small los maps. |
131 | // out los calculation times between large and small los maps. |
129 | // apply_lights also iterates over this area, maybe these |
132 | // apply_lights also iterates over this area, maybe these |
130 | // two passes could be combined somehow. |
133 | // two passes could be combined somehow. |
131 | unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y) |
134 | unordered_mapwalk (mapwalk_buf, pl->viewpoint, -half_x, -half_y, half_x, half_y) |
132 | { |
135 | { |
133 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
136 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
134 | l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; |
137 | l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; |
135 | } |
138 | } |
136 | } |
139 | } |
… | |
… | |
172 | l.culled &= !xi->visible; |
175 | l.culled &= !xi->visible; |
173 | |
176 | |
174 | /* merge input space */ |
177 | /* merge input space */ |
175 | if (expect_false (xi->xo || xi->yo)) |
178 | if (expect_false (xi->xo || xi->yo)) |
176 | { |
179 | { |
177 | // The X input can provide two main pieces of information: |
180 | // The X input can provide two main pieces of information: |
178 | // 1. Progressive X obscurity. |
181 | // 1. Progressive X obscurity. |
179 | // 2. Recessive Y obscurity. |
182 | // 2. Recessive Y obscurity. |
180 | |
183 | |
181 | // Progressive X obscurity, favouring recessive input angle |
184 | // Progressive X obscurity, favouring recessive input angle |
182 | if (xi->xe > 0 && l.xo == 0) |
185 | if (xi->xe > 0 && l.xo == 0) |
… | |
… | |
207 | l.culled &= !yi->visible; |
210 | l.culled &= !yi->visible; |
208 | |
211 | |
209 | /* merge input space */ |
212 | /* merge input space */ |
210 | if (expect_false (yi->yo || yi->xo)) |
213 | if (expect_false (yi->yo || yi->xo)) |
211 | { |
214 | { |
212 | // The Y input can provide two main pieces of information: |
215 | // The Y input can provide two main pieces of information: |
213 | // 1. Progressive Y obscurity. |
216 | // 1. Progressive Y obscurity. |
214 | // 2. Recessive X obscurity. |
217 | // 2. Recessive X obscurity. |
215 | |
218 | |
216 | // Progressive Y obscurity, favouring recessive input angle |
219 | // Progressive Y obscurity, favouring recessive input angle |
217 | if (yi->ye > 0 && l.yo == 0) |
220 | if (yi->ye > 0 && l.yo == 0) |
… | |
… | |
258 | } |
261 | } |
259 | |
262 | |
260 | } |
263 | } |
261 | |
264 | |
262 | // Expands by the unit length in each component's current direction. |
265 | // Expands by the unit length in each component's current direction. |
263 | // If a component has no direction, then it is expanded in both of its |
266 | // If a component has no direction, then it is expanded in both of its |
264 | // positive and negative directions. |
267 | // positive and negative directions. |
265 | if (!l.culled) |
268 | if (!l.culled) |
266 | { |
269 | { |
267 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
270 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
268 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
271 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
… | |
… | |
303 | for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) |
306 | 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; |
307 | vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4; |
305 | } |
308 | } |
306 | } los_init; |
309 | } los_init; |
307 | |
310 | |
|
|
311 | // the following functions cannot be static, due to c++ stupidity :/ |
|
|
312 | namespace { |
|
|
313 | // brighten area, ignore los |
308 | sint8 |
314 | sint8 |
|
|
315 | los_brighten_nolos (sint8 b, sint8 l) |
|
|
316 | { |
|
|
317 | return min (b, l); |
|
|
318 | } |
|
|
319 | |
|
|
320 | // brighten area, but respect los |
|
|
321 | sint8 |
309 | los_brighten (sint8 b, sint8 l) |
322 | los_brighten (sint8 b, sint8 l) |
310 | { |
323 | { |
311 | return b == LOS_BLOCKED ? b : min (b, l); |
324 | return b == LOS_BLOCKED ? b : min (b, l); |
312 | } |
325 | } |
313 | |
326 | |
|
|
327 | // darken area, respect los |
314 | sint8 |
328 | sint8 |
315 | los_darken (sint8 b, sint8 l) |
329 | los_darken (sint8 b, sint8 l) |
316 | { |
330 | { |
317 | return max (b, l); |
331 | return max (b, l); |
318 | } |
332 | } |
|
|
333 | }; |
319 | |
334 | |
320 | template<sint8 change_it (sint8, sint8)> |
335 | template<sint8 change_it (sint8, sint8)> |
321 | static void |
336 | static void |
322 | apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) |
337 | apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table) |
323 | { |
338 | { |
… | |
… | |
337 | for (int ay = ay0; ay <= ay1; ay++) |
352 | for (int ay = ay0; ay <= ay1; ay++) |
338 | pl->los[ax][ay] = |
353 | pl->los[ax][ay] = |
339 | change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]); |
354 | change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]); |
340 | } |
355 | } |
341 | |
356 | |
342 | /* add light, by finding all (non-null) nearby light sources, then |
357 | /* add light, by finding all (non-null) nearby light sources, then |
343 | * mark those squares specially. |
358 | * mark those squares specially. |
344 | */ |
359 | */ |
345 | static void |
360 | static void |
346 | apply_lights (player *pl) |
361 | apply_lights (player *pl) |
347 | { |
362 | { |
348 | object *op = pl->observe; |
363 | object *op = pl->viewpoint; |
349 | int darklevel = op->map->darklevel (); |
364 | int darklevel = op->map->darklevel (); |
350 | |
365 | |
351 | int half_x = pl->ns->mapx / 2; |
366 | int half_x = pl->ns->mapx / 2; |
352 | int half_y = pl->ns->mapy / 2; |
367 | int half_y = pl->ns->mapy / 2; |
353 | |
368 | |
354 | int pass2 = 0; // negative lights have an extra pass |
369 | int pass2 = 0; // negative lights have an extra pass |
355 | |
370 | |
356 | maprect *rects = pl->observe->map->split_to_tiles ( |
371 | maprect *rects = pl->viewpoint->map->split_to_tiles ( |
|
|
372 | mapwalk_buf, |
357 | pl->observe->x - half_x - MAX_LIGHT_RADIUS, |
373 | pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, |
358 | pl->observe->y - half_y - MAX_LIGHT_RADIUS, |
374 | pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, |
359 | pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, |
375 | pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, |
360 | pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 |
376 | pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1 |
361 | ); |
377 | ); |
362 | |
378 | |
363 | /* If the player can see in the dark, increase light/vision radius */ |
379 | /* 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; |
380 | int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0; |
365 | |
381 | |
… | |
… | |
389 | if (light < 0) |
405 | if (light < 0) |
390 | pass2 = 1; |
406 | pass2 = 1; |
391 | else |
407 | else |
392 | { |
408 | { |
393 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
409 | 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]); |
410 | apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]); |
395 | } |
411 | } |
396 | } |
412 | } |
397 | |
413 | |
398 | /* grant some vision to the player, based on outside, outdoor, and darklevel */ |
414 | /* grant some vision to the player, based on outside, outdoor, and darklevel */ |
399 | { |
415 | { |
… | |
… | |
409 | light = clamp (light + bonus, 0, MAX_VISION); |
425 | light = clamp (light + bonus, 0, MAX_VISION); |
410 | |
426 | |
411 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
427 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
412 | } |
428 | } |
413 | } |
429 | } |
|
|
430 | |
|
|
431 | // when we fly high, we have some minimum viewable area around us, like x-ray |
|
|
432 | if (op->move_type & MOVE_FLY_HIGH) |
|
|
433 | apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]); |
414 | |
434 | |
415 | // possibly do 2nd pass for rare negative glow radii |
435 | // possibly do 2nd pass for rare negative glow radii |
416 | // for effect, those are always considered to be stronger than anything else |
436 | // for effect, those are always considered to be stronger than anything else |
417 | // but they can't darken a place completely |
437 | // but they can't darken a place completely |
418 | if (pass2) |
438 | if (pass2) |
… | |
… | |
424 | sint8 light = ms.light; |
444 | sint8 light = ms.light; |
425 | |
445 | |
426 | if (expect_false (light < 0)) |
446 | if (expect_false (light < 0)) |
427 | { |
447 | { |
428 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
448 | 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]); |
449 | apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); |
430 | } |
450 | } |
431 | } |
451 | } |
432 | } |
452 | } |
433 | |
453 | |
434 | /* blinded_sight() - sets all viewable squares to blocked except |
454 | /* blinded_sight() - sets all viewable squares to blocked except |
435 | * for the one the central one that the player occupies. A little |
455 | * for the one the central one that the player occupies. A little |
436 | * odd that you can see yourself (and what your standing on), but |
456 | * odd that you can see yourself (and what your standing on), but |
437 | * really need for any reasonable game play. |
457 | * really need for any reasonable game play. |
438 | */ |
458 | */ |
439 | static void |
459 | static void |
… | |
… | |
452 | if (ob->flag [FLAG_REMOVED])//D really needed? |
472 | if (ob->flag [FLAG_REMOVED])//D really needed? |
453 | return; |
473 | return; |
454 | |
474 | |
455 | if (ob->flag [FLAG_WIZLOOK]) |
475 | if (ob->flag [FLAG_WIZLOOK]) |
456 | clear_los (0); |
476 | clear_los (0); |
457 | else if (observe->flag [FLAG_BLIND]) /* player is blind */ |
477 | else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */ |
458 | { |
478 | { |
459 | clear_los (); |
479 | clear_los (); |
460 | blinded_sight (this); |
480 | blinded_sight (this); |
461 | } |
481 | } |
462 | else |
482 | else |
… | |
… | |
464 | clear_los (); |
484 | clear_los (); |
465 | calculate_los (this); |
485 | calculate_los (this); |
466 | apply_lights (this); |
486 | apply_lights (this); |
467 | } |
487 | } |
468 | |
488 | |
469 | if (observe->flag [FLAG_XRAYS]) |
489 | if (viewpoint->flag [FLAG_XRAYS]) |
470 | for (int dx = -2; dx <= 2; dx++) |
490 | for (int dx = -2; dx <= 2; dx++) |
471 | for (int dy = -2; dy <= 2; dy++) |
491 | for (int dy = -2; dy <= 2; dy++) |
472 | min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); |
492 | min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); |
473 | } |
493 | } |
474 | |
494 | |
… | |
… | |
515 | if (!pl->ob || !pl->ob->map || !pl->ns) |
535 | if (!pl->ob || !pl->ob->map || !pl->ns) |
516 | continue; |
536 | continue; |
517 | |
537 | |
518 | rv_vector rv; |
538 | rv_vector rv; |
519 | |
539 | |
520 | get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv); |
540 | get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv); |
521 | |
541 | |
522 | if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) |
542 | if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) |
523 | pl->do_los = 1; |
543 | pl->do_los = 1; |
524 | } |
544 | } |
525 | } |
545 | } |