Source:NetHack 5.0.0/src/vision.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to vision.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/vision.c#line12]], 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.

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