--- deliantra/server/common/los.C 2008/12/24 01:37:23 1.51 +++ deliantra/server/common/los.C 2018/12/05 19:03:26 1.78 @@ -1,30 +1,31 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. - * - * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team - * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team - * Copyright (©) 1992,2007 Frank Tore Johansen - * - * Deliantra is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * + * + * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team + * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * + * Deliantra is free software: you can redistribute it and/or modify it under + * the terms of the Affero GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * + * + * You should have received a copy of the Affero GNU General Public License + * and the GNU General Public License along with this program. If not, see + * . + * * The authors can be reached via e-mail to */ #include #include -#define SEE_IN_DARK_RADIUS 3 +#define SEE_IN_DARK_RADIUS 2 +#define MAX_VISION 10 // maximum visible radius // los flags enum { @@ -34,6 +35,10 @@ FLG_QUEUED = 0x80 // already queued in queue, or border }; +// it is important for performance reasons that this structure +// has a size easily computable by the cpu (*8 is perfect). +// it is possible to move culled and visible into flags, at +// some speed loss. struct los_info { uint8 flags; // FLG_xxx @@ -49,7 +54,7 @@ // one los_info for each lightable map space static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; -struct point +struct point8 { sint8 x, y; }; @@ -58,7 +63,7 @@ #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) // a queue of spaces to calculate -static point queue [QUEUE_LENGTH]; +static point8 queue [QUEUE_LENGTH]; static int q1, q2; // queue start, end /* @@ -83,7 +88,7 @@ l.flags |= flags; - if (l.flags & FLG_QUEUED) + if (ecb_expect_false (l.flags & FLG_QUEUED)) return; l.flags |= FLG_QUEUED; @@ -103,6 +108,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); @@ -120,14 +127,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 (mapwalk_buf, pl->viewpoint, -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 @@ -153,13 +161,13 @@ los_info &l = los[x][y]; - if (expect_true (l.flags & (FLG_XI | FLG_YI))) + if (ecb_expect_true (l.flags & (FLG_XI | FLG_YI))) { l.culled = 1; l.xo = l.yo = l.xe = l.ye = 0; // check contributing spaces, first horizontal - if (expect_true (l.flags & FLG_XI)) + if (ecb_expect_true (l.flags & FLG_XI)) { los_info *xi = &los[x - sign (dx)][y]; @@ -167,9 +175,9 @@ l.culled &= !xi->visible; /* merge input space */ - if (expect_false (xi->xo || xi->yo)) + if (ecb_expect_false (xi->xo || xi->yo)) { - // The X input can provide two main pieces of information: + // The X input can provide two main pieces of information: // 1. Progressive X obscurity. // 2. Recessive Y obscurity. @@ -194,7 +202,7 @@ } // check contributing spaces, last vertical, identical structure - if (expect_true (l.flags & FLG_YI)) + if (ecb_expect_true (l.flags & FLG_YI)) { los_info *yi = &los[x][y - sign (dy)]; @@ -202,9 +210,9 @@ l.culled &= !yi->visible; /* merge input space */ - if (expect_false (yi->yo || yi->xo)) + if (ecb_expect_false (yi->yo || yi->xo)) { - // The Y input can provide two main pieces of information: + // The Y input can provide two main pieces of information: // 1. Progressive Y obscurity. // 2. Recessive X obscurity. @@ -255,7 +263,7 @@ } // Expands by the unit length in each component's current direction. - // If a component has no direction, then it is expanded in both of its + // If a component has no direction, then it is expanded in both of its // positive and negative directions. if (!l.culled) { @@ -267,24 +275,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 + SEE_IN_DARK_RADIUS + 1][(MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 3 / 2 + 1]; +static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1]; static struct los_init { @@ -301,7 +294,7 @@ int intensity = min (LOS_MAX, abs (radius) + 1); // actual intensity - intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0)); + intensity = max (0, lerp_ru (distance, 0, abs (radius) + 1, intensity, 0)); light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0 ? min (3, intensity) @@ -309,23 +302,35 @@ } /* for general vision */ - for (int radius = 0; radius <= MAX_DARKNESS + SEE_IN_DARK_RADIUS; ++radius) - for (int distance = 0; distance <= (MAX_DARKNESS + SEE_IN_DARK_RADIUS) * 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; -sint8 -los_brighten (sint8 b, sint8 l) -{ - return b == LOS_BLOCKED ? b : min (b, l); -} +// the following functions cannot be static, due to c++ stupidity :/ +namespace { + // brighten area, ignore los + sint8 + los_brighten_nolos (sint8 b, sint8 l) + { + return min (b, l); + } -sint8 -los_darken (sint8 b, sint8 l) -{ - return max (b, l); -} + // brighten area, but respect los + sint8 + los_brighten (sint8 b, sint8 l) + { + return b == LOS_BLOCKED ? b : min (b, l); + } + + // darken area, respect los + sint8 + los_darken (sint8 b, sint8 l) + { + return max (b, l); + } +}; template static void @@ -349,13 +354,13 @@ change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]); } -/* add light, by finding all (non-null) nearby light sources, then +/* add light, by finding all (non-null) nearby light sources, then * mark those squares specially. */ static void apply_lights (player *pl) { - object *op = pl->observe; + object *op = pl->viewpoint; int darklevel = op->map->darklevel (); int half_x = pl->ns->mapx / 2; @@ -363,6 +368,17 @@ int pass2 = 0; // negative lights have an extra pass + maprect *rects = pl->viewpoint->map->split_to_tiles ( + mapwalk_buf, + pl->viewpoint->x - half_x - MAX_LIGHT_RADIUS, + pl->viewpoint->y - half_y - MAX_LIGHT_RADIUS, + pl->viewpoint->x + half_x + MAX_LIGHT_RADIUS + 1, + pl->viewpoint->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 @@ -378,51 +394,64 @@ * 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 (); sint8 light = ms.light; - if (expect_false (light)) + if (ecb_expect_false (light)) 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->viewpoint->x, dy - pl->viewpoint->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); - /* If the player can see in the dark, lower the darklevel for him */ - if (op->flag [FLAG_SEE_IN_DARK]) - light += SEE_IN_DARK_RADIUS; + light = clamp (light + bonus, 0, MAX_VISION); apply_light (pl, 0, 0, light, vision_atten [light]); } } + // when we fly high, we have some minimum viewable area around us, like x-ray + if (op->move_type & MOVE_FLY_HIGH) + apply_light (pl, 0, 0, 9, vision_atten [9]); + // possibly do 2nd pass for rare negative glow radii // 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 (ecb_expect_false (light < 0)) + { + light = clamp (light - bonus, 0, MAX_DARKNESS); + apply_light (pl, dx - pl->viewpoint->x, dy - pl->viewpoint->y, -light, light_atten [light + MAX_LIGHT_RADIUS]); + } + } } -/* blinded_sight() - sets all viewable squares to blocked except +/* blinded_sight() - sets all viewable squares to blocked except * for the one the central one that the player occupies. A little * odd that you can see yourself (and what your standing on), but * really need for any reasonable game play. @@ -445,7 +474,7 @@ if (ob->flag [FLAG_WIZLOOK]) clear_los (0); - else if (observe->flag [FLAG_BLIND]) /* player is blind */ + else if (viewpoint->flag [FLAG_BLIND]) /* player is blind */ { clear_los (); blinded_sight (this); @@ -457,7 +486,7 @@ apply_lights (this); } - if (observe->flag [FLAG_XRAYS]) + if (viewpoint->flag [FLAG_XRAYS]) for (int dx = -2; dx <= 2; dx++) for (int dy = -2; dy <= 2; dy++) min_it (los[dx + LOS_X0][dy + LOS_Y0], 1); @@ -506,56 +535,12 @@ if (!pl->ob || !pl->ob->map || !pl->ns) continue; - /* Same map is simple case - see if pl is close enough. - * Note in all cases, we did the check for same map first, - * and then see if the player is close enough and update - * los if that is the case. If the player is on the - * corresponding map, but not close enough, then the - * player can't be on another map that may be closer, - * so by setting it up this way, we trim processing - * 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; - } + rv_vector rv; - /* Now we check to see if player is on adjacent - * maps to the one that changed and also within - * view. The tile_maps[] could be null, but in that - * case it should never match the pl->ob->map, so - * we want ever try to dereference any of the data in it. - * - * The logic for 0 and 3 is to see how far the player is - * from the edge of the map (height/width) - pl->ob->(x,y) - * and to add current position on this map - that gives a - * distance. - * For 1 and 2, we check to see how far the given - * coordinate (x,y) is from the corresponding edge, - * and then add the players location, which gives - * a distance. - */ - else if (pl->ob->map == map->tile_map[0]) - { - if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (y + map->tile_map[0]->height - pl->ob->y) <= pl->ns->mapy / 2)) - pl->do_los = 1; - } - else if (pl->ob->map == map->tile_map[2]) - { - if ((abs (pl->ob->x - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y + map->height - y) <= pl->ns->mapy / 2)) - pl->do_los = 1; - } - else if (pl->ob->map == map->tile_map[1]) - { - if ((abs (pl->ob->x + map->width - x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) - pl->do_los = 1; - } - else if (pl->ob->map == map->tile_map[3]) - { - if ((abs (x + map->tile_map[3]->width - pl->ob->x) <= pl->ns->mapx / 2) && (abs (pl->ob->y - y) <= pl->ns->mapy / 2)) - pl->do_los = 1; - } + get_rangevector_from_mapcoord (pl->ob->map, x, y, pl->ob, &rv); + + if ((abs (rv.distance_x) <= pl->ns->mapx / 2) && (abs (rv.distance_y) <= pl->ns->mapy / 2)) + pl->do_los = 1; } } @@ -638,3 +623,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; } +