--- deliantra/server/common/los.C 2007/02/16 21:38:18 1.22 +++ deliantra/server/common/los.C 2008/12/18 07:01:31 1.39 @@ -1,31 +1,29 @@ /* - * CrossFire, A Multiplayer game for X-windows + * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team - * Copyright (C) 2002 Mark Wedel & Crossfire Development Team - * Copyright (C) 1992 Frank Tore Johansen + * 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 * - * This program is free software; you can redistribute it and/or modify + * 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 2 of the License, or + * 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 + * 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, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * The authors can be reached via e-mail at + * along with this program. If not, see . + * + * The authors can be reached via e-mail to */ /* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */ #include -#include #include /* Distance must be less than this for the object to be blocked. @@ -34,7 +32,6 @@ * block view in our tables. * .4 or less lets you see through walls. .5 is about right. */ - #define SPACE_BLOCK 0.5 typedef struct blstr @@ -43,7 +40,11 @@ int index; } blocks; -blocks block[(MAP_CLIENT_X | 1) + 1][(MAP_CLIENT_Y | 1) + 1]; // still a speed hack +// 31/32 == a speed hack +// we would like to use 32 for speed, but the code loops endlessly +// then, reason not yet identified, so only make the array use 32, +// not the define's. +blocks block[MAP_CLIENT_X][MAP_CLIENT_Y == 31 ? 32 : MAP_CLIENT_Y]; static void expand_lighted_sight (object *op); @@ -87,33 +88,27 @@ * are the only ones further out that are still possibly in the * sightline. */ - void init_block (void) { - int x, y, dx, dy, i; - static int block_x[3] = { -1, -1, 0 }, block_y[3] = - { - -1, 0, -1}; - - for (x = 0; x < MAP_CLIENT_X; x++) - for (y = 0; y < MAP_CLIENT_Y; y++) - { - block[x][y].index = 0; - } + static int block_x[3] = { -1, -1, 0 }, + block_y[3] = { -1, 0, -1 }; + for (int x = 0; x < MAP_CLIENT_X; x++) + for (int y = 0; y < MAP_CLIENT_Y; y++) + block[x][y].index = 0; /* The table should be symmetric, so only do the upper left * quadrant - makes the processing easier. */ - for (x = 1; x <= MAP_CLIENT_X / 2; x++) + for (int x = 1; x <= MAP_CLIENT_X / 2; x++) { - for (y = 1; y <= MAP_CLIENT_Y / 2; y++) + for (int y = 1; y <= MAP_CLIENT_Y / 2; y++) { - for (i = 0; i < 3; i++) + for (int i = 0; i < 3; i++) { - dx = x + block_x[i]; - dy = y + block_y[i]; + int dx = x + block_x[i]; + int dy = y + block_y[i]; /* center space never blocks */ if (x == MAP_CLIENT_X / 2 && y == MAP_CLIENT_Y / 2) @@ -127,29 +122,25 @@ */ set_block (x, y, dx, dy); if (x == MAP_CLIENT_X / 2) - { - set_block (x, MAP_CLIENT_Y - y - 1, dx, MAP_CLIENT_Y - dy - 1); - } + set_block (x, MAP_CLIENT_Y - y - 1, dx, MAP_CLIENT_Y - dy - 1); else if (y == MAP_CLIENT_Y / 2) - { - set_block (MAP_CLIENT_X - x - 1, y, MAP_CLIENT_X - dx - 1, dy); - } + set_block (MAP_CLIENT_X - x - 1, y, MAP_CLIENT_X - dx - 1, dy); } else { float d1, r, s, l; - /* We use the algorihm that found out how close the point + /* We use the algorithm that found out how close the point * (x,y) is to the line from dx,dy to the center of the viewable - * area. l is the distance from x,y to the line. + * area. l is the distance from x,y to the line. * r is more a curiosity - it lets us know what direction (left/right) * the line is off */ - d1 = (float) (pow (MAP_CLIENT_X / 2 - dx, 2.f) + pow (MAP_CLIENT_Y / 2 - dy, 2.f)); - r = (float) ((dy - y) * (dy - MAP_CLIENT_Y / 2) - (dx - x) * (MAP_CLIENT_X / 2 - dx)) / d1; - s = (float) ((dy - y) * (MAP_CLIENT_X / 2 - dx) - (dx - x) * (MAP_CLIENT_Y / 2 - dy)) / d1; - l = FABS (sqrt (d1) * s); + d1 = (powf (MAP_CLIENT_X / 2 - dx, 2.f) + powf (MAP_CLIENT_Y / 2 - dy, 2.f)); + r = ((dy - y) * (dy - MAP_CLIENT_Y / 2) - (dx - x) * (MAP_CLIENT_X / 2 - dx)) / d1; + s = ((dy - y) * (MAP_CLIENT_X / 2 - dx) - (dx - x) * (MAP_CLIENT_Y / 2 - dy)) / d1; + l = fabs (sqrtf (d1) * s); if (l <= SPACE_BLOCK) { @@ -177,13 +168,10 @@ * spaces behind it may block other spaces, etc. * In this way, the chain of visibility is set. */ - static void set_wall (object *op, int x, int y) { - int i; - - for (i = 0; i < block[x][y].index; i++) + for (int i = 0; i < block[x][y].index; i++) { int dx = block[x][y].x[i], dy = block[x][y].y[i], ax, ay; @@ -202,7 +190,7 @@ * code wants the los to start from the 0,0 * and not be relative to middle of los array. */ - op->contr->blocked_los[ax][ay] = 100; + op->contr->blocked_los[ax][ay] = LOS_BLOCKED; set_wall (op, dx, dy); } } @@ -212,7 +200,6 @@ * op is the object, x and y values based on MAP_CLIENT_X and Y. * this is because they index the blocked[][] arrays. */ - static void check_wall (object *op, int x, int y) { @@ -240,10 +227,9 @@ * whatever has set this space to be blocked has done the work and already * done the dependency chain. */ - if (op->contr->blocked_los[ax][ay] == 100) + if (op->contr->blocked_los[ax][ay] == LOS_BLOCKED) return; - if (get_map_flags (op->map, NULL, op->x + x - MAP_CLIENT_X / 2, op->y + y - MAP_CLIENT_Y / 2, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP)) set_wall (op, x, y); } @@ -254,14 +240,14 @@ */ void -clear_los (object *op) +clear_los (player *pl) { /* This is safer than using the ns->mapx, mapy because * we index the blocked_los as a 2 way array, so clearing * the first z spaces may not not cover the spaces we are * actually going to use */ - (void) memset ((void *) op->contr->blocked_los, 0, MAP_CLIENT_X * MAP_CLIENT_Y); + memset (pl->blocked_los, 0, MAP_CLIENT_X * MAP_CLIENT_Y); } /* @@ -270,37 +256,31 @@ * to a certain degree, be able to see into corners. * This is somewhat suboptimal, would be better to improve the formula. */ - static void expand_sight (object *op) { - int i, x, y, dx, dy; - - for (x = 1; x < op->contr->ns->mapx - 1; x++) /* loop over inner squares */ - for (y = 1; y < op->contr->ns->mapy - 1; y++) - { - if (!op->contr->blocked_los[x][y] && - !(get_map_flags (op->map, NULL, - op->x - op->contr->ns->mapx / 2 + x, - op->y - op->contr->ns->mapy / 2 + y, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP))) - { + for (int x = 1; x < op->contr->ns->mapx - 1; x++) /* loop over inner squares */ + for (int y = 1; y < op->contr->ns->mapy - 1; y++) + if (!op->contr->blocked_los[x][y] && + !(get_map_flags (op->map, NULL, + op->x - op->contr->ns->mapx / 2 + x, + op->y - op->contr->ns->mapy / 2 + y, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP))) + { + for (int i = 1; i <= 8; i += 1) + { /* mark all directions */ + int dx = x + freearr_x[i]; + int dy = y + freearr_y[i]; - for (i = 1; i <= 8; i += 1) - { /* mark all directions */ - dx = x + freearr_x[i]; - dy = y + freearr_y[i]; - if (op->contr->blocked_los[dx][dy] > 0) /* for any square blocked */ - op->contr->blocked_los[dx][dy] = -1; - } - } - } + if (op->contr->blocked_los[dx][dy] > 0) /* for any square blocked */ + op->contr->blocked_los[dx][dy] = -1; + } + } - if (op->map->darkness > 0) /* player is on a dark map */ - expand_lighted_sight (op); + expand_lighted_sight (op); /* clear mark squares */ - for (x = 0; x < op->contr->ns->mapx; x++) - for (y = 0; y < op->contr->ns->mapy; y++) + for (int x = 0; x < op->contr->ns->mapx; x++) + for (int y = 0; y < op->contr->ns->mapy; y++) if (op->contr->blocked_los[x][y] < 0) op->contr->blocked_los[x][y] = 0; } @@ -310,7 +290,6 @@ * 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) { @@ -321,10 +300,72 @@ return 0; } +/* radius, distance => lightness adjust */ +static sint8 darkness[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 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 * 3 / 2; ++distance) + { + // max intensity + int intensity = min (LOS_MAX, 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 + ? min (3, intensity) + : LOS_MAX - intensity; + } + } +} darkness_init; + +sint8 +los_brighten (sint8 b, sint8 l) +{ + return b == LOS_BLOCKED ? b : min (b, l); +} + +sint8 +los_brighten_blocked (sint8 b, sint8 l) +{ + return min (b, l); +} + +sint8 +los_darken (sint8 b, sint8 l) +{ + return max (b, l); +} + +template +static void +apply_light (object *op, int basex, int basey, int light, const sint8 *darkness_table) +{ + // min or max the ciruclar area around basex, basey + player *pl = op->contr; + + int ax0 = max (0, basex - light); + int ay0 = max (0, basey - light); + int ax1 = min (basex + light, pl->ns->mapx - 1); + int ay1 = min (basey + light, pl->ns->mapy - 1); + + for (int ax = ax0; ax <= ax1; ax++) + for (int ay = ay0; ay <= ay1; ay++) + pl->blocked_los[ax][ay] = + change_it (pl->blocked_los[ax][ay], darkness_table [idistance (ax - basex, ay - basey)]); +} + +/* add light, by finding all (non-null) nearby light sources, then + * mark those squares specially. + */ static void expand_lighted_sight (object *op) { - int x, y, darklevel, ax, ay, basex, basey, mflags, light, x1, y1; + int darklevel, mflags, light, x1, y1; maptile *m = op->map; sint16 nx, ny; @@ -332,15 +373,7 @@ /* If the player can see in the dark, lower the darklevel for him */ if (QUERY_FLAG (op, FLAG_SEE_IN_DARK)) - darklevel -= 2; - - /* add light, by finding all (non-null) nearby light sources, then - * mark those squares specially. If the darklevel<1, there is no - * reason to do this, so we skip this function - */ - - if (darklevel < 1) - return; + darklevel -= LOS_MAX / 2; /* Do a sanity check. If not valid, some code below may do odd * things. @@ -351,99 +384,86 @@ darklevel = MAX_DARKNESS; } - /* First, limit player furthest (unlighted) vision */ - 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_LIGHT_RADII; - - /* the spaces[] darkness value contains the information we need. - * 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_RADII), basex = -MAX_LIGHT_RADII; - x <= (op->x + op->contr->ns->mapx / 2 + MAX_LIGHT_RADII); x++, basex++) - { - - for (y = (op->y - op->contr->ns->mapy / 2 - MAX_LIGHT_RADII), basey = -MAX_LIGHT_RADII; - y <= (op->y + op->contr->ns->mapy / 2 + MAX_LIGHT_RADII); y++, basey++) - { - m = op->map; - nx = x; - ny = y; + int half_x = op->contr->ns->mapx / 2; + int half_y = op->contr->ns->mapy / 2; - mflags = get_map_flags (m, &m, nx, ny, &nx, &ny); + 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; - if (mflags & P_OUT_OF_MAP) - continue; + int pass2 = 0; // negative lights have an extra pass - /* This space is providing light, so we need to brighten up the - * spaces around here. - */ - light = GET_MAP_LIGHT (m, nx, ny); - if (light != 0) - { -#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; + if (darklevel < 1) + pass2 = 1; + else + { + /* first, make everything totally dark */ + for (int x = 0; x < op->contr->ns->mapx; x++) + for (int y = 0; y < op->contr->ns->mapy; y++) + if (op->contr->blocked_los[x][y] != LOS_BLOCKED) + op->contr->blocked_los[x][y] = LOS_MAX; + + /* + * 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 (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 + apply_light (op, basex, basey, light, darkness [light + MAX_LIGHT_RADIUS]); + } - for (ay = basey - light; ay <= basey + light; ay++) - { - if (ay < 0 || ay >= op->contr->ns->mapy) - continue; + /* grant some vision to the player, based on the darklevel */ + /* for outdoor maps, ensure some mininum visibility radius */ + { + int light = clamp (MAX_DARKNESS - darklevel, op->map->outdoor ? 2 : 0, 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 circular manner - * using the the pythagorean theorem. glow_radius still - * represents the radius - */ - if (op->contr->blocked_los[ax][ay] != 100) - { - x1 = abs (basex - ax) * abs (basex - ax); - y1 = abs (basey - ay) * abs (basey - ay); - - if (light > 0) op->contr->blocked_los[ax][ay] -= max (light - isqrt (x1 + y1), 0); - if (light < 0) op->contr->blocked_los[ax][ay] -= min (light + isqrt (x1 + y1), 0); - } - } - } - } - } + apply_light (op, half_x, half_y, light, darkness [light + MAX_LIGHT_RADIUS]); + } } - /* Outdoor should never really be completely pitch black dark like - * a dungeon, so let the player at least see a little around themselves - */ - if (op->map->outdoor && darklevel > (MAX_DARKNESS - 3)) - { - if (op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] > (MAX_DARKNESS - 3)) - op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] = MAX_DARKNESS - 3; + // 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) + 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; - for (x = -1; x <= 1; x++) - for (y = -1; y <= 1; y++) - { - if (op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] > (MAX_DARKNESS - 2)) - op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] = MAX_DARKNESS - 2; - } - } + if (!xy_normalise (m, nx, ny)) + continue; + + mapspace &ms = m->at (nx, ny); + ms.update (); + sint8 light = ms.light; - /* grant some vision to the player, based on the darklevel */ - for (x = darklevel - MAX_DARKNESS; x < MAX_DARKNESS + 1 - darklevel; x++) - 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))); + if (expect_false (light < 0)) + apply_light (op, basex, basey, -light, darkness [light + MAX_LIGHT_RADIUS]); + } } -/* blinded_sight() - sets all veiwable 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. @@ -455,7 +475,7 @@ for (x = 0; x < op->contr->ns->mapx; x++) for (y = 0; y < op->contr->ns->mapy; y++) - op->contr->blocked_los[x][y] = 100; + op->contr->blocked_los[x][y] = LOS_BLOCKED; op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] = 0; } @@ -464,7 +484,6 @@ * update_los() recalculates the array which specifies what is * visible for the given player-object. */ - void update_los (object *op) { @@ -473,7 +492,8 @@ if (QUERY_FLAG (op, FLAG_REMOVED)) return; - clear_los (op); + clear_los (op->contr); + if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ ) return;