/src/cave.c
C | 3993 lines | 1654 code | 736 blank | 1603 comment | 438 complexity | 427748e19cbc3ca6ac9a5ad570f643b5 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * File: cave.c
- * Purpose: Lighting and update functions
- *
- * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
- *
- * This work is free software; you can redistribute it and/or modify it
- * under the terms of either:
- *
- * a) the GNU General Public License as published by the Free Software
- * Foundation, version 2, or
- *
- * b) the "Angband licence":
- * This software may be copied and distributed for educational, research,
- * and not for profit purposes provided that this copyright and statement
- * are included in all such copies. Other copyrights may also apply.
- */
- #include "angband.h"
- #include "object/tvalsval.h"
- #include "game-event.h"
- #include "game-cmd.h"
-
- /*
- * Support for Adam Bolt's tileset, lighting and transparency effects
- * by Robert Ruehlmann (rr9@thangorodrim.net)
- */
-
- /*
- * Approximate distance between two points.
- *
- * When either the X or Y component dwarfs the other component,
- * this function is almost perfect, and otherwise, it tends to
- * over-estimate about one grid per fifteen grids of distance.
- *
- * Algorithm: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2
- */
- int distance(int y1, int x1, int y2, int x2)
- {
- /* Find the absolute y/x distance components */
- int ay = abs(y2 - y1);
- int ax = abs(x2 - x1);
-
- /* Approximate the distance */
- return ay > ax ? ay + (ax>>1) : ax + (ay>>1);
- }
-
-
- /*
- * A simple, fast, integer-based line-of-sight algorithm. By Joseph Hall,
- * 4116 Brewster Drive, Raleigh NC 27606. Email to jnh@ecemwl.ncsu.edu.
- *
- * This function returns TRUE if a "line of sight" can be traced from the
- * center of the grid (x1,y1) to the center of the grid (x2,y2), with all
- * of the grids along this path (except for the endpoints) being non-wall
- * grids. Actually, the "chess knight move" situation is handled by some
- * special case code which allows the grid diagonally next to the player
- * to be obstructed, because this yields better gameplay semantics. This
- * algorithm is totally reflexive, except for "knight move" situations.
- *
- * Because this function uses (short) ints for all calculations, overflow
- * may occur if dx and dy exceed 90.
- *
- * Once all the degenerate cases are eliminated, we determine the "slope"
- * ("m"), and we use special "fixed point" mathematics in which we use a
- * special "fractional component" for one of the two location components
- * ("qy" or "qx"), which, along with the slope itself, are "scaled" by a
- * scale factor equal to "abs(dy*dx*2)" to keep the math simple. Then we
- * simply travel from start to finish along the longer axis, starting at
- * the border between the first and second tiles (where the y offset is
- * thus half the slope), using slope and the fractional component to see
- * when motion along the shorter axis is necessary. Since we assume that
- * vision is not blocked by "brushing" the corner of any grid, we must do
- * some special checks to avoid testing grids which are "brushed" but not
- * actually "entered".
- *
- * Angband three different "line of sight" type concepts, including this
- * function (which is used almost nowhere), the "project()" method (which
- * is used for determining the paths of projectables and spells and such),
- * and the "update_view()" concept (which is used to determine which grids
- * are "viewable" by the player, which is used for many things, such as
- * determining which grids are illuminated by the player's torch, and which
- * grids and monsters can be "seen" by the player, etc).
- */
- bool los(int y1, int x1, int y2, int x2)
- {
- /* Delta */
- int dx, dy;
-
- /* Absolute */
- int ax, ay;
-
- /* Signs */
- int sx, sy;
-
- /* Fractions */
- int qx, qy;
-
- /* Scanners */
- int tx, ty;
-
- /* Scale factors */
- int f1, f2;
-
- /* Slope, or 1/Slope, of LOS */
- int m;
-
-
- /* Extract the offset */
- dy = y2 - y1;
- dx = x2 - x1;
-
- /* Extract the absolute offset */
- ay = ABS(dy);
- ax = ABS(dx);
-
-
- /* Handle adjacent (or identical) grids */
- if ((ax < 2) && (ay < 2)) return (TRUE);
-
-
- /* Directly South/North */
- if (!dx)
- {
- /* South -- check for walls */
- if (dy > 0)
- {
- for (ty = y1 + 1; ty < y2; ty++)
- {
- if (!cave_floor_bold(ty, x1)) return (FALSE);
- }
- }
-
- /* North -- check for walls */
- else
- {
- for (ty = y1 - 1; ty > y2; ty--)
- {
- if (!cave_floor_bold(ty, x1)) return (FALSE);
- }
- }
-
- /* Assume los */
- return (TRUE);
- }
-
- /* Directly East/West */
- if (!dy)
- {
- /* East -- check for walls */
- if (dx > 0)
- {
- for (tx = x1 + 1; tx < x2; tx++)
- {
- if (!cave_floor_bold(y1, tx)) return (FALSE);
- }
- }
-
- /* West -- check for walls */
- else
- {
- for (tx = x1 - 1; tx > x2; tx--)
- {
- if (!cave_floor_bold(y1, tx)) return (FALSE);
- }
- }
-
- /* Assume los */
- return (TRUE);
- }
-
-
- /* Extract some signs */
- sx = (dx < 0) ? -1 : 1;
- sy = (dy < 0) ? -1 : 1;
-
-
- /* Vertical "knights" */
- if (ax == 1)
- {
- if (ay == 2)
- {
- if (cave_floor_bold(y1 + sy, x1)) return (TRUE);
- }
- }
-
- /* Horizontal "knights" */
- else if (ay == 1)
- {
- if (ax == 2)
- {
- if (cave_floor_bold(y1, x1 + sx)) return (TRUE);
- }
- }
-
-
- /* Calculate scale factor div 2 */
- f2 = (ax * ay);
-
- /* Calculate scale factor */
- f1 = f2 << 1;
-
-
- /* Travel horizontally */
- if (ax >= ay)
- {
- /* Let m = dy / dx * 2 * (dy * dx) = 2 * dy * dy */
- qy = ay * ay;
- m = qy << 1;
-
- tx = x1 + sx;
-
- /* Consider the special case where slope == 1. */
- if (qy == f2)
- {
- ty = y1 + sy;
- qy -= f1;
- }
- else
- {
- ty = y1;
- }
-
- /* Note (below) the case (qy == f2), where */
- /* the LOS exactly meets the corner of a tile. */
- while (x2 - tx)
- {
- if (!cave_floor_bold(ty, tx)) return (FALSE);
-
- qy += m;
-
- if (qy < f2)
- {
- tx += sx;
- }
- else if (qy > f2)
- {
- ty += sy;
- if (!cave_floor_bold(ty, tx)) return (FALSE);
- qy -= f1;
- tx += sx;
- }
- else
- {
- ty += sy;
- qy -= f1;
- tx += sx;
- }
- }
- }
-
- /* Travel vertically */
- else
- {
- /* Let m = dx / dy * 2 * (dx * dy) = 2 * dx * dx */
- qx = ax * ax;
- m = qx << 1;
-
- ty = y1 + sy;
-
- if (qx == f2)
- {
- tx = x1 + sx;
- qx -= f1;
- }
- else
- {
- tx = x1;
- }
-
- /* Note (below) the case (qx == f2), where */
- /* the LOS exactly meets the corner of a tile. */
- while (y2 - ty)
- {
- if (!cave_floor_bold(ty, tx)) return (FALSE);
-
- qx += m;
-
- if (qx < f2)
- {
- ty += sy;
- }
- else if (qx > f2)
- {
- tx += sx;
- if (!cave_floor_bold(ty, tx)) return (FALSE);
- qx -= f1;
- ty += sy;
- }
- else
- {
- tx += sx;
- qx -= f1;
- ty += sy;
- }
- }
- }
-
- /* Assume los */
- return (TRUE);
- }
-
-
-
-
- /*
- * Returns true if the player's grid is dark
- */
- bool no_light(void)
- {
- return (!player_can_see_bold(p_ptr->py, p_ptr->px));
- }
-
-
-
-
- /*
- * Determine if a given location may be "destroyed"
- *
- * Used by destruction spells, and for placing stairs, etc.
- */
- bool cave_valid_bold(int y, int x)
- {
- object_type *o_ptr;
-
- /* Forbid perma-grids */
- if (cave_perma_bold(y, x)) return (FALSE);
-
- /* Check objects */
- for (o_ptr = get_first_object(y, x); o_ptr; o_ptr = get_next_object(o_ptr))
- {
- /* Forbid artifact grids */
- if (artifact_p(o_ptr)) return (FALSE);
- }
-
- /* Accept */
- return (TRUE);
- }
-
-
- /*
- * Hack -- Hallucinatory monster
- */
- static u16b hallucinatory_monster(void)
- {
- monster_race *r_ptr;
-
- byte a;
- char c;
-
- while (1)
- {
- /* Select a random monster */
- r_ptr = &r_info[randint0(z_info->r_max)];
-
- /* Skip non-entries */
- if (!r_ptr->name) continue;
-
- /* Retrieve attr/char */
- a = r_ptr->x_attr;
- c = r_ptr->x_char;
-
- /* Encode */
- return (PICT(a,c));
- }
- }
-
-
- /*
- * Hack -- Hallucinatory object
- */
- static u16b hallucinatory_object(void)
- {
- object_kind *k_ptr;
-
- byte a;
- char c;
-
- while (1)
- {
- /* Select a random object */
- k_ptr = &k_info[randint0(z_info->k_max - 1) + 1];
-
- /* Skip non-entries */
- if (!k_ptr->name) continue;
-
- /* Retrieve attr/char (HACK - without flavors) */
- a = k_ptr->x_attr;
- c = k_ptr->x_char;
-
- /* HACK - Skip empty entries */
- if ((a == 0) || (c == 0)) continue;
-
- /* Encode */
- return (PICT(a,c));
- }
- }
-
-
-
- /*
- * The 16x16 tile of the terrain supports lighting
- */
- bool feat_supports_lighting(int feat)
- {
- /* Pseudo graphics don't support lighting */
- if (use_graphics == GRAPHICS_PSEUDO) return FALSE;
-
- if ((use_graphics != GRAPHICS_DAVID_GERVAIS) &&
- (feat >= FEAT_TRAP_HEAD) && (feat <= FEAT_TRAP_TAIL))
- {
- return TRUE;
- }
-
- switch (feat)
- {
- case FEAT_FLOOR:
- case FEAT_INVIS:
- case FEAT_SECRET:
- case FEAT_MAGMA:
- case FEAT_QUARTZ:
- case FEAT_MAGMA_H:
- case FEAT_QUARTZ_H:
- case FEAT_WALL_EXTRA:
- case FEAT_WALL_INNER:
- case FEAT_WALL_OUTER:
- case FEAT_WALL_SOLID:
- case FEAT_PERM_EXTRA:
- case FEAT_PERM_INNER:
- case FEAT_PERM_OUTER:
- case FEAT_PERM_SOLID:
- return TRUE;
- default:
- return FALSE;
- }
- }
-
-
- /*
- * Translate text colours.
- *
- * This translates a color based on the attribute. We use this to set terrain to
- * be lighter or darker, make metallic monsters shimmer, highlight text under the
- * mouse, and reduce the colours on mono colour or 16 colour terms to the correct
- * colour space.
- *
- * TODO: Honour the attribute for the term (full color, mono, 16 color) but ensure
- * that e.g. the lighter version of yellow becomes white in a 16 color term, but
- * light yellow in a full colour term.
- */
- byte get_color(byte a, int attr, int n)
- {
- /* Accept any graphical attr (high bit set) */
- if (a & (0x80)) return (a);
-
- /* TODO: Honour the attribute for the term (full color, mono, 16 color) */
- if (!attr) return(a);
-
- /* Translate the color N times */
- while (n > 0)
- {
- a = color_table[a].color_translate[attr];
- n--;
- }
-
- /* Return the modified color */
- return (a);
- }
-
-
- /*
- * This function modifies the attr/char pair for an empty floor space
- * to reflect the various lighting options available.
- *
- * For text, this means changing the colouring for OPT(view_yellow_light) or
- * OPT(view_bright_light), and for graphics it means modifying the char to
- * use a different tile in the tileset. These modifications are different
- * for different sets, depending on the tiles available, and their position
- * in the set.
- */
- static void special_lighting_floor(byte *a, char *c, enum grid_light_level lighting, bool in_view)
- {
- /* The floor starts off "lit" - i.e. rendered in white or the default
- * tile. */
-
- if (lighting == LIGHT_TORCH && OPT(view_yellow_light))
- {
- /*
- * OPT(view_yellow_light) distinguishes between torchlit and
- * permanently-lit areas
- */
- switch (use_graphics)
- {
- case GRAPHICS_NONE:
- case GRAPHICS_PSEUDO:
- /* Use "yellow" */
- if (*a == TERM_WHITE) *a = TERM_YELLOW;
- break;
- case GRAPHICS_ADAM_BOLT:
- *c += 2;
- break;
- case GRAPHICS_DAVID_GERVAIS:
- *c -= 1;
- break;
- }
- }
- else if (lighting == LIGHT_DARK)
- {
- /* Use a dark tile */
- switch (use_graphics)
- {
- case GRAPHICS_NONE:
- case GRAPHICS_PSEUDO:
- /* Use "dark gray" */
- if (*a == TERM_WHITE) *a = TERM_L_DARK;
- break;
- case GRAPHICS_ADAM_BOLT:
- case GRAPHICS_DAVID_GERVAIS:
- *c += 1;
- break;
- }
- }
- else
- {
- /*
- * OPT(view_bright_light) makes tiles that aren't in the "eyeline"
- * of the player show up dimmer than those that are.
- */
- if (OPT(view_bright_light) && !in_view)
- {
- switch (use_graphics)
- {
- case GRAPHICS_NONE:
- case GRAPHICS_PSEUDO:
- /* Use "gray" */
- if (*a == TERM_WHITE) *a = TERM_SLATE;
- else if (*a == TERM_L_GREEN) *a = TERM_GREEN;
- break;
- case GRAPHICS_ADAM_BOLT:
- case GRAPHICS_DAVID_GERVAIS:
- *c += 1;
- break;
- }
- }
- }
- }
-
- /*
- * This function modifies the attr/char pair for a wall (or other "interesting"
- * grids to show them as more-or-less lit. Note that how walls are drawn
- * isn't directly related to how they are lit - walls are always "lit".
- * The lighting effects we use are as a visual cue to emphasise blindness
- * and to show field-of-view (OPT(view_bright_light)).
- *
- * For text, we change the attr and for graphics we modify the char to
- * use a different tile in the tileset. These modifications are different
- * for different sets, depending on the tiles available, and their position
- * in the set.
- */
- static void special_wall_display(byte *a, char *c, bool in_view, int feat)
- {
- /* Grids currently in view are left alone, rendered as "white" */
- if (in_view) return;
-
- /* When blind, we make walls and other "white" things dark */
- if (p_ptr->timed[TMD_BLIND])
- {
- switch (use_graphics)
- {
- case GRAPHICS_NONE:
- case GRAPHICS_PSEUDO:
- /* Use "dark gray" */
- if (*a == TERM_WHITE) *a = TERM_L_DARK;
- break;
- case GRAPHICS_ADAM_BOLT:
- case GRAPHICS_DAVID_GERVAIS:
- if (feat_supports_lighting(feat)) *c += 1;
- break;
- }
- }
-
- /* Handle "OPT(view_bright_light)" by dimming walls not "in view" */
- else if (OPT(view_bright_light))
- {
- switch (use_graphics)
- {
- case GRAPHICS_NONE:
- case GRAPHICS_PSEUDO:
- /* Use "gray" */
- if (*a == TERM_WHITE) *a = TERM_SLATE;
- break;
- case GRAPHICS_ADAM_BOLT:
- case GRAPHICS_DAVID_GERVAIS:
- if (feat_supports_lighting(feat)) *c += 1;
- break;
- }
- }
- else
- {
- /* Use a brightly lit tile */
- switch (use_graphics)
- {
- case GRAPHICS_ADAM_BOLT:
- if (feat_supports_lighting(feat)) *c += 2;
- break;
- case GRAPHICS_DAVID_GERVAIS:
- if (feat_supports_lighting(feat)) *c -= 1;
- break;
- }
- }
- }
-
-
- /*
- * Checks if a square is at the (inner) edge of a trap detect area
- */
- bool dtrap_edge(int y, int x)
- {
- /* Check if the square is a dtrap in the first place */
- if (!cave_info2[y][x] & CAVE2_DTRAP) return FALSE;
-
- /* Check for non-dtrap adjacent grids */
- if (in_bounds_fully(y + 1, x ) && (!cave_info2[y + 1][x ] & CAVE2_DTRAP)) return TRUE;
- if (in_bounds_fully(y , x + 1) && (!cave_info2[y ][x + 1] & CAVE2_DTRAP)) return TRUE;
- if (in_bounds_fully(y - 1, x ) && (!cave_info2[y - 1][x ] & CAVE2_DTRAP)) return TRUE;
- if (in_bounds_fully(y , x - 1) && (!cave_info2[y ][x - 1] & CAVE2_DTRAP)) return TRUE;
-
- return FALSE;
- }
-
-
- /*
- * This function takes a pointer to a grid info struct describing the
- * contents of a grid location (as obtained through the function map_info)
- * and fills in the character and attr pairs for display.
- *
- * ap and cp are filled with the attr/char pair for the monster, object or
- * floor tile that is at the "top" of the grid (monsters covering objects,
- * which cover floor, assuming all are present).
- *
- * tap and tcp are filled with the attr/char pair for the floor, regardless
- * of what is on it. This can be used by graphical displays with
- * transparency to place an object onto a floor tile, is desired.
- *
- * Any lighting effects are also applied to these pairs, clear monsters allow
- * the underlying colour or feature to show through (ATTR_CLEAR and
- * CHAR_CLEAR), multi-hued colour-changing (ATTR_MULTI) is applied, and so on.
- * Technically, the flag "CHAR_MULTI" is supposed to indicate that a monster
- * looks strange when examined, but this flag is currently ignored.
- *
- * NOTES:
- * This is called pretty frequently, whenever a grid on the map display
- * needs updating, so don't overcomplicate it.
- *
- * The "zero" entry in the feature/object/monster arrays are
- * used to provide "special" attr/char codes, with "monster zero" being
- * used for the player attr/char, "object zero" being used for the "pile"
- * attr/char, and "feature zero" being used for the "darkness" attr/char.
- *
- * TODO:
- * The transformations for tile colors, or brightness for the 16x16
- * tiles should be handled differently. One possibility would be to
- * extend feature_type with attr/char definitions for the different states.
- * This will probably be done outside of the current text->graphics mappings
- * though.
- */
- void grid_data_as_text(grid_data *g, byte *ap, char *cp, byte *tap, char *tcp)
- {
- byte a;
- char c;
-
- feature_type *f_ptr = &f_info[g->f_idx];
-
- /* Normal attr and char */
- a = f_ptr->x_attr;
- c = f_ptr->x_char;
-
- /* Check for trap detection boundaries */
- if (g->trapborder && g->f_idx == FEAT_FLOOR &&
- (use_graphics == GRAPHICS_NONE ||
- use_graphics == GRAPHICS_PSEUDO))
- a = TERM_L_GREEN;
-
- /* Special lighting effects */
- if (g->f_idx <= FEAT_INVIS && OPT(view_special_light))
- special_lighting_floor(&a, &c, g->lighting, g->in_view);
-
- /* Special lighting effects (walls only) */
- if (g->f_idx > FEAT_INVIS && OPT(view_granite_light))
- special_wall_display(&a, &c, g->in_view, g->f_idx);
-
- /* Save the terrain info for the transparency effects */
- (*tap) = a;
- (*tcp) = c;
-
-
- /* If there's an object, deal with that. */
- if (g->first_k_idx)
- {
- if (g->hallucinate)
- {
- /* Just pick a random object to display. */
- int i = hallucinatory_object();
-
- a = PICT_A(i);
- c = PICT_C(i);
- }
- else
- {
- object_kind *k_ptr = &k_info[g->first_k_idx];
-
- /* Normal attr and char */
- a = object_kind_attr(g->first_k_idx);
- c = object_kind_char(g->first_k_idx);
-
- if (OPT(show_piles) && g->multiple_objects)
- {
- /* Get the "pile" feature instead */
- k_ptr = &k_info[0];
-
- a = k_ptr->x_attr;
- c = k_ptr->x_char;
- }
- }
- }
-
- /* If there's a monster */
- if (g->m_idx > 0)
- {
- if (g->hallucinate)
- {
- /* Just pick a random monster to display. */
- int i = hallucinatory_monster();
-
- a = PICT_A(i);
- c = PICT_C(i);
- }
- else
- {
- monster_type *m_ptr = &mon_list[g->m_idx];
- monster_race *r_ptr = &r_info[m_ptr->r_idx];
-
- byte da;
- char dc;
-
- /* Desired attr & char*/
- da = r_ptr->x_attr;
- dc = r_ptr->x_char;
-
- /* Special attr/char codes */
- if ((da & 0x80) && (dc & 0x80))
- {
- /* Use attr */
- a = da;
-
- /* Use char */
- c = dc;
- }
-
- /* Multi-hued monster */
- else if (rf_has(r_ptr->flags, RF_ATTR_MULTI) ||
- rf_has(r_ptr->flags, RF_ATTR_FLICKER))
- {
- /* Multi-hued attr */
- a = m_ptr->attr ? m_ptr->attr : 1;
-
- /* Normal char */
- c = dc;
- }
-
- /* Normal monster (not "clear" in any way) */
- else if (!flags_test(r_ptr->flags, RF_SIZE, RF_ATTR_CLEAR,
- RF_CHAR_CLEAR, FLAG_END))
- {
- /* Use attr */
- a = da;
-
- /* Desired attr & char */
- da = r_ptr->x_attr;
- dc = r_ptr->x_char;
-
- /* Use char */
- c = dc;
- }
-
- /* Hack -- Bizarre grid under monster */
- else if ((a & 0x80) || (c & 0x80))
- {
- /* Use attr */
- a = da;
-
- /* Use char */
- c = dc;
- }
-
- /* Normal char, Clear attr, monster */
- else if (!rf_has(r_ptr->flags, RF_CHAR_CLEAR))
- {
- /* Normal char */
- c = dc;
- }
-
- /* Normal attr, Clear char, monster */
- else if (!rf_has(r_ptr->flags, RF_ATTR_CLEAR))
- {
- /* Normal attr */
- a = da;
- }
-
- /* Store the drawing attr so we can use it other places too */
- m_ptr->attr = a;
- }
- }
-
- /* Handle "player" */
- else if (g->is_player)
- {
- monster_race *r_ptr = &r_info[0];
-
- /* Get the "player" attr */
- a = r_ptr->x_attr;
- if ((OPT(hp_changes_color)) && (arg_graphics == GRAPHICS_NONE))
- {
- switch(p_ptr->chp * 10 / p_get_mhp())
- {
- case 10:
- case 9:
- {
- a = TERM_WHITE;
- break;
- }
- case 8:
- case 7:
- {
- a = TERM_YELLOW;
- break;
- }
- case 6:
- case 5:
- {
- a = TERM_ORANGE;
- break;
- }
- case 4:
- case 3:
- {
- a = TERM_L_RED;
- break;
- }
- case 2:
- case 1:
- case 0:
- {
- a = TERM_RED;
- break;
- }
- default:
- {
- a = TERM_WHITE;
- break;
- }
- }
- }
-
- /* Get the "player" char */
- c = r_ptr->x_char;
- }
-
- /* Result */
- (*ap) = a;
- (*cp) = c;
- }
-
-
-
-
- /*
- * This function takes a grid location (x, y) and extracts information the
- * player is allowed to know about it, filling in the grid_data structure
- * passed in 'g'.
- *
- * The information filled in is as follows:
- * - g->f_idx is filled in with the terrain's feature type, or FEAT_NONE
- * if the player doesn't know anything about the grid. The function
- * makes use of the "mimic" field in terrain in order to allow one
- * feature to look like another (hiding secret doors, invisible traps,
- * etc). This will return the terrain type the player "Knows" about,
- * not necessarily the real terrain.
- * - g->m_idx is set to the monster index, or 0 if there is none (or the
- * player doesn't know it).
- * - g->first_k_idx is set to the index of the first object in a grid
- * that the player knows (and cares, as per OPT(hide_squelchable)) about,
- * or zero for no object in the grid.
- * - g->muliple_objects is TRUE if there is more than one object in the
- * grid that the player knows and cares about (to facilitate any special
- * floor stack symbol that might be used).
- * - g->in_view is TRUE if the player can currently see the grid - this can
- * be used to indicate field-of-view, such as through the OPT(view_bright_light)
- * option.
- * - g->lighting is set to indicate the lighting level for the grid:
- * LIGHT_DARK for unlit grids, LIGHT_TORCH for those lit by the player's
- * light source, and LIGHT_GLOW for inherently light grids (lit rooms, etc).
- * Note that lighting is always LIGHT_GLOW for known "interesting" grids
- * like walls.
- * - g->is_player is TRUE if the player is on the given grid.
- * - g->hallucinate is TRUE if the player is hallucinating something "strange"
- * for this grid - this should pick a random monster to show if the m_idx
- * is non-zero, and a random object if first_k_idx is non-zero.
- *
- * NOTES:
- * This is called pretty frequently, whenever a grid on the map display
- * needs updating, so don't overcomplicate it.
- *
- * Terrain is remembered separately from objects and monsters, so can be
- * shown even when the player can't "see" it. This leads to things like
- * doors out of the player's view still change from closed to open and so on.
- *
- * TODO:
- * Hallucination is currently disabled (it was a display-level hack before,
- * and we need it to be a knowledge-level hack). The idea is that objects
- * may turn into different objects, monsters into different monsters, and
- * terrain may be objects, monsters, or stay the same.
- */
- void map_info(unsigned y, unsigned x, grid_data *g)
- {
- object_type *o_ptr;
- byte info, info2;
-
- assert(x < DUNGEON_WID);
- assert(y < DUNGEON_HGT);
-
- info = cave_info[y][x];
- info2 = cave_info2[y][x];
-
- /* Default "clear" values, others will be set later where appropriate. */
- g->first_k_idx = 0;
- g->multiple_objects = FALSE;
- g->lighting = LIGHT_GLOW;
-
- /* Set things we can work out right now */
- g->f_idx = cave_feat[y][x];
- g->in_view = (info & CAVE_SEEN) ? TRUE : FALSE;
- g->is_player = (cave_m_idx[y][x] < 0) ? TRUE : FALSE;
- g->m_idx = (g->is_player) ? 0 : cave_m_idx[y][x];
- g->hallucinate = p_ptr->timed[TMD_IMAGE] ? TRUE : FALSE;
- g->trapborder = (dtrap_edge(y, x)) ? TRUE : FALSE;
-
- /* If the grid is memorised or can currently be seen */
- if ((info & CAVE_MARK) || (info & CAVE_SEEN))
- {
- /* Apply "mimic" field */
- g->f_idx = f_info[g->f_idx].mimic;
-
- /* Boring grids (floors, etc) */
- if (g->f_idx <= FEAT_INVIS)
- {
- /* Get the floor feature */
- g->f_idx = FEAT_FLOOR;
-
- /* Handle currently visible grids */
- if (info & CAVE_SEEN)
- {
- /* Only lit by "torch" light */
- if (info & CAVE_GLOW)
- g->lighting = LIGHT_GLOW;
- else
- g->lighting = LIGHT_TORCH;
- }
-
- /* Handle "dark" grids and "blindness" */
- else if (p_ptr->timed[TMD_BLIND] || !(info & CAVE_GLOW))
- g->lighting = LIGHT_DARK;
- }
- }
- /* Unknown */
- else
- {
- g->f_idx = FEAT_NONE;
- }
-
-
-
- /* Objects */
- for (o_ptr = get_first_object(y, x); o_ptr; o_ptr = get_next_object(o_ptr))
- {
- /* Memorized objects */
- if (o_ptr->marked && !squelch_hide_item(o_ptr))
- {
- /* First item found */
- if (g->first_k_idx == 0)
- {
- g->first_k_idx = o_ptr->k_idx;
- }
- else
- {
- g->multiple_objects = TRUE;
-
- /* And we know all we need to know. */
- break;
- }
- }
- }
-
- /* Monsters */
- if (g->m_idx > 0)
- {
- /* If the monster isn't "visible", make sure we don't list it.*/
- monster_type *m_ptr = &mon_list[g->m_idx];
- if (!m_ptr->ml) g->m_idx = 0;
-
- }
-
- /* Rare random hallucination on non-outer walls */
- if (g->hallucinate && g->m_idx == 0 && g->first_k_idx == 0)
- {
- if (one_in_(256) && (g->f_idx < FEAT_PERM_SOLID))
- {
- /* Normally, make an imaginary monster */
- if (randint0(100) < 75)
- {
- g->m_idx = 1;
- }
- /* Otherwise, an imaginary object */
- else
- {
- g->first_k_idx = 1;
- }
- }
- else
- {
- g->hallucinate = FALSE;
- }
- }
-
- assert(g->f_idx <= FEAT_PERM_SOLID);
- assert(g->m_idx < (u32b) mon_max);
- assert(g->first_k_idx < z_info->k_max);
- /* All other g fields are 'flags', mostly booleans. */
- }
-
-
-
- /*
- * Move the cursor to a given map location.
- */
- static void move_cursor_relative_map(int y, int x)
- {
- int ky, kx;
-
- term *old;
-
- int j;
-
- /* Scan windows */
- for (j = 0; j < ANGBAND_TERM_MAX; j++)
- {
- term *t = angband_term[j];
-
- /* No window */
- if (!t) continue;
-
- /* No relevant flags */
- if (!(op_ptr->window_flag[j] & (PW_MAP))) continue;
-
- /* Location relative to panel */
- ky = y - t->offset_y;
-
- /* Verify location */
- if ((ky < 0) || (ky >= t->hgt)) continue;
-
- /* Location relative to panel */
- kx = x - t->offset_x;
-
- if (use_bigtile) kx += kx;
-
- /* Verify location */
- if ((kx < 0) || (kx >= t->wid)) continue;
-
- /* Go there */
- old = Term;
- Term_activate(t);
- (void)Term_gotoxy(kx, ky);
- Term_activate(old);
- }
- }
-
-
- /*
- * Move the cursor to a given map location.
- *
- * The main screen will always be at least 24x80 in size.
- */
- void move_cursor_relative(int y, int x)
- {
- int ky, kx;
- int vy, vx;
-
- /* Move the cursor on map sub-windows */
- move_cursor_relative_map(y, x);
-
- /* Location relative to panel */
- ky = y - Term->offset_y;
-
- /* Verify location */
- if ((ky < 0) || (ky >= SCREEN_HGT)) return;
-
- /* Location relative to panel */
- kx = x - Term->offset_x;
-
- /* Verify location */
- if ((kx < 0) || (kx >= SCREEN_WID)) return;
-
- /* Location in window */
- vy = ky + ROW_MAP;
-
- /* Location in window */
- vx = kx + COL_MAP;
-
- if (use_bigtile) vx += kx;
-
- /* Go there */
- (void)Term_gotoxy(vx, vy);
- }
-
-
-
- /*
- * Display an attr/char pair at the given map location
- *
- * Note the inline use of "panel_contains()" for efficiency.
- *
- * Note the use of "Term_queue_char()" for efficiency.
- */
- static void print_rel_map(char c, byte a, int y, int x)
- {
- int ky, kx;
-
- int j;
-
- /* Scan windows */
- for (j = 0; j < ANGBAND_TERM_MAX; j++)
- {
- term *t = angband_term[j];
-
- /* No window */
- if (!t) continue;
-
- /* No relevant flags */
- if (!(op_ptr->window_flag[j] & (PW_MAP))) continue;
-
- /* Location relative to panel */
- ky = y - t->offset_y;
-
- /* Verify location */
- if ((ky < 0) || (ky >= t->hgt)) continue;
-
- /* Location relative to panel */
- kx = x - t->offset_x;
-
- if (use_bigtile)
- {
- kx += kx;
- if (kx + 1 >= t->wid) continue;
- }
-
- /* Verify location */
- if ((kx < 0) || (kx >= t->wid)) continue;
-
- /* Hack -- Queue it */
- Term_queue_char(t, kx, ky, a, c, 0, 0);
-
- if (use_bigtile)
- {
- /* Mega-Hack : Queue dummy char */
- if (a & 0x80)
- Term_queue_char(t, kx+1, ky, 255, -1, 0, 0);
- else
- Term_queue_char(t, kx+1, ky, TERM_WHITE, ' ', 0, 0);
- }
- }
- }
-
-
-
- /*
- * Display an attr/char pair at the given map location
- *
- * Note the inline use of "panel_contains()" for efficiency.
- *
- * Note the use of "Term_queue_char()" for efficiency.
- *
- * The main screen will always be at least 24x80 in size.
- */
- void print_rel(char c, byte a, int y, int x)
- {
- int ky, kx;
- int vy, vx;
-
- /* Print on map sub-windows */
- print_rel_map(c, a, y, x);
-
- /* Location relative to panel */
- ky = y - Term->offset_y;
-
- /* Verify location */
- if ((ky < 0) || (ky >= SCREEN_HGT)) return;
-
- /* Location relative to panel */
- kx = x - Term->offset_x;
-
- /* Verify location */
- if ((kx < 0) || (kx >= SCREEN_WID)) return;
-
- /* Location in window */
- vy = ky + ROW_MAP;
-
- /* Location in window */
- vx = kx + COL_MAP;
-
- if (use_bigtile) vx += kx;
-
- /* Hack -- Queue it */
- Term_queue_char(Term, vx, vy, a, c, 0, 0);
-
- if (use_bigtile)
- {
- /* Mega-Hack : Queue dummy char */
- if (a & 0x80)
- Term_queue_char(Term, vx+1, vy, 255, -1, 0, 0);
- else
- Term_queue_char(Term, vx+1, vy, TERM_WHITE, ' ', 0, 0);
- }
- }
-
-
-
-
- /*
- * Memorize interesting viewable object/features in the given grid
- *
- * This function should only be called on "legal" grids.
- *
- * This function will memorize the object and/or feature in the given grid,
- * if they are (1) see-able and (2) interesting. Note that all objects are
- * interesting, all terrain features except floors (and invisible traps) are
- * interesting, and floors (and invisible traps) are interesting sometimes
- * (depending on various options involving the illumination of floor grids).
- *
- * The automatic memorization of all objects and non-floor terrain features
- * as soon as they are displayed allows incredible amounts of optimization
- * in various places, especially "map_info()" and this function itself.
- *
- * Note that the memorization of objects is completely separate from the
- * memorization of terrain features, preventing annoying floor memorization
- * when a detected object is picked up from a dark floor, and object
- * memorization when an object is dropped into a floor grid which is
- * memorized but out-of-sight.
- *
- * This function should be called every time the "memorization" of a grid
- * (or the object in a grid) is called into question, such as when an object
- * is created in a grid, when a terrain feature "changes" from "floor" to
- * "non-floor", and when any grid becomes "see-able" for any reason.
- *
- * This function is called primarily from the "update_view()" function, for
- * each grid which becomes newly "see-able".
- */
- void note_spot(int y, int x)
- {
- byte info;
-
- object_type *o_ptr;
-
- /* Get cave info */
- info = cave_info[y][x];
-
- /* Require "seen" flag */
- if (!(info & (CAVE_SEEN))) return;
-
-
- /* Hack -- memorize objects */
- for (o_ptr = get_first_object(y, x); o_ptr; o_ptr = get_next_object(o_ptr))
- {
- /* Memorize objects */
- o_ptr->marked = TRUE;
- }
-
-
- /* Hack -- memorize grids */
- if (!(info & (CAVE_MARK)))
- {
- /* Memorize some "boring" grids */
- if (cave_feat[y][x] <= FEAT_INVIS)
- {
- /* Option -- memorize certain floors */
- if (((info & (CAVE_GLOW)) && OPT(view_perma_grids)) ||
- OPT(view_torch_grids))
- {
- /* Memorize */
- cave_info[y][x] |= (CAVE_MARK);
- }
- }
-
- /* Memorize all "interesting" grids */
- else
- {
- /* Memorize */
- cave_info[y][x] |= (CAVE_MARK);
- }
- }
- }
-
-
-
- /*
- * Redraw (on the screen) a given map location
- *
- * This function should only be called on "legal" grids.
- */
- void light_spot(int y, int x)
- {
- event_signal_point(EVENT_MAP, x, y);
- }
-
-
- static void prt_map_aux(void)
- {
- byte a;
- char c;
- byte ta;
- char tc;
- grid_data g;
-
- int y, x;
- int vy, vx;
- int ty, tx;
-
- int j;
-
- /* Scan windows */
- for (j = 0; j < ANGBAND_TERM_MAX; j++)
- {
- term *t = angband_term[j];
-
- /* No window */
- if (!t) continue;
-
- /* No relevant flags */
- if (!(op_ptr->window_flag[j] & (PW_MAP))) continue;
-
- /* Assume screen */
- ty = t->offset_y + t->hgt;
- tx = t->offset_x + t->wid;
-
- if (use_bigtile) tx = t->offset_x + (t->wid / 2);
-
- /* Dump the map */
- for (y = t->offset_y, vy = 0; y < ty; vy++, y++)
- {
- for (x = t->offset_x, vx = 0; x < tx; vx++, x++)
- {
- /* Check bounds */
- if (!in_bounds(y, x)) continue;
-
- if (use_bigtile && (vx + 1 >= t->wid)) continue;
-
- /* Determine what is there */
- map_info(y, x, &g);
- grid_data_as_text(&g, &a, &c, &ta, &tc);
- Term_queue_char(t, vx, vy, a, c, ta, tc);
-
- if (use_bigtile)
- {
- vx++;
-
- /* Mega-Hack : Queue dummy char */
- if (a & 0x80)
- Term_queue_char(t, vx, vy, 255, -1, 0, 0);
- else
- Term_queue_char(t, vx, vy, TERM_WHITE, ' ', TERM_WHITE, ' ');
- }
- }
- }
- }
- }
-
-
-
- /*
- * Redraw (on the screen) the current map panel
- *
- * Note the inline use of "light_spot()" for efficiency.
- *
- * The main screen will always be at least 24x80 in size.
- */
- void prt_map(void)
- {
- byte a;
- char c;
- byte ta;
- char tc;
- grid_data g;
-
- int y, x;
- int vy, vx;
- int ty, tx;
-
- /* Redraw map sub-windows */
- prt_map_aux();
-
- /* Assume screen */
- ty = Term->offset_y + SCREEN_HGT;
- tx = Term->offset_x + SCREEN_WID;
-
- /* Dump the map */
- for (y = Term->offset_y, vy = ROW_MAP; y < ty; vy++, y++)
- {
- for (x = Term->offset_x, vx = COL_MAP; x < tx; vx++, x++)
- {
- /* Check bounds */
- if (!in_bounds(y, x)) continue;
-
- /* Determine what is there */
- map_info(y, x, &g);
- grid_data_as_text(&g, &a, &c, &ta, &tc);
-
- /* Hack -- Queue it */
- Term_queue_char(Term, vx, vy, a, c, ta, tc);
-
- if (use_bigtile)
- {
- vx++;
-
- /* Mega-Hack : Queue dummy char */
- if (a & 0x80)
- Term_queue_char(Term, vx, vy, 255, -1, 0, 0);
- else
- Term_queue_char(Term, vx, vy, TERM_WHITE, ' ', TERM_WHITE, ' ');
- }
- }
- }
- }
-
-
-
-
-
- /*
- * Hack -- priority array (see below)
- *
- * Note that all "walls" always look like "secret doors" (see "map_info()").
- */
- static const int priority_table[14][2] =
- {
- /* Dark */
- { FEAT_NONE, 2 },
-
- /* Floors */
- { FEAT_FLOOR, 5 },
-
- /* Walls */
- { FEAT_SECRET, 10 },
-
- /* Quartz */
- { FEAT_QUARTZ, 11 },
-
- /* Magma */
- { FEAT_MAGMA, 12 },
-
- /* Rubble */
- { FEAT_RUBBLE, 13 },
-
- /* Open doors */
- { FEAT_OPEN, 15 },
- { FEAT_BROKEN, 15 },
-
- /* Closed doors */
- { FEAT_DOOR_HEAD + 0x00, 17 },
-
- /* Hidden gold */
- { FEAT_QUARTZ_K, 19 },
- { FEAT_MAGMA_K, 19 },
-
- /* Stairs */
- { FEAT_LESS, 25 },
- { FEAT_MORE, 25 },
-
- /* End */
- { 0, 0 }
- };
-
-
- /*
- * Hack -- a priority function (see below)
- */
- static byte priority(byte a, char c)
- {
- int i, p0, p1;
-
- feature_type *f_ptr;
-
- /* Scan the table */
- for (i = 0; TRUE; i++)
- {
- /* Priority level */
- p1 = priority_table[i][1];
-
- /* End of table */
- if (!p1) break;
-
- /* Feature index */
- p0 = priority_table[i][0];
-
- /* Get the feature */
- f_ptr = &f_info[p0];
-
- /* Check character and attribute, accept matches */
- if ((f_ptr->x_char == c) && (f_ptr->x_attr == a)) return (p1);
- }
-
- /* Default */
- return (20);
- }
-
-
- /*
- * Display a "small-scale" map of the dungeon in the active Term.
- *
- * Note that this function must "disable" the special lighting effects so
- * that the "priority" function will work.
- *
- * Note the use of a specialized "priority" function to allow this function
- * to work with any graphic attr/char mappings, and the attempts to optimize
- * this function where possible.
- *
- * If "cy" and "cx" are not NULL, then returns the screen location at which
- * the player was displayed, so the cursor can be moved to that location,
- * and restricts the horizontal map size to SCREEN_WID. Otherwise, nothing
- * is returned (obviously), and no restrictions are enforced.
- */
- void display_map(int *cy, int *cx)
- {
- int py = p_ptr->py;
- int px = p_ptr->px;
-
- int map_hgt, map_wid;
- int dungeon_hgt, dungeon_wid;
- int row, col;
-
- int x, y;
- grid_data g;
-
- byte ta;
- char tc;
-
- byte tp;
-
- /* Large array on the stack */
- byte mp[DUNGEON_HGT][DUNGEON_WID];
-
- bool old_view_special_light;
- bool old_view_granite_light;
-
- monster_race *r_ptr = &r_info[0];
-
- /* Desired map height */
- map_hgt = Term->hgt - 2;
- map_wid = Term->wid - 2;
-
- dungeon_hgt = (p_ptr->depth == 0) ? TOWN_HGT : DUNGEON_HGT;
- dungeon_wid = (p_ptr->depth == 0) ? TOWN_WID : DUNGEON_WID;
-
- /* Prevent accidents */
- if (map_hgt > dungeon_hgt) map_hgt = dungeon_hgt;
- if (map_wid > dungeon_wid) map_wid = dungeon_wid;
-
- /* Prevent accidents */
- if ((map_wid < 1) || (map_hgt < 1)) return;
-
-
- /* Save lighting effects */
- old_view_special_light = OPT(view_special_light);
- old_view_granite_light = OPT(view_granite_light);
-
- /* Disable lighting effects */
- OPT(view_special_light) = FALSE;
- OPT(view_granite_light) = FALSE;
-
-
- /* Nothing here */
- ta = TERM_WHITE;
- tc = ' ';
-
- /* Clear the priorities */
- for (y = 0; y < map_hgt; ++y)
- {
- for (x = 0; x < map_wid; ++x)
- {
- /* No priority */
- mp[y][x] = 0;
- }
- }
-
-
- /* Draw a box around the edge of the term */
- window_make(0, 0, map_wid + 1, map_hgt + 1);
-
- /* Analyze the actual map */
- for (y = 0; y < dungeon_hgt; y++)
- {
- for (x = 0; x < dungeon_wid; x++)
- {
- row = (y * map_hgt / dungeon_hgt);
- col = (x * map_wid / dungeon_wid);
-
- if (use_bigtile)
- col = col & ~1;
-
- /* Get the attr/char at that map location */
- map_info(y, x, &g);
- grid_data_as_text(&g, &ta, &tc, &ta, &tc);
-
- /* Get the priority of that attr/char */
- tp = priority(ta, tc);
-
- /* Save "best" */
- if (mp[row][col] < tp)
- {
- /* Add the character */
- Term_putch(col + 1, row + 1, ta, tc);
-
- if (use_bigtile)
- {
- if (ta & 0x80)
- Term_putch(col + 2, row + 1, 255, -1);
- else
- Term_putch(col + 2, row + 1, TERM_WHITE, ' ');
- }
-
- /* Save priority */
- mp[row][col] = tp;
- }
- }
- }
-
-
- /* Player location */
- row = (py * map_hgt / dungeon_hgt);
- col = (px * map_wid / dungeon_wid);
-
- if (use_bigtile)
- col = col & ~1;
-
- /*** Make sure the player is visible ***/
-
- /* Get the "player" attr */
- ta = r_ptr->x_attr;
-
- /* Get the "player" char */
- tc = r_ptr->x_char;
-
- /* Draw the player */
- Term_putch(col + 1, row + 1, ta, tc);
-
- /* Return player location */
- if (cy != NULL) (*cy) = row + 1;
- if (cx != NULL) (*cx) = col + 1;
-
-
- /* Restore lighting effects */
- OPT(view_special_light) = old_view_special_light;
- OPT(view_granite_light) = old_view_granite_light;
- }
-
-
- /*
- * Display a "small-scale" map of the dungeon.
- *
- * Note that the "player" is always displayed on the map.
- */
- void do_cmd_view_map(void)
- {
- int cy, cx;
- cptr prompt = "Hit any key to continue";
-
- /* Save screen */
- screen_save();
-
- /* Note */
- prt("Please wait...", 0, 0);
-
- /* Flush */
- Term_fresh();
-
- /* Clear the screen */
- Term_clear();
-
- /* Display the map */
- display_map(&cy, &cx);
-
- /* Show the prompt */
- put_str(prompt, Term->hgt - 1, Term->wid / 2 - strlen(prompt) / 2);
-
- /* Highlight the player */
- Term_gotoxy(cx, cy);
-
- /* Get any key */
- (void)anykey();
-
- /* Load screen */
- screen_load();
- }
-
-
-
- /*
- * Some comments on the dungeon related data structures and functions...
- *
- * Angband is primarily a dungeon exploration game, and it should come as
- * no surprise that the internal representation of the dungeon has evolved
- * over time in much the same way as the game itself, to provide semantic
- * changes to the game itself, to make the code simpler to understand, and
- * to make the executable itself faster or more efficient in various ways.
- *
- * There are a variety of dungeon related data structures, and associated
- * functions, which store information about the dungeon, and provide methods
- * by which this information can be accessed or modified.
- *
- * Some of this information applies to the dungeon as a whole, such as the
- * list of unique monsters which are still alive. Some of this information
- * only applies to the current dungeon level, such as the current depth, or
- * the list of monsters currently inhabiting the level. And some of the
- * information only applies to a single grid of the current dungeon level,
- * such as whether the grid is illuminated, or whether the grid contains a
- * monster, or whether the grid can be seen by the player. If Angband was
- * to be turned into a multi-player game, some of the information currently
- * associated with the dungeon should really be associated with the player,
- * such as whether a given grid is viewable by a given player.
- *
- * One of the major bottlenecks in ancient versions of Angband was in the
- * calculation of "line of sight" from the player to various grids, such
- * as those containing monsters, using the relatively expensive "los()"
- * function. This was such a nasty bottleneck that a lot of silly things
- * were done to reduce the dependancy on "line of sight", for example, you
- * could not "see" any grids in a lit room until you actually entered the
- * room, at which point every grid in the room became "illuminated" and
- * all of the grids in the room were "memorized" forever. Other major
- * bottlenecks involved the determination of whether a grid was lit by the
- * player's torch, and whether a grid blocked the player's line of sight.
- * These bottlenecks led to the development of special new functions to
- * optimize issues involved with "line of sight" and "torch lit grids".
- * These optimizations led to entirely new additions to the game, such as
- * the ability to display the player's entire field of view using different
- * colors than were used for the "memorized" portions of the dungeon, and
- * the ability to memorize dark floor grids, but to indicate by the way in
- * which they are displayed that they are not actually illuminated. And
- * of course many of them simply made the game itself faster or more fun.
- * Also, over time, the definition of "line of sight" has been relaxed to
- * allow the player to see a wider "field of view", which is slightly more
- * realistic, and only slightly more expensive to maintain.
- *
- * Currently, a lot of the information about the dungeon is stored in ways
- * that make it very efficient to access or modify the information, while
- * still attempting to be relatively conservative about memory usage, even
- * if this means that some information is stored in multiple places, or in
- * ways which require the use of special code idioms. For example, each
- * monster record in the monster array contains the location of the monster,
- * and each cave grid has an index into the monster array, or a zero if no
- * monster is in the grid. This allows the monster code to efficiently see
- * where the monster is located, while allowing the dungeon code to quickly
- * determine not only if a monster is present in a given grid, but also to
- * find out which monster. The extra space used to store the information
- * twice is inconsequential compared to the speed increase.
- *
- * Some of the information about the dungeon is used by functions which can
- * constitute the "critical efficiency path" of the game itself, and so the
- * way in which they are stored and accessed has been optimized in order to
- * optimize the game itself. For example, the "update_view()" function was
- * originally created to speed up the game itself (when the player was not
- * running), but then it took on extra responsibility as the provider of the
- * new "special effects lighting code", and became one of the most important
- * bottlenecks when the player was running. So many rounds of optimization
- * were performed on both the function itself, and the data structures which
- * it uses, resulting eventually in a function which not only made the game
- * faster than before, but which was responsible for even more calculations
- * (including the determination of which grids are "viewable" by the player,
- * which grids are illuminated by the player's torch, and which grids can be
- * "seen" in some way by the player), as well as for providing the guts of
- * the special effects lighting code, and for the efficient redisplay of any
- * grids whose visual representation may have changed.
- *
- * Several pieces of information about each cave grid are stored in various
- * two dimensional arrays, with one unit of information for each grid in the
- * dungeon. Some of these arrays have been intentionally expanded by a small
- * factor to make the two dimensional array accesses faster by allowing the
- * use of shifting instead of multiplication.
- *
- * Several pieces of information about each cave grid are stored in the
- * "cave_info" array, which is a special two dimensional array of bytes,
- * one for each cave grid, each containing eight separate "flags" which
- * describe some property of the cave grid. These flags can be checked and
- * modified extremely quickly, especially when special idioms are used to
- * force the compiler to keep a local register pointing to the base of the
- * array. Special location offset macros can be used to minimize the number
- * of computations which must be performed at runtime. Note that using a
- * byte for each flag set may be slightly more efficient than using a larger
- * unit, so if another flag (or two) is needed later, and it must be fast,
- * then the two existing flags which do not have to be fast should be moved
- * out into some other data structure and the new flags should take their
- * place. This may require a few minor changes in the savefile code.
- *
- * The "CAVE_ROOM" flag is saved in the savefile and is used to determine
- * which grids are part of "rooms", and thus which grids are affected by
- * "illumination" spells. This flag does not have to be very fast.
- *
- * The "CAVE_ICKY" flag is saved in the savefile and is used to determine
- * which grids are part of "vaults", and thus which grids cannot serve as
- * the destinations of player teleportation. This flag does not have to
- * be very fast.
- *
- * The "CAVE_MARK" flag is saved in the savefile and is used to determine
- * which grids have been "memorized" by the player. This flag is used by
- * the "map_info()" function to determine if a grid should be displayed.
- * This flag is used in a few other places to determine if the player can
- * "know" about a given grid. This flag must be very fast.
- *
- * The "CAVE_GLOW" flag is saved in the savefile and is used to determine
- * which grids are "permanently illuminated". This flag is used by the
- * "update_view()" function to help determine which viewable flags may
- * be "seen" by the player. This flag is used by the "map_info" function
- * to determine if a grid is only lit by the player's torch. This flag
- * has special semantics for wall grids (see "update_view()"). This flag
- * must be very fast.
- *
- * The "CAVE_WALL" flag is used to determine which grids block the player's
- * line of sight. This flag is used by the "update_view()" function to
- * determine which grids block line of sight, and to help determine which
- * grids can be "seen" by the player. This flag must be very fast.
- *
- * The "CAVE_VIEW" flag is used to determine which grids are currently in
- * line of sight of the player. This flag is set by (and used by) the
- * "update_view()" function. This flag is used by any code which needs to
- * know if the player can "view" a given grid. This flag is used by the
- * "map_info()" function for some optional special lighting effects. The
- * "player_has_los_bold()" macro wraps an abstraction around this flag, but
- * certain code idioms are much more efficient. This flag is used to check
- * if a modification to a terrain feature might affect the player's field of
- * view. This flag is used to see if certain monsters are "visible" to the
- * player. This flag is used to allow any monster in the player's field of
- * view to "sense" the presence of the player. This flag must be very fast.
- *
- * The "CAVE_SEEN" flag is used to determine which grids are currently in
- * line of sight of the player and also illuminated in some way. This flag
- * is set by the "update_view()" function, using computations based on the
- * "CAVE_VIEW" and "CAVE_WALL" and "CAVE_GLOW" flags of various grids. This
- * flag is used by any code which needs to know if the player can "see" a
- * given grid. This flag is used by the "map_info()" function both to see
- * if a given "boring" grid can be seen by the player, and for some optional
- * special lighting effects. The "player_can_see_bold()" macro wraps an
- * abstraction around this flag, but certain code idioms are much more
- * efficient. This flag is used to see if certain monsters are "visible" to
- * the player. This flag is never set for a grid unless "CAVE_VIEW" is also
- * set for the grid. Whenever the "CAVE_WALL" or "CAVE_GLOW" flag changes
- * for a grid which has the "CAVE_VIEW" flag set, the "CAVE_SEEN" flag must
- * be recalculated. The simplest way to do this is to call "forget_view()"
- * and "update_view()" whenever the "CAVE_WALL" or "CAVE_GLOW" flags change
- * for a grid which has "CAVE_VIEW" set. This flag must be very fast.
- *
- * The "CAVE_TEMP" flag is used for a variety of temporary purposes. This
- * flag is used to determine if the "CAVE_SEEN" flag for a grid has changed
- * during the "update_view()" function. This flag is us…
Large files files are truncated, but you can click here to view the full file