# Source:NetHack 3.6.0/src/vision.c

Below is the full text to vision.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/vision.c#line123]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

## Top of file

1.  /* NetHack 3.6	vision.c	\$NHDT-Date: 1448013598 2015/11/20 09:59:58 \$  \$NHDT-Branch: master \$:\$NHDT-Revision: 1.27 \$ */
2.  /* Copyright (c) Dean Luick, with acknowledgements to Dave Cohrs, 1990. */
3.  /* NetHack may be freely redistributed.  See license for details.       */
4.
5.  #include "hack.h"
6.

## Circles

1.  /* Circles
2.   * ==================================================================*/
3.
4.  /*
5.   * These numbers are limit offsets for one quadrant of a circle of a given
6.   * radius (the first number of each line) from the source.  The number in
7.   * the comment is the element number (so pointers can be set up).  Each
8.   * "circle" has as many elements as its radius+1.  The radius is the number
9.   * of points away from the source that the limit exists.  The radius of the
10.   * offset on the same row as the source *is* included so we don't have to
11.   * make an extra check.  For example, a circle of radius 4 has offsets:
12.   *
13.   *              XXX     +2
14.   *              ...X    +3
15.   *              ....X   +4
16.   *              ....X   +4
17.   *              @...X   +4
18.   *
19.   */
20.  char circle_data[] = {
21.      /*  0*/ 1,  1,
22.      /*  2*/ 2,  2,  1,
23.      /*  5*/ 3,  3,  2,  1,
24.      /*  9*/ 4,  4,  4,  3,  2,
25.      /* 14*/ 5,  5,  5,  4,  3,  2,
26.      /* 20*/ 6,  6,  6,  5,  5,  4,  2,
27.      /* 27*/ 7,  7,  7,  6,  6,  5,  4,  2,
28.      /* 35*/ 8,  8,  8,  7,  7,  6,  6,  4,  2,
29.      /* 44*/ 9,  9,  9,  9,  8,  8,  7,  6,  5,  3,
30.      /* 54*/ 10, 10, 10, 10, 9,  9,  8,  7,  6,  5,  3,
31.      /* 65*/ 11, 11, 11, 11, 10, 10, 9,  9,  8,  7,  5,  3,
32.      /* 77*/ 12, 12, 12, 12, 11, 11, 10, 10, 9,  8,  7,  5,  3,
33.      /* 90*/ 13, 13, 13, 13, 12, 12, 12, 11, 10, 10, 9,  7,  6, 3,
34.      /*104*/ 14, 14, 14, 14, 13, 13, 13, 12, 12, 11, 10, 9,  8, 6, 3,
35.      /*119*/ 15, 15, 15, 15, 14, 14, 14, 13, 13, 12, 11, 10, 9, 8, 6, 3,
36.      /*135*/ 16 /* MAX_RADIUS+1; used to terminate range loops -dlc */
37.  };
38.
39.  /*
40.   * These are the starting indexes into the circle_data[] array for a
41.   * circle of a given radius.
42.   */
43.  char circle_start[] = {
44.      /*  */ 0, /* circles of radius zero are not used */
45.      /* 1*/ 0,
46.      /* 2*/ 2,
47.      /* 3*/ 5,
48.      /* 4*/ 9,
49.      /* 5*/ 14,
50.      /* 6*/ 20,
51.      /* 7*/ 27,
52.      /* 8*/ 35,
53.      /* 9*/ 44,
54.      /*10*/ 54,
55.      /*11*/ 65,
56.      /*12*/ 77,
57.      /*13*/ 90,
58.      /*14*/ 104,
59.      /*15*/ 119,
60.  };
61.

## Vision (arbitrary line of sight)

1.  /*==========================================================================*/
2.  /* Vision (arbitrary line of sight)
3.   * =========================================*/
4.
5.  /*------ global variables ------*/
6.
7.  #if 0 /* (moved to decl.c) */
8.  /* True if we need to run a full vision recalculation. */
9.  boolean vision_full_recalc = 0;
10.
11.  /* Pointers to the current vision array. */
12.  char    **viz_array;
13.  #endif
14.  char *viz_rmin, *viz_rmax; /* current vision cs bounds */
15.
16.  /*------ local variables ------*/
17.
18.  static char could_see[2][ROWNO][COLNO]; /* vision work space */
19.  static char *cs_rows0[ROWNO], *cs_rows1[ROWNO];
20.  static char cs_rmin0[ROWNO], cs_rmax0[ROWNO];
21.  static char cs_rmin1[ROWNO], cs_rmax1[ROWNO];
22.
23.  static char viz_clear[ROWNO][COLNO]; /* vision clear/blocked map */
24.  static char *viz_clear_rows[ROWNO];
25.
26.  static char left_ptrs[ROWNO][COLNO]; /* LOS algorithm helpers */
27.  static char right_ptrs[ROWNO][COLNO];
28.
29.  /* Forward declarations. */
30.  STATIC_DCL void FDECL(fill_point, (int, int));
31.  STATIC_DCL void FDECL(dig_point, (int, int));
32.  STATIC_DCL void NDECL(view_init);
33.  STATIC_DCL void FDECL(view_from, (int, int, char **, char *, char *, int,
34.                                    void (*)(int, int, genericptr_t),
35.                                    genericptr_t));
36.  STATIC_DCL void FDECL(get_unused_cs, (char ***, char **, char **));
37.  STATIC_DCL void FDECL(rogue_vision, (char **, char *, char *));
38.
39.  /* Macro definitions that I can't find anywhere. */
40.  #define sign(z) ((z) < 0 ? -1 : ((z) ? 1 : 0))
41.  #define v_abs(z) ((z) < 0 ? -(z) : (z)) /* don't use abs -- it may exist */
42.

### vision_init

