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 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 | /* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */ |
|
|
25 | |
|
|
26 | #include <global.h> |
23 | #include <global.h> |
27 | #include <cmath> |
24 | #include <cmath> |
28 | |
25 | |
29 | static void expand_lighted_sight (object *op); |
26 | #define SEE_IN_DARK_RADIUS 2 |
|
|
27 | #define MAX_VISION 10 // maximum visible radius |
30 | |
28 | |
|
|
29 | // los flags |
31 | enum { |
30 | enum { |
32 | LOS_XI = 0x01, |
31 | FLG_XI = 0x01, // we have an x-parent |
33 | LOS_YI = 0x02, |
32 | FLG_YI = 0x02, // we have an y-parent |
|
|
33 | FLG_BLOCKED = 0x04, // this space blocks the view |
|
|
34 | FLG_QUEUED = 0x80 // already queued in queue, or border |
34 | }; |
35 | }; |
35 | |
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. |
36 | struct los_info |
41 | struct los_info |
37 | { |
42 | { |
|
|
43 | uint8 flags; // FLG_xxx |
|
|
44 | uint8 culled; // culled from "tree" |
|
|
45 | uint8 visible; |
|
|
46 | uint8 pad0; |
|
|
47 | |
38 | sint8 xo, yo; // obscure angle |
48 | sint8 xo, yo; // obscure angle |
39 | sint8 xe, ye; // angle deviation |
49 | sint8 xe, ye; // angle deviation |
40 | uint8 culled; // culled from "tree" |
|
|
41 | uint8 queued; // already queued |
|
|
42 | uint8 visible; |
|
|
43 | uint8 flags; // LOS_XI/YI |
|
|
44 | }; |
50 | }; |
45 | |
51 | |
46 | // temporary storage for the los algorithm, |
52 | // temporary storage for the los algorithm, |
47 | // one los_info for each lightable map space |
53 | // one los_info for each lightable map space |
48 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
54 | static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; |
… | |
… | |
75 | enqueue (sint8 dx, sint8 dy, uint8 flags = 0) |
81 | enqueue (sint8 dx, sint8 dy, uint8 flags = 0) |
76 | { |
82 | { |
77 | sint8 x = LOS_X0 + dx; |
83 | sint8 x = LOS_X0 + dx; |
78 | sint8 y = LOS_Y0 + dy; |
84 | sint8 y = LOS_Y0 + dy; |
79 | |
85 | |
80 | if (x < 0 || x >= MAP_CLIENT_X) return; |
|
|
81 | if (y < 0 || y >= MAP_CLIENT_Y) return; |
|
|
82 | |
|
|
83 | los_info &l = los[x][y]; |
86 | los_info &l = los[x][y]; |
84 | |
87 | |
85 | l.flags |= flags; |
88 | l.flags |= flags; |
86 | |
89 | |
87 | if (l.queued) |
90 | if (expect_false (l.flags & FLG_QUEUED)) |
88 | return; |
91 | return; |
89 | |
92 | |
90 | l.queued = 1; |
93 | l.flags |= FLG_QUEUED; |
91 | |
94 | |
92 | queue[q1].x = dx; |
95 | queue[q1].x = dx; |
93 | queue[q1].y = dy; |
96 | queue[q1].y = dy; |
94 | |
97 | |
95 | q1 = (q1 + 1) & (QUEUE_LENGTH - 1); |
98 | q1 = (q1 + 1) & (QUEUE_LENGTH - 1); |
… | |
… | |
99 | // this is a variant of a spiral los algorithm taken from |
102 | // this is a variant of a spiral los algorithm taken from |
100 | // http://www.geocities.com/temerra/los_rays.html |
103 | // http://www.geocities.com/temerra/los_rays.html |
101 | // which has been simplified and changed considerably, but |
104 | // which has been simplified and changed considerably, but |
102 | // still is basically the same algorithm. |
105 | // still is basically the same algorithm. |
103 | static void |
106 | static void |
104 | do_los (object *op) |
107 | calculate_los (player *pl) |
105 | { |
108 | { |
106 | player *pl = op->contr; |
109 | { |
107 | |
|
|
108 | int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2; |
|
|
109 | |
|
|
110 | memset (los, 0, sizeof (los)); |
110 | memset (los, 0, sizeof (los)); |
|
|
111 | |
|
|
112 | // we keep one line for ourselves, for the border flag |
|
|
113 | // so the client area is actually MAP_CLIENT_(X|Y) - 2 |
|
|
114 | int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2); |
|
|
115 | int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2); |
|
|
116 | |
|
|
117 | // create borders, the corners are not touched |
|
|
118 | for (int dx = -half_x; dx <= half_x; ++dx) |
|
|
119 | los [dx + LOS_X0][LOS_Y0 - (half_y + 1)].flags = |
|
|
120 | los [dx + LOS_X0][LOS_Y0 + (half_y + 1)].flags = FLG_QUEUED; |
|
|
121 | |
|
|
122 | for (int dy = -half_y; dy <= half_y; ++dy) |
|
|
123 | los [LOS_X0 - (half_x + 1)][dy + LOS_Y0].flags = |
|
|
124 | los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED; |
|
|
125 | |
|
|
126 | // now reset the los area and also add blocked flags |
|
|
127 | // which supposedly is faster than doing it inside the |
|
|
128 | // spiral path algorithm below, except when very little |
|
|
129 | // area is visible, in which case it is slower. which evens |
|
|
130 | // out los calculation times between large and small los maps. |
|
|
131 | // apply_lights also iterates over this area, maybe these |
|
|
132 | // two passes could be combined somehow. |
|
|
133 | unordered_mapwalk (pl->viewpoint, -half_x, -half_y, half_x, half_y) |
|
|
134 | { |
|
|
135 | los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; |
|
|
136 | l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; |
|
|
137 | } |
|
|
138 | } |
111 | |
139 | |
112 | q1 = 0; q2 = 0; // initialise queue, not strictly required |
140 | q1 = 0; q2 = 0; // initialise queue, not strictly required |
113 | enqueue (0, 0); // enqueue center |
141 | enqueue (0, 0); // enqueue center |
114 | |
142 | |
115 | // treat the origin specially |
143 | // treat the origin specially |
… | |
… | |
128 | q2 = (q2 + 1) & (QUEUE_LENGTH - 1); |
156 | q2 = (q2 + 1) & (QUEUE_LENGTH - 1); |
129 | |
157 | |
130 | sint8 x = LOS_X0 + dx; |
158 | sint8 x = LOS_X0 + dx; |
131 | sint8 y = LOS_Y0 + dy; |
159 | sint8 y = LOS_Y0 + dy; |
132 | |
160 | |
133 | //int distance = idistance (dx, dy); if (distance > max_radius) continue;//D |
|
|
134 | int distance = 0;//D |
|
|
135 | |
|
|
136 | los_info &l = los[x][y]; |
161 | los_info &l = los[x][y]; |
137 | |
162 | |
138 | if (expect_true (l.flags & (LOS_XI | LOS_YI))) |
163 | if (expect_true (l.flags & (FLG_XI | FLG_YI))) |
139 | { |
164 | { |
140 | l.culled = 1; |
165 | l.culled = 1; |
|
|
166 | l.xo = l.yo = l.xe = l.ye = 0; |
141 | |
167 | |
142 | // check contributing spaces, first horizontal |
168 | // check contributing spaces, first horizontal |
143 | if (expect_true (l.flags & LOS_XI)) |
169 | if (expect_true (l.flags & FLG_XI)) |
144 | { |
170 | { |
145 | los_info *xi = &los[x - sign (dx)][y]; |
171 | los_info *xi = &los[x - sign (dx)][y]; |
146 | |
172 | |
147 | // don't cull unless obscured |
173 | // don't cull unless obscured |
148 | l.culled &= !xi->visible; |
174 | l.culled &= !xi->visible; |
… | |
… | |
173 | } |
199 | } |
174 | } |
200 | } |
175 | } |
201 | } |
176 | |
202 | |
177 | // check contributing spaces, last vertical, identical structure |
203 | // check contributing spaces, last vertical, identical structure |
178 | if (expect_true (l.flags & LOS_YI)) |
204 | if (expect_true (l.flags & FLG_YI)) |
179 | { |
205 | { |
180 | los_info *yi = &los[x][y - sign (dy)]; |
206 | los_info *yi = &los[x][y - sign (dy)]; |
181 | |
207 | |
182 | // don't cull unless obscured |
208 | // don't cull unless obscured |
183 | l.culled &= !yi->visible; |
209 | l.culled &= !yi->visible; |
… | |
… | |
207 | l.xo = yi->xo; |
233 | l.xo = yi->xo; |
208 | } |
234 | } |
209 | } |
235 | } |
210 | } |
236 | } |
211 | |
237 | |
212 | // check whether this space blocks the view |
238 | if (l.flags & FLG_BLOCKED) |
213 | maptile *m = op->map; |
|
|
214 | sint16 nx = op->x + dx; |
|
|
215 | sint16 ny = op->y + dy; |
|
|
216 | |
|
|
217 | if (expect_true (!xy_normalise (m, nx, ny)) |
|
|
218 | || expect_false (m->at (nx, ny).flags () & P_BLOCKSVIEW)) |
|
|
219 | { |
239 | { |
220 | l.xo = l.xe = abs (dx); |
240 | l.xo = l.xe = abs (dx); |
221 | l.yo = l.ye = abs (dy); |
241 | l.yo = l.ye = abs (dy); |
222 | |
242 | |
223 | // we obscure dependents, but might be visible |
243 | // we obscure dependents, but might be visible |
224 | // copy the los from the square towards the player, |
244 | // copy the los from the square towards the player, |
225 | // so outward diagonal corners are lit. |
245 | // so outward diagonal corners are lit. |
226 | pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED; |
246 | pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED; |
|
|
247 | |
227 | l.visible = false; |
248 | l.visible = false; |
228 | } |
249 | } |
229 | else |
250 | else |
230 | { |
251 | { |
231 | // we are not blocked, so calculate visibility, by checking |
252 | // we are not blocked, so calculate visibility, by checking |
232 | // whether we are inside or outside the shadow |
253 | // whether we are inside or outside the shadow |
233 | l.visible = (l.xe <= 0 || l.xe > l.xo) |
254 | l.visible = (l.xe <= 0 || l.xe > l.xo) |
234 | && (l.ye <= 0 || l.ye > l.yo); |
255 | && (l.ye <= 0 || l.ye > l.yo); |
235 | |
256 | |
236 | pl->los[x][y] = l.culled ? LOS_BLOCKED |
257 | pl->los[x][y] = l.culled ? LOS_BLOCKED |
237 | : l.visible ? max (0, 2 - max_radius + distance) |
258 | : l.visible ? 0 |
238 | : 3; |
259 | : 3; |
239 | } |
260 | } |
240 | |
261 | |
241 | } |
262 | } |
242 | |
263 | |
243 | // Expands by the unit length in each component's current direction. |
264 | // Expands by the unit length in each component's current direction. |
244 | // If a component has no direction, then it is expanded in both of its |
265 | // If a component has no direction, then it is expanded in both of its |
245 | // positive and negative directions. |
266 | // positive and negative directions. |
246 | if (!l.culled) |
267 | if (!l.culled) |
247 | { |
268 | { |
248 | if (dx >= 0) enqueue (dx + 1, dy, LOS_XI); |
269 | if (dx >= 0) enqueue (dx + 1, dy, FLG_XI); |
249 | if (dx <= 0) enqueue (dx - 1, dy, LOS_XI); |
270 | if (dx <= 0) enqueue (dx - 1, dy, FLG_XI); |
250 | if (dy >= 0) enqueue (dx, dy + 1, LOS_YI); |
271 | if (dy >= 0) enqueue (dx, dy + 1, FLG_YI); |
251 | if (dy <= 0) enqueue (dx, dy - 1, LOS_YI); |
272 | if (dy <= 0) enqueue (dx, dy - 1, FLG_YI); |
252 | } |
273 | } |
253 | } |
274 | } |
254 | } |
275 | } |
255 | |
276 | |
256 | /* returns true if op carries one or more lights |
|
|
257 | * This is a trivial function now days, but it used to |
|
|
258 | * be a bit longer. Probably better for callers to just |
|
|
259 | * check the op->glow_radius instead of calling this. |
|
|
260 | */ |
|
|
261 | int |
|
|
262 | has_carried_lights (const object *op) |
|
|
263 | { |
|
|
264 | /* op may glow! */ |
|
|
265 | if (op->glow_radius > 0) |
|
|
266 | return 1; |
|
|
267 | |
|
|
268 | return 0; |
|
|
269 | } |
|
|
270 | |
|
|
271 | /* radius, distance => lightness adjust */ |
277 | /* radius, distance => lightness adjust */ |
272 | 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]; |
273 | static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; |
279 | static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1]; |
274 | |
280 | |
275 | static struct los_init |
281 | static struct los_init |
276 | { |
282 | { |
277 | los_init () |
283 | los_init () |
278 | { |
284 | { |
|
|
285 | assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two", |
|
|
286 | !(QUEUE_LENGTH & (QUEUE_LENGTH - 1)))); |
|
|
287 | |
279 | /* for lights */ |
288 | /* for lights */ |
280 | for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius) |
289 | for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius) |
281 | for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance) |
290 | for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance) |
282 | { |
291 | { |
283 | // max intensity |
292 | // max intensity |
284 | int intensity = min (LOS_MAX, abs (radius) + 1); |
293 | int intensity = min (LOS_MAX, abs (radius) + 1); |
285 | |
294 | |
286 | // actual intensity |
295 | // actual intensity |
287 | 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)); |
288 | |
297 | |
289 | light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 |
298 | light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 |
290 | ? min (3, intensity) |
299 | ? min (3, intensity) |
291 | : LOS_MAX - intensity; |
300 | : LOS_MAX - intensity; |
292 | } |
301 | } |
293 | |
302 | |
294 | /* for general vision */ |
303 | /* for general vision */ |
295 | for (int radius = 0; radius <= MAX_DARKNESS; ++radius) |
304 | for (int radius = 0; radius <= MAX_VISION; ++radius) |
296 | for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) |
305 | for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) |
297 | { |
306 | vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4; |
298 | vision_atten [radius][distance] = distance <= radius ? 3 : 4; |
|
|
299 | } |
|
|
300 | } |
307 | } |
301 | } los_init; |
308 | } los_init; |
302 | |
309 | |
|
|
310 | // the following functions cannot be static, due to c++ stupidity :/ |
|
|
311 | namespace { |
|
|
312 | // brighten area, ignore los |
303 | 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 |
304 | los_brighten (sint8 b, sint8 l) |
321 | los_brighten (sint8 b, sint8 l) |
305 | { |
322 | { |
306 | return b == LOS_BLOCKED ? b : min (b, l); |
323 | return b == LOS_BLOCKED ? b : min (b, l); |
307 | } |
324 | } |
308 | |
325 | |
|
|
326 | // darken area, respect los |
309 | sint8 |
327 | sint8 |
310 | los_darken (sint8 b, sint8 l) |
328 | los_darken (sint8 b, sint8 l) |
311 | { |
329 | { |
312 | return max (b, l); |
330 | return max (b, l); |
313 | } |
331 | } |
|
|
332 | }; |
314 | |
333 | |
315 | template<sint8 change_it (sint8, sint8)> |
334 | template<sint8 change_it (sint8, sint8)> |
316 | static void |
335 | static void |
317 | apply_light (object *op, 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) |
318 | { |
337 | { |
319 | // min or max the circular area around basex, basey |
338 | // min or max the circular area around basex, basey |
320 | player *pl = op->contr; |
|
|
321 | |
|
|
322 | dx += LOS_X0; |
339 | dx += LOS_X0; |
323 | dy += LOS_Y0; |
340 | dy += LOS_Y0; |
324 | |
341 | |
325 | int hx = op->contr->ns->mapx / 2; |
342 | int hx = pl->ns->mapx / 2; |
326 | int hy = op->contr->ns->mapy / 2; |
343 | int hy = pl->ns->mapy / 2; |
327 | |
344 | |
328 | int ax0 = max (LOS_X0 - hx, dx - light); |
345 | int ax0 = max (LOS_X0 - hx, dx - light); |
329 | int ay0 = max (LOS_Y0 - hy, dy - light); |
346 | int ay0 = max (LOS_Y0 - hy, dy - light); |
330 | int ax1 = min (dx + light, LOS_X0 + hx); |
347 | int ax1 = min (dx + light, LOS_X0 + hx); |
331 | int ay1 = min (dy + light, LOS_Y0 + hy); |
348 | int ay1 = min (dy + light, LOS_Y0 + hy); |
… | |
… | |
338 | |
355 | |
339 | /* add light, by finding all (non-null) nearby light sources, then |
356 | /* add light, by finding all (non-null) nearby light sources, then |
340 | * mark those squares specially. |
357 | * mark those squares specially. |
341 | */ |
358 | */ |
342 | static void |
359 | static void |
343 | apply_lights (object *op) |
360 | apply_lights (player *pl) |
344 | { |
361 | { |
345 | int darklevel, mflags, light, x1, y1; |
362 | object *op = pl->viewpoint; |
346 | maptile *m = op->map; |
363 | int darklevel = op->map->darklevel (); |
347 | sint16 nx, ny; |
|
|
348 | |
364 | |
349 | darklevel = m->darkness; |
|
|
350 | |
|
|
351 | /* If the player can see in the dark, lower the darklevel for him */ |
|
|
352 | if (QUERY_FLAG (op, FLAG_SEE_IN_DARK)) |
|
|
353 | darklevel -= LOS_MAX / 2; |
|
|
354 | |
|
|
355 | /* Do a sanity check. If not valid, some code below may do odd |
|
|
356 | * things. |
|
|
357 | */ |
|
|
358 | if (darklevel > MAX_DARKNESS) |
|
|
359 | { |
|
|
360 | LOG (llevError, "Map darkness for %s on %s is too high (%d)\n", &op->name, &op->map->path, darklevel); |
|
|
361 | darklevel = MAX_DARKNESS; |
|
|
362 | } |
|
|
363 | |
|
|
364 | int half_x = op->contr->ns->mapx / 2; |
365 | int half_x = pl->ns->mapx / 2; |
365 | int half_y = op->contr->ns->mapy / 2; |
366 | int half_y = pl->ns->mapy / 2; |
366 | |
|
|
367 | int min_x = op->x - half_x - MAX_LIGHT_RADIUS; |
|
|
368 | int min_y = op->y - half_y - MAX_LIGHT_RADIUS; |
|
|
369 | int max_x = op->x + half_x + MAX_LIGHT_RADIUS; |
|
|
370 | int max_y = op->y + half_y + MAX_LIGHT_RADIUS; |
|
|
371 | |
367 | |
372 | int pass2 = 0; // negative lights have an extra pass |
368 | int pass2 = 0; // negative lights have an extra pass |
373 | |
369 | |
|
|
370 | maprect *rects = pl->viewpoint->map->split_to_tiles ( |
|
|
371 | pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, |
|
|
372 | pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, |
|
|
373 | pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, |
|
|
374 | pl->viewpoint->y + half_y + MAX_LIGHT_RADIUS + 1 |
|
|
375 | ); |
|
|
376 | |
|
|
377 | /* If the player can see in the dark, increase light/vision radius */ |
|
|
378 | int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0; |
|
|
379 | |
374 | if (darklevel < 1) |
380 | if (!darklevel) |
375 | pass2 = 1; |
381 | pass2 = 1; |
376 | else |
382 | else |
377 | { |
383 | { |
378 | /* first, make everything totally dark */ |
384 | /* first, make everything totally dark */ |
379 | for (int dx = -half_x; dx <= half_x; dx++) |
385 | for (int dx = -half_x; dx <= half_x; dx++) |
380 | for (int dy = -half_x; dy <= half_y; dy++) |
386 | for (int dy = -half_x; dy <= half_y; dy++) |
381 | if (op->contr->los[dx + LOS_X0][dy + LOS_Y0] != LOS_BLOCKED) |
|
|
382 | op->contr->los[dx + LOS_X0][dy + LOS_Y0] = LOS_MAX; |
387 | max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX); |
383 | |
388 | |
384 | /* |
389 | /* |
385 | * Only process the area of interest. |
390 | * Only process the area of interest. |
386 | * the basex, basey values represent the position in the op->contr->los |
391 | * the basex, basey values represent the position in the op->contr->los |
387 | * array. Its easier to just increment them here (and start with the right |
392 | * array. Its easier to just increment them here (and start with the right |
388 | * value) than to recalculate them down below. |
393 | * value) than to recalculate them down below. |
389 | */ |
394 | */ |
390 | for (int x = min_x; x <= max_x; x++) |
395 | for (maprect *r = rects; r->m; ++r) |
391 | for (int y = min_y; y <= max_y; y++) |
396 | rect_mapwalk (r, 0, 0) |
392 | { |
397 | { |
393 | maptile *m = op->map; |
|
|
394 | sint16 nx = x; |
|
|
395 | sint16 ny = y; |
|
|
396 | |
|
|
397 | if (!xy_normalise (m, nx, ny)) |
|
|
398 | continue; |
|
|
399 | |
|
|
400 | mapspace &ms = m->at (nx, ny); |
398 | mapspace &ms = m->at (nx, ny); |
401 | ms.update (); |
399 | ms.update (); |
402 | sint8 light = ms.light; |
400 | sint8 light = ms.light; |
403 | |
401 | |
404 | if (expect_false (light)) |
402 | if (expect_false (light)) |
405 | if (light < 0) |
403 | if (light < 0) |
406 | pass2 = 1; |
404 | pass2 = 1; |
407 | else |
405 | else |
|
|
406 | { |
|
|
407 | light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); |
408 | apply_light<los_brighten> (op, x - op->x, y - op->y, light, light_atten [light + MAX_LIGHT_RADIUS]); |
408 | apply_light<los_brighten> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, light, light_atten [light + MAX_LIGHT_RADIUS]); |
|
|
409 | } |
409 | } |
410 | } |
410 | |
411 | |
411 | /* grant some vision to the player, based on the darklevel */ |
412 | /* grant some vision to the player, based on outside, outdoor, and darklevel */ |
412 | { |
413 | { |
413 | int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); |
414 | int light; |
414 | |
415 | |
|
|
416 | if (!op->map->outdoor) // not outdoor, darkness becomes light radius |
|
|
417 | light = MAX_DARKNESS - op->map->darkness; |
|
|
418 | else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius |
|
|
419 | light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0); |
|
|
420 | else // outdoor and darkness <= 0 => start wide and decrease quickly |
|
|
421 | light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2); |
|
|
422 | |
|
|
423 | light = clamp (light + bonus, 0, MAX_VISION); |
|
|
424 | |
415 | apply_light<los_brighten> (op, 0, 0, light, vision_atten [light]); |
425 | apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); |
416 | } |
426 | } |
417 | } |
427 | } |
|
|
428 | |
|
|
429 | // when we fly high, we have some minimum viewable area around us, like x-ray |
|
|
430 | if (op->move_type & MOVE_FLY_HIGH) |
|
|
431 | apply_light<los_brighten_nolos> (pl, 0, 0, 9, vision_atten [9]); |
418 | |
432 | |
419 | // possibly do 2nd pass for rare negative glow radii |
433 | // possibly do 2nd pass for rare negative glow radii |
420 | // for effect, those are always considered to be stronger than anything else |
434 | // for effect, those are always considered to be stronger than anything else |
421 | // but they can't darken a place completely |
435 | // but they can't darken a place completely |
422 | if (pass2) |
436 | if (pass2) |
423 | for (int x = min_x; x <= max_x; x++) |
437 | for (maprect *r = rects; r->m; ++r) |
424 | for (int y = min_y; y <= max_y; y++) |
438 | rect_mapwalk (r, 0, 0) |
425 | { |
439 | { |
426 | maptile *m = op->map; |
|
|
427 | sint16 nx = x; |
|
|
428 | sint16 ny = y; |
|
|
429 | |
|
|
430 | if (!xy_normalise (m, nx, ny)) |
|
|
431 | continue; |
|
|
432 | |
|
|
433 | mapspace &ms = m->at (nx, ny); |
440 | mapspace &ms = m->at (nx, ny); |
434 | ms.update (); |
441 | ms.update (); |
435 | sint8 light = ms.light; |
442 | sint8 light = ms.light; |
436 | |
443 | |
437 | if (expect_false (light < 0)) |
444 | if (expect_false (light < 0)) |
|
|
445 | { |
|
|
446 | light = clamp (light - bonus, 0, MAX_DARKNESS); |
438 | apply_light<los_darken> (op, x - op->x, y - op->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); |
447 | apply_light<los_darken> (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); |
|
|
448 | } |
439 | } |
449 | } |
440 | } |
450 | } |
441 | |
451 | |
442 | /* blinded_sight() - sets all viewable squares to blocked except |
452 | /* blinded_sight() - sets all viewable squares to blocked except |
443 | * for the one the central one that the player occupies. A little |
453 | * for the one the central one that the player occupies. A little |
444 | * odd that you can see yourself (and what your standing on), but |
454 | * odd that you can see yourself (and what your standing on), but |
445 | * really need for any reasonable game play. |
455 | * really need for any reasonable game play. |
446 | */ |
456 | */ |
447 | static void |
457 | static void |
448 | blinded_sight (object *op) |
458 | blinded_sight (player *pl) |
449 | { |
459 | { |
450 | op->contr->los[LOS_X0][LOS_Y0] = 1; |
460 | pl->los[LOS_X0][LOS_Y0] = 1; |
451 | } |
461 | } |
452 | |
462 | |
453 | /* |
463 | /* |
454 | * update_los() recalculates the array which specifies what is |
464 | * update_los() recalculates the array which specifies what is |
455 | * visible for the given player-object. |
465 | * visible for the given player-object. |
456 | */ |
466 | */ |
457 | void |
467 | void |
458 | update_los (object *op) |
468 | player::update_los () |
459 | { |
469 | { |
460 | if (QUERY_FLAG (op, FLAG_REMOVED)) |
470 | if (ob->flag [FLAG_REMOVED])//D really needed? |
461 | return; |
471 | return; |
462 | |
472 | |
463 | op->contr->clear_los (); |
473 | if (ob->flag [FLAG_WIZLOOK]) |
464 | |
474 | clear_los (0); |
465 | if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ ) |
|
|
466 | memset (op->contr->los, 0, sizeof (op->contr->los)); |
|
|
467 | else if (QUERY_FLAG (op, FLAG_BLIND)) /* player is blind */ |
475 | else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */ |
|
|
476 | { |
|
|
477 | clear_los (); |
468 | blinded_sight (op); |
478 | blinded_sight (this); |
|
|
479 | } |
469 | else |
480 | else |
470 | { |
481 | { |
471 | do_los (op); |
482 | clear_los (); |
|
|
483 | calculate_los (this); |
472 | apply_lights (op); |
484 | apply_lights (this); |
473 | } |
485 | } |
474 | |
486 | |
475 | if (QUERY_FLAG (op, FLAG_XRAYS)) |
487 | if (viewpoint->flag [FLAG_XRAYS]) |
476 | for (int dx = -2; dx <= 2; dx++) |
488 | for (int dx = -2; dx <= 2; dx++) |
477 | for (int dy = -2; dy <= 2; dy++) |
489 | for (int dy = -2; dy <= 2; dy++) |
478 | op->contr->los[dx + LOS_X0][dy + LOS_X0] = 0; |
490 | min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); |
479 | } |
491 | } |
480 | |
492 | |
481 | /* update all_map_los is like update_all_los below, |
493 | /* update all_map_los is like update_all_los below, |
482 | * but updates everyone on the map, no matter where they |
494 | * but updates everyone on the map, no matter where they |
483 | * are. This generally should not be used, as a per |
495 | * are. This generally should not be used, as a per |
… | |
… | |
490 | * change_map_light function |
502 | * change_map_light function |
491 | */ |
503 | */ |
492 | void |
504 | void |
493 | update_all_map_los (maptile *map) |
505 | update_all_map_los (maptile *map) |
494 | { |
506 | { |
495 | for_all_players (pl) |
507 | for_all_players_on_map (pl, map) |
496 | if (pl->ob && pl->ob->map == map) |
|
|
497 | pl->do_los = 1; |
508 | pl->do_los = 1; |
498 | } |
509 | } |
499 | |
510 | |
500 | /* |
511 | /* |
501 | * This function makes sure that update_los() will be called for all |
512 | * This function makes sure that update_los() will be called for all |
502 | * players on the given map within the next frame. |
513 | * players on the given map within the next frame. |
… | |
… | |
510 | * map is the map that changed, x and y are the coordinates. |
521 | * map is the map that changed, x and y are the coordinates. |
511 | */ |
522 | */ |
512 | void |
523 | void |
513 | update_all_los (const maptile *map, int x, int y) |
524 | update_all_los (const maptile *map, int x, int y) |
514 | { |
525 | { |
|
|
526 | map->at (x, y).invalidate (); |
|
|
527 | |
515 | for_all_players (pl) |
528 | for_all_players (pl) |
516 | { |
529 | { |
517 | /* Player should not have a null map, but do this |
530 | /* Player should not have a null map, but do this |
518 | * check as a safety |
531 | * check as a safety |
519 | */ |
532 | */ |
520 | if (!pl->ob || !pl->ob->map || !pl->ns) |
533 | if (!pl->ob || !pl->ob->map || !pl->ns) |
521 | continue; |
534 | continue; |
522 | |
535 | |
523 | /* Same map is simple case - see if pl is close enough. |
536 | rv_vector rv; |
524 | * Note in all cases, we did the check for same map first, |
537 | |
525 | * and then see if the player is close enough and update |
538 | get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv); |
526 | * los if that is the case. If the player is on the |
|
|
527 | * corresponding map, but not close enough, then the |
|
|
528 | * player can't be on another map that may be closer, |
|
|
529 | * so by setting it up this way, we trim processing |
|
|
530 | * some. |
|
|
531 | */ |
539 | |
532 | if (pl->ob->map == map) |
540 | if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) |
533 | { |
|
|
534 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
535 | pl->do_los = 1; |
541 | pl->do_los = 1; |
536 | } |
|
|
537 | |
|
|
538 | /* Now we check to see if player is on adjacent |
|
|
539 | * maps to the one that changed and also within |
|
|
540 | * view. The tile_maps[] could be null, but in that |
|
|
541 | * case it should never match the pl->ob->map, so |
|
|
542 | * we want ever try to dereference any of the data in it. |
|
|
543 | * |
|
|
544 | * The logic for 0 and 3 is to see how far the player is |
|
|
545 | * from the edge of the map (height/width) - pl->ob->(x,y) |
|
|
546 | * and to add current position on this map - that gives a |
|
|
547 | * distance. |
|
|
548 | * For 1 and 2, we check to see how far the given |
|
|
549 | * coordinate (x,y) is from the corresponding edge, |
|
|
550 | * and then add the players location, which gives |
|
|
551 | * a distance. |
|
|
552 | */ |
|
|
553 | else if (pl->ob->map == map->tile_map[0]) |
|
|
554 | { |
|
|
555 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2)) |
|
|
556 | pl->do_los = 1; |
|
|
557 | } |
|
|
558 | else if (pl->ob->map == map->tile_map[2]) |
|
|
559 | { |
|
|
560 | if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2)) |
|
|
561 | pl->do_los = 1; |
|
|
562 | } |
|
|
563 | else if (pl->ob->map == map->tile_map[1]) |
|
|
564 | { |
|
|
565 | if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
566 | pl->do_los = 1; |
|
|
567 | } |
|
|
568 | else if (pl->ob->map == map->tile_map[3]) |
|
|
569 | { |
|
|
570 | if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) |
|
|
571 | pl->do_los = 1; |
|
|
572 | } |
|
|
573 | } |
542 | } |
|
|
543 | } |
|
|
544 | |
|
|
545 | static const int season_darkness[5][HOURS_PER_DAY] = { |
|
|
546 | /*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 | { 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5 }, |
|
|
548 | { 5, 5, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4 }, |
|
|
549 | { 5, 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4 }, |
|
|
550 | { 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4 }, |
|
|
551 | { 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4 } |
|
|
552 | }; |
|
|
553 | |
|
|
554 | /* |
|
|
555 | * Tell players the time and compute the darkness level for all maps in the game. |
|
|
556 | * MUST be called exactly once per hour. |
|
|
557 | */ |
|
|
558 | void |
|
|
559 | maptile::adjust_daylight () |
|
|
560 | { |
|
|
561 | timeofday_t tod; |
|
|
562 | |
|
|
563 | get_tod (&tod); |
|
|
564 | |
|
|
565 | // log the time to log-1 every hour, and to chat every day |
|
|
566 | { |
|
|
567 | char todbuf[512]; |
|
|
568 | |
|
|
569 | format_tod (todbuf, sizeof (todbuf), &tod); |
|
|
570 | |
|
|
571 | for_all_players (pl) |
|
|
572 | pl->ns->send_msg (NDI_GREY, tod.hour == 15 ? CHAT_CHANNEL : LOG_CHANNEL, todbuf); |
|
|
573 | } |
|
|
574 | |
|
|
575 | /* If the light level isn't changing, no reason to do all |
|
|
576 | * the work below. |
|
|
577 | */ |
|
|
578 | sint8 new_darkness = season_darkness[tod.season][tod.hour]; |
|
|
579 | |
|
|
580 | if (new_darkness == maptile::outdoor_darkness) |
|
|
581 | return; |
|
|
582 | |
|
|
583 | new_draw_info (NDI_GREY | NDI_UNIQUE | NDI_ALL, 1, 0, |
|
|
584 | new_darkness > maptile::outdoor_darkness |
|
|
585 | ? "It becomes darker." |
|
|
586 | : "It becomes brighter."); |
|
|
587 | |
|
|
588 | maptile::outdoor_darkness = new_darkness; |
|
|
589 | |
|
|
590 | // we simply update the los for all players, which is unnecessarily |
|
|
591 | // costly, but should do for the moment. |
|
|
592 | for_all_players (pl) |
|
|
593 | pl->do_los = 1; |
574 | } |
594 | } |
575 | |
595 | |
576 | /* |
596 | /* |
577 | * make_sure_seen: The object is supposed to be visible through walls, thus |
597 | * make_sure_seen: The object is supposed to be visible through walls, thus |
578 | * check if any players are nearby, and edit their LOS array. |
598 | * check if any players are nearby, and edit their LOS array. |
… | |
… | |
582 | { |
602 | { |
583 | for_all_players (pl) |
603 | for_all_players (pl) |
584 | if (pl->ob->map == op->map && |
604 | if (pl->ob->map == op->map && |
585 | pl->ob->y - pl->ns->mapy / 2 <= op->y && |
605 | pl->ob->y - pl->ns->mapy / 2 <= op->y && |
586 | 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) |
606 | 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) |
587 | pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_X0] = 0; |
607 | pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0; |
588 | } |
608 | } |
589 | |
609 | |
590 | /* |
610 | /* |
591 | * make_sure_not_seen: The object which is supposed to be visible through |
611 | * make_sure_not_seen: The object which is supposed to be visible through |
592 | * walls has just been removed from the map, so update the los of any |
612 | * walls has just been removed from the map, so update the los of any |
… | |
… | |
599 | if (pl->ob->map == op->map && |
619 | if (pl->ob->map == op->map && |
600 | pl->ob->y - pl->ns->mapy / 2 <= op->y && |
620 | pl->ob->y - pl->ns->mapy / 2 <= op->y && |
601 | 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) |
621 | 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) |
602 | pl->do_los = 1; |
622 | pl->do_los = 1; |
603 | } |
623 | } |
|
|
624 | |