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,2010 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> |
… | |
… | |
51 | |
52 | |
52 | // temporary storage for the los algorithm, |
53 | // temporary storage for the los algorithm, |
53 | // one los_info for each lightable map space |
54 | // one los_info for each lightable map space |
54 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
55 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
55 | |
56 | |
56 | struct point |
57 | struct point8 |
57 | { |
58 | { |
58 | sint8 x, y; |
59 | sint8 x, y; |
59 | }; |
60 | }; |
60 | |
61 | |
61 | // minimum size, but must be a power of two |
62 | // minimum size, but must be a power of two |
62 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
63 | #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) |
63 | |
64 | |
64 | // a queue of spaces to calculate |
65 | // a queue of spaces to calculate |
65 | static point queue [QUEUE_LENGTH]; |
66 | static point8 queue [QUEUE_LENGTH]; |
66 | static int q1, q2; // queue start, end |
67 | static int q1, q2; // queue start, end |
67 | |
68 | |
68 | /* |
69 | /* |
69 | * Clears/initialises the los-array associated to the player |
70 | * Clears/initialises the los-array associated to the player |
70 | * controlling the object. |
71 | * controlling the object. |
… | |
… | |
85 | |
86 | |
86 | los_info &l = los[x][y]; |
87 | los_info &l = los[x][y]; |
87 | |
88 | |
88 | l.flags |= flags; |
89 | l.flags |= flags; |
89 | |
90 | |
90 | if (expect_false (l.flags & FLG_QUEUED)) |
91 | if (ecb_expect_false (l.flags & FLG_QUEUED)) |
91 | return; |
92 | return; |
92 | |
93 | |
93 | l.flags |= FLG_QUEUED; |
94 | l.flags |= FLG_QUEUED; |
94 | |
95 | |
95 | queue[q1].x = dx; |
96 | queue[q1].x = dx; |
… | |
… | |
158 | sint8 x = LOS_X0 + dx; |
159 | sint8 x = LOS_X0 + dx; |
159 | sint8 y = LOS_Y0 + dy; |
160 | sint8 y = LOS_Y0 + dy; |
160 | |
161 | |
161 | los_info &l = los[x][y]; |
162 | los_info &l = los[x][y]; |
162 | |
163 | |
163 | if (expect_true (l.flags & (FLG_XI | FLG_YI))) |
164 | if (ecb_expect_true (l.flags & (FLG_XI | FLG_YI))) |
164 | { |
165 | { |
165 | l.culled = 1; |
166 | l.culled = 1; |
166 | l.xo = l.yo = l.xe = l.ye = 0; |
167 | l.xo = l.yo = l.xe = l.ye = 0; |
167 | |
168 | |
168 | // check contributing spaces, first horizontal |
169 | // check contributing spaces, first horizontal |
169 | if (expect_true (l.flags & FLG_XI)) |
170 | if (ecb_expect_true (l.flags & FLG_XI)) |
170 | { |
171 | { |
171 | los_info *xi = &los[x - sign (dx)][y]; |
172 | los_info *xi = &los[x - sign (dx)][y]; |
172 | |
173 | |
173 | // don't cull unless obscured |
174 | // don't cull unless obscured |
174 | l.culled &= !xi->visible; |
175 | l.culled &= !xi->visible; |
175 | |
176 | |
176 | /* merge input space */ |
177 | /* merge input space */ |
177 | if (expect_false (xi->xo || xi->yo)) |
178 | if (ecb_expect_false (xi->xo || xi->yo)) |
178 | { |
179 | { |
179 | // The X input can provide two main pieces of information: |
180 | // The X input can provide two main pieces of information: |
180 | // 1. Progressive X obscurity. |
181 | // 1. Progressive X obscurity. |
181 | // 2. Recessive Y obscurity. |
182 | // 2. Recessive Y obscurity. |
182 | |
183 | |
183 | // Progressive X obscurity, favouring recessive input angle |
184 | // Progressive X obscurity, favouring recessive input angle |
184 | if (xi->xe > 0 && l.xo == 0) |
185 | if (xi->xe > 0 && l.xo == 0) |
… | |
… | |
199 | } |
200 | } |
200 | } |
201 | } |
201 | } |
202 | } |
202 | |
203 | |
203 | // check contributing spaces, last vertical, identical structure |
204 | // check contributing spaces, last vertical, identical structure |
204 | if (expect_true (l.flags & FLG_YI)) |
205 | if (ecb_expect_true (l.flags & FLG_YI)) |
205 | { |
206 | { |
206 | los_info *yi = &los[x][y - sign (dy)]; |
207 | los_info *yi = &los[x][y - sign (dy)]; |
207 | |
208 | |
208 | // don't cull unless obscured |
209 | // don't cull unless obscured |
209 | l.culled &= !yi->visible; |
210 | l.culled &= !yi->visible; |
210 | |
211 | |
211 | /* merge input space */ |
212 | /* merge input space */ |
212 | if (expect_false (yi->yo || yi->xo)) |
213 | if (ecb_expect_false (yi->yo || yi->xo)) |
213 | { |
214 | { |
214 | // The Y input can provide two main pieces of information: |
215 | // The Y input can provide two main pieces of information: |
215 | // 1. Progressive Y obscurity. |
216 | // 1. Progressive Y obscurity. |
216 | // 2. Recessive X obscurity. |
217 | // 2. Recessive X obscurity. |
217 | |
218 | |
218 | // Progressive Y obscurity, favouring recessive input angle |
219 | // Progressive Y obscurity, favouring recessive input angle |
219 | if (yi->ye > 0 && l.yo == 0) |
220 | if (yi->ye > 0 && l.yo == 0) |
… | |
… | |
260 | } |
261 | } |
261 | |
262 | |
262 | } |
263 | } |
263 | |
264 | |
264 | // Expands by the unit length in each component's current direction. |
265 | // Expands by the unit length in each component's current direction. |
265 | // 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 |
266 | // positive and negative directions. |
267 | // positive and negative directions. |
267 | if (!l.culled) |
268 | if (!l.culled) |
268 | { |
269 | { |
269 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
270 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
270 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
271 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
… | |
… | |
351 | for (int ay = ay0; ay <= ay1; ay++) |
352 | for (int ay = ay0; ay <= ay1; ay++) |
352 | pl->los[ax][ay] = |
353 | pl->los[ax][ay] = |
353 | 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)]); |
354 | } |
355 | } |
355 | |
356 | |
356 | /* add light, by finding all (non-null) nearby light sources, then |
357 | /* add light, by finding all (non-null) nearby light sources, then |
357 | * mark those squares specially. |
358 | * mark those squares specially. |
358 | */ |
359 | */ |
359 | static void |
360 | static void |
360 | apply_lights (player *pl) |
361 | apply_lights (player *pl) |
361 | { |
362 | { |
… | |
… | |
398 | { |
399 | { |
399 | mapspace &ms = m->at (nx, ny); |
400 | mapspace &ms = m->at (nx, ny); |
400 | ms.update (); |
401 | ms.update (); |
401 | sint8 light = ms.light; |
402 | sint8 light = ms.light; |
402 | |
403 | |
403 | if (expect_false (light)) |
404 | if (ecb_expect_false (light)) |
404 | if (light < 0) |
405 | if (light < 0) |
405 | pass2 = 1; |
406 | pass2 = 1; |
406 | else |
407 | else |
407 | { |
408 | { |
408 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
409 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
… | |
… | |
440 | { |
441 | { |
441 | mapspace &ms = m->at (nx, ny); |
442 | mapspace &ms = m->at (nx, ny); |
442 | ms.update (); |
443 | ms.update (); |
443 | sint8 light = ms.light; |
444 | sint8 light = ms.light; |
444 | |
445 | |
445 | if (expect_false (light < 0)) |
446 | if (ecb_expect_false (light < 0)) |
446 | { |
447 | { |
447 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
448 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
448 | 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]); |
449 | } |
450 | } |
450 | } |
451 | } |
451 | } |
452 | } |
452 | |
453 | |
453 | /* blinded_sight() - sets all viewable squares to blocked except |
454 | /* blinded_sight() - sets all viewable squares to blocked except |
454 | * 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 |
455 | * 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 |
456 | * really need for any reasonable game play. |
457 | * really need for any reasonable game play. |
457 | */ |
458 | */ |
458 | static void |
459 | static void |