# Source:NetHack 3.3.0/vision.c

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

Warning! This is the source code from an old release. For the latest release, see Source code

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.

```1.    /*	SCCS Id: @(#)vision.c	3.3	99/02/18	*/
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.
7.    /* Circles ==================================================================*/
8.
9.    /*
10.    * These numbers are limit offsets for one quadrant of a circle of a given
11.    * radius (the first number of each line) from the source.  The number in
12.    * the comment is the element number (so pointers can be set up).  Each
13.    * "circle" has as many elements as its radius+1.  The radius is the number
14.    * of points away from the source that the limit exists.  The radius of the
15.    * offset on the same row as the source *is* included so we don't have to
16.    * make an extra check.  For example, a circle of radius 4 has offsets:
17.    *
18.    *				XXX	+2
19.    *				...X	+3
20.    *				....X	+4
21.    *				....X	+4
22.    *				@...X   +4
23.    *
24.    */
25.   char circle_data[] = {
26.   /*  0*/	 1, 1,
27.   /*  2*/	 2, 2, 1,
28.   /*  5*/	 3, 3, 2, 1,
29.   /*  9*/	 4, 4, 4, 3, 2,
30.   /* 14*/	 5, 5, 5, 4, 3, 2,
31.   /* 20*/	 6, 6, 6, 5, 5, 4, 2,
32.   /* 27*/	 7, 7, 7, 6, 6, 5, 4, 2,
33.   /* 35*/	 8, 8, 8, 7, 7, 6, 6, 4, 2,
34.   /* 44*/	 9, 9, 9, 9, 8, 8, 7, 6, 5, 3,
35.   /* 54*/	10,10,10,10, 9, 9, 8, 7, 6, 5, 3,
36.   /* 65*/	11,11,11,11,10,10, 9, 9, 8, 7, 5, 3,
37.   /* 77*/	12,12,12,12,11,11,10,10, 9, 8, 7, 5, 3,
38.   /* 90*/	13,13,13,13,12,12,12,11,10,10, 9, 7, 6, 3,
39.   /*104*/	14,14,14,14,13,13,13,12,12,11,10, 9, 8, 6, 3,
40.   /*119*/	15,15,15,15,14,14,14,13,13,12,11,10, 9, 8, 6, 3,
41.   /*135*/ 16 /* should be MAX_RADIUS+1; used to terminate range loops -dlc */
42.   };
43.
44.   /*
45.    * These are the starting indexes into the circle_data[] array for a
46.    * circle of a given radius.
47.    */
48.   char circle_start[] = {
49.   /*  */	  0,	/* circles of radius zero are not used */
50.   /* 1*/    0,
51.   /* 2*/	  2,
52.   /* 3*/	  5,
53.   /* 4*/	  9,
54.   /* 5*/	 14,
55.   /* 6*/	 20,
56.   /* 7*/	 27,
57.   /* 8*/	 35,
58.   /* 9*/	 44,
59.   /*10*/	 54,
60.   /*11*/	 65,
61.   /*12*/	 77,
62.   /*13*/	 90,
63.   /*14*/	104,
64.   /*15*/	119,
65.   };
66.
67.
68.   /*===========================================================================*/
69.   /* Vision (arbitrary line of sight) =========================================*/
70.
71.   /*------ global variables ------*/
72.
73.   #if 0	/* (moved to decl.c) */
74.   /* True if we need to run a full vision recalculation. */
75.   boolean	vision_full_recalc = 0;
76.
77.   /* Pointers to the current vision array. */
78.   char	**viz_array;
79.   #endif
80.   char	*viz_rmin, *viz_rmax;		/* current vision cs bounds */
81.
82.
83.   /*------ local variables ------*/
84.
85.
86.   static char could_see[2][ROWNO][COLNO];		/* vision work space */
87.   static char *cs_rows0[ROWNO], *cs_rows1[ROWNO];
88.   static char  cs_rmin0[ROWNO],  cs_rmax0[ROWNO];
89.   static char  cs_rmin1[ROWNO],  cs_rmax1[ROWNO];
90.
91.   static char  viz_clear[ROWNO][COLNO];		/* vision clear/blocked map */
92.   static char *viz_clear_rows[ROWNO];
93.
94.   static char  left_ptrs[ROWNO][COLNO];		/* LOS algorithm helpers */
95.   static char right_ptrs[ROWNO][COLNO];
96.
97.   /* Forward declarations. */
98.   STATIC_DCL void FDECL(fill_point, (int,int));
99.   STATIC_DCL void FDECL(dig_point, (int,int));
100.  STATIC_DCL void NDECL(view_init);
101.  STATIC_DCL void FDECL(view_from,(int,int,char **,char *,char *,int,
102.  			     void (*)(int,int,genericptr_t),genericptr_t));
103.  STATIC_DCL void FDECL(get_unused_cs, (char ***,char **,char **));
104.  #ifdef REINCARNATION
105.  STATIC_DCL void FDECL(rogue_vision, (char **,char *,char *));
106.  #endif
107.
108.  /* Macro definitions that I can't find anywhere. */
109.  #define sign(z) ((z) < 0 ? -1 : ((z) ? 1 : 0 ))
110.  #define v_abs(z)  ((z) < 0 ? -(z) : (z))	/* don't use abs -- it may exist */
111.
112.  /*
113.   * vision_init()
114.   *
115.   * The one-time vision initialization routine.
116.   *
117.   * This must be called before mklev() is called in newgame() [allmain.c],
118.   * or before a game restore.   Else we die a horrible death.
119.   */
120.  void
121.  vision_init()
122.  {
123.      int i;
124.
125.      /* Set up the pointers. */
126.      for (i = 0; i < ROWNO; i++) {
127.  	cs_rows0[i] = could_see[0][i];
128.  	cs_rows1[i] = could_see[1][i];
129.  	viz_clear_rows[i] = viz_clear[i];
130.      }
131.
132.      /* Start out with cs0 as our current array */
133.      viz_array = cs_rows0;
134.      viz_rmin  = cs_rmin0;
135.      viz_rmax  = cs_rmax0;
136.
137.      vision_full_recalc = 0;
138.      (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
139.
140.      /* Initialize the vision algorithm (currently C or D). */
141.      view_init();
142.
143.  #ifdef VISION_TABLES
144.      /* Note:  this initializer doesn't do anything except guarantee that
145.  	      we're linked properly.
146.      */
147.      vis_tab_init();
148.  #endif
149.  }
150.
151.  /*
152.   * does_block()
153.   *
154.   * Returns true if the level feature, object, or monster at (x,y) blocks
155.   * sight.
156.   */
157.  int
158.  does_block(x,y,lev)
159.      int x, y;
160.      register struct rm    *lev;
161.  {
162.      struct obj   *obj;
163.      struct monst *mon;
164.
165.      /* Features that block . . */
166.      if (IS_ROCK(lev->typ) || lev->typ == TREE || (IS_DOOR(lev->typ) &&
167.  			    (lev->doormask & (D_CLOSED|D_LOCKED|D_TRAPPED) )))
168.  	return 1;
169.
170.      if (lev->typ == CLOUD || lev->typ == WATER ||
171.  			(lev->typ == MOAT && Underwater))
172.  	return 1;
173.
174.      /* Boulders block light. */
175.      for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
176.  	if (obj->otyp == BOULDER) return 1;
177.
178.      /* Mimics mimicing a door or boulder block light. */
179.      if ((mon = m_at(x,y)) && (!mon->minvis || See_invisible) &&
180.  	  ((mon->m_ap_type == M_AP_FURNITURE &&
181.  	  (mon->mappearance == S_hcdoor || mon->mappearance == S_vcdoor)) ||
182.  	  (mon->m_ap_type == M_AP_OBJECT && mon->mappearance == BOULDER)))
183.  	return 1;
184.
185.      return 0;
186.  }
187.
188.  /*
189.   * vision_reset()
190.   *
191.   * This must be called *after* the levl[][] structure is set with the new
192.   * level and the level monsters and objects are in place.
193.   */
194.  void
195.  vision_reset()
196.  {
197.      int y;
198.      register int x, i, dig_left, block;
199.      register struct rm    *lev;
200.
201.      /* Start out with cs0 as our current array */
202.      viz_array = cs_rows0;
203.      viz_rmin  = cs_rmin0;
204.      viz_rmax  = cs_rmax0;
205.
206.      (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
207.
208.      /* Reset the pointers and clear so that we have a "full" dungeon. */
209.      (void) memset((genericptr_t) viz_clear,        0, sizeof(viz_clear));
210.
211.      /* Dig the level */
212.      for (y = 0; y < ROWNO; y++) {
213.  	dig_left = 0;
214.  	block = TRUE;	/* location (0,y) is always stone; it's !isok() */
215.  	lev = &levl[1][y];
216.  	for (x = 1; x < COLNO; x++, lev += ROWNO)
217.  	    if (block != (IS_ROCK(lev->typ) || does_block(x,y,lev))) {
218.  		if(block) {
219.  		    for(i=dig_left; i<x; i++) {
220.  			left_ptrs [y][i] = dig_left;
221.  			right_ptrs[y][i] = x-1;
222.  		    }
223.  		} else {
224.  		    i = dig_left;
225.  		    if(dig_left) dig_left--; /* point at first blocked point */
226.  		    for(; i<x; i++) {
227.  			left_ptrs [y][i] = dig_left;
228.  			right_ptrs[y][i] = x;
229.  			viz_clear[y][i] = 1;
230.  		    }
231.  		}
232.  		dig_left = x;
233.  		block = !block;
234.  	    }
235.  	/* handle right boundary; almost identical for blocked/unblocked */
236.  	i = dig_left;
237.  	if(!block && dig_left) dig_left--; /* point at first blocked point */
238.  	for(; i<COLNO; i++) {
239.  	    left_ptrs [y][i] = dig_left;
240.  	    right_ptrs[y][i] = (COLNO-1);
241.  	    viz_clear[y][i] = !block;
242.  	}
243.      }
244.
245.      vision_full_recalc = 1;	/* we want to run vision_recalc() */
246.  }
247.
248.
249.  /*
250.   * get_unused_cs()
251.   *
252.   * Called from vision_recalc() and at least one light routine.  Get pointers
253.   * to the unused vision work area.
254.   */
255.  STATIC_OVL void
256.  get_unused_cs(rows, rmin, rmax)
257.      char ***rows;
258.      char **rmin, **rmax;
259.  {
260.      register int  row;
261.      register char *nrmin, *nrmax;
262.
263.      if (viz_array == cs_rows0) {
264.  	*rows = cs_rows1;
265.  	*rmin = cs_rmin1;
266.  	*rmax = cs_rmax1;
267.      } else {
268.  	*rows = cs_rows0;
269.  	*rmin = cs_rmin0;
270.  	*rmax = cs_rmax0;
271.      }
272.
273.      /* return an initialized, unused work area */
274.      nrmin = *rmin;
275.      nrmax = *rmax;
276.
277.      (void) memset((genericptr_t)**rows, 0, ROWNO*COLNO);  /* we see nothing */
278.      for (row = 0; row < ROWNO; row++) {		/* set row min & max */
279.  	*nrmin++ = COLNO-1;
280.  	*nrmax++ = 0;
281.      }
282.  }
283.
284.
285.  #ifdef REINCARNATION
286.  /*
287.   * rogue_vision()
288.   *
289.   * Set the "could see" and in sight bits so vision acts just like the old
290.   * rogue game:
291.   *
292.   *	+ If in a room, the hero can see to the room boundaries.
293.   *	+ The hero can always see adjacent squares.
294.   *
295.   * We set the in_sight bit here as well to escape a bug that shows up
296.   * due to the one-sided lit wall hack.
297.   */
298.  STATIC_OVL void
299.  rogue_vision(next, rmin, rmax)
300.      char **next;	/* could_see array pointers */
301.      char *rmin, *rmax;
302.  {
303.      int rnum = levl[u.ux][u.uy].roomno - ROOMOFFSET; /* no SHARED... */
304.      int start, stop, in_door, xhi, xlo, yhi, ylo;
305.      register int zx, zy;
306.
307.      /* If in a lit room, we are able to see to its boundaries. */
308.      /* If dark, set COULD_SEE so various spells work -dlc */
309.      if (rnum >= 0) {
310.  	for (zy = rooms[rnum].ly-1; zy <= rooms[rnum].hy+1; zy++) {
311.  	    rmin[zy] = start = rooms[rnum].lx-1;
312.  	    rmax[zy] = stop  = rooms[rnum].hx+1;
313.
314.  	    for (zx = start; zx <= stop; zx++) {
315.  		if (rooms[rnum].rlit) {
316.  		    next[zy][zx] = COULD_SEE | IN_SIGHT;
317.  		    levl[zx][zy].seenv = SVALL;	/* see the walls */
318.  		} else
319.  		    next[zy][zx] = COULD_SEE;
320.  	    }
321.  	}
322.      }
323.
324.      in_door = levl[u.ux][u.uy].typ == DOOR;
325.
326.      /* Can always see adjacent. */
327.      ylo = max(u.uy - 1, 0);
328.      yhi = min(u.uy + 1, ROWNO - 1);
329.      xlo = max(u.ux - 1, 1);
330.      xhi = min(u.ux + 1, COLNO - 1);
331.      for (zy = ylo; zy <= yhi; zy++) {
332.  	if (xlo < rmin[zy]) rmin[zy] = xlo;
333.  	if (xhi > rmax[zy]) rmax[zy] = xhi;
334.
335.  	for (zx = xlo; zx <= xhi; zx++) {
336.  	    next[zy][zx] = COULD_SEE | IN_SIGHT;
337.  	    /*
338.  	     * Yuck, update adjacent non-diagonal positions when in a doorway.
339.  	     * We need to do this to catch the case when we first step into
340.  	     * a room.  The room's walls were not seen from the outside, but
341.  	     * now are seen (the seen bits are set just above).  However, the
342.  	     * positions are not updated because they were already in sight.
343.  	     * So, we have to do it here.
344.  	     */
345.  	    if (in_door && (zx == u.ux || zy == u.uy)) newsym(zx,zy);
346.  	}
347.      }
348.  }
349.  #endif /* REINCARNATION */
350.
351.  /*#define EXTEND_SPINE*/	/* possibly better looking wall-angle */
352.
353.  #ifdef EXTEND_SPINE
354.
355.  STATIC_DCL int FDECL(new_angle, (struct rm *, unsigned char *, int, int));
356.  /*
357.   * new_angle()
358.   *
359.   * Return the new angle seen by the hero for this location.  The angle
360.   * bit is given in the value pointed at by sv.
361.   *
362.   * For T walls and crosswall, just setting the angle bit, even though
363.   * it is technically correct, doesn't look good.  If we can see the
364.   * next position beyond the current one and it is a wall that we can
365.   * see, then we want to extend a spine of the T to connect with the wall
366.   * that is beyond.  Example:
367.   *
368.   *	 Correct, but ugly			   Extend T spine
369.   *
370.   *		| ...					| ...
371.   *		| ...	<-- wall beyond & floor -->	| ...
372.   *		| ...					| ...
373.   * Unseen   -->   ...					| ...
374.   * spine	+-...	<-- trwall & doorway	-->	+-...
375.   *		| ...					| ...
376.   *
377.   *
378.   *		   @	<-- hero		-->	   @
379.   *
380.   *
381.   * We fake the above check by only checking if the horizontal &
382.   * vertical positions adjacent to the crosswall and T wall are
383.   * unblocked.  Then, _in general_ we can see beyond.  Generally,
384.   * this is good enough.
385.   *
386.   *	+ When this function is called we don't have all of the seen
387.   *	  information (we're doing a top down scan in vision_recalc).
388.   *	  We would need to scan once to set all IN_SIGHT and COULD_SEE
389.   *	  bits, then again to correctly set the seenv bits.
390.   *	+ I'm trying to make this as cheap as possible.  The display &
391.   *	  vision eat up too much CPU time.
392.   *
393.   *
394.   * Note:  Even as I write this, I'm still not convinced.  There are too
395.   *	  many exceptions.  I may have to bite the bullet and do more
396.   *	  checks.	- Dean 2/11/93
397.   */
398.  STATIC_OVL int
399.  new_angle(lev, sv, row, col)
400.      struct rm *lev;
401.      unsigned char *sv;
402.      int row, col;
403.  {
404.      register int res = *sv;
405.
406.      /*
407.       * Do extra checks for crosswalls and T walls if we see them from
408.       * an angle.
409.       */
410.      if (lev->typ >= CROSSWALL && lev->typ <= TRWALL) {
411.  	switch (res) {
412.  	    case SV0:
413.  		if (col > 0	  && viz_clear[row][col-1]) res |= SV7;
414.  		if (row > 0	  && viz_clear[row-1][col]) res |= SV1;
415.  		break;
416.  	    case SV2:
417.  		if (row > 0	  && viz_clear[row-1][col]) res |= SV1;
418.  		if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
419.  		break;
420.  	    case SV4:
421.  		if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
422.  		if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
423.  		break;
424.  	    case SV6:
425.  		if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
426.  		if (col > 0	  && viz_clear[row][col-1]) res |= SV7;
427.  		break;
428.  	}
429.      }
430.      return res;
431.  }
432.  #else
433.  /*
434.   * new_angle()
435.   *
436.   * Return the new angle seen by the hero for this location.  The angle
437.   * bit is given in the value pointed at by sv.
438.   *
439.   * The other parameters are not used.
440.   */
441.  #define new_angle(lev, sv, row, col) (*sv)
442.
443.  #endif
444.
445.
446.  /*
447.   * vision_recalc()
448.   *
449.   * Do all of the heavy vision work.  Recalculate all locations that could
450.   * possibly be seen by the hero --- if the location were lit, etc.  Note
451.   * which locations are actually seen because of lighting.  Then add to
452.   * this all locations that be seen by hero due to night vision and x-ray
453.   * vision.  Finally, compare with what the hero was able to see previously.
454.   * Update the difference.
455.   *
456.   * This function is usually called only when the variable 'vision_full_recalc'
457.   * is set.  The following is a list of places where this function is called,
458.   * with three valid values for the control flag parameter:
459.   *
460.   * Control flag = 0.  A complete vision recalculation.  Generate the vision
461.   * tables from scratch.  This is necessary to correctly set what the hero
462.   * can see.  (1) and (2) call this routine for synchronization purposes, (3)
463.   * calls this routine so it can operate correctly.
464.   *
465.   *	+ After the monster move, before input from the player. [moveloop()]
466.   *	+ At end of moveloop. [moveloop() ??? not sure why this is here]
467.   *	+ Right before something is printed. [pline()]
468.   *	+ Right before we do a vision based operation. [do_clear_area()]
469.   *	+ screen redraw, so we can renew all positions in sight. [docrt()]
470.   *
471.   * Control flag = 1.  An adjacent vision recalculation.  The hero has moved
472.   * one square.  Knowing this, it might be possible to optimize the vision
473.   * recalculation using the current knowledge.  This is presently unimplemented
474.   * and is treated as a control = 0 call.
475.   *
476.   *	+ Right after the hero moves. [domove()]
477.   *
478.   * Control flag = 2.  Turn off the vision system.  Nothing new will be
479.   * displayed, since nothing is seen.  This is usually done when you need
480.   * a newsym() run on all locations in sight, or on some locations but you
481.   * don't know which ones.
482.   *
483.   *	+ Before a screen redraw, so all positions are renewed. [docrt()]
484.   *	+ Right before the hero arrives on a new level. [goto_level()]
485.   *	+ Right after a scroll of light is read. [litroom()]
486.   *	+ After an option has changed that affects vision [parseoptions()]
487.   *	+ Right after the hero is swallowed. [gulpmu()]
488.   *	+ Just before bubbles are moved. [movebubbles()]
489.   */
490.  void
491.  vision_recalc(control)
492.      int control;
493.  {
494.      char **temp_array;	/* points to the old vision array */
495.      char **next_array;	/* points to the new vision array */
496.      char *next_row;	/* row pointer for the new array */
497.      char *old_row;	/* row pointer for the old array */
498.      char *next_rmin;	/* min pointer for the new array */
499.      char *next_rmax;	/* max pointer for the new array */
500.      char *ranges;	/* circle ranges -- used for xray & night vision */
501.      int row;		/* row counter (outer loop)  */
502.      int start, stop;	/* inner loop starting/stopping index */
503.      int dx, dy;		/* one step from a lit door or lit wall (see below) */
504.      register int col;	/* inner loop counter */
505.      register struct rm *lev;	/* pointer to current pos */
506.      struct rm *flev;	/* pointer to position in "front" of current pos */
507.      extern unsigned char seenv_matrix[3][3];	/* from display.c */
508.      static unsigned char colbump[COLNO+1];	/* cols to bump sv */
509.      unsigned char *sv;				/* ptr to seen angle bits */
510.      int oldseenv;				/* previous seenv value */
511.
512.      vision_full_recalc = 0;			/* reset flag */
513.      if (in_mklev) return;
514.
515.  #ifdef GCC_WARN
516.      row = 0;
517.  #endif
518.
519.      /*
520.       * Either the light sources have been taken care of, or we must
521.       * recalculate them here.
522.       */
523.
524.      /* Get the unused could see, row min, and row max arrays. */
525.      get_unused_cs(&next_array, &next_rmin, &next_rmax);
526.
527.      /* You see nothing, nothing can see you --- if swallowed or refreshing. */
528.      if (u.uswallow || control == 2) {
529.  	/* do nothing -- get_unused_cs() nulls out the new work area */
530.
531.      } else if (Blind) {
532.  	/*
533.  	 * Calculate the could_see array even when blind so that monsters
534.  	 * can see you, even if you can't see them.  Note that the current
535.  	 * setup allows:
536.  	 *
537.  	 *	+ Monsters to see with the "new" vision, even on the rogue
538.  	 *	  level.
539.  	 *
540.  	 *	+ Monsters can see you even when you're in a pit.
541.  	 */
542.  	view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
543.  		0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);
544.
545.  	/*
546.  	 * Our own version of the update loop below.  We know we can't see
547.  	 * anything, so we only need update positions we used to be able
548.  	 * to see.
549.  	 */
550.  	temp_array = viz_array;	/* set viz_array so newsym() will work */
551.  	viz_array = next_array;
552.
553.  	for (row = 0; row < ROWNO; row++) {
554.  	    old_row = temp_array[row];
555.
556.  	    /* Find the min and max positions on the row. */
557.  	    start = min(viz_rmin[row], next_rmin[row]);
558.  	    stop  = max(viz_rmax[row], next_rmax[row]);
559.
560.  	    for (col = start; col <= stop; col++)
561.  		if (old_row[col] & IN_SIGHT) newsym(col,row);
562.  	}
563.
564.  	/* skip the normal update loop */
565.  	goto skip;
566.      }
567.  #ifdef REINCARNATION
568.      else if (Is_rogue_level(&u.uz)) {
569.  	rogue_vision(next_array,next_rmin,next_rmax);
570.      }
571.  #endif
572.      else {
573.  	int has_night_vision = 1;	/* hero has night vision */
574.
575.  	if (Underwater && !Is_waterlevel(&u.uz)) {
576.  	    /*
577.  	     * The hero is under water.  Only see surrounding locations if
578.  	     * they are also underwater.  This overrides night vision but
579.  	     * does not override x-ray vision.
580.  	     */
581.  	    has_night_vision = 0;
582.
583.  	    for (row = u.uy-1; row <= u.uy+1; row++)
584.  		for (col = u.ux-1; col <= u.ux+1; col++) {
585.  		    if (!isok(col,row) || !is_pool(col,row)) continue;
586.
587.  		    next_rmin[row] = min(next_rmin[row], col);
588.  		    next_rmax[row] = max(next_rmax[row], col);
589.  		    next_array[row][col] = IN_SIGHT;
590.  		}
591.  	}
592.
593.  	/* if in a pit, just update for immediate locations */
594.  	else if (u.utrap && u.utraptype == TT_PIT) {
595.  	    for (row = u.uy-1; row <= u.uy+1; row++) {
596.  		if (row < 0) continue;	if (row >= ROWNO) break;
597.
598.  		next_rmin[row] = max(      0, u.ux - 1);
599.  		next_rmax[row] = min(COLNO-1, u.ux + 1);
600.  		next_row = next_array[row];
601.
602.  		for(col=next_rmin[row]; col <= next_rmax[row]; col++)
603.  		    next_row[col] = IN_SIGHT;
604.  	    }
605.  	} else
606.  	    view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
607.  		0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);
608.
609.  	/*
610.  	 * Set the IN_SIGHT bit for xray and night vision.
611.  	 */
612.  	if (u.xray_range >= 0) {
613.  	    if (u.xray_range) {
614.  		ranges = circle_ptr(u.xray_range);
615.
616.  		for (row = u.uy-u.xray_range; row <= u.uy+u.xray_range; row++) {
617.  		    if (row < 0) continue;	if (row >= ROWNO) break;
618.  		    dy = v_abs(u.uy-row);	next_row = next_array[row];
619.
620.  		    start = max(      0, u.ux - ranges[dy]);
621.  		    stop  = min(COLNO-1, u.ux + ranges[dy]);
622.
623.  		    for (col = start; col <= stop; col++) {
624.  			char old_row_val = next_row[col];
625.  			next_row[col] |= IN_SIGHT;
626.  			oldseenv = levl[col][row].seenv;
627.  			levl[col][row].seenv = SVALL;	/* see all! */
628.  			/* Update if previously not in sight or new angle. */
629.  			if (!(old_row_val & IN_SIGHT) || oldseenv != SVALL)
630.  			    newsym(col,row);
631.  		    }
632.
633.  		    next_rmin[row] = min(start, next_rmin[row]);
634.  		    next_rmax[row] = max(stop, next_rmax[row]);
635.  		}
636.
637.  	    } else {	/* range is 0 */
638.  		next_array[u.uy][u.ux] |= IN_SIGHT;
639.  		levl[u.ux][u.uy].seenv = SVALL;
640.  		next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
641.  		next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
642.  	    }
643.  	}
644.
645.  	if (has_night_vision && u.xray_range < u.nv_range) {
646.  	    if (!u.nv_range) {	/* range is 0 */
647.  		next_array[u.uy][u.ux] |= IN_SIGHT;
648.  		levl[u.ux][u.uy].seenv = SVALL;
649.  		next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
650.  		next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
651.  	    } else if (u.nv_range > 0) {
652.  		ranges = circle_ptr(u.nv_range);
653.
654.  		for (row = u.uy-u.nv_range; row <= u.uy+u.nv_range; row++) {
655.  		    if (row < 0) continue;	if (row >= ROWNO) break;
656.  		    dy = v_abs(u.uy-row);	next_row = next_array[row];
657.
658.  		    start = max(      0, u.ux - ranges[dy]);
659.  		    stop  = min(COLNO-1, u.ux + ranges[dy]);
660.
661.  		    for (col = start; col <= stop; col++)
662.  			if (next_row[col]) next_row[col] |= IN_SIGHT;
663.
664.  		    next_rmin[row] = min(start, next_rmin[row]);
665.  		    next_rmax[row] = max(stop, next_rmax[row]);
666.  		}
667.  	    }
668.  	}
669.      }
670.
671.      /* Set the correct bits for all light sources. */
672.      do_light_sources(next_array);
673.
674.
675.      /*
676.       * Make the viz_array the new array so that cansee() will work correctly.
677.       */
678.      temp_array = viz_array;
679.      viz_array = next_array;
680.
681.      /*
682.       * The main update loop.  Here we do two things:
683.       *
684.       *	    + Set the IN_SIGHT bit for places that we could see and are lit.
685.       *	    + Reset changed places.
686.       *
687.       * There is one thing that make deciding what the hero can see
688.       * difficult:
689.       *
690.       *  1.  Directional lighting.  Items that block light create problems.
691.       *      The worst offenders are doors.  Suppose a door to a lit room
692.       *      is closed.  It is lit on one side, but not on the other.  How
693.       *      do you know?  You have to check the closest adjacent position.
694.       *	    Even so, that is not entirely correct.  But it seems close
695.       *	    enough for now.
696.       */
697.      colbump[u.ux] = colbump[u.ux+1] = 1;
698.      for (row = 0; row < ROWNO; row++) {
699.  	dy = u.uy - row;                dy = sign(dy);
700.  	next_row = next_array[row];     old_row = temp_array[row];
701.
702.  	/* Find the min and max positions on the row. */
703.  	start = min(viz_rmin[row], next_rmin[row]);
704.  	stop  = max(viz_rmax[row], next_rmax[row]);
705.  	lev = &levl[start][row];
706.
707.  	sv = &seenv_matrix[dy+1][start < u.ux ? 0 : (start > u.ux ? 2:1)];
708.
709.  	for (col = start; col <= stop;
710.  				lev += ROWNO, sv += (int) colbump[++col]) {
711.  	    if (next_row[col] & IN_SIGHT) {
712.  		/*
713.  		 * We see this position because of night- or xray-vision.
714.  		 */
715.  		oldseenv = lev->seenv;
716.  		lev->seenv |= new_angle(lev,sv,row,col); /* update seen angle */
717.
718.  		/* Update pos if previously not in sight or new angle. */
719.  		if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
720.  		    newsym(col,row);
721.  	    }
722.
723.  	    else if (next_row[col] & COULD_SEE
724.  				&& (lev->lit || next_row[col] & TEMP_LIT)) {
725.  		/*
726.  		 * We see this position because it is lit.
727.  		 */
728.  		if (IS_DOOR(lev->typ) && !viz_clear[row][col]) {
729.  		    /*
730.  		     * Make sure doors, boulders or mimics don't show up
731.  		     * at the end of dark hallways.  We do this by checking
732.  		     * the adjacent position.  If it is lit, then we can see
733.  		     * the door, otherwise we can't.
734.  		     */
735.  		    dx = u.ux - col;	dx = sign(dx);
736.  		    flev = &(levl[col+dx][row+dy]);
737.  		    if (flev->lit || next_array[row+dy][col+dx] & TEMP_LIT) {
738.  			next_row[col] |= IN_SIGHT;	/* we see it */
739.
740.  			oldseenv = lev->seenv;
741.  			lev->seenv |= new_angle(lev,sv,row,col);
742.
743.  			/* Update pos if previously not in sight or new angle.*/
744.  			if (!(old_row[col] & IN_SIGHT) || oldseenv!=lev->seenv)
745.  			    newsym(col,row);
746.  		    } else
747.  			goto not_in_sight;	/* we don't see it */
748.
749.  		} else {
750.  		    next_row[col] |= IN_SIGHT;	/* we see it */
751.
752.  		    oldseenv = lev->seenv;
753.  		    lev->seenv |= new_angle(lev,sv,row,col);
754.
755.  		    /* Update pos if previously not in sight or new angle. */
756.  		    if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
757.  			newsym(col,row);
758.  		}
759.  	    } else if (next_row[col] & COULD_SEE && lev->waslit) {
760.  		/*
761.  		 * If we make it here, the hero _could see_ the location,
762.  		 * but doesn't see it (location is not lit).
763.  		 * However, the hero _remembers_ it as lit (waslit is true).
764.  		 * The hero can now see that it is not lit, so change waslit
765.  		 * and update the location.
766.  		 */
767.  		lev->waslit = 0; /* remember lit condition */
768.  		newsym(col,row);
769.  	    }
770.  	    /*
771.  	     * At this point we know that the row position is *not* in
772.  	     * sight.  If the old one *was* in sight, then clean up the
773.  	     * position.
774.  	     */
775.  	    else {
776.  not_in_sight:
777.  		if (old_row[col] & IN_SIGHT) newsym(col,row);
778.  	    }
779.
780.  	} /* end for col . . */
781.      }	/* end for row . .  */
782.      colbump[u.ux] = colbump[u.ux+1] = 0;
783.
784.  skip:
785.      newsym(u.ux,u.uy);		/* Make sure the hero shows up! */
786.
787.      /* Set the new min and max pointers. */
788.      viz_rmin  = next_rmin;
789.      viz_rmax = next_rmax;
790.  }
791.
792.
793.  /*
794.   * block_point()
795.   *
796.   * Make the location opaque to light.
797.   */
798.  void
799.  block_point(x,y)
800.      int x, y;
801.  {
802.      fill_point(y,x);
803.
804.      /* recalc light sources here? */
805.
806.      /*
807.       * We have to do a full vision recalculation if we "could see" the
808.       * location.  Why? Suppose some monster opened a way so that the
809.       * hero could see a lit room.  However, the position of the opening
810.       * was out of night-vision range of the hero.  Suddenly the hero should
811.       * see the lit room.
812.       */
813.      if (viz_array[y][x]) vision_full_recalc = 1;
814.  }
815.
816.  /*
817.   * unblock_point()
818.   *
819.   * Make the location transparent to light.
820.   */
821.  void
822.  unblock_point(x,y)
823.      int x, y;
824.  {
825.      dig_point(y,x);
826.
827.      /* recalc light sources here? */
828.
829.      if (viz_array[y][x]) vision_full_recalc = 1;
830.  }
831.
832.
833.  /*===========================================================================*\
834.   |									     |
835.   |	Everything below this line uses (y,x) instead of (x,y) --- the	     |
836.   |	algorithms are faster if they are less recursive and can scan	     |
837.   |	on a row longer.						     |
838.   |									     |
839.  \*===========================================================================*/
840.
841.
842.  /* ========================================================================= *\
843.  			Left and Right Pointer Updates
844.  \* ========================================================================= */
845.
846.  /*
847.   *			LEFT and RIGHT pointer rules
848.   *
849.   *
850.   * **NOTE**  The rules changed on 4/4/90.  This comment reflects the
851.   * new rules.  The change was so that the stone-wall optimization
852.   * would work.
853.   *
854.   * OK, now the tough stuff.  We must maintain our left and right
855.   * row pointers.  The rules are as follows:
856.   *
857.   * Left Pointers:
858.   * ______________
859.   *
860.   * + If you are a clear spot, your left will point to the first
861.   *   stone to your left.  If there is none, then point the first
862.   *   legal position in the row (0).
863.   *
864.   * + If you are a blocked spot, then your left will point to the
865.   *   left-most blocked spot to your left that is connected to you.
866.   *   This means that a left-edge (a blocked spot that has an open
867.   *   spot on its left) will point to itself.
868.   *
869.   *
870.   * Right Pointers:
871.   * ---------------
872.   * + If you are a clear spot, your right will point to the first
873.   *   stone to your right.  If there is none, then point the last
874.   *   legal position in the row (COLNO-1).
875.   *
876.   * + If you are a blocked spot, then your right will point to the
877.   *   right-most blocked spot to your right that is connected to you.
878.   *   This means that a right-edge (a blocked spot that has an open
879.   *    spot on its right) will point to itself.
880.   */
881.  STATIC_OVL void
882.  dig_point(row,col)
883.      int row,col;
884.  {
885.      int i;
886.
887.      if (viz_clear[row][col]) return;		/* already done */
888.
889.      viz_clear[row][col] = 1;
890.
891.      /*
892.       * Boundary cases first.
893.       */
894.      if (col == 0) {				/* left edge */
895.  	if (viz_clear[row][1]) {
896.  	    right_ptrs[row][0] = right_ptrs[row][1];
897.  	} else {
898.  	    right_ptrs[row][0] = 1;
899.  	    for (i = 1; i <= right_ptrs[row][1]; i++)
900.  		left_ptrs[row][i] = 1;
901.  	}
902.      } else if (col == (COLNO-1)) {		/* right edge */
903.
904.  	if (viz_clear[row][COLNO-2]) {
905.  	    left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
906.  	} else {
907.  	    left_ptrs[row][COLNO-1] = COLNO-2;
908.  	    for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
909.  		right_ptrs[row][i] = COLNO-2;
910.  	}
911.      }
912.
913.      /*
914.       * At this point, we know we aren't on the boundaries.
915.       */
916.      else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
917.  	/* Both sides clear */
918.  	for (i = left_ptrs[row][col-1]; i <= col; i++) {
919.  	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
920.  	    right_ptrs[row][i] = right_ptrs[row][col+1];
921.  	}
922.  	for (i = col; i <= right_ptrs[row][col+1]; i++) {
923.  	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
924.  	    left_ptrs[row][i] = left_ptrs[row][col-1];
925.  	}
926.
927.      } else if (viz_clear[row][col-1]) {
928.  	/* Left side clear, right side blocked. */
929.  	for (i = col+1; i <= right_ptrs[row][col+1]; i++)
930.  	    left_ptrs[row][i] = col+1;
931.
932.  	for (i = left_ptrs[row][col-1]; i <= col; i++) {
933.  	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
934.  	    right_ptrs[row][i] = col+1;
935.  	}
936.  	left_ptrs[row][col] = left_ptrs[row][col-1];
937.
938.      } else if (viz_clear[row][col+1]) {
939.  	/* Right side clear, left side blocked. */
940.  	for (i = left_ptrs[row][col-1]; i < col; i++)
941.  	    right_ptrs[row][i] = col-1;
942.
943.  	for (i = col; i <= right_ptrs[row][col+1]; i++) {
944.  	    if (!viz_clear[row][i]) continue;	/* catch non-end case */
945.  	    left_ptrs[row][i] = col-1;
946.  	}
947.  	right_ptrs[row][col] = right_ptrs[row][col+1];
948.
949.      } else {
950.  	/* Both sides blocked */
951.  	for (i = left_ptrs[row][col-1]; i < col; i++)
952.  	    right_ptrs[row][i] = col-1;
953.
954.  	for (i = col+1; i <= right_ptrs[row][col+1]; i++)
955.  	    left_ptrs[row][i] = col+1;
956.
957.  	left_ptrs[row][col]  = col-1;
958.  	right_ptrs[row][col] = col+1;
959.      }
960.  }
961.
962.  STATIC_OVL void
963.  fill_point(row,col)
964.      int row, col;
965.  {
966.      int i;
967.
968.      if (!viz_clear[row][col]) return;
969.
970.      viz_clear[row][col] = 0;
971.
972.      if (col == 0) {
973.  	if (viz_clear[row][1]) {			/* adjacent is clear */
974.  	    right_ptrs[row][0] = 0;
975.  	} else {
976.  	    right_ptrs[row][0] = right_ptrs[row][1];
977.  	    for (i = 1; i <= right_ptrs[row][1]; i++)
978.  		left_ptrs[row][i] = 0;
979.  	}
980.      } else if (col == COLNO-1) {
981.  	if (viz_clear[row][COLNO-2]) {		/* adjacent is clear */
982.  	    left_ptrs[row][COLNO-1] = COLNO-1;
983.  	} else {
984.  	    left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
985.  	    for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
986.  		right_ptrs[row][i] = COLNO-1;
987.  	}
988.      }
989.
990.      /*
991.       * Else we know that we are not on an edge.
992.       */
993.      else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
994.  	/* Both sides clear */
995.  	for (i = left_ptrs[row][col-1]+1; i <= col; i++)
996.  	    right_ptrs[row][i] = col;
997.
998.  	if (!left_ptrs[row][col-1])		/* catch the end case */
999.  	    right_ptrs[row][0] = col;
1000.
1001. 	for (i = col; i < right_ptrs[row][col+1]; i++)
1002. 	    left_ptrs[row][i] = col;
1003.
1004. 	if (right_ptrs[row][col+1] == COLNO-1)	/* catch the end case */
1005. 	    left_ptrs[row][COLNO-1] = col;
1006.
1007.     } else if (viz_clear[row][col-1]) {
1008. 	/* Left side clear, right side blocked. */
1009. 	for (i = col; i <= right_ptrs[row][col+1]; i++)
1010. 	    left_ptrs[row][i] = col;
1011.
1012. 	for (i = left_ptrs[row][col-1]+1; i < col; i++)
1013. 	    right_ptrs[row][i] = col;
1014.
1015. 	if (!left_ptrs[row][col-1])		/* catch the end case */
1016. 	    right_ptrs[row][i] = col;
1017.
1018. 	right_ptrs[row][col] = right_ptrs[row][col+1];
1019.
1020.     } else if (viz_clear[row][col+1]) {
1021. 	/* Right side clear, left side blocked. */
1022. 	for (i = left_ptrs[row][col-1]; i <= col; i++)
1023. 	    right_ptrs[row][i] = col;
1024.
1025. 	for (i = col+1; i < right_ptrs[row][col+1]; i++)
1026. 	    left_ptrs[row][i] = col;
1027.
1028. 	if (right_ptrs[row][col+1] == COLNO-1)	/* catch the end case */
1029. 	    left_ptrs[row][i] = col;
1030.
1031. 	left_ptrs[row][col] = left_ptrs[row][col-1];
1032.
1033.     } else {
1034. 	/* Both sides blocked */
1035. 	for (i = left_ptrs[row][col-1]; i <= col; i++)
1036. 	    right_ptrs[row][i] = right_ptrs[row][col+1];
1037.
1038. 	for (i = col; i <= right_ptrs[row][col+1]; i++)
1039. 	    left_ptrs[row][i] = left_ptrs[row][col-1];
1040.     }
1041. }
1042.
1043.
1044. /*===========================================================================*/
1045. /*===========================================================================*/
1046. /* Use either algorithm C or D.  See the config.h for more details. =========*/
1047.
1048. /*
1049.  * Variables local to both Algorithms C and D.
1050.  */
1051. static int  start_row;
1052. static int  start_col;
1053. static int  step;
1054. static char **cs_rows;
1055. static char *cs_left;
1056. static char *cs_right;
1057.
1058. static void FDECL((*vis_func), (int,int,genericptr_t));
1059. static genericptr_t varg;
1060.
1061. /*
1062.  * Both Algorithms C and D use the following macros.
1063.  *
1064.  *      good_row(z)	  - Return TRUE if the argument is a legal row.
1065.  *      set_cs(rowp,col)  - Set the local could see array.
1066.  *      set_min(z)	  - Save the min value of the argument and the current
1067.  *			      row minimum.
1068.  *      set_max(z)	  - Save the max value of the argument and the current
1069.  *			      row maximum.
1070.  *
1071.  * The last three macros depend on having local pointers row_min, row_max,
1072.  * and rowp being set correctly.
1073.  */
1074. #define set_cs(rowp,col) (rowp[col] = COULD_SEE)
1075. #define good_row(z) ((z) >= 0 && (z) < ROWNO)
1076. #define set_min(z) if (*row_min > (z)) *row_min = (z)
1077. #define set_max(z) if (*row_max < (z)) *row_max = (z)
1078. #define is_clear(row,col) viz_clear_rows[row][col]
1079.
1080. /*
1081.  * clear_path()		expanded into 4 macros/functions:
1082.  *
1083.  *	q1_path()
1084.  *	q2_path()
1085.  *	q3_path()
1086.  *	q4_path()
1087.  *
1088.  * "Draw" a line from the start to the given location.  Stop if we hit
1089.  * something that blocks light.  The start and finish points themselves are
1090.  * not checked, just the points between them.  These routines do _not_
1091.  * expect to be called with the same starting and stopping point.
1092.  *
1093.  * These routines use the generalized integer Bresenham's algorithm (fast
1094.  * line drawing) for all quadrants.  The algorithm was taken from _Procedural
1095.  * Elements for Computer Graphics_, by David F. Rogers.  McGraw-Hill, 1985.
1096.  */
1097. #ifdef MACRO_CPATH	/* quadrant calls are macros */
1098.
1099. /*
1100.  * When called, the result is in "result".
1101.  * The first two arguments (srow,scol) are one end of the path.  The next
1102.  * two arguments (row,col) are the destination.  The last argument is
1103.  * used as a C language label.  This means that it must be different
1104.  * in each pair of calls.
1105.  */
1106.
1107. /*
1108.  *  Quadrant I (step < 0).
1109.  */
1110. #define q1_path(srow,scol,y2,x2,label)			\
1111. {							\
1112.     int dx, dy;						\
1113.     register int k, err, x, y, dxs, dys;		\
1114. 							\
1115.     x  = (scol);	y  = (srow);			\
1116.     dx = (x2) - x;	dy = y - (y2);			\
1117. 							\
1118.     result = 0;		 /* default to a blocked path */\
1119. 							\
1120.     dxs = dx << 1;	   /* save the shifted values */\
1121.     dys = dy << 1;					\
1122.     if (dy > dx) {					\
1123. 	err = dxs - dy;					\
1124. 							\
1125. 	for (k = dy-1; k; k--) {			\
1126. 	    if (err >= 0) {				\
1127. 		x++;					\
1128. 		err -= dys;				\
1129. 	    }						\
1130. 	    y--;					\
1131. 	    err += dxs;					\
1132. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1133. 	}						\
1134.     } else {						\
1135. 	err = dys - dx;					\
1136. 							\
1137. 	for (k = dx-1; k; k--) {			\
1138. 	    if (err >= 0) {				\
1139. 		y--;					\
1140. 		err -= dxs;				\
1141. 	    }						\
1142. 	    x++;					\
1143. 	    err += dys;					\
1144. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1145. 	}						\
1146.     }							\
1147. 							\
1148.     result = 1;						\
1149. }
1150.
1151. /*
1152.  * Quadrant IV (step > 0).
1153.  */
1154. #define q4_path(srow,scol,y2,x2,label)			\
1155. {							\
1156.     int dx, dy;						\
1157.     register int k, err, x, y, dxs, dys;		\
1158. 							\
1159.     x  = (scol);	y  = (srow);			\
1160.     dx = (x2) - x;	dy = (y2) - y;			\
1161. 							\
1162.     result = 0;		 /* default to a blocked path */\
1163. 							\
1164.     dxs = dx << 1;	   /* save the shifted values */\
1165.     dys = dy << 1;					\
1166.     if (dy > dx) {					\
1167. 	err = dxs - dy;					\
1168. 							\
1169. 	for (k = dy-1; k; k--) {			\
1170. 	    if (err >= 0) {				\
1171. 		x++;					\
1172. 		err -= dys;				\
1173. 	    }						\
1174. 	    y++;					\
1175. 	    err += dxs;					\
1176. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1177. 	}						\
1178. 							\
1179.     } else {						\
1180. 	err = dys - dx;					\
1181. 							\
1182. 	for (k = dx-1; k; k--) {			\
1183. 	    if (err >= 0) {				\
1184. 		y++;					\
1185. 		err -= dxs;				\
1186. 	    }						\
1187. 	    x++;					\
1188. 	    err += dys;					\
1189. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1190. 	}						\
1191.     }							\
1192. 							\
1193.     result = 1;						\
1194. }
1195.
1196. /*
1197.  * Quadrant II (step < 0).
1198.  */
1199. #define q2_path(srow,scol,y2,x2,label)			\
1200. {							\
1201.     int dx, dy;						\
1202.     register int k, err, x, y, dxs, dys;		\
1203. 							\
1204.     x  = (scol);	y  = (srow);			\
1205.     dx = x - (x2);	dy = y - (y2);			\
1206. 							\
1207.     result = 0;		 /* default to a blocked path */\
1208. 							\
1209.     dxs = dx << 1;	   /* save the shifted values */\
1210.     dys = dy << 1;					\
1211.     if (dy > dx) {					\
1212. 	err = dxs - dy;					\
1213. 							\
1214. 	for (k = dy-1; k; k--) {			\
1215. 	    if (err >= 0) {				\
1216. 		x--;					\
1217. 		err -= dys;				\
1218. 	    }						\
1219. 	    y--;					\
1220. 	    err += dxs;					\
1221. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1222. 	}						\
1223.     } else {						\
1224. 	err = dys - dx;					\
1225. 							\
1226. 	for (k = dx-1; k; k--) {			\
1227. 	    if (err >= 0) {				\
1228. 		y--;					\
1229. 		err -= dxs;				\
1230. 	    }						\
1231. 	    x--;					\
1232. 	    err += dys;					\
1233. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1234. 	}						\
1235.     }							\
1236. 							\
1237.     result = 1;						\
1238. }
1239.
1240. /*
1241.  * Quadrant III (step > 0).
1242.  */
1243. #define q3_path(srow,scol,y2,x2,label)			\
1244. {							\
1245.     int dx, dy;						\
1246.     register int k, err, x, y, dxs, dys;		\
1247. 							\
1248.     x  = (scol);	y  = (srow);			\
1249.     dx = x - (x2);	dy = (y2) - y;			\
1250. 							\
1251.     result = 0;		 /* default to a blocked path */\
1252. 							\
1253.     dxs = dx << 1;	   /* save the shifted values */\
1254.     dys = dy << 1;					\
1255.     if (dy > dx) {					\
1256. 	err = dxs - dy;					\
1257. 							\
1258. 	for (k = dy-1; k; k--) {			\
1259. 	    if (err >= 0) {				\
1260. 		x--;					\
1261. 		err -= dys;				\
1262. 	    }						\
1263. 	    y++;					\
1264. 	    err += dxs;					\
1265. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1266. 	}						\
1267. 							\
1268.     } else {						\
1269. 	err = dys - dx;					\
1270. 							\
1271. 	for (k = dx-1; k; k--) {			\
1272. 	    if (err >= 0) {				\
1273. 		y++;					\
1274. 		err -= dxs;				\
1275. 	    }						\
1276. 	    x--;					\
1277. 	    err += dys;					\
1278. 	    if (!is_clear(y,x)) goto label;/* blocked */\
1279. 	}						\
1280.     }							\
1281. 							\
1282.     result = 1;						\
1283. }
1284.
1285. #else   /* quadrants are really functions */
1286.
1287. STATIC_DCL int FDECL(_q1_path, (int,int,int,int));
1288. STATIC_DCL int FDECL(_q2_path, (int,int,int,int));
1289. STATIC_DCL int FDECL(_q3_path, (int,int,int,int));
1290. STATIC_DCL int FDECL(_q4_path, (int,int,int,int));
1291.
1292. #define q1_path(sy,sx,y,x,dummy) result = _q1_path(sy,sx,y,x)
1293. #define q2_path(sy,sx,y,x,dummy) result = _q2_path(sy,sx,y,x)
1294. #define q3_path(sy,sx,y,x,dummy) result = _q3_path(sy,sx,y,x)
1295. #define q4_path(sy,sx,y,x,dummy) result = _q4_path(sy,sx,y,x)
1296.
1297. /*
1298.  * Quadrant I (step < 0).
1299.  */
1300. STATIC_OVL int
1301. _q1_path(srow,scol,y2,x2)
1302.     int scol, srow, y2, x2;
1303. {
1304.     int dx, dy;
1305.     register int k, err, x, y, dxs, dys;
1306.
1307.     x  = scol;		y  = srow;
1308.     dx = x2 - x;	dy = y - y2;
1309.
1310.     dxs = dx << 1;	   /* save the shifted values */
1311.     dys = dy << 1;
1312.     if (dy > dx) {
1313. 	err = dxs - dy;
1314.
1315. 	for (k = dy-1; k; k--) {
1316. 	    if (err >= 0) {
1317. 		x++;
1318. 		err -= dys;
1319. 	    }
1320. 	    y--;
1321. 	    err += dxs;
1322. 	    if (!is_clear(y,x)) return 0; /* blocked */
1323. 	}
1324.     } else {
1325. 	err = dys - dx;
1326.
1327. 	for (k = dx-1; k; k--) {
1328. 	    if (err >= 0) {
1329. 		y--;
1330. 		err -= dxs;
1331. 	    }
1332. 	    x++;
1333. 	    err += dys;
1334. 	    if (!is_clear(y,x)) return 0;/* blocked */
1335. 	}
1336.     }
1337.
1338.     return 1;
1339. }
1340.
1341. /*
1342.  * Quadrant IV (step > 0).
1343.  */
1344. STATIC_OVL int
1345. _q4_path(srow,scol,y2,x2)
1346.     int scol, srow, y2, x2;
1347. {
1348.     int dx, dy;
1349.     register int k, err, x, y, dxs, dys;
1350.
1351.     x  = scol;		y  = srow;
1352.     dx = x2 - x;	dy = y2 - y;
1353.
1354.     dxs = dx << 1;	   /* save the shifted values */
1355.     dys = dy << 1;
1356.     if (dy > dx) {
1357. 	err = dxs - dy;
1358.
1359. 	for (k = dy-1; k; k--) {
1360. 	    if (err >= 0) {
1361. 		x++;
1362. 		err -= dys;
1363. 	    }
1364. 	    y++;
1365. 	    err += dxs;
1366. 	    if (!is_clear(y,x)) return 0; /* blocked */
1367. 	}
1368.     } else {
1369. 	err = dys - dx;
1370.
1371. 	for (k = dx-1; k; k--) {
1372. 	    if (err >= 0) {
1373. 		y++;
1374. 		err -= dxs;
1375. 	    }
1376. 	    x++;
1377. 	    err += dys;
1378. 	    if (!is_clear(y,x)) return 0;/* blocked */
1379. 	}
1380.     }
1381.
1382.     return 1;
1383. }
1384.
1385. /*
1386.  * Quadrant II (step < 0).
1387.  */
1388. STATIC_OVL int
1389. _q2_path(srow,scol,y2,x2)
1390.     int scol, srow, y2, x2;
1391. {
1392.     int dx, dy;
1393.     register int k, err, x, y, dxs, dys;
1394.
1395.     x  = scol;		y  = srow;
1396.     dx = x - x2;	dy = y - y2;
1397.
1398.     dxs = dx << 1;	   /* save the shifted values */
1399.     dys = dy << 1;
1400.     if (dy > dx) {
1401. 	err = dxs - dy;
1402.
1403. 	for (k = dy-1; k; k--) {
1404. 	    if (err >= 0) {
1405. 		x--;
1406. 		err -= dys;
1407. 	    }
1408. 	    y--;
1409. 	    err += dxs;
1410. 	    if (!is_clear(y,x)) return 0; /* blocked */
1411. 	}
1412.     } else {
1413. 	err = dys - dx;
1414.
1415. 	for (k = dx-1; k; k--) {
1416. 	    if (err >= 0) {
1417. 		y--;
1418. 		err -= dxs;
1419. 	    }
1420. 	    x--;
1421. 	    err += dys;
1422. 	    if (!is_clear(y,x)) return 0;/* blocked */
1423. 	}
1424.     }
1425.
1426.     return 1;
1427. }
1428.
1429. /*
1430.  * Quadrant III (step > 0).
1431.  */
1432. STATIC_OVL int
1433. _q3_path(srow,scol,y2,x2)
1434.     int scol, srow, y2, x2;
1435. {
1436.     int dx, dy;
1437.     register int k, err, x, y, dxs, dys;
1438.
1439.     x  = scol;		y  = srow;
1440.     dx = x - x2;	dy = y2 - y;
1441.
1442.     dxs = dx << 1;	   /* save the shifted values */
1443.     dys = dy << 1;
1444.     if (dy > dx) {
1445. 	err = dxs - dy;
1446.
1447. 	for (k = dy-1; k; k--) {
1448. 	    if (err >= 0) {
1449. 		x--;
1450. 		err -= dys;
1451. 	    }
1452. 	    y++;
1453. 	    err += dxs;
1454. 	    if (!is_clear(y,x)) return 0; /* blocked */
1455. 	}
1456.     } else {
1457. 	err = dys - dx;
1458.
1459. 	for (k = dx-1; k; k--) {
1460. 	    if (err >= 0) {
1461. 		y++;
1462. 		err -= dxs;
1463. 	    }
1464. 	    x--;
1465. 	    err += dys;
1466. 	    if (!is_clear(y,x)) return 0;/* blocked */
1467. 	}
1468.     }
1469.
1470.     return 1;
1471. }
1472.
1473. #endif	/* quadrants are functions */
1474.
1475. /*
1476.  * Use vision tables to determine if there is a clear path from
1477.  * (col1,row1) to (col2,row2).  This is used by:
1478.  *		m_cansee()
1479.  *		m_canseeu()
1480.  *		do_light_sources()
1481.  */
1482. boolean
1483. clear_path(col1,row1,col2,row2)
1484.     int col1, row1, col2, row2;
1485. {
1486.     int result;
1487.
1488.     if(col1 < col2) {
1489. 	if(row1 > row2) {
1490. 	    q1_path(row1,col1,row2,col2,cleardone);
1491. 	} else {
1492. 	    q4_path(row1,col1,row2,col2,cleardone);
1493. 	}
1494.     } else {
1495. 	if(row1 > row2) {
1496. 	    q2_path(row1,col1,row2,col2,cleardone);
1497. 	} else if(row1 == row2 && col1 == col2) {
1498. 	    result = 1;
1499. 	} else {
1500. 	    q3_path(row1,col1,row2,col2,cleardone);
1501. 	}
1502.     }
1503. cleardone:
1504.     return((boolean)result);
1505. }
1506.
1507. #ifdef VISION_TABLES
1508. /*===========================================================================*\
1509. 			    GENERAL LINE OF SIGHT
1510. 				Algorithm D
1511. \*===========================================================================*/
1512.
1513.
1514. /*
1515.  * Indicate caller for the shadow routines.
1516.  */
1517. #define FROM_RIGHT 0
1518. #define FROM_LEFT  1
1519.
1520.
1521. /*
1522.  * Include the table definitions.
1523.  */
1524. #include "vis_tab.h"
1525.
1526.
1527. /* 3D table pointers. */
1528. static close2d *close_dy[CLOSE_MAX_BC_DY];
1529. static far2d   *far_dy[FAR_MAX_BC_DY];
1530.
1531. STATIC_DCL void FDECL(right_side, (int,int,int,int,int,int,int,char*));
1532. STATIC_DCL void FDECL(left_side, (int,int,int,int,int,int,int,char*));
1533. STATIC_DCL int FDECL(close_shadow, (int,int,int,int));
1534. STATIC_DCL int FDECL(far_shadow, (int,int,int,int));
1535.
1536. /*
1537.  * Initialize algorithm D's table pointers.  If we don't have these,
1538.  * then we do 3D table lookups.  Verrrry slow.
1539.  */
1540. STATIC_OVL void
1541. view_init()
1542. {
1543.     int i;
1544.
1545.     for (i = 0; i < CLOSE_MAX_BC_DY; i++)
1546. 	close_dy[i] = &close_table[i];
1547.
1548.     for (i = 0; i < FAR_MAX_BC_DY; i++)
1549. 	far_dy[i] = &far_table[i];
1550. }
1551.
1552.
1553. /*
1554.  * If the far table has an entry of OFF_TABLE, then the far block prevents
1555.  * us from seeing the location just above/below it.  I.e. the first visible
1556.  * location is one *before* the block.
1557.  */
1558. #define OFF_TABLE 0xff
1559.
1560. STATIC_OVL int
1562.     int side,this_row,block_row,block_col;
1563. {
1564.     register int sdy, sdx, pdy, offset;
1565.
1566.     /*
1567.      * If on the same column (block_row = -1), then we can see it.
1568.      */
1569.     if (block_row < 0) return block_col;
1570.
1571.     /* Take explicit absolute values.  Adjust. */
1572.     if ((sdy = (start_row-block_row)) < 0) sdy = -sdy; --sdy;	/* src   dy */
1573.     if ((sdx = (start_col-block_col)) < 0) sdx = -sdx;		/* src   dx */
1574.     if ((pdy = (block_row-this_row))  < 0) pdy = -pdy;		/* point dy */
1575.
1576.     if (sdy < 0 || sdy >= CLOSE_MAX_SB_DY || sdx >= CLOSE_MAX_SB_DX ||
1577. 						    pdy >= CLOSE_MAX_BC_DY) {
1579. 	return block_col;
1580.     }
1581.     offset = close_dy[sdy]->close[sdx][pdy];
1582.     if (side == FROM_RIGHT)
1583. 	return block_col + offset;
1584.
1585.     return block_col - offset;
1586. }
1587.
1588.
1589. STATIC_OVL int
1591.     int side,this_row,block_row,block_col;
1592. {
1593.     register int sdy, sdx, pdy, offset;
1594.
1595.     /*
1596.      * Take care of a bug that shows up only on the borders.
1597.      *
1598.      * If the block is beyond the border, then the row is negative.  Return
1599.      * the block's column number (should be 0 or COLNO-1).
1600.      *
1601.      * Could easily have the column be -1, but then wouldn't know if it was
1602.      * the left or right border.
1603.      */
1604.     if (block_row < 0) return block_col;
1605.
1606.     /* Take explicit absolute values.  Adjust. */
1607.     if ((sdy = (start_row-block_row)) < 0) sdy = -sdy;		/* src   dy */
1608.     if ((sdx = (start_col-block_col)) < 0) sdx = -sdx; --sdx;	/* src   dx */
1609.     if ((pdy = (block_row-this_row))  < 0) pdy = -pdy; --pdy;	/* point dy */
1610.
1611.     if (sdy >= FAR_MAX_SB_DY || sdx < 0 || sdx >= FAR_MAX_SB_DX ||
1612. 					    pdy < 0 || pdy >= FAR_MAX_BC_DY) {
1614. 	return block_col;
1615.     }
1616.     if ((offset = far_dy[sdy]->far_q[sdx][pdy]) == OFF_TABLE) offset = -1;
1617.     if (side == FROM_RIGHT)
1618. 	return block_col + offset;
1619.
1620.     return block_col - offset;
1621. }
1622.
1623.
1624. /*
1625.  * right_side()
1626.  *
1627.  * Figure out what could be seen on the right side of the source.
1628.  */
1629. STATIC_OVL void
1630. right_side(row, cb_row, cb_col, fb_row, fb_col, left, right_mark, limits)
1631.     int row;		/* current row */
1632.     int	cb_row, cb_col;	/* close block row and col */
1633.     int	fb_row, fb_col;	/* far block row and col */
1634.     int left;		/* left mark of the previous row */
1635.     int	right_mark;	/* right mark of previous row */
1636.     char *limits;	/* points at range limit for current row, or NULL */
1637. {
1638.     register int  i;
1639.     register char *rowp;
1640.     int  hit_stone = 0;
1642.     int  lblock_col;		/* local block column (current row) */
1643.     int  nrow, deeper;
1644.     char *row_min;		/* left most */
1645.     char *row_max;		/* right most */
1646.     int		  lim_max;	/* right most limit of circle */
1647.
1648.     nrow    = row + step;
1649.     deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
1650.     if(!vis_func) {
1651. 	rowp    = cs_rows[row];
1652. 	row_min = &cs_left[row];
1653. 	row_max = &cs_right[row];
1654.     }
1655.     if(limits) {
1656. 	lim_max = start_col + *limits;
1657. 	if(lim_max > COLNO-1) lim_max = COLNO-1;
1658. 	if(right_mark > lim_max) right_mark = lim_max;
1659. 	limits++; /* prepare for next row */
1660.     } else
1661. 	lim_max = COLNO-1;
1662.
1663.     /*
1664.      * Get the left shadow from the close block.  This value could be
1665.      * illegal.
1666.      */
1668.
1669.     /*
1670.      * Mark all stone walls as seen before the left shadow.  All this work
1671.      * for a special case.
1672.      *
1673.      * NOTE.  With the addition of this code in here, it is now *required*
1674.      * for the algorithm to work correctly.  If this is commented out,
1675.      * change the above assignment so that left and not left_shadow is the
1676.      * variable that gets the shadow.
1677.      */
1678.     while (left <= right_mark) {
1679. 	loc_right = right_ptrs[row][left];
1680. 	if(loc_right > lim_max) loc_right = lim_max;
1681. 	if (viz_clear_rows[row][left]) {
1682. 	    if (loc_right >= left_shadow) {
1683. 		left = left_shadow;	/* opening ends beyond shadow */
1684. 		break;
1685. 	    }
1686. 	    left = loc_right;
1687. 	    loc_right = right_ptrs[row][left];
1688. 	    if(loc_right > lim_max) loc_right = lim_max;
1689. 	    if (left == loc_right) return;	/* boundary */
1690.
1691. 	    /* Shadow covers opening, beyond right mark */
1692. 	    if (left == right_mark && left_shadow > right_mark) return;
1693. 	}
1694.
1695. 	if (loc_right > right_mark)	/* can't see stone beyond the mark */
1696. 	    loc_right = right_mark;
1697.
1698. 	if(vis_func) {
1699. 	    for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1700. 	} else {
1701. 	    for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1702. 	    set_min(left);	set_max(loc_right);
1703. 	}
1704.
1705. 	if (loc_right == right_mark) return;	/* all stone */
1706. 	if (loc_right >= left_shadow) hit_stone = 1;
1707. 	left = loc_right + 1;
1708.     }
1709.
1710.     /*
1711.      * At this point we are at the first visible clear spot on or beyond
1712.      * the left shadow, unless the left shadow is an illegal value.  If we
1713.      * have "hit stone" then we have a stone wall just to our left.
1714.      */
1715.
1716.     /*
1717.      * Get the right shadow.  Make sure that it is a legal value.
1718.      */
1719.     if ((right_shadow = far_shadow(FROM_RIGHT,row,fb_row,fb_col)) >= COLNO)
1720. 	right_shadow = COLNO-1;
1721.     /*
1722.      * Make vertical walls work the way we want them.  In this case, we
1723.      * note when the close block blocks the column just above/beneath
1724.      * it (right_shadow < fb_col [actually right_shadow == fb_col-1]).  If
1725.      * the location is filled, then we want to see it, so we put the
1726.      * right shadow back (same as fb_col).
1727.      */
1728.     if (right_shadow < fb_col && !viz_clear_rows[row][fb_col])
1729. 	right_shadow = fb_col;
1730.     if(right_shadow > lim_max) right_shadow = lim_max;
1731.
1732.     /*
1733.      * Main loop.  Within the range of sight of the previous row, mark all
1734.      * stone walls as seen.  Follow open areas recursively.
1735.      */
1736.     while (left <= right_mark) {
1737. 	/* Get the far right of the opening or wall */
1738. 	loc_right = right_ptrs[row][left];
1739. 	if(loc_right > lim_max) loc_right = lim_max;
1740.
1741. 	if (!viz_clear_rows[row][left]) {
1742. 	    hit_stone = 1;	/* use stone on this row as close block */
1743. 	    /*
1744. 	     * We can see all of the wall until the next open spot or the
1745. 	     * start of the shadow caused by the far block (right).
1746. 	     *
1747. 	     * Can't see stone beyond the right mark.
1748. 	     */
1749. 	    if (loc_right > right_mark) loc_right = right_mark;
1750.
1751. 	    if(vis_func) {
1752. 		for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1753. 	    } else {
1754. 		for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1755. 		set_min(left);	set_max(loc_right);
1756. 	    }
1757.
1758. 	    if (loc_right == right_mark) return;	/* hit the end */
1759. 	    left = loc_right + 1;
1760. 	    loc_right = right_ptrs[row][left];
1761. 	    if(loc_right > lim_max) loc_right = lim_max;
1762. 	    /* fall through... we know at least one position is visible */
1763. 	}
1764.
1765. 	/*
1766. 	 * We are in an opening.
1767. 	 *
1768. 	 * If this is the first open spot since the could see area  (this is
1769. 	 * true if we have hit stone), get the shadow generated by the wall
1770. 	 * just to our left.
1771. 	 */
1772. 	if (hit_stone) {
1773. 	    lblock_col = left-1;	/* local block column */
1774. 	    left = close_shadow(FROM_RIGHT,row,row,lblock_col);
1775. 	    if (left > lim_max) break;		/* off the end */
1776. 	}
1777.
1778. 	/*
1779. 	 * Check if the shadow covers the opening.  If it does, then
1780. 	 * move to end of the opening.  A shadow generated on from a
1781. 	 * wall on this row does *not* cover the wall on the right
1782. 	 * of the opening.
1783. 	 */
1784. 	if (left >= loc_right) {
1785. 	    if (loc_right == lim_max) {		/* boundary */
1786. 		if (left == lim_max) {
1787. 		    if(vis_func) (*vis_func)(lim_max, row, varg);
1788. 		    else {
1789. 			set_cs(rowp,lim_max);	/* last pos */
1790. 			set_max(lim_max);
1791. 		    }
1792. 		}
1793. 		return;					/* done */
1794. 	    }
1795. 	    left = loc_right;
1796. 	    continue;
1797. 	}
1798.
1799. 	/*
1800. 	 * If the far wall of the opening (loc_right) is closer than the
1801. 	 * shadow limit imposed by the far block (right) then use the far
1802. 	 * wall as our new far block when we recurse.
1803. 	 *
1804. 	 * If the limits are the the same, and the far block really exists
1805. 	 * (fb_row >= 0) then do the same as above.
1806. 	 *
1807. 	 * Normally, the check would be for the far wall being closer OR EQUAL
1808. 	 * to the shadow limit.  However, there is a bug that arises from the
1809. 	 * fact that the clear area pointers end in an open space (if it
1810. 	 * exists) on a boundary.  This then makes a far block exist where it
1811. 	 * shouldn't --- on a boundary.  To get around that, I had to
1812. 	 * introduce the concept of a non-existent far block (when the
1813. 	 * row < 0).  Next I have to check for it.  Here is where that check
1814. 	 * exists.
1815. 	 */
1816. 	if ((loc_right < right_shadow) ||
1817. 				(fb_row >= 0 && loc_right == right_shadow)) {
1818. 	    if(vis_func) {
1819. 		for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1820. 	    } else {
1821. 		for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1822. 		set_min(left);	set_max(loc_right);
1823. 	    }
1824.
1825. 	    if (deeper) {
1826. 		if (hit_stone)
1827. 		    right_side(nrow,row,lblock_col,row,loc_right,
1828. 							left,loc_right,limits);
1829. 		else
1830. 		    right_side(nrow,cb_row,cb_col,row,loc_right,
1831. 							left,loc_right,limits);
1832. 	    }
1833.
1834. 	    /*
1835. 	     * The following line, setting hit_stone, is needed for those
1836. 	     * walls that are only 1 wide.  If hit stone is *not* set and
1837. 	     * the stone is only one wide, then the close block is the old
1838. 	     * one instead one on the current row.  A way around having to
1839. 	     * set it here is to make left = loc_right (not loc_right+1) and
1840. 	     * let the outer loop take care of it.  However, if we do that
1841. 	     * then we then have to check for boundary conditions here as
1842. 	     * well.
1843. 	     */
1844. 	    hit_stone = 1;
1845.
1846. 	    left = loc_right+1;
1847. 	}
1848. 	/*
1849. 	 * The opening extends beyond the right mark.  This means that
1850. 	 * the next far block is the current far block.
1851. 	 */
1852. 	else {
1853. 	    if(vis_func) {
1854. 		for (i=left; i <= right_shadow; i++) (*vis_func)(i, row, varg);
1855. 	    } else {
1856. 		for (i = left; i <= right_shadow; i++) set_cs(rowp,i);
1858. 	    }
1859.
1860. 	    if (deeper) {
1861. 		if (hit_stone)
1862. 		    right_side(nrow,   row,lblock_col,fb_row,fb_col,
1864. 		else
1865. 		    right_side(nrow,cb_row,    cb_col,fb_row,fb_col,
1867. 	    }
1868.
1869. 	    return;	/* we're outta here */
1870. 	}
1871.     }
1872. }
1873.
1874.
1875. /*
1876.  * left_side()
1877.  *
1878.  * This routine is the mirror image of right_side().  Please see right_side()
1879.  * for blow by blow comments.
1880.  */
1881. STATIC_OVL void
1882. left_side(row, cb_row, cb_col, fb_row, fb_col, left_mark, right, limits)
1883.     int row;		/* the current row */
1884.     int	cb_row, cb_col;	/* close block row and col */
1885.     int	fb_row, fb_col;	/* far block row and col */
1886.     int	left_mark;	/* left mark of previous row */
1887.     int right;		/* right mark of the previous row */
1888.     char *limits;
1889. {
1890.     register int  i;
1891.     register char *rowp;
1892.     int  hit_stone = 0;
1894.     int  lblock_col;		/* local block column (current row) */
1895.     int  nrow, deeper;
1896.     char *row_min;		/* left most */
1897.     char *row_max;		/* right most */
1898.     int		  lim_min;
1899.
1900.     nrow    = row + step;
1901.     deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
1902.     if(!vis_func) {
1903. 	rowp    = cs_rows[row];
1904. 	row_min = &cs_left[row];
1905. 	row_max = &cs_right[row];
1906.     }
1907.     if(limits) {
1908. 	lim_min = start_col - *limits;
1909. 	if(lim_min < 0) lim_min = 0;
1910. 	if(left_mark < lim_min) left_mark = lim_min;
1911. 	limits++; /* prepare for next row */
1912.     } else
1913. 	lim_min = 0;
1914.
1915.     /* This value could be illegal. */
1917.
1918.     while ( right >= left_mark ) {
1919. 	loc_left = left_ptrs[row][right];
1920. 	if(loc_left < lim_min) loc_left = lim_min;
1921. 	if (viz_clear_rows[row][right]) {
1922. 	    if (loc_left <= right_shadow) {
1923. 		right = right_shadow;	/* opening ends beyond shadow */
1924. 		break;
1925. 	    }
1926. 	    right = loc_left;
1927. 	    loc_left = left_ptrs[row][right];
1928. 	    if(loc_left < lim_min) loc_left = lim_min;
1929. 	    if (right == loc_left) return;	/* boundary */
1930. 	}
1931.
1932. 	if (loc_left < left_mark)	/* can't see beyond the left mark */
1933. 	    loc_left = left_mark;
1934.
1935. 	if(vis_func) {
1936. 	    for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
1937. 	} else {
1938. 	    for (i = loc_left; i <= right; i++) set_cs(rowp,i);
1939. 	    set_min(loc_left);	set_max(right);
1940. 	}
1941.
1942. 	if (loc_left == left_mark) return;	/* all stone */
1943. 	if (loc_left <= right_shadow) hit_stone = 1;
1944. 	right = loc_left - 1;
1945.     }
1946.
1947.     /* At first visible clear spot on or beyond the right shadow. */
1948.
1949.     if ((left_shadow = far_shadow(FROM_LEFT,row,fb_row,fb_col)) < 0)
1950. 	left_shadow = 0;
1951.
1952.     /* Do vertical walls as we want. */
1953.     if (left_shadow > fb_col && !viz_clear_rows[row][fb_col])
1954. 	left_shadow = fb_col;
1955.     if(left_shadow < lim_min) left_shadow = lim_min;
1956.
1957.     while (right >= left_mark) {
1958. 	loc_left = left_ptrs[row][right];
1959.
1960. 	if (!viz_clear_rows[row][right]) {
1961. 	    hit_stone = 1;	/* use stone on this row as close block */
1962.
1963. 	    /* We can only see walls until the left mark */
1964. 	    if (loc_left < left_mark) loc_left = left_mark;
1965.
1966. 	    if(vis_func) {
1967. 		for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
1968. 	    } else {
1969. 		for (i = loc_left; i <= right; i++) set_cs(rowp,i);
1970. 		set_min(loc_left);	set_max(right);
1971. 	    }
1972.
1973. 	    if (loc_left == left_mark) return;	/* hit end */
1974. 	    right = loc_left - 1;
1975. 	    loc_left = left_ptrs[row][right];
1976. 	    if (loc_left < lim_min) loc_left = lim_min;
1977. 	    /* fall through...*/
1978. 	}
1979.
1980. 	/* We are in an opening. */
1981. 	if (hit_stone) {
1982. 	    lblock_col = right+1;	/* stone block (local) */
1983. 	    right = close_shadow(FROM_LEFT,row,row,lblock_col);
1984. 	    if (right < lim_min) return;	/* off the end */
1985. 	}
1986.
1987. 	/*  Check if the shadow covers the opening. */
1988. 	if (right <= loc_left) {
1989. 	    /*  Make a boundary condition work. */
1990. 	    if (loc_left == lim_min) {	/* at boundary */
1991. 		if (right == lim_min) {
1992. 		    if(vis_func) (*vis_func)(lim_min, row, varg);
1993. 		    else {
1994. 			set_cs(rowp,lim_min);	/* caught the last pos */
1995. 			set_min(lim_min);
1996. 		    }
1997. 		}
1998. 		return;			/* and break out the loop */
1999. 	    }
2000.
2001. 	    right = loc_left;
2002. 	    continue;
2003. 	}
2004.
2005. 	/* If the far wall of the opening is closer than the shadow limit. */
2006. 	if ((loc_left > left_shadow) ||
2007. 				    (fb_row >= 0 && loc_left == left_shadow)) {
2008. 	    if(vis_func) {
2009. 		for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
2010. 	    } else {
2011. 		for (i = loc_left; i <= right; i++) set_cs(rowp,i);
2012. 		set_min(loc_left);	set_max(right);
2013. 	    }
2014.
2015. 	    if (deeper) {
2016. 		if (hit_stone)
2017. 		    left_side(nrow,row,lblock_col,row,loc_left,
2018. 							loc_left,right,limits);
2019. 		else
2020. 		    left_side(nrow,cb_row,cb_col,row,loc_left,
2021. 							loc_left,right,limits);
2022. 	    }
2023.
2024. 	    hit_stone = 1;	/* needed for walls of width 1 */
2025. 	    right = loc_left-1;
2026. 	}
2027. 	/*  The opening extends beyond the left mark. */
2028. 	else {
2029. 	    if(vis_func) {
2030. 		for (i=left_shadow; i <= right; i++) (*vis_func)(i, row, varg);
2031. 	    } else {
2032. 		for (i = left_shadow; i <= right; i++) set_cs(rowp,i);
2034. 	    }
2035.
2036. 	    if (deeper) {
2037. 		if (hit_stone)
2038. 		    left_side(nrow,row,lblock_col,fb_row,fb_col,
2040. 		else
2041. 		    left_side(nrow,cb_row,cb_col,fb_row,fb_col,
2043. 	    }
2044.
2045. 	    return;	/* we're outta here */
2046. 	}
2047.
2048.     }
2049. }
2050.
2051. /*
2052.  * view_from
2053.  *
2054.  * Calculate a view from the given location.  Initialize and fill a
2055.  * ROWNOxCOLNO array (could_see) with all the locations that could be
2056.  * seen from the source location.  Initialize and fill the left most
2057.  * and right most boundaries of what could be seen.
2058.  */
2059. STATIC_OVL void
2060. view_from(srow,scol,loc_cs_rows,left_most,right_most, range, func, arg)
2061.     int  srow, scol;			/* source row and column */
2062.     char **loc_cs_rows;			/* could_see array (row pointers) */
2063.     char *left_most, *right_most;	/* limits of what could be seen */
2064.     int range;		/* 0 if unlimited */
2065.     void FDECL((*func), (int,int,genericptr_t));
2066.     genericptr_t arg;
2067. {
2068.     register int i;
2069.     char	 *rowp;
2070.     int		 nrow, left, right, left_row, right_row;
2071.     char	 *limits;
2072.
2073.     /* Set globals for near_shadow(), far_shadow(), etc. to use. */
2074.     start_col = scol;
2075.     start_row = srow;
2076.     cs_rows   = loc_cs_rows;
2077.     cs_left   = left_most;
2078.     cs_right  = right_most;
2079.     vis_func = func;
2080.     varg = arg;
2081.
2082.     /*  Find the left and right limits of sight on the starting row. */
2083.     if (viz_clear_rows[srow][scol]) {
2084. 	left  = left_ptrs[srow][scol];
2085. 	right = right_ptrs[srow][scol];
2086.     } else {
2087. 	left  = (!scol) ? 0 :
2088. 	    (viz_clear_rows[srow][scol-1] ?  left_ptrs[srow][scol-1] : scol-1);
2089. 	right = (scol == COLNO-1) ? COLNO-1 :
2090. 	    (viz_clear_rows[srow][scol+1] ? right_ptrs[srow][scol+1] : scol+1);
2091.     }
2092.
2093.     if(range) {
2094. 	if(range > MAX_RADIUS || range < 1)
2095. 	    panic("view_from called with range %d", range);
2096. 	limits = circle_ptr(range) + 1; /* start at next row */
2097. 	if(left < scol - range) left = scol - range;
2098. 	if(right > scol + range) right = scol + range;
2099.     } else
2100. 	limits = (char*) 0;
2101.
2102.     if(func) {
2103. 	for (i = left; i <= right; i++) (*func)(i, srow, arg);
2104.     } else {
2105. 	/* Row optimization */
2106. 	rowp = cs_rows[srow];
2107.
2108. 	/* We know that we can see our row. */
2109. 	for (i = left; i <= right; i++) set_cs(rowp,i);
2110. 	cs_left[srow]  = left;
2111. 	cs_right[srow] = right;
2112.     }
2113.
2114.     /* The far block has a row number of -1 if we are on an edge. */
2115.     right_row = (right == COLNO-1) ? -1 : srow;
2116.     left_row  = (!left)		   ? -1 : srow;
2117.
2118.     /*
2119.      *  Check what could be seen in quadrants.
2120.      */
2121.     if ( (nrow = srow+1) < ROWNO ) {
2122. 	step =  1;	/* move down */
2123. 	if (scol<COLNO-1)
2124. 	    right_side(nrow,-1,scol,right_row,right,scol,right,limits);
2125. 	if (scol)
2126. 	    left_side(nrow,-1,scol,left_row, left, left, scol,limits);
2127.     }
2128.
2129.     if ( (nrow = srow-1) >= 0 ) {
2130. 	step = -1;	/* move up */
2131. 	if (scol<COLNO-1)
2132. 	    right_side(nrow,-1,scol,right_row,right,scol,right,limits);
2133. 	if (scol)
2134. 	    left_side(nrow,-1,scol,left_row, left, left, scol,limits);
2135.     }
2136. }
2137.
2138.
2139. #else	/*===== End of algorithm D =====*/
2140.
2141.
2142. /*===========================================================================*\
2143. 			    GENERAL LINE OF SIGHT
2144. 				Algorithm C
2145. \*===========================================================================*/
2146.
2147. /*
2148.  * Defines local to Algorithm C.
2149.  */
2150. STATIC_DCL void FDECL(right_side, (int,int,int,char*));
2151. STATIC_DCL void FDECL(left_side, (int,int,int,char*));
2152.
2153. /* Initialize algorithm C (nothing). */
2154. STATIC_OVL void
2155. view_init()
2156. {
2157. }
2158.
2159. /*
2160.  * Mark positions as visible on one quadrant of the right side.  The
2161.  * quadrant is determined by the value of the global variable step.
2162.  */
2163. STATIC_OVL void
2164. right_side(row, left, right_mark, limits)
2165.     int row;		/* current row */
2166.     int left;		/* first (left side) visible spot on prev row */
2167.     int right_mark;	/* last (right side) visible spot on prev row */
2168.     char *limits;	/* points at range limit for current row, or NULL */
2169. {
2170.     int		  right;	/* right limit of "could see" */
2171.     int		  right_edge;	/* right edge of an opening */
2172.     int		  nrow;		/* new row (calculate once) */
2173.     int		  deeper;	/* if TRUE, call self as needed */
2174.     int		  result;	/* set by q?_path() */
2175.     register int  i;		/* loop counter */
2176.     register char *rowp;	/* row optimization */
2177.     char	  *row_min;	/* left most  [used by macro set_min()] */
2178.     char	  *row_max;	/* right most [used by macro set_max()] */
2179.     int		  lim_max;	/* right most limit of circle */
2180.
2181. #ifdef GCC_WARN
2182.     rowp = row_min = row_max = 0;
2183. #endif
2184.     nrow    = row + step;
2185.     /*
2186.      * Can go deeper if the row is in bounds and the next row is within
2187.      * the circle's limit.  We tell the latter by checking to see if the next
2188.      * limit value is the start of a new circle radius (meaning we depend
2189.      * on the structure of circle_data[]).
2190.      */
2191.     deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
2192.     if(!vis_func) {
2193. 	rowp    = cs_rows[row];	/* optimization */
2194. 	row_min = &cs_left[row];
2195. 	row_max = &cs_right[row];
2196.     }
2197.     if(limits) {
2198. 	lim_max = start_col + *limits;
2199. 	if(lim_max > COLNO-1) lim_max = COLNO-1;
2200. 	if(right_mark > lim_max) right_mark = lim_max;
2201. 	limits++; /* prepare for next row */
2202.     } else
2203. 	lim_max = COLNO-1;
2204.
2205.     while (left <= right_mark) {
2206. 	right_edge = right_ptrs[row][left];
2207. 	if(right_edge > lim_max) right_edge = lim_max;
2208.
2209. 	if (!is_clear(row,left)) {
2210. 	    /*
2211. 	     * Jump to the far side of a stone wall.  We can set all
2212. 	     * the points in between as seen.
2213. 	     *
2214. 	     * If the right edge goes beyond the right mark, check to see
2215. 	     * how much we can see.
2216. 	     */
2217. 	    if (right_edge > right_mark) {
2218. 		/*
2219. 		 * If the mark on the previous row was a clear position,
2220. 		 * the odds are that we can actually see part of the wall
2221. 		 * beyond the mark on this row.  If so, then see one beyond
2222. 		 * the mark.  Otherwise don't.  This is a kludge so corners
2223. 		 * with an adjacent doorway show up in nethack.
2224. 		 */
2225. 		right_edge = is_clear(row-step,right_mark) ?
2226. 						    right_mark+1 : right_mark;
2227. 	    }
2228. 	    if(vis_func) {
2229. 		for (i = left; i <= right_edge; i++) (*vis_func)(i, row, varg);
2230. 	    } else {
2231. 		for (i = left; i <= right_edge; i++) set_cs(rowp,i);
2232. 		set_min(left);      set_max(right_edge);
2233. 	    }
2234. 	    left = right_edge + 1; /* no limit check necessary */
2235. 	    continue;
2236. 	}
2237.
2238. 	/* No checking needed if our left side is the start column. */
2239. 	if (left != start_col) {
2240. 	    /*
2241. 	     * Find the left side.  Move right until we can see it or we run
2242. 	     * into a wall.
2243. 	     */
2244. 	    for (; left <= right_edge; left++) {
2245. 		if (step < 0) {
2246. 		    q1_path(start_row,start_col,row,left,rside1);
2247. 		} else {
2248. 		    q4_path(start_row,start_col,row,left,rside1);
2249. 		}
2250. rside1:					/* used if q?_path() is a macro */
2251. 		if (result) break;
2252. 	    }
2253.
2254. 	    /*
2255. 	     * Check for boundary conditions.  We *need* check (2) to break
2256. 	     * an infinite loop where:
2257. 	     *
2258. 	     *		left == right_edge == right_mark == lim_max.
2259. 	     *
2260. 	     */
2261. 	    if (left > lim_max) return;	/* check (1) */
2262. 	    if (left == lim_max) {	/* check (2) */
2263. 		if(vis_func) (*vis_func)(lim_max, row, varg);
2264. 		else {
2265. 		    set_cs(rowp,lim_max);
2266. 		    set_max(lim_max);
2267. 		}
2268. 		return;
2269. 	    }
2270. 	    /*
2271. 	     * Check if we can see any spots in the opening.  We might
2272. 	     * (left == right_edge) or might not (left == right_edge+1) have
2273. 	     * been able to see the far wall.  Make sure we *can* see the
2274. 	     * wall (remember, we can see the spot above/below this one)
2275. 	     * by backing up.
2276. 	     */
2277. 	    if (left >= right_edge) {
2278. 		left = right_edge;	/* for the case left == right_edge+1 */
2279. 		continue;
2280. 	    }
2281. 	}
2282.
2283. 	/*
2284. 	 * Find the right side.  If the marker from the previous row is
2285. 	 * closer than the edge on this row, then we have to check
2286. 	 * how far we can see around the corner (under the overhang).  Stop
2287. 	 * at the first non-visible spot or we actually hit the far wall.
2288. 	 *
2289. 	 * Otherwise, we know we can see the right edge of the current row.
2290. 	 *
2291. 	 * This must be a strict less than so that we can always see a
2292. 	 * horizontal wall, even if it is adjacent to us.
2293. 	 */
2294. 	if (right_mark < right_edge) {
2295. 	    for (right = right_mark; right <= right_edge; right++) {
2296. 		if (step < 0) {
2297. 		    q1_path(start_row,start_col,row,right,rside2);
2298. 		} else {
2299. 		    q4_path(start_row,start_col,row,right,rside2);
2300. 		}
2301. rside2:					/* used if q?_path() is a macro */
2302. 		if (!result) break;
2303. 	    }
2304. 	    --right;	/* get rid of the last increment */
2305. 	}
2306. 	else
2307. 	    right = right_edge;
2308.
2309. 	/*
2310. 	 * We have the range that we want.  Set the bits.  Note that
2311. 	 * there is no else --- we no longer handle splinters.
2312. 	 */
2313. 	if (left <= right) {
2314. 	    /*
2315. 	     * An ugly special case.  If you are adjacent to a vertical wall
2316. 	     * and it has a break in it, then the right mark is set to be
2317. 	     * start_col.  We *want* to be able to see adjacent vertical
2318. 	     * walls, so we have to set it back.
2319. 	     */
2320. 	    if (left == right && left == start_col &&
2321. 			start_col < (COLNO-1) && !is_clear(row,start_col+1))
2322. 		right = start_col+1;
2323.
2324. 	    if(right > lim_max) right = lim_max;
2325. 	    /* set the bits */
2326. 	    if(vis_func)
2327. 		for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
2328. 	    else {
2329. 		for (i = left; i <= right; i++) set_cs(rowp,i);
2330. 		set_min(left);      set_max(right);
2331. 	    }
2332.
2333. 	    /* recursive call for next finger of light */
2334. 	    if (deeper) right_side(nrow,left,right,limits);
2335. 	    left = right + 1; /* no limit check necessary */
2336. 	}
2337.     }
2338. }
2339.
2340.
2341. /*
2342.  * This routine is the mirror image of right_side().  See right_side() for
2343.  * extensive comments.
2344.  */
2345. STATIC_OVL void
2346. left_side(row, left_mark, right, limits)
2347.     int row, left_mark, right;
2348.     char *limits;
2349. {
2350.     int		  left, left_edge, nrow, deeper, result;
2351.     register int  i;
2352.     register char *rowp;
2353.     char	  *row_min, *row_max;
2354.     int		  lim_min;
2355.
2356. #ifdef GCC_WARN
2357.     rowp = row_min = row_max = 0;
2358. #endif
2359.     nrow    = row+step;
2360.     deeper  = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
2361.     if(!vis_func) {
2362. 	rowp    = cs_rows[row];
2363. 	row_min = &cs_left[row];
2364. 	row_max = &cs_right[row];
2365.     }
2366.     if(limits) {
2367. 	lim_min = start_col - *limits;
2368. 	if(lim_min < 0) lim_min = 0;
2369. 	if(left_mark < lim_min) left_mark = lim_min;
2370. 	limits++; /* prepare for next row */
2371.     } else
2372. 	lim_min = 0;
2373.
2374.     while (right >= left_mark) {
2375. 	left_edge = left_ptrs[row][right];
2376. 	if(left_edge < lim_min) left_edge = lim_min;
2377.
2378. 	if (!is_clear(row,right)) {
2379. 	    /* Jump to the far side of a stone wall. */
2380. 	    if (left_edge < left_mark) {
2381. 		/* Maybe see more (kludge). */
2382. 		left_edge = is_clear(row-step,left_mark) ?
2383. 						    left_mark-1 : left_mark;
2384. 	    }
2385. 	    if(vis_func) {
2386. 		for (i = left_edge; i <= right; i++) (*vis_func)(i, row, varg);
2387. 	    } else {
2388. 		for (i = left_edge; i <= right; i++) set_cs(rowp,i);
2389. 		set_min(left_edge); set_max(right);
2390. 	    }
2391. 	    right = left_edge - 1; /* no limit check necessary */
2392. 	    continue;
2393. 	}
2394.
2395. 	if (right != start_col) {
2396. 	    /* Find the right side. */
2397. 	    for (; right >= left_edge; right--) {
2398. 		if (step < 0) {
2399. 		    q2_path(start_row,start_col,row,right,lside1);
2400. 		} else {
2401. 		    q3_path(start_row,start_col,row,right,lside1);
2402. 		}
2403. lside1:					/* used if q?_path() is a macro */
2404. 		if (result) break;
2405. 	    }
2406.
2407. 	    /* Check for boundary conditions. */
2408. 	    if (right < lim_min) return;
2409. 	    if (right == lim_min) {
2410. 		if(vis_func) (*vis_func)(lim_min, row, varg);
2411. 		else {
2412. 		    set_cs(rowp,lim_min);
2413. 		    set_min(lim_min);
2414. 		}
2415. 		return;
2416. 	    }
2417. 	    /* Check if we can see any spots in the opening. */
2418. 	    if (right <= left_edge) {
2419. 		right = left_edge;
2420. 		continue;
2421. 	    }
2422. 	}
2423.
2424. 	/* Find the left side. */
2425. 	if (left_mark > left_edge) {
2426. 	    for (left = left_mark; left >= left_edge; --left) {
2427. 		if (step < 0) {
2428. 		    q2_path(start_row,start_col,row,left,lside2);
2429. 		} else {
2430. 		    q3_path(start_row,start_col,row,left,lside2);
2431. 		}
2432. lside2:					/* used if q?_path() is a macro */
2433. 		if (!result) break;
2434. 	    }
2435. 	    left++;	/* get rid of the last decrement */
2436. 	}
2437. 	else
2438. 	    left = left_edge;
2439.
2440. 	if (left <= right) {
2441. 	    /* An ugly special case. */
2442. 	    if (left == right && right == start_col &&
2443. 			    start_col > 0 && !is_clear(row,start_col-1))
2444. 		left = start_col-1;
2445.
2446. 	    if(left < lim_min) left = lim_min;
2447. 	    if(vis_func)
2448. 		for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
2449. 	    else {
2450. 		for (i = left; i <= right; i++) set_cs(rowp,i);
2451. 		set_min(left);      set_max(right);
2452. 	    }
2453.
2454. 	    /* Recurse */
2455. 	    if (deeper) left_side(nrow,left,right,limits);
2456. 	    right = left - 1; /* no limit check necessary */
2457. 	}
2458.     }
2459. }
2460.
2461.
2462. /*
2463.  * Calculate all possible visible locations from the given location
2464.  * (srow,scol).  NOTE this is (y,x)!  Mark the visible locations in the
2465.  * array provided.
2466.  */
2467. STATIC_OVL void
2468. view_from(srow, scol, loc_cs_rows, left_most, right_most, range, func, arg)
2469.     int  srow, scol;	/* starting row and column */
2470.     char **loc_cs_rows;	/* pointers to the rows of the could_see array */
2471.     char *left_most;	/* min mark on each row */
2472.     char *right_most;	/* max mark on each row */
2473.     int range;		/* 0 if unlimited */
2474.     void FDECL((*func), (int,int,genericptr_t));
2475.     genericptr_t arg;
2476. {
2477.     register int i;		/* loop counter */
2478.     char         *rowp;		/* optimization for setting could_see */
2479.     int		 nrow;		/* the next row */
2480.     int		 left;		/* the left-most visible column */
2481.     int		 right;		/* the right-most visible column */
2482.     char	 *limits;	/* range limit for next row */
2483.
2484.     /* Set globals for q?_path(), left_side(), and right_side() to use. */
2485.     start_col = scol;
2486.     start_row = srow;
2487.     cs_rows   = loc_cs_rows;	/* 'could see' rows */
2488.     cs_left   = left_most;
2489.     cs_right  = right_most;
2490.     vis_func = func;
2491.     varg = arg;
2492.
2493.     /*
2494.      * Determine extent of sight on the starting row.
2495.      */
2496.     if (is_clear(srow,scol)) {
2497. 	left =  left_ptrs[srow][scol];
2498. 	right = right_ptrs[srow][scol];
2499.     } else {
2500. 	/*
2501. 	 * When in stone, you can only see your adjacent squares, unless
2502. 	 * you are on an array boundary or a stone/clear boundary.
2503. 	 */
2504. 	left  = (!scol) ? 0 :
2505. 		(is_clear(srow,scol-1) ? left_ptrs[srow][scol-1] : scol-1);
2506. 	right = (scol == COLNO-1) ? COLNO-1 :
2507. 		(is_clear(srow,scol+1) ? right_ptrs[srow][scol+1] : scol+1);
2508.     }
2509.
2510.     if(range) {
2511. 	if(range > MAX_RADIUS || range < 1)
2512. 	    panic("view_from called with range %d", range);
2513. 	limits = circle_ptr(range) + 1; /* start at next row */
2514. 	if(left < scol - range) left = scol - range;
2515. 	if(right > scol + range) right = scol + range;
2516.     } else
2517. 	limits = (char*) 0;
2518.
2519.     if(func) {
2520. 	for (i = left; i <= right; i++) (*func)(i, srow, arg);
2521.     } else {
2522. 	/* Row pointer optimization. */
2523. 	rowp = cs_rows[srow];
2524.
2525. 	/* We know that we can see our row. */
2526. 	for (i = left; i <= right; i++) set_cs(rowp,i);
2527. 	cs_left[srow]  = left;
2528. 	cs_right[srow] = right;
2529.     }
2530.
2531.     /*
2532.      * Check what could be seen in quadrants.  We need to check for valid
2533.      * rows here, since we don't do it in the routines right_side() and
2534.      * left_side() [ugliness to remove extra routine calls].
2535.      */
2536.     if ( (nrow = srow+1) < ROWNO ) {	/* move down */
2537. 	step =  1;
2538. 	if (scol < COLNO-1) right_side(nrow, scol, right, limits);
2539. 	if (scol)	    left_side (nrow, left,  scol, limits);
2540.     }
2541.
2542.     if ( (nrow = srow-1) >= 0 ) {	/* move up */
2543. 	step = -1;
2544. 	if (scol < COLNO-1) right_side(nrow, scol, right, limits);
2545. 	if (scol)	    left_side (nrow, left,  scol, limits);
2546.     }
2547. }
2548.
2549. #endif	/*===== End of algorithm C =====*/
2550.
2551. /*
2552.  * AREA OF EFFECT "ENGINE"
2553.  *
2554.  * Calculate all possible visible locations as viewed from the given location
2555.  * (srow,scol) within the range specified. Perform "func" with (x, y) args and
2556.  * additional argument "arg" for each square.
2557.  *
2558.  * If not centered on the hero, just forward arguments to view_from(); it
2559.  * will call "func" when necessary.  If the hero is the center, use the
2560.  * vision matrix and reduce extra work.
2561.  */
2562. void
2563. do_clear_area(scol,srow,range,func,arg)
2564.     int scol, srow, range;
2565.     void FDECL((*func), (int,int,genericptr_t));
2566.     genericptr_t arg;
2567. {
2568. 	/* If not centered on hero, do the hard work of figuring the area */
2569. 	if (scol != u.ux || srow != u.uy)
2570. 	    view_from(srow, scol, (char **)0, (char *)0, (char *)0,
2571. 							range, func, arg);
2572. 	else {
2573. 	    register int x;
2574. 	    int y, min_x, max_x, max_y, offset;
2575. 	    char *limits;
2576.
2577. 	    if (range > MAX_RADIUS || range < 1)
2578. 		panic("do_clear_area:  illegal range %d", range);
2579. 	    if(vision_full_recalc)
2580. 		vision_recalc(0);	/* recalc vision if dirty */
2581. 	    limits = circle_ptr(range);
2582. 	    if ((max_y = (srow + range)) >= ROWNO) max_y = ROWNO-1;
2583. 	    if ((y = (srow - range)) < 0) y = 0;
2584. 	    for (; y <= max_y; y++) {
2585. 		offset = limits[v_abs(y-srow)];
2586. 		if((min_x = (scol - offset)) < 0) min_x = 0;
2587. 		if((max_x = (scol + offset)) >= COLNO) max_x = COLNO-1;
2588. 		for (x = min_x; x <= max_x; x++)
2589. 		    if (couldsee(x, y))
2590. 			(*func)(x, y, arg);
2591. 	    }
2592. 	}
2593. }
2594.
2595. /*vision.c*/
```