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 | * |
6 | * |
6 | * 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 |
7 | * 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 |
8 | * 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 |
9 | * option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * 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, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * 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 |
17 | * 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 |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * 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> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #include <global.h> |
24 | #include <global.h> |
24 | #include <cmath> |
25 | #include <cmath> |
… | |
… | |
32 | FLG_YI = 0x02, // we have an y-parent |
33 | FLG_YI = 0x02, // we have an y-parent |
33 | FLG_BLOCKED = 0x04, // this space blocks the view |
34 | FLG_BLOCKED = 0x04, // this space blocks the view |
34 | FLG_QUEUED = 0x80 // already queued in queue, or border |
35 | FLG_QUEUED = 0x80 // already queued in queue, or border |
35 | }; |
36 | }; |
36 | |
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. |
37 | struct los_info |
42 | struct los_info |
38 | { |
43 | { |
39 | uint8 flags; // FLG_xxx |
44 | uint8 flags; // FLG_xxx |
40 | uint8 culled; // culled from "tree" |
45 | uint8 culled; // culled from "tree" |
41 | uint8 visible; |
46 | uint8 visible; |
… | |
… | |
47 | |
52 | |
48 | // temporary storage for the los algorithm, |
53 | // temporary storage for the los algorithm, |
49 | // one los_info for each lightable map space |
54 | // one los_info for each lightable map space |
50 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
55 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
51 | |
56 | |
52 | struct point |
57 | struct point8 |
53 | { |
58 | { |
54 | sint8 x, y; |
59 | sint8 x, y; |
55 | }; |
60 | }; |
56 | |
61 | |
57 | // minimum size, but must be a power of two |
62 | // minimum size, but must be a power of two |
58 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
63 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
59 | |
64 | |
60 | // a queue of spaces to calculate |
65 | // a queue of spaces to calculate |
61 | static point queue [QUEUE_LENGTH]; |
66 | static point8 queue [QUEUE_LENGTH]; |
62 | static int q1, q2; // queue start, end |
67 | static int q1, q2; // queue start, end |
63 | |
68 | |
64 | /* |
69 | /* |
65 | * Clears/initialises the los-array associated to the player |
70 | * Clears/initialises the los-array associated to the player |
66 | * controlling the object. |
71 | * controlling the object. |
… | |
… | |
81 | |
86 | |
82 | los_info &l = los[x][y]; |
87 | los_info &l = los[x][y]; |
83 | |
88 | |
84 | l.flags |= flags; |
89 | l.flags |= flags; |
85 | |
90 | |
86 | if (l.flags & FLG_QUEUED) |
91 | if (ecb_expect_false (l.flags & FLG_QUEUED)) |
87 | return; |
92 | return; |
88 | |
93 | |
89 | l.flags |= FLG_QUEUED; |
94 | l.flags |= FLG_QUEUED; |
90 | |
95 | |
91 | queue[q1].x = dx; |
96 | queue[q1].x = dx; |
… | |
… | |
124 | // spiral path algorithm below, except when very little |
129 | // spiral path algorithm below, except when very little |
125 | // area is visible, in which case it is slower. which evens |
130 | // area is visible, in which case it is slower. which evens |
126 | // out los calculation times between large and small los maps. |
131 | // out los calculation times between large and small los maps. |
127 | // apply_lights also iterates over this area, maybe these |
132 | // apply_lights also iterates over this area, maybe these |
128 | // two passes could be combined somehow. |
133 | // two passes could be combined somehow. |
129 | unordered_mapwalk (pl->viewpoint, -half_x, -half_y, half_x, half_y) |
134 | unordered_mapwalk (mapwalk_buf, pl->viewpoint, -half_x, -half_y, half_x, half_y) |
130 | { |
135 | { |
131 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
136 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
132 | 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; |
133 | } |
138 | } |
134 | } |
139 | } |
… | |
… | |
154 | sint8 x = LOS_X0 + dx; |
159 | sint8 x = LOS_X0 + dx; |
155 | sint8 y = LOS_Y0 + dy; |
160 | sint8 y = LOS_Y0 + dy; |
156 | |
161 | |
157 | los_info &l = los[x][y]; |
162 | los_info &l = los[x][y]; |
158 | |
163 | |
159 | if (expect_true (l.flags & (FLG_XI | FLG_YI))) |
164 | if (ecb_expect_true (l.flags & (FLG_XI | FLG_YI))) |
160 | { |
165 | { |
161 | l.culled = 1; |
166 | l.culled = 1; |
162 | l.xo = l.yo = l.xe = l.ye = 0; |
167 | l.xo = l.yo = l.xe = l.ye = 0; |
163 | |
168 | |
164 | // check contributing spaces, first horizontal |
169 | // check contributing spaces, first horizontal |
165 | if (expect_true (l.flags & FLG_XI)) |
170 | if (ecb_expect_true (l.flags & FLG_XI)) |
166 | { |
171 | { |
167 | los_info *xi = &los[x - sign (dx)][y]; |
172 | los_info *xi = &los[x - sign (dx)][y]; |
168 | |
173 | |
169 | // don't cull unless obscured |
174 | // don't cull unless obscured |
170 | l.culled &= !xi->visible; |
175 | l.culled &= !xi->visible; |
171 | |
176 | |
172 | /* merge input space */ |
177 | /* merge input space */ |
173 | if (expect_false (xi->xo || xi->yo)) |
178 | if (ecb_expect_false (xi->xo || xi->yo)) |
174 | { |
179 | { |
175 | // The X input can provide two main pieces of information: |
180 | // The X input can provide two main pieces of information: |
176 | // 1. Progressive X obscurity. |
181 | // 1. Progressive X obscurity. |
177 | // 2. Recessive Y obscurity. |
182 | // 2. Recessive Y obscurity. |
178 | |
183 | |
179 | // Progressive X obscurity, favouring recessive input angle |
184 | // Progressive X obscurity, favouring recessive input angle |
180 | if (xi->xe > 0 && l.xo == 0) |
185 | if (xi->xe > 0 && l.xo == 0) |
… | |
… | |
195 | } |
200 | } |
196 | } |
201 | } |
197 | } |
202 | } |
198 | |
203 | |
199 | // check contributing spaces, last vertical, identical structure |
204 | // check contributing spaces, last vertical, identical structure |
200 | if (expect_true (l.flags & FLG_YI)) |
205 | if (ecb_expect_true (l.flags & FLG_YI)) |
201 | { |
206 | { |
202 | los_info *yi = &los[x][y - sign (dy)]; |
207 | los_info *yi = &los[x][y - sign (dy)]; |
203 | |
208 | |
204 | // don't cull unless obscured |
209 | // don't cull unless obscured |
205 | l.culled &= !yi->visible; |
210 | l.culled &= !yi->visible; |
206 | |
211 | |
207 | /* merge input space */ |
212 | /* merge input space */ |
208 | if (expect_false (yi->yo || yi->xo)) |
213 | if (ecb_expect_false (yi->yo || yi->xo)) |
209 | { |
214 | { |
210 | // The Y input can provide two main pieces of information: |
215 | // The Y input can provide two main pieces of information: |
211 | // 1. Progressive Y obscurity. |
216 | // 1. Progressive Y obscurity. |
212 | // 2. Recessive X obscurity. |
217 | // 2. Recessive X obscurity. |
213 | |
218 | |
214 | // Progressive Y obscurity, favouring recessive input angle |
219 | // Progressive Y obscurity, favouring recessive input angle |
215 | if (yi->ye > 0 && l.yo == 0) |
220 | if (yi->ye > 0 && l.yo == 0) |
… | |
… | |
256 | } |
261 | } |
257 | |
262 | |
258 | } |
263 | } |
259 | |
264 | |
260 | // Expands by the unit length in each component's current direction. |
265 | // Expands by the unit length in each component's current direction. |
261 | // 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 |
262 | // positive and negative directions. |
267 | // positive and negative directions. |
263 | if (!l.culled) |
268 | if (!l.culled) |
264 | { |
269 | { |
265 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
270 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
266 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
271 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
… | |
… | |
301 | for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) |
306 | for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) |
302 | 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; |
303 | } |
308 | } |
304 | } los_init; |
309 | } los_init; |
305 | |
310 | |
|
|
311 | // the following functions cannot be static, due to c++ stupidity :/ |
|
|
312 | namespace { |
306 | // brighten area, ignore los |
313 | // brighten area, ignore los |
307 | sint8 |
314 | sint8 |
308 | los_brighten_nolos (sint8 b, sint8 l) |
315 | los_brighten_nolos (sint8 b, sint8 l) |
309 | { |
316 | { |
310 | return min (b, l); |
317 | return min (b, l); |
311 | } |
318 | } |
312 | |
319 | |
313 | // brighten area, but respect los |
320 | // brighten area, but respect los |
314 | sint8 |
321 | sint8 |
315 | los_brighten (sint8 b, sint8 l) |
322 | los_brighten (sint8 b, sint8 l) |
316 | { |
323 | { |
317 | return b == LOS_BLOCKED ? b : min (b, l); |
324 | return b == LOS_BLOCKED ? b : min (b, l); |
318 | } |
325 | } |
319 | |
326 | |
320 | // darken area, respect los |
327 | // darken area, respect los |
321 | sint8 |
328 | sint8 |
322 | los_darken (sint8 b, sint8 l) |
329 | los_darken (sint8 b, sint8 l) |
323 | { |
330 | { |
324 | return max (b, l); |
331 | return max (b, l); |
325 | } |
332 | } |
|
|
333 | }; |
326 | |
334 | |
327 | template<sint8 change_it (sint8, sint8)> |
335 | template<sint8 change_it (sint8, sint8)> |
328 | static void |
336 | static void |
329 | 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) |
330 | { |
338 | { |
… | |
… | |
344 | for (int ay = ay0; ay <= ay1; ay++) |
352 | for (int ay = ay0; ay <= ay1; ay++) |
345 | pl->los[ax][ay] = |
353 | pl->los[ax][ay] = |
346 | 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)]); |
347 | } |
355 | } |
348 | |
356 | |
349 | /* add light, by finding all (non-null) nearby light sources, then |
357 | /* add light, by finding all (non-null) nearby light sources, then |
350 | * mark those squares specially. |
358 | * mark those squares specially. |
351 | */ |
359 | */ |
352 | static void |
360 | static void |
353 | apply_lights (player *pl) |
361 | apply_lights (player *pl) |
354 | { |
362 | { |
… | |
… | |
359 | int half_y = pl->ns->mapy / 2; |
367 | int half_y = pl->ns->mapy / 2; |
360 | |
368 | |
361 | int pass2 = 0; // negative lights have an extra pass |
369 | int pass2 = 0; // negative lights have an extra pass |
362 | |
370 | |
363 | maprect *rects = pl->viewpoint->map->split_to_tiles ( |
371 | maprect *rects = pl->viewpoint->map->split_to_tiles ( |
|
|
372 | mapwalk_buf, |
364 | pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, |
373 | pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, |
365 | pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, |
374 | pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, |
366 | pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, |
375 | pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, |
367 | pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1 |
376 | pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1 |
368 | ); |
377 | ); |
… | |
… | |
390 | { |
399 | { |
391 | mapspace &ms = m->at (nx, ny); |
400 | mapspace &ms = m->at (nx, ny); |
392 | ms.update (); |
401 | ms.update (); |
393 | sint8 light = ms.light; |
402 | sint8 light = ms.light; |
394 | |
403 | |
395 | if (expect_false (light)) |
404 | if (ecb_expect_false (light)) |
396 | if (light < 0) |
405 | if (light < 0) |
397 | pass2 = 1; |
406 | pass2 = 1; |
398 | else |
407 | else |
399 | { |
408 | { |
400 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
409 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
… | |
… | |
432 | { |
441 | { |
433 | mapspace &ms = m->at (nx, ny); |
442 | mapspace &ms = m->at (nx, ny); |
434 | ms.update (); |
443 | ms.update (); |
435 | sint8 light = ms.light; |
444 | sint8 light = ms.light; |
436 | |
445 | |
437 | if (expect_false (light < 0)) |
446 | if (ecb_expect_false (light < 0)) |
438 | { |
447 | { |
439 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
448 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
440 | apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->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]); |
441 | } |
450 | } |
442 | } |
451 | } |
443 | } |
452 | } |
444 | |
453 | |
445 | /* blinded_sight() - sets all viewable squares to blocked except |
454 | /* blinded_sight() - sets all viewable squares to blocked except |
446 | * 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 |
447 | * 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 |
448 | * really need for any reasonable game play. |
457 | * really need for any reasonable game play. |
449 | */ |
458 | */ |
450 | static void |
459 | static void |
… | |
… | |
526 | if (!pl->ob || !pl->ob->map || !pl->ns) |
535 | if (!pl->ob || !pl->ob->map || !pl->ns) |
527 | continue; |
536 | continue; |
528 | |
537 | |
529 | rv_vector rv; |
538 | rv_vector rv; |
530 | |
539 | |
531 | get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv); |
540 | get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv); |
532 | |
541 | |
533 | 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)) |
534 | pl->do_los = 1; |
543 | pl->do_los = 1; |
535 | } |
544 | } |
536 | } |
545 | } |