--- deliantra/server/common/los.C 2008/12/23 22:04:17 1.50 +++ deliantra/server/common/los.C 2009/01/08 22:35:00 1.58 @@ -24,6 +24,9 @@ #include #include +#define SEE_IN_DARK_RADIUS 2 +#define MAX_VISION 10 // maximum visible radius + // los flags enum { FLG_XI = 0x01, // we have an x-parent @@ -101,6 +104,8 @@ calculate_los (player *pl) { { + memset (los, 0, sizeof (los)); + // we keep one line for ourselves, for the border flag // so the client area is actually MAP_CLIENT_(X|Y) - 2 int half_x = min (LOS_X0 - 1, pl->ns->mapx / 2); @@ -118,14 +123,15 @@ // now reset the los area and also add blocked flags // which supposedly is faster than doing it inside the // spiral path algorithm below, except when very little - // area is visible, in which case it is slower, evening + // area is visible, in which case it is slower. which evens // out los calculation times between large and small los maps. // apply_lights also iterates over this area, maybe these // two passes could be combined somehow. - rectangular_mapspace_iterate_begin (pl->observe, -half_x, half_x, -half_y, half_y) - los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; - l.flags = m && m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; - rectangular_mapspace_iterate_end + unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y) + { + los_info &l = los [LOS_X0 + dx][LOS_Y0 + dy]; + l.flags = m->at (nx, ny).flags () & P_BLOCKSVIEW ? FLG_BLOCKED : 0; + } } q1 = 0; q2 = 0; // initialise queue, not strictly required @@ -265,24 +271,9 @@ } } -/* returns true if op carries one or more lights - * This is a trivial function now days, but it used to - * be a bit longer. Probably better for callers to just - * check the op->glow_radius instead of calling this. - */ -int -has_carried_lights (const object *op) -{ - /* op may glow! */ - if (op->glow_radius > 0) - return 1; - - return 0; -} - /* radius, distance => lightness adjust */ static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1]; -static sint8 vision_atten[MAX_DARKNESS + 1][MAX_DARKNESS * 3 / 2 + 1]; +static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1]; static struct los_init { @@ -307,11 +298,9 @@ } /* for general vision */ - for (int radius = 0; radius <= MAX_DARKNESS; ++radius) - for (int distance = 0; distance <= MAX_DARKNESS * 3 / 2; ++distance) - { - vision_atten [radius][distance] = distance <= radius ? 3 : 4; - } + for (int radius = 0; radius <= MAX_VISION; ++radius) + for (int distance = 0; distance <= MAX_VISION * 3 / 2; ++distance) + vision_atten [radius][distance] = distance <= radius ? clamp (lerp (radius, 0, MAX_DARKNESS, 3, 0), 0, 3) : 4; } } los_init; @@ -358,15 +347,21 @@ object *op = pl->observe; int darklevel = op->map->darklevel (); - /* If the player can see in the dark, lower the darklevel for him */ - if (op->flag [FLAG_SEE_IN_DARK]) - darklevel = max (0, darklevel - 2); - int half_x = pl->ns->mapx / 2; int half_y = pl->ns->mapy / 2; int pass2 = 0; // negative lights have an extra pass + maprect *rects = pl->observe->map->split_to_tiles ( + pl->observe->x - half_x - MAX_LIGHT_RADIUS, + pl->observe->y - half_y - MAX_LIGHT_RADIUS, + pl->observe->x + half_x + MAX_LIGHT_RADIUS + 1, + pl->observe->y + half_y + MAX_LIGHT_RADIUS + 1 + ); + + /* If the player can see in the dark, increase light/vision radius */ + int bonus = op->flag [FLAG_SEE_IN_DARK] ? SEE_IN_DARK_RADIUS : 0; + if (!darklevel) pass2 = 1; else @@ -382,8 +377,8 @@ * array. Its easier to just increment them here (and start with the right * value) than to recalculate them down below. */ - rectangular_mapspace_iterate_begin (pl->observe, -half_x - MAX_LIGHT_RADIUS, half_x + MAX_LIGHT_RADIUS, -half_y - MAX_LIGHT_RADIUS, half_y + MAX_LIGHT_RADIUS) - if (m) + for (maprect *r = rects; r->m; ++r) + rect_mapwalk (r, 0, 0) { mapspace &ms = m->at (nx, ny); ms.update (); @@ -393,13 +388,24 @@ if (light < 0) pass2 = 1; else - apply_light (pl, dx, dy, light, light_atten [light + MAX_LIGHT_RADIUS]); + { + light = clamp (light + bonus, 0, MAX_LIGHT_RADIUS); + apply_light (pl, dx - pl->observe->x, dy - pl->observe->y, light, light_atten [light + MAX_LIGHT_RADIUS]); + } } - rectangular_mapspace_iterate_end - /* grant some vision to the player, based on the darklevel */ + /* grant some vision to the player, based on outside, outdoor, and darklevel */ { - int light = clamp (MAX_DARKNESS - darklevel, 0, MAX_DARKNESS); + int light; + + if (!op->map->outdoor) // not outdoor, darkness becomes light radius + light = MAX_DARKNESS - op->map->darkness; + else if (op->map->darkness > 0) // outdoor and darkness > 0 => use darkness as max radius + light = lerp_rd (maptile::outdoor_darkness + 0, 0, MAX_DARKNESS, MAX_DARKNESS - op->map->darkness, 0); + else // outdoor and darkness <= 0 => start wide and decrease quickly + light = lerp (maptile::outdoor_darkness + op->map->darkness, 0, MAX_DARKNESS, MAX_VISION, 2); + + light = clamp (light, 0, MAX_VISION); apply_light (pl, 0, 0, light, vision_atten [light]); } @@ -409,17 +415,19 @@ // for effect, those are always considered to be stronger than anything else // but they can't darken a place completely if (pass2) - rectangular_mapspace_iterate_begin (pl->observe, -half_x - MAX_LIGHT_RADIUS, half_x + MAX_LIGHT_RADIUS, -half_y - MAX_LIGHT_RADIUS, half_y + MAX_LIGHT_RADIUS) - if (m) - { - mapspace &ms = m->at (nx, ny); - ms.update (); - sint8 light = ms.light; - - if (expect_false (light < 0)) - apply_light (pl, dx, dy, -light, light_atten [light + MAX_LIGHT_RADIUS]); - } - rectangular_mapspace_iterate_end + for (maprect *r = rects; r->m; ++r) + rect_mapwalk (r, 0, 0) + { + mapspace &ms = m->at (nx, ny); + ms.update (); + sint8 light = ms.light; + + if (expect_false (light < 0)) + { + light = clamp (light - bonus, 0, MAX_DARKNESS); + apply_light (pl, dx - pl->observe->x, dy - pl->observe->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); + } + } } /* blinded_sight() - sets all viewable squares to blocked except @@ -496,6 +504,10 @@ void update_all_los (const maptile *map, int x, int y) { + // no need to do anything if we don't have darkness + if (map->darklevel () <= 0) + return; + map->at (x, y).invalidate (); for_all_players (pl) @@ -516,10 +528,8 @@ * some. */ if (pl->ob->map == map) - { - if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) - pl->do_los = 1; - } + if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) + pl->do_los = 1; /* Now we check to see if player is on adjacent * maps to the one that changed and also within @@ -638,3 +648,4 @@ 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) pl->do_los = 1; } +