--- deliantra/server/common/los.C 2008/12/04 01:07:35 1.31 +++ deliantra/server/common/los.C 2008/12/04 03:48:37 1.33 @@ -34,7 +34,7 @@ */ #define SPACE_BLOCK 0.5 -#define MAX_UNLIGHTED_VISION 4 +#define MAX_DARKNESS_LOS 4 /* 4 == totally dark */ typedef struct blstr { @@ -316,10 +316,33 @@ return 0; } +/* radius, distance => lightness adjust */ +static sint8 darkness[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS + 1]; + +static struct darkness_init +{ + darkness_init () + { + for (int radius = -MAX_LIGHT_RADIUS; radius <= MAX_LIGHT_RADIUS; ++radius) + for (int distance = 0; distance <= MAX_LIGHT_RADIUS; ++distance) + { + // max intensity + int intensity = min (MAX_DARKNESS_LOS, abs (radius) + 1); + + // actual intensity + intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); + + darkness [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 + ? intensity + : MAX_DARKNESS_LOS - intensity; + } + } +} darkness_init; + static void expand_lighted_sight (object *op) { - int x, y, darklevel, ax, ay, basex, basey, mflags, light, x1, y1; + int x, y, darklevel, basex, basey, mflags, light, x1, y1; maptile *m = op->map; sint16 nx, ny; @@ -327,7 +350,7 @@ /* If the player can see in the dark, lower the darklevel for him */ if (QUERY_FLAG (op, FLAG_SEE_IN_DARK)) - darklevel -= 2; + darklevel -= MAX_DARKNESS_LOS / 2; /* add light, by finding all (non-null) nearby light sources, then * mark those squares specially. If the darklevel<1, there is no @@ -346,74 +369,85 @@ darklevel = MAX_DARKNESS; } - /* First, limit player furthest (unlighted) vision */ + /* first, make everything totally dark */ for (x = 0; x < op->contr->ns->mapx; x++) for (y = 0; y < op->contr->ns->mapy; y++) if (op->contr->blocked_los[x][y] != 100) - op->contr->blocked_los[x][y] = MAX_UNLIGHTED_VISION; + op->contr->blocked_los[x][y] = MAX_DARKNESS_LOS; - /* the spaces[] darkness value contains the information we need. + int half_x = op->contr->ns->mapx / 2; + int half_y = op->contr->ns->mapy / 2; + + int min_x = op->x - half_x - MAX_LIGHT_RADIUS; + int min_y = op->y - half_y - MAX_LIGHT_RADIUS; + int max_x = op->x + half_x + MAX_LIGHT_RADIUS; + int max_y = op->y + half_y + MAX_LIGHT_RADIUS; + + int pass2 = 0; // negative lights have an extra pass + + /* * Only process the area of interest. * the basex, basey values represent the position in the op->contr->blocked_los * array. Its easier to just increment them here (and start with the right * value) than to recalculate them down below. */ - for (x = (op->x - op->contr->ns->mapx / 2 - MAX_LIGHT_RADIUS), basex = -MAX_LIGHT_RADIUS; - x <= (op->x + op->contr->ns->mapx / 2 + MAX_LIGHT_RADIUS); x++, basex++) - { + for (int x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++) + for (int y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++) + { + maptile *m = op->map; + sint16 nx = x; + sint16 ny = y; + + if (!xy_normalise (m, nx, ny)) + continue; + + mapspace &ms = m->at (nx, ny); + ms.update (); + sint8 light = ms.light; + + if (expect_false (light)) + if (light < 0) + pass2 = 1; + else + { + /* This space is providing light, so we need to brighten up the + * spaces around here. + */ + const sint8 *darkness_table = darkness [light + MAX_LIGHT_RADIUS]; + + for (int ax = max (0, basex - light); ax <= min (basex + light, op->contr->ns->mapx - 1); ax++) + for (int ay = max (0, basey - light); ay <= min (basey + light, op->contr->ns->mapy - 1); ay++) + if (op->contr->blocked_los[ax][ay] != 100) + min_it (op->contr->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]); + } + } - for (y = (op->y - op->contr->ns->mapy / 2 - MAX_LIGHT_RADIUS), basey = -MAX_LIGHT_RADIUS; - y <= (op->y + op->contr->ns->mapy / 2 + MAX_LIGHT_RADIUS); y++, basey++) + // psosibly do 2nd pass for rare negative glow radii + if (pass2) + for (x = min_x, basex = -MAX_LIGHT_RADIUS; x <= max_x; x++, basex++) + for (y = min_y, basey = -MAX_LIGHT_RADIUS; y <= max_y; y++, basey++) { - m = op->map; - nx = x; - ny = y; - - mflags = get_map_flags (m, &m, nx, ny, &nx, &ny); + maptile *m = op->map; + sint16 nx = x; + sint16 ny = y; - if (mflags & P_OUT_OF_MAP) + if (!xy_normalise (m, nx, ny)) continue; - /* This space is providing light, so we need to brighten up the - * spaces around here. - */ - light = GET_MAP_LIGHT (m, nx, ny); - if (light) - { -#if 0 - LOG (llevDebug, "expand_lighted_sight: Found light at x=%d, y=%d, basex=%d, basey=%d\n", x, y, basex, basey); -#endif - for (ax = basex - light; ax <= basex + light; ax++) - { - if (ax < 0 || ax >= op->contr->ns->mapx) - continue; - - x1 = (basex - ax) * (basex - ax); + mapspace &ms = m->at (nx, ny); + ms.update (); + sint8 light = ms.light; - for (ay = basey - light; ay <= basey + light; ay++) - { - if (ay < 0 || ay >= op->contr->ns->mapy) - continue; + if (expect_false (light < 0)) + { + const sint8 *darkness_table = darkness [light + MAX_LIGHT_RADIUS]; - /* If the space is fully blocked, do nothing. Otherwise, we - * brighten the space. The further the light is away from the - * source (basex-x), the less effect it has. Though light used - * to dim in a square manner, it now dims in a roughly circular - * manner. glow_radius still represents the radius. - */ - if (op->contr->blocked_los[ax][ay] != 100) - { - y1 = (basey - ay) * (basey - ay); - - op->contr->blocked_los[ax][ay] -= light > 0 - ? max (light - isqrt (x1 + y1), 0) - : min (light + isqrt (x1 + y1), 0); - } - } - } + for (int ax = max (0, basex + light); ax <= min (basex - light, op->contr->ns->mapx - 1); ax++) + for (int ay = max (0, basey + light); ay <= min (basey - light, op->contr->ns->mapy - 1); ay++) + if (op->contr->blocked_los[ax][ay] != 100) + max_it (op->contr->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]); } } - } /* Outdoor should never really be completely pitch black dark like * a dungeon, so let the player at least see a little around themselves @@ -436,7 +470,7 @@ for (y = darklevel - MAX_DARKNESS; y < MAX_DARKNESS + 1 - darklevel; y++) if (!(op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] == 100)) op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] -= - MAX (0, 6 - darklevel - MAX (abs (x), abs (y))); + max (0, 6 - darklevel - max (abs (x), abs (y))); } /* blinded_sight() - sets all viewable squares to blocked except