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.36 by root, Mon Dec 8 15:40:13 2008 UTC vs.
Revision 1.40 by root, Fri Dec 19 17:52:50 2008 UTC

30 * An object is 1.0 wide, so if set to 0.5, it means the object 30 * An object is 1.0 wide, so if set to 0.5, it means the object
31 * that blocks half the view (0.0 is complete block) will 31 * that blocks half the view (0.0 is complete block) will
32 * block view in our tables. 32 * block view in our tables.
33 * .4 or less lets you see through walls. .5 is about right. 33 * .4 or less lets you see through walls. .5 is about right.
34 */ 34 */
35
36#define SPACE_BLOCK 0.5 35#define SPACE_BLOCK 0.5
37 36
38typedef struct blstr 37typedef struct blstr
39{ 38{
40 int x[4], y[4]; 39 int x[4], y[4];
87/* since we are only doing the upper left quadrant, only 86/* since we are only doing the upper left quadrant, only
88 * these spaces could possibly get blocked, since these 87 * these spaces could possibly get blocked, since these
89 * are the only ones further out that are still possibly in the 88 * are the only ones further out that are still possibly in the
90 * sightline. 89 * sightline.
91 */ 90 */
92
93void 91void
94init_block (void) 92init_block (void)
95{ 93{
96 int x, y, dx, dy, i;
97 static int block_x[3] = { -1, -1, 0 }, 94 static int block_x[3] = { -1, -1, 0 },
98 block_y[3] = { -1, 0, -1 }; 95 block_y[3] = { -1, 0, -1 };
99 96
100 for (x = 0; x < MAP_CLIENT_X; x++) 97 for (int x = 0; x < MAP_CLIENT_X; x++)
101 for (y = 0; y < MAP_CLIENT_Y; y++) 98 for (int y = 0; y < MAP_CLIENT_Y; y++)
102 block[x][y].index = 0; 99 block[x][y].index = 0;
103
104 100
105 /* The table should be symmetric, so only do the upper left 101 /* The table should be symmetric, so only do the upper left
106 * quadrant - makes the processing easier. 102 * quadrant - makes the processing easier.
107 */ 103 */
108 for (x = 1; x <= MAP_CLIENT_X / 2; x++) 104 for (int x = 1; x <= MAP_CLIENT_X / 2; x++)
109 { 105 {
110 for (y = 1; y <= MAP_CLIENT_Y / 2; y++) 106 for (int y = 1; y <= MAP_CLIENT_Y / 2; y++)
111 { 107 {
112 for (i = 0; i < 3; i++) 108 for (int i = 0; i < 3; i++)
113 { 109 {
114 dx = x + block_x[i]; 110 int dx = x + block_x[i];
115 dy = y + block_y[i]; 111 int dy = y + block_y[i];
116 112
117 /* center space never blocks */ 113 /* center space never blocks */
118 if (x == MAP_CLIENT_X / 2 && y == MAP_CLIENT_Y / 2) 114 if (x == MAP_CLIENT_X / 2 && y == MAP_CLIENT_Y / 2)
119 continue; 115 continue;
120 116
132 } 128 }
133 else 129 else
134 { 130 {
135 float d1, r, s, l; 131 float d1, r, s, l;
136 132
137 /* We use the algorihm that found out how close the point 133 /* We use the algorithm that found out how close the point
138 * (x,y) is to the line from dx,dy to the center of the viewable 134 * (x,y) is to the line from dx,dy to the center of the viewable
139 * area. l is the distance from x,y to the line. 135 * area. l is the distance from x,y to the line.
140 * r is more a curiosity - it lets us know what direction (left/right) 136 * r is more a curiosity - it lets us know what direction (left/right)
141 * the line is off 137 * the line is off
142 */ 138 */
143 139
144 d1 = (float) (pow (MAP_CLIENT_X / 2 - dx, 2.f) + pow (MAP_CLIENT_Y / 2 - dy, 2.f)); 140 d1 = (powf (MAP_CLIENT_X / 2 - dx, 2.f) + powf (MAP_CLIENT_Y / 2 - dy, 2.f));
145 r = (float) ((dy - y) * (dy - MAP_CLIENT_Y / 2) - (dx - x) * (MAP_CLIENT_X / 2 - dx)) / d1; 141 r = ((dy - y) * (dy - MAP_CLIENT_Y / 2) - (dx - x) * (MAP_CLIENT_X / 2 - dx)) / d1;
146 s = (float) ((dy - y) * (MAP_CLIENT_X / 2 - dx) - (dx - x) * (MAP_CLIENT_Y / 2 - dy)) / d1; 142 s = ((dy - y) * (MAP_CLIENT_X / 2 - dx) - (dx - x) * (MAP_CLIENT_Y / 2 - dy)) / d1;
147 l = FABS (sqrt (d1) * s); 143 l = fabs (sqrtf (d1) * s);
148 144
149 if (l <= SPACE_BLOCK) 145 if (l <= SPACE_BLOCK)
150 { 146 {
151 /* For simplicity, we mirror the coordinates to block the other 147 /* For simplicity, we mirror the coordinates to block the other
152 * quadrants. 148 * quadrants.
173 * In this way, the chain of visibility is set. 169 * In this way, the chain of visibility is set.
174 */ 170 */
175static void 171static void
176set_wall (object *op, int x, int y) 172set_wall (object *op, int x, int y)
177{ 173{
178 int i;
179
180 for (i = 0; i < block[x][y].index; i++) 174 for (int i = 0; i < block[x][y].index; i++)
181 { 175 {
182 int dx = block[x][y].x[i], dy = block[x][y].y[i], ax, ay; 176 int dx = block[x][y].x[i], dy = block[x][y].y[i], ax, ay;
183 177
184 /* ax, ay are the values as adjusted to be in the 178 /* ax, ay are the values as adjusted to be in the
185 * socket look structure. 179 * socket look structure.
204/* 198/*
205 * Used to initialise the array used by the LOS routines. 199 * Used to initialise the array used by the LOS routines.
206 * op is the object, x and y values based on MAP_CLIENT_X and Y. 200 * op is the object, x and y values based on MAP_CLIENT_X and Y.
207 * this is because they index the blocked[][] arrays. 201 * this is because they index the blocked[][] arrays.
208 */ 202 */
209
210static void 203static void
211check_wall (object *op, int x, int y) 204check_wall (object *op, int x, int y)
212{ 205{
213 int ax, ay; 206 int ax, ay;
214 207
261 * expand_sight goes through the array of what the given player is 254 * expand_sight goes through the array of what the given player is
262 * able to see, and expands the visible area a bit, so the player will, 255 * able to see, and expands the visible area a bit, so the player will,
263 * to a certain degree, be able to see into corners. 256 * to a certain degree, be able to see into corners.
264 * This is somewhat suboptimal, would be better to improve the formula. 257 * This is somewhat suboptimal, would be better to improve the formula.
265 */ 258 */
266
267static void 259static void
268expand_sight (object *op) 260expand_sight (object *op)
269{ 261{
270 int i, x, y, dx, dy;
271
272 for (x = 1; x < op->contr->ns->mapx - 1; x++) /* loop over inner squares */ 262 for (int x = 1; x < op->contr->ns->mapx - 1; x++) /* loop over inner squares */
273 for (y = 1; y < op->contr->ns->mapy - 1; y++) 263 for (int y = 1; y < op->contr->ns->mapy - 1; y++)
274 {
275 if (!op->contr->blocked_los[x][y] && 264 if (!op->contr->blocked_los[x][y] &&
276 !(get_map_flags (op->map, NULL, 265 !(get_map_flags (op->map, NULL,
277 op->x - op->contr->ns->mapx / 2 + x, 266 op->x - op->contr->ns->mapx / 2 + x,
278 op->y - op->contr->ns->mapy / 2 + y, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP))) 267 op->y - op->contr->ns->mapy / 2 + y, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP)))
279 { 268 {
280
281 for (i = 1; i <= 8; i += 1) 269 for (int i = 1; i <= 8; i += 1)
282 { /* mark all directions */ 270 { /* mark all directions */
283 dx = x + freearr_x[i]; 271 int dx = x + freearr_x[i];
284 dy = y + freearr_y[i]; 272 int dy = y + freearr_y[i];
273
285 if (op->contr->blocked_los[dx][dy] > 0) /* for any square blocked */ 274 if (op->contr->blocked_los[dx][dy] > 0) /* for any square blocked */
286 op->contr->blocked_los[dx][dy] = -1; 275 op->contr->blocked_los[dx][dy] = -1;
287 } 276 }
288 } 277 }
289 }
290 278
291 if (op->map->darkness > 0) /* player is on a dark map */
292 expand_lighted_sight (op); 279 expand_lighted_sight (op);
293 280
294 /* clear mark squares */ 281 /* clear mark squares */
295 for (x = 0; x < op->contr->ns->mapx; x++) 282 for (int x = 0; x < op->contr->ns->mapx; x++)
296 for (y = 0; y < op->contr->ns->mapy; y++) 283 for (int y = 0; y < op->contr->ns->mapy; y++)
297 if (op->contr->blocked_los[x][y] < 0) 284 if (op->contr->blocked_los[x][y] < 0)
298 op->contr->blocked_los[x][y] = 0; 285 op->contr->blocked_los[x][y] = 0;
299} 286}
300 287
301/* returns true if op carries one or more lights 288/* returns true if op carries one or more lights
302 * This is a trivial function now days, but it used to 289 * This is a trivial function now days, but it used to
303 * be a bit longer. Probably better for callers to just 290 * be a bit longer. Probably better for callers to just
304 * check the op->glow_radius instead of calling this. 291 * check the op->glow_radius instead of calling this.
305 */ 292 */
306
307int 293int
308has_carried_lights (const object *op) 294has_carried_lights (const object *op)
309{ 295{
310 /* op may glow! */ 296 /* op may glow! */
311 if (op->glow_radius > 0) 297 if (op->glow_radius > 0)
335 : LOS_MAX - intensity; 321 : LOS_MAX - intensity;
336 } 322 }
337 } 323 }
338} darkness_init; 324} darkness_init;
339 325
326sint8
327los_brighten (sint8 b, sint8 l)
328{
329 return b == LOS_BLOCKED ? b : min (b, l);
330}
331
332sint8
333los_darken (sint8 b, sint8 l)
334{
335 return max (b, l);
336}
337
338template<sint8 change_it (sint8, sint8)>
339static void
340apply_light (object *op, int basex, int basey, int light, const sint8 *darkness_table)
341{
342 // min or max the ciruclar area around basex, basey
343 player *pl = op->contr;
344
345 int ax0 = max (0, basex - light);
346 int ay0 = max (0, basey - light);
347 int ax1 = min (basex + light, pl->ns->mapx - 1);
348 int ay1 = min (basey + light, pl->ns->mapy - 1);
349
350 for (int ax = ax0; ax <= ax1; ax++)
351 for (int ay = ay0; ay <= ay1; ay++)
352 pl->blocked_los[ax][ay] =
353 change_it (pl->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]);
354}
355
356/* add light, by finding all (non-null) nearby light sources, then
357 * mark those squares specially.
358 */
340static void 359static void
341expand_lighted_sight (object *op) 360expand_lighted_sight (object *op)
342{ 361{
343 int x, y, darklevel, basex, basey, mflags, light, x1, y1; 362 int darklevel, mflags, light, x1, y1;
344 maptile *m = op->map; 363 maptile *m = op->map;
345 sint16 nx, ny; 364 sint16 nx, ny;
346 365
347 darklevel = m->darkness; 366 darklevel = m->darkness;
348 367
349 /* If the player can see in the dark, lower the darklevel for him */ 368 /* If the player can see in the dark, lower the darklevel for him */
350 if (QUERY_FLAG (op, FLAG_SEE_IN_DARK)) 369 if (QUERY_FLAG (op, FLAG_SEE_IN_DARK))
351 darklevel -= LOS_MAX / 2; 370 darklevel -= LOS_MAX / 2;
352
353 /* add light, by finding all (non-null) nearby light sources, then
354 * mark those squares specially. If the darklevel<1, there is no
355 * reason to do this, so we skip this function
356 */
357
358 if (darklevel < 1)
359 return;
360 371
361 /* Do a sanity check. If not valid, some code below may do odd 372 /* Do a sanity check. If not valid, some code below may do odd
362 * things. 373 * things.
363 */ 374 */
364 if (darklevel > MAX_DARKNESS) 375 if (darklevel > MAX_DARKNESS)
365 { 376 {
366 LOG (llevError, "Map darkness for %s on %s is too high (%d)\n", &op->name, &op->map->path, darklevel); 377 LOG (llevError, "Map darkness for %s on %s is too high (%d)\n", &op->name, &op->map->path, darklevel);
367 darklevel = MAX_DARKNESS; 378 darklevel = MAX_DARKNESS;
368 } 379 }
369 380
370 /* first, make everything totally dark */
371 for (x = 0; x < op->contr->ns->mapx; x++)
372 for (y = 0; y < op->contr->ns->mapy; y++)
373 if (op->contr->blocked_los[x][y] != LOS_BLOCKED)
374 op->contr->blocked_los[x][y] = LOS_MAX;
375
376 int half_x = op->contr->ns->mapx / 2; 381 int half_x = op->contr->ns->mapx / 2;
377 int half_y = op->contr->ns->mapy / 2; 382 int half_y = op->contr->ns->mapy / 2;
378 383
379 int min_x = op->x - half_x - MAX_LIGHT_RADIUS; 384 int min_x = op->x - half_x - MAX_LIGHT_RADIUS;
380 int min_y = op->y - half_y - MAX_LIGHT_RADIUS; 385 int min_y = op->y - half_y - MAX_LIGHT_RADIUS;
381 int max_x = op->x + half_x + MAX_LIGHT_RADIUS; 386 int max_x = op->x + half_x + MAX_LIGHT_RADIUS;
382 int max_y = op->y + half_y + MAX_LIGHT_RADIUS; 387 int max_y = op->y + half_y + MAX_LIGHT_RADIUS;
383 388
384 int pass2 = 0; // negative lights have an extra pass 389 int pass2 = 0; // negative lights have an extra pass
385 390
386 /* 391 if (darklevel < 1)
392 pass2 = 1;
393 else
394 {
395 /* first, make everything totally dark */
396 for (int x = 0; x < op->contr->ns->mapx; x++)
397 for (int y = 0; y < op->contr->ns->mapy; y++)
398 if (op->contr->blocked_los[x][y] != LOS_BLOCKED)
399 op->contr->blocked_los[x][y] = LOS_MAX;
400
401 /*
387 * Only process the area of interest. 402 * Only process the area of interest.
388 * the basex, basey values represent the position in the op->contr->blocked_los 403 * the basex, basey values represent the position in the op->contr->blocked_los
389 * array. Its easier to just increment them here (and start with the right 404 * array. Its easier to just increment them here (and start with the right
390 * value) than to recalculate them down below. 405 * value) than to recalculate them down below.
391 */ 406 */
392 for (int x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++) 407 for (int x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++)
393 for (int y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++) 408 for (int y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++)
409 {
410 maptile *m = op->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);
418 ms.update ();
419 sint8 light = ms.light;
420
421 if (expect_false (light))
422 if (light < 0)
423 pass2 = 1;
424 else
425 apply_light<los_brighten> (op, basex, basey, light, darkness [light + MAX_LIGHT_RADIUS]);
426 }
427
428 /* grant some vision to the player, based on the darklevel */
429 /* for outdoor maps, ensure some mininum visibility radius */
394 { 430 {
395 maptile *m = op->map; 431 int light = clamp (MAX_DARKNESS - darklevel, op->map->outdoor ? 2 : 0, MAX_LIGHT_RADIUS);
396 sint16 nx = x;
397 sint16 ny = y;
398 432
399 if (!xy_normalise (m, nx, ny)) 433 apply_light<los_brighten> (op, half_x, half_y, light, darkness [light + MAX_LIGHT_RADIUS]);
400 continue;
401
402 mapspace &ms = m->at (nx, ny);
403 ms.update ();
404 sint8 light = ms.light;
405
406 if (expect_false (light))
407 if (light < 0)
408 pass2 = 1;
409 else
410 {
411 /* This space is providing light, so we need to brighten up the
412 * spaces around here.
413 */
414 const sint8 *darkness_table = darkness [light + MAX_LIGHT_RADIUS];
415
416 for (int ax = max (0, basex - light); ax <= min (basex + light, op->contr->ns->mapx - 1); ax++)
417 for (int ay = max (0, basey - light); ay <= min (basey + light, op->contr->ns->mapy - 1); ay++)
418 if (op->contr->blocked_los[ax][ay] != LOS_BLOCKED)
419 min_it (op->contr->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]);
420 }
421 } 434 }
435 }
422 436
423 // psosibly do 2nd pass for rare negative glow radii 437 // possibly do 2nd pass for rare negative glow radii
424 if (expect_false (pass2)) 438 // for effect, those are always considered to be stronger than anything else
439 // but they can't darken a place completely
440 if (pass2)
425 for (x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++) 441 for (int x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++)
426 for (y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++) 442 for (int y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++)
427 { 443 {
428 maptile *m = op->map; 444 maptile *m = op->map;
429 sint16 nx = x; 445 sint16 nx = x;
430 sint16 ny = y; 446 sint16 ny = y;
431 447
435 mapspace &ms = m->at (nx, ny); 451 mapspace &ms = m->at (nx, ny);
436 ms.update (); 452 ms.update ();
437 sint8 light = ms.light; 453 sint8 light = ms.light;
438 454
439 if (expect_false (light < 0)) 455 if (expect_false (light < 0))
440 { 456 apply_light<los_darken> (op, basex, basey, -light, darkness [light + MAX_LIGHT_RADIUS]);
441 const sint8 *darkness_table = darkness [light + MAX_LIGHT_RADIUS];
442
443 for (int ax = max (0, basex + light); ax <= min (basex - light, op->contr->ns->mapx - 1); ax++)
444 for (int ay = max (0, basey + light); ay <= min (basey - light, op->contr->ns->mapy - 1); ay++)
445 if (op->contr->blocked_los[ax][ay] != LOS_BLOCKED)
446 max_it (op->contr->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]);
447 } 457 }
448 }
449
450 /* Outdoor should never really be completely pitch black dark like
451 * a dungeon, so let the player at least see a little around themselves
452 */
453 if (op->map->outdoor && darklevel > MAX_DARKNESS - 3)
454 {
455 if (op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] > (LOS_MAX - 3))
456 op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] = LOS_MAX - 3;
457
458 for (x = -1; x <= 1; x++)
459 for (y = -1; y <= 1; y++)
460 if (op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] > (LOS_MAX - 2))
461 op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] = LOS_MAX - 2;
462 }
463
464 /* grant some vision to the player, based on the darklevel */
465 for (x = darklevel - MAX_DARKNESS; x < MAX_DARKNESS + 1 - darklevel; x++)
466 for (y = darklevel - MAX_DARKNESS; y < MAX_DARKNESS + 1 - darklevel; y++)
467 if (!(op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] == LOS_BLOCKED))
468 op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] -=
469 max (0, 6 - darklevel - max (abs (x), abs (y)));
470} 458}
471 459
472/* blinded_sight() - sets all viewable squares to blocked except 460/* blinded_sight() - sets all viewable squares to blocked except
473 * for the one the central one that the player occupies. A little 461 * for the one the central one that the player occupies. A little
474 * odd that you can see yourself (and what your standing on), but 462 * odd that you can see yourself (and what your standing on), but

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines