Source:NetHack 3.4.3/src/vision.c

From NetHackWiki
(Redirected from Source:Ref/get unused cs)
Jump to: navigation, search

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

Top of file

  1. /*	SCCS Id: @(#)vision.c	3.4	1999/02/18	*/
  2. /* Copyright (c) Dean Luick, with acknowledgements to Dave Cohrs, 1990.	*/
  3. /* NetHack may be freely redistributed.  See license for details.	*/

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. #include "hack.h"

Circles

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

Vision (arbitrary line of sight)

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

vision_init

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

does_block

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

vision_reset

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

get_unused_cs

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

rogue_vision

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

new_angle

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

vision_recalc

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

block_point

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

unblock_point

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

dig_point

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

fill_point

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

Algorithm C or D

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

clear_path

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

q1_path macro

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

q4_path macro

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

q2_path macro

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

q3_path macro

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

_q1_path function

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

_q4_path function

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

_q2_path function

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

_q3_path function

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

clear_path

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

Algorithm D

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

view_init D

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

close_shadow D

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

far_shadow D

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

right_side D

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

left_side D

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

view_from D

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

Algorithm C

  1. /*===========================================================================*\
  2. 			    GENERAL LINE OF SIGHT
  3. 				Algorithm C
  4. \*===========================================================================*/
  5.  
  6. /*
  7. * Defines local to Algorithm C.
  8. */
  9. STATIC_DCL void FDECL(right_side, (int,int,int,char*));
  10. STATIC_DCL void FDECL(left_side, (int,int,int,char*));

view_init C

  1. /* Initialize algorithm C (nothing). */
  2. STATIC_OVL void
  3. view_init()
  4. {
  5. }

right_side C

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

left_side C

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

view_from C

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

do_clear_area

  1. /*
  2. * AREA OF EFFECT "ENGINE"
  3. *
  4. * Calculate all possible visible locations as viewed from the given location
  5. * (srow,scol) within the range specified. Perform "func" with (x, y) args and
  6. * additional argument "arg" for each square.
  7. *
  8. * If not centered on the hero, just forward arguments to view_from(); it
  9. * will call "func" when necessary.  If the hero is the center, use the
  10. * vision matrix and reduce extra work.
  11. */
  12. void
  13. do_clear_area(scol,srow,range,func,arg)
  14. int scol, srow, range;
  15. void FDECL((*func), (int,int,genericptr_t));
  16. genericptr_t arg;
  17. {
  18. 	/* If not centered on hero, do the hard work of figuring the area */
  19. 	if (scol != u.ux || srow != u.uy)
  20. 	    view_from(srow, scol, (char **)0, (char *)0, (char *)0,
  21. 							range, func, arg);
  22. 	else {
  23. 	    register int x;
  24. 	    int y, min_x, max_x, max_y, offset;
  25. 	    char *limits;
  26.  
  27. 	    if (range > MAX_RADIUS || range < 1)
  28. 		panic("do_clear_area:  illegal range %d", range);
  29. 	    if(vision_full_recalc)
  30. 		vision_recalc(0);	/* recalc vision if dirty */
  31. 	    limits = circle_ptr(range);
  32. 	    if ((max_y = (srow + range)) >= ROWNO) max_y = ROWNO-1;
  33. 	    if ((y = (srow - range)) < 0) y = 0;
  34. 	    for (; y <= max_y; y++) {
  35. 		offset = limits[v_abs(y-srow)];
  36. 		if((min_x = (scol - offset)) < 0) min_x = 0;
  37. 		if((max_x = (scol + offset)) >= COLNO) max_x = COLNO-1;
  38. 		for (x = min_x; x <= max_x; x++)
  39. 		    if (couldsee(x, y))
  40. 			(*func)(x, y, arg);
  41. 	    }
  42. 	}
  43. }
  44.  
  45. /*vision.c*/