ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/los.C
Revision: 1.62
Committed: Mon Oct 12 14:00:57 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81
Changes since 1.61: +7 -6 lines
Log Message:
clarify license

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * 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 Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 #include <global.h>
26 #include <cmath>
27
28 #define SEE_IN_DARK_RADIUS 2
29 #define MAX_VISION 10 // maximum visible radius
30
31 // los flags
32 enum {
33 FLG_XI = 0x01, // we have an x-parent
34 FLG_YI = 0x02, // we have an y-parent
35 FLG_BLOCKED = 0x04, // this space blocks the view
36 FLG_QUEUED = 0x80 // already queued in queue, or border
37 };
38
39 struct los_info
40 {
41 uint8 flags; // FLG_xxx
42 uint8 culled; // culled from "tree"
43 uint8 visible;
44 uint8 pad0;
45
46 sint8 xo, yo; // obscure angle
47 sint8 xe, ye; // angle deviation
48 };
49
50 // temporary storage for the los algorithm,
51 // one los_info for each lightable map space
52 static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y];
53
54 struct point
55 {
56 sint8 x, y;
57 };
58
59 // minimum size, but must be a power of two
60 #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2)
61
62 // a queue of spaces to calculate
63 static point queue [QUEUE_LENGTH];
64 static int q1, q2; // queue start, end
65
66 /*
67 * Clears/initialises the los-array associated to the player
68 * controlling the object.
69 */
70 void
71 player::clear_los (sint8 value)
72 {
73 memset (los, value, sizeof (los));
74 }
75
76 // enqueue a single mapspace, but only if it hasn't
77 // been enqueued yet.
78 static void
79 enqueue (sint8 dx, sint8 dy, uint8 flags = 0)
80 {
81 sint8 x = LOS_X0 + dx;
82 sint8 y = LOS_Y0 + dy;
83
84 los_info &l = los[x][y];
85
86 l.flags |= flags;
87
88 if (l.flags & FLG_QUEUED)
89 return;
90
91 l.flags |= FLG_QUEUED;
92
93 queue[q1].x = dx;
94 queue[q1].y = dy;
95
96 q1 = (q1 + 1) & (QUEUE_LENGTH - 1);
97 }
98
99 // run the los algorithm
100 // this is a variant of a spiral los algorithm taken from
101 // http://www.geocities.com/temerra/los_rays.html
102 // which has been simplified and changed considerably, but
103 // still is basically the same algorithm.
104 static void
105 calculate_los (player *pl)
106 {
107 {
108 memset (los, 0, sizeof (los));
109
110 // we keep one line for ourselves, for the border flag
111 // so the client area is actually MAP_CLIENT_(X|Y) - 2
112 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2);
113 int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
114
115 // create borders, the corners are not touched
116 for (int dx = -half_x; dx <= half_x; ++dx)
117 los [dx + LOS_X0][LOS_Y0 - (half_y + 1)].flags =
118 los [dx + LOS_X0][LOS_Y0 + (half_y + 1)].flags = FLG_QUEUED;
119
120 for (int dy = -half_y; dy <= half_y; ++dy)
121 los [LOS_X0 - (half_x + 1)][dy + LOS_Y0].flags =
122 los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
123
124 // now reset the los area and also add blocked flags
125 // which supposedly is faster than doing it inside the
126 // spiral path algorithm below, except when very little
127 // area is visible, in which case it is slower. which evens
128 // out los calculation times between large and small los maps.
129 // apply_lights also iterates over this area, maybe these
130 // two passes could be combined somehow.
131 unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y)
132 {
133 los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy];
134 l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0;
135 }
136 }
137
138 q1 = 0; q2 = 0; // initialise queue, not strictly required
139 enqueue (0, 0); // enqueue center
140
141 // treat the origin specially
142 los[LOS_X0][LOS_Y0].visible = 1;
143 pl->los[LOS_X0][LOS_Y0] = 0;
144
145 // loop over all enqueued points until the queue is empty
146 // the order in which this is done ensures that we
147 // never touch a mapspace whose input spaces we haven't checked
148 // yet.
149 while (q1 != q2)
150 {
151 sint8 dx = queue[q2].x;
152 sint8 dy = queue[q2].y;
153
154 q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
155
156 sint8 x = LOS_X0 + dx;
157 sint8 y = LOS_Y0 + dy;
158
159 los_info &l = los[x][y];
160
161 if (expect_true (l.flags & (FLG_XI | FLG_YI)))
162 {
163 l.culled = 1;
164 l.xo = l.yo = l.xe = l.ye = 0;
165
166 // check contributing spaces, first horizontal
167 if (expect_true (l.flags & FLG_XI))
168 {
169 los_info *xi = &los[x - sign (dx)][y];
170
171 // don't cull unless obscured
172 l.culled &= !xi->visible;
173
174 /* merge input space */
175 if (expect_false (xi->xo || xi->yo))
176 {
177 // The X input can provide two main pieces of information:
178 // 1. Progressive X obscurity.
179 // 2. Recessive Y obscurity.
180
181 // Progressive X obscurity, favouring recessive input angle
182 if (xi->xe > 0 && l.xo == 0)
183 {
184 l.xe = xi->xe - xi->yo;
185 l.ye = xi->ye + xi->yo;
186 l.xo = xi->xo;
187 l.yo = xi->yo;
188 }
189
190 // Recessive Y obscurity
191 if (xi->ye <= 0 && xi->yo > 0 && xi->xe > 0)
192 {
193 l.ye = xi->yo + xi->ye;
194 l.xe = xi->xe - xi->yo;
195 l.xo = xi->xo;
196 l.yo = xi->yo;
197 }
198 }
199 }
200
201 // check contributing spaces, last vertical, identical structure
202 if (expect_true (l.flags & FLG_YI))
203 {
204 los_info *yi = &los[x][y - sign (dy)];
205
206 // don't cull unless obscured
207 l.culled &= !yi->visible;
208
209 /* merge input space */
210 if (expect_false (yi->yo || yi->xo))
211 {
212 // The Y input can provide two main pieces of information:
213 // 1. Progressive Y obscurity.
214 // 2. Recessive X obscurity.
215
216 // Progressive Y obscurity, favouring recessive input angle
217 if (yi->ye > 0 && l.yo == 0)
218 {
219 l.ye = yi->ye - yi->xo;
220 l.xe = yi->xe + yi->xo;
221 l.yo = yi->yo;
222 l.xo = yi->xo;
223 }
224
225 // Recessive X obscurity
226 if (yi->xe <= 0 && yi->xo > 0 && yi->ye > 0)
227 {
228 l.xe = yi->xo + yi->xe;
229 l.ye = yi->ye - yi->xo;
230 l.yo = yi->yo;
231 l.xo = yi->xo;
232 }
233 }
234 }
235
236 if (l.flags & FLG_BLOCKED)
237 {
238 l.xo = l.xe = abs (dx);
239 l.yo = l.ye = abs (dy);
240
241 // we obscure dependents, but might be visible
242 // copy the los from the square towards the player,
243 // so outward diagonal corners are lit.
244 pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED;
245
246 l.visible = false;
247 }
248 else
249 {
250 // we are not blocked, so calculate visibility, by checking
251 // whether we are inside or outside the shadow
252 l.visible = (l.xe <= 0 || l.xe > l.xo)
253 && (l.ye <= 0 || l.ye > l.yo);
254
255 pl->los[x][y] = l.culled ? LOS_BLOCKED
256 : l.visible ? 0
257 : 3;
258 }
259
260 }
261
262 // Expands by the unit length in each component's current direction.
263 // If a component has no direction, then it is expanded in both of its
264 // positive and negative directions.
265 if (!l.culled)
266 {
267 if (dx >= 0) enqueue (dx + 1, dy, FLG_XI);
268 if (dx <= 0) enqueue (dx - 1, dy, FLG_XI);
269 if (dy >= 0) enqueue (dx, dy + 1, FLG_YI);
270 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
271 }
272 }
273 }
274
275 /* radius, distance => lightness adjust */
276 static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
277 static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
278
279 static struct los_init
280 {
281 los_init ()
282 {
283 assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two",
284 !(QUEUE_LENGTH & (QUEUE_LENGTH - 1))));
285
286 /* for lights */
287 for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius)
288 for (int distance = 0; distance <= MAX_LIGHT_RADIUS * 3 / 2; ++distance)
289 {
290 // max intensity
291 int intensity = min (LOS_MAX, abs (radius) + 1);
292
293 // actual intensity
294 intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0));
295
296 light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
297 ? min (3, intensity)
298 : LOS_MAX - intensity;
299 }
300
301 /* for general vision */
302 for (int radius = 0; radius <= MAX_VISION; ++radius)
303 for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance)
304 vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4;
305 }
306 } los_init;
307
308 sint8
309 los_brighten (sint8 b, sint8 l)
310 {
311 return b == LOS_BLOCKED ? b : min (b, l);
312 }
313
314 sint8
315 los_darken (sint8 b, sint8 l)
316 {
317 return max (b, l);
318 }
319
320 template<sint8 change_it (sint8, sint8)>
321 static void
322 apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table)
323 {
324 // min or max the circular area around basex, basey
325 dx += LOS_X0;
326 dy += LOS_Y0;
327
328 int hx = pl->ns->mapx / 2;
329 int hy = pl->ns->mapy / 2;
330
331 int ax0 = max (LOS_X0 - hx, dx - light);
332 int ay0 = max (LOS_Y0 - hy, dy - light);
333 int ax1 = min (dx + light, LOS_X0 + hx);
334 int ay1 = min (dy + light, LOS_Y0 + hy);
335
336 for (int ax = ax0; ax <= ax1; ax++)
337 for (int ay = ay0; ay <= ay1; ay++)
338 pl->los[ax][ay] =
339 change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]);
340 }
341
342 /* add light, by finding all (non-null) nearby light sources, then
343 * mark those squares specially.
344 */
345 static void
346 apply_lights (player *pl)
347 {
348 object *op = pl->observe;
349 int darklevel = op->map->darklevel ();
350
351 int half_x = pl->ns->mapx / 2;
352 int half_y = pl->ns->mapy / 2;
353
354 int pass2 = 0; // negative lights have an extra pass
355
356 maprect *rects = pl->observe->map->split_to_tiles (
357 pl->observe->x - half_x - MAX_LIGHT_RADIUS,
358 pl->observe->y - half_y - MAX_LIGHT_RADIUS,
359 pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1,
360 pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1
361 );
362
363 /* If the player can see in the dark, increase light/vision radius */
364 int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0;
365
366 if (!darklevel)
367 pass2 = 1;
368 else
369 {
370 /* first, make everything totally dark */
371 for (int dx = -half_x; dx <= half_x; dx++)
372 for (int dy = -half_x; dy <= half_y; dy++)
373 max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX);
374
375 /*
376 * Only process the area of interest.
377 * 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
379 * value) than to recalculate them down below.
380 */
381 for (maprect *r = rects; r->m; ++r)
382 rect_mapwalk (r, 0, 0)
383 {
384 mapspace &ms = m->at (nx, ny);
385 ms.update ();
386 sint8 light = ms.light;
387
388 if (expect_false (light))
389 if (light < 0)
390 pass2 = 1;
391 else
392 {
393 light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS);
394 apply_light<los_brighten> (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]);
395 }
396 }
397
398 /* grant some vision to the player, based on outside, outdoor, and darklevel */
399 {
400 int light;
401
402 if (!op->map->outdoor) // not outdoor, darkness becomes light radius
403 light = MAX_DARKNESS - op->map->darkness;
404 else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius
405 light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0);
406 else // outdoor and darkness <= 0 => start wide and decrease quickly
407 light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2);
408
409 light = clamp (light + bonus, 0, MAX_VISION);
410
411 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
412 }
413 }
414
415 // possibly do 2nd pass for rare negative glow radii
416 // for effect, those are always considered to be stronger than anything else
417 // but they can't darken a place completely
418 if (pass2)
419 for (maprect *r = rects; r->m; ++r)
420 rect_mapwalk (r, 0, 0)
421 {
422 mapspace &ms = m->at (nx, ny);
423 ms.update ();
424 sint8 light = ms.light;
425
426 if (expect_false (light < 0))
427 {
428 light = clamp (light - bonus, 0, MAX_DARKNESS);
429 apply_light<los_darken> (pl, dx - pl->observe->x, dy - pl->observe->y, -light, light_atten [light + MAX_LIGHT_RADIUS]);
430 }
431 }
432 }
433
434 /* blinded_sight() - sets all viewable squares to blocked except
435 * 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
437 * really need for any reasonable game play.
438 */
439 static void
440 blinded_sight (player *pl)
441 {
442 pl->los[LOS_X0][LOS_Y0] = 1;
443 }
444
445 /*
446 * update_los() recalculates the array which specifies what is
447 * visible for the given player-object.
448 */
449 void
450 player::update_los ()
451 {
452 if (ob->flag [FLAG_REMOVED])//D really needed?
453 return;
454
455 if (ob->flag [FLAG_WIZLOOK])
456 clear_los (0);
457 else if (observe->flag [FLAG_BLIND]) /* player is blind */
458 {
459 clear_los ();
460 blinded_sight (this);
461 }
462 else
463 {
464 clear_los ();
465 calculate_los (this);
466 apply_lights (this);
467 }
468
469 if (observe->flag [FLAG_XRAYS])
470 for (int dx = -2; dx <= 2; dx++)
471 for (int dy = -2; dy <= 2; dy++)
472 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
473 }
474
475 /* update all_map_los is like update_all_los below,
476 * but updates everyone on the map, no matter where they
477 * are. This generally should not be used, as a per
478 * specific map change doesn't make much sense when tiling
479 * is considered (lowering darkness would certainly be a
480 * strange effect if done on a tile map, as it makes
481 * the distinction between maps much more obvious to the
482 * players, which is should not be.
483 * Currently, this function is called from the
484 * change_map_light function
485 */
486 void
487 update_all_map_los (maptile *map)
488 {
489 for_all_players_on_map (pl, map)
490 pl->do_los = 1;
491 }
492
493 /*
494 * This function makes sure that update_los() will be called for all
495 * players on the given map within the next frame.
496 * It is triggered by removal or inserting of objects which blocks
497 * the sight in the map.
498 * Modified by MSW 2001-07-12 to take a coordinate of the changed
499 * position, and to also take map tiling into account. This change
500 * means that just being on the same map is not sufficient - the
501 * space that changes must be withing your viewable area.
502 *
503 * map is the map that changed, x and y are the coordinates.
504 */
505 void
506 update_all_los (const maptile *map, int x, int y)
507 {
508 map->at (x, y).invalidate ();
509
510 for_all_players (pl)
511 {
512 /* Player should not have a null map, but do this
513 * check as a safety
514 */
515 if (!pl->ob || !pl->ob->map || !pl->ns)
516 continue;
517
518 rv_vector rv;
519
520 get_rangevector_from_mapcoord (map, x, y, pl->ob, &rv);
521
522 if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2))
523 pl->do_los = 1;
524 }
525 }
526
527 static const int season_darkness[5][HOURS_PER_DAY] = {
528 /*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 */
529 { 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 },
530 { 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 },
531 { 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 },
532 { 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 },
533 { 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 }
534 };
535
536 /*
537 * Tell players the time and compute the darkness level for all maps in the game.
538 * MUST be called exactly once per hour.
539 */
540 void
541 maptile::adjust_daylight ()
542 {
543 timeofday_t tod;
544
545 get_tod (&tod);
546
547 // log the time to log-1 every hour, and to chat every day
548 {
549 char todbuf[512];
550
551 format_tod (todbuf, sizeof (todbuf), &tod);
552
553 for_all_players (pl)
554 pl->ns->send_msg (NDI_GREY, tod.hour == 15 ? CHAT_CHANNEL : LOG_CHANNEL, todbuf);
555 }
556
557 /* If the light level isn't changing, no reason to do all
558 * the work below.
559 */
560 sint8 new_darkness = season_darkness[tod.season][tod.hour];
561
562 if (new_darkness == maptile::outdoor_darkness)
563 return;
564
565 new_draw_info (NDI_GREY | NDI_UNIQUE | NDI_ALL, 1, 0,
566 new_darkness > maptile::outdoor_darkness
567 ? "It becomes darker."
568 : "It becomes brighter.");
569
570 maptile::outdoor_darkness = new_darkness;
571
572 // we simply update the los for all players, which is unnecessarily
573 // costly, but should do for the moment.
574 for_all_players (pl)
575 pl->do_los = 1;
576 }
577
578 /*
579 * make_sure_seen: The object is supposed to be visible through walls, thus
580 * check if any players are nearby, and edit their LOS array.
581 */
582 void
583 make_sure_seen (const object *op)
584 {
585 for_all_players (pl)
586 if (pl->ob->map == op->map &&
587 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
588 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)
589 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0;
590 }
591
592 /*
593 * make_sure_not_seen: The object which is supposed to be visible through
594 * walls has just been removed from the map, so update the los of any
595 * players within its range
596 */
597 void
598 make_sure_not_seen (const object *op)
599 {
600 for_all_players (pl)
601 if (pl->ob->map == op->map &&
602 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
603 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)
604 pl->do_los = 1;
605 }
606