ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/los.C
Revision: 1.47
Committed: Tue Dec 23 01:51:27 2008 UTC (15 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.46: +56 -3 lines
Log Message:
rmeove the last remnants of the weather code

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 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 *
8 * Deliantra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */
25
26 #include <global.h>
27 #include <cmath>
28
29 static void expand_lighted_sight (object *op);
30
31 enum {
32 LOS_XI = 0x01,
33 LOS_YI = 0x02,
34 };
35
36 struct los_info
37 {
38 sint8 xo, yo; // obscure angle
39 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
46 // temporary storage for the los algorithm,
47 // one los_info for each lightable map space
48 static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y];
49
50 struct point
51 {
52 sint8 x, y;
53 };
54
55 // minimum size, but must be a power of two
56 #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2)
57
58 // a queue of spaces to calculate
59 static point queue [QUEUE_LENGTH];
60 static int q1, q2; // queue start, end
61
62 /*
63 * Clears/initialises the los-array associated to the player
64 * controlling the object.
65 */
66 void
67 player::clear_los (sint8 value)
68 {
69 memset (los, value, sizeof (los));
70 }
71
72 // enqueue a single mapspace, but only if it hasn't
73 // been enqueued yet.
74 static void
75 enqueue (sint8 dx, sint8 dy, uint8 flags = 0)
76 {
77 sint8 x = LOS_X0 + dx;
78 sint8 y = LOS_Y0 + dy;
79
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];
84
85 l.flags |= flags;
86
87 if (l.queued)
88 return;
89
90 l.queued = 1;
91
92 queue[q1].x = dx;
93 queue[q1].y = dy;
94
95 q1 = (q1 + 1) & (QUEUE_LENGTH - 1);
96 }
97
98 // run the los algorithm
99 // this is a variant of a spiral los algorithm taken from
100 // http://www.geocities.com/temerra/los_rays.html
101 // which has been simplified and changed considerably, but
102 // still is basically the same algorithm.
103 static void
104 do_los (object *op)
105 {
106 player *pl = op->contr;
107
108 int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2;
109
110 memset (los, 0, sizeof (los));
111
112 q1 = 0; q2 = 0; // initialise queue, not strictly required
113 enqueue (0, 0); // enqueue center
114
115 // treat the origin specially
116 los[LOS_X0][LOS_Y0].visible = 1;
117 pl->los[LOS_X0][LOS_Y0] = 0;
118
119 // loop over all enqueued points until the queue is empty
120 // the order in which this is done ensures that we
121 // never touch a mapspace whose input spaces we haven't checked
122 // yet.
123 while (q1 != q2)
124 {
125 sint8 dx = queue[q2].x;
126 sint8 dy = queue[q2].y;
127
128 q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
129
130 sint8 x = LOS_X0 + dx;
131 sint8 y = LOS_Y0 + dy;
132
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];
137
138 if (expect_true (l.flags & (LOS_XI | LOS_YI)))
139 {
140 l.culled = 1;
141
142 // check contributing spaces, first horizontal
143 if (expect_true (l.flags & LOS_XI))
144 {
145 los_info *xi = &los[x - sign (dx)][y];
146
147 // don't cull unless obscured
148 l.culled &= !xi->visible;
149
150 /* merge input space */
151 if (expect_false (xi->xo || xi->yo))
152 {
153 // The X input can provide two main pieces of information:
154 // 1. Progressive X obscurity.
155 // 2. Recessive Y obscurity.
156
157 // Progressive X obscurity, favouring recessive input angle
158 if (xi->xe > 0 && l.xo == 0)
159 {
160 l.xe = xi->xe - xi->yo;
161 l.ye = xi->ye + xi->yo;
162 l.xo = xi->xo;
163 l.yo = xi->yo;
164 }
165
166 // Recessive Y obscurity
167 if (xi->ye <= 0 && xi->yo > 0 && xi->xe > 0)
168 {
169 l.ye = xi->yo + xi->ye;
170 l.xe = xi->xe - xi->yo;
171 l.xo = xi->xo;
172 l.yo = xi->yo;
173 }
174 }
175 }
176
177 // check contributing spaces, last vertical, identical structure
178 if (expect_true (l.flags & LOS_YI))
179 {
180 los_info *yi = &los[x][y - sign (dy)];
181
182 // don't cull unless obscured
183 l.culled &= !yi->visible;
184
185 /* merge input space */
186 if (expect_false (yi->yo || yi->xo))
187 {
188 // The Y input can provide two main pieces of information:
189 // 1. Progressive Y obscurity.
190 // 2. Recessive X obscurity.
191
192 // Progressive Y obscurity, favouring recessive input angle
193 if (yi->ye > 0 && l.yo == 0)
194 {
195 l.ye = yi->ye - yi->xo;
196 l.xe = yi->xe + yi->xo;
197 l.yo = yi->yo;
198 l.xo = yi->xo;
199 }
200
201 // Recessive X obscurity
202 if (yi->xe <= 0 && yi->xo > 0 && yi->ye > 0)
203 {
204 l.xe = yi->xo + yi->xe;
205 l.ye = yi->ye - yi->xo;
206 l.yo = yi->yo;
207 l.xo = yi->xo;
208 }
209 }
210 }
211
212 // check whether this space blocks the view
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 {
220 l.xo = l.xe = abs (dx);
221 l.yo = l.ye = abs (dy);
222
223 // we obscure dependents, but might be visible
224 // copy the los from the square towards the player,
225 // so outward diagonal corners are lit.
226 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED;
227 l.visible = false;
228 }
229 else
230 {
231 // we are not blocked, so calculate visibility, by checking
232 // whether we are inside or outside the shadow
233 l.visible = (l.xe <= 0 || l.xe > l.xo)
234 && (l.ye <= 0 || l.ye > l.yo);
235
236 pl->los[x][y] = l.culled ? LOS_BLOCKED
237 : l.visible ? max (0, 2 - max_radius + distance)
238 : 3;
239 }
240
241 }
242
243 // 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
245 // positive and negative directions.
246 if (!l.culled)
247 {
248 if (dx >= 0) enqueue (dx + 1, dy, LOS_XI);
249 if (dx <= 0) enqueue (dx - 1, dy, LOS_XI);
250 if (dy >= 0) enqueue (dx, dy + 1, LOS_YI);
251 if (dy <= 0) enqueue (dx, dy - 1, LOS_YI);
252 }
253 }
254 }
255
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 */
272 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];
274
275 static struct los_init
276 {
277 los_init ()
278 {
279 /* for lights */
280 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius)
281 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance)
282 {
283 // max intensity
284 int intensity = min (LOS_MAX, abs (radius) + 1);
285
286 // actual intensity
287 intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0));
288
289 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
290 ? min (3, intensity)
291 : LOS_MAX - intensity;
292 }
293
294 /* for general vision */
295 for (int radius = 0; radius <= MAX_DARKNESS; ++radius)
296 for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance)
297 {
298 vision_atten [radius][distance] = distance <= radius ? 3 : 4;
299 }
300 }
301 } los_init;
302
303 sint8
304 los_brighten (sint8 b, sint8 l)
305 {
306 return b == LOS_BLOCKED ? b : min (b, l);
307 }
308
309 sint8
310 los_darken (sint8 b, sint8 l)
311 {
312 return max (b, l);
313 }
314
315 template<sint8 change_it (sint8, sint8)>
316 static void
317 apply_light (object *op, int dx, int dy, int light, const sint8 *atten_table)
318 {
319 // min or max the circular area around basex, basey
320 player *pl = op->contr;
321
322 dx += LOS_X0;
323 dy += LOS_Y0;
324
325 int hx = op->contr->ns->mapx / 2;
326 int hy = op->contr->ns->mapy / 2;
327
328 int ax0 = max (LOS_X0 - hx, dx - light);
329 int ay0 = max (LOS_Y0 - hy, dy - light);
330 int ax1 = min (dx + light, LOS_X0 + hx);
331 int ay1 = min (dy + light, LOS_Y0 + hy);
332
333 for (int ax = ax0; ax <= ax1; ax++)
334 for (int ay = ay0; ay <= ay1; ay++)
335 pl->los[ax][ay] =
336 change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]);
337 }
338
339 /* add light, by finding all (non-null) nearby light sources, then
340 * mark those squares specially.
341 */
342 static void
343 apply_lights (object *op)
344 {
345 int darklevel, mflags, light, x1, y1;
346 maptile *m = op->map;
347 sint16 nx, ny;
348
349 darklevel = m->darklevel ();
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 -= 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_y = op->contr->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
372 int pass2 = 0; // negative lights have an extra pass
373
374 if (darklevel < 1)
375 pass2 = 1;
376 else
377 {
378 /* first, make everything totally dark */
379 for (int dx = -half_x; dx <= half_x; dx++)
380 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;
383
384 /*
385 * Only process the area of interest.
386 * 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
388 * value) than to recalculate them down below.
389 */
390 for (int x = min_x; x <= max_x; x++)
391 for (int y = min_y; y <= max_y; y++)
392 {
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);
401 ms.update ();
402 sint8 light = ms.light;
403
404 if (expect_false (light))
405 if (light < 0)
406 pass2 = 1;
407 else
408 apply_light<los_brighten> (op, x - op->x, y - op->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
409 }
410
411 /* grant some vision to the player, based on the darklevel */
412 {
413 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS);
414
415 apply_light<los_brighten> (op, 0, 0, light, vision_atten [light]);
416 }
417 }
418
419 // possibly do 2nd pass for rare negative glow radii
420 // for effect, those are always considered to be stronger than anything else
421 // but they can't darken a place completely
422 if (pass2)
423 for (int x = min_x; x <= max_x; x++)
424 for (int y = min_y; y <= max_y; y++)
425 {
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);
434 ms.update ();
435 sint8 light = ms.light;
436
437 if (expect_false (light < 0))
438 apply_light<los_darken> (op, x - op->x, y - op->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
439 }
440 }
441
442 /* blinded_sight() - sets all viewable squares to blocked except
443 * 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
445 * really need for any reasonable game play.
446 */
447 static void
448 blinded_sight (object *op)
449 {
450 op->contr->los[LOS_X0][LOS_Y0] = 1;
451 }
452
453 /*
454 * update_los() recalculates the array which specifies what is
455 * visible for the given player-object.
456 */
457 void
458 update_los (object *op)
459 {
460 if (QUERY_FLAG (op, FLAG_REMOVED))
461 return;
462
463 op->contr->clear_los ();
464
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 */
468 blinded_sight (op);
469 else
470 {
471 do_los (op);
472 apply_lights (op);
473 }
474
475 if (QUERY_FLAG (op, FLAG_XRAYS))
476 for (int dx = -2; dx <= 2; dx++)
477 for (int dy = -2; dy <= 2; dy++)
478 min_it (op->contr->los[dx + LOS_X0][dy + LOS_X0], 1);
479 }
480
481 /* update all_map_los is like update_all_los below,
482 * but updates everyone on the map, no matter where they
483 * are. This generally should not be used, as a per
484 * specific map change doesn't make much sense when tiling
485 * is considered (lowering darkness would certainly be a
486 * strange effect if done on a tile map, as it makes
487 * the distinction between maps much more obvious to the
488 * players, which is should not be.
489 * Currently, this function is called from the
490 * change_map_light function
491 */
492 void
493 update_all_map_los (maptile *map)
494 {
495 for_all_players_on_map (pl, map)
496 pl->do_los = 1;
497 }
498
499 /*
500 * This function makes sure that update_los() will be called for all
501 * players on the given map within the next frame.
502 * It is triggered by removal or inserting of objects which blocks
503 * the sight in the map.
504 * Modified by MSW 2001-07-12 to take a coordinate of the changed
505 * position, and to also take map tiling into account. This change
506 * means that just being on the same map is not sufficient - the
507 * space that changes must be withing your viewable area.
508 *
509 * map is the map that changed, x and y are the coordinates.
510 */
511 void
512 update_all_los (const maptile *map, int x, int y)
513 {
514 map->at (x, y).invalidate ();
515
516 for_all_players (pl)
517 {
518 /* Player should not have a null map, but do this
519 * check as a safety
520 */
521 if (!pl->ob || !pl->ob->map || !pl->ns)
522 continue;
523
524 /* Same map is simple case - see if pl is close enough.
525 * Note in all cases, we did the check for same map first,
526 * and then see if the player is close enough and update
527 * los if that is the case. If the player is on the
528 * corresponding map, but not close enough, then the
529 * player can't be on another map that may be closer,
530 * so by setting it up this way, we trim processing
531 * some.
532 */
533 if (pl->ob->map == map)
534 {
535 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
536 pl->do_los = 1;
537 }
538
539 /* Now we check to see if player is on adjacent
540 * maps to the one that changed and also within
541 * view. The tile_maps[] could be null, but in that
542 * case it should never match the pl->ob->map, so
543 * we want ever try to dereference any of the data in it.
544 *
545 * The logic for 0 and 3 is to see how far the player is
546 * from the edge of the map (height/width) - pl->ob->(x,y)
547 * and to add current position on this map - that gives a
548 * distance.
549 * For 1 and 2, we check to see how far the given
550 * coordinate (x,y) is from the corresponding edge,
551 * and then add the players location, which gives
552 * a distance.
553 */
554 else if (pl->ob->map == map->tile_map[0])
555 {
556 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2))
557 pl->do_los = 1;
558 }
559 else if (pl->ob->map == map->tile_map[2])
560 {
561 if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2))
562 pl->do_los = 1;
563 }
564 else if (pl->ob->map == map->tile_map[1])
565 {
566 if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
567 pl->do_los = 1;
568 }
569 else if (pl->ob->map == map->tile_map[3])
570 {
571 if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2))
572 pl->do_los = 1;
573 }
574 }
575 }
576
577 static const int season_timechange[5][HOURS_PER_DAY] = {
578 /* 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 */
579 { 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1},
580 { 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0},
581 { 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0},
582 { 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0},
583 { 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0}
584 };
585
586 void
587 maptile::set_darkness_map ()
588 {
589 timeofday_t tod;
590
591 if (!outdoor)
592 return;
593
594 get_tod (&tod);
595 darkness = 0;
596
597 for (int i = HOURS_PER_DAY / 2; i < HOURS_PER_DAY; i++)
598 change_map_light (season_timechange[tod.season][i]);
599
600 for (int i = 0; i <= tod.hour; i++)
601 change_map_light (season_timechange[tod.season][i]);
602 }
603
604 /*
605 * Compute the darkness level for all maps in the game. Requires the
606 * time of day as an argument.
607 */
608
609 static void
610 dawn_to_dusk (const timeofday_t * tod)
611 {
612 /* If the light level isn't changing, no reason to do all
613 * the work below.
614 */
615 if (season_timechange[tod->season][tod->hour] == 0)
616 return;
617
618 maptile::change_all_map_light (season_timechange[tod->season][tod->hour]);
619 }
620
621 void
622 adjust_daylight ()
623 {
624 timeofday_t tod;
625
626 get_tod (&tod);
627 dawn_to_dusk (&tod);
628 }
629
630 /*
631 * make_sure_seen: The object is supposed to be visible through walls, thus
632 * check if any players are nearby, and edit their LOS array.
633 */
634 void
635 make_sure_seen (const object *op)
636 {
637 for_all_players (pl)
638 if (pl->ob->map == op->map &&
639 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
640 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)
641 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_X0] = 0;
642 }
643
644 /*
645 * make_sure_not_seen: The object which is supposed to be visible through
646 * walls has just been removed from the map, so update the los of any
647 * players within its range
648 */
649 void
650 make_sure_not_seen (const object *op)
651 {
652 for_all_players (pl)
653 if (pl->ob->map == op->map &&
654 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
655 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)
656 pl->do_los = 1;
657 }