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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines