ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/los.C
(Generate patch)

Comparing deliantra/server/common/los.C (file contents):
Revision 1.42 by root, Sat Dec 20 02:32:31 2008 UTC vs.
Revision 1.54 by root, Sat Dec 27 07:50:05 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines