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