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. */
7 /* Circles ==================================================================*/
10 * These numbers are limit offsets for one quadrant of a circle of a given
11 * radius (the first number of each line) from the source. The number in
12 * the comment is the element number (so pointers can be set up). Each
13 * "circle" has as many elements as its radius+1. The radius is the number
14 * of points away from the source that the limit exists. The radius of the
15 * offset on the same row as the source *is* included so we don't have to
16 * make an extra check. For example, a circle of radius 4 has offsets:
25 char circle_data[] = {
30 /* 14*/ 5, 5, 5, 4, 3, 2,
31 /* 20*/ 6, 6, 6, 5, 5, 4, 2,
32 /* 27*/ 7, 7, 7, 6, 6, 5, 4, 2,
33 /* 35*/ 8, 8, 8, 7, 7, 6, 6, 4, 2,
34 /* 44*/ 9, 9, 9, 9, 8, 8, 7, 6, 5, 3,
35 /* 54*/ 10,10,10,10, 9, 9, 8, 7, 6, 5, 3,
36 /* 65*/ 11,11,11,11,10,10, 9, 9, 8, 7, 5, 3,
37 /* 77*/ 12,12,12,12,11,11,10,10, 9, 8, 7, 5, 3,
38 /* 90*/ 13,13,13,13,12,12,12,11,10,10, 9, 7, 6, 3,
39 /*104*/ 14,14,14,14,13,13,13,12,12,11,10, 9, 8, 6, 3,
40 /*119*/ 15,15,15,15,14,14,14,13,13,12,11,10, 9, 8, 6, 3,
41 /*135*/ 16 /* should be MAX_RADIUS+1; used to terminate range loops -dlc */
45 * These are the starting indexes into the circle_data[] array for a
46 * circle of a given radius.
48 char circle_start[] = {
49 /* */ 0, /* circles of radius zero are not used */
68 /*===========================================================================*/
69 /* Vision (arbitrary line of sight) =========================================*/
71 /*------ global variables ------*/
73 #if 0 /* (moved to decl.c) */
74 /* True if we need to run a full vision recalculation. */
75 boolean vision_full_recalc = 0;
77 /* Pointers to the current vision array. */
80 char *viz_rmin, *viz_rmax; /* current vision cs bounds */
83 /*------ local variables ------*/
86 static char could_see[2][ROWNO][COLNO]; /* vision work space */
87 static char *cs_rows0[ROWNO], *cs_rows1[ROWNO];
88 static char cs_rmin0[ROWNO], cs_rmax0[ROWNO];
89 static char cs_rmin1[ROWNO], cs_rmax1[ROWNO];
91 static char viz_clear[ROWNO][COLNO]; /* vision clear/blocked map */
92 static char *viz_clear_rows[ROWNO];
94 static char left_ptrs[ROWNO][COLNO]; /* LOS algorithm helpers */
95 static char right_ptrs[ROWNO][COLNO];
97 /* Forward declarations. */
98 STATIC_DCL void FDECL(fill_point, (int,int));
99 STATIC_DCL void FDECL(dig_point, (int,int));
100 STATIC_DCL void NDECL(view_init);
101 STATIC_DCL void FDECL(view_from,(int,int,char **,char *,char *,int,
102 void (*)(int,int,genericptr_t),genericptr_t));
103 STATIC_DCL void FDECL(get_unused_cs, (char ***,char **,char **));
105 STATIC_DCL void FDECL(rogue_vision, (char **,char *,char *));
108 /* Macro definitions that I can't find anywhere. */
109 #define sign(z) ((z) < 0 ? -1 : ((z) ? 1 : 0 ))
110 #define v_abs(z) ((z) < 0 ? -(z) : (z)) /* don't use abs -- it may exist */
115 * The one-time vision initialization routine.
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.
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];
132 /* Start out with cs0 as our current array */
133 viz_array = cs_rows0;
137 vision_full_recalc = 0;
138 (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
140 /* Initialize the vision algorithm (currently C or D). */
144 /* Note: this initializer doesn't do anything except guarantee that
145 we're linked properly.
154 * Returns true if the level feature, object, or monster at (x,y) blocks
160 register struct rm *lev;
165 /* Features that block . . */
166 if (IS_ROCK(lev->typ) || lev->typ == TREE || (IS_DOOR(lev->typ) &&
167 (lev->doormask & (D_CLOSED|D_LOCKED|D_TRAPPED) )))
170 if (lev->typ == CLOUD || lev->typ == WATER ||
171 (lev->typ == MOAT && Underwater))
174 /* Boulders block light. */
175 for (obj = level.objects[x][y]; obj; obj = obj->nexthere)
176 if (obj->otyp == BOULDER) return 1;
178 /* Mimics mimicing a door or boulder block light. */
179 if ((mon = m_at(x,y)) && (!mon->minvis || See_invisible) &&
180 ((mon->m_ap_type == M_AP_FURNITURE &&
181 (mon->mappearance == S_hcdoor || mon->mappearance == S_vcdoor)) ||
182 (mon->m_ap_type == M_AP_OBJECT && mon->mappearance == BOULDER)))
191 * This must be called *after* the levl[][] structure is set with the new
192 * level and the level monsters and objects are in place.
198 register int x, i, dig_left, block;
199 register struct rm *lev;
201 /* Start out with cs0 as our current array */
202 viz_array = cs_rows0;
206 (void) memset((genericptr_t) could_see, 0, sizeof(could_see));
208 /* Reset the pointers and clear so that we have a "full" dungeon. */
209 (void) memset((genericptr_t) viz_clear, 0, sizeof(viz_clear));
212 for (y = 0; y < ROWNO; y++) {
214 block = TRUE; /* location (0,y) is always stone; it's !isok() */
216 for (x = 1; x < COLNO; x++, lev += ROWNO)
217 if (block != (IS_ROCK(lev->typ) || does_block(x,y,lev))) {
219 for(i=dig_left; i<x; i++) {
220 left_ptrs [y][i] = dig_left;
221 right_ptrs[y][i] = x-1;
225 if(dig_left) dig_left--; /* point at first blocked point */
227 left_ptrs [y][i] = dig_left;
228 right_ptrs[y][i] = x;
235 /* handle right boundary; almost identical for blocked/unblocked */
237 if(!block && dig_left) dig_left--; /* point at first blocked point */
238 for(; i<COLNO; i++) {
239 left_ptrs [y][i] = dig_left;
240 right_ptrs[y][i] = (COLNO-1);
241 viz_clear[y][i] = !block;
245 iflags.vision_inited = 1; /* vision is ready */
246 vision_full_recalc = 1; /* we want to run vision_recalc() */
253 * Called from vision_recalc() and at least one light routine. Get pointers
254 * to the unused vision work area.
257 get_unused_cs(rows, rmin, rmax)
262 register char *nrmin, *nrmax;
264 if (viz_array == cs_rows0) {
274 /* return an initialized, unused work area */
278 (void) memset((genericptr_t)**rows, 0, ROWNO*COLNO); /* we see nothing */
279 for (row = 0; row < ROWNO; row++) { /* set row min & max */
290 * Set the "could see" and in sight bits so vision acts just like the old
293 * + If in a room, the hero can see to the room boundaries.
294 * + The hero can always see adjacent squares.
296 * We set the in_sight bit here as well to escape a bug that shows up
297 * due to the one-sided lit wall hack.
300 rogue_vision(next, rmin, rmax)
301 char **next; /* could_see array pointers */
304 int rnum = levl[u.ux][u.uy].roomno - ROOMOFFSET; /* no SHARED... */
305 int start, stop, in_door, xhi, xlo, yhi, ylo;
308 /* If in a lit room, we are able to see to its boundaries. */
309 /* If dark, set COULD_SEE so various spells work -dlc */
311 for (zy = rooms[rnum].ly-1; zy <= rooms[rnum].hy+1; zy++) {
312 rmin[zy] = start = rooms[rnum].lx-1;
313 rmax[zy] = stop = rooms[rnum].hx+1;
315 for (zx = start; zx <= stop; zx++) {
316 if (rooms[rnum].rlit) {
317 next[zy][zx] = COULD_SEE | IN_SIGHT;
318 levl[zx][zy].seenv = SVALL; /* see the walls */
320 next[zy][zx] = COULD_SEE;
325 in_door = levl[u.ux][u.uy].typ == DOOR;
327 /* Can always see adjacent. */
328 ylo = max(u.uy - 1, 0);
329 yhi = min(u.uy + 1, ROWNO - 1);
330 xlo = max(u.ux - 1, 1);
331 xhi = min(u.ux + 1, COLNO - 1);
332 for (zy = ylo; zy <= yhi; zy++) {
333 if (xlo < rmin[zy]) rmin[zy] = xlo;
334 if (xhi > rmax[zy]) rmax[zy] = xhi;
336 for (zx = xlo; zx <= xhi; zx++) {
337 next[zy][zx] = COULD_SEE | IN_SIGHT;
339 * Yuck, update adjacent non-diagonal positions when in a doorway.
340 * We need to do this to catch the case when we first step into
341 * a room. The room's walls were not seen from the outside, but
342 * now are seen (the seen bits are set just above). However, the
343 * positions are not updated because they were already in sight.
344 * So, we have to do it here.
346 if (in_door && (zx == u.ux || zy == u.uy)) newsym(zx,zy);
350 #endif /* REINCARNATION */
352 /*#define EXTEND_SPINE*/ /* possibly better looking wall-angle */
356 STATIC_DCL int FDECL(new_angle, (struct rm *, unsigned char *, int, int));
360 * Return the new angle seen by the hero for this location. The angle
361 * bit is given in the value pointed at by sv.
363 * For T walls and crosswall, just setting the angle bit, even though
364 * it is technically correct, doesn't look good. If we can see the
365 * next position beyond the current one and it is a wall that we can
366 * see, then we want to extend a spine of the T to connect with the wall
367 * that is beyond. Example:
369 * Correct, but ugly Extend T spine
372 * | ... <-- wall beyond & floor --> | ...
374 * Unseen --> ... | ...
375 * spine +-... <-- trwall & doorway --> +-...
382 * We fake the above check by only checking if the horizontal &
383 * vertical positions adjacent to the crosswall and T wall are
384 * unblocked. Then, _in general_ we can see beyond. Generally,
385 * this is good enough.
387 * + When this function is called we don't have all of the seen
388 * information (we're doing a top down scan in vision_recalc).
389 * We would need to scan once to set all IN_SIGHT and COULD_SEE
390 * bits, then again to correctly set the seenv bits.
391 * + I'm trying to make this as cheap as possible. The display &
392 * vision eat up too much CPU time.
395 * Note: Even as I write this, I'm still not convinced. There are too
396 * many exceptions. I may have to bite the bullet and do more
397 * checks. - Dean 2/11/93
400 new_angle(lev, sv, row, col)
405 register int res = *sv;
408 * Do extra checks for crosswalls and T walls if we see them from
411 if (lev->typ >= CROSSWALL && lev->typ <= TRWALL) {
414 if (col > 0 && viz_clear[row][col-1]) res |= SV7;
415 if (row > 0 && viz_clear[row-1][col]) res |= SV1;
418 if (row > 0 && viz_clear[row-1][col]) res |= SV1;
419 if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
422 if (col < COLNO-1 && viz_clear[row][col+1]) res |= SV3;
423 if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
426 if (row < ROWNO-1 && viz_clear[row+1][col]) res |= SV5;
427 if (col > 0 && viz_clear[row][col-1]) res |= SV7;
437 * Return the new angle seen by the hero for this location. The angle
438 * bit is given in the value pointed at by sv.
440 * The other parameters are not used.
442 #define new_angle(lev, sv, row, col) (*sv)
450 * Do all of the heavy vision work. Recalculate all locations that could
451 * possibly be seen by the hero --- if the location were lit, etc. Note
452 * which locations are actually seen because of lighting. Then add to
453 * this all locations that be seen by hero due to night vision and x-ray
454 * vision. Finally, compare with what the hero was able to see previously.
455 * Update the difference.
457 * This function is usually called only when the variable 'vision_full_recalc'
458 * is set. The following is a list of places where this function is called,
459 * with three valid values for the control flag parameter:
461 * Control flag = 0. A complete vision recalculation. Generate the vision
462 * tables from scratch. This is necessary to correctly set what the hero
463 * can see. (1) and (2) call this routine for synchronization purposes, (3)
464 * calls this routine so it can operate correctly.
466 * + After the monster move, before input from the player. [moveloop()]
467 * + At end of moveloop. [moveloop() ??? not sure why this is here]
468 * + Right before something is printed. [pline()]
469 * + Right before we do a vision based operation. [do_clear_area()]
470 * + screen redraw, so we can renew all positions in sight. [docrt()]
472 * Control flag = 1. An adjacent vision recalculation. The hero has moved
473 * one square. Knowing this, it might be possible to optimize the vision
474 * recalculation using the current knowledge. This is presently unimplemented
475 * and is treated as a control = 0 call.
477 * + Right after the hero moves. [domove()]
479 * Control flag = 2. Turn off the vision system. Nothing new will be
480 * displayed, since nothing is seen. This is usually done when you need
481 * a newsym() run on all locations in sight, or on some locations but you
482 * don't know which ones.
484 * + Before a screen redraw, so all positions are renewed. [docrt()]
485 * + Right before the hero arrives on a new level. [goto_level()]
486 * + Right after a scroll of light is read. [litroom()]
487 * + After an option has changed that affects vision [parseoptions()]
488 * + Right after the hero is swallowed. [gulpmu()]
489 * + Just before bubbles are moved. [movebubbles()]
492 vision_recalc(control)
495 char **temp_array; /* points to the old vision array */
496 char **next_array; /* points to the new vision array */
497 char *next_row; /* row pointer for the new array */
498 char *old_row; /* row pointer for the old array */
499 char *next_rmin; /* min pointer for the new array */
500 char *next_rmax; /* max pointer for the new array */
501 char *ranges; /* circle ranges -- used for xray & night vision */
502 int row; /* row counter (outer loop) */
503 int start, stop; /* inner loop starting/stopping index */
504 int dx, dy; /* one step from a lit door or lit wall (see below) */
505 register int col; /* inner loop counter */
506 register struct rm *lev; /* pointer to current pos */
507 struct rm *flev; /* pointer to position in "front" of current pos */
508 extern unsigned char seenv_matrix[3][3]; /* from display.c */
509 static unsigned char colbump[COLNO+1]; /* cols to bump sv */
510 unsigned char *sv; /* ptr to seen angle bits */
511 int oldseenv; /* previous seenv value */
513 vision_full_recalc = 0; /* reset flag */
514 if (in_mklev || !iflags.vision_inited) return;
521 * Either the light sources have been taken care of, or we must
522 * recalculate them here.
525 /* Get the unused could see, row min, and row max arrays. */
526 get_unused_cs(&next_array, &next_rmin, &next_rmax);
528 /* You see nothing, nothing can see you --- if swallowed or refreshing. */
529 if (u.uswallow || control == 2) {
530 /* do nothing -- get_unused_cs() nulls out the new work area */
534 * Calculate the could_see array even when blind so that monsters
535 * can see you, even if you can't see them. Note that the current
538 * + Monsters to see with the "new" vision, even on the rogue
541 * + Monsters can see you even when you're in a pit.
543 view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
544 0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);
547 * Our own version of the update loop below. We know we can't see
548 * anything, so we only need update positions we used to be able
551 temp_array = viz_array; /* set viz_array so newsym() will work */
552 viz_array = next_array;
554 for (row = 0; row < ROWNO; row++) {
555 old_row = temp_array[row];
557 /* Find the min and max positions on the row. */
558 start = min(viz_rmin[row], next_rmin[row]);
559 stop = max(viz_rmax[row], next_rmax[row]);
561 for (col = start; col <= stop; col++)
562 if (old_row[col] & IN_SIGHT) newsym(col,row);
565 /* skip the normal update loop */
569 else if (Is_rogue_level(&u.uz)) {
570 rogue_vision(next_array,next_rmin,next_rmax);
574 int has_night_vision = 1; /* hero has night vision */
576 if (Underwater && !Is_waterlevel(&u.uz)) {
578 * The hero is under water. Only see surrounding locations if
579 * they are also underwater. This overrides night vision but
580 * does not override x-ray vision.
582 has_night_vision = 0;
584 for (row = u.uy-1; row <= u.uy+1; row++)
585 for (col = u.ux-1; col <= u.ux+1; col++) {
586 if (!isok(col,row) || !is_pool(col,row)) continue;
588 next_rmin[row] = min(next_rmin[row], col);
589 next_rmax[row] = max(next_rmax[row], col);
590 next_array[row][col] = IN_SIGHT | COULD_SEE;
594 /* if in a pit, just update for immediate locations */
595 else if (u.utrap && u.utraptype == TT_PIT) {
596 for (row = u.uy-1; row <= u.uy+1; row++) {
597 if (row < 0) continue; if (row >= ROWNO) break;
599 next_rmin[row] = max( 0, u.ux - 1);
600 next_rmax[row] = min(COLNO-1, u.ux + 1);
601 next_row = next_array[row];
603 for(col=next_rmin[row]; col <= next_rmax[row]; col++)
604 next_row[col] = IN_SIGHT | COULD_SEE;
607 view_from(u.uy, u.ux, next_array, next_rmin, next_rmax,
608 0, (void FDECL((*),(int,int,genericptr_t)))0, (genericptr_t)0);
611 * Set the IN_SIGHT bit for xray and night vision.
613 if (u.xray_range >= 0) {
615 ranges = circle_ptr(u.xray_range);
617 for (row = u.uy-u.xray_range; row <= u.uy+u.xray_range; row++) {
618 if (row < 0) continue; if (row >= ROWNO) break;
619 dy = v_abs(u.uy-row); next_row = next_array[row];
621 start = max( 0, u.ux - ranges[dy]);
622 stop = min(COLNO-1, u.ux + ranges[dy]);
624 for (col = start; col <= stop; col++) {
625 char old_row_val = next_row[col];
626 next_row[col] |= IN_SIGHT;
627 oldseenv = levl[col][row].seenv;
628 levl[col][row].seenv = SVALL; /* see all! */
629 /* Update if previously not in sight or new angle. */
630 if (!(old_row_val & IN_SIGHT) || oldseenv != SVALL)
634 next_rmin[row] = min(start, next_rmin[row]);
635 next_rmax[row] = max(stop, next_rmax[row]);
638 } else { /* range is 0 */
639 next_array[u.uy][u.ux] |= IN_SIGHT;
640 levl[u.ux][u.uy].seenv = SVALL;
641 next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
642 next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
646 if (has_night_vision && u.xray_range < u.nv_range) {
647 if (!u.nv_range) { /* range is 0 */
648 next_array[u.uy][u.ux] |= IN_SIGHT;
649 levl[u.ux][u.uy].seenv = SVALL;
650 next_rmin[u.uy] = min(u.ux, next_rmin[u.uy]);
651 next_rmax[u.uy] = max(u.ux, next_rmax[u.uy]);
652 } else if (u.nv_range > 0) {
653 ranges = circle_ptr(u.nv_range);
655 for (row = u.uy-u.nv_range; row <= u.uy+u.nv_range; row++) {
656 if (row < 0) continue; if (row >= ROWNO) break;
657 dy = v_abs(u.uy-row); next_row = next_array[row];
659 start = max( 0, u.ux - ranges[dy]);
660 stop = min(COLNO-1, u.ux + ranges[dy]);
662 for (col = start; col <= stop; col++)
663 if (next_row[col]) next_row[col] |= IN_SIGHT;
665 next_rmin[row] = min(start, next_rmin[row]);
666 next_rmax[row] = max(stop, next_rmax[row]);
672 /* Set the correct bits for all light sources. */
673 do_light_sources(next_array);
677 * Make the viz_array the new array so that cansee() will work correctly.
679 temp_array = viz_array;
680 viz_array = next_array;
683 * The main update loop. Here we do two things:
685 * + Set the IN_SIGHT bit for places that we could see and are lit.
686 * + Reset changed places.
688 * There is one thing that make deciding what the hero can see
691 * 1. Directional lighting. Items that block light create problems.
692 * The worst offenders are doors. Suppose a door to a lit room
693 * is closed. It is lit on one side, but not on the other. How
694 * do you know? You have to check the closest adjacent position.
695 * Even so, that is not entirely correct. But it seems close
698 colbump[u.ux] = colbump[u.ux+1] = 1;
699 for (row = 0; row < ROWNO; row++) {
700 dy = u.uy - row; dy = sign(dy);
701 next_row = next_array[row]; old_row = temp_array[row];
703 /* Find the min and max positions on the row. */
704 start = min(viz_rmin[row], next_rmin[row]);
705 stop = max(viz_rmax[row], next_rmax[row]);
706 lev = &levl[start][row];
708 sv = &seenv_matrix[dy+1][start < u.ux ? 0 : (start > u.ux ? 2:1)];
710 for (col = start; col <= stop;
711 lev += ROWNO, sv += (int) colbump[++col]) {
712 if (next_row[col] & IN_SIGHT) {
714 * We see this position because of night- or xray-vision.
716 oldseenv = lev->seenv;
717 lev->seenv |= new_angle(lev,sv,row,col); /* update seen angle */
719 /* Update pos if previously not in sight or new angle. */
720 if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
724 else if ((next_row[col] & COULD_SEE)
725 && (lev->lit || (next_row[col] & TEMP_LIT))) {
727 * We see this position because it is lit.
729 if ((IS_DOOR(lev->typ) || lev->typ == SDOOR ||
730 IS_WALL(lev->typ)) && !viz_clear[row][col]) {
732 * Make sure doors, walls, boulders or mimics don't show up
733 * at the end of dark hallways. We do this by checking
734 * the adjacent position. If it is lit, then we can see
735 * the door or wall, otherwise we can't.
737 dx = u.ux - col; dx = sign(dx);
738 flev = &(levl[col+dx][row+dy]);
739 if (flev->lit || next_array[row+dy][col+dx] & TEMP_LIT) {
740 next_row[col] |= IN_SIGHT; /* we see it */
742 oldseenv = lev->seenv;
743 lev->seenv |= new_angle(lev,sv,row,col);
745 /* Update pos if previously not in sight or new angle.*/
746 if (!(old_row[col] & IN_SIGHT) || oldseenv!=lev->seenv)
749 goto not_in_sight; /* we don't see it */
752 next_row[col] |= IN_SIGHT; /* we see it */
754 oldseenv = lev->seenv;
755 lev->seenv |= new_angle(lev,sv,row,col);
757 /* Update pos if previously not in sight or new angle. */
758 if ( !(old_row[col] & IN_SIGHT) || oldseenv != lev->seenv)
761 } else if ((next_row[col] & COULD_SEE) && lev->waslit) {
763 * If we make it here, the hero _could see_ the location,
764 * but doesn't see it (location is not lit).
765 * However, the hero _remembers_ it as lit (waslit is true).
766 * The hero can now see that it is not lit, so change waslit
767 * and update the location.
769 lev->waslit = 0; /* remember lit condition */
773 * At this point we know that the row position is *not* in normal
774 * sight. That is, the position is could be seen, but is dark
775 * or LOS is just plain blocked.
777 * Update the position if:
778 * o If the old one *was* in sight. We may need to clean up
779 * the glyph -- E.g. darken room spot, etc.
780 * o If we now could see the location (yet the location is not
781 * lit), but previously we couldn't see the location, or vice
782 * versa. Update the spot because there there may be an infared
787 if ((old_row[col] & IN_SIGHT)
788 || ((next_row[col] & COULD_SEE)
789 ^ (old_row[col] & COULD_SEE)))
793 } /* end for col . . */
794 } /* end for row . . */
795 colbump[u.ux] = colbump[u.ux+1] = 0;
798 /* This newsym() caused a crash delivering msg about failure to open
799 * dungeon file init_dungeons() -> panic() -> done(11) ->
800 * vision_recalc(2) -> newsym() -> crash! u.ux and u.uy are 0 and
801 * program_state.panicking == 1 under those circumstances
803 if (!program_state.panicking)
804 newsym(u.ux, u.uy); /* Make sure the hero shows up! */
806 /* Set the new min and max pointers. */
807 viz_rmin = next_rmin;
808 viz_rmax = next_rmax;
815 * Make the location opaque to light.
823 /* recalc light sources here? */
826 * We have to do a full vision recalculation if we "could see" the
827 * location. Why? Suppose some monster opened a way so that the
828 * hero could see a lit room. However, the position of the opening
829 * was out of night-vision range of the hero. Suddenly the hero should
832 if (viz_array[y][x]) vision_full_recalc = 1;
838 * Make the location transparent to light.
846 /* recalc light sources here? */
848 if (viz_array[y][x]) vision_full_recalc = 1;
852 /*===========================================================================*\
854 | Everything below this line uses (y,x) instead of (x,y) --- the |
855 | algorithms are faster if they are less recursive and can scan |
858 \*===========================================================================*/
861 /* ========================================================================= *\
862 Left and Right Pointer Updates
863 \* ========================================================================= */
866 * LEFT and RIGHT pointer rules
869 * **NOTE** The rules changed on 4/4/90. This comment reflects the
870 * new rules. The change was so that the stone-wall optimization
873 * OK, now the tough stuff. We must maintain our left and right
874 * row pointers. The rules are as follows:
879 * + If you are a clear spot, your left will point to the first
880 * stone to your left. If there is none, then point the first
881 * legal position in the row (0).
883 * + If you are a blocked spot, then your left will point to the
884 * left-most blocked spot to your left that is connected to you.
885 * This means that a left-edge (a blocked spot that has an open
886 * spot on its left) will point to itself.
891 * + If you are a clear spot, your right will point to the first
892 * stone to your right. If there is none, then point the last
893 * legal position in the row (COLNO-1).
895 * + If you are a blocked spot, then your right will point to the
896 * right-most blocked spot to your right that is connected to you.
897 * This means that a right-edge (a blocked spot that has an open
898 * spot on its right) will point to itself.
906 if (viz_clear[row][col]) return; /* already done */
908 viz_clear[row][col] = 1;
911 * Boundary cases first.
913 if (col == 0) { /* left edge */
914 if (viz_clear[row][1]) {
915 right_ptrs[row][0] = right_ptrs[row][1];
917 right_ptrs[row][0] = 1;
918 for (i = 1; i <= right_ptrs[row][1]; i++)
919 left_ptrs[row][i] = 1;
921 } else if (col == (COLNO-1)) { /* right edge */
923 if (viz_clear[row][COLNO-2]) {
924 left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
926 left_ptrs[row][COLNO-1] = COLNO-2;
927 for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
928 right_ptrs[row][i] = COLNO-2;
933 * At this point, we know we aren't on the boundaries.
935 else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
936 /* Both sides clear */
937 for (i = left_ptrs[row][col-1]; i <= col; i++) {
938 if (!viz_clear[row][i]) continue; /* catch non-end case */
939 right_ptrs[row][i] = right_ptrs[row][col+1];
941 for (i = col; i <= right_ptrs[row][col+1]; i++) {
942 if (!viz_clear[row][i]) continue; /* catch non-end case */
943 left_ptrs[row][i] = left_ptrs[row][col-1];
946 } else if (viz_clear[row][col-1]) {
947 /* Left side clear, right side blocked. */
948 for (i = col+1; i <= right_ptrs[row][col+1]; i++)
949 left_ptrs[row][i] = col+1;
951 for (i = left_ptrs[row][col-1]; i <= col; i++) {
952 if (!viz_clear[row][i]) continue; /* catch non-end case */
953 right_ptrs[row][i] = col+1;
955 left_ptrs[row][col] = left_ptrs[row][col-1];
957 } else if (viz_clear[row][col+1]) {
958 /* Right side clear, left side blocked. */
959 for (i = left_ptrs[row][col-1]; i < col; i++)
960 right_ptrs[row][i] = col-1;
962 for (i = col; i <= right_ptrs[row][col+1]; i++) {
963 if (!viz_clear[row][i]) continue; /* catch non-end case */
964 left_ptrs[row][i] = col-1;
966 right_ptrs[row][col] = right_ptrs[row][col+1];
969 /* Both sides blocked */
970 for (i = left_ptrs[row][col-1]; i < col; i++)
971 right_ptrs[row][i] = col-1;
973 for (i = col+1; i <= right_ptrs[row][col+1]; i++)
974 left_ptrs[row][i] = col+1;
976 left_ptrs[row][col] = col-1;
977 right_ptrs[row][col] = col+1;
987 if (!viz_clear[row][col]) return;
989 viz_clear[row][col] = 0;
992 if (viz_clear[row][1]) { /* adjacent is clear */
993 right_ptrs[row][0] = 0;
995 right_ptrs[row][0] = right_ptrs[row][1];
996 for (i = 1; i <= right_ptrs[row][1]; i++)
997 left_ptrs[row][i] = 0;
999 } else if (col == COLNO-1) {
1000 if (viz_clear[row][COLNO-2]) { /* adjacent is clear */
1001 left_ptrs[row][COLNO-1] = COLNO-1;
1003 left_ptrs[row][COLNO-1] = left_ptrs[row][COLNO-2];
1004 for (i = left_ptrs[row][COLNO-2]; i < COLNO-1; i++)
1005 right_ptrs[row][i] = COLNO-1;
1010 * Else we know that we are not on an edge.
1012 else if (viz_clear[row][col-1] && viz_clear[row][col+1]) {
1013 /* Both sides clear */
1014 for (i = left_ptrs[row][col-1]+1; i <= col; i++)
1015 right_ptrs[row][i] = col;
1017 if (!left_ptrs[row][col-1]) /* catch the end case */
1018 right_ptrs[row][0] = col;
1020 for (i = col; i < right_ptrs[row][col+1]; i++)
1021 left_ptrs[row][i] = col;
1023 if (right_ptrs[row][col+1] == COLNO-1) /* catch the end case */
1024 left_ptrs[row][COLNO-1] = col;
1026 } else if (viz_clear[row][col-1]) {
1027 /* Left side clear, right side blocked. */
1028 for (i = col; i <= right_ptrs[row][col+1]; i++)
1029 left_ptrs[row][i] = col;
1031 for (i = left_ptrs[row][col-1]+1; i < col; i++)
1032 right_ptrs[row][i] = col;
1034 if (!left_ptrs[row][col-1]) /* catch the end case */
1035 right_ptrs[row][i] = col;
1037 right_ptrs[row][col] = right_ptrs[row][col+1];
1039 } else if (viz_clear[row][col+1]) {
1040 /* Right side clear, left side blocked. */
1041 for (i = left_ptrs[row][col-1]; i <= col; i++)
1042 right_ptrs[row][i] = col;
1044 for (i = col+1; i < right_ptrs[row][col+1]; i++)
1045 left_ptrs[row][i] = col;
1047 if (right_ptrs[row][col+1] == COLNO-1) /* catch the end case */
1048 left_ptrs[row][i] = col;
1050 left_ptrs[row][col] = left_ptrs[row][col-1];
1053 /* Both sides blocked */
1054 for (i = left_ptrs[row][col-1]; i <= col; i++)
1055 right_ptrs[row][i] = right_ptrs[row][col+1];
1057 for (i = col; i <= right_ptrs[row][col+1]; i++)
1058 left_ptrs[row][i] = left_ptrs[row][col-1];
1063 /*===========================================================================*/
1064 /*===========================================================================*/
1065 /* Use either algorithm C or D. See the config.h for more details. =========*/
1068 * Variables local to both Algorithms C and D.
1070 static int start_row;
1071 static int start_col;
1073 static char **cs_rows;
1074 static char *cs_left;
1075 static char *cs_right;
1077 static void FDECL((*vis_func), (int,int,genericptr_t));
1078 static genericptr_t varg;
1081 * Both Algorithms C and D use the following macros.
1083 * good_row(z) - Return TRUE if the argument is a legal row.
1084 * set_cs(rowp,col) - Set the local could see array.
1085 * set_min(z) - Save the min value of the argument and the current
1087 * set_max(z) - Save the max value of the argument and the current
1090 * The last three macros depend on having local pointers row_min, row_max,
1091 * and rowp being set correctly.
1093 #define set_cs(rowp,col) (rowp[col] = COULD_SEE)
1094 #define good_row(z) ((z) >= 0 && (z) < ROWNO)
1095 #define set_min(z) if (*row_min > (z)) *row_min = (z)
1096 #define set_max(z) if (*row_max < (z)) *row_max = (z)
1097 #define is_clear(row,col) viz_clear_rows[row][col]
1100 * clear_path() expanded into 4 macros/functions:
1107 * "Draw" a line from the start to the given location. Stop if we hit
1108 * something that blocks light. The start and finish points themselves are
1109 * not checked, just the points between them. These routines do _not_
1110 * expect to be called with the same starting and stopping point.
1112 * These routines use the generalized integer Bresenham's algorithm (fast
1113 * line drawing) for all quadrants. The algorithm was taken from _Procedural
1114 * Elements for Computer Graphics_, by David F. Rogers. McGraw-Hill, 1985.
1116 #ifdef MACRO_CPATH /* quadrant calls are macros */
1119 * When called, the result is in "result".
1120 * The first two arguments (srow,scol) are one end of the path. The next
1121 * two arguments (row,col) are the destination. The last argument is
1122 * used as a C language label. This means that it must be different
1123 * in each pair of calls.
1127 * Quadrant I (step < 0).
1129 #define q1_path(srow,scol,y2,x2,label) \
1132 register int k, err, x, y, dxs, dys; \
1134 x = (scol); y = (srow); \
1135 dx = (x2) - x; dy = y - (y2); \
1137 result = 0; /* default to a blocked path */\
1139 dxs = dx << 1; /* save the shifted values */\
1144 for (k = dy-1; k; k--) { \
1151 if (!is_clear(y,x)) goto label;/* blocked */\
1156 for (k = dx-1; k; k--) { \
1163 if (!is_clear(y,x)) goto label;/* blocked */\
1171 * Quadrant IV (step > 0).
1173 #define q4_path(srow,scol,y2,x2,label) \
1176 register int k, err, x, y, dxs, dys; \
1178 x = (scol); y = (srow); \
1179 dx = (x2) - x; dy = (y2) - y; \
1181 result = 0; /* default to a blocked path */\
1183 dxs = dx << 1; /* save the shifted values */\
1188 for (k = dy-1; k; k--) { \
1195 if (!is_clear(y,x)) goto label;/* blocked */\
1201 for (k = dx-1; k; k--) { \
1208 if (!is_clear(y,x)) goto label;/* blocked */\
1216 * Quadrant II (step < 0).
1218 #define q2_path(srow,scol,y2,x2,label) \
1221 register int k, err, x, y, dxs, dys; \
1223 x = (scol); y = (srow); \
1224 dx = x - (x2); dy = y - (y2); \
1226 result = 0; /* default to a blocked path */\
1228 dxs = dx << 1; /* save the shifted values */\
1233 for (k = dy-1; k; k--) { \
1240 if (!is_clear(y,x)) goto label;/* blocked */\
1245 for (k = dx-1; k; k--) { \
1252 if (!is_clear(y,x)) goto label;/* blocked */\
1260 * Quadrant III (step > 0).
1262 #define q3_path(srow,scol,y2,x2,label) \
1265 register int k, err, x, y, dxs, dys; \
1267 x = (scol); y = (srow); \
1268 dx = x - (x2); dy = (y2) - y; \
1270 result = 0; /* default to a blocked path */\
1272 dxs = dx << 1; /* save the shifted values */\
1277 for (k = dy-1; k; k--) { \
1284 if (!is_clear(y,x)) goto label;/* blocked */\
1290 for (k = dx-1; k; k--) { \
1297 if (!is_clear(y,x)) goto label;/* blocked */\
1304 #else /* quadrants are really functions */
1306 STATIC_DCL int FDECL(_q1_path, (int,int,int,int));
1307 STATIC_DCL int FDECL(_q2_path, (int,int,int,int));
1308 STATIC_DCL int FDECL(_q3_path, (int,int,int,int));
1309 STATIC_DCL int FDECL(_q4_path, (int,int,int,int));
1311 #define q1_path(sy,sx,y,x,dummy) result = _q1_path(sy,sx,y,x)
1312 #define q2_path(sy,sx,y,x,dummy) result = _q2_path(sy,sx,y,x)
1313 #define q3_path(sy,sx,y,x,dummy) result = _q3_path(sy,sx,y,x)
1314 #define q4_path(sy,sx,y,x,dummy) result = _q4_path(sy,sx,y,x)
1317 * Quadrant I (step < 0).
1320 _q1_path(srow,scol,y2,x2)
1321 int scol, srow, y2, x2;
1324 register int k, err, x, y, dxs, dys;
1327 dx = x2 - x; dy = y - y2;
1329 dxs = dx << 1; /* save the shifted values */
1334 for (k = dy-1; k; k--) {
1341 if (!is_clear(y,x)) return 0; /* blocked */
1346 for (k = dx-1; k; k--) {
1353 if (!is_clear(y,x)) return 0;/* blocked */
1361 * Quadrant IV (step > 0).
1364 _q4_path(srow,scol,y2,x2)
1365 int scol, srow, y2, x2;
1368 register int k, err, x, y, dxs, dys;
1371 dx = x2 - x; dy = y2 - y;
1373 dxs = dx << 1; /* save the shifted values */
1378 for (k = dy-1; k; k--) {
1385 if (!is_clear(y,x)) return 0; /* blocked */
1390 for (k = dx-1; k; k--) {
1397 if (!is_clear(y,x)) return 0;/* blocked */
1405 * Quadrant II (step < 0).
1408 _q2_path(srow,scol,y2,x2)
1409 int scol, srow, y2, x2;
1412 register int k, err, x, y, dxs, dys;
1415 dx = x - x2; dy = y - y2;
1417 dxs = dx << 1; /* save the shifted values */
1422 for (k = dy-1; k; k--) {
1429 if (!is_clear(y,x)) return 0; /* blocked */
1434 for (k = dx-1; k; k--) {
1441 if (!is_clear(y,x)) return 0;/* blocked */
1449 * Quadrant III (step > 0).
1452 _q3_path(srow,scol,y2,x2)
1453 int scol, srow, y2, x2;
1456 register int k, err, x, y, dxs, dys;
1459 dx = x - x2; dy = y2 - y;
1461 dxs = dx << 1; /* save the shifted values */
1466 for (k = dy-1; k; k--) {
1473 if (!is_clear(y,x)) return 0; /* blocked */
1478 for (k = dx-1; k; k--) {
1485 if (!is_clear(y,x)) return 0;/* blocked */
1492 #endif /* quadrants are functions */
1495 * Use vision tables to determine if there is a clear path from
1496 * (col1,row1) to (col2,row2). This is used by:
1499 * do_light_sources()
1502 clear_path(col1,row1,col2,row2)
1503 int col1, row1, col2, row2;
1509 q1_path(row1,col1,row2,col2,cleardone);
1511 q4_path(row1,col1,row2,col2,cleardone);
1515 q2_path(row1,col1,row2,col2,cleardone);
1516 } else if(row1 == row2 && col1 == col2) {
1519 q3_path(row1,col1,row2,col2,cleardone);
1525 return((boolean)result);
1528 #ifdef VISION_TABLES
1529 /*===========================================================================*\
1530 GENERAL LINE OF SIGHT
1532 \*===========================================================================*/
1536 * Indicate caller for the shadow routines.
1538 #define FROM_RIGHT 0
1543 * Include the table definitions.
1545 #include "vis_tab.h"
1548 /* 3D table pointers. */
1549 static close2d *close_dy[CLOSE_MAX_BC_DY];
1550 static far2d *far_dy[FAR_MAX_BC_DY];
1552 STATIC_DCL void FDECL(right_side, (int,int,int,int,int,int,int,char*));
1553 STATIC_DCL void FDECL(left_side, (int,int,int,int,int,int,int,char*));
1554 STATIC_DCL int FDECL(close_shadow, (int,int,int,int));
1555 STATIC_DCL int FDECL(far_shadow, (int,int,int,int));
1558 * Initialize algorithm D's table pointers. If we don't have these,
1559 * then we do 3D table lookups. Verrrry slow.
1566 for (i = 0; i < CLOSE_MAX_BC_DY; i++)
1567 close_dy[i] = &close_table[i];
1569 for (i = 0; i < FAR_MAX_BC_DY; i++)
1570 far_dy[i] = &far_table[i];
1575 * If the far table has an entry of OFF_TABLE, then the far block prevents
1576 * us from seeing the location just above/below it. I.e. the first visible
1577 * location is one *before* the block.
1579 #define OFF_TABLE 0xff
1582 close_shadow(side,this_row,block_row,block_col)
1583 int side,this_row,block_row,block_col;
1585 register int sdy, sdx, pdy, offset;
1588 * If on the same column (block_row = -1), then we can see it.
1590 if (block_row < 0) return block_col;
1592 /* Take explicit absolute values. Adjust. */
1593 if ((sdy = (start_row-block_row)) < 0) sdy = -sdy; --sdy; /* src dy */
1594 if ((sdx = (start_col-block_col)) < 0) sdx = -sdx; /* src dx */
1595 if ((pdy = (block_row-this_row)) < 0) pdy = -pdy; /* point dy */
1597 if (sdy < 0 || sdy >= CLOSE_MAX_SB_DY || sdx >= CLOSE_MAX_SB_DX ||
1598 pdy >= CLOSE_MAX_BC_DY) {
1599 impossible("close_shadow: bad value");
1602 offset = close_dy[sdy]->close[sdx][pdy];
1603 if (side == FROM_RIGHT)
1604 return block_col + offset;
1606 return block_col - offset;
1611 far_shadow(side,this_row,block_row,block_col)
1612 int side,this_row,block_row,block_col;
1614 register int sdy, sdx, pdy, offset;
1617 * Take care of a bug that shows up only on the borders.
1619 * If the block is beyond the border, then the row is negative. Return
1620 * the block's column number (should be 0 or COLNO-1).
1622 * Could easily have the column be -1, but then wouldn't know if it was
1623 * the left or right border.
1625 if (block_row < 0) return block_col;
1627 /* Take explicit absolute values. Adjust. */
1628 if ((sdy = (start_row-block_row)) < 0) sdy = -sdy; /* src dy */
1629 if ((sdx = (start_col-block_col)) < 0) sdx = -sdx; --sdx; /* src dx */
1630 if ((pdy = (block_row-this_row)) < 0) pdy = -pdy; --pdy; /* point dy */
1632 if (sdy >= FAR_MAX_SB_DY || sdx < 0 || sdx >= FAR_MAX_SB_DX ||
1633 pdy < 0 || pdy >= FAR_MAX_BC_DY) {
1634 impossible("far_shadow: bad value");
1637 if ((offset = far_dy[sdy]->far_q[sdx][pdy]) == OFF_TABLE) offset = -1;
1638 if (side == FROM_RIGHT)
1639 return block_col + offset;
1641 return block_col - offset;
1648 * Figure out what could be seen on the right side of the source.
1651 right_side(row, cb_row, cb_col, fb_row, fb_col, left, right_mark, limits)
1652 int row; /* current row */
1653 int cb_row, cb_col; /* close block row and col */
1654 int fb_row, fb_col; /* far block row and col */
1655 int left; /* left mark of the previous row */
1656 int right_mark; /* right mark of previous row */
1657 char *limits; /* points at range limit for current row, or NULL */
1660 register char *rowp;
1662 int left_shadow, right_shadow, loc_right;
1663 int lblock_col; /* local block column (current row) */
1665 char *row_min; /* left most */
1666 char *row_max; /* right most */
1667 int lim_max; /* right most limit of circle */
1673 deeper = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
1675 rowp = cs_rows[row];
1676 row_min = &cs_left[row];
1677 row_max = &cs_right[row];
1680 lim_max = start_col + *limits;
1681 if(lim_max > COLNO-1) lim_max = COLNO-1;
1682 if(right_mark > lim_max) right_mark = lim_max;
1683 limits++; /* prepare for next row */
1688 * Get the left shadow from the close block. This value could be
1691 left_shadow = close_shadow(FROM_RIGHT,row,cb_row,cb_col);
1694 * Mark all stone walls as seen before the left shadow. All this work
1695 * for a special case.
1697 * NOTE. With the addition of this code in here, it is now *required*
1698 * for the algorithm to work correctly. If this is commented out,
1699 * change the above assignment so that left and not left_shadow is the
1700 * variable that gets the shadow.
1702 while (left <= right_mark) {
1703 loc_right = right_ptrs[row][left];
1704 if(loc_right > lim_max) loc_right = lim_max;
1705 if (viz_clear_rows[row][left]) {
1706 if (loc_right >= left_shadow) {
1707 left = left_shadow; /* opening ends beyond shadow */
1711 loc_right = right_ptrs[row][left];
1712 if(loc_right > lim_max) loc_right = lim_max;
1713 if (left == loc_right) return; /* boundary */
1715 /* Shadow covers opening, beyond right mark */
1716 if (left == right_mark && left_shadow > right_mark) return;
1719 if (loc_right > right_mark) /* can't see stone beyond the mark */
1720 loc_right = right_mark;
1723 for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1725 for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1726 set_min(left); set_max(loc_right);
1729 if (loc_right == right_mark) return; /* all stone */
1730 if (loc_right >= left_shadow) hit_stone = 1;
1731 left = loc_right + 1;
1735 * At this point we are at the first visible clear spot on or beyond
1736 * the left shadow, unless the left shadow is an illegal value. If we
1737 * have "hit stone" then we have a stone wall just to our left.
1741 * Get the right shadow. Make sure that it is a legal value.
1743 if ((right_shadow = far_shadow(FROM_RIGHT,row,fb_row,fb_col)) >= COLNO)
1744 right_shadow = COLNO-1;
1746 * Make vertical walls work the way we want them. In this case, we
1747 * note when the close block blocks the column just above/beneath
1748 * it (right_shadow < fb_col [actually right_shadow == fb_col-1]). If
1749 * the location is filled, then we want to see it, so we put the
1750 * right shadow back (same as fb_col).
1752 if (right_shadow < fb_col && !viz_clear_rows[row][fb_col])
1753 right_shadow = fb_col;
1754 if(right_shadow > lim_max) right_shadow = lim_max;
1757 * Main loop. Within the range of sight of the previous row, mark all
1758 * stone walls as seen. Follow open areas recursively.
1760 while (left <= right_mark) {
1761 /* Get the far right of the opening or wall */
1762 loc_right = right_ptrs[row][left];
1763 if(loc_right > lim_max) loc_right = lim_max;
1765 if (!viz_clear_rows[row][left]) {
1766 hit_stone = 1; /* use stone on this row as close block */
1768 * We can see all of the wall until the next open spot or the
1769 * start of the shadow caused by the far block (right).
1771 * Can't see stone beyond the right mark.
1773 if (loc_right > right_mark) loc_right = right_mark;
1776 for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1778 for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1779 set_min(left); set_max(loc_right);
1782 if (loc_right == right_mark) return; /* hit the end */
1783 left = loc_right + 1;
1784 loc_right = right_ptrs[row][left];
1785 if(loc_right > lim_max) loc_right = lim_max;
1786 /* fall through... we know at least one position is visible */
1790 * We are in an opening.
1792 * If this is the first open spot since the could see area (this is
1793 * true if we have hit stone), get the shadow generated by the wall
1797 lblock_col = left-1; /* local block column */
1798 left = close_shadow(FROM_RIGHT,row,row,lblock_col);
1799 if (left > lim_max) break; /* off the end */
1803 * Check if the shadow covers the opening. If it does, then
1804 * move to end of the opening. A shadow generated on from a
1805 * wall on this row does *not* cover the wall on the right
1808 if (left >= loc_right) {
1809 if (loc_right == lim_max) { /* boundary */
1810 if (left == lim_max) {
1811 if(vis_func) (*vis_func)(lim_max, row, varg);
1813 set_cs(rowp,lim_max); /* last pos */
1824 * If the far wall of the opening (loc_right) is closer than the
1825 * shadow limit imposed by the far block (right) then use the far
1826 * wall as our new far block when we recurse.
1828 * If the limits are the the same, and the far block really exists
1829 * (fb_row >= 0) then do the same as above.
1831 * Normally, the check would be for the far wall being closer OR EQUAL
1832 * to the shadow limit. However, there is a bug that arises from the
1833 * fact that the clear area pointers end in an open space (if it
1834 * exists) on a boundary. This then makes a far block exist where it
1835 * shouldn't --- on a boundary. To get around that, I had to
1836 * introduce the concept of a non-existent far block (when the
1837 * row < 0). Next I have to check for it. Here is where that check
1840 if ((loc_right < right_shadow) ||
1841 (fb_row >= 0 && loc_right == right_shadow)) {
1843 for (i = left; i <= loc_right; i++) (*vis_func)(i, row, varg);
1845 for (i = left; i <= loc_right; i++) set_cs(rowp,i);
1846 set_min(left); set_max(loc_right);
1851 right_side(nrow,row,lblock_col,row,loc_right,
1852 left,loc_right,limits);
1854 right_side(nrow,cb_row,cb_col,row,loc_right,
1855 left,loc_right,limits);
1859 * The following line, setting hit_stone, is needed for those
1860 * walls that are only 1 wide. If hit stone is *not* set and
1861 * the stone is only one wide, then the close block is the old
1862 * one instead one on the current row. A way around having to
1863 * set it here is to make left = loc_right (not loc_right+1) and
1864 * let the outer loop take care of it. However, if we do that
1865 * then we then have to check for boundary conditions here as
1873 * The opening extends beyond the right mark. This means that
1874 * the next far block is the current far block.
1878 for (i=left; i <= right_shadow; i++) (*vis_func)(i, row, varg);
1880 for (i = left; i <= right_shadow; i++) set_cs(rowp,i);
1881 set_min(left); set_max(right_shadow);
1886 right_side(nrow, row,lblock_col,fb_row,fb_col,
1887 left,right_shadow,limits);
1889 right_side(nrow,cb_row, cb_col,fb_row,fb_col,
1890 left,right_shadow,limits);
1893 return; /* we're outta here */
1902 * This routine is the mirror image of right_side(). Please see right_side()
1903 * for blow by blow comments.
1906 left_side(row, cb_row, cb_col, fb_row, fb_col, left_mark, right, limits)
1907 int row; /* the current row */
1908 int cb_row, cb_col; /* close block row and col */
1909 int fb_row, fb_col; /* far block row and col */
1910 int left_mark; /* left mark of previous row */
1911 int right; /* right mark of the previous row */
1915 register char *rowp;
1917 int left_shadow, right_shadow, loc_left;
1918 int lblock_col; /* local block column (current row) */
1920 char *row_min; /* left most */
1921 char *row_max; /* right most */
1928 deeper = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
1930 rowp = cs_rows[row];
1931 row_min = &cs_left[row];
1932 row_max = &cs_right[row];
1935 lim_min = start_col - *limits;
1936 if(lim_min < 0) lim_min = 0;
1937 if(left_mark < lim_min) left_mark = lim_min;
1938 limits++; /* prepare for next row */
1942 /* This value could be illegal. */
1943 right_shadow = close_shadow(FROM_LEFT,row,cb_row,cb_col);
1945 while ( right >= left_mark ) {
1946 loc_left = left_ptrs[row][right];
1947 if(loc_left < lim_min) loc_left = lim_min;
1948 if (viz_clear_rows[row][right]) {
1949 if (loc_left <= right_shadow) {
1950 right = right_shadow; /* opening ends beyond shadow */
1954 loc_left = left_ptrs[row][right];
1955 if(loc_left < lim_min) loc_left = lim_min;
1956 if (right == loc_left) return; /* boundary */
1959 if (loc_left < left_mark) /* can't see beyond the left mark */
1960 loc_left = left_mark;
1963 for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
1965 for (i = loc_left; i <= right; i++) set_cs(rowp,i);
1966 set_min(loc_left); set_max(right);
1969 if (loc_left == left_mark) return; /* all stone */
1970 if (loc_left <= right_shadow) hit_stone = 1;
1971 right = loc_left - 1;
1974 /* At first visible clear spot on or beyond the right shadow. */
1976 if ((left_shadow = far_shadow(FROM_LEFT,row,fb_row,fb_col)) < 0)
1979 /* Do vertical walls as we want. */
1980 if (left_shadow > fb_col && !viz_clear_rows[row][fb_col])
1981 left_shadow = fb_col;
1982 if(left_shadow < lim_min) left_shadow = lim_min;
1984 while (right >= left_mark) {
1985 loc_left = left_ptrs[row][right];
1987 if (!viz_clear_rows[row][right]) {
1988 hit_stone = 1; /* use stone on this row as close block */
1990 /* We can only see walls until the left mark */
1991 if (loc_left < left_mark) loc_left = left_mark;
1994 for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
1996 for (i = loc_left; i <= right; i++) set_cs(rowp,i);
1997 set_min(loc_left); set_max(right);
2000 if (loc_left == left_mark) return; /* hit end */
2001 right = loc_left - 1;
2002 loc_left = left_ptrs[row][right];
2003 if (loc_left < lim_min) loc_left = lim_min;
2004 /* fall through...*/
2007 /* We are in an opening. */
2009 lblock_col = right+1; /* stone block (local) */
2010 right = close_shadow(FROM_LEFT,row,row,lblock_col);
2011 if (right < lim_min) return; /* off the end */
2014 /* Check if the shadow covers the opening. */
2015 if (right <= loc_left) {
2016 /* Make a boundary condition work. */
2017 if (loc_left == lim_min) { /* at boundary */
2018 if (right == lim_min) {
2019 if(vis_func) (*vis_func)(lim_min, row, varg);
2021 set_cs(rowp,lim_min); /* caught the last pos */
2025 return; /* and break out the loop */
2032 /* If the far wall of the opening is closer than the shadow limit. */
2033 if ((loc_left > left_shadow) ||
2034 (fb_row >= 0 && loc_left == left_shadow)) {
2036 for (i = loc_left; i <= right; i++) (*vis_func)(i, row, varg);
2038 for (i = loc_left; i <= right; i++) set_cs(rowp,i);
2039 set_min(loc_left); set_max(right);
2044 left_side(nrow,row,lblock_col,row,loc_left,
2045 loc_left,right,limits);
2047 left_side(nrow,cb_row,cb_col,row,loc_left,
2048 loc_left,right,limits);
2051 hit_stone = 1; /* needed for walls of width 1 */
2054 /* The opening extends beyond the left mark. */
2057 for (i=left_shadow; i <= right; i++) (*vis_func)(i, row, varg);
2059 for (i = left_shadow; i <= right; i++) set_cs(rowp,i);
2060 set_min(left_shadow); set_max(right);
2065 left_side(nrow,row,lblock_col,fb_row,fb_col,
2066 left_shadow,right,limits);
2068 left_side(nrow,cb_row,cb_col,fb_row,fb_col,
2069 left_shadow,right,limits);
2072 return; /* we're outta here */
2081 * Calculate a view from the given location. Initialize and fill a
2082 * ROWNOxCOLNO array (could_see) with all the locations that could be
2083 * seen from the source location. Initialize and fill the left most
2084 * and right most boundaries of what could be seen.
2087 view_from(srow,scol,loc_cs_rows,left_most,right_most, range, func, arg)
2088 int srow, scol; /* source row and column */
2089 char **loc_cs_rows; /* could_see array (row pointers) */
2090 char *left_most, *right_most; /* limits of what could be seen */
2091 int range; /* 0 if unlimited */
2092 void FDECL((*func), (int,int,genericptr_t));
2097 int nrow, left, right, left_row, right_row;
2100 /* Set globals for near_shadow(), far_shadow(), etc. to use. */
2103 cs_rows = loc_cs_rows;
2104 cs_left = left_most;
2105 cs_right = right_most;
2109 /* Find the left and right limits of sight on the starting row. */
2110 if (viz_clear_rows[srow][scol]) {
2111 left = left_ptrs[srow][scol];
2112 right = right_ptrs[srow][scol];
2114 left = (!scol) ? 0 :
2115 (viz_clear_rows[srow][scol-1] ? left_ptrs[srow][scol-1] : scol-1);
2116 right = (scol == COLNO-1) ? COLNO-1 :
2117 (viz_clear_rows[srow][scol+1] ? right_ptrs[srow][scol+1] : scol+1);
2121 if(range > MAX_RADIUS || range < 1)
2122 panic("view_from called with range %d", range);
2123 limits = circle_ptr(range) + 1; /* start at next row */
2124 if(left < scol - range) left = scol - range;
2125 if(right > scol + range) right = scol + range;
2130 for (i = left; i <= right; i++) (*func)(i, srow, arg);
2132 /* Row optimization */
2133 rowp = cs_rows[srow];
2135 /* We know that we can see our row. */
2136 for (i = left; i <= right; i++) set_cs(rowp,i);
2137 cs_left[srow] = left;
2138 cs_right[srow] = right;
2141 /* The far block has a row number of -1 if we are on an edge. */
2142 right_row = (right == COLNO-1) ? -1 : srow;
2143 left_row = (!left) ? -1 : srow;
2146 * Check what could be seen in quadrants.
2148 if ( (nrow = srow+1) < ROWNO ) {
2149 step = 1; /* move down */
2151 right_side(nrow,-1,scol,right_row,right,scol,right,limits);
2153 left_side(nrow,-1,scol,left_row, left, left, scol,limits);
2156 if ( (nrow = srow-1) >= 0 ) {
2157 step = -1; /* move up */
2159 right_side(nrow,-1,scol,right_row,right,scol,right,limits);
2161 left_side(nrow,-1,scol,left_row, left, left, scol,limits);
2166 #else /*===== End of algorithm D =====*/
2169 /*===========================================================================*\
2170 GENERAL LINE OF SIGHT
2172 \*===========================================================================*/
2175 * Defines local to Algorithm C.
2177 STATIC_DCL void FDECL(right_side, (int,int,int,char*));
2178 STATIC_DCL void FDECL(left_side, (int,int,int,char*));
2180 /* Initialize algorithm C (nothing). */
2187 * Mark positions as visible on one quadrant of the right side. The
2188 * quadrant is determined by the value of the global variable step.
2191 right_side(row, left, right_mark, limits)
2192 int row; /* current row */
2193 int left; /* first (left side) visible spot on prev row */
2194 int right_mark; /* last (right side) visible spot on prev row */
2195 char *limits; /* points at range limit for current row, or NULL */
2197 int right; /* right limit of "could see" */
2198 int right_edge; /* right edge of an opening */
2199 int nrow; /* new row (calculate once) */
2200 int deeper; /* if TRUE, call self as needed */
2201 int result; /* set by q?_path() */
2202 register int i; /* loop counter */
2203 register char *rowp; /* row optimization */
2204 char *row_min; /* left most [used by macro set_min()] */
2205 char *row_max; /* right most [used by macro set_max()] */
2206 int lim_max; /* right most limit of circle */
2209 rowp = row_min = row_max = 0;
2213 * Can go deeper if the row is in bounds and the next row is within
2214 * the circle's limit. We tell the latter by checking to see if the next
2215 * limit value is the start of a new circle radius (meaning we depend
2216 * on the structure of circle_data[]).
2218 deeper = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
2220 rowp = cs_rows[row]; /* optimization */
2221 row_min = &cs_left[row];
2222 row_max = &cs_right[row];
2225 lim_max = start_col + *limits;
2226 if(lim_max > COLNO-1) lim_max = COLNO-1;
2227 if(right_mark > lim_max) right_mark = lim_max;
2228 limits++; /* prepare for next row */
2232 while (left <= right_mark) {
2233 right_edge = right_ptrs[row][left];
2234 if(right_edge > lim_max) right_edge = lim_max;
2236 if (!is_clear(row,left)) {
2238 * Jump to the far side of a stone wall. We can set all
2239 * the points in between as seen.
2241 * If the right edge goes beyond the right mark, check to see
2242 * how much we can see.
2244 if (right_edge > right_mark) {
2246 * If the mark on the previous row was a clear position,
2247 * the odds are that we can actually see part of the wall
2248 * beyond the mark on this row. If so, then see one beyond
2249 * the mark. Otherwise don't. This is a kludge so corners
2250 * with an adjacent doorway show up in nethack.
2252 right_edge = is_clear(row-step,right_mark) ?
2253 right_mark+1 : right_mark;
2256 for (i = left; i <= right_edge; i++) (*vis_func)(i, row, varg);
2258 for (i = left; i <= right_edge; i++) set_cs(rowp,i);
2259 set_min(left); set_max(right_edge);
2261 left = right_edge + 1; /* no limit check necessary */
2265 /* No checking needed if our left side is the start column. */
2266 if (left != start_col) {
2268 * Find the left side. Move right until we can see it or we run
2271 for (; left <= right_edge; left++) {
2273 q1_path(start_row,start_col,row,left,rside1);
2275 q4_path(start_row,start_col,row,left,rside1);
2277 rside1: /* used if q?_path() is a macro */
2282 * Check for boundary conditions. We *need* check (2) to break
2283 * an infinite loop where:
2285 * left == right_edge == right_mark == lim_max.
2288 if (left > lim_max) return; /* check (1) */
2289 if (left == lim_max) { /* check (2) */
2290 if(vis_func) (*vis_func)(lim_max, row, varg);
2292 set_cs(rowp,lim_max);
2298 * Check if we can see any spots in the opening. We might
2299 * (left == right_edge) or might not (left == right_edge+1) have
2300 * been able to see the far wall. Make sure we *can* see the
2301 * wall (remember, we can see the spot above/below this one)
2304 if (left >= right_edge) {
2305 left = right_edge; /* for the case left == right_edge+1 */
2311 * Find the right side. If the marker from the previous row is
2312 * closer than the edge on this row, then we have to check
2313 * how far we can see around the corner (under the overhang). Stop
2314 * at the first non-visible spot or we actually hit the far wall.
2316 * Otherwise, we know we can see the right edge of the current row.
2318 * This must be a strict less than so that we can always see a
2319 * horizontal wall, even if it is adjacent to us.
2321 if (right_mark < right_edge) {
2322 for (right = right_mark; right <= right_edge; right++) {
2324 q1_path(start_row,start_col,row,right,rside2);
2326 q4_path(start_row,start_col,row,right,rside2);
2328 rside2: /* used if q?_path() is a macro */
2331 --right; /* get rid of the last increment */
2337 * We have the range that we want. Set the bits. Note that
2338 * there is no else --- we no longer handle splinters.
2340 if (left <= right) {
2342 * An ugly special case. If you are adjacent to a vertical wall
2343 * and it has a break in it, then the right mark is set to be
2344 * start_col. We *want* to be able to see adjacent vertical
2345 * walls, so we have to set it back.
2347 if (left == right && left == start_col &&
2348 start_col < (COLNO-1) && !is_clear(row,start_col+1))
2349 right = start_col+1;
2351 if(right > lim_max) right = lim_max;
2354 for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
2356 for (i = left; i <= right; i++) set_cs(rowp,i);
2357 set_min(left); set_max(right);
2360 /* recursive call for next finger of light */
2361 if (deeper) right_side(nrow,left,right,limits);
2362 left = right + 1; /* no limit check necessary */
2369 * This routine is the mirror image of right_side(). See right_side() for
2370 * extensive comments.
2373 left_side(row, left_mark, right, limits)
2374 int row, left_mark, right;
2377 int left, left_edge, nrow, deeper, result;
2379 register char *rowp;
2380 char *row_min, *row_max;
2384 rowp = row_min = row_max = 0;
2387 deeper = good_row(nrow) && (!limits || (*limits >= *(limits+1)));
2389 rowp = cs_rows[row];
2390 row_min = &cs_left[row];
2391 row_max = &cs_right[row];
2394 lim_min = start_col - *limits;
2395 if(lim_min < 0) lim_min = 0;
2396 if(left_mark < lim_min) left_mark = lim_min;
2397 limits++; /* prepare for next row */
2401 while (right >= left_mark) {
2402 left_edge = left_ptrs[row][right];
2403 if(left_edge < lim_min) left_edge = lim_min;
2405 if (!is_clear(row,right)) {
2406 /* Jump to the far side of a stone wall. */
2407 if (left_edge < left_mark) {
2408 /* Maybe see more (kludge). */
2409 left_edge = is_clear(row-step,left_mark) ?
2410 left_mark-1 : left_mark;
2413 for (i = left_edge; i <= right; i++) (*vis_func)(i, row, varg);
2415 for (i = left_edge; i <= right; i++) set_cs(rowp,i);
2416 set_min(left_edge); set_max(right);
2418 right = left_edge - 1; /* no limit check necessary */
2422 if (right != start_col) {
2423 /* Find the right side. */
2424 for (; right >= left_edge; right--) {
2426 q2_path(start_row,start_col,row,right,lside1);
2428 q3_path(start_row,start_col,row,right,lside1);
2430 lside1: /* used if q?_path() is a macro */
2434 /* Check for boundary conditions. */
2435 if (right < lim_min) return;
2436 if (right == lim_min) {
2437 if(vis_func) (*vis_func)(lim_min, row, varg);
2439 set_cs(rowp,lim_min);
2444 /* Check if we can see any spots in the opening. */
2445 if (right <= left_edge) {
2451 /* Find the left side. */
2452 if (left_mark > left_edge) {
2453 for (left = left_mark; left >= left_edge; --left) {
2455 q2_path(start_row,start_col,row,left,lside2);
2457 q3_path(start_row,start_col,row,left,lside2);
2459 lside2: /* used if q?_path() is a macro */
2462 left++; /* get rid of the last decrement */
2467 if (left <= right) {
2468 /* An ugly special case. */
2469 if (left == right && right == start_col &&
2470 start_col > 0 && !is_clear(row,start_col-1))
2473 if(left < lim_min) left = lim_min;
2475 for (i = left; i <= right; i++) (*vis_func)(i, row, varg);
2477 for (i = left; i <= right; i++) set_cs(rowp,i);
2478 set_min(left); set_max(right);
2482 if (deeper) left_side(nrow,left,right,limits);
2483 right = left - 1; /* no limit check necessary */
2490 * Calculate all possible visible locations from the given location
2491 * (srow,scol). NOTE this is (y,x)! Mark the visible locations in the
2495 view_from(srow, scol, loc_cs_rows, left_most, right_most, range, func, arg)
2496 int srow, scol; /* starting row and column */
2497 char **loc_cs_rows; /* pointers to the rows of the could_see array */
2498 char *left_most; /* min mark on each row */
2499 char *right_most; /* max mark on each row */
2500 int range; /* 0 if unlimited */
2501 void FDECL((*func), (int,int,genericptr_t));
2504 register int i; /* loop counter */
2505 char *rowp; /* optimization for setting could_see */
2506 int nrow; /* the next row */
2507 int left; /* the left-most visible column */
2508 int right; /* the right-most visible column */
2509 char *limits; /* range limit for next row */
2511 /* Set globals for q?_path(), left_side(), and right_side() to use. */
2514 cs_rows = loc_cs_rows; /* 'could see' rows */
2515 cs_left = left_most;
2516 cs_right = right_most;
2521 * Determine extent of sight on the starting row.
2523 if (is_clear(srow,scol)) {
2524 left = left_ptrs[srow][scol];
2525 right = right_ptrs[srow][scol];
2528 * When in stone, you can only see your adjacent squares, unless
2529 * you are on an array boundary or a stone/clear boundary.
2531 left = (!scol) ? 0 :
2532 (is_clear(srow,scol-1) ? left_ptrs[srow][scol-1] : scol-1);
2533 right = (scol == COLNO-1) ? COLNO-1 :
2534 (is_clear(srow,scol+1) ? right_ptrs[srow][scol+1] : scol+1);
2538 if(range > MAX_RADIUS || range < 1)
2539 panic("view_from called with range %d", range);
2540 limits = circle_ptr(range) + 1; /* start at next row */
2541 if(left < scol - range) left = scol - range;
2542 if(right > scol + range) right = scol + range;
2547 for (i = left; i <= right; i++) (*func)(i, srow, arg);
2549 /* Row pointer optimization. */
2550 rowp = cs_rows[srow];
2552 /* We know that we can see our row. */
2553 for (i = left; i <= right; i++) set_cs(rowp,i);
2554 cs_left[srow] = left;
2555 cs_right[srow] = right;
2559 * Check what could be seen in quadrants. We need to check for valid
2560 * rows here, since we don't do it in the routines right_side() and
2561 * left_side() [ugliness to remove extra routine calls].
2563 if ( (nrow = srow+1) < ROWNO ) { /* move down */
2565 if (scol < COLNO-1) right_side(nrow, scol, right, limits);
2566 if (scol) left_side (nrow, left, scol, limits);
2569 if ( (nrow = srow-1) >= 0 ) { /* move up */
2571 if (scol < COLNO-1) right_side(nrow, scol, right, limits);
2572 if (scol) left_side (nrow, left, scol, limits);
2576 #endif /*===== End of algorithm C =====*/
2579 * AREA OF EFFECT "ENGINE"
2581 * Calculate all possible visible locations as viewed from the given location
2582 * (srow,scol) within the range specified. Perform "func" with (x, y) args and
2583 * additional argument "arg" for each square.
2585 * If not centered on the hero, just forward arguments to view_from(); it
2586 * will call "func" when necessary. If the hero is the center, use the
2587 * vision matrix and reduce extra work.
2590 do_clear_area(scol,srow,range,func,arg)
2591 int scol, srow, range;
2592 void FDECL((*func), (int,int,genericptr_t));
2595 /* If not centered on hero, do the hard work of figuring the area */
2596 if (scol != u.ux || srow != u.uy)
2597 view_from(srow, scol, (char **)0, (char *)0, (char *)0,
2601 int y, min_x, max_x, max_y, offset;
2604 if (range > MAX_RADIUS || range < 1)
2605 panic("do_clear_area: illegal range %d", range);
2606 if(vision_full_recalc)
2607 vision_recalc(0); /* recalc vision if dirty */
2608 limits = circle_ptr(range);
2609 if ((max_y = (srow + range)) >= ROWNO) max_y = ROWNO-1;
2610 if ((y = (srow - range)) < 0) y = 0;
2611 for (; y <= max_y; y++) {
2612 offset = limits[v_abs(y-srow)];
2613 if((min_x = (scol - offset)) < 0) min_x = 0;
2614 if((max_x = (scol + offset)) >= COLNO) max_x = COLNO-1;
2615 for (x = min_x; x <= max_x; x++)