--- deliantra/server/common/los.C 2006/09/10 16:00:23 1.4
+++ deliantra/server/common/los.C 2009/01/08 22:35:00 1.58
@@ -1,521 +1,479 @@
-
/*
- * static char *rcsid_los_c =
- * "$Id: los.C,v 1.4 2006/09/10 16:00:23 root Exp $";
+ * 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.
+ *
+ * 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 .
+ *
+ * The authors can be reached via e-mail to
*/
-/*
- CrossFire, A Multiplayer game for X-windows
-
- Copyright (C) 2002 Mark Wedel & Crossfire Development Team
- Copyright (C) 1992 Frank Tore Johansen
-
- This program 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.
-
- 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, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- The authors can be reached via e-mail at crossfire-devel@real-time.com
-*/
+#include
+#include
-/* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */
+#define SEE_IN_DARK_RADIUS 2
+#define MAX_VISION 10 // maximum visible radius
-#include
-#include
-#include
+// los flags
+enum {
+ FLG_XI = 0x01, // we have an x-parent
+ FLG_YI = 0x02, // we have an y-parent
+ FLG_BLOCKED = 0x04, // this space blocks the view
+ FLG_QUEUED = 0x80 // already queued in queue, or border
+};
+struct los_info
+{
+ uint8 flags; // FLG_xxx
+ uint8 culled; // culled from "tree"
+ uint8 visible;
+ uint8 pad0;
-/* Distance must be less than this for the object to be blocked.
- * An object is 1.0 wide, so if set to 0.5, it means the object
- * that blocks half the view (0.0 is complete block) will
- * block view in our tables.
- * .4 or less lets you see through walls. .5 is about right.
- */
+ sint8 xo, yo; // obscure angle
+ sint8 xe, ye; // angle deviation
+};
-#define SPACE_BLOCK 0.5
+// temporary storage for the los algorithm,
+// one los_info for each lightable map space
+static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y];
-typedef struct blstr
+struct point
{
- int x[4], y[4];
- int index;
-} blocks;
+ sint8 x, y;
+};
-blocks block[MAP_CLIENT_X][MAP_CLIENT_Y];
+// minimum size, but must be a power of two
+#define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2)
-static void expand_lighted_sight (object *op);
+// a queue of spaces to calculate
+static point queue [QUEUE_LENGTH];
+static int q1, q2; // queue start, end
/*
- * Used to initialise the array used by the LOS routines.
- * What this sets if that x,y blocks the view of bx,by
- * This then sets up a relation - for example, something
- * at 5,4 blocks view at 5,3 which blocks view at 5,2
- * etc. So when we check 5,4 and find it block, we have
- * the data to know that 5,3 and 5,2 and 5,1 should also
- * be blocked.
+ * Clears/initialises the los-array associated to the player
+ * controlling the object.
*/
+void
+player::clear_los (sint8 value)
+{
+ memset (los, value, sizeof (los));
+}
+// enqueue a single mapspace, but only if it hasn't
+// been enqueued yet.
static void
-set_block (int x, int y, int bx, int by)
+enqueue (sint8 dx, sint8 dy, uint8 flags = 0)
{
- int index = block[x][y].index, i;
+ sint8 x = LOS_X0 + dx;
+ sint8 y = LOS_Y0 + dy;
- /* Due to flipping, we may get duplicates - better safe than sorry.
- */
- for (i = 0; i < index; i++)
- {
- if (block[x][y].x[i] == bx && block[x][y].y[i] == by)
- return;
- }
+ los_info &l = los[x][y];
- block[x][y].x[index] = bx;
- block[x][y].y[index] = by;
- block[x][y].index++;
-#ifdef LOS_DEBUG
- LOG (llevDebug, "setblock: added %d %d -> %d %d (%d)\n", x, y, bx, by, block[x][y].index);
-#endif
-}
+ l.flags |= flags;
-/*
- * initialises the array used by the LOS routines.
- */
+ if (l.flags & FLG_QUEUED)
+ return;
-/* since we are only doing the upper left quadrant, only
- * these spaces could possibly get blocked, since these
- * are the only ones further out that are still possibly in the
- * sightline.
- */
+ l.flags |= FLG_QUEUED;
-void
-init_block (void)
+ queue[q1].x = dx;
+ queue[q1].y = dy;
+
+ q1 = (q1 + 1) & (QUEUE_LENGTH - 1);
+}
+
+// run the los algorithm
+// this is a variant of a spiral los algorithm taken from
+// http://www.geocities.com/temerra/los_rays.html
+// which has been simplified and changed considerably, but
+// still is basically the same algorithm.
+static void
+calculate_los (player *pl)
{
- int x, y, dx, dy, i;
- static int block_x[3] = { -1, -1, 0 }, block_y[3] =
{
- -1, 0, -1};
+ memset (los, 0, sizeof (los));
- for (x = 0; x < MAP_CLIENT_X; x++)
- for (y = 0; y < MAP_CLIENT_Y; y++)
+ // 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);
+ int half_y = min (LOS_Y0 - 1, pl->ns->mapy / 2);
+
+ // create borders, the corners are not touched
+ for (int dx = -half_x; dx <= half_x; ++dx)
+ los [dx + LOS_X0][LOS_Y0 - (half_y + 1)].flags =
+ los [dx + LOS_X0][LOS_Y0 + (half_y + 1)].flags = FLG_QUEUED;
+
+ for (int dy = -half_y; dy <= half_y; ++dy)
+ los [LOS_X0 - (half_x + 1)][dy + LOS_Y0].flags =
+ los [LOS_X0 + (half_x + 1)][dy + LOS_Y0].flags = FLG_QUEUED;
+
+ // 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. 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.
+ unordered_mapwalk (pl->observe, -half_x, -half_y, half_x, half_y)
{
- block[x][y].index = 0;
+ 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
+ enqueue (0, 0); // enqueue center
- /* 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++)
+ // treat the origin specially
+ los[LOS_X0][LOS_Y0].visible = 1;
+ pl->los[LOS_X0][LOS_Y0] = 0;
+
+ // loop over all enqueued points until the queue is empty
+ // the order in which this is done ensures that we
+ // never touch a mapspace whose input spaces we haven't checked
+ // yet.
+ while (q1 != q2)
{
- for (y = 1; y <= MAP_CLIENT_Y / 2; y++)
+ sint8 dx = queue[q2].x;
+ sint8 dy = queue[q2].y;
+
+ q2 = (q2 + 1) & (QUEUE_LENGTH - 1);
+
+ sint8 x = LOS_X0 + dx;
+ sint8 y = LOS_Y0 + dy;
+
+ los_info &l = los[x][y];
+
+ if (expect_true (l.flags & (FLG_XI | FLG_YI)))
{
- for (i = 0; i < 3; i++)
+ l.culled = 1;
+ l.xo = l.yo = l.xe = l.ye = 0;
+
+ // check contributing spaces, first horizontal
+ if (expect_true (l.flags & FLG_XI))
{
- dx = x + block_x[i];
- dy = y + block_y[i];
+ los_info *xi = &los[x - sign (dx)][y];
- /* center space never blocks */
- if (x == MAP_CLIENT_X / 2 && y == MAP_CLIENT_Y / 2)
- continue;
+ // don't cull unless obscured
+ l.culled &= !xi->visible;
- /* If its a straight line, its blocked */
- if ((dx == x && x == MAP_CLIENT_X / 2) || (dy == y && y == MAP_CLIENT_Y / 2))
+ /* merge input space */
+ if (expect_false (xi->xo || xi->yo))
{
- /* For simplicity, we mirror the coordinates to block the other
- * quadrants.
- */
- set_block (x, y, dx, dy);
- if (x == MAP_CLIENT_X / 2)
+ // The X input can provide two main pieces of information:
+ // 1. Progressive X obscurity.
+ // 2. Recessive Y obscurity.
+
+ // Progressive X obscurity, favouring recessive input angle
+ if (xi->xe > 0 && l.xo == 0)
{
- set_block (x, MAP_CLIENT_Y - y - 1, dx, MAP_CLIENT_Y - dy - 1);
- }
- else if (y == MAP_CLIENT_Y / 2)
+ l.xe = xi->xe - xi->yo;
+ l.ye = xi->ye + xi->yo;
+ l.xo = xi->xo;
+ l.yo = xi->yo;
+ }
+
+ // Recessive Y obscurity
+ if (xi->ye <= 0 && xi->yo > 0 && xi->xe > 0)
{
- set_block (MAP_CLIENT_X - x - 1, y, MAP_CLIENT_X - dx - 1, dy);
- }
+ l.ye = xi->yo + xi->ye;
+ l.xe = xi->xe - xi->yo;
+ l.xo = xi->xo;
+ l.yo = xi->yo;
+ }
}
- else
- {
- float d1, r, s, l;
+ }
+
+ // check contributing spaces, last vertical, identical structure
+ if (expect_true (l.flags & FLG_YI))
+ {
+ los_info *yi = &los[x][y - sign (dy)];
- /* We use the algorihm 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.
- * 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) + pow (MAP_CLIENT_Y / 2 - dy, 2));
- 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);
+ // don't cull unless obscured
+ l.culled &= !yi->visible;
- if (l <= SPACE_BLOCK)
+ /* merge input space */
+ if (expect_false (yi->yo || yi->xo))
+ {
+ // The Y input can provide two main pieces of information:
+ // 1. Progressive Y obscurity.
+ // 2. Recessive X obscurity.
+
+ // Progressive Y obscurity, favouring recessive input angle
+ if (yi->ye > 0 && l.yo == 0)
{
- /* For simplicity, we mirror the coordinates to block the other
- * quadrants.
- */
- set_block (x, y, dx, dy);
- set_block (MAP_CLIENT_X - x - 1, y, MAP_CLIENT_X - dx - 1, dy);
- set_block (x, MAP_CLIENT_Y - y - 1, dx, MAP_CLIENT_Y - dy - 1);
- set_block (MAP_CLIENT_X - x - 1, MAP_CLIENT_Y - y - 1, MAP_CLIENT_X - dx - 1, MAP_CLIENT_Y - dy - 1);
- }
+ l.ye = yi->ye - yi->xo;
+ l.xe = yi->xe + yi->xo;
+ l.yo = yi->yo;
+ l.xo = yi->xo;
+ }
+
+ // Recessive X obscurity
+ if (yi->xe <= 0 && yi->xo > 0 && yi->ye > 0)
+ {
+ l.xe = yi->xo + yi->xe;
+ l.ye = yi->ye - yi->xo;
+ l.yo = yi->yo;
+ l.xo = yi->xo;
+ }
}
}
- }
- }
-}
-/*
- * Used to initialise the array used by the LOS routines.
- * x,y are indexes into the blocked[][] array.
- * This recursively sets the blocked line of sight view.
- * From the blocked[][] array, we know for example
- * that if some particular space is blocked, it blocks
- * the view of the spaces 'behind' it, and those blocked
- * spaces behind it may block other spaces, etc.
- * In this way, the chain of visibility is set.
- */
+ if (l.flags & FLG_BLOCKED)
+ {
+ l.xo = l.xe = abs (dx);
+ l.yo = l.ye = abs (dy);
-static void
-set_wall (object *op, int x, int y)
-{
- int i;
+ // we obscure dependents, but might be visible
+ // copy the los from the square towards the player,
+ // so outward diagonal corners are lit.
+ pl->los[x][y] = los[x - sign0 (dx)][y - sign0 (dy)].visible ? 0 : LOS_BLOCKED;
- for (i = 0; i < block[x][y].index; i++)
- {
- int dx = block[x][y].x[i], dy = block[x][y].y[i], ax, ay;
+ l.visible = false;
+ }
+ else
+ {
+ // we are not blocked, so calculate visibility, by checking
+ // whether we are inside or outside the shadow
+ l.visible = (l.xe <= 0 || l.xe > l.xo)
+ && (l.ye <= 0 || l.ye > l.yo);
+
+ pl->los[x][y] = l.culled ? LOS_BLOCKED
+ : l.visible ? 0
+ : 3;
+ }
- /* ax, ay are the values as adjusted to be in the
- * socket look structure.
- */
- ax = dx - (MAP_CLIENT_X - op->contr->socket.mapx) / 2;
- ay = dy - (MAP_CLIENT_Y - op->contr->socket.mapy) / 2;
+ }
- if (ax < 0 || ax >= op->contr->socket.mapx || ay < 0 || ay >= op->contr->socket.mapy)
- continue;
-#if 0
- LOG (llevDebug, "blocked %d %d -> %d %d\n", dx, dy, ax, ay);
-#endif
- /* we need to adjust to the fact that the socket
- * 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;
- set_wall (op, dx, dy);
+ // 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
+ // positive and negative directions.
+ if (!l.culled)
+ {
+ if (dx >= 0) enqueue (dx + 1, dy, FLG_XI);
+ if (dx <= 0) enqueue (dx - 1, dy, FLG_XI);
+ if (dy >= 0) enqueue (dx, dy + 1, FLG_YI);
+ if (dy <= 0) enqueue (dx, dy - 1, FLG_YI);
+ }
}
}
-/*
- * Used to initialise the array used by the LOS routines.
- * op is the object, x and y values based on MAP_CLIENT_X and Y.
- * this is because they index the blocked[][] arrays.
- */
+/* radius, distance => lightness adjust */
+static sint8 light_atten[MAX_LIGHT_RADIUS * 2 + 1][MAX_LIGHT_RADIUS * 3 / 2 + 1];
+static sint8 vision_atten[MAX_VISION + 1][MAX_VISION * 3 / 2 + 1];
-static void
-check_wall (object *op, int x, int y)
+static struct los_init
{
- int ax, ay;
+ los_init ()
+ {
+ assert (("QUEUE_LENGTH, MAP_CLIENT_X and MAP_CLIENT_Y *must* be powers of two",
+ !(QUEUE_LENGTH & (QUEUE_LENGTH - 1))));
- if (!block[x][y].index)
- return;
+ /* for lights */
+ 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);
- /* ax, ay are coordinates as indexed into the look window */
- ax = x - (MAP_CLIENT_X - op->contr->socket.mapx) / 2;
- ay = y - (MAP_CLIENT_Y - op->contr->socket.mapy) / 2;
+ // actual intensity
+ intensity = max (0, lerp_rd (distance, 0, abs (radius) + 1, intensity, 0));
- /* If the converted coordinates are outside the viewable
- * area for the client, return now.
- */
- if (ax < 0 || ay < 0 || ax >= op->contr->socket.mapx || ay >= op->contr->socket.mapy)
- return;
-
-#if 0
- LOG (llevDebug, "check_wall, ax,ay=%d, %d x,y = %d, %d blocksview = %d, %d\n",
- ax, ay, x, y, op->x + x - MAP_CLIENT_X / 2, op->y + y - MAP_CLIENT_Y / 2);
-#endif
-
- /* If this space is already blocked, prune the processing - presumably
- * 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)
- return;
+ light_atten [radius + MAX_LIGHT_RADIUS][distance] = radius < 0
+ ? min (3, intensity)
+ : LOS_MAX - intensity;
+ }
+ /* for general vision */
+ 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;
- 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);
+sint8
+los_brighten (sint8 b, sint8 l)
+{
+ return b == LOS_BLOCKED ? b : min (b, l);
}
-/*
- * Clears/initialises the los-array associated to the player
- * controlling the object.
- */
-
-void
-clear_los (object *op)
+sint8
+los_darken (sint8 b, sint8 l)
{
- /* This is safer than using the socket->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);
+ return max (b, l);
}
-/*
- * expand_sight goes through the array of what the given player is
- * able to see, and expands the visible area a bit, so the player will,
- * to a certain degree, be able to see into corners.
- * This is somewhat suboptimal, would be better to improve the formula.
- */
-
+template
static void
-expand_sight (object *op)
+apply_light (player *pl, int dx, int dy, int light, const sint8 *atten_table)
{
- int i, x, y, dx, dy;
-
- for (x = 1; x < op->contr->socket.mapx - 1; x++) /* loop over inner squares */
- for (y = 1; y < op->contr->socket.mapy - 1; y++)
- {
- if (!op->contr->blocked_los[x][y] &&
- !(get_map_flags (op->map, NULL,
- op->x - op->contr->socket.mapx / 2 + x,
- op->y - op->contr->socket.mapy / 2 + y, NULL, NULL) & (P_BLOCKSVIEW | P_OUT_OF_MAP)))
- {
-
- 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;
- }
- }
- }
+ // min or max the circular area around basex, basey
+ dx += LOS_X0;
+ dy += LOS_Y0;
- if (MAP_DARKNESS (op->map) > 0) /* player is on a dark map */
- expand_lighted_sight (op);
+ int hx = pl->ns->mapx / 2;
+ int hy = pl->ns->mapy / 2;
+ int ax0 = max (LOS_X0 - hx, dx - light);
+ int ay0 = max (LOS_Y0 - hy, dy - light);
+ int ax1 = min (dx + light, LOS_X0 + hx);
+ int ay1 = min (dy + light, LOS_Y0 + hy);
- /* clear mark squares */
- for (x = 0; x < op->contr->socket.mapx; x++)
- for (y = 0; y < op->contr->socket.mapy; y++)
- if (op->contr->blocked_los[x][y] < 0)
- op->contr->blocked_los[x][y] = 0;
+ for (int ax = ax0; ax <= ax1; ax++)
+ for (int ay = ay0; ay <= ay1; ay++)
+ pl->los[ax][ay] =
+ change_it (pl->los[ax][ay], atten_table [idistance (ax - dx, ay - dy)]);
}
-
-
-
-/* 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.
+/* add light, by finding all (non-null) nearby light sources, then
+ * mark those squares specially.
*/
-
-int
-has_carried_lights (const object *op)
-{
- /* op may glow! */
- if (op->glow_radius > 0)
- return 1;
-
- return 0;
-}
-
static void
-expand_lighted_sight (object *op)
+apply_lights (player *pl)
{
- int x, y, darklevel, ax, ay, basex, basey, mflags, light, x1, y1;
- mapstruct *m = op->map;
- sint16 nx, ny;
+ object *op = pl->observe;
+ int darklevel = op->map->darklevel ();
- darklevel = MAP_DARKNESS (m);
+ int half_x = pl->ns->mapx / 2;
+ int half_y = pl->ns->mapy / 2;
- /* If the player can see in the dark, lower the darklevel for him */
- if (QUERY_FLAG (op, FLAG_SEE_IN_DARK))
- darklevel -= 2;
+ int pass2 = 0; // negative lights have an extra pass
- /* 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
- */
+ 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 (darklevel < 1)
- return;
+ /* 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;
- /* Do a sanity check. If not valid, some code below may do odd
- * things.
- */
- if (darklevel > MAX_DARKNESS)
- {
- LOG (llevError, "Map darkness for %s on %s is too high (%d)\n", &op->name, op->map->path, darklevel);
- darklevel = MAX_DARKNESS;
- }
-
- /* First, limit player furthest (unlighted) vision */
- for (x = 0; x < op->contr->socket.mapx; x++)
- for (y = 0; y < op->contr->socket.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->socket.mapx / 2 - MAX_LIGHT_RADII), basex = -MAX_LIGHT_RADII;
- x <= (op->x + op->contr->socket.mapx / 2 + MAX_LIGHT_RADII); x++, basex++)
+ if (!darklevel)
+ pass2 = 1;
+ else
{
-
- for (y = (op->y - op->contr->socket.mapy / 2 - MAX_LIGHT_RADII), basey = -MAX_LIGHT_RADII;
- y <= (op->y + op->contr->socket.mapy / 2 + MAX_LIGHT_RADII); y++, basey++)
- {
- m = op->map;
- nx = x;
- ny = y;
-
- mflags = get_map_flags (m, &m, nx, ny, &nx, &ny);
-
- if (mflags & P_OUT_OF_MAP)
- 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++)
+ /* first, make everything totally dark */
+ for (int dx = -half_x; dx <= half_x; dx++)
+ for (int dy = -half_x; dy <= half_y; dy++)
+ max_it (pl->los[dx + LOS_X0][dy + LOS_Y0], LOS_MAX);
+
+ /*
+ * Only process the area of interest.
+ * the basex, basey values represent the position in the op->contr->los
+ * array. Its easier to just increment them here (and start with the right
+ * value) than to recalculate them down below.
+ */
+ 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 (light < 0)
+ pass2 = 1;
+ else
{
- if (ax < 0 || ax >= op->contr->socket.mapx)
- continue;
- for (ay = basey - light; ay <= basey + light; ay++)
- {
- if (ay < 0 || ay >= op->contr->socket.mapy)
- continue;
+ 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]);
+ }
+ }
- /* 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 ay */
- } /* for ax */
- } /* if this space is providing light */
- } /* for y */
- } /* for x */
+ /* grant some vision to the player, based on outside, outdoor, and darklevel */
+ {
+ int light;
- /* 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->socket.mapx / 2][op->contr->socket.mapy / 2] > (MAX_DARKNESS - 3))
- op->contr->blocked_los[op->contr->socket.mapx / 2][op->contr->socket.mapy / 2] = MAX_DARKNESS - 3;
+ 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);
- for (x = -1; x <= 1; x++)
- for (y = -1; y <= 1; y++)
- {
- if (op->contr->blocked_los[x + op->contr->socket.mapx / 2][y + op->contr->socket.mapy / 2] > (MAX_DARKNESS - 2))
- op->contr->blocked_los[x + op->contr->socket.mapx / 2][y + op->contr->socket.mapy / 2] = MAX_DARKNESS - 2;
- }
+ light = clamp (light, 0, MAX_VISION);
+
+ apply_light (pl, 0, 0, light, vision_atten [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->socket.mapx / 2][y + op->contr->socket.mapy / 2] == 100))
- op->contr->blocked_los[x + op->contr->socket.mapx / 2][y + op->contr->socket.mapy / 2] -=
- MAX (0, 6 - darklevel - MAX (abs (x), abs (y)));
+
+ // 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 (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 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.
*/
-
static void
-blinded_sight (object *op)
+blinded_sight (player *pl)
{
- int x, y;
-
- for (x = 0; x < op->contr->socket.mapx; x++)
- for (y = 0; y < op->contr->socket.mapy; y++)
- op->contr->blocked_los[x][y] = 100;
-
- op->contr->blocked_los[op->contr->socket.mapx / 2][op->contr->socket.mapy / 2] = 0;
+ pl->los[LOS_X0][LOS_Y0] = 1;
}
/*
* update_los() recalculates the array which specifies what is
* visible for the given player-object.
*/
-
void
-update_los (object *op)
+player::update_los ()
{
- int dx = op->contr->socket.mapx / 2, dy = op->contr->socket.mapy / 2, x, y;
-
- if (QUERY_FLAG (op, FLAG_REMOVED))
- return;
-
- clear_los (op);
- if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ )
+ if (ob->flag [FLAG_REMOVED])//D really needed?
return;
- /* For larger maps, this is more efficient than the old way which
- * used the chaining of the block array. Since many space views could
- * be blocked by different spaces in front, this mean that a lot of spaces
- * could be examined multile times, as each path would be looked at.
- */
- for (x = (MAP_CLIENT_X - op->contr->socket.mapx) / 2 - 1; x < (MAP_CLIENT_X + op->contr->socket.mapx) / 2 + 1; x++)
- for (y = (MAP_CLIENT_Y - op->contr->socket.mapy) / 2 - 1; y < (MAP_CLIENT_Y + op->contr->socket.mapy) / 2 + 1; y++)
- check_wall (op, x, y);
-
-
- /* do the los of the player. 3 (potential) cases */
- if (QUERY_FLAG (op, FLAG_BLIND)) /* player is blind */
- blinded_sight (op);
+ if (ob->flag [FLAG_WIZLOOK])
+ clear_los (0);
+ else if (observe->flag [FLAG_BLIND]) /* player is blind */
+ {
+ clear_los ();
+ blinded_sight (this);
+ }
else
- expand_sight (op);
-
- if (QUERY_FLAG (op, FLAG_XRAYS))
{
- int x, y;
-
- for (x = -2; x <= 2; x++)
- for (y = -2; y <= 2; y++)
- op->contr->blocked_los[dx + x][dy + y] = 0;
+ clear_los ();
+ calculate_los (this);
+ apply_lights (this);
}
+
+ if (observe->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);
}
/* update all_map_los is like update_all_los below,
* but updates everyone on the map, no matter where they
- * are. This generally should not be used, as a per
+ * are. This generally should not be used, as a per
* specific map change doesn't make much sense when tiling
* is considered (lowering darkness would certainly be a
* strange effect if done on a tile map, as it makes
@@ -525,18 +483,12 @@
* change_map_light function
*/
void
-update_all_map_los (mapstruct *map)
+update_all_map_los (maptile *map)
{
- player *pl;
-
- for (pl = first_player; pl != NULL; pl = pl->next)
- {
- if (pl->ob->map == map)
- pl->do_los = 1;
- }
+ for_all_players_on_map (pl, map)
+ pl->do_los = 1;
}
-
/*
* This function makes sure that update_los() will be called for all
* players on the given map within the next frame.
@@ -549,18 +501,21 @@
*
* map is the map that changed, x and y are the coordinates.
*/
-
void
-update_all_los (const mapstruct *map, int x, int y)
+update_all_los (const maptile *map, int x, int y)
{
- player *pl;
+ // no need to do anything if we don't have darkness
+ if (map->darklevel () <= 0)
+ return;
+
+ map->at (x, y).invalidate ();
- for (pl = first_player; pl != NULL; pl = pl->next)
+ for_all_players (pl)
{
/* Player should not have a null map, but do this
* check as a safety
*/
- if (!pl->ob->map)
+ if (!pl->ob || !pl->ob->map || !pl->ns)
continue;
/* Same map is simple case - see if pl is close enough.
@@ -573,18 +528,16 @@
* some.
*/
if (pl->ob->map == map)
- {
- if ((abs (pl->ob->x - x) <= pl->socket.mapx / 2) && (abs (pl->ob->y - y) <= pl->socket.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
* 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
+ *
+ * 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.
@@ -595,72 +548,90 @@
*/
else if (pl->ob->map == map->tile_map[0])
{
- if ((abs (pl->ob->x - x) <= pl->socket.mapx / 2) && (abs (y + MAP_HEIGHT (map->tile_map[0]) - pl->ob->y) <= pl->socket.mapy / 2))
+ 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->socket.mapx / 2) && (abs (pl->ob->y + MAP_HEIGHT (map) - y) <= pl->socket.mapy / 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 (map) - x) <= pl->socket.mapx / 2) && (abs (pl->ob->y - y) <= pl->socket.mapy / 2))
+ 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_WIDTH (map->tile_map[3]) - pl->ob->x) <= pl->socket.mapx / 2) && (abs (pl->ob->y - y) <= pl->socket.mapy / 2))
+ 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;
}
}
}
+static const int season_darkness[5][HOURS_PER_DAY] = {
+ /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 */
+ { 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5 },
+ { 5, 5, 4, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4 },
+ { 5, 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4 },
+ { 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4 },
+ { 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4 }
+};
+
/*
- * Debug-routine which dumps the array which specifies the visible
- * area of a player. Triggered by the z key in DM mode.
+ * Tell players the time and compute the darkness level for all maps in the game.
+ * MUST be called exactly once per hour.
*/
-
void
-print_los (object *op)
+maptile::adjust_daylight ()
{
- int x, y;
- char buf[50], buf2[10];
+ timeofday_t tod;
- strcpy (buf, " ");
- for (x = 0; x < op->contr->socket.mapx; x++)
- {
- sprintf (buf2, "%2d", x);
- strcat (buf, buf2);
- }
- new_draw_info (NDI_UNIQUE, 0, op, buf);
- for (y = 0; y < op->contr->socket.mapy; y++)
- {
- sprintf (buf, "%2d:", y);
- for (x = 0; x < op->contr->socket.mapx; x++)
- {
- sprintf (buf2, " %1d", op->contr->blocked_los[x][y]);
- strcat (buf, buf2);
- }
- new_draw_info (NDI_UNIQUE, 0, op, buf);
- }
+ get_tod (&tod);
+
+ // log the time to log-1 every hour, and to chat every day
+ {
+ char todbuf[512];
+
+ format_tod (todbuf, sizeof (todbuf), &tod);
+
+ for_all_players (pl)
+ pl->ns->send_msg (NDI_GREY, tod.hour == 15 ? CHAT_CHANNEL : LOG_CHANNEL, todbuf);
+ }
+
+ /* If the light level isn't changing, no reason to do all
+ * the work below.
+ */
+ sint8 new_darkness = season_darkness[tod.season][tod.hour];
+
+ if (new_darkness == maptile::outdoor_darkness)
+ return;
+
+ new_draw_info (NDI_GREY | NDI_UNIQUE | NDI_ALL, 1, 0,
+ new_darkness > maptile::outdoor_darkness
+ ? "It becomes darker."
+ : "It becomes brighter.");
+
+ maptile::outdoor_darkness = new_darkness;
+
+ // we simply update the los for all players, which is unnecessarily
+ // costly, but should do for the moment.
+ for_all_players (pl)
+ pl->do_los = 1;
}
/*
* make_sure_seen: The object is supposed to be visible through walls, thus
* check if any players are nearby, and edit their LOS array.
*/
-
void
make_sure_seen (const object *op)
{
- player *pl;
-
- for (pl = first_player; pl; pl = pl->next)
+ for_all_players (pl)
if (pl->ob->map == op->map &&
- pl->ob->y - pl->socket.mapy / 2 <= op->y &&
- pl->ob->y + pl->socket.mapy / 2 >= op->y && pl->ob->x - pl->socket.mapx / 2 <= op->x && pl->ob->x + pl->socket.mapx / 2 >= op->x)
- pl->blocked_los[pl->socket.mapx / 2 + op->x - pl->ob->x][pl->socket.mapy / 2 + op->y - pl->ob->y] = 0;
+ pl->ob->y - pl->ns->mapy / 2 <= op->y &&
+ 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->los[op->x - pl->ob->x + LOS_X0][op->y - pl->ob->y + LOS_Y0] = 0;
}
/*
@@ -668,15 +639,13 @@
* walls has just been removed from the map, so update the los of any
* players within its range
*/
-
void
make_sure_not_seen (const object *op)
{
- player *pl;
-
- for (pl = first_player; pl; pl = pl->next)
+ for_all_players (pl)
if (pl->ob->map == op->map &&
- pl->ob->y - pl->socket.mapy / 2 <= op->y &&
- pl->ob->y + pl->socket.mapy / 2 >= op->y && pl->ob->x - pl->socket.mapx / 2 <= op->x && pl->ob->x + pl->socket.mapx / 2 >= op->x)
+ pl->ob->y - pl->ns->mapy / 2 <= op->y &&
+ 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;
}
+