1.  /*
2.   * vision_init()
3.   *
4.   * The one-time vision initialization routine.
5.   *
6.   * This must be called before mklev() is called in newgame() [allmain.c],
7.   * or before a game restore.   Else we die a horrible death.
8.   */
9.  void
10.  vision_init()
11.  {
12.      int i;
13.
14.      /* Set up the pointers. */
15.      for (i = 0; i < ROWNO; i++) {
16.          cs_rows0[i] = could_see[0][i];
17.          cs_rows1[i] = could_see[1][i];
18.          viz_clear_rows[i] = viz_clear[i];
19.      }
20.
21.      /* Start out with cs0 as our current array */
22.      viz_array = cs_rows0;
23.      viz_rmin = cs_rmin0;
24.      viz_rmax = cs_rmax0;
25.
26.      vision_full_recalc = 0;
27.      (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
28.
29.      /* Initialize the vision algorithm (currently C or D). */
30.      view_init();
31.
32.  #ifdef VISION_TABLES
33.      /* Note:  this initializer doesn't do anything except guarantee that
35.       */
36.      vis_tab_init();
37.  #endif
38.  }
39.

### does_block

1.  /*
2.   * does_block()
3.   *
4.   * Returns true if the level feature, object, or monster at (x,y) blocks
5.   * sight.
6.   */
7.  int
8.  does_block(x, y, lev)
9.  int x, y;
10.  register struct rm *lev;
11.  {
12.      struct obj *obj;
13.      struct monst *mon;
14.
15.      /* Features that block . . */
16.      if (IS_ROCK(lev->typ) || lev->typ == TREE
17.          || (IS_DOOR(lev->typ)
18.              && (lev->doormask & (D_CLOSED | D_LOCKED | D_TRAPPED))))
19.          return 1;
20.
21.      if (lev->typ == CLOUD || lev->typ == WATER
22.          || (lev->typ == MOAT && Underwater))
23.          return 1;
24.
25.      /* Boulders block light. */
26.      for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
27.          if (obj->otyp == BOULDER)
28.              return 1;
29.
30.      /* Mimics mimicing a door or boulder block light. */
31.      if ((mon = m_at(x, y)) && (!mon->minvis || See_invisible)
32.          && (is_door_mappear(mon) || is_obj_mappear(mon,BOULDER)))
33.          return 1;
34.
35.      return 0;
36.  }
37.

### vision_reset

1.  /*
2.   * vision_reset()
3.   *
4.   * This must be called *after* the levl[][] structure is set with the new
5.   * level and the level monsters and objects are in place.
6.   */
7.  void
8.  vision_reset()
9.  {
10.      int y;
11.      register int x, i, dig_left, block;
12.      register struct rm *lev;
13.
14.      /* Start out with cs0 as our current array */
15.      viz_array = cs_rows0;
16.      viz_rmin = cs_rmin0;
17.      viz_rmax = cs_rmax0;
18.
19.      (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
20.
21.      /* Reset the pointers and clear so that we have a "full" dungeon. */
22.      (void) memset((genericptr_t) viz_clear, 0, sizeof(viz_clear));
23.
24.      /* Dig the level */
25.      for (y = 0; y < ROWNO; y++) {
26.          dig_left = 0;
27.          block = TRUE; /* location (0,y) is always stone; it's !isok() */
28.          lev = &levl[1][y];
29.          for (x = 1; x < COLNO; x++, lev += ROWNO)
30.              if (block != (IS_ROCK(lev->typ) || does_block(x, y, lev))) {
31.                  if (block) {
32.                      for (i = dig_left; i < x; i++) {
33.                          left_ptrs[y][i] = dig_left;
34.                          right_ptrs[y][i] = x - 1;
35.                      }
36.                  } else {
37.                      i = dig_left;
38.                      if (dig_left)
39.                          dig_left--; /* point at first blocked point */
40.                      for (; i < x; i++) {
41.                          left_ptrs[y][i] = dig_left;
42.                          right_ptrs[y][i] = x;
43.                          viz_clear[y][i] = 1;
44.                      }
45.                  }
46.                  dig_left = x;
47.                  block = !block;
48.              }
49.          /* handle right boundary; almost identical for blocked/unblocked */
50.          i = dig_left;
51.          if (!block && dig_left)
52.              dig_left--; /* point at first blocked point */
53.          for (; i < COLNO; i++) {
54.              left_ptrs[y][i] = dig_left;
55.              right_ptrs[y][i] = (COLNO - 1);
56.              viz_clear[y][i] = !block;
57.          }
58.      }
59.
60.      iflags.vision_inited = 1; /* vision is ready */
61.      vision_full_recalc = 1;   /* we want to run vision_recalc() */
62.  }
63.

### get_unused_cs

1.  /*
2.   * get_unused_cs()
3.   *
4.   * Called from vision_recalc() and at least one light routine.  Get pointers
5.   * to the unused vision work area.
6.   */
7.  STATIC_OVL void
8.  get_unused_cs(rows, rmin, rmax)
9.  char ***rows;
10.  char **rmin, **rmax;
11.  {
12.      register int row;
13.      register char *nrmin, *nrmax;
14.
15.      if (viz_array == cs_rows0) {
16.          *rows = cs_rows1;
17.          *rmin = cs_rmin1;
18.          *rmax = cs_rmax1;
19.      } else {
20.          *rows = cs_rows0;
21.          *rmin = cs_rmin0;
22.          *rmax = cs_rmax0;
23.      }
24.
25.      /* return an initialized, unused work area */
26.      nrmin = *rmin;
27.      nrmax = *rmax;
28.
29.      (void) memset((genericptr_t) * *rows, 0,
30.                    ROWNO * COLNO);       /* we see nothing */
31.      for (row = 0; row < ROWNO; row++) { /* set row min & max */
32.          *nrmin++ = COLNO - 1;
33.          *nrmax++ = 0;
34.      }
35.  }
36.

### rogue_vision

1.  /*
2.   * rogue_vision()
3.   *
4.   * Set the "could see" and in sight bits so vision acts just like the old
5.   * rogue game:
6.   *
7.   *      + If in a room, the hero can see to the room boundaries.
8.   *      + The hero can always see adjacent squares.
9.   *
10.   * We set the in_sight bit here as well to escape a bug that shows up
11.   * due to the one-sided lit wall hack.
12.   */
13.  STATIC_OVL void
14.  rogue_vision(next, rmin, rmax)
15.  char **next; /* could_see array pointers */
16.  char *rmin, *rmax;
17.  {
18.      int rnum = levl[u.ux][u.uy].roomno - ROOMOFFSET; /* no SHARED... */
19.      int start, stop, in_door, xhi, xlo, yhi, ylo;
20.      register int zx, zy;
21.
22.      /* If in a lit room, we are able to see to its boundaries. */
23.      /* If dark, set COULD_SEE so various spells work -dlc */
24.      if (rnum >= 0) {
25.          for (zy = rooms[rnum].ly - 1; zy <= rooms[rnum].hy + 1; zy++) {
26.              rmin[zy] = start = rooms[rnum].lx - 1;
27.              rmax[zy] = stop = rooms[rnum].hx + 1;
28.
29.              for (zx = start; zx <= stop; zx++) {
30.                  if (rooms[rnum].rlit) {
31.                      next[zy][zx] = COULD_SEE | IN_SIGHT;
32.                      levl[zx][zy].seenv = SVALL; /* see the walls */
33.                  } else
34.                      next[zy][zx] = COULD_SEE;
35.              }
36.          }
37.      }
38.
39.      in_door = levl[u.ux][u.uy].typ == DOOR;
40.
41.      /* Can always see adjacent. */
42.      ylo = max(u.uy - 1, 0);
43.      yhi = min(u.uy + 1, ROWNO - 1);
44.      xlo = max(u.ux - 1, 1);
45.      xhi = min(u.ux + 1, COLNO - 1);
46.      for (zy = ylo; zy <= yhi; zy++) {
47.          if (xlo < rmin[zy])
48.              rmin[zy] = xlo;
49.          if (xhi > rmax[zy])
50.              rmax[zy] = xhi;
51.
52.          for (zx = xlo; zx <= xhi; zx++) {
53.              next[zy][zx] = COULD_SEE | IN_SIGHT;
54.              /*
55.               * Yuck, update adjacent non-diagonal positions when in a doorway.
56.               * We need to do this to catch the case when we first step into
57.               * a room.  The room's walls were not seen from the outside, but
58.               * now are seen (the seen bits are set just above).  However, the
59.               * positions are not updated because they were already in sight.
60.               * So, we have to do it here.
61.               */
62.              if (in_door && (zx == u.ux || zy == u.uy))
63.                  newsym(zx, zy);
64.          }
65.      }
66.  }
67.

### new_angle

1.  /*#define EXTEND_SPINE*/ /* possibly better looking wall-angle */
2.
3.  #ifdef EXTEND_SPINE
4.
5.  STATIC_DCL int FDECL(new_angle, (struct rm *, unsigned char *, int, int));
6.  /*
7.   * new_angle()
8.   *
9.   * Return the new angle seen by the hero for this location.  The angle
10.   * bit is given in the value pointed at by sv.
11.   *
12.   * For T walls and crosswall, just setting the angle bit, even though
13.   * it is technically correct, doesn't look good.  If we can see the
14.   * next position beyond the current one and it is a wall that we can
15.   * see, then we want to extend a spine of the T to connect with the wall
16.   * that is beyond.  Example:
17.   *
18.   *       Correct, but ugly                         Extend T spine
19.   *
20.   *              | ...                                   | ...
21.   *              | ...   <-- wall beyond & floor -->     | ...
22.   *              | ...                                   | ...
23.   * Unseen   -->   ...                                   | ...
24.   * spine        +-...   <-- trwall & doorway    -->     +-...
25.   *              | ...                                   | ...
26.   *
27.   *
28.   *                 @    <-- hero                -->        @
29.   *
30.   *
31.   * We fake the above check by only checking if the horizontal &
32.   * vertical positions adjacent to the crosswall and T wall are
33.   * unblocked.  Then, _in general_ we can see beyond.  Generally,
34.   * this is good enough.
35.   *
36.   *      + When this function is called we don't have all of the seen
37.   *        information (we're doing a top down scan in vision_recalc).
38.   *        We would need to scan once to set all IN_SIGHT and COULD_SEE
39.   *        bits, then again to correctly set the seenv bits.
40.   *      + I'm trying to make this as cheap as possible.  The display &
41.   *        vision eat up too much CPU time.
42.   *
43.   *
44.   * Note:  Even as I write this, I'm still not convinced.  There are too
45.   *        many exceptions.  I may have to bite the bullet and do more
46.   *        checks.       - Dean 2/11/93
398.  */
1.  STATIC_OVL int
2.  new_angle(lev, sv, row, col)
3.  struct rm *lev;
4.  unsigned char *sv;
5.  int row, col;
6.  {
7.      register int res = *sv;
8.
9.      /*
10.       * Do extra checks for crosswalls and T walls if we see them from
11.       * an angle.
12.       */
13.      if (lev->typ >= CROSSWALL && lev->typ <= TRWALL) {
14.          switch (res) {
15.          case SV0:
16.              if (col > 0 && viz_clear[row][col - 1])
17.                  res |= SV7;
18.              if (row > 0 && viz_clear[row - 1][col])
19.                  res |= SV1;
20.              break;
21.          case SV2:
22.              if (row > 0 && viz_clear[row - 1][col])
23.                  res |= SV1;
24.              if (col < COLNO - 1 && viz_clear[row][col + 1])
25.                  res |= SV3;
26.              break;
27.          case SV4:
28.              if (col < COLNO - 1 && viz_clear[row][col + 1])
29.                  res |= SV3;
30.              if (row < ROWNO - 1 && viz_clear[row + 1][col])
31.                  res |= SV5;
32.              break;
33.          case SV6:
34.              if (row < ROWNO - 1 && viz_clear[row + 1][col])
35.                  res |= SV5;
36.              if (col > 0 && viz_clear[row][col - 1])
37.                  res |= SV7;
38.              break;
39.          }
40.      }
41.      return res;
42.  }
43.  #else
44.  /*
45.   * new_angle()
46.   *
47.   * Return the new angle seen by the hero for this location.  The angle
48.   * bit is given in the value pointed at by sv.
49.   *
50.   * The other parameters are not used.
51.   */
52.  #define new_angle(lev, sv, row, col) (*sv)
53.
54.  #endif
55.

### vision_recalc

1.  /*
2.   * vision_recalc()
3.   *
4.   * Do all of the heavy vision work.  Recalculate all locations that could
5.   * possibly be seen by the hero --- if the location were lit, etc.  Note
6.   * which locations are actually seen because of lighting.  Then add to
7.   * this all locations that be seen by hero due to night vision and x-ray
8.   * vision.  Finally, compare with what the hero was able to see previously.
9.   * Update the difference.
10.   *
11.   * This function is usually called only when the variable 'vision_full_recalc'
12.   * is set.  The following is a list of places where this function is called,
13.   * with three valid values for the control flag parameter:
14.   *
15.   * Control flag = 0.  A complete vision recalculation.  Generate the vision
16.   * tables from scratch.  This is necessary to correctly set what the hero
17.   * can see.  (1) and (2) call this routine for synchronization purposes, (3)
18.   * calls this routine so it can operate correctly.
19.   *
20.   *      + After the monster move, before input from the player. [moveloop()]
21.   *      + At end of moveloop. [moveloop() ??? not sure why this is here]
22.   *      + Right before something is printed. [pline()]
23.   *      + Right before we do a vision based operation. [do_clear_area()]
24.   *      + screen redraw, so we can renew all positions in sight. [docrt()]
25.   *      + When toggling temporary blindness, in case additional events
26.   *        impacted by vision occur during the same move [make_blinded()]
27.   *
28.   * Control flag = 1.  An adjacent vision recalculation.  The hero has moved
29.   * one square.  Knowing this, it might be possible to optimize the vision
30.   * recalculation using the current knowledge.  This is presently unimplemented
31.   * and is treated as a control = 0 call.
32.   *
33.   *      + Right after the hero moves. [domove()]
34.   *
35.   * Control flag = 2.  Turn off the vision system.  Nothing new will be
36.   * displayed, since nothing is seen.  This is usually done when you need
37.   * a newsym() run on all locations in sight, or on some locations but you
38.   * don't know which ones.
39.   *
40.   *      + Before a screen redraw, so all positions are renewed. [docrt()]
41.   *      + Right before the hero arrives on a new level. [goto_level()]
42.   *      + Right after a scroll of light is read. [litroom()]
43.   *      + After an option has changed that affects vision [parseoptions()]
44.   *      + Right after the hero is swallowed. [gulpmu()]
45.   *      + Just before bubbles are moved. [movebubbles()]
46.   */
47.  void
48.  vision_recalc(control)
49.  int control;
50.  {
51.      char **temp_array; /* points to the old vision array */
52.      char **next_array; /* points to the new vision array */
53.      char *next_row;    /* row pointer for the new array */
54.      char *old_row;     /* row pointer for the old array */
55.      char *next_rmin;   /* min pointer for the new array */
56.      char *next_rmax;   /* max pointer for the new array */
57.      char *ranges;      /* circle ranges -- used for xray & night vision */
58.      int row = 0;       /* row counter (outer loop)  */
59.      int start, stop;   /* inner loop starting/stopping index */
60.      int dx, dy;        /* one step from a lit door or lit wall (see below) */
61.      register int col;  /* inner loop counter */
62.      register struct rm *lev; /* pointer to current pos */
63.      struct rm *flev; /* pointer to position in "front" of current pos */
64.      extern unsigned char seenv_matrix[3][3]; /* from display.c */
65.      static unsigned char colbump[COLNO + 1]; /* cols to bump sv */
66.      unsigned char *sv;                       /* ptr to seen angle bits */
67.      int oldseenv;                            /* previous seenv value */
68.
69.      vision_full_recalc = 0; /* reset flag */
70.      if (in_mklev || !iflags.vision_inited)
71.          return;
72.
73.      /*
74.       * Either the light sources have been taken care of, or we must
75.       * recalculate them here.
76.       */
77.
78.      /* Get the unused could see, row min, and row max arrays. */
79.      get_unused_cs(&next_array, &next_rmin, &next_rmax);
80.
81.      /* You see nothing, nothing can see you --- if swallowed or refreshing. */
82.      if (u.uswallow || control == 2) {
83.          /* do nothing -- get_unused_cs() nulls out the new work area */
84.          ;
85.      } else if (Blind) {
86.          /*
87.           * Calculate the could_see array even when blind so that monsters
88.           * can see you, even if you can't see them.  Note that the current
89.           * setup allows:
90.           *
91.           *      + Monsters to see with the "new" vision, even on the rogue
92.           *        level.
93.           *
94.           *      + Monsters can see you even when you're in a pit.
95.           */
96.          view_from(u.uy, u.ux, next_array, next_rmin, next_rmax, 0,
97.                    (void FDECL((*), (int, int, genericptr_t))) 0,
98.                    (genericptr_t) 0);
99.
100.          /*
101.           * Our own version of the update loop below.  We know we can't see
102.           * anything, so we only need update positions we used to be able
103.           * to see.
104.           */
105.          temp_array = viz_array; /* set viz_array so newsym() will work */
106.          viz_array = next_array;
107.
108.          for (row = 0; row < ROWNO; row++) {
109.              old_row = temp_array[row];
110.
111.              /* Find the min and max positions on the row. */
112.              start = min(viz_rmin[row], next_rmin[row]);
113.              stop = max(viz_rmax[row], next_rmax[row]);
114.
115.              for (col = start; col <= stop; col++)
116.                  if (old_row[col] & IN_SIGHT)
117.                      newsym(col, row);
118.          }
119.
120.          /* skip the normal update loop */
121.          goto skip;
122.      } else if (Is_rogue_level(&u.uz)) {
123.          rogue_vision(next_array, next_rmin, next_rmax);
124.      } else {
125.          int has_night_vision = 1; /* hero has night vision */
126.
127.          if (Underwater && !Is_waterlevel(&u.uz)) {
128.              /*
129.               * The hero is under water.  Only see surrounding locations if
130.               * they are also underwater.  This overrides night vision but
131.               * does not override x-ray vision.
132.               */
133.              has_night_vision = 0;
134.
135.              for (row = u.uy - 1; row <= u.uy + 1; row++)
136.                  for (col = u.ux - 1; col <= u.ux + 1; col++) {
137.                      if (!isok(col, row) || !is_pool(col, row))
138.                          continue;
139.
140.                      next_rmin[row] = min(next_rmin[row], col);
141.                      next_rmax[row] = max(next_rmax[row], col);
142.                      next_array[row][col] = IN_SIGHT | COULD_SEE;
143.                  }
144.
145.          /* if in a pit, just update for immediate locations */
146.          } else if (u.utrap && u.utraptype == TT_PIT) {
147.              for (row = u.uy - 1; row <= u.uy + 1; row++) {
148.                  if (row < 0)
149.                      continue;
150.                  if (row >= ROWNO)
151.                      break;
152.
153.                  next_rmin[row] = max(0, u.ux - 1);
154.                  next_rmax[row] = min(COLNO - 1, u.ux + 1);
155.                  next_row = next_array[row];
156.
157.                  for (col = next_rmin[row]; col <= next_rmax[row]; col++)
158.                      next_row[col] = IN_SIGHT | COULD_SEE;
159.              }
160.          } else
161.              view_from(u.uy, u.ux, next_array, next_rmin, next_rmax, 0,
162.                        (void FDECL((*), (int, int, genericptr_t))) 0,
163.                        (genericptr_t) 0);
164.
165.          /*
166.           * Set the IN_SIGHT bit for xray and night vision.
167.           */
168.          if (u.xray_range >= 0) {
169.              if (u.xray_range) {
170.                  ranges = circle_ptr(u.xray_range);
171.
172.                  for (row = u.uy - u.xray_range; row <= u.uy + u.xray_range;
173.                       row++) {
174.                      if (row < 0)
175.                          continue;
176.                      if (row >= ROWNO)
177.                          break;
178.                      dy = v_abs(u.uy - row);
179.                      next_row = next_array[row];
180.
181.                      start = max(0, u.ux - ranges[dy]);
182.                      stop = min(COLNO - 1, u.ux + ranges[dy]);
183.
184.                      for (col = start; col <= stop; col++) {
185.                          char old_row_val = next_row[col];
186.                          next_row[col] |= IN_SIGHT;
187.                          oldseenv = levl[col][row].seenv;
188.                          levl[col][row].seenv = SVALL; /* see all! */
189.                          /* Update if previously not in sight or new angle. */
190.                          if (!(old_row_val & IN_SIGHT) || oldseenv != SVALL)
191.                              newsym(col, row);
192.                      }
193.
194.                      next_rmin[row] = min(start, next_rmin[row]);
195.                      next_rmax[row] = max(stop, next_rmax[row]);
196.                  }
197.
198.              } else { /* range is 0 */
199.                  next_array[u.uy][u.ux] |= IN_SIGHT;
200.                  levl[u.ux][u.uy].seenv = SVALL;
201.                  next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
202.                  next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
203.              }
204.          }
205.
206.          if (has_night_vision && u.xray_range < u.nv_range) {
207.              if (!u.nv_range) { /* range is 0 */
208.                  next_array[u.uy][u.ux] |= IN_SIGHT;
209.                  levl[u.ux][u.uy].seenv = SVALL;
210.                  next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
211.                  next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
212.              } else if (u.nv_range > 0) {
213.                  ranges = circle_ptr(u.nv_range);
214.
215.                  for (row = u.uy - u.nv_range; row <= u.uy + u.nv_range;
216.                       row++) {
217.                      if (row < 0)
218.                          continue;
219.                      if (row >= ROWNO)
220.                          break;
221.                      dy = v_abs(u.uy - row);
222.                      next_row = next_array[row];
223.
224.                      start = max(0, u.ux - ranges[dy]);
225.                      stop = min(COLNO - 1, u.ux + ranges[dy]);
226.
227.                      for (col = start; col <= stop; col++)
228.                          if (next_row[col])
229.                              next_row[col] |= IN_SIGHT;
230.
231.                      next_rmin[row] = min(start, next_rmin[row]);
232.                      next_rmax[row] = max(stop, next_rmax[row]);
233.                  }
234.              }
235.          }
236.      }
237.
238.      /* Set the correct bits for all light sources. */
239.      do_light_sources(next_array);
240.
241.      /*
242.       * Make the viz_array the new array so that cansee() will work correctly.
243.       */
244.      temp_array = viz_array;
245.      viz_array = next_array;
246.
247.      /*
248.       * The main update loop.  Here we do two things:
249.       *
250.       *      + Set the IN_SIGHT bit for places that we could see and are lit.
251.       *      + Reset changed places.
252.       *
253.       * There is one thing that make deciding what the hero can see
254.       * difficult:
255.       *
256.       *  1.  Directional lighting.  Items that block light create problems.
257.       *      The worst offenders are doors.  Suppose a door to a lit room
258.       *      is closed.  It is lit on one side, but not on the other.  How
259.       *      do you know?  You have to check the closest adjacent position.
260.       *      Even so, that is not entirely correct.  But it seems close
261.       *      enough for now.
262.       */
263.      colbump[u.ux] = colbump[u.ux + 1] = 1;
264.      for (row = 0; row < ROWNO; row++) {
265.          dy = u.uy - row;
266.          dy = sign(dy);
267.          next_row = next_array[row];
268.          old_row = temp_array[row];
269.
270.          /* Find the min and max positions on the row. */
271.          start = min(viz_rmin[row], next_rmin[row]);
272.          stop = max(viz_rmax[row], next_rmax[row]);
273.          lev = &levl[start][row];
274.
275.          sv = &seenv_matrix[dy + 1][start < u.ux ? 0 : (start > u.ux ? 2 : 1)];
276.
277.          for (col = start; col <= stop;
278.               lev += ROWNO, sv += (int) colbump[++col]) {
279.              if (next_row[col] & IN_SIGHT) {
280.                  /*
281.                   * We see this position because of night- or xray-vision.
282.                   */
283.                  oldseenv = lev->seenv;
284.                  lev->seenv |=
285.                      new_angle(lev, sv, row, col); /* update seen angle */
286.
287.                  /* Update pos if previously not in sight or new angle. */
288.                  if (!(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
289.                      newsym(col, row);
290.
291.              } else if ((next_row[col] & COULD_SEE)
292.                       && (lev->lit || (next_row[col] & TEMP_LIT))) {
293.                  /*
294.                   * We see this position because it is lit.
295.                   */
296.                  if ((IS_DOOR(lev->typ) || lev->typ == SDOOR
297.                       || IS_WALL(lev->typ)) && !viz_clear[row][col]) {
298.                      /*
299.                       * Make sure doors, walls, boulders or mimics don't show
300.                       * up
301.                       * at the end of dark hallways.  We do this by checking
302.                       * the adjacent position.  If it is lit, then we can see
303.                       * the door or wall, otherwise we can't.
304.                       */
305.                      dx = u.ux - col;
306.                      dx = sign(dx);
307.                      flev = &(levl[col + dx][row + dy]);
308.                      if (flev->lit
309.                          || next_array[row + dy][col + dx] & TEMP_LIT) {
310.                          next_row[col] |= IN_SIGHT; /* we see it */
311.
312.                          oldseenv = lev->seenv;
313.                          lev->seenv |= new_angle(lev, sv, row, col);
314.
315.                          /* Update pos if previously not in sight or new
316.                           * angle.*/
317.                          if (!(old_row[col] & IN_SIGHT)
318.                              || oldseenv != lev->seenv)
319.                              newsym(col, row);
320.                      } else
321.                          goto not_in_sight; /* we don't see it */
322.
323.                  } else {
324.                      next_row[col] |= IN_SIGHT; /* we see it */
325.
326.                      oldseenv = lev->seenv;
327.                      lev->seenv |= new_angle(lev, sv, row, col);
328.
329.                      /* Update pos if previously not in sight or new angle. */
330.                      if (!(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
331.                          newsym(col, row);
332.                  }
333.              } else if ((next_row[col] & COULD_SEE) && lev->waslit) {
334.                  /*
335.                   * If we make it here, the hero _could see_ the location,
336.                   * but doesn't see it (location is not lit).
337.                   * However, the hero _remembers_ it as lit (waslit is true).
338.                   * The hero can now see that it is not lit, so change waslit
339.                   * and update the location.
340.                   */
341.                  lev->waslit = 0; /* remember lit condition */
342.                  newsym(col, row);
343.
344.              /*
345.               * At this point we know that the row position is *not* in normal
346.               * sight.  That is, the position is could be seen, but is dark
347.               * or LOS is just plain blocked.
348.               *
349.               * Update the position if:
350.               * o If the old one *was* in sight.  We may need to clean up
351.               *   the glyph -- E.g. darken room spot, etc.
352.               * o If we now could see the location (yet the location is not
353.               *   lit), but previously we couldn't see the location, or vice
354.               *   versa.  Update the spot because there there may be an
355.               *   infrared monster there.
356.               */
357.              } else {
358.              not_in_sight:
359.                  if ((old_row[col] & IN_SIGHT)
360.                      || ((next_row[col] & COULD_SEE)
361.                          ^ (old_row[col] & COULD_SEE)))
362.                      newsym(col, row);
363.              }
364.
365.          } /* end for col . . */
366.      }     /* end for row . .  */
367.      colbump[u.ux] = colbump[u.ux + 1] = 0;
368.
369.  skip:
370.      /* This newsym() caused a crash delivering msg about failure to open
371.       * dungeon file init_dungeons() -> panic() -> done(11) ->
372.       * vision_recalc(2) -> newsym() -> crash!  u.ux and u.uy are 0 and
373.       * program_state.panicking == 1 under those circumstances
374.       */
375.      if (!program_state.panicking)
376.          newsym(u.ux, u.uy); /* Make sure the hero shows up! */
377.
378.      /* Set the new min and max pointers. */
379.      viz_rmin = next_rmin;
380.      viz_rmax = next_rmax;
381.
382.      recalc_mapseen();
383.  }
384.

### block_point

1.  /*
2.   * block_point()
3.   *
4.   * Make the location opaque to light.
5.   */
6.  void
7.  block_point(x, y)
8.  int x, y;
9.  {
10.      fill_point(y, x);
11.
12.      /* recalc light sources here? */
13.
14.      /*
15.       * We have to do a full vision recalculation if we "could see" the
16.       * location.  Why? Suppose some monster opened a way so that the
17.       * hero could see a lit room.  However, the position of the opening
18.       * was out of night-vision range of the hero.  Suddenly the hero should
19.       * see the lit room.
20.       */
21.      if (viz_array[y][x])
22.          vision_full_recalc = 1;
23.  }
24.

### unblock_point

1.  /*
2.   * unblock_point()
3.   *
4.   * Make the location transparent to light.
5.   */
6.  void
7.  unblock_point(x, y)
8.  int x, y;
9.  {
10.      dig_point(y, x);
11.
12.      /* recalc light sources here? */
13.
14.      if (viz_array[y][x])
15.          vision_full_recalc = 1;
16.  }
17.

## Left and right pointer updates

1.  /*==========================================================================*\
2.   |                                                                          |
3.   |      Everything below this line uses (y,x) instead of (x,y) --- the      |
4.   |      algorithms are faster if they are less recursive and can scan       |
5.   |      on a row longer.                                                    |
6.   |                                                                          |
7.  \*==========================================================================*/
8.
9.  /* ======================================================================= *\
10.                          Left and Right Pointer Updates
11.  \* ======================================================================= */
12.
13.  /*
14.   *              LEFT and RIGHT pointer rules
15.   *
16.   *
17.   * **NOTE**  The rules changed on 4/4/90.  This comment reflects the
18.   * new rules.  The change was so that the stone-wall optimization
19.   * would work.
20.   *
21.   * OK, now the tough stuff.  We must maintain our left and right
22.   * row pointers.  The rules are as follows:
23.   *
24.   * Left Pointers:
25.   * ______________
26.   *
27.   * + If you are a clear spot, your left will point to the first
28.   *   stone to your left.  If there is none, then point the first
29.   *   legal position in the row (0).
30.   *
31.   * + If you are a blocked spot, then your left will point to the
32.   *   left-most blocked spot to your left that is connected to you.
33.   *   This means that a left-edge (a blocked spot that has an open
34.   *   spot on its left) will point to itself.
35.   *
36.   *
37.   * Right Pointers:
38.   * ---------------
39.   * + If you are a clear spot, your right will point to the first
40.   *   stone to your right.  If there is none, then point the last
41.   *   legal position in the row (COLNO-1).
42.   *
43.   * + If you are a blocked spot, then your right will point to the
44.   *   right-most blocked spot to your right that is connected to you.
45.   *   This means that a right-edge (a blocked spot that has an open
46.   *    spot on its right) will point to itself.
47.   */

### dig_point

1.  STATIC_OVL void
2.  dig_point(row, col)
3.  int row, col;
4.  {
5.      int i;
6.
7.      if (viz_clear[row][col])
8.          return; /* already done */
9.
10.      viz_clear[row][col] = 1;
11.
12.      /*
13.       * Boundary cases first.
14.       */
15.      if (col == 0) { /* left edge */
16.          if (viz_clear[row][1]) {
17.              right_ptrs[row][0] = right_ptrs[row][1];
18.          } else {
19.              right_ptrs[row][0] = 1;
20.              for (i = 1; i <= right_ptrs[row][1]; i++)
21.                  left_ptrs[row][i] = 1;
22.          }
23.      } else if (col == (COLNO - 1)) { /* right edge */
24.
25.          if (viz_clear[row][COLNO - 2]) {
26.              left_ptrs[row][COLNO - 1] = left_ptrs[row][COLNO - 2];
27.          } else {
28.              left_ptrs[row][COLNO - 1] = COLNO - 2;
29.              for (i = left_ptrs[row][COLNO - 2]; i < COLNO - 1; i++)
30.                  right_ptrs[row][i] = COLNO - 2;
31.          }
32.
33.      /*
34.       * At this point, we know we aren't on the boundaries.
35.       */
36.      } else if (viz_clear[row][col - 1] && viz_clear[row][col + 1]) {
37.          /* Both sides clear */
38.          for (i = left_ptrs[row][col - 1]; i <= col; i++) {
39.              if (!viz_clear[row][i])
40.                  continue; /* catch non-end case */
41.              right_ptrs[row][i] = right_ptrs[row][col + 1];
42.          }
43.          for (i = col; i <= right_ptrs[row][col + 1]; i++) {
44.              if (!viz_clear[row][i])
45.                  continue; /* catch non-end case */
46.              left_ptrs[row][i] = left_ptrs[row][col - 1];
47.          }
48.
49.      } else if (viz_clear[row][col - 1]) {
50.          /* Left side clear, right side blocked. */
51.          for (i = col + 1; i <= right_ptrs[row][col + 1]; i++)
52.              left_ptrs[row][i] = col + 1;
53.
54.          for (i = left_ptrs[row][col - 1]; i <= col; i++) {
55.              if (!viz_clear[row][i])
56.                  continue; /* catch non-end case */
57.              right_ptrs[row][i] = col + 1;
58.          }
59.          left_ptrs[row][col] = left_ptrs[row][col - 1];
60.
61.      } else if (viz_clear[row][col + 1]) {
62.          /* Right side clear, left side blocked. */
63.          for (i = left_ptrs[row][col - 1]; i < col; i++)
64.              right_ptrs[row][i] = col - 1;
65.
66.          for (i = col; i <= right_ptrs[row][col + 1]; i++) {
67.              if (!viz_clear[row][i])
68.                  continue; /* catch non-end case */
69.              left_ptrs[row][i] = col - 1;
70.          }
71.          right_ptrs[row][col] = right_ptrs[row][col + 1];
72.
73.      } else {
74.          /* Both sides blocked */
75.          for (i = left_ptrs[row][col - 1]; i < col; i++)
76.              right_ptrs[row][i] = col - 1;
77.
78.          for (i = col + 1; i <= right_ptrs[row][col + 1]; i++)
79.              left_ptrs[row][i] = col + 1;
80.
81.          left_ptrs[row][col] = col - 1;
82.          right_ptrs[row][col] = col + 1;
83.      }
84.  }
85.

### fill_point

1.  STATIC_OVL void
2.  fill_point(row, col)
3.  int row, col;
4.  {
5.      int i;
6.
7.      if (!viz_clear[row][col])
8.          return;
9.
10.      viz_clear[row][col] = 0;
11.
12.      if (col == 0) {
13.          if (viz_clear[row][1]) { /* adjacent is clear */
14.              right_ptrs[row][0] = 0;
15.          } else {
16.              right_ptrs[row][0] = right_ptrs[row][1];
17.              for (i = 1; i <= right_ptrs[row][1]; i++)
18.                  left_ptrs[row][i] = 0;
19.          }
20.      } else if (col == COLNO - 1) {
21.          if (viz_clear[row][COLNO - 2]) { /* adjacent is clear */
22.              left_ptrs[row][COLNO - 1] = COLNO - 1;
23.          } else {
24.              left_ptrs[row][COLNO - 1] = left_ptrs[row][COLNO - 2];
25.              for (i = left_ptrs[row][COLNO - 2]; i < COLNO - 1; i++)
26.                  right_ptrs[row][i] = COLNO - 1;
27.          }
28.
29.      /*
30.       * Else we know that we are not on an edge.
31.       */
32.      } else if (viz_clear[row][col - 1] && viz_clear[row][col + 1]) {
33.          /* Both sides clear */
34.          for (i = left_ptrs[row][col - 1] + 1; i <= col; i++)
35.              right_ptrs[row][i] = col;
36.
37.          if (!left_ptrs[row][col - 1]) /* catch the end case */
38.              right_ptrs[row][0] = col;
39.
40.          for (i = col; i < right_ptrs[row][col + 1]; i++)
41.              left_ptrs[row][i] = col;
42.
43.          if (right_ptrs[row][col + 1] == COLNO - 1) /* catch the end case */
44.              left_ptrs[row][COLNO - 1] = col;
45.
46.      } else if (viz_clear[row][col - 1]) {
47.          /* Left side clear, right side blocked. */
48.          for (i = col; i <= right_ptrs[row][col + 1]; i++)
49.              left_ptrs[row][i] = col;
50.
51.          for (i = left_ptrs[row][col - 1] + 1; i < col; i++)
52.              right_ptrs[row][i] = col;
53.
54.          if (!left_ptrs[row][col - 1]) /* catch the end case */
55.              right_ptrs[row][i] = col;
56.
57.          right_ptrs[row][col] = right_ptrs[row][col + 1];
58.
59.      } else if (viz_clear[row][col + 1]) {
60.          /* Right side clear, left side blocked. */
61.          for (i = left_ptrs[row][col - 1]; i <= col; i++)
62.              right_ptrs[row][i] = col;
63.
64.          for (i = col + 1; i < right_ptrs[row][col + 1]; i++)
65.              left_ptrs[row][i] = col;
66.
67.          if (right_ptrs[row][col + 1] == COLNO - 1) /* catch the end case */
68.              left_ptrs[row][i] = col;
69.
70.          left_ptrs[row][col] = left_ptrs[row][col - 1];
71.
72.      } else {
73.          /* Both sides blocked */
74.          for (i = left_ptrs[row][col - 1]; i <= col; i++)
75.              right_ptrs[row][i] = right_ptrs[row][col + 1];
76.
77.          for (i = col; i <= right_ptrs[row][col + 1]; i++)
78.              left_ptrs[row][i] = left_ptrs[row][col - 1];
79.      }
80.  }
81.

## Algorithms C and D

1.  /*==========================================================================*/
2.  /*==========================================================================*/
3.  /* Use either algorithm C or D.  See the config.h for more details.
4.   * =========*/
5.
6.  /*
7.   * Variables local to both Algorithms C and D.
8.   */
9.  static int start_row;
10.  static int start_col;
11.  static int step;
12.  static char **cs_rows;
13.  static char *cs_left;
14.  static char *cs_right;
15.
16.  static void FDECL((*vis_func), (int, int, genericptr_t));
17.  static genericptr_t varg;
18.
19.  /*
20.   * Both Algorithms C and D use the following macros.
21.   *
22.   *      good_row(z)       - Return TRUE if the argument is a legal row.
23.   *      set_cs(rowp,col)  - Set the local could see array.
24.   *      set_min(z)        - Save the min value of the argument and the current
25.   *                            row minimum.
26.   *      set_max(z)        - Save the max value of the argument and the current
27.   *                            row maximum.
28.   *
29.   * The last three macros depend on having local pointers row_min, row_max,
30.   * and rowp being set correctly.
31.   */
32.  #define set_cs(rowp, col) (rowp[col] = COULD_SEE)
33.  #define good_row(z) ((z) >= 0 && (z) < ROWNO)
34.  #define set_min(z)      \
35.      if (*row_min > (z)) \
36.          *row_min = (z)
37.  #define set_max(z)      \
38.      if (*row_max < (z)) \
39.          *row_max = (z)
40.  #define is_clear(row, col) viz_clear_rows[row][col]
41.

### clear_path macros/functions

1.  /*
2.   * clear_path()         expanded into 4 macros/functions:
3.   *
4.   *      q1_path()
5.   *      q2_path()
6.   *      q3_path()
7.   *      q4_path()
8.   *
9.   * "Draw" a line from the start to the given location.  Stop if we hit
10.   * something that blocks light.  The start and finish points themselves are
11.   * not checked, just the points between them.  These routines do _not_
12.   * expect to be called with the same starting and stopping point.
13.   *
14.   * These routines use the generalized integer Bresenham's algorithm (fast
15.   * line drawing) for all quadrants.  The algorithm was taken from _Procedural
16.   * Elements for Computer Graphics_, by David F. Rogers.  McGraw-Hill, 1985.
17.   */
18.  #ifdef MACRO_CPATH /* quadrant calls are macros */
19.
20.  /*
21.   * When called, the result is in "result".
22.   * The first two arguments (srow,scol) are one end of the path.  The next
23.   * two arguments (row,col) are the destination.  The last argument is
24.   * used as a C language label.  This means that it must be different
25.   * in each pair of calls.
26.   */
27.

#### q1_path

1.  /*
2.   *  Quadrant I (step < 0).
3.   */
4.  #define q1_path(srow, scol, y2, x2, label)           \
5.      {                                                \
6.          int dx, dy;                                  \
7.          register int k, err, x, y, dxs, dys;         \
8.                                                       \
9.          x = (scol);                                  \
10.          y = (srow);                                  \
11.          dx = (x2) -x;                                \
12.          dy = y - (y2);                               \
13.                                                       \
14.          result = 0; /* default to a blocked path */  \
15.                                                       \
16.          dxs = dx << 1; /* save the shifted values */ \
17.          dys = dy << 1;                               \
18.          if (dy > dx) {                               \
19.              err = dxs - dy;                          \
20.                                                       \
21.              for (k = dy - 1; k; k--) {               \
22.                  if (err >= 0) {                      \
23.                      x++;                             \
24.                      err -= dys;                      \
25.                  }                                    \
26.                  y--;                                 \
27.                  err += dxs;                          \
28.                  if (!is_clear(y, x))                 \
29.                      goto label; /* blocked */        \
30.              }                                        \
31.          } else {                                     \
32.              err = dys - dx;                          \
33.                                                       \
34.              for (k = dx - 1; k; k--) {               \
35.                  if (err >= 0) {                      \
36.                      y--;                             \
37.                      err -= dxs;                      \
38.                  }                                    \
39.                  x++;                                 \
40.                  err += dys;                          \
41.                  if (!is_clear(y, x))                 \
42.                      goto label; /* blocked */        \
43.              }                                        \
44.          }                                            \
45.                                                       \
46.          result = 1;                                  \
47.      }
48.

#### q4_path

1.  /*
2.   * Quadrant IV (step > 0).
3.   */
4.  #define q4_path(srow, scol, y2, x2, label)           \
5.      {                                                \
6.          int dx, dy;                                  \
7.          register int k, err, x, y, dxs, dys;         \
8.                                                       \
9.          x = (scol);                                  \
10.          y = (srow);                                  \
11.          dx = (x2) -x;                                \
12.          dy = (y2) -y;                                \
13.                                                       \
14.          result = 0; /* default to a blocked path */  \
15.                                                       \
16.          dxs = dx << 1; /* save the shifted values */ \
17.          dys = dy << 1;                               \
18.          if (dy > dx) {                               \
19.              err = dxs - dy;                          \
20.                                                       \
21.              for (k = dy - 1; k; k--) {               \
22.                  if (err >= 0) {                      \
23.                      x++;                             \
24.                      err -= dys;                      \
25.                  }                                    \
26.                  y++;                                 \
27.                  err += dxs;                          \
28.                  if (!is_clear(y, x))                 \
29.                      goto label; /* blocked */        \
30.              }                                        \
31.                                                       \
32.          } else {                                     \
33.              err = dys - dx;                          \
34.                                                       \
35.              for (k = dx - 1; k; k--) {               \
36.                  if (err >= 0) {                      \
37.                      y++;                             \
38.                      err -= dxs;                      \
39.                  }                                    \
40.                  x++;                                 \
41.                  err += dys;                          \
42.                  if (!is_clear(y, x))                 \
43.                      goto label; /* blocked */        \
44.              }                                        \
45.          }                                            \
46.                                                       \
47.          result = 1;                                  \
48.      }
49.

#### q2_path

1.  /*
2.   * Quadrant II (step < 0).
3.   */
4.  #define q2_path(srow, scol, y2, x2, label)           \
5.      {                                                \
6.          int dx, dy;                                  \
7.          register int k, err, x, y, dxs, dys;         \
8.                                                       \
9.          x = (scol);                                  \
10.          y = (srow);                                  \
11.          dx = x - (x2);                               \
12.          dy = y - (y2);                               \
13.                                                       \
14.          result = 0; /* default to a blocked path */  \
15.                                                       \
16.          dxs = dx << 1; /* save the shifted values */ \
17.          dys = dy << 1;                               \
18.          if (dy > dx) {                               \
19.              err = dxs - dy;                          \
20.                                                       \
21.              for (k = dy - 1; k; k--) {               \
22.                  if (err >= 0) {                      \
23.                      x--;                             \
24.                      err -= dys;                      \
25.                  }                                    \
26.                  y--;                                 \
27.                  err += dxs;                          \
28.                  if (!is_clear(y, x))                 \
29.                      goto label; /* blocked */        \
30.              }                                        \
31.          } else {                                     \
32.              err = dys - dx;                          \
33.                                                       \
34.              for (k = dx - 1; k; k--) {               \
35.                  if (err >= 0) {                      \
36.                      y--;                             \
37.                      err -= dxs;                      \
38.                  }                                    \
39.                  x--;                                 \
40.                  err += dys;                          \
41.                  if (!is_clear(y, x))                 \
42.                      goto label; /* blocked */        \
43.              }                                        \
44.          }                                            \
45.                                                       \
46.          result = 1;                                  \
47.      }
48.

#### q3_path

1.  /*
2.   * Quadrant III (step > 0).
3.   */
4.  #define q3_path(srow, scol, y2, x2, label)           \
5.      {                                                \
6.          int dx, dy;                                  \
7.          register int k, err, x, y, dxs, dys;         \
8.                                                       \
9.          x = (scol);                                  \
10.          y = (srow);                                  \
11.          dx = x - (x2);                               \
12.          dy = (y2) -y;                                \
13.                                                       \
14.          result = 0; /* default to a blocked path */  \
15.                                                       \
16.          dxs = dx << 1; /* save the shifted values */ \
17.          dys = dy << 1;                               \
18.          if (dy > dx) {                               \
19.              err = dxs - dy;                          \
20.                                                       \
21.              for (k = dy - 1; k; k--) {               \
22.                  if (err >= 0) {                      \
23.                      x--;                             \
24.                      err -= dys;                      \
25.                  }                                    \
26.                  y++;                                 \
27.                  err += dxs;                          \
28.                  if (!is_clear(y, x))                 \
29.                      goto label; /* blocked */        \
30.              }                                        \
31.                                                       \
32.          } else {                                     \
33.              err = dys - dx;                          \
34.                                                       \
35.              for (k = dx - 1; k; k--) {               \
36.                  if (err >= 0) {                      \
37.                      y++;                             \
38.                      err -= dxs;                      \
39.                  }                                    \
40.                  x--;                                 \
41.                  err += dys;                          \
42.                  if (!is_clear(y, x))                 \
43.                      goto label; /* blocked */        \
44.              }                                        \
45.          }                                            \
46.                                                       \
47.          result = 1;                                  \
48.      }
49.

1.  #else /* !MACRO_CPATH -- quadrants are really functions */
2.
3.  STATIC_DCL int FDECL(_q1_path, (int, int, int, int));
4.  STATIC_DCL int FDECL(_q2_path, (int, int, int, int));
5.  STATIC_DCL int FDECL(_q3_path, (int, int, int, int));
6.  STATIC_DCL int FDECL(_q4_path, (int, int, int, int));
7.
8.  #define q1_path(sy, sx, y, x, dummy) result = _q1_path(sy, sx, y, x)
9.  #define q2_path(sy, sx, y, x, dummy) result = _q2_path(sy, sx, y, x)
10.  #define q3_path(sy, sx, y, x, dummy) result = _q3_path(sy, sx, y, x)
11.  #define q4_path(sy, sx, y, x, dummy) result = _q4_path(sy, sx, y, x)
12.

#### _q1_path

1.  /*
2.   * Quadrant I (step < 0).
3.   */
4.  STATIC_OVL int
5.  _q1_path(srow, scol, y2, x2)
6.  int scol, srow, y2, x2;
7.  {
8.      int dx, dy;
9.      register int k, err, x, y, dxs, dys;
10.
11.      x = scol;
12.      y = srow;
13.      dx = x2 - x;
14.      dy = y - y2;
15.
16.      dxs = dx << 1; /* save the shifted values */
17.      dys = dy << 1;
18.      if (dy > dx) {
19.          err = dxs - dy;
20.
21.          for (k = dy - 1; k; k--) {
22.              if (err >= 0) {
23.                  x++;
24.                  err -= dys;
25.              }
26.              y--;
27.              err += dxs;
28.              if (!is_clear(y, x))
29.                  return 0; /* blocked */
30.          }
31.      } else {
32.          err = dys - dx;
33.
34.          for (k = dx - 1; k; k--) {
35.              if (err >= 0) {
36.                  y--;
37.                  err -= dxs;
38.              }
39.              x++;
40.              err += dys;
41.              if (!is_clear(y, x))
42.                  return 0; /* blocked */
43.          }
44.      }
45.
46.      return 1;
47.  }
48.

#### _q4_path

1.  /*
2.   * Quadrant IV (step > 0).
3.   */
4.  STATIC_OVL int
5.  _q4_path(srow, scol, y2, x2)
6.  int scol, srow, y2, x2;
7.  {
8.      int dx, dy;
9.      register int k, err, x, y, dxs, dys;
10.
11.      x = scol;
12.      y = srow;
13.      dx = x2 - x;
14.      dy = y2 - y;
15.
16.      dxs = dx << 1; /* save the shifted values */
17.      dys = dy << 1;
18.      if (dy > dx) {
19.          err = dxs - dy;
20.
21.          for (k = dy - 1; k; k--) {
22.              if (err >= 0) {
23.                  x++;
24.                  err -= dys;
25.              }
26.              y++;
27.              err += dxs;
28.              if (!is_clear(y, x))
29.                  return 0; /* blocked */
30.          }
31.      } else {
32.          err = dys - dx;
33.
34.          for (k = dx - 1; k; k--) {
35.              if (err >= 0) {
36.                  y++;
37.                  err -= dxs;
38.              }
39.              x++;
40.              err += dys;
41.              if (!is_clear(y, x))
42.                  return 0; /* blocked */
43.          }
44.      }
45.
46.      return 1;
47.  }
48.

#### _q2_path

1.  /*
2.   * Quadrant II (step < 0).
3.   */
4.  STATIC_OVL int
5.  _q2_path(srow, scol, y2, x2)
6.  int scol, srow, y2, x2;
7.  {
8.      int dx, dy;
9.      register int k, err, x, y, dxs, dys;
10.
11.      x = scol;
12.      y = srow;
13.      dx = x - x2;
14.      dy = y - y2;
15.
16.      dxs = dx << 1; /* save the shifted values */
17.      dys = dy << 1;
18.      if (dy > dx) {
19.          err = dxs - dy;
20.
21.          for (k = dy - 1; k; k--) {
22.              if (err >= 0) {
23.                  x--;
24.                  err -= dys;
25.              }
26.              y--;
27.              err += dxs;
28.              if (!is_clear(y, x))
29.                  return 0; /* blocked */
30.          }
31.      } else {
32.          err = dys - dx;
33.
34.          for (k = dx - 1; k; k--) {
35.              if (err >= 0) {
36.                  y--;
37.                  err -= dxs;
38.              }
39.              x--;
40.              err += dys;
41.              if (!is_clear(y, x))
42.                  return 0; /* blocked */
43.          }
44.      }
45.
46.      return 1;
47.  }
48.

#### _q3_path

1.  /*
2.   * Quadrant III (step > 0).
3.   */
4.  STATIC_OVL int
5.  _q3_path(srow, scol, y2, x2)
6.  int scol, srow, y2, x2;
7.  {
8.      int dx, dy;
9.      register int k, err, x, y, dxs, dys;
10.
11.      x = scol;
12.      y = srow;
13.      dx = x - x2;
14.      dy = y2 - y;
15.
16.      dxs = dx << 1; /* save the shifted values */
17.      dys = dy << 1;
18.      if (dy > dx) {
19.          err = dxs - dy;
20.
21.          for (k = dy - 1; k; k--) {
22.              if (err >= 0) {
23.                  x--;
24.                  err -= dys;
25.              }
26.              y++;
27.              err += dxs;
28.              if (!is_clear(y, x))
29.                  return 0; /* blocked */
30.          }
31.      } else {
32.          err = dys - dx;
33.
34.          for (k = dx - 1; k; k--) {
35.              if (err >= 0) {
36.                  y++;
37.                  err -= dxs;
38.              }
39.              x--;
40.              err += dys;
41.              if (!is_clear(y, x))
42.                  return 0; /* blocked */
43.          }
44.      }
45.
46.      return 1;
47.  }
48.
49.  #endif /* ?MACRO_CPATH */
50.

### clear_path

1.  /*
2.   * Use vision tables to determine if there is a clear path from
3.   * (col1,row1) to (col2,row2).  This is used by:
4.   *      m_cansee()
5.   *      m_canseeu()
6.   *      do_light_sources()
7.   */
8.  boolean
9.  clear_path(col1, row1, col2, row2)
10.  int col1, row1, col2, row2;
11.  {
12.      int result;
13.
14.      if (col1 < col2) {
15.          if (row1 > row2) {
16.              q1_path(row1, col1, row2, col2, cleardone);
17.          } else {
18.              q4_path(row1, col1, row2, col2, cleardone);
19.          }
20.      } else {
21.          if (row1 > row2) {
22.              q2_path(row1, col1, row2, col2, cleardone);
23.          } else if (row1 == row2 && col1 == col2) {
24.              result = 1;
25.          } else {
26.              q3_path(row1, col1, row2, col2, cleardone);
27.          }
28.      }
29.  #ifdef MACRO_CPATH
30.  cleardone:
31.  #endif
32.      return (boolean) result;
33.  }
34.

## General Line of Sight: Algorithm D

1.  #ifdef VISION_TABLES
2.  /*==========================================================================*\
3.                              GENERAL LINE OF SIGHT
4.                                  Algorithm D
5.  \*==========================================================================*/
6.
7.  /*
8.   * Indicate caller for the shadow routines.
9.   */
10.  #define FROM_RIGHT 0
11.  #define FROM_LEFT 1
12.
13.  /*
14.   * Include the table definitions.
15.   */
16.  #include "vis_tab.h"
17.
18.  /* 3D table pointers. */
19.  static close2d *close_dy[CLOSE_MAX_BC_DY];
20.  static far2d *far_dy[FAR_MAX_BC_DY];
21.
22.  STATIC_DCL void FDECL(right_side,  (int, int, int, int, int,
23.                                      int, int, char *));
24.  STATIC_DCL void FDECL(left_side, (int, int, int, int, int, int, int, char *));
25.  STATIC_DCL int FDECL(close_shadow, (int, int, int, int));
26.  STATIC_DCL int FDECL(far_shadow, (int, int, int, int));
27.

### view_init

1.  /*
2.   * Initialize algorithm D's table pointers.  If we don't have these,
3.   * then we do 3D table lookups.  Verrrry slow.
4.   */
5.  STATIC_OVL void
6.  view_init()
7.  {
8.      int i;
9.
10.      for (i = 0; i < CLOSE_MAX_BC_DY; i++)
11.          close_dy[i] = &close_table[i];
12.
13.      for (i = 0; i < FAR_MAX_BC_DY; i++)
14.          far_dy[i] = &far_table[i];
15.  }
16.

1.  /*
2.   * If the far table has an entry of OFF_TABLE, then the far block prevents
3.   * us from seeing the location just above/below it.  I.e. the first visible
4.   * location is one *before* the block.
5.   */
6.  #define OFF_TABLE 0xff
7.
8.  STATIC_OVL int
10.  int side, this_row, block_row, block_col;
11.  {
12.      register int sdy, sdx, pdy, offset;
13.
14.      /*
15.       * If on the same column (block_row = -1), then we can see it.
16.       */
17.      if (block_row < 0)
18.          return block_col;
19.
20.      /* Take explicit absolute values.  Adjust. */
21.      if ((sdy = (start_row - block_row)) < 0)
22.          sdy = -sdy;
23.      --sdy; /* src   dy */
24.      if ((sdx = (start_col - block_col)) < 0)
25.          sdx = -sdx; /* src   dx */
26.      if ((pdy = (block_row - this_row)) < 0)
27.          pdy = -pdy; /* point dy */
28.
29.      if (sdy < 0 || sdy >= CLOSE_MAX_SB_DY || sdx >= CLOSE_MAX_SB_DX
30.          || pdy >= CLOSE_MAX_BC_DY) {
32.          return block_col;
33.      }
34.      offset = close_dy[sdy]->close[sdx][pdy];
35.      if (side == FROM_RIGHT)
36.          return block_col + offset;
37.
38.      return block_col - offset;
39.  }
40.

1.  STATIC_OVL int
3.  int side, this_row, block_row, block_col;
4.  {
5.      register int sdy, sdx, pdy, offset;
6.
7.      /*
8.       * Take care of a bug that shows up only on the borders.
9.       *
10.       * If the block is beyond the border, then the row is negative.  Return
11.       * the block's column number (should be 0 or COLNO-1).
12.       *
13.       * Could easily have the column be -1, but then wouldn't know if it was
14.       * the left or right border.
15.       */
16.      if (block_row < 0)
17.          return block_col;
18.
19.      /* Take explicit absolute values.  Adjust. */
20.      if ((sdy = (start_row - block_row)) < 0)
21.          sdy = -sdy; /* src   dy */
22.      if ((sdx = (start_col - block_col)) < 0)
23.          sdx = -sdx;
24.      --sdx; /* src   dx */
25.      if ((pdy = (block_row - this_row)) < 0)
26.          pdy = -pdy;
27.      --pdy; /* point dy */
28.
29.      if (sdy >= FAR_MAX_SB_DY || sdx < 0 || sdx >= FAR_MAX_SB_DX || pdy < 0
30.          || pdy >= FAR_MAX_BC_DY) {
32.          return block_col;
33.      }
34.      if ((offset = far_dy[sdy]->far_q[sdx][pdy]) == OFF_TABLE)
35.          offset = -1;
36.      if (side == FROM_RIGHT)
37.          return block_col + offset;
38.
39.      return block_col - offset;
40.  }
41.

### right_side

1.  /*
2.   * right_side()
3.   *
4.   * Figure out what could be seen on the right side of the source.
5.   */
6.  STATIC_OVL void
7.  right_side(row, cb_row, cb_col, fb_row, fb_col, left, right_mark, limits)
8.  int row;            /* current row */
9.  int cb_row, cb_col; /* close block row and col */
10.  int fb_row, fb_col; /* far block row and col */
11.  int left;           /* left mark of the previous row */
12.  int right_mark;     /* right mark of previous row */
13.  char *limits;       /* points at range limit for current row, or NULL */
14.  {
15.      register int i;
16.      register char *rowp = NULL;
17.      int hit_stone = 0;
19.      int lblock_col; /* local block column (current row) */
20.      int nrow, deeper;
21.      char *row_min = NULL; /* left most */
22.      char *row_max = NULL; /* right most */
23.      int lim_max;          /* right most limit of circle */
24.
25.      nrow = row + step;
26.      deeper = good_row(nrow) && (!limits || (*limits >= *(limits + 1)));
27.      if (!vis_func) {
28.          rowp = cs_rows[row];
29.          row_min = &cs_left[row];
30.          row_max = &cs_right[row];
31.      }
32.      if (limits) {
33.          lim_max = start_col + *limits;
34.          if (lim_max > COLNO - 1)
35.              lim_max = COLNO - 1;
36.          if (right_mark > lim_max)
37.              right_mark = lim_max;
38.          limits++; /* prepare for next row */
39.      } else
40.          lim_max = COLNO - 1;
41.
42.      /*
43.       * Get the left shadow from the close block.  This value could be
44.       * illegal.
45.       */
47.
48.      /*
49.       * Mark all stone walls as seen before the left shadow.  All this work
50.       * for a special case.
51.       *
52.       * NOTE.  With the addition of this code in here, it is now *required*
53.       * for the algorithm to work correctly.  If this is commented out,
54.       * change the above assignment so that left and not left_shadow is the
55.       * variable that gets the shadow.
56.       */
57.      while (left <= right_mark) {
58.          loc_right = right_ptrs[row][left];
59.          if (loc_right > lim_max)
60.              loc_right = lim_max;
61.          if (viz_clear_rows[row][left]) {
62.              if (loc_right >= left_shadow) {
64.                  break;
65.              }
66.              left = loc_right;
67.              loc_right = right_ptrs[row][left];
68.              if (loc_right > lim_max)
69.                  loc_right = lim_max;
70.              if (left == loc_right)
71.                  return; /* boundary */
72.
73.              /* Shadow covers opening, beyond right mark */
74.              if (left == right_mark && left_shadow > right_mark)
75.                  return;
76.          }
77.
78.          if (loc_right > right_mark) /* can't see stone beyond the mark */
79.              loc_right = right_mark;
80.
81.          if (vis_func) {
82.              for (i = left; i <= loc_right; i++)
83.                  (*vis_func)(i, row, varg);
84.          } else {
85.              for (i = left; i <= loc_right; i++)
86.                  set_cs(rowp, i);
87.              set_min(left);
88.              set_max(loc_right);
89.          }
90.
91.          if (loc_right == right_mark)
92.              return; /* all stone */
94.              hit_stone = 1;
95.          left = loc_right + 1;
96.      }
97.
98.      /*
99.       * At this point we are at the first visible clear spot on or beyond
100.       * the left shadow, unless the left shadow is an illegal value.  If we
101.       * have "hit stone" then we have a stone wall just to our left.
102.       */
103.
104.      /*
105.       * Get the right shadow.  Make sure that it is a legal value.
106.       */
108.          right_shadow = COLNO - 1;
109.      /*
110.       * Make vertical walls work the way we want them.  In this case, we
111.       * note when the close block blocks the column just above/beneath
113.       * the location is filled, then we want to see it, so we put the
114.       * right shadow back (same as fb_col).
115.       */
116.      if (right_shadow < fb_col && !viz_clear_rows[row][fb_col])
120.
121.      /*
122.       * Main loop.  Within the range of sight of the previous row, mark all
123.       * stone walls as seen.  Follow open areas recursively.
124.       */
125.      while (left <= right_mark) {
126.          /* Get the far right of the opening or wall */
127.          loc_right = right_ptrs[row][left];
128.          if (loc_right > lim_max)
129.              loc_right = lim_max;
130.
131.          if (!viz_clear_rows[row][left]) {
132.              hit_stone = 1; /* use stone on this row as close block */
133.              /*
134.               * We can see all of the wall until the next open spot or the
135.               * start of the shadow caused by the far block (right).
136.               *
137.               * Can't see stone beyond the right mark.
138.               */
139.              if (loc_right > right_mark)
140.                  loc_right = right_mark;
141.
142.              if (vis_func) {
143.                  for (i = left; i <= loc_right; i++)
144.                      (*vis_func)(i, row, varg);
145.              } else {
146.                  for (i = left; i <= loc_right; i++)
147.                      set_cs(rowp, i);
148.                  set_min(left);
149.                  set_max(loc_right);
150.              }
151.
152.              if (loc_right == right_mark)
153.                  return; /* hit the end */
154.              left = loc_right + 1;
155.              loc_right = right_ptrs[row][left];
156.              if (loc_right > lim_max)
157.                  loc_right = lim_max;
158.              /* fall through... we know at least one position is visible */
159.          }
160.
161.          /*
162.           * We are in an opening.
163.           *
164.           * If this is the first open spot since the could see area  (this is
165.           * true if we have hit stone), get the shadow generated by the wall
166.           * just to our left.
167.           */
168.          if (hit_stone) {
169.              lblock_col = left - 1; /* local block column */
170.              left = close_shadow(FROM_RIGHT, row, row, lblock_col);
171.              if (left > lim_max)
172.                  break; /* off the end */
173.          }
174.
175.          /*
176.           * Check if the shadow covers the opening.  If it does, then
177.           * move to end of the opening.  A shadow generated on from a
178.           * wall on this row does *not* cover the wall on the right
179.           * of the opening.
180.           */
181.          if (left >= loc_right) {
182.              if (loc_right == lim_max) { /* boundary */
183.                  if (left == lim_max) {
184.                      if (vis_func)
185.                          (*vis_func)(lim_max, row, varg);
186.                      else {
187.                          set_cs(rowp, lim_max); /* last pos */
188.                          set_max(lim_max);
189.                      }
190.                  }
191.                  return; /* done */
192.              }
193.              left = loc_right;
194.              continue;
195.          }
196.
197.          /*
198.           * If the far wall of the opening (loc_right) is closer than the
199.           * shadow limit imposed by the far block (right) then use the far
200.           * wall as our new far block when we recurse.
201.           *
202.           * If the limits are the the same, and the far block really exists
203.           * (fb_row >= 0) then do the same as above.
204.           *
205.           * Normally, the check would be for the far wall being closer OR EQUAL
206.           * to the shadow limit.  However, there is a bug that arises from the
207.           * fact that the clear area pointers end in an open space (if it
208.           * exists) on a boundary.  This then makes a far block exist where it
209.           * shouldn't --- on a boundary.  To get around that, I had to
210.           * introduce the concept of a non-existent far block (when the
211.           * row < 0).  Next I have to check for it.  Here is where that check
212.           * exists.
213.           */
215.              || (fb_row >= 0 && loc_right == right_shadow)) {
216.              if (vis_func) {
217.                  for (i = left; i <= loc_right; i++)
218.                      (*vis_func)(i, row, varg);
219.              } else {
220.                  for (i = left; i <= loc_right; i++)
221.                      set_cs(rowp, i);
222.                  set_min(left);
223.                  set_max(loc_right);
224.              }
225.
226.              if (deeper) {
227.                  if (hit_stone)
228.                      right_side(nrow, row, lblock_col, row, loc_right, left,
229.                                 loc_right, limits);
230.                  else
231.                      right_side(nrow, cb_row, cb_col, row, loc_right, left,
232.                                 loc_right, limits);
233.              }
234.
235.              /*
236.               * The following line, setting hit_stone, is needed for those
237.               * walls that are only 1 wide.  If hit stone is *not* set and
238.               * the stone is only one wide, then the close block is the old
239.               * one instead one on the current row.  A way around having to
240.               * set it here is to make left = loc_right (not loc_right+1) and
241.               * let the outer loop take care of it.  However, if we do that
242.               * then we then have to check for boundary conditions here as
243.               * well.
244.               */
245.              hit_stone = 1;
246.
247.              left = loc_right + 1;
248.
249.          /*
250.           * The opening extends beyond the right mark.  This means that
251.           * the next far block is the current far block.
252.           */
253.          } else {
254.              if (vis_func) {
255.                  for (i = left; i <= right_shadow; i++)
256.                      (*vis_func)(i, row, varg);
257.              } else {
258.                  for (i = left; i <= right_shadow; i++)
259.                      set_cs(rowp, i);
260.                  set_min(left);
262.              }
263.
264.              if (deeper) {
265.                  if (hit_stone)
266.                      right_side(nrow, row, lblock_col, fb_row, fb_col, left,
268.                  else
269.                      right_side(nrow, cb_row, cb_col, fb_row, fb_col, left,
271.              }
272.
273.              return; /* we're outta here */
274.          }
275.      }
276.  }
277.

### left_side

1.  /*
2.   * left_side()
3.   *
4.   * This routine is the mirror image of right_side().  Please see right_side()
5.   * for blow by blow comments.
6.   */
7.  STATIC_OVL void
8.  left_side(row, cb_row, cb_col, fb_row, fb_col, left_mark, right, limits)
9.  int row;            /* the current row */
10.  int cb_row, cb_col; /* close block row and col */
11.  int fb_row, fb_col; /* far block row and col */
12.  int left_mark;      /* left mark of previous row */
13.  int right;          /* right mark of the previous row */
14.  char *limits;
15.  {
16.      register int i;
17.      register char *rowp = NULL;
18.      int hit_stone = 0;
20.      int lblock_col; /* local block column (current row) */
21.      int nrow, deeper;
22.      char *row_min = NULL; /* left most */
23.      char *row_max = NULL; /* right most */
24.      int lim_min;
25.
26.      nrow = row + step;
27.      deeper = good_row(nrow) && (!limits || (*limits >= *(limits + 1)));
28.      if (!vis_func) {
29.          rowp = cs_rows[row];
30.          row_min = &cs_left[row];
31.          row_max = &cs_right[row];
32.      }
33.      if (limits) {
34.          lim_min = start_col - *limits;
35.          if (lim_min < 0)
36.              lim_min = 0;
37.          if (left_mark < lim_min)
38.              left_mark = lim_min;
39.          limits++; /* prepare for next row */
40.      } else
41.          lim_min = 0;
42.
43.      /* This value could be illegal. */
45.
46.      while (right >= left_mark) {
47.          loc_left = left_ptrs[row][right];
48.          if (loc_left < lim_min)
49.              loc_left = lim_min;
50.          if (viz_clear_rows[row][right]) {
51.              if (loc_left <= right_shadow) {
53.                  break;
54.              }
55.              right = loc_left;
56.              loc_left = left_ptrs[row][right];
57.              if (loc_left < lim_min)
58.                  loc_left = lim_min;
59.              if (right == loc_left)
60.                  return; /* boundary */
61.          }
62.
63.          if (loc_left < left_mark) /* can't see beyond the left mark */
64.              loc_left = left_mark;
65.
66.          if (vis_func) {
67.              for (i = loc_left; i <= right; i++)
68.                  (*vis_func)(i, row, varg);
69.          } else {
70.              for (i = loc_left; i <= right; i++)
71.                  set_cs(rowp, i);
72.              set_min(loc_left);
73.              set_max(right);
74.          }
75.
76.          if (loc_left == left_mark)
77.              return; /* all stone */
79.              hit_stone = 1;
80.          right = loc_left - 1;
81.      }
82.
83.      /* At first visible clear spot on or beyond the right shadow. */
84.
87.
88.      /* Do vertical walls as we want. */
89.      if (left_shadow > fb_col && !viz_clear_rows[row][fb_col])
93.
94.      while (right >= left_mark) {
95.          loc_left = left_ptrs[row][right];
96.
97.          if (!viz_clear_rows[row][right]) {
98.              hit_stone = 1; /* use stone on this row as close block */
99.
100.              /* We can only see walls until the left mark */
101.              if (loc_left < left_mark)
102.                  loc_left = left_mark;
103.
104.              if (vis_func) {
105.                  for (i = loc_left; i <= right; i++)
106.                      (*vis_func)(i, row, varg);
107.              } else {
108.                  for (i = loc_left; i <= right; i++)
109.                      set_cs(rowp, i);
110.                  set_min(loc_left);
111.                  set_max(right);
112.              }
113.
114.              if (loc_left == left_mark)
115.                  return; /* hit end */
116.              right = loc_left - 1;
117.              loc_left = left_ptrs[row][right];
118.              if (loc_left < lim_min)
119.                  loc_left = lim_min;
120.              /* fall through...*/
121.          }
122.
123.          /* We are in an opening. */
124.          if (hit_stone) {
125.              lblock_col = right + 1; /* stone block (local) */
126.              right = close_shadow(FROM_LEFT, row, row, lblock_col);
127.              if (right < lim_min)
128.                  return; /* off the end */
129.          }
130.
131.          /*  Check if the shadow covers the opening. */
132.          if (right <= loc_left) {
133.              /*  Make a boundary condition work. */
134.              if (loc_left == lim_min) { /* at boundary */
135.                  if (right == lim_min) {
136.                      if (vis_func)
137.                          (*vis_func)(lim_min, row, varg);
138.                      else {
139.                          set_cs(rowp, lim_min); /* caught the last pos */
140.                          set_min(lim_min);
141.                      }
142.                  }
143.                  return; /* and break out the loop */
144.              }
145.
146.              right = loc_left;
147.              continue;
148.          }
149.
150.          /* If the far wall of the opening is closer than the shadow limit. */
152.              || (fb_row >= 0 && loc_left == left_shadow)) {
153.              if (vis_func) {
154.                  for (i = loc_left; i <= right; i++)
155.                      (*vis_func)(i, row, varg);
156.              } else {
157.                  for (i = loc_left; i <= right; i++)
158.                      set_cs(rowp, i);
159.                  set_min(loc_left);
160.                  set_max(right);
161.              }
162.
163.              if (deeper) {
164.                  if (hit_stone)
165.                      left_side(nrow, row, lblock_col, row, loc_left, loc_left,
166.                                right, limits);
167.                  else
168.                      left_side(nrow, cb_row, cb_col, row, loc_left, loc_left,
169.                                right, limits);
170.              }
171.
172.              hit_stone = 1; /* needed for walls of width 1 */
173.              right = loc_left - 1;
174.
175.          /*  The opening extends beyond the left mark. */
176.          } else {
177.              if (vis_func) {
178.                  for (i = left_shadow; i <= right; i++)
179.                      (*vis_func)(i, row, varg);
180.              } else {
181.                  for (i = left_shadow; i <= right; i++)
182.                      set_cs(rowp, i);
184.                  set_max(right);
185.              }
186.
187.              if (deeper) {
188.                  if (hit_stone)
189.                      left_side(nrow, row, lblock_col, fb_row, fb_col,
191.                  else
192.                      left_side(nrow, cb_row, cb_col, fb_row, fb_col,
194.              }
195.
196.              return; /* we're outta here */
197.          }
198.      }
199.  }
200.

### view_from

1.  /*
2.   * view_from
3.   *
4.   * Calculate a view from the given location.  Initialize and fill a
5.   * ROWNOxCOLNO array (could_see) with all the locations that could be
6.   * seen from the source location.  Initialize and fill the left most
7.   * and right most boundaries of what could be seen.
8.   */
9.  STATIC_OVL void
10.  view_from(srow, scol, loc_cs_rows, left_most, right_most, range, func, arg)
11.  int srow, scol;               /* source row and column */
12.  char **loc_cs_rows;           /* could_see array (row pointers) */
13.  char *left_most, *right_most; /* limits of what could be seen */
14.  int range;                    /* 0 if unlimited */
15.  void FDECL((*func), (int, int, genericptr_t));
16.  genericptr_t arg;
17.  {
18.      register int i;
19.      char *rowp;
20.      int nrow, left, right, left_row, right_row;
21.      char *limits;
22.
24.      start_col = scol;
25.      start_row = srow;
26.      cs_rows = loc_cs_rows;
27.      cs_left = left_most;
28.      cs_right = right_most;
29.      vis_func = func;
30.      varg = arg;
31.
32.      /*  Find the left and right limits of sight on the starting row. */
33.      if (viz_clear_rows[srow][scol]) {
34.          left = left_ptrs[srow][scol];
35.          right = right_ptrs[srow][scol];
36.      } else {
37.          left = (!scol) ? 0 : (viz_clear_rows[srow][scol - 1]
38.                                    ? left_ptrs[srow][scol - 1]
39.                                    : scol - 1);
40.          right = (scol == COLNO - 1) ? COLNO - 1
41.                                      : (viz_clear_rows[srow][scol + 1]
42.                                             ? right_ptrs[srow][scol + 1]
43.                                             : scol + 1);
44.      }
45.
46.      if (range) {
47.          if (range > MAX_RADIUS || range < 1)
48.              panic("view_from called with range %d", range);
49.          limits = circle_ptr(range) + 1; /* start at next row */
50.          if (left < scol - range)
51.              left = scol - range;
52.          if (right > scol + range)
53.              right = scol + range;
54.      } else
55.          limits = (char *) 0;
56.
57.      if (func) {
58.          for (i = left; i <= right; i++)
59.              (*func)(i, srow, arg);
60.      } else {
61.          /* Row optimization */
62.          rowp = cs_rows[srow];
63.
64.          /* We know that we can see our row. */
65.          for (i = left; i <= right; i++)
66.              set_cs(rowp, i);
67.          cs_left[srow] = left;
68.          cs_right[srow] = right;
69.      }
70.
71.      /* The far block has a row number of -1 if we are on an edge. */
72.      right_row = (right == COLNO - 1) ? -1 : srow;
73.      left_row = (!left) ? -1 : srow;
74.
75.      /*
76.       *  Check what could be seen in quadrants.
77.       */
78.      if ((nrow = srow + 1) < ROWNO) {
79.          step = 1; /* move down */
80.          if (scol < COLNO - 1)
81.              right_side(nrow, -1, scol, right_row, right, scol, right, limits);
82.          if (scol)
83.              left_side(nrow, -1, scol, left_row, left, left, scol, limits);
84.      }
85.
86.      if ((nrow = srow - 1) >= 0) {
87.          step = -1; /* move up */
88.          if (scol < COLNO - 1)
89.              right_side(nrow, -1, scol, right_row, right, scol, right, limits);
90.          if (scol)
91.              left_side(nrow, -1, scol, left_row, left, left, scol, limits);
92.      }
93.  }
94.
95.  #else /*===== End of algorithm D =====*/
96.

## General Line of Sight: Algorithm C

1.  /*==========================================================================*\
2.                              GENERAL LINE OF SIGHT
3.                                  Algorithm C
4.  \*==========================================================================*/
5.
6.  /*
7.   * Defines local to Algorithm C.
8.   */
9.  STATIC_DCL void FDECL(right_side, (int, int, int, char *));
10.  STATIC_DCL void FDECL(left_side, (int, int, int, char *));
11.
12.  /* Initialize algorithm C (nothing). */
13.  STATIC_OVL void
14.  view_init()
15.  {
16.  }
17.

### right_side

1.  /*
2.   * Mark positions as visible on one quadrant of the right side.  The
3.   * quadrant is determined by the value of the global variable step.
4.   */
5.  STATIC_OVL void
6.  right_side(row, left, right_mark, limits)
7.  int row;        /* current row */
8.  int left;       /* first (left side) visible spot on prev row */
9.  int right_mark; /* last (right side) visible spot on prev row */
10.  char *limits;   /* points at range limit for current row, or NULL */
11.  {
12.      int right;                  /* right limit of "could see" */
13.      int right_edge;             /* right edge of an opening */
14.      int nrow;                   /* new row (calculate once) */
15.      int deeper;                 /* if TRUE, call self as needed */
16.      int result;                 /* set by q?_path() */
17.      register int i;             /* loop counter */
18.      register char *rowp = NULL; /* row optimization */
19.      char *row_min = NULL;       /* left most  [used by macro set_min()] */
20.      char *row_max = NULL;       /* right most [used by macro set_max()] */
21.      int lim_max;                /* right most limit of circle */
22.
23.      nrow = row + step;
24.      /*
25.       * Can go deeper if the row is in bounds and the next row is within
26.       * the circle's limit.  We tell the latter by checking to see if the next
27.       * limit value is the start of a new circle radius (meaning we depend
28.       * on the structure of circle_data[]).
29.       */
30.      deeper = good_row(nrow) && (!limits || (*limits >= *(limits + 1)));
31.      if (!vis_func) {
32.          rowp = cs_rows[row]; /* optimization */
33.          row_min = &cs_left[row];
34.          row_max = &cs_right[row];
35.      }
36.      if (limits) {
37.          lim_max = start_col + *limits;
38.          if (lim_max > COLNO - 1)
39.              lim_max = COLNO - 1;
40.          if (right_mark > lim_max)
41.              right_mark = lim_max;
42.          limits++; /* prepare for next row */
43.      } else
44.          lim_max = COLNO - 1;
45.
46.      while (left <= right_mark) {
47.          right_edge = right_ptrs[row][left];
48.          if (right_edge > lim_max)
49.              right_edge = lim_max;
50.
51.          if (!is_clear(row, left)) {
52.              /*
53.               * Jump to the far side of a stone wall.  We can set all
54.               * the points in between as seen.
55.               *
56.               * If the right edge goes beyond the right mark, check to see
57.               * how much we can see.
58.               */
59.              if (right_edge > right_mark) {
60.                  /*
61.                   * If the mark on the previous row was a clear position,
62.                   * the odds are that we can actually see part of the wall
63.                   * beyond the mark on this row.  If so, then see one beyond
64.                   * the mark.  Otherwise don't.  This is a kludge so corners
65.                   * with an adjacent doorway show up in nethack.
66.                   */
67.                  right_edge = is_clear(row - step, right_mark) ? right_mark + 1
68.                                                                : right_mark;
69.              }
70.              if (vis_func) {
71.                  for (i = left; i <= right_edge; i++)
72.                      (*vis_func)(i, row, varg);
73.              } else {
74.                  for (i = left; i <= right_edge; i++)
75.                      set_cs(rowp, i);
76.                  set_min(left);
77.                  set_max(right_edge);
78.              }
79.              left = right_edge + 1; /* no limit check necessary */
80.              continue;
81.          }
82.
83.          /* No checking needed if our left side is the start column. */
84.          if (left != start_col) {
85.              /*
86.               * Find the left side.  Move right until we can see it or we run
87.               * into a wall.
88.               */
89.              for (; left <= right_edge; left++) {
90.                  if (step < 0) {
91.                      q1_path(start_row, start_col, row, left, rside1);
92.                  } else {
93.                      q4_path(start_row, start_col, row, left, rside1);
94.                  }
95.              rside1: /* used if q?_path() is a macro */
96.                  if (result)
97.                      break;
98.              }
99.
100.              /*
101.               * Check for boundary conditions.  We *need* check (2) to break
102.               * an infinite loop where:
103.               *
104.               *           left == right_edge == right_mark == lim_max.
105.               *
106.               */
107.              if (left > lim_max)
108.                  return;            /* check (1) */
109.              if (left == lim_max) { /* check (2) */
110.                  if (vis_func) {
111.                      (*vis_func)(lim_max, row, varg);
112.                  } else {
113.                      set_cs(rowp, lim_max);
114.                      set_max(lim_max);
115.                  }
116.                  return;
117.              }
118.              /*
119.               * Check if we can see any spots in the opening.  We might
120.               * (left == right_edge) or might not (left == right_edge+1) have
121.               * been able to see the far wall.  Make sure we *can* see the
122.               * wall (remember, we can see the spot above/below this one)
123.               * by backing up.
124.               */
125.              if (left >= right_edge) {
126.                  left = right_edge; /* for the case left == right_edge+1 */
127.                  continue;
128.              }
129.          }
130.
131.          /*
132.           * Find the right side.  If the marker from the previous row is
133.           * closer than the edge on this row, then we have to check
134.           * how far we can see around the corner (under the overhang).  Stop
135.           * at the first non-visible spot or we actually hit the far wall.
136.           *
137.           * Otherwise, we know we can see the right edge of the current row.
138.           *
139.           * This must be a strict less than so that we can always see a
140.           * horizontal wall, even if it is adjacent to us.
141.           */
142.          if (right_mark < right_edge) {
143.              for (right = right_mark; right <= right_edge; right++) {
144.                  if (step < 0) {
145.                      q1_path(start_row, start_col, row, right, rside2);
146.                  } else {
147.                      q4_path(start_row, start_col, row, right, rside2);
148.                  }
149.              rside2: /* used if q?_path() is a macro */
150.                  if (!result)
151.                      break;
152.              }
153.              --right; /* get rid of the last increment */
154.          } else
155.              right = right_edge;
156.
157.          /*
158.           * We have the range that we want.  Set the bits.  Note that
159.           * there is no else --- we no longer handle splinters.
160.           */
161.          if (left <= right) {
162.              /*
163.               * An ugly special case.  If you are adjacent to a vertical wall
164.               * and it has a break in it, then the right mark is set to be
165.               * start_col.  We *want* to be able to see adjacent vertical
166.               * walls, so we have to set it back.
167.               */
168.              if (left == right && left == start_col && start_col < (COLNO - 1)
169.                  && !is_clear(row, start_col + 1))
170.                  right = start_col + 1;
171.
172.              if (right > lim_max)
173.                  right = lim_max;
174.              /* set the bits */
175.              if (vis_func) {
176.                  for (i = left; i <= right; i++)
177.                      (*vis_func)(i, row, varg);
178.              } else {
179.                  for (i = left; i <= right; i++)
180.                      set_cs(rowp, i);
181.                  set_min(left);
182.                  set_max(right);
183.              }
184.
185.              /* recursive call for next finger of light */
186.              if (deeper)
187.                  right_side(nrow, left, right, limits);
188.              left = right + 1; /* no limit check necessary */
189.          }
190.      }
191.  }
192.

### left_side

1.  /*
2.   * This routine is the mirror image of right_side().  See right_side() for
4.   */
5.  STATIC_OVL void
6.  left_side(row, left_mark, right, limits)
7.  int row, left_mark, right;
8.  char *limits;
9.  {
10.      int left, left_edge, nrow, deeper, result;
11.      register int i;
12.      register char *rowp = NULL;
13.      char *row_min = NULL;
14.      char *row_max = NULL;
15.      int lim_min;
16.
17.  #ifdef GCC_WARN
18.      rowp = row_min = row_max = 0;
19.  #endif
20.      nrow = row + step;
21.      deeper = good_row(nrow) && (!limits || (*limits >= *(limits + 1)));
22.      if (!vis_func) {
23.          rowp = cs_rows[row];
24.          row_min = &cs_left[row];
25.          row_max = &cs_right[row];
26.      }
27.      if (limits) {
28.          lim_min = start_col - *limits;
29.          if (lim_min < 0)
30.              lim_min = 0;
31.          if (left_mark < lim_min)
32.              left_mark = lim_min;
33.          limits++; /* prepare for next row */
34.      } else
35.          lim_min = 0;
36.
37.      while (right >= left_mark) {
38.          left_edge = left_ptrs[row][right];
39.          if (left_edge < lim_min)
40.              left_edge = lim_min;
41.
42.          if (!is_clear(row, right)) {
43.              /* Jump to the far side of a stone wall. */
44.              if (left_edge < left_mark) {
45.                  /* Maybe see more (kludge). */
46.                  left_edge = is_clear(row - step, left_mark) ? left_mark - 1
47.                                                              : left_mark;
48.              }
49.              if (vis_func) {
50.                  for (i = left_edge; i <= right; i++)
51.                      (*vis_func)(i, row, varg);
52.              } else {
53.                  for (i = left_edge; i <= right; i++)
54.                      set_cs(rowp, i);
55.                  set_min(left_edge);
56.                  set_max(right);
57.              }
58.              right = left_edge - 1; /* no limit check necessary */
59.              continue;
60.          }
61.
62.          if (right != start_col) {
63.              /* Find the right side. */
64.              for (; right >= left_edge; right--) {
65.                  if (step < 0) {
66.                      q2_path(start_row, start_col, row, right, lside1);
67.                  } else {
68.                      q3_path(start_row, start_col, row, right, lside1);
69.                  }
70.              lside1: /* used if q?_path() is a macro */
71.                  if (result)
72.                      break;
73.              }
74.
75.              /* Check for boundary conditions. */
76.              if (right < lim_min)
77.                  return;
78.              if (right == lim_min) {
79.                  if (vis_func) {
80.                      (*vis_func)(lim_min, row, varg);
81.                  } else {
82.                      set_cs(rowp, lim_min);
83.                      set_min(lim_min);
84.                  }
85.                  return;
86.              }
87.              /* Check if we can see any spots in the opening. */
88.              if (right <= left_edge) {
89.                  right = left_edge;
90.                  continue;
91.              }
92.          }
93.
94.          /* Find the left side. */
95.          if (left_mark > left_edge) {
96.              for (left = left_mark; left >= left_edge; --left) {
97.                  if (step < 0) {
98.                      q2_path(start_row, start_col, row, left, lside2);
99.                  } else {
100.                      q3_path(start_row, start_col, row, left, lside2);
101.                  }
102.              lside2: /* used if q?_path() is a macro */
103.                  if (!result)
104.                      break;
105.              }
106.              left++; /* get rid of the last decrement */
107.          } else
108.              left = left_edge;
109.
110.          if (left <= right) {
111.              /* An ugly special case. */
112.              if (left == right && right == start_col && start_col > 0
113.                  && !is_clear(row, start_col - 1))
114.                  left = start_col - 1;
115.
116.              if (left < lim_min)
117.                  left = lim_min;
118.              if (vis_func) {
119.                  for (i = left; i <= right; i++)
120.                      (*vis_func)(i, row, varg);
121.              } else {
122.                  for (i = left; i <= right; i++)
123.                      set_cs(rowp, i);
124.                  set_min(left);
125.                  set_max(right);
126.              }
127.
128.              /* Recurse */
129.              if (deeper)
130.                  left_side(nrow, left, right, limits);
131.              right = left - 1; /* no limit check necessary */
132.          }
133.      }
134.  }
135.

### view_from

1.  /*
2.   * Calculate all possible visible locations from the given location
3.   * (srow,scol).  NOTE this is (y,x)!  Mark the visible locations in the
4.   * array provided.
5.   */
6.  STATIC_OVL void
7.  view_from(srow, scol, loc_cs_rows, left_most, right_most, range, func, arg)
8.  int srow, scol;     /* starting row and column */
9.  char **loc_cs_rows; /* pointers to the rows of the could_see array */
10.  char *left_most;    /* min mark on each row */
11.  char *right_most;   /* max mark on each row */
12.  int range;          /* 0 if unlimited */
13.  void FDECL((*func), (int, int, genericptr_t));
14.  genericptr_t arg;
15.  {
16.      register int i; /* loop counter */
17.      char *rowp;     /* optimization for setting could_see */
18.      int nrow;       /* the next row */
19.      int left;       /* the left-most visible column */
20.      int right;      /* the right-most visible column */
21.      char *limits;   /* range limit for next row */
22.
23.      /* Set globals for q?_path(), left_side(), and right_side() to use. */
24.      start_col = scol;
25.      start_row = srow;
26.      cs_rows = loc_cs_rows; /* 'could see' rows */
27.      cs_left = left_most;
28.      cs_right = right_most;
29.      vis_func = func;
30.      varg = arg;
31.
32.      /*
33.       * Determine extent of sight on the starting row.
34.       */
35.      if (is_clear(srow, scol)) {
36.          left = left_ptrs[srow][scol];
37.          right = right_ptrs[srow][scol];
38.      } else {
39.          /*
40.           * When in stone, you can only see your adjacent squares, unless
41.           * you are on an array boundary or a stone/clear boundary.
42.           */
43.          left = (!scol) ? 0
44.                         : (is_clear(srow, scol - 1) ? left_ptrs[srow][scol - 1]
45.                                                     : scol - 1);
46.          right = (scol == COLNO - 1)
47.                      ? COLNO - 1
48.                      : (is_clear(srow, scol + 1) ? right_ptrs[srow][scol + 1]
49.                                                  : scol + 1);
50.      }
51.
52.      if (range) {
53.          if (range > MAX_RADIUS || range < 1)
54.              panic("view_from called with range %d", range);
55.          limits = circle_ptr(range) + 1; /* start at next row */
56.          if (left < scol - range)
57.              left = scol - range;
58.          if (right > scol + range)
59.              right = scol + range;
60.      } else
61.          limits = (char *) 0;
62.
63.      if (func) {
64.          for (i = left; i <= right; i++)
65.              (*func)(i, srow, arg);
66.      } else {
67.          /* Row pointer optimization. */
68.          rowp = cs_rows[srow];
69.
70.          /* We know that we can see our row. */
71.          for (i = left; i <= right; i++)
72.              set_cs(rowp, i);
73.          cs_left[srow] = left;
74.          cs_right[srow] = right;
75.      }
76.
77.      /*
78.       * Check what could be seen in quadrants.  We need to check for valid
79.       * rows here, since we don't do it in the routines right_side() and
80.       * left_side() [ugliness to remove extra routine calls].
81.       */
82.      if ((nrow = srow + 1) < ROWNO) { /* move down */
83.          step = 1;
84.          if (scol < COLNO - 1)
85.              right_side(nrow, scol, right, limits);
86.          if (scol)
87.              left_side(nrow, left, scol, limits);
88.      }
89.
90.      if ((nrow = srow - 1) >= 0) { /* move up */
91.          step = -1;
92.          if (scol < COLNO - 1)
93.              right_side(nrow, scol, right, limits);
94.          if (scol)
95.              left_side(nrow, left, scol, limits);
96.      }
97.  }
98.
99.  #endif /*===== End of algorithm C =====*/
100.

## Area of Effect "Engine"

1.  /*
2.   * AREA OF EFFECT "ENGINE"
3.   *
4.   * Calculate all possible visible locations as viewed from the given location
5.   * (srow,scol) within the range specified. Perform "func" with (x, y) args and
6.   * additional argument "arg" for each square.
7.   *
8.   * If not centered on the hero, just forward arguments to view_from(); it
9.   * will call "func" when necessary.  If the hero is the center, use the
10.   * vision matrix and reduce extra work.
11.   */

### do_clear_area

1.  void
2.  do_clear_area(scol, srow, range, func, arg)
3.  int scol, srow, range;
4.  void FDECL((*func), (int, int, genericptr_t));
5.  genericptr_t arg;
6.  {
7.      /* If not centered on hero, do the hard work of figuring the area */
8.      if (scol != u.ux || srow != u.uy) {
9.          view_from(srow, scol, (char **) 0, (char *) 0, (char *) 0, range,
10.                    func, arg);
11.      } else {
12.          register int x;
13.          int y, min_x, max_x, max_y, offset;
14.          char *limits;
15.          boolean override_vision;
16.
17.          /* vision doesn't pass through water or clouds, detection should
18.             [this probably ought to be an arg supplied by our caller...] */
19.          override_vision =
20.              (Is_waterlevel(&u.uz) || Is_airlevel(&u.uz)) && detecting(func);
21.
22.          if (range > MAX_RADIUS || range < 1)
23.              panic("do_clear_area:  illegal range %d", range);
24.          if (vision_full_recalc)
25.              vision_recalc(0); /* recalc vision if dirty */
26.          limits = circle_ptr(range);
27.          if ((max_y = (srow + range)) >= ROWNO)
28.              max_y = ROWNO - 1;
29.          if ((y = (srow - range)) < 0)
30.              y = 0;
31.          for (; y <= max_y; y++) {
32.              offset = limits[v_abs(y - srow)];
33.              if ((min_x = (scol - offset)) < 0)
34.                  min_x = 0;
35.              if ((max_x = (scol + offset)) >= COLNO)
36.                  max_x = COLNO - 1;
37.              for (x = min_x; x <= max_x; x++)
38.                  if (couldsee(x, y) || override_vision)
39.                      (*func)(x, y, arg);
40.          }
41.      }
42.  }
43.

### howmonseen

1.  /* bitmask indicating ways mon is seen; extracted from lookat(pager.c) */
2.  unsigned
3.  howmonseen(mon)
4.  struct monst *mon;
5.  {
6.      boolean useemon = (boolean) canseemon(mon);
7.      int xraydist = (u.xray_range < 0) ? -1 : (u.xray_range * u.xray_range);
8.      unsigned how_seen = 0; /* result */
9.
10.      /* normal vision;
11.         cansee is true for both normal and astral vision,
12.         but couldsee it not true for astral vision */
13.      if ((mon->wormno ? worm_known(mon) : (cansee(mon->mx, mon->my)
14.                                            && couldsee(mon->mx, mon->my)))
15.          && mon_visible(mon) && !mon->minvis)
16.          how_seen |= MONSEEN_NORMAL;
17.      /* see invisible */
18.      if (useemon && mon->minvis)
19.          how_seen |= MONSEEN_SEEINVIS;
20.      /* infravision */
21.      if ((!mon->minvis || See_invisible) && see_with_infrared(mon))
22.          how_seen |= MONSEEN_INFRAVIS;
23.      /* telepathy */
24.      if (tp_sensemon(mon))
25.          how_seen |= MONSEEN_TELEPAT;
26.      /* xray */
27.      if (useemon && xraydist > 0 && distu(mon->mx, mon->my) <= xraydist)
28.          how_seen |= MONSEEN_XRAYVIS;
29.      /* extended detection */
30.      if (Detect_monsters)
31.          how_seen |= MONSEEN_DETECT;
32.      /* class-/type-specific warning */
33.      if (MATCH_WARN_OF_MON(mon))
34.          how_seen |= MONSEEN_WARNMON;
35.
36.      return how_seen;
37.  }
38.
39.  /*vision.c*/