/* * 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 */ /* Nov 95 - inserted USE_LIGHTING code stuff in here - b.t. */ #include #include static void expand_lighted_sight (object *op); enum { LOS_XI = 0x01, LOS_YI = 0x02, }; struct los_info { sint8 xo, yo; // obscure angle sint8 xe, ye; // angle deviation uint8 culled; // culled from "tree" uint8 queued; // already queued uint8 visible; uint8 flags; // LOS_XI/YI }; // temporary storage for the los algorithm, // one los_info for each lightable map space static los_info los[MAP_CLIENT_X][MAP_CLIENT_Y]; struct point { sint8 x, y; }; // minimum size, but must be a power of two #define QUEUE_LENGTH ((MAP_CLIENT_X + MAP_CLIENT_Y) * 2) // a queue of spaces to calculate static point queue [QUEUE_LENGTH]; static int q1, q2; // queue start, end /* * 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 enqueue (sint8 dx, sint8 dy, uint8 flags = 0) { sint8 x = LOS_X0 + dx; sint8 y = LOS_Y0 + dy; if (x < 0 || x >= MAP_CLIENT_X) return; if (y < 0 || y >= MAP_CLIENT_Y) return; los_info &l = los[x][y]; l.flags |= flags; if (l.queued) return; l.queued = 1; 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 do_los (object *op) { player *pl = op->contr; int max_radius = max (pl->ns->mapx, pl->ns->mapy) / 2; memset (los, 0, sizeof (los)); q1 = 0; q2 = 0; // initialise queue, not strictly required enqueue (0, 0); // enqueue center // 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) { 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; //int distance = idistance (dx, dy); if (distance > max_radius) continue;//D int distance = 0;//D los_info &l = los[x][y]; if (expect_true (l.flags & (LOS_XI | LOS_YI))) { l.culled = 1; // check contributing spaces, first horizontal if (expect_true (l.flags & LOS_XI)) { los_info *xi = &los[x - sign (dx)][y]; // don't cull unless obscured l.culled &= !xi->visible; /* merge input space */ if (expect_false (xi->xo || xi->yo)) { // 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) { 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) { l.ye = xi->yo + xi->ye; l.xe = xi->xe - xi->yo; l.xo = xi->xo; l.yo = xi->yo; } } } // check contributing spaces, last vertical, identical structure if (expect_true (l.flags & LOS_YI)) { los_info *yi = &los[x][y - sign (dy)]; // don't cull unless obscured l.culled &= !yi->visible; /* 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) { 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; } } } // check whether this space blocks the view maptile *m = op->map; sint16 nx = op->x + dx; sint16 ny = op->y + dy; if (expect_true (!xy_normalise (m, nx, ny)) || expect_false (m->at (nx, ny).flags () & P_BLOCKSVIEW)) { l.xo = l.xe = abs (dx); l.yo = l.ye = abs (dy); // 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; 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 ? max (0, 2 - max_radius + distance) : 3; } } // 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, LOS_XI); if (dx <= 0) enqueue (dx - 1, dy, LOS_XI); if (dy >= 0) enqueue (dx, dy + 1, LOS_YI); if (dy <= 0) enqueue (dx, dy - 1, LOS_YI); } } } /* 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 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_darken (sint8 b, sint8 l) { return max (b, l); } template static void apply_light (object *op, int dx, int dy, int light, const sint8 *darkness_table) { // min or max the circular area around basex, basey player *pl = op->contr; dx += LOS_X0; dy += LOS_Y0; int hx = op->contr->ns->mapx / 2; int hy = op->contr->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); for (int ax = ax0; ax <= ax1; ax++) for (int ay = ay0; ay <= ay1; ay++) pl->los[ax][ay] = change_it (pl->los[ax][ay], darkness_table [idistance (ax - dx, ay - dy)]); } /* add light, by finding all (non-null) nearby light sources, then * mark those squares specially. */ static void apply_lights (object *op) { int darklevel, mflags, light, x1, y1; maptile *m = op->map; sint16 nx, ny; darklevel = m->darkness; /* If the player can see in the dark, lower the darklevel for him */ if (QUERY_FLAG (op, FLAG_SEE_IN_DARK)) darklevel -= LOS_MAX / 2; /* 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; } 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 if (darklevel < 1) pass2 = 1; else { /* first, make everything totally dark */ for (int dx = -half_x; dx <= half_x; dx++) for (int dy = -half_x; dy <= half_y; dy++) if (op->contr->los[dx + LOS_X0][dy + LOS_Y0] != LOS_BLOCKED) op->contr->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 (int x = min_x; x <= max_x; x++) for (int y = min_y; y <= max_y; y++) { 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, x - op->x, y - op->y, light, darkness [light + MAX_LIGHT_RADIUS]); } /* 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); apply_light (op, 0, 0, light, darkness [light + MAX_LIGHT_RADIUS]); } } // 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; x <= max_x; x++) for (int y = min_y; y <= max_y; y++) { 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 < 0)) apply_light (op, x - op->x, y - op->y, -light, darkness [light + MAX_LIGHT_RADIUS]); } } /* 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) { op->contr->los[LOS_X0][LOS_Y0] = 3; } /* * update_los() recalculates the array which specifies what is * visible for the given player-object. */ void update_los (object *op) { if (QUERY_FLAG (op, FLAG_REMOVED)) return; op->contr->clear_los (); if (QUERY_FLAG (op, FLAG_WIZ) /* ||XRAYS(op) */ ) memset (op->contr->los, 0, sizeof (op->contr->los)); else if (QUERY_FLAG (op, FLAG_BLIND)) /* player is blind */ blinded_sight (op); else { do_los (op); apply_lights (op); } if (QUERY_FLAG (op, FLAG_XRAYS)) for (int dx = -2; dx <= 2; dx++) for (int dy = -2; dy <= 2; dy++) op->contr->los[dx + LOS_X0][dy + LOS_X0] = 0; } /* 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 * 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 * the distinction between maps much more obvious to the * players, which is should not be. * Currently, this function is called from the * change_map_light function */ void update_all_map_los (maptile *map) { for_all_players (pl) if (pl->ob && pl->ob->map == 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. * It is triggered by removal or inserting of objects which blocks * the sight in the map. * Modified by MSW 2001-07-12 to take a coordinate of the changed * position, and to also take map tiling into account. This change * means that just being on the same map is not sufficient - the * space that changes must be withing your viewable area. * * map is the map that changed, x and y are the coordinates. */ void update_all_los (const maptile *map, int x, int y) { for_all_players (pl) { /* Player should not have a null map, but do this * check as a safety */ 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; } /* 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; } } } /* * 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) { for_all_players (pl) if (pl->ob->map == op->map && 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_X0] = 0; } /* * make_sure_not_seen: The object which is supposed to be visible through * 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) { for_all_players (pl) if (pl->ob->map == op->map && 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; }