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.48 by root, Tue Dec 23 06:58:23 2008 UTC vs.
Revision 1.52 by root, Fri Dec 26 10:36:42 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{
39 uint8 flags; // FLG_xxx
40 uint8 culled; // culled from "tree"
41 uint8 visible;
42 uint8 pad0;
43
38 sint8 xo, yo; // obscure angle 44 sint8 xo, yo; // obscure angle
39 sint8 xe, ye; // angle deviation 45 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}; 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);
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
104calculate_los (player *pl) 103calculate_los (player *pl)
105{ 104{
106 int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2; 105 {
107
108 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, evening
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 }
109 135
110 q1 = 0; q2 = 0; // initialise queue, not strictly required 136 q1 = 0; q2 = 0; // initialise queue, not strictly required
111 enqueue (0, 0); // enqueue center 137 enqueue (0, 0); // enqueue center
112 138
113 // treat the origin specially 139 // treat the origin specially
126 q2 = (q2 + 1) & (QUEUE_LENGTH - 1); 152 q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
127 153
128 sint8 x = LOS_X0 + dx; 154 sint8 x = LOS_X0 + dx;
129 sint8 y = LOS_Y0 + dy; 155 sint8 y = LOS_Y0 + dy;
130 156
131 //int distance = idistance (dx, dy); if (distance > max_radius) continue;//D
132 int distance = 0;//D
133
134 los_info &l = los[x][y]; 157 los_info &l = los[x][y];
135 158
136 if (expect_true (l.flags & (LOS_XI | LOS_YI))) 159 if (expect_true (l.flags & (FLG_XI | FLG_YI)))
137 { 160 {
138 l.culled = 1; 161 l.culled = 1;
162 l.xo = l.yo = l.xe = l.ye = 0;
139 163
140 // check contributing spaces, first horizontal 164 // check contributing spaces, first horizontal
141 if (expect_true (l.flags & LOS_XI)) 165 if (expect_true (l.flags & FLG_XI))
142 { 166 {
143 los_info *xi = &los[x - sign (dx)][y]; 167 los_info *xi = &los[x - sign (dx)][y];
144 168
145 // don't cull unless obscured 169 // don't cull unless obscured
146 l.culled &= !xi->visible; 170 l.culled &= !xi->visible;
171 } 195 }
172 } 196 }
173 } 197 }
174 198
175 // check contributing spaces, last vertical, identical structure 199 // check contributing spaces, last vertical, identical structure
176 if (expect_true (l.flags & LOS_YI)) 200 if (expect_true (l.flags & FLG_YI))
177 { 201 {
178 los_info *yi = &los[x][y - sign (dy)]; 202 los_info *yi = &los[x][y - sign (dy)];
179 203
180 // don't cull unless obscured 204 // don't cull unless obscured
181 l.culled &= !yi->visible; 205 l.culled &= !yi->visible;
205 l.xo = yi->xo; 229 l.xo = yi->xo;
206 } 230 }
207 } 231 }
208 } 232 }
209 233
210 // check whether this space blocks the view 234 if (l.flags & FLG_BLOCKED)
211 maptile *m = pl->observe->map;
212 sint16 nx = pl->observe->x + dx;
213 sint16 ny = pl->observe->y + dy;
214
215 if (expect_true (!xy_normalise (m, nx, ny))
216 || expect_false (m->at (nx, ny).flags () & P_BLOCKSVIEW))
217 { 235 {
218 l.xo = l.xe = abs (dx); 236 l.xo = l.xe = abs (dx);
219 l.yo = l.ye = abs (dy); 237 l.yo = l.ye = abs (dy);
220 238
221 // we obscure dependents, but might be visible 239 // we obscure dependents, but might be visible
222 // copy the los from the square towards the player, 240 // copy the los from the square towards the player,
223 // so outward diagonal corners are lit. 241 // so outward diagonal corners are lit.
224 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
225 l.visible = false; 244 l.visible = false;
226 } 245 }
227 else 246 else
228 { 247 {
229 // we are not blocked, so calculate visibility, by checking 248 // we are not blocked, so calculate visibility, by checking
230 // whether we are inside or outside the shadow 249 // whether we are inside or outside the shadow
231 l.visible = (l.xe <= 0 || l.xe > l.xo) 250 l.visible = (l.xe <= 0 || l.xe > l.xo)
232 && (l.ye <= 0 || l.ye > l.yo); 251 && (l.ye <= 0 || l.ye > l.yo);
233 252
234 pl->los[x][y] = l.culled ? LOS_BLOCKED 253 pl->los[x][y] = l.culled ? LOS_BLOCKED
235 : l.visible ? max (0, 2 - max_radius + distance) 254 : l.visible ? 0
236 : 3; 255 : 3;
237 } 256 }
238 257
239 } 258 }
240 259
241 // Expands by the unit length in each component's current direction. 260 // Expands by the unit length in each component's current direction.
242 // 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
243 // positive and negative directions. 262 // positive and negative directions.
244 if (!l.culled) 263 if (!l.culled)
245 { 264 {
246 if (dx >= 0) enqueue (dx + 1, dy, LOS_XI); 265 if (dx >= 0) enqueue (dx + 1, dy, FLG_XI);
247 if (dx <= 0) enqueue (dx - 1, dy, LOS_XI); 266 if (dx <= 0) enqueue (dx - 1, dy, FLG_XI);
248 if (dy >= 0) enqueue (dx, dy + 1, LOS_YI); 267 if (dy >= 0) enqueue (dx, dy + 1, FLG_YI);
249 if (dy <= 0) enqueue (dx, dy - 1, LOS_YI); 268 if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
250 } 269 }
251 } 270 }
252} 271}
253 272
254/* returns true if op carries one or more lights 273/* returns true if op carries one or more lights
266 return 0; 285 return 0;
267} 286}
268 287
269/* radius, distance => lightness adjust */ 288/* radius, distance => lightness adjust */
270static sint8 light_atten[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];
271static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; 290static sint8 vision_atten[MAX_DARKNESS + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1];
272 291
273static struct los_init 292static struct los_init
274{ 293{
275 los_init () 294 los_init ()
276 { 295 {
296 assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two",
297 !(QUEUE_LENGTH & (QUEUE_LENGTH - 1))));
298
277 /* for lights */ 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
288 ? min (3, intensity) 310 ? min (3, intensity)
289 : LOS_MAX - intensity; 311 : LOS_MAX - intensity;
290 } 312 }
291 313
292 /* for general vision */ 314 /* for general vision */
293 for (int radius = 0; radius <= MAX_DARKNESS; ++radius) 315 for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius)
294 for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) 316 for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2; ++distance)
295 {
296 vision_atten [radius][distance] = distance <= radius ? 3 : 4; 317 vision_atten [radius][distance] = distance <= radius ? 3 : 4;
297 }
298 } 318 }
299} los_init; 319} los_init;
300 320
301sint8 321sint8
302los_brighten (sint8 b, sint8 l) 322los_brighten (sint8 b, sint8 l)
339apply_lights (player *pl) 359apply_lights (player *pl)
340{ 360{
341 object *op = pl->observe; 361 object *op = pl->observe;
342 int darklevel = op->map->darklevel (); 362 int darklevel = op->map->darklevel ();
343 363
344 /* If the player can see in the dark, lower the darklevel for him */
345 if (op->flag [FLAG_SEE_IN_DARK])
346 darklevel = max (0, darklevel - 2);
347
348 int half_x = pl->ns->mapx / 2; 364 int half_x = pl->ns->mapx / 2;
349 int half_y = pl->ns->mapy / 2; 365 int half_y = pl->ns->mapy / 2;
350 366
351 int min_x = op->x - half_x - MAX_LIGHT_RADIUS;
352 int min_y = op->y - half_y - MAX_LIGHT_RADIUS;
353 int max_x = op->x + half_x + MAX_LIGHT_RADIUS;
354 int max_y = op->y + half_y + MAX_LIGHT_RADIUS;
355
356 int pass2 = 0; // negative lights have an extra pass 367 int pass2 = 0; // negative lights have an extra pass
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 );
357 375
358 if (!darklevel) 376 if (!darklevel)
359 pass2 = 1; 377 pass2 = 1;
360 else 378 else
361 { 379 {
362 /* first, make everything totally dark */ 380 /* first, make everything totally dark */
363 for (int dx = -half_x; dx <= half_x; dx++) 381 for (int dx = -half_x; dx <= half_x; dx++)
364 for (int dy = -half_x; dy <= half_y; dy++) 382 for (int dy = -half_x; dy <= half_y; dy++)
365 if (pl->los[dx + LOS_X0][dy + LOS_Y0] != LOS_BLOCKED)
366 pl->los[dx + LOS_X0][dy + LOS_Y0] = LOS_MAX; 383 max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX);
367 384
368 /* 385 /*
369 * Only process the area of interest. 386 * Only process the area of interest.
370 * 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
371 * 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
372 * value) than to recalculate them down below. 389 * value) than to recalculate them down below.
373 */ 390 */
374 for (int x = min_x; x <= max_x; x++) 391 for (maprect *r = rects; r->m; ++r)
375 for (int y = min_y; y <= max_y; y++) 392 rect_mapwalk (r, 0, 0)
376 { 393 {
377 maptile *m = pl->observe->map;
378 sint16 nx = x;
379 sint16 ny = y;
380
381 if (!xy_normalise (m, nx, ny))
382 continue;
383
384 mapspace &ms = m->at (nx, ny); 394 mapspace &ms = m->at (nx, ny);
385 ms.update (); 395 ms.update ();
386 sint8 light = ms.light; 396 sint8 light = ms.light;
387 397
388 if (expect_false (light)) 398 if (expect_false (light))
389 if (light < 0) 399 if (light < 0)
390 pass2 = 1; 400 pass2 = 1;
391 else 401 else
392 apply_light<los_brighten> (pl, x - op->x, y - op->y, light, light_atten [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]);
393 } 403 }
394 404
395 /* grant some vision to the player, based on the darklevel */ 405 /* grant some vision to the player, based on the darklevel */
396 { 406 {
397 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); 407 int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS);
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;
398 412
399 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]); 413 apply_light<los_brighten> (pl, 0, 0, light, vision_atten [light]);
400 } 414 }
401 } 415 }
402 416
403 // possibly do 2nd pass for rare negative glow radii 417 // possibly do 2nd pass for rare negative glow radii
404 // 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
405 // but they can't darken a place completely 419 // but they can't darken a place completely
406 if (pass2) 420 if (pass2)
407 for (int x = min_x; x <= max_x; x++) 421 for (maprect *r = rects; r->m; ++r)
408 for (int y = min_y; y <= max_y; y++) 422 rect_mapwalk (r, 0, 0)
409 { 423 {
410 maptile *m = pl->observe->map;
411 sint16 nx = x;
412 sint16 ny = y;
413
414 if (!xy_normalise (m, nx, ny))
415 continue;
416
417 mapspace &ms = m->at (nx, ny); 424 mapspace &ms = m->at (nx, ny);
418 ms.update (); 425 ms.update ();
419 sint8 light = ms.light; 426 sint8 light = ms.light;
420 427
421 if (expect_false (light < 0)) 428 if (expect_false (light < 0))
422 apply_light<los_darken> (pl, x - op->x, y - op->y, -light, light_atten [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]);
423 } 430 }
424} 431}
425 432
426/* blinded_sight() - sets all viewable squares to blocked except 433/* blinded_sight() - sets all viewable squares to blocked except
427 * 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
442player::update_los () 449player::update_los ()
443{ 450{
444 if (ob->flag [FLAG_REMOVED])//D really needed? 451 if (ob->flag [FLAG_REMOVED])//D really needed?
445 return; 452 return;
446 453
447 clear_los ();
448
449 if (ob->flag [FLAG_WIZLOOK]) 454 if (ob->flag [FLAG_WIZLOOK])
450 memset (los, 0, sizeof (los)); 455 clear_los (0);
451 else if (observe->flag [FLAG_BLIND]) /* player is blind */ 456 else if (observe->flag [FLAG_BLIND]) /* player is blind */
457 {
458 clear_los ();
452 blinded_sight (this); 459 blinded_sight (this);
460 }
453 else 461 else
454 { 462 {
463 clear_los ();
455 calculate_los (this); 464 calculate_los (this);
456 apply_lights (this); 465 apply_lights (this);
457 } 466 }
458 467
459 if (observe->flag [FLAG_XRAYS]) 468 if (observe->flag [FLAG_XRAYS])
460 for (int dx = -2; dx <= 2; dx++) 469 for (int dx = -2; dx <= 2; dx++)
461 for (int dy = -2; dy <= 2; dy++) 470 for (int dy = -2; dy <= 2; dy++)
462 min_it (los[dx + LOS_X0][dy + LOS_X0], 1); 471 min_it (los[dx + LOS_X0][dy + LOS_Y0], 1);
463} 472}
464 473
465/* update all_map_los is like update_all_los below, 474/* update all_map_los is like update_all_los below,
466 * but updates everyone on the map, no matter where they 475 * but updates everyone on the map, no matter where they
467 * are. This generally should not be used, as a per 476 * are. This generally should not be used, as a per
618{ 627{
619 for_all_players (pl) 628 for_all_players (pl)
620 if (pl->ob->map == op->map && 629 if (pl->ob->map == op->map &&
621 pl->ob->y - pl->ns->mapy / 2 <= op->y && 630 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
622 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) 631 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)
623 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_X0] = 0; 632 pl->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0;
624} 633}
625 634
626/* 635/*
627 * make_sure_not_seen: The object which is supposed to be visible through 636 * make_sure_not_seen: The object which is supposed to be visible through
628 * walls has just been removed from the map, so update the los of any 637 * walls has just been removed from the map, so update the los of any
635 if (pl->ob->map == op->map && 644 if (pl->ob->map == op->map &&
636 pl->ob->y - pl->ns->mapy / 2 <= op->y && 645 pl->ob->y - pl->ns->mapy / 2 <= op->y &&
637 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) 646 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)
638 pl->do_los = 1; 647 pl->do_los = 1;
639} 648}
649

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines