--- deliantra/server/common/los.C 2007/05/28 21:21:40 1.26
+++ deliantra/server/common/los.C 2008/12/08 15:40:13 1.36
@@ -1,31 +1,29 @@
/*
- * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
+ * This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
- * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
+ * 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
*
- * Crossfire TRT 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 (at your option)
- * any later version.
+ * 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.
*
- * 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.
+ * 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
- * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
*
- * The authors can be reached via e-mail to
+ * 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.
@@ -198,7 +196,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);
}
}
@@ -236,10 +234,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);
}
@@ -250,14 +247,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);
}
/*
@@ -317,10 +314,33 @@
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;
+
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;
@@ -328,7 +348,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 -= LOS_MAX / 2;
/* add light, by finding all (non-null) nearby light sources, then
* mark those squares specially. If the darklevel<1, there is no
@@ -347,99 +367,109 @@
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_LIGHT_RADII;
+ if (op->contr->blocked_los[x][y] != LOS_BLOCKED)
+ op->contr->blocked_los[x][y] = LOS_MAX;
- /* 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_RADII), basex = -MAX_LIGHT_RADII;
- x <= (op->x + op->contr->ns->mapx / 2 + MAX_LIGHT_RADII); 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] != LOS_BLOCKED)
+ 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_RADII), basey = -MAX_LIGHT_RADII;
- y <= (op->y + op->contr->ns->mapy / 2 + MAX_LIGHT_RADII); y++, basey++)
+ // psosibly do 2nd pass for rare negative glow radii
+ if (expect_false (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;
+ maptile *m = op->map;
+ sint16 nx = x;
+ sint16 ny = y;
- mflags = get_map_flags (m, &m, nx, ny, &nx, &ny);
-
- 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 != 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;
+ 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 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);
- }
- }
- }
+ 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] != LOS_BLOCKED)
+ 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
*/
- if (op->map->outdoor && darklevel > (MAX_DARKNESS - 3))
+ 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;
+ if (op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] > (LOS_MAX - 3))
+ op->contr->blocked_los[op->contr->ns->mapx / 2][op->contr->ns->mapy / 2] = LOS_MAX - 3;
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 (op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] > (LOS_MAX - 2))
+ op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] = LOS_MAX - 2;
}
/* 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))
+ if (!(op->contr->blocked_los[x + op->contr->ns->mapx / 2][y + op->contr->ns->mapy / 2] == LOS_BLOCKED))
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 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.
@@ -451,7 +481,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;
}
@@ -460,7 +490,6 @@
* update_los() recalculates the array which specifies what is
* visible for the given player-object.
*/
-
void
update_los (object *op)
{
@@ -469,7 +498,8 @@
if (QUERY_FLAG (op, FLAG_REMOVED))
return;
- clear_los (op);
+ clear_los (op->contr);
+
if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ )
return;