OSDN Git Service

幻覚状態でもMMで正しいアイテム名が表示されていたので、「何か奇妙な物」
[hengbandforosx/hengbandosx.git] / src / cave.c
1 /* File: cave.c */
2
3 /* Purpose: low level dungeon routines -BEN- */
4
5
6 #include "angband.h"
7
8
9 /*
10  * Support for Adam Bolt's tileset, lighting and transparency effects
11  * by Robert Ruehlmann (rr9@angband.org)
12  */
13
14 static byte display_autopick;
15 static int match_autopick;
16 static object_type *autopick_obj;
17 static int feat_priority;
18
19 /*
20  * Distance between two points via Newton-Raphson technique
21  */
22 int distance (int y1, int x1, int y2, int x2)
23 {
24         int dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
25         int dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
26
27         /* Squared distance */
28         int target = (dy * dy) + (dx * dx);
29
30         /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */
31         int d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
32
33         int err;
34
35         /* Simple case */
36         if (!dy || !dx) return d;
37
38         while (1)
39         {
40                 /* Approximate error */
41                 err = (target - d * d) / (2 * d);
42
43                 /* No error - we are done */
44                 if (!err) break;
45
46                 /* Adjust distance */
47                 d += err;
48         }
49
50         return d;
51 }
52
53
54 /*
55  * Return TRUE if the given feature is a trap
56  */
57 bool is_trap(int feat)
58 {
59         switch (feat)
60         {
61                 case FEAT_TRAP_TRAPDOOR:
62                 case FEAT_TRAP_PIT:
63                 case FEAT_TRAP_SPIKED_PIT:
64                 case FEAT_TRAP_POISON_PIT:
65                 case FEAT_TRAP_TY_CURSE:
66                 case FEAT_TRAP_TELEPORT:
67                 case FEAT_TRAP_FIRE:
68                 case FEAT_TRAP_ACID:
69                 case FEAT_TRAP_SLOW:
70                 case FEAT_TRAP_LOSE_STR:
71                 case FEAT_TRAP_LOSE_DEX:
72                 case FEAT_TRAP_LOSE_CON:
73                 case FEAT_TRAP_BLIND:
74                 case FEAT_TRAP_CONFUSE:
75                 case FEAT_TRAP_POISON:
76                 case FEAT_TRAP_SLEEP:
77                 case FEAT_TRAP_TRAPS:
78                 {
79                         /* A trap */
80                         return (TRUE);
81                 }
82                 default:
83                 {
84                         /* Not a trap */
85                         return (FALSE);
86                 }
87         }
88 }
89
90
91 /*
92  * A simple, fast, integer-based line-of-sight algorithm.  By Joseph Hall,
93  * 4116 Brewster Drive, Raleigh NC 27606.  Email to jnh@ecemwl.ncsu.edu.
94  *
95  * Returns TRUE if a line of sight can be traced from (x1,y1) to (x2,y2).
96  *
97  * The LOS begins at the center of the tile (x1,y1) and ends at the center of
98  * the tile (x2,y2).  If los() is to return TRUE, all of the tiles this line
99  * passes through must be floor tiles, except for (x1,y1) and (x2,y2).
100  *
101  * We assume that the "mathematical corner" of a non-floor tile does not
102  * block line of sight.
103  *
104  * Because this function uses (short) ints for all calculations, overflow may
105  * occur if dx and dy exceed 90.
106  *
107  * Once all the degenerate cases are eliminated, the values "qx", "qy", and
108  * "m" are multiplied by a scale factor "f1 = abs(dx * dy * 2)", so that
109  * we can use integer arithmetic.
110  *
111  * We travel from start to finish along the longer axis, starting at the border
112  * between the first and second tiles, where the y offset = .5 * slope, taking
113  * into account the scale factor.  See below.
114  *
115  * Also note that this function and the "move towards target" code do NOT
116  * share the same properties.  Thus, you can see someone, target them, and
117  * then fire a bolt at them, but the bolt may hit a wall, not them.  However,
118  * by clever choice of target locations, you can sometimes throw a "curve".
119  *
120  * Note that "line of sight" is not "reflexive" in all cases.
121  *
122  * Use the "projectable()" routine to test "spell/missile line of sight".
123  *
124  * Use the "update_view()" function to determine player line-of-sight.
125  */
126 bool los(int y1, int x1, int y2, int x2)
127 {
128         /* Delta */
129         int dx, dy;
130
131         /* Absolute */
132         int ax, ay;
133
134         /* Signs */
135         int sx, sy;
136
137         /* Fractions */
138         int qx, qy;
139
140         /* Scanners */
141         int tx, ty;
142
143         /* Scale factors */
144         int f1, f2;
145
146         /* Slope, or 1/Slope, of LOS */
147         int m;
148
149
150         /* Extract the offset */
151         dy = y2 - y1;
152         dx = x2 - x1;
153
154         /* Extract the absolute offset */
155         ay = ABS(dy);
156         ax = ABS(dx);
157
158
159         /* Handle adjacent (or identical) grids */
160         if ((ax < 2) && (ay < 2)) return (TRUE);
161
162
163         /* Paranoia -- require "safe" origin */
164         /* if (!in_bounds(y1, x1)) return (FALSE); */
165         /* if (!in_bounds(y2, x2)) return (FALSE); */
166
167
168         /* Directly South/North */
169         if (!dx)
170         {
171                 /* South -- check for walls */
172                 if (dy > 0)
173                 {
174                         for (ty = y1 + 1; ty < y2; ty++)
175                         {
176                                 if (!cave_floor_bold(ty, x1)) return (FALSE);
177                         }
178                 }
179
180                 /* North -- check for walls */
181                 else
182                 {
183                         for (ty = y1 - 1; ty > y2; ty--)
184                         {
185                                 if (!cave_floor_bold(ty, x1)) return (FALSE);
186                         }
187                 }
188
189                 /* Assume los */
190                 return (TRUE);
191         }
192
193         /* Directly East/West */
194         if (!dy)
195         {
196                 /* East -- check for walls */
197                 if (dx > 0)
198                 {
199                         for (tx = x1 + 1; tx < x2; tx++)
200                         {
201                                 if (!cave_floor_bold(y1, tx)) return (FALSE);
202                         }
203                 }
204
205                 /* West -- check for walls */
206                 else
207                 {
208                         for (tx = x1 - 1; tx > x2; tx--)
209                         {
210                                 if (!cave_floor_bold(y1, tx)) return (FALSE);
211                         }
212                 }
213
214                 /* Assume los */
215                 return (TRUE);
216         }
217
218
219         /* Extract some signs */
220         sx = (dx < 0) ? -1 : 1;
221         sy = (dy < 0) ? -1 : 1;
222
223
224         /* Vertical "knights" */
225         if (ax == 1)
226         {
227                 if (ay == 2)
228                 {
229                         if (cave_floor_bold(y1 + sy, x1)) return (TRUE);
230                 }
231         }
232
233         /* Horizontal "knights" */
234         else if (ay == 1)
235         {
236                 if (ax == 2)
237                 {
238                         if (cave_floor_bold(y1, x1 + sx)) return (TRUE);
239                 }
240         }
241
242
243         /* Calculate scale factor div 2 */
244         f2 = (ax * ay);
245
246         /* Calculate scale factor */
247         f1 = f2 << 1;
248
249
250         /* Travel horizontally */
251         if (ax >= ay)
252         {
253                 /* Let m = dy / dx * 2 * (dy * dx) = 2 * dy * dy */
254                 qy = ay * ay;
255                 m = qy << 1;
256
257                 tx = x1 + sx;
258
259                 /* Consider the special case where slope == 1. */
260                 if (qy == f2)
261                 {
262                         ty = y1 + sy;
263                         qy -= f1;
264                 }
265                 else
266                 {
267                         ty = y1;
268                 }
269
270                 /* Note (below) the case (qy == f2), where */
271                 /* the LOS exactly meets the corner of a tile. */
272                 while (x2 - tx)
273                 {
274                         if (!cave_floor_bold(ty, tx)) return (FALSE);
275
276                         qy += m;
277
278                         if (qy < f2)
279                         {
280                                 tx += sx;
281                         }
282                         else if (qy > f2)
283                         {
284                                 ty += sy;
285                                 if (!cave_floor_bold(ty, tx)) return (FALSE);
286                                 qy -= f1;
287                                 tx += sx;
288                         }
289                         else
290                         {
291                                 ty += sy;
292                                 qy -= f1;
293                                 tx += sx;
294                         }
295                 }
296         }
297
298         /* Travel vertically */
299         else
300         {
301                 /* Let m = dx / dy * 2 * (dx * dy) = 2 * dx * dx */
302                 qx = ax * ax;
303                 m = qx << 1;
304
305                 ty = y1 + sy;
306
307                 if (qx == f2)
308                 {
309                         tx = x1 + sx;
310                         qx -= f1;
311                 }
312                 else
313                 {
314                         tx = x1;
315                 }
316
317                 /* Note (below) the case (qx == f2), where */
318                 /* the LOS exactly meets the corner of a tile. */
319                 while (y2 - ty)
320                 {
321                         if (!cave_floor_bold(ty, tx)) return (FALSE);
322
323                         qx += m;
324
325                         if (qx < f2)
326                         {
327                                 ty += sy;
328                         }
329                         else if (qx > f2)
330                         {
331                                 tx += sx;
332                                 if (!cave_floor_bold(ty, tx)) return (FALSE);
333                                 qx -= f1;
334                                 ty += sy;
335                         }
336                         else
337                         {
338                                 tx += sx;
339                                 qx -= f1;
340                                 ty += sy;
341                         }
342                 }
343         }
344
345         /* Assume los */
346         return (TRUE);
347 }
348
349
350
351
352
353
354 /*
355  * Can the player "see" the given grid in detail?
356  *
357  * He must have vision, illumination, and line of sight.
358  *
359  * Note -- "CAVE_LITE" is only set if the "torch" has "los()".
360  * So, given "CAVE_LITE", we know that the grid is "fully visible".
361  *
362  * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean
363  * that a wall is visible from any direction.  That would be odd.  Except
364  * under wizard light, which might make sense.  Thus, for walls, we require
365  * not only that they be "CAVE_GLOW", but also, that they be adjacent to a
366  * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is
367  * in line of sight of the player.
368  *
369  * This extra check is expensive, but it provides a more "correct" semantics.
370  *
371  * Note that we should not run this check on walls which are "outer walls" of
372  * the dungeon, or we will induce a memory fault, but actually verifying all
373  * of the locations would be extremely expensive.
374  *
375  * Thus, to speed up the function, we assume that all "perma-walls" which are
376  * "CAVE_GLOW" are "illuminated" from all sides.  This is correct for all cases
377  * except "vaults" and the "buildings" in town.  But the town is a hack anyway,
378  * and the player has more important things on his mind when he is attacking a
379  * monster vault.  It is annoying, but an extremely important optimization.
380  *
381  * Note that "glowing walls" are only considered to be "illuminated" if the
382  * grid which is next to the wall in the direction of the player is also a
383  * "glowing" grid.  This prevents the player from being able to "see" the
384  * walls of illuminated rooms from a corridor outside the room.
385  */
386 bool player_can_see_bold(int y, int x)
387 {
388         int xx, yy;
389
390         cave_type *c_ptr;
391
392         /* Blind players see nothing */
393         if (p_ptr->blind) return (FALSE);
394
395         /* Access the cave grid */
396         c_ptr = &cave[y][x];
397
398         /* Note that "torch-lite" yields "illumination" */
399         if (c_ptr->info & (CAVE_LITE)) return (TRUE);
400
401         /* Require line of sight to the grid */
402         if (!player_has_los_bold(y, x)) return (FALSE);
403
404         if (p_ptr->pclass == CLASS_NINJA) return TRUE;
405
406         /* Require "perma-lite" of the grid */
407         if (!(c_ptr->info & (CAVE_GLOW | CAVE_MNLT))) return (FALSE);
408
409         /* Floors are simple */
410         if (cave_floor_bold(y, x)) return (TRUE);
411
412         /* Hack -- move towards player */
413         yy = (y < py) ? (y + 1) : (y > py) ? (y - 1) : y;
414         xx = (x < px) ? (x + 1) : (x > px) ? (x - 1) : x;
415
416         /* Check for "local" illumination */
417         if (cave[yy][xx].info & (CAVE_GLOW | CAVE_MNLT))
418         {
419                 /* Assume the wall is really illuminated */
420                 return (TRUE);
421         }
422
423         /* Assume not visible */
424         return (FALSE);
425 }
426
427
428
429 /*
430  * Returns true if the player's grid is dark
431  */
432 bool no_lite(void)
433 {
434         return (!player_can_see_bold(py, px));
435 }
436
437
438
439
440 /*
441  * Determine if a given location may be "destroyed"
442  *
443  * Used by destruction spells, and for placing stairs, etc.
444  */
445 bool cave_valid_bold(int y, int x)
446 {
447         cave_type *c_ptr = &cave[y][x];
448
449         s16b this_o_idx, next_o_idx = 0;
450
451
452         /* Forbid perma-grids */
453         if (cave_perma_grid(c_ptr)) return (FALSE);
454
455         /* Check objects */
456         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
457         {
458                 object_type *o_ptr;
459
460                 /* Acquire object */
461                 o_ptr = &o_list[this_o_idx];
462
463                 /* Acquire next object */
464                 next_o_idx = o_ptr->next_o_idx;
465
466                 /* Forbid artifact grids */
467                 if ((o_ptr->art_name) || artifact_p(o_ptr)) return (FALSE);
468         }
469
470         /* Accept */
471         return (TRUE);
472 }
473
474
475
476
477 /*
478  * Determine if a given location may be "destroyed"
479  *
480  * Used by destruction spells, and for placing stairs, etc.
481  */
482 bool cave_valid_grid(cave_type *c_ptr)
483 {
484         s16b this_o_idx, next_o_idx = 0;
485
486
487         /* Forbid perma-grids */
488         if (cave_perma_grid(c_ptr)) return (FALSE);
489
490         /* Check objects */
491         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
492         {
493                 object_type *o_ptr;
494
495                 /* Acquire object */
496                 o_ptr = &o_list[this_o_idx];
497
498                 /* Acquire next object */
499                 next_o_idx = o_ptr->next_o_idx;
500
501                 /* Forbid artifact grids */
502                 if ((o_ptr->art_name) || artifact_p(o_ptr)) return (FALSE);
503         }
504
505         /* Accept */
506         return (TRUE);
507 }
508
509
510
511
512 /*
513  * Hack -- Legal monster codes
514  */
515 static cptr image_monster_hack = \
516 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
517
518 /*
519  * Hack -- Legal monster codes for IBM pseudo-graphics
520  */
521 static cptr image_monster_hack_ibm = \
522 "aaa";
523
524 /*
525  * Mega-Hack -- Hallucinatory monster
526  */
527 static void image_monster(byte *ap, char *cp)
528 {
529         int n = strlen(image_monster_hack);
530
531         /* Random symbol from set above */
532         if (use_graphics)
533         {
534                 /* Normal graphics */
535                 if (!(streq(ANGBAND_SYS, "ibm")))
536                 {
537                         (*cp) = r_info[randint1(max_r_idx-1)].x_char;
538                         (*ap) = r_info[randint1(max_r_idx-1)].x_attr;
539                 }
540                 else
541                 /* IBM-pseudo graphics */
542                 {
543                         n = strlen(image_monster_hack_ibm);
544                         (*cp) = (image_monster_hack_ibm[randint0(n)]);
545
546                         /* Random color */
547                         (*ap) = randint1(15);
548                 }
549         }
550         else
551         /* Text mode */
552         {
553                 (*cp) = (image_monster_hack[randint0(n)]);
554
555                 /* Random color */
556                 (*ap) = randint1(15);
557         }
558 }
559
560
561
562 /*
563  * Hack -- Legal object codes
564  */
565 static cptr image_object_hack = \
566 "?/|\\\"!$()_-=[]{},~";
567
568 static cptr image_object_hack_ibm = \
569 "aaa";
570
571 /*
572  * Mega-Hack -- Hallucinatory object
573  */
574 static void image_object(byte *ap, char *cp)
575 {
576         int n = strlen(image_object_hack);
577
578         if (use_graphics)
579         {
580                 if (!(streq(ANGBAND_SYS, "ibm")))
581                 {
582                         (*cp) = k_info[randint1(max_k_idx-1)].x_char;
583                         (*ap) = k_info[randint1(max_k_idx-1)].x_attr;
584                 }
585                 else
586                 {
587                         n = strlen(image_object_hack_ibm);
588                         (*cp) = (image_object_hack_ibm[randint0(n)]);
589
590                         /* Random color */
591                         (*ap) = randint1(15);
592                 }
593         }
594         else
595         {
596                 (*cp) = (image_object_hack[randint0(n)]);
597
598                 /* Random color */
599                 (*ap) = randint1(15);
600         }
601 }
602
603
604
605 /*
606  * Hack -- Random hallucination
607  */
608 static void image_random(byte *ap, char *cp)
609 {
610         /* Normally, assume monsters */
611         if (randint0(100) < 75)
612         {
613                 image_monster(ap, cp);
614         }
615
616         /* Otherwise, assume objects */
617         else
618         {
619                 image_object(ap, cp);
620         }
621 }
622
623 /*
624  * Not using graphical tiles for this feature?
625  */
626 #define is_ascii_graphics(C , A) \
627     (!(((C) & 0x80) && ((A) & 0x80)))
628
629 /*
630  * The 16x16 tile of the terrain supports lighting
631  */
632 static bool feat_supports_lighting(byte feat)
633 {
634         if (is_trap(feat)) return TRUE;
635
636         switch (feat)
637         {
638         case FEAT_FLOOR:
639         case FEAT_INVIS:
640         case FEAT_GLYPH:
641         case FEAT_LESS:
642         case FEAT_MORE:
643         case FEAT_LESS_LESS:
644         case FEAT_MORE_MORE:
645         case FEAT_SECRET:
646         case FEAT_RUBBLE:
647         case FEAT_MAGMA:
648         case FEAT_QUARTZ:
649         case FEAT_MAGMA_H:
650         case FEAT_QUARTZ_H:
651         case FEAT_MAGMA_K:
652         case FEAT_QUARTZ_K:
653         case FEAT_WALL_EXTRA:
654         case FEAT_WALL_INNER:
655         case FEAT_WALL_OUTER:
656         case FEAT_WALL_SOLID:
657         case FEAT_PERM_EXTRA:
658         case FEAT_PERM_INNER:
659         case FEAT_PERM_OUTER:
660         case FEAT_PERM_SOLID:
661         case FEAT_MINOR_GLYPH:
662         case FEAT_DEEP_WATER:
663         case FEAT_SHAL_WATER:
664         case FEAT_DEEP_LAVA:
665         case FEAT_SHAL_LAVA:
666         case FEAT_DARK_PIT:
667         case FEAT_DIRT:
668         case FEAT_GRASS:
669         case FEAT_FLOWER:
670         case FEAT_DEEP_GRASS:
671         case FEAT_TREES:
672         case FEAT_MOUNTAIN:
673         case FEAT_MIRROR:
674                 return TRUE;
675         default:
676                 return FALSE;
677         }
678 }
679
680 /*
681  * This array lists the effects of "brightness" on various "base" colours.
682  *
683  * This is used to do dynamic lighting effects in ascii :-)
684  * At the moment, only the various "floor" tiles are affected.
685  *
686  * The layout of the array is [x][0] = light and [x][1] = dark.
687  */
688
689 static byte lighting_colours[16][2] =
690 {
691         /* TERM_DARK */
692         {TERM_L_DARK, TERM_DARK},
693
694         /* TERM_WHITE */
695         {TERM_YELLOW, TERM_SLATE},
696
697         /* TERM_SLATE */
698         {TERM_WHITE, TERM_L_DARK},
699
700         /* TERM_ORANGE */
701         {TERM_YELLOW, TERM_UMBER},
702
703         /* TERM_RED */
704         {TERM_RED, TERM_RED},
705
706         /* TERM_GREEN */
707         {TERM_L_GREEN, TERM_GREEN},
708
709         /* TERM_BLUE */
710         {TERM_BLUE, TERM_BLUE},
711
712         /* TERM_UMBER */
713         {TERM_L_UMBER, TERM_RED},
714
715         /* TERM_L_DARK */
716         {TERM_SLATE, TERM_L_DARK},
717
718         /* TERM_L_WHITE */
719         {TERM_WHITE, TERM_SLATE},
720
721         /* TERM_VIOLET */
722         {TERM_L_RED, TERM_BLUE},
723
724         /* TERM_YELLOW */
725         {TERM_YELLOW, TERM_ORANGE},
726
727         /* TERM_L_RED */
728         {TERM_L_RED, TERM_L_RED},
729
730         /* TERM_L_GREEN */
731         {TERM_L_GREEN, TERM_GREEN},
732
733         /* TERM_L_BLUE */
734         {TERM_L_BLUE, TERM_L_BLUE},
735
736         /* TERM_L_UMBER */
737         {TERM_YELLOW, TERM_UMBER}
738 };
739
740 /*
741  * Extract the attr/char to display at the given (legal) map location
742  *
743  * Basically, we "paint" the chosen attr/char in several passes, starting
744  * with any known "terrain features" (defaulting to darkness), then adding
745  * any known "objects", and finally, adding any known "monsters".  This
746  * is not the fastest method but since most of the calls to this function
747  * are made for grids with no monsters or objects, it is fast enough.
748  *
749  * Note that this function, if used on the grid containing the "player",
750  * will return the attr/char of the grid underneath the player, and not
751  * the actual player attr/char itself, allowing a lot of optimization
752  * in various "display" functions.
753  *
754  * Note that the "zero" entry in the feature/object/monster arrays are
755  * used to provide "special" attr/char codes, with "monster zero" being
756  * used for the player attr/char, "object zero" being used for the "stack"
757  * attr/char, and "feature zero" being used for the "nothing" attr/char,
758  * though this function makes use of only "feature zero".
759  *
760  * Note that monsters can have some "special" flags, including "ATTR_MULTI",
761  * which means their color changes, and "ATTR_CLEAR", which means they take
762  * the color of whatever is under them, and "CHAR_CLEAR", which means that
763  * they take the symbol of whatever is under them.  Technically, the flag
764  * "CHAR_MULTI" is supposed to indicate that a monster looks strange when
765  * examined, but this flag is currently ignored.
766  *
767  * Currently, we do nothing with multi-hued objects, because there are
768  * not any.  If there were, they would have to set "shimmer_objects"
769  * when they were created, and then new "shimmer" code in "dungeon.c"
770  * would have to be created handle the "shimmer" effect, and the code
771  * in "cave.c" would have to be updated to create the shimmer effect.
772  *
773  * Note the effects of hallucination.  Objects always appear as random
774  * "objects", monsters as random "monsters", and normal grids occasionally
775  * appear as random "monsters" or "objects", but note that these random
776  * "monsters" and "objects" are really just "colored ascii symbols".
777  *
778  * Note that "floors" and "invisible traps" (and "zero" features) are
779  * drawn as "floors" using a special check for optimization purposes,
780  * and these are the only features which get drawn using the special
781  * lighting effects activated by "view_special_lite".
782  *
783  * Note the use of the "mimic" field in the "terrain feature" processing,
784  * which allows any feature to "pretend" to be another feature.  This is
785  * used to "hide" secret doors, and to make all "doors" appear the same,
786  * and all "walls" appear the same, and "hidden" treasure stay hidden.
787  * It is possible to use this field to make a feature "look" like a floor,
788  * but the "special lighting effects" for floors will not be used.
789  *
790  * Note the use of the new "terrain feature" information.  Note that the
791  * assumption that all interesting "objects" and "terrain features" are
792  * memorized allows extremely optimized processing below.  Note the use
793  * of separate flags on objects to mark them as memorized allows a grid
794  * to have memorized "terrain" without granting knowledge of any object
795  * which may appear in that grid.
796  *
797  * Note the efficient code used to determine if a "floor" grid is
798  * "memorized" or "viewable" by the player, where the test for the
799  * grid being "viewable" is based on the facts that (1) the grid
800  * must be "lit" (torch-lit or perma-lit), (2) the grid must be in
801  * line of sight, and (3) the player must not be blind, and uses the
802  * assumption that all torch-lit grids are in line of sight.
803  *
804  * Note that floors (and invisible traps) are the only grids which are
805  * not memorized when seen, so only these grids need to check to see if
806  * the grid is "viewable" to the player (if it is not memorized).  Since
807  * most non-memorized grids are in fact walls, this induces *massive*
808  * efficiency, at the cost of *forcing* the memorization of non-floor
809  * grids when they are first seen.  Note that "invisible traps" are
810  * always treated exactly like "floors", which prevents "cheating".
811  *
812  * Note the "special lighting effects" which can be activated for floor
813  * grids using the "view_special_lite" option (for "white" floor grids),
814  * causing certain grids to be displayed using special colors.  If the
815  * player is "blind", we will use "dark gray", else if the grid is lit
816  * by the torch, and the "view_yellow_lite" option is set, we will use
817  * "yellow", else if the grid is "dark", we will use "dark gray", else
818  * if the grid is not "viewable", and the "view_bright_lite" option is
819  * set, and the we will use "slate" (gray).  We will use "white" for all
820  * other cases, in particular, for illuminated viewable floor grids.
821  *
822  * Note the "special lighting effects" which can be activated for wall
823  * grids using the "view_granite_lite" option (for "white" wall grids),
824  * causing certain grids to be displayed using special colors.  If the
825  * player is "blind", we will use "dark gray", else if the grid is lit
826  * by the torch, and the "view_yellow_lite" option is set, we will use
827  * "yellow", else if the "view_bright_lite" option is set, and the grid
828  * is not "viewable", or is "dark", or is glowing, but not when viewed
829  * from the player's current location, we will use "slate" (gray).  We
830  * will use "white" for all other cases, in particular, for correctly
831  * illuminated viewable wall grids.
832  *
833  * Note that, when "view_granite_lite" is set, we use an inline version
834  * of the "player_can_see_bold()" function to check the "viewability" of
835  * grids when the "view_bright_lite" option is set, and we do NOT use
836  * any special colors for "dark" wall grids, since this would allow the
837  * player to notice the walls of illuminated rooms from a hallway that
838  * happened to run beside the room.  The alternative, by the way, would
839  * be to prevent the generation of hallways next to rooms, but this
840  * would still allow problems when digging towards a room.
841  *
842  * Note that bizarre things must be done when the "attr" and/or "char"
843  * codes have the "high-bit" set, since these values are used to encode
844  * various "special" pictures in some versions, and certain situations,
845  * such as "multi-hued" or "clear" monsters, cause the attr/char codes
846  * to be "scrambled" in various ways.
847  *
848  * Note that eventually we may use the "&" symbol for embedded treasure,
849  * and use the "*" symbol to indicate multiple objects, though this will
850  * have to wait for Angband 2.8.0 or later.  Note that currently, this
851  * is not important, since only one object or terrain feature is allowed
852  * in each grid.  If needed, "k_info[0]" will hold the "stack" attr/char.
853  *
854  * Note the assumption that doing "x_ptr = &x_info[x]" plus a few of
855  * "x_ptr->xxx", is quicker than "x_info[x].xxx", if this is incorrect
856  * then a whole lot of code should be changed...  XXX XXX
857  */
858 #ifdef USE_TRANSPARENCY
859 void map_info(int y, int x, byte *ap, char *cp, byte *tap, char *tcp)
860 #else /* USE_TRANSPARENCY */
861 void map_info(int y, int x, byte *ap, char *cp)
862 #endif /* USE_TRANSPARENCY */
863 {
864         cave_type *c_ptr;
865
866         feature_type *f_ptr;
867
868         s16b this_o_idx, next_o_idx = 0;
869
870         byte feat;
871
872         byte a;
873         byte c;
874
875         /* Get the cave */
876         c_ptr = &cave[y][x];
877
878         /* Feature code */
879         feat = c_ptr->feat;
880
881         /* Floors (etc) */
882         if ((feat <= FEAT_INVIS) || (feat == FEAT_DIRT) || (feat == FEAT_GRASS))
883         {
884                 /* Memorized (or visible) floor */
885                 if   ((c_ptr->info & CAVE_MARK) ||
886                     (((c_ptr->info & CAVE_LITE) || (c_ptr->info & CAVE_MNLT) ||
887                      ((c_ptr->info & CAVE_GLOW) &&
888                       (c_ptr->info & CAVE_VIEW))) &&
889                      !p_ptr->blind))
890                 {
891                         /* Access floor */
892                         f_ptr = &f_info[feat];
893
894                         /* Normal char */
895                         c = f_ptr->x_char;
896
897                         /* Normal attr */
898                         a = f_ptr->x_attr;
899
900                         /* Special lighting effects */
901                         if (view_special_lite && (!p_ptr->wild_mode) && ((a == TERM_WHITE) || use_graphics))
902                         {
903                                 /* Handle "blind" */
904                                 if (p_ptr->blind)
905                                 {
906                                         if (new_ascii_graphics)
907                                         {
908                                                 if (is_ascii_graphics(c,a))
909                                                 {
910                                                         /* Use darkened colour */
911                                                         a = lighting_colours[a][1];
912                                                 }
913                                                 else if (use_graphics && feat_supports_lighting(feat))
914                                                 {
915                                                         /* Use a dark tile */
916                                                         c++;
917                                                 }
918                                         }
919                                         else
920                                         {
921                                                 if (use_graphics)
922                                                 {
923                                                         /* Use a dark tile */
924                                                         c++;
925                                                 }
926                                                 else
927                                                 {
928                                                         /* Use "dark gray" */
929                                                         a = TERM_L_DARK;
930                                                 }
931                                         }
932                                 }
933
934                                 /* Handle "torch-lit" grids */
935                                 else if (c_ptr->info & (CAVE_LITE | CAVE_MNLT))
936                                 {
937                                         /* Torch lite */
938                                         if (view_yellow_lite && !p_ptr->wild_mode)
939                                         {
940                                                 if (new_ascii_graphics)
941                                                 {
942                                                         if (is_ascii_graphics(c,a))
943                                                         {
944                                                                 /* Use lightened colour */
945                                                                 a = lighting_colours[a][0];
946                                                         }
947                                                         else if (use_graphics &&
948                                                                  feat_supports_lighting(feat))
949                                                         {
950                                                                 /* Use a brightly lit tile */
951                                                                 c += 2;
952                                                         }
953                                                 }
954                                                 else
955                                                 {
956                                                         /* Torch lite */
957                                                         if (view_yellow_lite)
958                                                         {
959                                                                 if (use_graphics)
960                                                                 {
961                                                                         /* Use a brightly lit tile */
962                                                                         c += 2;
963                                                                 }
964                                                                 else
965                                                                 {
966                                                                         /* Use "yellow" */
967                                                                         a = TERM_YELLOW;
968                                                                 }
969                                                         }
970                                                 }
971                                         }
972                                 }
973
974                                 /* Handle "dark" grids */
975                                 else if (!(c_ptr->info & CAVE_GLOW))
976                                 {
977                                         if (new_ascii_graphics)
978                                         {
979                                                 if (is_ascii_graphics(c,a))
980                                                 {
981                                                         /* Use darkened colour */
982                                                         a = lighting_colours[a][1];
983                                                 }
984                                                 else if (use_graphics && feat_supports_lighting(feat))
985                                                 {
986                                                         /* Use a dark tile */
987                                                         c++;
988                                                 }
989                                         }
990                                         else
991                                         {
992                                                 if (use_graphics)
993                                                 {
994                                                         /* Use a dark tile */
995                                                         c++;
996                                                 }
997                                                 else
998                                                 {
999                                                         /* Use "dark gray" */
1000                                                         a = TERM_L_DARK;
1001                                                 }
1002                                         }
1003                                 }
1004
1005                                 /* Handle "out-of-sight" grids */
1006                                 else if (!(c_ptr->info & CAVE_VIEW))
1007                                 {
1008                                         /* Special flag */
1009                                         if (view_bright_lite && !p_ptr->wild_mode)
1010                                         {
1011                                                 if (new_ascii_graphics)
1012                                                 {
1013                                                         if (is_ascii_graphics(c,a))
1014                                                         {
1015                                                                 /* Use darkened colour */
1016                                                                 a = lighting_colours[a][1];
1017                                                         }
1018                                                         else if (use_graphics && feat_supports_lighting(feat))
1019                                                         {
1020                                                                 /* Use a dark tile */
1021                                                                 c++;
1022                                                         }
1023                                                 }
1024                                                 else
1025                                                 {
1026                                                         if (use_graphics)
1027                                                         {
1028                                                                 /* Use a dark tile */
1029                                                                 c++;
1030                                                         }
1031                                                         else
1032                                                         {
1033                                                                 /* Use "gray" */
1034                                                                 a = TERM_SLATE;
1035                                                         }
1036                                                 }
1037                                         }
1038                                 }
1039                         }
1040                 }
1041
1042                 /* Unknown */
1043                 else
1044                 {
1045                         feat = FEAT_NONE;
1046
1047                         /* Access darkness */
1048                         f_ptr = &f_info[feat];
1049
1050                         /* Normal attr */
1051                         a = f_ptr->x_attr;
1052
1053                         /* Normal char */
1054                         c = f_ptr->x_char;
1055                 }
1056         }
1057
1058         /* Non floors */
1059         else
1060         {
1061                 /* Memorized grids */
1062                 if ((c_ptr->info & CAVE_MARK) && (view_granite_lite || !new_ascii_graphics))
1063                 {
1064                         /* Apply "mimic" field */
1065                         if (c_ptr->mimic)
1066                                 feat = c_ptr->mimic;
1067                         else
1068                                 feat = f_info[feat].mimic;
1069
1070                         /* Access feature */
1071                         f_ptr = &f_info[feat];
1072
1073                         /* Normal char */
1074                         c = f_ptr->x_char;
1075
1076                         /* Normal attr */
1077                         a = f_ptr->x_attr;
1078
1079                         if (new_ascii_graphics)
1080                         {
1081                                 /* Handle "blind" */
1082                                 if (p_ptr->blind)
1083                                 {
1084                                         if (is_ascii_graphics(c,a))
1085                                         {
1086                                                 /* Use darkened colour */
1087                                                 a = lighting_colours[a][1];
1088                                         }
1089                                         else if (use_graphics && feat_supports_lighting(feat))
1090                                         {
1091                                                 /* Use a dark tile */
1092                                                 c++;
1093                                         }
1094                                 }
1095
1096                                 /* Handle "torch-lit" grids */
1097                                 else if (c_ptr->info & (CAVE_LITE | CAVE_MNLT))
1098                                 {
1099                                         /* Torch lite */
1100                                         if (view_yellow_lite && !p_ptr->wild_mode && ((use_graphics && feat_supports_lighting(feat)) || is_ascii_graphics(c,a)))
1101                                         {
1102                                                 if (is_ascii_graphics(c,a))
1103                                                 {
1104                                                         /* Use lightened colour */
1105                                                         a = lighting_colours[a][0];
1106                                                 }
1107                                                 else if (use_graphics &&
1108                                                                 feat_supports_lighting(c_ptr->feat))
1109                                                 {
1110                                                         /* Use a brightly lit tile */
1111                                                         c += 2;
1112                                                 }
1113                                         }
1114                                 }
1115
1116                                 /* Handle "view_bright_lite" */
1117                                 else if (view_bright_lite && !p_ptr->wild_mode && ((use_graphics && feat_supports_lighting(feat)) || is_ascii_graphics(c,a)))
1118                                 {
1119                                         /* Not viewable */
1120                                         if (!(c_ptr->info & CAVE_VIEW))
1121                                         {
1122                                                 if (is_ascii_graphics(c,a))
1123                                                 {
1124                                                         /* Use darkened colour */
1125                                                         a = lighting_colours[a][1];
1126                                                 }
1127                                                 else if (use_graphics && feat_supports_lighting(feat))
1128                                                 {
1129                                                         /* Use a dark tile */
1130                                                         c++;
1131                                                 }
1132                                         }
1133
1134                                         /* Not glowing */
1135                                         else if (!(c_ptr->info & CAVE_GLOW))
1136                                         {
1137                                                 if (is_ascii_graphics(c,a))
1138                                                 {
1139                                                         /* Use darkened colour */
1140                                                         a = lighting_colours[a][1];
1141                                                 }
1142                                         }
1143                                 }
1144                         }
1145                         /* Special lighting effects */
1146                         else if (view_granite_lite && !p_ptr->wild_mode &&
1147                            (((a == TERM_WHITE) && !use_graphics) ||
1148                            (use_graphics && feat_supports_lighting(c_ptr->feat))))
1149                         {
1150                                 /* Handle "blind" */
1151                                 if (p_ptr->blind)
1152                                 {
1153                                         if (use_graphics)
1154                                         {
1155                                                 /* Use a dark tile */
1156                                                 c++;
1157                                         }
1158                                         else
1159                                         {
1160                                                 /* Use "dark gray" */
1161                                                 a = TERM_L_DARK;
1162                                         }
1163                                 }
1164
1165                                 /* Handle "torch-lit" grids */
1166                                 else if (c_ptr->info & (CAVE_LITE | CAVE_MNLT))
1167                                 {
1168                                         /* Torch lite */
1169                                         if (view_yellow_lite && !p_ptr->wild_mode)
1170                                         {
1171                                                 if (use_graphics)
1172                                                 {
1173                                                         /* Use a brightly lit tile */
1174                                                         c += 2;
1175                                                 }
1176                                                 else
1177                                                 {
1178                                                         /* Use "yellow" */
1179                                                         a = TERM_YELLOW;
1180                                                 }
1181                                         }
1182                                 }
1183
1184                                 /* Handle "view_bright_lite" */
1185                                 else if (view_bright_lite && !p_ptr->wild_mode)
1186                                 {
1187                                         /* Not viewable */
1188                                         if (!(c_ptr->info & CAVE_VIEW))
1189                                         {
1190                                                 if (use_graphics)
1191                                                 {
1192                                                         /* Use a dark tile */
1193                                                         c++;
1194                                                 }
1195                                                 else
1196                                                 {
1197                                                         /* Use "gray" */
1198                                                         a = TERM_SLATE;
1199                                                 }
1200                                         }
1201
1202                                         /* Not glowing */
1203                                         else if (!(c_ptr->info & CAVE_GLOW))
1204                                         {
1205                                                 if (use_graphics)
1206                                                 {
1207                                                         /* Use a lit tile */
1208                                                 }
1209                                                 else
1210                                                 {
1211                                                         /* Use "gray" */
1212                                                         a = TERM_SLATE;
1213                                                 }
1214                                         }
1215
1216                                         /* Not glowing correctly */
1217                                         else
1218                                         {
1219                                                 int xx, yy;
1220
1221                                                 /* Hack -- move towards player */
1222                                                 yy = (y < py) ? (y + 1) : (y > py) ? (y - 1) : y;
1223                                                 xx = (x < px) ? (x + 1) : (x > px) ? (x - 1) : x;
1224
1225                                                 /* Check for "local" illumination */
1226                                                 if (!(cave[yy][xx].info & CAVE_GLOW))
1227                                                 {
1228                                                         if (use_graphics)
1229                                                         {
1230                                                                 /* Use a lit tile */
1231                                                         }
1232                                                         else
1233                                                         {
1234                                                                 /* Use "gray" */
1235                                                                 a = TERM_SLATE;
1236                                                         }
1237                                                 }
1238                                         }
1239                                 }
1240                         }
1241                 }
1242
1243                 /* "Simple Lighting" */
1244                 else
1245                 {
1246                         /* Handle "blind" */
1247                         if (!(c_ptr->info & CAVE_MARK))
1248                         {
1249                                 feat = FEAT_NONE;
1250                         }
1251
1252                         /* Access feature */
1253                         f_ptr = &f_info[feat];
1254
1255                         /* Normal attr */
1256                         a = f_ptr->x_attr;
1257
1258                         /* Normal char */
1259                         c = f_ptr->x_char;
1260                 }
1261         }
1262
1263         if (feat_priority == -1)
1264         {
1265                 switch (feat)
1266                 {
1267                 case FEAT_NONE:
1268                 case FEAT_DARK_PIT:
1269                         feat_priority = 1;
1270                         break;
1271
1272                 case FEAT_FLOOR:
1273                 case FEAT_INVIS:
1274                 case FEAT_TRAP_TRAPDOOR:
1275                 case FEAT_TRAP_PIT:
1276                 case FEAT_TRAP_SPIKED_PIT:
1277                 case FEAT_TRAP_POISON_PIT:
1278                 case FEAT_TRAP_TY_CURSE:
1279                 case FEAT_TRAP_TELEPORT:
1280                 case FEAT_TRAP_FIRE:
1281                 case FEAT_TRAP_ACID:
1282                 case FEAT_TRAP_SLOW:
1283                 case FEAT_TRAP_LOSE_STR:
1284                 case FEAT_TRAP_LOSE_DEX:
1285                 case FEAT_TRAP_LOSE_CON:
1286                 case FEAT_TRAP_BLIND:
1287                 case FEAT_TRAP_CONFUSE:
1288                 case FEAT_TRAP_POISON:
1289                 case FEAT_TRAP_SLEEP:
1290                 case FEAT_TRAP_TRAPS:
1291                 case FEAT_DIRT:
1292                 case FEAT_GRASS:
1293                 case FEAT_FLOWER:
1294                 case FEAT_DEEP_GRASS:
1295                 case FEAT_SWAMP:
1296                 case FEAT_TREES:
1297                 case FEAT_SECRET:
1298                 case FEAT_RUBBLE:
1299                 case FEAT_MAGMA:
1300                 case FEAT_QUARTZ:
1301                 case FEAT_MAGMA_H:
1302                 case FEAT_QUARTZ_H:
1303                 case FEAT_WALL_EXTRA:
1304                 case FEAT_WALL_INNER:
1305                 case FEAT_WALL_OUTER:
1306                 case FEAT_WALL_SOLID:
1307                 case FEAT_DEEP_WATER:
1308                 case FEAT_SHAL_WATER:
1309                 case FEAT_DEEP_LAVA:
1310                 case FEAT_SHAL_LAVA:
1311                         feat_priority = 2;
1312                         break;
1313                         
1314                 case FEAT_MAGMA_K:
1315                 case FEAT_QUARTZ_K:
1316                         feat_priority = 3;
1317                         break;
1318                         
1319                 case FEAT_MOUNTAIN:
1320                 case FEAT_PERM_EXTRA:
1321                 case FEAT_PERM_INNER:
1322                 case FEAT_PERM_OUTER:
1323                 case FEAT_PERM_SOLID:
1324                         feat_priority = 5;
1325                         break;
1326                         
1327                         /* default is feat_priority = 20; (doors and stores) */ 
1328                         
1329                 case FEAT_GLYPH:
1330                 case FEAT_MINOR_GLYPH:
1331                 case FEAT_MIRROR:
1332                 case FEAT_PATTERN_START:
1333                 case FEAT_PATTERN_1:
1334                 case FEAT_PATTERN_2:
1335                 case FEAT_PATTERN_3:
1336                 case FEAT_PATTERN_4:
1337                 case FEAT_PATTERN_END:
1338                 case FEAT_PATTERN_OLD:
1339                 case FEAT_PATTERN_XTRA1:
1340                 case FEAT_PATTERN_XTRA2:
1341                         feat_priority = 16;
1342                         break;
1343                         
1344                         /* objects have feat_priority = 20 */ 
1345                         /* monsters have feat_priority = 30 */ 
1346                         
1347                 case FEAT_LESS:
1348                 case FEAT_MORE:
1349                 case FEAT_QUEST_ENTER:
1350                 case FEAT_QUEST_EXIT:
1351                 case FEAT_QUEST_DOWN:
1352                 case FEAT_QUEST_UP:
1353                 case FEAT_LESS_LESS:
1354                 case FEAT_MORE_MORE:
1355                 case FEAT_TOWN:
1356                 case FEAT_ENTRANCE:
1357                         feat_priority = 35;
1358                         break;
1359                         
1360                 default:
1361                         feat_priority = 10;
1362                         break;
1363                 }
1364         }
1365
1366         /* Hack -- rare random hallucination, except on outer dungeon walls */
1367         if (p_ptr->image && (c_ptr->feat < FEAT_PERM_SOLID) && !randint0(256))
1368         {
1369                 /* Hallucinate */
1370                 image_random(ap, cp);
1371         }
1372
1373 #ifdef USE_TRANSPARENCY
1374         /* Save the terrain info for the transparency effects */
1375         (*tap) = a;
1376         (*tcp) = c;
1377 #endif /* USE_TRANSPARENCY */
1378
1379         /* Save the info */
1380         (*ap) = a;
1381         (*cp) = c;
1382
1383         /* Objects */
1384         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1385         {
1386                 object_type *o_ptr;
1387
1388                 /* Acquire object */
1389                 o_ptr = &o_list[this_o_idx];
1390
1391                 /* Acquire next object */
1392                 next_o_idx = o_ptr->next_o_idx;
1393
1394                 /* Memorized objects */
1395                 if (o_ptr->marked)
1396                 {
1397                         if (display_autopick)
1398                         {
1399                                 byte act;
1400
1401                                 match_autopick = is_autopick(o_ptr);
1402                                 if(match_autopick == -1)
1403                                         continue;
1404
1405                                 act = autopick_action[match_autopick];
1406
1407                                 if ((act & DO_DISPLAY) && (act & display_autopick))
1408                                 {
1409                                         autopick_obj = o_ptr;
1410                                 }
1411                                 else
1412                                 {
1413                                         match_autopick = -1;
1414                                         continue;
1415                                 }
1416                         }
1417                         /* Normal char */
1418                         (*cp) = object_char(o_ptr);
1419
1420                         /* Normal attr */
1421                         (*ap) = object_attr(o_ptr);
1422
1423                         feat_priority = 20;
1424
1425                         /* Hack -- hallucination */
1426                         if (p_ptr->image) image_object(ap, cp);
1427
1428                         /* Done */
1429                         break;
1430                 }
1431         }
1432
1433
1434         /* Handle monsters */
1435         if (c_ptr->m_idx && display_autopick == 0 )
1436         {
1437                 monster_type *m_ptr = &m_list[c_ptr->m_idx];
1438
1439                 /* Visible monster */
1440                 if (m_ptr->ml)
1441                 {
1442                         monster_race *r_ptr;
1443                         if (m_ptr->mflag2 & MFLAG_KAGE) r_ptr = &r_info[MON_KAGE];
1444                         else r_ptr = &r_info[m_ptr->r_idx];
1445
1446                         /* Desired attr */
1447                         a = r_ptr->x_attr;
1448
1449                         /* Desired char */
1450                         c = r_ptr->x_char;
1451
1452                         feat_priority = 30;
1453
1454                         /* Mimics' colors vary */
1455                         if (strchr("\"!=", c) && !(r_ptr->flags1 & RF1_UNIQUE))
1456                         {
1457                                 /* Use char */
1458                                 (*cp) = c;
1459
1460                                 /* Use semi-random attr */
1461                                 (*ap) = c_ptr->m_idx % 15 + 1;
1462                         }
1463
1464                         /* Special attr/char codes */
1465                         else if ((a & 0x80) && (c & 0x80))
1466                         {
1467                                 /* Use char */
1468                                 (*cp) = c;
1469
1470                                 /* Use attr */
1471                                 (*ap) = a;
1472                         }
1473
1474                         /* Multi-hued monster */
1475                         else if (r_ptr->flags1 & (RF1_ATTR_MULTI))
1476                         {
1477                                 /* Is it a shapechanger? */
1478                                 if (r_ptr->flags2 & (RF2_SHAPECHANGER))
1479                                 {
1480                                         if (use_graphics)
1481                                         {
1482                                                 if (!(streq(ANGBAND_SYS, "ibm")))
1483                                                 {
1484                                                         (*cp) = r_info[randint1(max_r_idx-1)].x_char;
1485                                                         (*ap) = r_info[randint1(max_r_idx-1)].x_attr;
1486                                                 }
1487                                                 else
1488                                                 {
1489                                                         int n =  strlen(image_monster_hack_ibm);
1490                                                         (*cp) = (image_monster_hack_ibm[randint0(n)]);
1491
1492                                                         /* Random color */
1493                                                         (*ap) = randint1(15);
1494                                                 }
1495                                         }
1496                                         else
1497                                         {
1498                                                 (*cp) = (one_in_(25) ?
1499                                                         image_object_hack[randint0(strlen(image_object_hack))] :
1500                                                         image_monster_hack[randint0(strlen(image_monster_hack))]);
1501                                         }
1502                                 }
1503                                 else
1504                                         (*cp) = c;
1505
1506                                 /* Multi-hued attr */
1507                                 if (r_ptr->flags2 & RF2_ATTR_ANY)
1508                                         (*ap) = randint1(15);
1509                                 else switch (randint1(7))
1510                                 {
1511                                         case 1:
1512                                                 (*ap) = TERM_RED;
1513                                                 break;
1514                                         case 2:
1515                                                 (*ap) = TERM_L_RED;
1516                                                 break;
1517                                         case 3:
1518                                                 (*ap) = TERM_WHITE;
1519                                                 break;
1520                                         case 4:
1521                                                 (*ap) = TERM_L_GREEN;
1522                                                 break;
1523                                         case 5:
1524                                                 (*ap) = TERM_BLUE;
1525                                                 break;
1526                                         case 6:
1527                                                 (*ap) = TERM_L_DARK;
1528                                                 break;
1529                                         case 7:
1530                                                 (*ap) = TERM_GREEN;
1531                                                 break;
1532                                 }
1533                         }
1534
1535                         /* Normal monster (not "clear" in any way) */
1536                         else if (!(r_ptr->flags1 & (RF1_ATTR_CLEAR | RF1_CHAR_CLEAR)))
1537                         {
1538                                 /* Use char */
1539                                 (*cp) = c;
1540
1541                                 /* Use attr */
1542                                 (*ap) = a;
1543                         }
1544
1545                         /* Hack -- Bizarre grid under monster */
1546                         else if ((*ap & 0x80) || (*cp & 0x80))
1547                         {
1548                                 /* Use char */
1549                                 (*cp) = c;
1550
1551                                 /* Use attr */
1552                                 (*ap) = a;
1553                         }
1554
1555                         /* Normal */
1556                         else
1557                         {
1558                                 /* Normal (non-clear char) monster */
1559                                 if (!(r_ptr->flags1 & (RF1_CHAR_CLEAR)))
1560                                 {
1561                                         /* Normal char */
1562                                         (*cp) = c;
1563                                 }
1564
1565                                 /* Normal (non-clear attr) monster */
1566                                 else if (!(r_ptr->flags1 & (RF1_ATTR_CLEAR)))
1567                                 {
1568                                         /* Normal attr */
1569                                         (*ap) = a;
1570                                 }
1571                         }
1572
1573                         /* Hack -- hallucination */
1574                         if (p_ptr->image)
1575                         {
1576                                 /* Hallucinatory monster */
1577                                 image_monster(ap, cp);
1578                         }
1579                 }
1580         }
1581
1582         /* Handle "player" */
1583         if ((y == py) && (x == px))
1584         {
1585                 monster_race *r_ptr = &r_info[0];
1586
1587                 feat_priority = 31;
1588
1589                 /* Get the "player" attr */
1590                 a = r_ptr->x_attr;
1591
1592                 /* Get the "player" char */
1593                 c = r_ptr->x_char;
1594
1595 #ifdef VARIABLE_PLAYER_GRAPH
1596
1597                 if (!streq(ANGBAND_GRAF, "new"))
1598                 {
1599                         if (streq(ANGBAND_SYS,"ibm"))
1600                         {
1601                                 if (use_graphics && player_symbols)
1602                                 {
1603                                         if (p_ptr->psex == SEX_FEMALE) c = (char)242;
1604                                         switch (p_ptr->pclass)
1605                                         {
1606                                                 case CLASS_PALADIN:
1607                                                         if (p_ptr->lev < 20)
1608                                                                 a = TERM_L_WHITE;
1609                                                         else
1610                                                                 a = TERM_WHITE;
1611                                                         c = 253;
1612                                                         break;
1613                                                 case CLASS_WARRIOR_MAGE:
1614                                                 case CLASS_RED_MAGE:
1615                                                         if (p_ptr->lev < 20)
1616                                                                 a = TERM_L_RED;
1617                                                         else
1618                                                                 a = TERM_VIOLET;
1619                                                         break;
1620                                                 case CLASS_CHAOS_WARRIOR:
1621                                                         do
1622                                                         {
1623                                                                 a = randint1(15);
1624                                                         }
1625                                                         while (a == TERM_DARK);
1626                                                         break;
1627                                                 case CLASS_MAGE:
1628                                                 case CLASS_HIGH_MAGE:
1629                                                 case CLASS_SORCERER:
1630                                                 case CLASS_MAGIC_EATER:
1631                                                 case CLASS_BLUE_MAGE:
1632                                                         if (p_ptr->lev < 20)
1633                                                                 a = TERM_L_RED;
1634                                                         else
1635                                                                 a = TERM_RED;
1636                                                         c = 248;
1637                                                         break;
1638                                                 case CLASS_PRIEST:
1639                                                 case CLASS_BARD:
1640                                                         if (p_ptr->lev < 20)
1641                                                                 a = TERM_L_BLUE;
1642                                                         else
1643                                                                 a = TERM_BLUE;
1644                                                         c = 248;
1645                                                         break;
1646                                                 case CLASS_RANGER:
1647                                                 case CLASS_ARCHER:
1648                                                         if (p_ptr->lev < 20)
1649                                                                 a = TERM_L_GREEN;
1650                                                         else
1651                                                                 a = TERM_GREEN;
1652                                                         break;
1653                                                 case CLASS_ROGUE:
1654                                                 case CLASS_NINJA:
1655                                                         if (p_ptr->lev < 20)
1656                                                                 a = TERM_SLATE;
1657                                                         else
1658                                                                 a = TERM_L_DARK;
1659                                                         break;
1660                                                 case CLASS_WARRIOR:
1661                                                 case CLASS_SMITH:
1662                                                 case CLASS_BERSERKER:
1663                                                 case CLASS_SAMURAI:
1664                                                         if (p_ptr->lev < 20)
1665                                                                 a = TERM_L_UMBER;
1666                                                         else
1667                                                                 a = TERM_UMBER;
1668                                                         break;
1669                                                 case CLASS_MONK:
1670                                                 case CLASS_MINDCRAFTER:
1671                                                 case CLASS_FORCETRAINER:
1672                                                 case CLASS_MIRROR_MASTER:
1673                                                         if (p_ptr->lev < 20)
1674                                                                 a = TERM_L_UMBER;
1675                                                         else
1676                                                                 a = TERM_UMBER;
1677                                                         c = 248;
1678                                                         break;
1679                                                 default: /* Unknown */
1680                                                         a = TERM_WHITE;
1681                                         }
1682
1683                                         switch (p_ptr->prace)
1684                                         {
1685                                                 case RACE_GNOME:
1686                                                 case RACE_HOBBIT:
1687                                                         c = 144;
1688                                                         break;
1689                                                 case RACE_DWARF:
1690                                                         c = 236;
1691                                                         break;
1692                                                 case RACE_HALF_ORC:
1693                                                         c = 243;
1694                                                         break;
1695                                                 case RACE_HALF_TROLL:
1696                                                         c = 184;
1697                                                         break;
1698                                                 case RACE_ELF:
1699                                                 case RACE_ENT:
1700                                                 case RACE_HALF_ELF:
1701                                                 case RACE_HIGH_ELF:
1702                                                 case RACE_KUTA:
1703                                                         c = 223;
1704                                                         break;
1705                                                 case RACE_HALF_OGRE:
1706                                                         c = 168;
1707                                                         break;
1708                                                 case RACE_HALF_GIANT:
1709                                                 case RACE_HALF_TITAN:
1710                                                 case RACE_CYCLOPS:
1711                                                         c = 145;
1712                                                         break;
1713                                                 case RACE_YEEK:
1714                                                         c = 209;
1715                                                         break;
1716                                                 case RACE_KLACKON:
1717                                                         c = 229;
1718                                                         break;
1719                                                 case RACE_KOBOLD:
1720                                                         c = 204;
1721                                                         break;
1722                                                 case RACE_NIBELUNG:
1723                                                         c = 144;
1724                                                         break;
1725                                                 case RACE_DARK_ELF:
1726                                                         c = 223;
1727                                                         break;
1728                                                 case RACE_DRACONIAN:
1729                                                         if (p_ptr->lev < 20)
1730                                                                 c = 240;
1731                                                         else if (p_ptr->lev < 40)
1732                                                                 c = 22;
1733                                                         else
1734                                                                 c = 137;
1735                                                         break;
1736                                                 case RACE_MIND_FLAYER:
1737                                                         c = 236;
1738                                                         break;
1739                                                 case RACE_IMP:
1740                                                         c = 142;
1741                                                         break;
1742                                                 case RACE_GOLEM:
1743                                                 case RACE_ANDROID:
1744                                                         c = 6;
1745                                                         break;
1746                                                 case RACE_SKELETON:
1747                                                         if (p_ptr->pclass == CLASS_MAGE ||
1748                                                                 p_ptr->pclass == CLASS_PRIEST ||
1749                                                                 p_ptr->pclass == CLASS_HIGH_MAGE ||
1750                                                                 p_ptr->pclass == CLASS_SORCERER ||
1751                                                                 p_ptr->pclass == CLASS_MONK ||
1752                                                                 p_ptr->pclass == CLASS_FORCETRAINER ||
1753                                                                 p_ptr->pclass == CLASS_BLUE_MAGE ||
1754                                                                 p_ptr->pclass == CLASS_MIRROR_MASTER ||
1755                                                                 p_ptr->pclass == CLASS_MINDCRAFTER)
1756                                                                 c = 159;
1757                                                         else
1758                                                                 c = 181;
1759                                                         break;
1760                                                 case RACE_ZOMBIE:
1761                                                         c = 221;
1762                                                         break;
1763                                                 case RACE_VAMPIRE:
1764                                                         c = 217;
1765                                                         break;
1766                                                 case RACE_SPECTRE:
1767                                                         c = 241;
1768                                                         break;
1769                                                 case RACE_SPRITE:
1770                                                 case RACE_S_FAIRY:
1771                                                         c = 244;
1772                                                         break;
1773                                                 case RACE_BEASTMAN:
1774                                                         c = 154;
1775                                                         break;
1776                                                 case RACE_ANGEL:
1777                                                 case RACE_DEMON:
1778                                                         c = 144;
1779                                                         break;
1780                                         }
1781                                 }
1782                         }
1783                 }
1784
1785                 /* Save the info */
1786                 (*ap) = a;
1787                 (*cp) = c;
1788
1789 #endif /* VARIABLE_PLAYER_GRAPH */
1790
1791         }
1792 }
1793
1794
1795
1796 /*
1797  * Moves the cursor to a given MAP (y,x) location
1798  */
1799 void move_cursor_relative(int row, int col)
1800 {
1801         /* Real co-ords convert to screen positions */
1802         row -= panel_row_prt;
1803         col -= panel_col_prt;
1804
1805         /* Go there */
1806         Term_gotoxy(col, row);
1807 }
1808
1809
1810
1811 /*
1812  * Place an attr/char pair at the given map coordinate, if legal.
1813  */
1814 void print_rel(char c, byte a, int y, int x)
1815 {
1816         /* Only do "legal" locations */
1817         if (panel_contains(y, x))
1818         {
1819                 /* Hack -- fake monochrome */
1820                 if (!use_graphics || streq(ANGBAND_SYS, "ibm"))
1821                 {
1822                         if (world_monster) a = TERM_DARK;
1823                         else if (p_ptr->invuln || world_player) a = TERM_WHITE;
1824                         else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
1825                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1826                 }
1827
1828                 /* Draw the char using the attr */
1829                 Term_draw(x-panel_col_prt, y-panel_row_prt, a, c);
1830         }
1831 }
1832
1833
1834
1835
1836
1837 /*
1838  * Memorize interesting viewable object/features in the given grid
1839  *
1840  * This function should only be called on "legal" grids.
1841  *
1842  * This function will memorize the object and/or feature in the given
1843  * grid, if they are (1) viewable and (2) interesting.  Note that all
1844  * objects are interesting, all terrain features except floors (and
1845  * invisible traps) are interesting, and floors (and invisible traps)
1846  * are interesting sometimes (depending on various options involving
1847  * the illumination of floor grids).
1848  *
1849  * The automatic memorization of all objects and non-floor terrain
1850  * features as soon as they are displayed allows incredible amounts
1851  * of optimization in various places, especially "map_info()".
1852  *
1853  * Note that the memorization of objects is completely separate from
1854  * the memorization of terrain features, preventing annoying floor
1855  * memorization when a detected object is picked up from a dark floor,
1856  * and object memorization when an object is dropped into a floor grid
1857  * which is memorized but out-of-sight.
1858  *
1859  * This function should be called every time the "memorization" of
1860  * a grid (or the object in a grid) is called into question, such
1861  * as when an object is created in a grid, when a terrain feature
1862  * "changes" from "floor" to "non-floor", when any grid becomes
1863  * "illuminated" or "viewable", and when a "floor" grid becomes
1864  * "torch-lit".
1865  *
1866  * Note the relatively efficient use of this function by the various
1867  * "update_view()" and "update_lite()" calls, to allow objects and
1868  * terrain features to be memorized (and drawn) whenever they become
1869  * viewable or illuminated in any way, but not when they "maintain"
1870  * or "lose" their previous viewability or illumination.
1871  *
1872  * Note the butchered "internal" version of "player_can_see_bold()",
1873  * optimized primarily for the most common cases, that is, for the
1874  * non-marked floor grids.
1875  */
1876 void note_spot(int y, int x)
1877 {
1878         cave_type *c_ptr = &cave[y][x];
1879
1880         s16b this_o_idx, next_o_idx = 0;
1881
1882
1883         /* Blind players see nothing */
1884         if (p_ptr->blind) return;
1885
1886         /* Analyze non-torch-lit grids */
1887         if (!(c_ptr->info & (CAVE_LITE)))
1888         {
1889                 /* Require line of sight to the grid */
1890                 if (!(c_ptr->info & (CAVE_VIEW))) return;
1891
1892                 if (p_ptr->pclass != CLASS_NINJA)
1893                 {
1894                 /* Require "perma-lite" of the grid */
1895                 if (!(c_ptr->info & (CAVE_GLOW | CAVE_MNLT))) return;
1896                 }
1897         }
1898
1899
1900         /* Hack -- memorize objects */
1901         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1902         {
1903                 object_type *o_ptr = &o_list[this_o_idx];
1904
1905                 /* Acquire next object */
1906                 next_o_idx = o_ptr->next_o_idx;
1907
1908                 /* Memorize objects */
1909                 o_ptr->marked = TRUE;
1910         }
1911
1912
1913         /* Hack -- memorize grids */
1914         if (!(c_ptr->info & (CAVE_MARK)))
1915         {
1916                 if (p_ptr->pclass == CLASS_NINJA)
1917                 {
1918                         c_ptr->info |= (CAVE_MARK);
1919                 }
1920                 /* Handle floor grids first */
1921                 if ((c_ptr->feat <= FEAT_INVIS) || (c_ptr->feat == FEAT_DIRT) || (c_ptr->feat == FEAT_GRASS))
1922                 {
1923                         /* Option -- memorize all torch-lit floors */
1924                         if (view_torch_grids && (c_ptr->info & (CAVE_LITE)))
1925                         {
1926                                 /* Memorize */
1927                                 c_ptr->info |= (CAVE_MARK);
1928                         }
1929
1930                         /* Option -- memorize all perma-lit floors */
1931                         else if (view_perma_grids && (c_ptr->info & (CAVE_GLOW)))
1932                         {
1933                                 /* Memorize */
1934                                 c_ptr->info |= (CAVE_MARK);
1935                         }
1936                 }
1937
1938                 /* Memorize normal grids */
1939                 else if (cave_floor_grid(c_ptr))
1940                 {
1941                         /* Memorize */
1942                         c_ptr->info |= (CAVE_MARK);
1943                 }
1944
1945                 /* Memorize torch-lit walls */
1946                 else if (c_ptr->info & (CAVE_LITE))
1947                 {
1948                         /* Memorize */
1949                         c_ptr->info |= (CAVE_MARK);
1950                 }
1951
1952                 /* Memorize certain non-torch-lit wall grids */
1953                 else
1954                 {
1955                         int yy, xx;
1956
1957                         /* Hack -- move one grid towards player */
1958                         yy = (y < py) ? (y + 1) : (y > py) ? (y - 1) : y;
1959                         xx = (x < px) ? (x + 1) : (x > px) ? (x - 1) : x;
1960
1961                         /* Check for "local" illumination */
1962                         if (cave[yy][xx].info & (CAVE_GLOW))
1963                         {
1964                                 /* Memorize */
1965                                 c_ptr->info |= (CAVE_MARK);
1966                         }
1967                 }
1968         }
1969 }
1970
1971
1972 void display_dungeon(void)
1973 {
1974         int x, y;
1975         byte a;
1976         char c;
1977
1978 #ifdef USE_TRANSPARENCY
1979         byte ta;
1980         char tc;
1981 #endif /* USE_TRANSPARENCY */
1982
1983         for (x = px - Term->wid / 2 + 1; x <= px + Term->wid / 2; x++)
1984         {
1985                 for (y = py - Term->hgt / 2 + 1; y <= py + Term->hgt / 2; y++)
1986                 {
1987                         if (in_bounds2(y, x))
1988                         {
1989
1990 #ifdef USE_TRANSPARENCY
1991                                 /* Examine the grid */
1992                                 map_info(y, x, &a, &c, &ta, &tc);
1993 #else /* USE_TRANSPARENCY */
1994                                 /* Examine the grid */
1995                                 map_info(y, x, &a, &c);
1996 #endif /* USE_TRANSPARENCY */
1997
1998                                 /* Hack -- fake monochrome */
1999                                 if (!use_graphics || streq(ANGBAND_SYS, "ibm"))
2000                                 {
2001                                         if (world_monster) a = TERM_DARK;
2002                                         else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2003                                         else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2004                                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
2005                                 }
2006
2007 #ifdef USE_TRANSPARENCY
2008                                 /* Hack -- Queue it */
2009                                 Term_queue_char(x - px + Term->wid / 2 - 1, y - py + Term->hgt / 2 - 1, a, c, ta, tc);
2010 #else /* USE_TRANSPARENCY */
2011                                 /* Hack -- Queue it */
2012                                 Term_queue_char(x - px + Term->wid / 2 - 1, y - py + Term->hgt / 2 - 1, a, c);
2013 #endif /* USE_TRANSPARENCY */
2014
2015                         }
2016                         else
2017                         {
2018                                 /* Clear out-of-bound tiles */
2019
2020                                 /* Access darkness */
2021                                 feature_type *f_ptr = &f_info[FEAT_NONE];
2022
2023                                 /* Normal attr */
2024                                 a = f_ptr->x_attr;
2025
2026                                 /* Normal char */
2027                                 c = f_ptr->x_char;
2028
2029 #ifdef USE_TRANSPARENCY
2030                                 /* Hack -- Queue it */
2031                                 Term_queue_char(x - px + Term->wid / 2 - 1, y - py + Term->hgt / 2 - 1, a, c, ta, tc);
2032 #else /* USE_TRANSPARENCY */
2033                                 /* Hack -- Queue it */
2034                                 Term_queue_char(x - px + Term->wid / 2 - 1, y - py + Term->hgt / 2 - 1, a, c);
2035 #endif /* USE_TRANSPARENCY */
2036                         }
2037                 }
2038         }
2039 }
2040
2041
2042 /*
2043  * Redraw (on the screen) a given MAP location
2044  *
2045  * This function should only be called on "legal" grids
2046  */
2047 void lite_spot(int y, int x)
2048 {
2049         /* Redraw if on screen */
2050         if (panel_contains(y, x))
2051         {
2052                 byte a;
2053                 char c;
2054
2055 #ifdef USE_TRANSPARENCY
2056                 byte ta;
2057                 char tc;
2058
2059                 /* Examine the grid */
2060                 map_info(y, x, &a, &c, &ta, &tc);
2061 #else /* USE_TRANSPARENCY */
2062                 /* Examine the grid */
2063                 map_info(y, x, &a, &c);
2064 #endif /* USE_TRANSPARENCY */
2065
2066                 /* Hack -- fake monochrome */
2067                 if (!use_graphics || streq(ANGBAND_SYS, "ibm"))
2068                 {
2069                         if (world_monster) a = TERM_DARK;
2070                         else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2071                         else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2072                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
2073                 }
2074
2075 #ifdef USE_TRANSPARENCY
2076                 /* Hack -- Queue it */
2077                 Term_queue_char(x-panel_col_prt, y-panel_row_prt, a, c, ta, tc);
2078 #else /* USE_TRANSPARENCY */
2079                 /* Hack -- Queue it */
2080                 Term_queue_char(x-panel_col_prt, y-panel_row_prt, a, c);
2081 #endif /* USE_TRANSPARENCY */
2082         }
2083 }
2084
2085
2086 /*
2087  * Prints the map of the dungeon
2088  *
2089  * Note that, for efficiency, we contain an "optimized" version
2090  * of both "lite_spot()" and "print_rel()", and that we use the
2091  * "lite_spot()" function to display the player grid, if needed.
2092  */
2093 void prt_map(void)
2094 {
2095         int     x, y;
2096         int     v;
2097         bool    fake_monochrome = (!use_graphics || streq(ANGBAND_SYS, "ibm"));
2098
2099
2100         /* Access the cursor state */
2101         (void)Term_get_cursor(&v);
2102
2103         /* Hide the cursor */
2104         (void)Term_set_cursor(0);
2105
2106         /* Dump the map */
2107         for (y = panel_row_min; y <= panel_row_max; y++)
2108         {
2109                 /* Scan the columns of row "y" */
2110                 for (x = panel_col_min; x <= panel_col_max; x++)
2111                 {
2112                         byte a;
2113                         char c;
2114
2115 #ifdef USE_TRANSPARENCY
2116                         byte ta;
2117                         char tc;
2118
2119                         /* Determine what is there */
2120                         map_info(y, x, &a, &c, &ta, &tc);
2121
2122                         /* Hack -- fake monochrome */
2123                         if (fake_monochrome)
2124                         {
2125                                 if (world_monster) a = TERM_DARK;
2126                                 else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2127                                 else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2128                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2129                         }
2130
2131                         /* Efficiency -- Redraw that grid of the map */
2132                         Term_queue_char(x-panel_col_prt, y-panel_row_prt, a, c, ta, tc);
2133 #else /* USE_TRANSPARENCY */
2134                         /* Determine what is there */
2135                         map_info(y, x, &a, &c);
2136
2137                         /* Hack -- fake monochrome */
2138                         if (fake_monochrome)
2139                         {
2140                                 if (world_monster) a = TERM_DARK;
2141                                 else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2142                                 else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2143                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2144                         }
2145
2146                         /* Efficiency -- Redraw that grid of the map */
2147                         Term_queue_char(x-panel_col_prt, y-panel_row_prt, a, c);
2148 #endif /* USE_TRANSPARENCY */
2149                 }
2150         }
2151
2152         /* Display player */
2153         lite_spot(py, px);
2154
2155         /* Restore the cursor */
2156         (void)Term_set_cursor(v);
2157 }
2158
2159
2160
2161 /*
2162  * print project path
2163  */
2164 void prt_path(int y, int x)
2165 {
2166         int i;
2167         int path_n;
2168         u16b path_g[512];
2169         int default_color = TERM_SLATE;
2170         bool    fake_monochrome = (!use_graphics || streq(ANGBAND_SYS, "ibm"));
2171
2172         if (!display_path) return;
2173         if (-1 == project_length)
2174                 return;
2175
2176         /* Get projection path */
2177         path_n = project_path(path_g, (project_length ? project_length : MAX_RANGE), py, px, y, x, PROJECT_PATH|PROJECT_THRU);
2178
2179         /* Redraw map */
2180         p_ptr->redraw |= (PR_MAP);
2181
2182         /* Redraw stuff */
2183         redraw_stuff();
2184
2185         /* Draw path */
2186         for (i = 0; i < path_n; i++)
2187         {
2188                 int ny = GRID_Y(path_g[i]);
2189                 int nx = GRID_X(path_g[i]);
2190
2191                 if (panel_contains(ny, nx))
2192                 {
2193                         byte a = default_color;
2194                         char c;
2195
2196 #ifdef USE_TRANSPARENCY
2197                         byte ta;
2198                         char tc;
2199
2200                         if (cave[ny][nx].m_idx && m_list[cave[ny][nx].m_idx].ml)
2201                         {
2202                                 /* Determine what is there */
2203                                 map_info(ny, nx, &a, &c, &ta, &tc);
2204                         
2205                                 if (c == '.' && (a == TERM_WHITE || a == TERM_L_WHITE))
2206                                         a = default_color;
2207                                 else if (a == default_color)
2208                                         a = TERM_WHITE;
2209                         }
2210
2211                         if (fake_monochrome)
2212                         {
2213                                 if (world_monster) a = TERM_DARK;
2214                                 else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2215                                 else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2216                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2217                         }
2218
2219
2220                         /* Hack -- Queue it */
2221                         Term_queue_char(nx-panel_col_prt, ny-panel_row_prt, a, '*', ta, tc);
2222 #else /* USE_TRANSPARENCY */
2223
2224                         if (cave[ny][nx].m_idx && m_list[cave[ny][nx].m_idx].ml)
2225                         {
2226                                 /* Determine what is there */
2227                                 map_info(ny, nx, &a, &c);
2228                                 
2229                                 if (c == '.' && (a == TERM_WHITE || a == TERM_L_WHITE))
2230                                         a = default_color;
2231                                 else if (a == default_color)
2232                                         a = TERM_WHITE;
2233                         }
2234
2235                         if (fake_monochrome)
2236                         {
2237                                 if (world_monster) a = TERM_DARK;
2238                                 else if (p_ptr->invuln || world_player) a = TERM_WHITE;
2239                                 else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) a = TERM_WHITE;
2240                                 else if (p_ptr->wraith_form) a = TERM_L_DARK;
2241                         }
2242
2243                         /* Hack -- Queue it */
2244                         Term_queue_char(nx-panel_col_prt, ny-panel_row_prt, a, '*');
2245 #endif /* USE_TRANSPARENCY */
2246                 }
2247
2248                 /* Known Wall */
2249                 if ((cave[ny][nx].info & CAVE_MARK) && !cave_floor_bold(ny, nx)) break;
2250
2251                 /* Change color */
2252                 if (nx == x && ny == y) default_color = TERM_L_DARK;
2253         }
2254 }
2255
2256
2257 static cptr simplify_list[][2] =
2258 {
2259 #ifdef JP
2260         {"¤ÎËâË¡½ñ", ""},
2261         {NULL, NULL}
2262 #else
2263         {"^Ring of ",   "="},
2264         {"^Amulet of ", "\""},
2265         {"^Scroll of ", "?"},
2266         {"^Scroll titled ", "?"},
2267         {"^Wand of "  , "-"},
2268         {"^Rod of "   , "-"},
2269         {"^Staff of " , "_"},
2270         {"^Potion of ", "!"},
2271         {" Spellbook ",""},
2272         {"^Book of ",   ""},
2273         {" Magic [",   "["},
2274         {" Book [",    "["},
2275         {" Arts [",    "["},
2276         {"^Set of ",    ""},
2277         {"^Pair of ",   ""},
2278         {NULL, NULL}
2279 #endif
2280 };
2281
2282 static void display_shortened_item_name(object_type *o_ptr, int y)
2283 {
2284         char buf[MAX_NLEN];
2285         char *c = buf;
2286         int len = 0;
2287         byte attr;
2288
2289         object_desc(buf, o_ptr, FALSE, 0);
2290         attr = tval_to_attr[o_ptr->tval % 128];
2291
2292         if (p_ptr->image)
2293         {
2294                 attr = TERM_WHITE;
2295 #ifdef JP
2296                 strcpy(buf, "²¿¤«´ñ̯¤Êʪ");
2297 #else
2298                 strcpy(buf, "something strange");
2299 #endif
2300         }
2301
2302         for (c = buf; *c; c++)
2303         {
2304                 int i;
2305                 for (i = 0; simplify_list[i][1]; i++)
2306                 {
2307                         cptr org_w = simplify_list[i][0];
2308
2309                         if (*org_w == '^')
2310                         {
2311                                 if (c == buf)
2312                                         org_w++;
2313                                 else
2314                                         continue;
2315                         }
2316
2317                         if (!strncmp(c, org_w, strlen(org_w)))
2318                         {
2319                                 char *s = c;
2320                                 cptr tmp = simplify_list[i][1];
2321                                 while (*tmp)
2322                                         *s++ = *tmp++;
2323                                 tmp = c + strlen(org_w);
2324                                 while (*tmp)
2325                                         *s++ = *tmp++;
2326                                 *s = '\0';
2327                         }
2328                 }
2329         }
2330
2331         c = buf;
2332         len = 0;
2333         /* È¾³Ñ 12 Ê¸»úʬ¤ÇÀÚ¤ë */
2334         while(*c)
2335         {
2336 #ifdef JP
2337                 if(iskanji(*c))
2338                 {
2339                         if(len + 2 > 12) break;
2340                         c+=2;
2341                         len+=2;
2342                 }
2343                 else
2344 #endif
2345                 {
2346                         if(len + 1 > 12) break;
2347                         c++;
2348                         len++;
2349                 }
2350         }
2351         *c='\0';
2352         Term_putstr(0, y, 12, attr, buf);
2353 }
2354
2355 /*
2356  * Display a "small-scale" map of the dungeon in the active Term
2357  */
2358 void display_map(int *cy, int *cx)
2359 {
2360         int i, j, x, y;
2361
2362         byte ta;
2363         char tc;
2364
2365         byte tp;
2366
2367         byte bigma[MAX_HGT+2][MAX_WID+2];
2368         char bigmc[MAX_HGT+2][MAX_WID+2];
2369         byte bigmp[MAX_HGT+2][MAX_WID+2];
2370
2371         byte ma[SCREEN_HGT + 2][SCREEN_WID + 2];
2372         char mc[SCREEN_HGT + 2][SCREEN_WID + 2];
2373
2374         byte mp[SCREEN_HGT + 2][SCREEN_WID + 2];
2375
2376         bool old_view_special_lite;
2377         bool old_view_granite_lite;
2378
2379         bool fake_monochrome = (!use_graphics || streq(ANGBAND_SYS, "ibm"));
2380
2381         int yrat = cur_hgt / SCREEN_HGT;
2382         int xrat = cur_wid / SCREEN_WID;
2383
2384
2385         int match_autopick_yx[SCREEN_HGT+2][SCREEN_WID+2];
2386         object_type *object_autopick_yx[SCREEN_HGT+2][SCREEN_WID+2];
2387
2388         /* Save lighting effects */
2389         old_view_special_lite = view_special_lite;
2390         old_view_granite_lite = view_granite_lite;
2391
2392         /* Disable lighting effects */
2393         view_special_lite = FALSE;
2394         view_granite_lite = FALSE;
2395
2396         /* Clear the chars and attributes */
2397         for (y = 0; y < SCREEN_HGT + 2; ++y)
2398         {
2399                 for (x = 0; x < SCREEN_WID + 2; ++x)
2400                 {
2401                         match_autopick_yx[y][x] = -1;
2402                         object_autopick_yx[y][x] = NULL;
2403
2404                         /* Nothing here */
2405                         ma[y][x] = TERM_WHITE;
2406                         mc[y][x] = ' ';
2407
2408                         /* No priority */
2409                         mp[y][x] = 0;
2410                 }
2411         }
2412
2413         for (j = 0; j < cur_hgt + 2; ++j)
2414         {
2415                 for (i = 0; i < cur_wid + 2; ++i)
2416                 {
2417                         /* Nothing here */
2418                         bigma[j][i] = TERM_WHITE;
2419                         bigmc[j][i] = ' ';
2420
2421                         /* No priority */
2422                         bigmp[j][i] = 0;
2423                 }
2424         }
2425
2426         /* Fill in the map */
2427         for (i = 0; i < cur_wid; ++i)
2428         {
2429                 for (j = 0; j < cur_hgt; ++j)
2430                 {
2431                         /* Location */
2432                         x = i / xrat + 1;
2433                         y = j / yrat + 1;
2434
2435                         match_autopick=-1;
2436                         autopick_obj=NULL;
2437                         feat_priority = -1;
2438
2439                         /* Extract the current attr/char at that map location */
2440 #ifdef USE_TRANSPARENCY
2441                         map_info(j, i, &ta, &tc, &ta, &tc);
2442 #else /* USE_TRANSPARENCY */
2443                         map_info(j, i, &ta, &tc);
2444 #endif /* USE_TRANSPARENCY */
2445
2446                         /* Extract the priority */
2447                         tp = feat_priority;
2448
2449                         if(match_autopick!=-1
2450                            && (match_autopick_yx[y][x] == -1
2451                                || match_autopick_yx[y][x] > match_autopick))
2452                         {
2453                                 match_autopick_yx[y][x] = match_autopick;
2454                                 object_autopick_yx[y][x] = autopick_obj;
2455                                 tp = 0x7f;
2456                         }
2457
2458                         /* Save the char, attr and priority */
2459                         bigmc[j+1][i+1] = tc;
2460                         bigma[j+1][i+1] = ta;
2461                         bigmp[j+1][i+1] = tp;
2462                 }
2463         }
2464
2465         for (j = 0; j < cur_hgt; ++j)
2466         {
2467                 for (i = 0; i < cur_wid; ++i)
2468                 {
2469                         /* Location */
2470                         x = i / xrat + 1;
2471                         y = j / yrat + 1;
2472
2473                         tc = bigmc[j+1][i+1];
2474                         ta = bigma[j+1][i+1];
2475                         tp = bigmp[j+1][i+1];
2476
2477                         /* rare feature has more priority */
2478                         if (mp[y][x] == tp)
2479                         {
2480                                 int t;
2481                                 int cnt = 0;
2482
2483                                 for (t = 0; t < 8; t++)
2484                                 {
2485                                         if (tc == bigmc[j+1+ddy_cdd[t]][i+1+ddx_cdd[t]] &&
2486                                             ta == bigma[j+1+ddy_cdd[t]][i+1+ddx_cdd[t]])
2487                                                 cnt++;
2488                                 }
2489                                 if (cnt <= 4)
2490                                         tp++;
2491                         }
2492
2493                         /* Save "best" */
2494                         if (mp[y][x] < tp)
2495                         {
2496                                 /* Save the char, attr and priority */
2497                                 mc[y][x] = tc;
2498                                 ma[y][x] = ta;
2499                                 mp[y][x] = tp;
2500                         }
2501                 }
2502         }
2503
2504
2505         /* Corners */
2506         x = SCREEN_WID + 1;
2507         y = SCREEN_HGT + 1;
2508
2509         /* Draw the corners */
2510         mc[0][0] = mc[0][x] = mc[y][0] = mc[y][x] = '+';
2511
2512         /* Draw the horizontal edges */
2513         for (x = 1; x <= SCREEN_WID; x++) mc[0][x] = mc[y][x] = '-';
2514
2515         /* Draw the vertical edges */
2516         for (y = 1; y <= SCREEN_HGT; y++) mc[y][0] = mc[y][x] = '|';
2517
2518
2519         /* Display each map line in order */
2520         for (y = 0; y < SCREEN_HGT+2; ++y)
2521         {
2522                 /* Start a new line */
2523                 Term_gotoxy(COL_MAP, y);
2524
2525                 /* Display the line */
2526                 for (x = 0; x < SCREEN_WID+2; ++x)
2527                 {
2528                         ta = ma[y][x];
2529                         tc = mc[y][x];
2530
2531                         /* Hack -- fake monochrome */
2532                         if (fake_monochrome)
2533                         {
2534                                 if (world_monster) ta = TERM_DARK;
2535                                 else if (p_ptr->invuln || world_player) ta = TERM_WHITE;
2536                                 else if ((p_ptr->pclass == CLASS_BARD) && (p_ptr->magic_num1[0] == MUSIC_INVULN)) ta = TERM_WHITE;
2537                                 else if (p_ptr->wraith_form) ta = TERM_L_DARK;
2538                         }
2539
2540                         /* Add the character */
2541                         Term_addch(ta, tc);
2542                 }
2543         }
2544
2545
2546         for (y = 1; y < SCREEN_HGT+1; ++y)
2547         {
2548           match_autopick = -1;
2549           for (x = 1; x <= SCREEN_WID; x++){
2550             if (match_autopick_yx[y][x] != -1 &&
2551                 (match_autopick > match_autopick_yx[y][x] ||
2552                  match_autopick == -1)){
2553               match_autopick = match_autopick_yx[y][x];
2554               autopick_obj = object_autopick_yx[y][x];
2555             }
2556           }
2557
2558           /* Clear old display */
2559           Term_putstr(0, y, 12, 0, "            ");
2560
2561           if (match_autopick != -1)
2562 #if 1
2563                   display_shortened_item_name(autopick_obj, y);
2564 #else
2565           {
2566                   char buf[13] = "\0";
2567                   strncpy(buf,autopick_name[match_autopick],12);
2568                   buf[12] = '\0';
2569                   put_str(buf,y,0); 
2570           }
2571 #endif
2572
2573         }
2574
2575         /* Player location */
2576         (*cy) = py / yrat + 1 + ROW_MAP;
2577         (*cx) = px / xrat + 1 + COL_MAP;
2578
2579
2580         /* Restore lighting effects */
2581         view_special_lite = old_view_special_lite;
2582         view_granite_lite = old_view_granite_lite;
2583 }
2584
2585
2586 /*
2587  * Display a "small-scale" map of the dungeon for the player
2588  *
2589  * Currently, the "player" is displayed on the map.  XXX XXX XXX
2590  */
2591 void do_cmd_view_map(void)
2592 {
2593         int cy, cx;
2594
2595
2596         /* Save the screen */
2597         screen_save();
2598
2599         /* Note */
2600 #ifdef JP
2601 prt("¤ªÂÔ¤Á²¼¤µ¤¤...", 0, 0);
2602 #else
2603         prt("Please wait...", 0, 0);
2604 #endif
2605
2606         /* Flush */
2607         Term_fresh();
2608
2609         /* Clear the screen */
2610         Term_clear();
2611
2612         display_autopick = 0;
2613
2614         /* Display the map */
2615         display_map(&cy, &cx);
2616
2617         /* Wait for it */
2618         if(max_autopick && !p_ptr->wild_mode)
2619         {
2620                 display_autopick = ITEM_DISPLAY;
2621
2622                 while (1)
2623                 {
2624                         int i;
2625                         byte flag;
2626
2627 #ifdef JP
2628                         put_str("²¿¤«¥­¡¼¤ò²¡¤·¤Æ¤¯¤À¤µ¤¤('M':½¦¤¦ 'N':ÊüÃÖ 'D':M+N 'K':²õ¤¹¥¢¥¤¥Æ¥à¤òɽ¼¨)", 23, 1);
2629 #else
2630                         put_str(" Hit M, N(for ~), K(for !), or D(same as M+N) to display auto-picker items.", 23, 1);
2631 #endif
2632
2633                         /* Hilite the player */
2634                         move_cursor(cy, cx);
2635
2636                         i = inkey();
2637
2638                         if ('M' == i)
2639                                 flag = DO_AUTOPICK;
2640                         else if ('N' == i)
2641                                 flag = DONT_AUTOPICK;
2642                         else if ('K' == i)
2643                                 flag = DO_AUTODESTROY;
2644                         else if ('D' == i)
2645                                 flag = (DO_AUTOPICK | DONT_AUTOPICK);
2646                         else
2647                                 break;
2648
2649                         Term_fresh();
2650                         
2651                         if (~display_autopick & flag)
2652                                 display_autopick |= flag;
2653                         else
2654                                 display_autopick &= ~flag;
2655                         /* Display the map */
2656                         display_map(&cy, &cx);
2657                 }
2658                 
2659                 display_autopick = 0;
2660
2661         }
2662         else
2663         {
2664 #ifdef JP
2665                 put_str("²¿¤«¥­¡¼¤ò²¡¤¹¤È¥²¡¼¥à¤ËÌá¤ê¤Þ¤¹", 23, 30);
2666 #else
2667                 put_str("Hit any key to continue", 23, 30);
2668 #endif          /* Hilite the player */
2669                 move_cursor(cy, cx);
2670                 /* Get any key */
2671                 inkey();
2672         }
2673
2674         /* Restore the screen */
2675         screen_load();
2676 }
2677
2678
2679
2680
2681
2682 /*
2683  * Some comments on the cave grid flags.  -BEN-
2684  *
2685  *
2686  * One of the major bottlenecks in previous versions of Angband was in
2687  * the calculation of "line of sight" from the player to various grids,
2688  * such as monsters.  This was such a nasty bottleneck that a lot of
2689  * silly things were done to reduce the dependancy on "line of sight",
2690  * for example, you could not "see" any grids in a lit room until you
2691  * actually entered the room, and there were all kinds of bizarre grid
2692  * flags to enable this behavior.  This is also why the "call light"
2693  * spells always lit an entire room.
2694  *
2695  * The code below provides functions to calculate the "field of view"
2696  * for the player, which, once calculated, provides extremely fast
2697  * calculation of "line of sight from the player", and to calculate
2698  * the "field of torch lite", which, again, once calculated, provides
2699  * extremely fast calculation of "which grids are lit by the player's
2700  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
2701  * "GRID_LITE", as appropriate, these functions maintain an array for
2702  * each of these two flags, each array containing the locations of all
2703  * of the grids marked with the appropriate flag, which can be used to
2704  * very quickly scan through all of the grids in a given set.
2705  *
2706  * To allow more "semantically valid" field of view semantics, whenever
2707  * the field of view (or the set of torch lit grids) changes, all of the
2708  * grids in the field of view (or the set of torch lit grids) are "drawn"
2709  * so that changes in the world will become apparent as soon as possible.
2710  * This has been optimized so that only grids which actually "change" are
2711  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
2712  * of the grids which are entering or leaving the relevent set of grids.
2713  *
2714  * These new methods are so efficient that the old nasty code was removed.
2715  *
2716  * Note that there is no reason to "update" the "viewable space" unless
2717  * the player "moves", or walls/doors are created/destroyed, and there
2718  * is no reason to "update" the "torch lit grids" unless the field of
2719  * view changes, or the "light radius" changes.  This means that when
2720  * the player is resting, or digging, or doing anything that does not
2721  * involve movement or changing the state of the dungeon, there is no
2722  * need to update the "view" or the "lite" regions, which is nice.
2723  *
2724  * Note that the calls to the nasty "los()" function have been reduced
2725  * to a bare minimum by the use of the new "field of view" calculations.
2726  *
2727  * I wouldn't be surprised if slight modifications to the "update_view()"
2728  * function would allow us to determine "reverse line-of-sight" as well
2729  * as "normal line-of-sight", which would allow monsters to use a more
2730  * "correct" calculation to determine if they can "see" the player.  For
2731  * now, monsters simply "cheat" somewhat and assume that if the player
2732  * has "line of sight" to the monster, then the monster can "pretend"
2733  * that it has "line of sight" to the player.
2734  *
2735  *
2736  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
2737  * grid and maintains an array of all "CAVE_LITE" grids.
2738  *
2739  * This set of grids is the complete set of all grids which are lit by
2740  * the players light source, which allows the "player_can_see_bold()"
2741  * function to work very quickly.
2742  *
2743  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
2744  * fact, the player (unless blind) can always "see" all grids which are
2745  * marked as "CAVE_LITE", unless they are "off screen".
2746  *
2747  *
2748  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
2749  * grid and maintains an array of all "CAVE_VIEW" grids.
2750  *
2751  * This set of grids is the complete set of all grids within line of sight
2752  * of the player, allowing the "player_has_los_bold()" macro to work very
2753  * quickly.
2754  *
2755  *
2756  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
2757  * temporary internal flag to mark those grids which are not only in view,
2758  * but which are also "easily" in line of sight of the player.  This flag
2759  * is always cleared when we are done.
2760  *
2761  *
2762  * The current "update_lite()" and "update_view()" algorithms use the
2763  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
2764  * to keep track of which grids were previously marked as "CAVE_LITE" or
2765  * "CAVE_VIEW", which allows us to optimize the "screen updates".
2766  *
2767  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
2768  * for various other purposes, such as spreading lite or darkness during
2769  * "lite_room()" / "unlite_room()", and for calculating monster flow.
2770  *
2771  *
2772  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
2773  * in some way permanently lit.  However, for the player to "see" anything
2774  * in the grid, as determined by "player_can_see()", the player must not be
2775  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
2776  * grids, even if marked as "perma lit", are only illuminated if they touch
2777  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
2778  *
2779  *
2780  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
2781  * that even if the player cannot "see" the grid, he "knows" the terrain in
2782  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
2783  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
2784  * or when one of the "memorize floor grids" options induces memorization.
2785  *
2786  * Objects are "memorized" in a different way, using a special "marked" flag
2787  * on the object itself, which is set when an object is observed or detected.
2788  *
2789  *
2790  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
2791  * and should be illuminated by "lite room" and "darkness" spells.
2792  *
2793  *
2794  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
2795  * and should be unavailable for "teleportation" destinations.
2796  *
2797  *
2798  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
2799  * which is observed, and the "view_torch_grids" allows the player to memorize
2800  * every torch-lit grid.  The player will always memorize important walls,
2801  * doors, stairs, and other terrain features, as well as any "detected" grids.
2802  *
2803  * Note that the new "update_view()" method allows, among other things, a room
2804  * to be "partially" seen as the player approaches it, with a growing cone of
2805  * floor appearing as the player gets closer to the door.  Also, by not turning
2806  * on the "memorize perma-lit grids" option, the player will only "see" those
2807  * floor grids which are actually in line of sight.
2808  *
2809  * And my favorite "plus" is that you can now use a special option to draw the
2810  * "floors" in the "viewable region" brightly (actually, to draw the *other*
2811  * grids dimly), providing a "pretty" effect as the player runs around, and
2812  * to efficiently display the "torch lite" in a special color.
2813  *
2814  *
2815  * Some comments on the "update_view()" algorithm...
2816  *
2817  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
2818  * and only has to call "los()" on the borderline cases.  The major axes/diags
2819  * even terminate early when they hit walls.  I need to find a quick way
2820  * to "terminate" the other scans.
2821  *
2822  * Note that in the worst case (a big empty area with say 5% scattered walls),
2823  * each of the 1500 or so nearby grids is checked once, most of them getting
2824  * an "instant" rating, and only a small portion requiring a call to "los()".
2825  *
2826  * The only time that the algorithm appears to be "noticeably" too slow is
2827  * when running, and this is usually only important in town, since the town
2828  * provides about the worst scenario possible, with large open regions and
2829  * a few scattered obstructions.  There is a special "efficiency" option to
2830  * allow the player to reduce his field of view in town, if needed.
2831  *
2832  * In the "best" case (say, a normal stretch of corridor), the algorithm
2833  * makes one check for each viewable grid, and makes no calls to "los()".
2834  * So running in corridors is very fast, and if a lot of monsters are
2835  * nearby, it is much faster than the old methods.
2836  *
2837  * Note that resting, most normal commands, and several forms of running,
2838  * plus all commands executed near large groups of monsters, are strictly
2839  * more efficient with "update_view()" that with the old "compute los() on
2840  * demand" method, primarily because once the "field of view" has been
2841  * calculated, it does not have to be recalculated until the player moves
2842  * (or a wall or door is created or destroyed).
2843  *
2844  * Note that we no longer have to do as many "los()" checks, since once the
2845  * "view" region has been built, very few things cause it to be "changed"
2846  * (player movement, and the opening/closing of doors, changes in wall status).
2847  * Note that door/wall changes are only relevant when the door/wall itself is
2848  * in the "view" region.
2849  *
2850  * The algorithm seems to only call "los()" from zero to ten times, usually
2851  * only when coming down a corridor into a room, or standing in a room, just
2852  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
2853  * we will be reducing the calls to "los()".
2854  *
2855  * I am thinking in terms of an algorithm that "walks" from the central point
2856  * out to the maximal "distance", at each point, determining the "view" code
2857  * (above).  For each grid not on a major axis or diagonal, the "view" code
2858  * depends on the "cave_floor_bold()" and "view" of exactly two other grids
2859  * (the one along the nearest diagonal, and the one next to that one, see
2860  * "update_view_aux()"...).
2861  *
2862  * We "memorize" the viewable space array, so that at the cost of under 3000
2863  * bytes, we reduce the time taken by "forget_view()" to one assignment for
2864  * each grid actually in the "viewable space".  And for another 3000 bytes,
2865  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
2866  * are also used by other routines, thus reducing the cost to almost nothing.
2867  *
2868  * A similar thing is done for "forget_lite()" in which case the savings are
2869  * much less, but save us from doing bizarre maintenance checking.
2870  *
2871  * In the worst "normal" case (in the middle of the town), the reachable space
2872  * actually reaches to more than half of the largest possible "circle" of view,
2873  * or about 800 grids, and in the worse case (in the middle of a dungeon level
2874  * where all the walls have been removed), the reachable space actually reaches
2875  * the theoretical maximum size of just under 1500 grids.
2876  *
2877  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
2878  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
2879  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
2880  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
2881  * entire possible space (including initialization) in one step per grid.  If
2882  * we do the "clearing" as a separate step (and use an array of "view" grids),
2883  * then the clearing will take as many steps as grids that were viewed, and the
2884  * algorithm will be able to "stop" scanning at various points.
2885  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
2886  */
2887
2888
2889
2890
2891
2892
2893
2894
2895 /*
2896  * Actually erase the entire "lite" array, redrawing every grid
2897  */
2898 void forget_lite(void)
2899 {
2900         int i, x, y;
2901
2902         /* None to forget */
2903         if (!lite_n) return;
2904
2905         /* Clear them all */
2906         for (i = 0; i < lite_n; i++)
2907         {
2908                 y = lite_y[i];
2909                 x = lite_x[i];
2910
2911                 /* Forget "LITE" flag */
2912                 cave[y][x].info &= ~(CAVE_LITE);
2913
2914                 /* Redraw */
2915                 lite_spot(y, x);
2916         }
2917
2918         /* None left */
2919         lite_n = 0;
2920 }
2921
2922
2923 /*
2924  * XXX XXX XXX
2925  *
2926  * This macro allows us to efficiently add a grid to the "lite" array,
2927  * note that we are never called for illegal grids, or for grids which
2928  * have already been placed into the "lite" array, and we are never
2929  * called when the "lite" array is full.
2930  */
2931 #define cave_lite_hack(Y,X) \
2932 {\
2933     if (!(cave[Y][X].info & (CAVE_LITE))) { \
2934     cave[Y][X].info |= (CAVE_LITE); \
2935     lite_y[lite_n] = (Y); \
2936     lite_x[lite_n] = (X); \
2937                             lite_n++;} \
2938 }
2939
2940
2941 /*
2942  * Update the set of grids "illuminated" by the player's lite.
2943  *
2944  * This routine needs to use the results of "update_view()"
2945  *
2946  * Note that "blindness" does NOT affect "torch lite".  Be careful!
2947  *
2948  * We optimize most lites (all non-artifact lites) by using "obvious"
2949  * facts about the results of "small" lite radius, and we attempt to
2950  * list the "nearby" grids before the more "distant" ones in the
2951  * array of torch-lit grids.
2952  *
2953  * We will correctly handle "large" radius lites, though currently,
2954  * it is impossible for the player to have more than radius 3 lite.
2955  *
2956  * We assume that "radius zero" lite is in fact no lite at all.
2957  *
2958  *     Torch     Lantern     Artifacts
2959  *     (etc)
2960  *                              ***
2961  *                 ***         *****
2962  *      ***       *****       *******
2963  *      *@*       **@**       ***@***
2964  *      ***       *****       *******
2965  *                 ***         *****
2966  *                              ***
2967  */
2968 void update_lite(void)
2969 {
2970         int i, x, y, min_x, max_x, min_y, max_y;
2971         int p = p_ptr->cur_lite;
2972
2973         /*** Special case ***/
2974
2975         /* Hack -- Player has no lite */
2976         if (p <= 0)
2977         {
2978                 /* Forget the old lite */
2979                 forget_lite();
2980
2981                 /* Draw the player */
2982                 lite_spot(py, px);
2983         }
2984
2985
2986         /*** Save the old "lite" grids for later ***/
2987
2988         /* Clear them all */
2989         for (i = 0; i < lite_n; i++)
2990         {
2991                 y = lite_y[i];
2992                 x = lite_x[i];
2993
2994                 /* Mark the grid as not "lite" */
2995                 cave[y][x].info &= ~(CAVE_LITE);
2996
2997                 /* Mark the grid as "seen" */
2998                 cave[y][x].info |= (CAVE_TEMP);
2999
3000                 /* Add it to the "seen" set */
3001                 temp_y[temp_n] = y;
3002                 temp_x[temp_n] = x;
3003                 temp_n++;
3004         }
3005
3006         /* None left */
3007         lite_n = 0;
3008
3009
3010         /*** Collect the new "lite" grids ***/
3011
3012         /* Radius 1 -- torch radius */
3013         if (p >= 1)
3014         {
3015                 /* Player grid */
3016                 cave_lite_hack(py, px);
3017
3018                 /* Adjacent grid */
3019                 cave_lite_hack(py+1, px);
3020                 cave_lite_hack(py-1, px);
3021                 cave_lite_hack(py, px+1);
3022                 cave_lite_hack(py, px-1);
3023
3024                 /* Diagonal grids */
3025                 cave_lite_hack(py+1, px+1);
3026                 cave_lite_hack(py+1, px-1);
3027                 cave_lite_hack(py-1, px+1);
3028                 cave_lite_hack(py-1, px-1);
3029         }
3030
3031         /* Radius 2 -- lantern radius */
3032         if (p >= 2)
3033         {
3034                 /* South of the player */
3035                 if (cave_floor_bold(py+1, px))
3036                 {
3037                         cave_lite_hack(py+2, px);
3038                         cave_lite_hack(py+2, px+1);
3039                         cave_lite_hack(py+2, px-1);
3040                 }
3041
3042                 /* North of the player */
3043                 if (cave_floor_bold(py-1, px))
3044                 {
3045                         cave_lite_hack(py-2, px);
3046                         cave_lite_hack(py-2, px+1);
3047                         cave_lite_hack(py-2, px-1);
3048                 }
3049
3050                 /* East of the player */
3051                 if (cave_floor_bold(py, px+1))
3052                 {
3053                         cave_lite_hack(py, px+2);
3054                         cave_lite_hack(py+1, px+2);
3055                         cave_lite_hack(py-1, px+2);
3056                 }
3057
3058                 /* West of the player */
3059                 if (cave_floor_bold(py, px-1))
3060                 {
3061                         cave_lite_hack(py, px-2);
3062                         cave_lite_hack(py+1, px-2);
3063                         cave_lite_hack(py-1, px-2);
3064                 }
3065         }
3066
3067         /* Radius 3+ -- artifact radius */
3068         if (p >= 3)
3069         {
3070                 int d;
3071
3072                 /* Paranoia -- see "LITE_MAX" */
3073                 if (p > 5) p = 5;
3074
3075                 /* South-East of the player */
3076                 if (cave_floor_bold(py+1, px+1))
3077                 {
3078                         cave_lite_hack(py+2, px+2);
3079                 }
3080
3081                 /* South-West of the player */
3082                 if (cave_floor_bold(py+1, px-1))
3083                 {
3084                         cave_lite_hack(py+2, px-2);
3085                 }
3086
3087                 /* North-East of the player */
3088                 if (cave_floor_bold(py-1, px+1))
3089                 {
3090                         cave_lite_hack(py-2, px+2);
3091                 }
3092
3093                 /* North-West of the player */
3094                 if (cave_floor_bold(py-1, px-1))
3095                 {
3096                         cave_lite_hack(py-2, px-2);
3097                 }
3098
3099                 /* Maximal north */
3100                 min_y = py - p;
3101                 if (min_y < 0) min_y = 0;
3102
3103                 /* Maximal south */
3104                 max_y = py + p;
3105                 if (max_y > cur_hgt-1) max_y = cur_hgt-1;
3106
3107                 /* Maximal west */
3108                 min_x = px - p;
3109                 if (min_x < 0) min_x = 0;
3110
3111                 /* Maximal east */
3112                 max_x = px + p;
3113                 if (max_x > cur_wid-1) max_x = cur_wid-1;
3114
3115                 /* Scan the maximal box */
3116                 for (y = min_y; y <= max_y; y++)
3117                 {
3118                         for (x = min_x; x <= max_x; x++)
3119                         {
3120                                 int dy = (py > y) ? (py - y) : (y - py);
3121                                 int dx = (px > x) ? (px - x) : (x - px);
3122
3123                                 /* Skip the "central" grids (above) */
3124                                 if ((dy <= 2) && (dx <= 2)) continue;
3125
3126                                 /* Hack -- approximate the distance */
3127                                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
3128
3129                                 /* Skip distant grids */
3130                                 if (d > p) continue;
3131
3132                                 /* Viewable, nearby, grids get "torch lit" */
3133                                 if (player_has_los_bold(y, x))
3134                                 {
3135                                         /* This grid is "torch lit" */
3136                                         cave_lite_hack(y, x);
3137                                 }
3138                         }
3139                 }
3140         }
3141
3142
3143         /*** Complete the algorithm ***/
3144
3145         /* Draw the new grids */
3146         for (i = 0; i < lite_n; i++)
3147         {
3148                 y = lite_y[i];
3149                 x = lite_x[i];
3150
3151                 /* Update fresh grids */
3152                 if (cave[y][x].info & (CAVE_TEMP)) continue;
3153
3154                 /* Note */
3155                 note_spot(y, x);
3156
3157                 /* Redraw */
3158                 lite_spot(y, x);
3159         }
3160
3161         /* Clear them all */
3162         for (i = 0; i < temp_n; i++)
3163         {
3164                 y = temp_y[i];
3165                 x = temp_x[i];
3166
3167                 /* No longer in the array */
3168                 cave[y][x].info &= ~(CAVE_TEMP);
3169
3170                 /* Update stale grids */
3171                 if (cave[y][x].info & (CAVE_LITE)) continue;
3172
3173                 /* Redraw */
3174                 lite_spot(y, x);
3175         }
3176
3177         /* None left */
3178         temp_n = 0;
3179 }
3180
3181
3182 static bool mon_invis;
3183
3184 /*
3185  * Add a square to the changes array
3186  */
3187 static void mon_lite_hack(int y, int x)
3188 {
3189         cave_type *c_ptr;
3190
3191         /* Out of bounds */
3192         if (!in_bounds2(y, x)) return;
3193
3194         c_ptr = &cave[y][x];
3195
3196         /* Want a unlit square in view of the player */
3197         if ((c_ptr->info & (CAVE_MNLT | CAVE_VIEW)) != CAVE_VIEW) return;
3198
3199         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
3200         if (!cave_floor_grid(c_ptr) && mon_invis) return;
3201
3202         /* Save this square */
3203         if (temp_n < TEMP_MAX)
3204         {
3205                 temp_x[temp_n] = x;
3206                 temp_y[temp_n] = y;
3207                 temp_n++;
3208         }
3209
3210         /* Light it */
3211         c_ptr->info |= CAVE_MNLT;
3212 }
3213
3214  
3215
3216
3217 /*
3218  * Update squares illuminated by monsters.
3219  *
3220  * Hack - use the CAVE_ROOM flag (renamed to be CAVE_MNLT) to
3221  * denote squares illuminated by monsters.
3222  *
3223  * The CAVE_TEMP flag is used to store the state during the
3224  * updating.  Only squares in view of the player, whos state
3225  * changes are drawn via lite_spot().
3226  */
3227 void update_mon_lite(void)
3228 {
3229         int i, rad;
3230         cave_type *c_ptr;
3231
3232         s16b fx, fy;
3233
3234         s16b end_temp;
3235
3236         bool daytime = ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2));
3237
3238         /* Clear all monster lit squares */
3239         for (i = 0; i < mon_lite_n; i++)
3240         {
3241                 /* Point to grid */
3242                 c_ptr = &cave[mon_lite_y[i]][mon_lite_x[i]];
3243
3244                 /* Set temp flag */
3245                 c_ptr->info |= (CAVE_TEMP);
3246
3247                 /* Clear monster illumination flag */
3248                 c_ptr->info &= ~(CAVE_MNLT);
3249         }
3250
3251         /* Empty temp list of new squares to lite up */
3252         temp_n = 0;
3253
3254         /* Loop through monsters, adding newly lit squares to changes list */
3255         for (i = 1; i < m_max; i++)
3256         {
3257                 monster_type *m_ptr = &m_list[i];
3258                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
3259
3260                 /* Skip dead monsters */
3261                 if (!m_ptr->r_idx) continue;
3262
3263                 /* Is it too far away? */
3264                 if (m_ptr->cdis > ((d_info[dungeon_type].flags1 & DF1_DARKNESS) ? MAX_SIGHT / 2 + 1 : MAX_SIGHT + 3)) continue;
3265
3266                 /* Get lite radius */
3267                 rad = 0;
3268
3269                 /* Note the radii are cumulative */
3270                 if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_SELF_LITE_1)) rad++;
3271                 if (r_ptr->flags7 & (RF7_HAS_LITE_2 | RF7_SELF_LITE_2)) rad += 2;
3272
3273                 /* Exit if has no light */
3274                 if (!rad) continue;
3275                 if (!(r_ptr->flags7 & (RF7_SELF_LITE_1 | RF7_SELF_LITE_2)) && (m_ptr->csleep || (!dun_level && daytime) || p_ptr->inside_battle)) continue;
3276
3277                 if (world_monster) continue;
3278
3279                 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) rad = 1;
3280
3281                 /* Access the location */
3282                 fx = m_ptr->fx;
3283                 fy = m_ptr->fy;
3284
3285                 /* Is the monster visible? */
3286                 mon_invis = !(cave[fy][fx].info & CAVE_VIEW);
3287
3288                 /* The square it is on */
3289                 mon_lite_hack(fy, fx);
3290
3291                 /* Adjacent squares */
3292                 mon_lite_hack(fy + 1, fx);
3293                 mon_lite_hack(fy - 1, fx);
3294                 mon_lite_hack(fy, fx + 1);
3295                 mon_lite_hack(fy, fx - 1);
3296                 mon_lite_hack(fy + 1, fx + 1);
3297                 mon_lite_hack(fy + 1, fx - 1);
3298                 mon_lite_hack(fy - 1, fx + 1);
3299                 mon_lite_hack(fy - 1, fx - 1);
3300
3301                 /* Radius 2 */
3302                 if (rad >= 2)
3303                 {
3304                         /* South of the monster */
3305                         if (cave_floor_bold(fy + 1, fx))
3306                         {
3307                                 mon_lite_hack(fy + 2, fx + 1);
3308                                 mon_lite_hack(fy + 2, fx);
3309                                 mon_lite_hack(fy + 2, fx - 1);
3310
3311                                 c_ptr = &cave[fy + 2][fx];
3312
3313                                 /* Radius 3 */
3314                                 if ((rad == 3) && cave_floor_grid(c_ptr))
3315                                 {
3316                                         mon_lite_hack(fy + 3, fx + 1);
3317                                         mon_lite_hack(fy + 3, fx);
3318                                         mon_lite_hack(fy + 3, fx - 1);
3319                                 }
3320                         }
3321
3322                         /* North of the monster */
3323                         if (cave_floor_bold(fy - 1, fx))
3324                         {
3325                                 mon_lite_hack(fy - 2, fx + 1);
3326                                 mon_lite_hack(fy - 2, fx);
3327                                 mon_lite_hack(fy - 2, fx - 1);
3328
3329                                 c_ptr = &cave[fy - 2][fx];
3330
3331                                 /* Radius 3 */
3332                                 if ((rad == 3) && cave_floor_grid(c_ptr))
3333                                 {
3334                                         mon_lite_hack(fy - 3, fx + 1);
3335                                         mon_lite_hack(fy - 3, fx);
3336                                         mon_lite_hack(fy - 3, fx - 1);
3337                                 }
3338                         }
3339
3340                         /* East of the monster */
3341                         if (cave_floor_bold(fy, fx + 1))
3342                         {
3343                                 mon_lite_hack(fy + 1, fx + 2);
3344                                 mon_lite_hack(fy, fx + 2);
3345                                 mon_lite_hack(fy - 1, fx + 2);
3346
3347                                 c_ptr = &cave[fy][fx + 2];
3348
3349                                 /* Radius 3 */
3350                                 if ((rad == 3) && cave_floor_grid(c_ptr))
3351                                 {
3352                                         mon_lite_hack(fy + 1, fx + 3);
3353                                         mon_lite_hack(fy, fx + 3);
3354                                         mon_lite_hack(fy - 1, fx + 3);
3355                                 }
3356                         }
3357
3358                         /* West of the monster */
3359                         if (cave_floor_bold(fy, fx - 1))
3360                         {
3361                                 mon_lite_hack(fy + 1, fx - 2);
3362                                 mon_lite_hack(fy, fx - 2);
3363                                 mon_lite_hack(fy - 1, fx - 2);
3364
3365                                 c_ptr = &cave[fy][fx - 2];
3366
3367                                 /* Radius 3 */
3368                                 if ((rad == 3) && cave_floor_grid(c_ptr))
3369                                 {
3370                                         mon_lite_hack(fy + 1, fx - 3);
3371                                         mon_lite_hack(fy, fx - 3);
3372                                         mon_lite_hack(fy - 1, fx - 3);
3373                                 }
3374                         }
3375                 }
3376
3377                 /* Radius 3 */
3378                 if (rad == 3)
3379                 {
3380                         /* South-East of the monster */
3381                         if (cave_floor_bold(fy + 1, fx + 1))
3382                         {
3383                                 mon_lite_hack(fy + 2, fx + 2);
3384                         }
3385
3386                         /* South-West of the monster */
3387                         if (cave_floor_bold(fy + 1, fx - 1))
3388                         {
3389                                 mon_lite_hack(fy + 2, fx - 2);
3390                         }
3391
3392                         /* North-East of the monster */
3393                         if (cave_floor_bold(fy - 1, fx + 1))
3394                         {
3395                                 mon_lite_hack(fy - 2, fx + 2);
3396                         }
3397
3398                         /* North-West of the monster */
3399                         if (cave_floor_bold(fy - 1, fx - 1))
3400                         {
3401                                 mon_lite_hack(fy - 2, fx - 2);
3402                         }
3403                 }
3404         }
3405
3406         /* Save end of list of new squares */
3407         end_temp = temp_n;
3408
3409         /*
3410          * Look at old set flags to see if there are any changes.
3411          */
3412         for (i = 0; i < mon_lite_n; i++)
3413         {
3414                 fx = mon_lite_x[i];
3415                 fy = mon_lite_y[i];
3416
3417                 if (!in_bounds2(fy, fx)) continue;
3418
3419                 /* Point to grid */
3420                 c_ptr = &cave[fy][fx];
3421
3422                 /* It it no longer lit? */
3423                 if (!(c_ptr->info & CAVE_MNLT) && player_has_los_grid(c_ptr))
3424                 {
3425                         /* It is now unlit */
3426                         note_spot(fy, fx);
3427                         lite_spot(fy, fx);
3428                 }
3429
3430                 /* Add to end of temp array */
3431                 temp_x[temp_n] = (byte)fx;
3432                 temp_y[temp_n] = (byte)fy;
3433                 temp_n++;
3434         }
3435
3436         /* Clear the lite array */
3437         mon_lite_n = 0;
3438
3439         /* Copy the temp array into the lit array lighting the new squares. */
3440         for (i = 0; i < temp_n; i++)
3441         {
3442                 fx = temp_x[i];
3443                 fy = temp_y[i];
3444
3445                 if (!in_bounds2(fy, fx)) continue;
3446
3447                 /* Point to grid */
3448                 c_ptr = &cave[fy][fx];
3449
3450                 if (i >= end_temp)
3451                 {
3452                         /* Clear the temp flag for the old lit grids */
3453                         c_ptr->info &= ~(CAVE_TEMP);
3454                 }
3455                 else
3456                 {
3457                         /* The is the square newly lit and visible? */
3458                         if ((c_ptr->info & (CAVE_VIEW | CAVE_TEMP)) == CAVE_VIEW)
3459                         {
3460                                 /* It is now lit */
3461                                 lite_spot(fy, fx);
3462                                 note_spot(fy, fx);
3463                         }
3464
3465                         /* Save in the monster lit array */
3466                         mon_lite_x[mon_lite_n] = fx;
3467                         mon_lite_y[mon_lite_n] = fy;
3468                         mon_lite_n++;
3469                 }
3470         }
3471
3472         /* Finished with temp_n */
3473         temp_n = 0;
3474
3475         p_ptr->monlite = (cave[py][px].info & CAVE_MNLT) ? TRUE : FALSE;
3476
3477         if (p_ptr->special_defense & NINJA_S_STEALTH)
3478         {
3479                 if (p_ptr->old_monlite != p_ptr->monlite)
3480                 {
3481                         if (p_ptr->monlite)
3482                         {
3483 #ifdef JP
3484                                 msg_print("±Æ¤Îʤ¤¤¤¬Çö¤ì¤¿µ¤¤¬¤¹¤ë¡£");
3485 #else
3486                                 msg_print("Your mantle of shadow become thin.");
3487 #endif
3488                         }
3489                         else
3490                         {
3491 #ifdef JP
3492                                 msg_print("±Æ¤Îʤ¤¤¤¬Ç»¤¯¤Ê¤Ã¤¿¡ª");
3493 #else
3494                                 msg_print("Your mantle of shadow restored its original darkness.");
3495 #endif
3496                         }
3497                 }
3498         }
3499         p_ptr->old_monlite = p_ptr->monlite;
3500 }
3501
3502 void clear_mon_lite(void)
3503 {
3504         int i;
3505         cave_type *c_ptr;
3506
3507         /* Clear all monster lit squares */
3508         for (i = 0; i < mon_lite_n; i++)
3509         {
3510                 /* Point to grid */
3511                 c_ptr = &cave[mon_lite_y[i]][mon_lite_x[i]];
3512
3513                 /* Clear monster illumination flag */
3514                 c_ptr->info &= ~(CAVE_MNLT);
3515         }
3516
3517         /* Empty the array */
3518         mon_lite_n = 0;
3519 }
3520
3521
3522
3523 /*
3524  * Clear the viewable space
3525  */
3526 void forget_view(void)
3527 {
3528         int i;
3529
3530         cave_type *c_ptr;
3531
3532         /* None to forget */
3533         if (!view_n) return;
3534
3535         /* Clear them all */
3536         for (i = 0; i < view_n; i++)
3537         {
3538                 int y = view_y[i];
3539                 int x = view_x[i];
3540
3541                 /* Access the grid */
3542                 c_ptr = &cave[y][x];
3543
3544                 /* Forget that the grid is viewable */
3545                 c_ptr->info &= ~(CAVE_VIEW);
3546
3547                 /* Update the screen */
3548                 lite_spot(y, x);
3549         }
3550
3551         /* None left */
3552         view_n = 0;
3553 }
3554
3555
3556
3557 /*
3558  * This macro allows us to efficiently add a grid to the "view" array,
3559  * note that we are never called for illegal grids, or for grids which
3560  * have already been placed into the "view" array, and we are never
3561  * called when the "view" array is full.
3562  */
3563 #define cave_view_hack(C,Y,X) \
3564 {\
3565     if (!((C)->info & (CAVE_VIEW))){\
3566     (C)->info |= (CAVE_VIEW); \
3567     view_y[view_n] = (Y); \
3568     view_x[view_n] = (X); \
3569     view_n++;}\
3570 }
3571
3572
3573
3574 /*
3575  * Helper function for "update_view()" below
3576  *
3577  * We are checking the "viewability" of grid (y,x) by the player.
3578  *
3579  * This function assumes that (y,x) is legal (i.e. on the map).
3580  *
3581  * Grid (y1,x1) is on the "diagonal" between (py,px) and (y,x)
3582  * Grid (y2,x2) is "adjacent", also between (py,px) and (y,x).
3583  *
3584  * Note that we are using the "CAVE_XTRA" field for marking grids as
3585  * "easily viewable".  This bit is cleared at the end of "update_view()".
3586  *
3587  * This function adds (y,x) to the "viewable set" if necessary.
3588  *
3589  * This function now returns "TRUE" if vision is "blocked" by grid (y,x).
3590  */
3591 static bool update_view_aux(int y, int x, int y1, int x1, int y2, int x2)
3592 {
3593         bool f1, f2, v1, v2, z1, z2, wall;
3594
3595         cave_type *c_ptr;
3596
3597         cave_type *g1_c_ptr;
3598         cave_type *g2_c_ptr;
3599
3600         /* Access the grids */
3601         g1_c_ptr = &cave[y1][x1];
3602         g2_c_ptr = &cave[y2][x2];
3603
3604
3605         /* Check for walls */
3606         f1 = (cave_floor_grid(g1_c_ptr));
3607         f2 = (cave_floor_grid(g2_c_ptr));
3608
3609         /* Totally blocked by physical walls */
3610         if (!f1 && !f2) return (TRUE);
3611
3612
3613         /* Check for visibility */
3614         v1 = (f1 && (g1_c_ptr->info & (CAVE_VIEW)));
3615         v2 = (f2 && (g2_c_ptr->info & (CAVE_VIEW)));
3616
3617         /* Totally blocked by "unviewable neighbors" */
3618         if (!v1 && !v2) return (TRUE);
3619
3620
3621         /* Access the grid */
3622         c_ptr = &cave[y][x];
3623
3624
3625         /* Check for walls */
3626         wall = (!cave_floor_grid(c_ptr));
3627
3628
3629         /* Check the "ease" of visibility */
3630         z1 = (v1 && (g1_c_ptr->info & (CAVE_XTRA)));
3631         z2 = (v2 && (g2_c_ptr->info & (CAVE_XTRA)));
3632
3633         /* Hack -- "easy" plus "easy" yields "easy" */
3634         if (z1 && z2)
3635         {
3636                 c_ptr->info |= (CAVE_XTRA);
3637
3638                 cave_view_hack(c_ptr, y, x);
3639
3640                 return (wall);
3641         }
3642
3643         /* Hack -- primary "easy" yields "viewed" */
3644         if (z1)
3645         {
3646                 cave_view_hack(c_ptr, y, x);
3647
3648                 return (wall);
3649         }
3650
3651         /* Hack -- "view" plus "view" yields "view" */
3652         if (v1 && v2)
3653         {
3654                 /* c_ptr->info |= (CAVE_XTRA); */
3655
3656                 cave_view_hack(c_ptr, y, x);
3657
3658                 return (wall);
3659         }
3660
3661
3662         /* Mega-Hack -- the "los()" function works poorly on walls */
3663         if (wall)
3664         {
3665                 cave_view_hack(c_ptr, y, x);
3666
3667                 return (wall);
3668         }
3669
3670
3671         /* Hack -- check line of sight */
3672         if (los(py, px, y, x))
3673         {
3674                 cave_view_hack(c_ptr, y, x);
3675
3676                 return (wall);
3677         }
3678
3679
3680         /* Assume no line of sight. */
3681         return (TRUE);
3682 }
3683
3684
3685
3686 /*
3687  * Calculate the viewable space
3688  *
3689  *  1: Process the player
3690  *  1a: The player is always (easily) viewable
3691  *  2: Process the diagonals
3692  *  2a: The diagonals are (easily) viewable up to the first wall
3693  *  2b: But never go more than 2/3 of the "full" distance
3694  *  3: Process the main axes
3695  *  3a: The main axes are (easily) viewable up to the first wall
3696  *  3b: But never go more than the "full" distance
3697  *  4: Process sequential "strips" in each of the eight octants
3698  *  4a: Each strip runs along the previous strip
3699  *  4b: The main axes are "previous" to the first strip
3700  *  4c: Process both "sides" of each "direction" of each strip
3701  *  4c1: Each side aborts as soon as possible
3702  *  4c2: Each side tells the next strip how far it has to check
3703  *
3704  * Note that the octant processing involves some pretty interesting
3705  * observations involving when a grid might possibly be viewable from
3706  * a given grid, and on the order in which the strips are processed.
3707  *
3708  * Note the use of the mathematical facts shown below, which derive
3709  * from the fact that (1 < sqrt(2) < 1.5), and that the length of the
3710  * hypotenuse of a right triangle is primarily determined by the length
3711  * of the longest side, when one side is small, and is strictly less
3712  * than one-and-a-half times as long as the longest side when both of
3713  * the sides are large.
3714  *
3715  *   if (manhatten(dy,dx) < R) then (hypot(dy,dx) < R)
3716  *   if (manhatten(dy,dx) > R*3/2) then (hypot(dy,dx) > R)
3717  *
3718  *   hypot(dy,dx) is approximated by (dx+dy+MAX(dx,dy)) / 2
3719  *
3720  * These observations are important because the calculation of the actual
3721  * value of "hypot(dx,dy)" is extremely expensive, involving square roots,
3722  * while for small values (up to about 20 or so), the approximations above
3723  * are correct to within an error of at most one grid or so.
3724  *
3725  * Observe the use of "full" and "over" in the code below, and the use of
3726  * the specialized calculation involving "limit", all of which derive from
3727  * the observations given above.  Basically, we note that the "circle" of
3728  * view is completely contained in an "octagon" whose bounds are easy to
3729  * determine, and that only a few steps are needed to derive the actual
3730  * bounds of the circle given the bounds of the octagon.
3731  *
3732  * Note that by skipping all the grids in the corners of the octagon, we
3733  * place an upper limit on the number of grids in the field of view, given
3734  * that "full" is never more than 20.  Of the 1681 grids in the "square" of
3735  * view, only about 1475 of these are in the "octagon" of view, and even
3736  * fewer are in the "circle" of view, so 1500 or 1536 is more than enough
3737  * entries to completely contain the actual field of view.
3738  *
3739  * Note also the care taken to prevent "running off the map".  The use of
3740  * explicit checks on the "validity" of the "diagonal", and the fact that
3741  * the loops are never allowed to "leave" the map, lets "update_view_aux()"
3742  * use the optimized "cave_floor_bold()" macro, and to avoid the overhead
3743  * of multiple checks on the validity of grids.
3744  *
3745  * Note the "optimizations" involving the "se","sw","ne","nw","es","en",
3746  * "ws","wn" variables.  They work like this: While travelling down the
3747  * south-bound strip just to the east of the main south axis, as soon as
3748  * we get to a grid which does not "transmit" viewing, if all of the strips
3749  * preceding us (in this case, just the main axis) had terminated at or before
3750  * the same point, then we can stop, and reset the "max distance" to ourself.
3751  * So, each strip (named by major axis plus offset, thus "se" in this case)
3752  * maintains a "blockage" variable, initialized during the main axis step,
3753  * and checks it whenever a blockage is observed.  After processing each
3754  * strip as far as the previous strip told us to process, the next strip is
3755  * told not to go farther than the current strip's farthest viewable grid,
3756  * unless open space is still available.  This uses the "k" variable.
3757  *
3758  * Note the use of "inline" macros for efficiency.  The "cave_floor_grid()"
3759  * macro is a replacement for "cave_floor_bold()" which takes a pointer to
3760  * a cave grid instead of its location.  The "cave_view_hack()" macro is a
3761  * chunk of code which adds the given location to the "view" array if it
3762  * is not already there, using both the actual location and a pointer to
3763  * the cave grid.  See above.
3764  *
3765  * By the way, the purpose of this code is to reduce the dependancy on the
3766  * "los()" function which is slow, and, in some cases, not very accurate.
3767  *
3768  * It is very possible that I am the only person who fully understands this
3769  * function, and for that I am truly sorry, but efficiency was very important
3770  * and the "simple" version of this function was just not fast enough.  I am
3771  * more than willing to replace this function with a simpler one, if it is
3772  * equally efficient, and especially willing if the new function happens to
3773  * derive "reverse-line-of-sight" at the same time, since currently monsters
3774  * just use an optimized hack of "you see me, so I see you", and then use the
3775  * actual "projectable()" function to check spell attacks.
3776  */
3777 void update_view(void)
3778 {
3779         int n, m, d, k, y, x, z;
3780
3781         int se, sw, ne, nw, es, en, ws, wn;
3782
3783         int full, over;
3784
3785         int y_max = cur_hgt - 1;
3786         int x_max = cur_wid - 1;
3787
3788         cave_type *c_ptr;
3789
3790         /*** Initialize ***/
3791
3792         /* Optimize */
3793         if (view_reduce_view && !dun_level)
3794         {
3795                 /* Full radius (10) */
3796                 full = MAX_SIGHT / 2;
3797
3798                 /* Octagon factor (15) */
3799                 over = MAX_SIGHT * 3 / 4;
3800         }
3801
3802         /* Normal */
3803         else
3804         {
3805                 /* Full radius (20) */
3806                 full = MAX_SIGHT;
3807
3808                 /* Octagon factor (30) */
3809                 over = MAX_SIGHT * 3 / 2;
3810         }
3811
3812
3813         /*** Step 0 -- Begin ***/
3814
3815         /* Save the old "view" grids for later */
3816         for (n = 0; n < view_n; n++)
3817         {
3818                 y = view_y[n];
3819                 x = view_x[n];
3820
3821                 /* Access the grid */
3822                 c_ptr = &cave[y][x];
3823
3824                 /* Mark the grid as not in "view" */
3825                 c_ptr->info &= ~(CAVE_VIEW);
3826
3827                 /* Mark the grid as "seen" */
3828                 c_ptr->info |= (CAVE_TEMP);
3829
3830                 /* Add it to the "seen" set */
3831                 temp_y[temp_n] = y;
3832                 temp_x[temp_n] = x;
3833                 temp_n++;
3834         }
3835
3836         /* Start over with the "view" array */
3837         view_n = 0;
3838
3839         /*** Step 1 -- adjacent grids ***/
3840
3841         /* Now start on the player */
3842         y = py;
3843         x = px;
3844
3845         /* Access the grid */
3846         c_ptr = &cave[y][x];
3847
3848         /* Assume the player grid is easily viewable */
3849         c_ptr->info |= (CAVE_XTRA);
3850
3851         /* Assume the player grid is viewable */
3852         cave_view_hack(c_ptr, y, x);
3853
3854
3855         /*** Step 2 -- Major Diagonals ***/
3856
3857         /* Hack -- Limit */
3858         z = full * 2 / 3;
3859
3860         /* Scan south-east */
3861         for (d = 1; d <= z; d++)
3862         {
3863                 c_ptr = &cave[y+d][x+d];
3864                 c_ptr->info |= (CAVE_XTRA);
3865                 cave_view_hack(c_ptr, y+d, x+d);
3866                 if (!cave_floor_grid(c_ptr)) break;
3867         }
3868
3869         /* Scan south-west */
3870         for (d = 1; d <= z; d++)
3871         {
3872                 c_ptr = &cave[y+d][x-d];
3873                 c_ptr->info |= (CAVE_XTRA);
3874                 cave_view_hack(c_ptr, y+d, x-d);
3875                 if (!cave_floor_grid(c_ptr)) break;
3876         }
3877
3878         /* Scan north-east */
3879         for (d = 1; d <= z; d++)
3880         {
3881                 c_ptr = &cave[y-d][x+d];
3882                 c_ptr->info |= (CAVE_XTRA);
3883                 cave_view_hack(c_ptr, y-d, x+d);
3884                 if (!cave_floor_grid(c_ptr)) break;
3885         }
3886
3887         /* Scan north-west */
3888         for (d = 1; d <= z; d++)
3889         {
3890                 c_ptr = &cave[y-d][x-d];
3891                 c_ptr->info |= (CAVE_XTRA);
3892                 cave_view_hack(c_ptr, y-d, x-d);
3893                 if (!cave_floor_grid(c_ptr)) break;
3894         }
3895
3896
3897         /*** Step 3 -- major axes ***/
3898
3899         /* Scan south */
3900         for (d = 1; d <= full; d++)
3901         {
3902                 c_ptr = &cave[y+d][x];
3903                 c_ptr->info |= (CAVE_XTRA);
3904                 cave_view_hack(c_ptr, y+d, x);
3905                 if (!cave_floor_grid(c_ptr)) break;
3906         }
3907
3908         /* Initialize the "south strips" */
3909         se = sw = d;
3910
3911         /* Scan north */
3912         for (d = 1; d <= full; d++)
3913         {
3914                 c_ptr = &cave[y-d][x];
3915                 c_ptr->info |= (CAVE_XTRA);
3916                 cave_view_hack(c_ptr, y-d, x);
3917                 if (!cave_floor_grid(c_ptr)) break;
3918         }
3919
3920         /* Initialize the "north strips" */
3921         ne = nw = d;
3922
3923         /* Scan east */
3924         for (d = 1; d <= full; d++)
3925         {
3926                 c_ptr = &cave[y][x+d];
3927                 c_ptr->info |= (CAVE_XTRA);
3928                 cave_view_hack(c_ptr, y, x+d);
3929                 if (!cave_floor_grid(c_ptr)) break;
3930         }
3931
3932         /* Initialize the "east strips" */
3933         es = en = d;
3934
3935         /* Scan west */
3936         for (d = 1; d <= full; d++)
3937         {
3938                 c_ptr = &cave[y][x-d];
3939                 c_ptr->info |= (CAVE_XTRA);
3940                 cave_view_hack(c_ptr, y, x-d);
3941                 if (!cave_floor_grid(c_ptr)) break;
3942         }
3943
3944         /* Initialize the "west strips" */
3945         ws = wn = d;
3946
3947
3948         /*** Step 4 -- Divide each "octant" into "strips" ***/
3949
3950         /* Now check each "diagonal" (in parallel) */
3951         for (n = 1; n <= over / 2; n++)
3952         {
3953                 int ypn, ymn, xpn, xmn;
3954
3955
3956                 /* Acquire the "bounds" of the maximal circle */
3957                 z = over - n - n;
3958                 if (z > full - n) z = full - n;
3959                 while ((z + n + (n>>1)) > full) z--;
3960
3961
3962                 /* Access the four diagonal grids */
3963                 ypn = y + n;
3964                 ymn = y - n;
3965                 xpn = x + n;
3966                 xmn = x - n;
3967
3968
3969                 /* South strip */
3970                 if (ypn < y_max)
3971                 {
3972                         /* Maximum distance */
3973                         m = MIN(z, y_max - ypn);
3974
3975                         /* East side */
3976                         if ((xpn <= x_max) && (n < se))
3977                         {
3978                                 /* Scan */
3979                                 for (k = n, d = 1; d <= m; d++)
3980                                 {
3981                                         /* Check grid "d" in strip "n", notice "blockage" */
3982                                         if (update_view_aux(ypn+d, xpn, ypn+d-1, xpn-1, ypn+d-1, xpn))
3983                                         {
3984                                                 if (n + d >= se) break;
3985                                         }
3986
3987                                         /* Track most distant "non-blockage" */
3988                                         else
3989                                         {
3990                                                 k = n + d;
3991                                         }
3992                                 }
3993
3994                                 /* Limit the next strip */
3995                                 se = k + 1;
3996                         }
3997
3998                         /* West side */
3999                         if ((xmn >= 0) && (n < sw))
4000                         {
4001                                 /* Scan */
4002                                 for (k = n, d = 1; d <= m; d++)
4003                                 {
4004                                         /* Check grid "d" in strip "n", notice "blockage" */
4005                                         if (update_view_aux(ypn+d, xmn, ypn+d-1, xmn+1, ypn+d-1, xmn))
4006                                         {
4007                                                 if (n + d >= sw) break;
4008                                         }
4009
4010                                         /* Track most distant "non-blockage" */
4011                                         else
4012                                         {
4013                                                 k = n + d;
4014                                         }
4015                                 }
4016
4017                                 /* Limit the next strip */
4018                                 sw = k + 1;
4019                         }
4020                 }
4021
4022
4023                 /* North strip */
4024                 if (ymn > 0)
4025                 {
4026                         /* Maximum distance */
4027                         m = MIN(z, ymn);
4028
4029                         /* East side */
4030                         if ((xpn <= x_max) && (n < ne))
4031                         {
4032                                 /* Scan */
4033                                 for (k = n, d = 1; d <= m; d++)
4034                                 {
4035                                         /* Check grid "d" in strip "n", notice "blockage" */
4036                                         if (update_view_aux(ymn-d, xpn, ymn-d+1, xpn-1, ymn-d+1, xpn))
4037                                         {
4038                                                 if (n + d >= ne) break;
4039                                         }
4040
4041                                         /* Track most distant "non-blockage" */
4042                                         else
4043                                         {
4044                                                 k = n + d;
4045                                         }
4046                                 }
4047
4048                                 /* Limit the next strip */
4049                                 ne = k + 1;
4050                         }
4051
4052                         /* West side */
4053                         if ((xmn >= 0) && (n < nw))
4054                         {
4055                                 /* Scan */
4056                                 for (k = n, d = 1; d <= m; d++)
4057                                 {
4058                                         /* Check grid "d" in strip "n", notice "blockage" */
4059                                         if (update_view_aux(ymn-d, xmn, ymn-d+1, xmn+1, ymn-d+1, xmn))
4060                                         {
4061                                                 if (n + d >= nw) break;
4062                                         }
4063
4064                                         /* Track most distant "non-blockage" */
4065                                         else
4066                                         {
4067                                                 k = n + d;
4068                                         }
4069                                 }
4070
4071                                 /* Limit the next strip */
4072                                 nw = k + 1;
4073                         }
4074                 }
4075
4076
4077                 /* East strip */
4078                 if (xpn < x_max)
4079                 {
4080                         /* Maximum distance */
4081                         m = MIN(z, x_max - xpn);
4082
4083                         /* South side */
4084                         if ((ypn <= x_max) && (n < es))
4085                         {
4086                                 /* Scan */
4087                                 for (k = n, d = 1; d <= m; d++)
4088                                 {
4089                                         /* Check grid "d" in strip "n", notice "blockage" */
4090                                         if (update_view_aux(ypn, xpn+d, ypn-1, xpn+d-1, ypn, xpn+d-1))
4091                                         {
4092                                                 if (n + d >= es) break;
4093                                         }
4094
4095                                         /* Track most distant "non-blockage" */
4096                                         else
4097                                         {
4098                                                 k = n + d;
4099                                         }
4100                                 }
4101
4102                                 /* Limit the next strip */
4103                                 es = k + 1;
4104                         }
4105
4106                         /* North side */
4107                         if ((ymn >= 0) && (n < en))
4108                         {
4109                                 /* Scan */
4110                                 for (k = n, d = 1; d <= m; d++)
4111                                 {
4112                                         /* Check grid "d" in strip "n", notice "blockage" */
4113                                         if (update_view_aux(ymn, xpn+d, ymn+1, xpn+d-1, ymn, xpn+d-1))
4114                                         {
4115                                                 if (n + d >= en) break;
4116                                         }
4117
4118                                         /* Track most distant "non-blockage" */
4119                                         else
4120                                         {
4121                                                 k = n + d;
4122                                         }
4123                                 }
4124
4125                                 /* Limit the next strip */
4126                                 en = k + 1;
4127                         }
4128                 }
4129
4130
4131                 /* West strip */
4132                 if (xmn > 0)
4133                 {
4134                         /* Maximum distance */
4135                         m = MIN(z, xmn);
4136
4137                         /* South side */
4138                         if ((ypn <= y_max) && (n < ws))
4139                         {
4140                                 /* Scan */
4141                                 for (k = n, d = 1; d <= m; d++)
4142                                 {
4143                                         /* Check grid "d" in strip "n", notice "blockage" */
4144                                         if (update_view_aux(ypn, xmn-d, ypn-1, xmn-d+1, ypn, xmn-d+1))
4145                                         {
4146                                                 if (n + d >= ws) break;
4147                                         }
4148
4149                                         /* Track most distant "non-blockage" */
4150                                         else
4151                                         {
4152                                                 k = n + d;
4153                                         }
4154                                 }
4155
4156                                 /* Limit the next strip */
4157                                 ws = k + 1;
4158                         }
4159
4160                         /* North side */
4161                         if ((ymn >= 0) && (n < wn))
4162                         {
4163                                 /* Scan */
4164                                 for (k = n, d = 1; d <= m; d++)
4165                                 {
4166                                         /* Check grid "d" in strip "n", notice "blockage" */
4167                                         if (update_view_aux(ymn, xmn-d, ymn+1, xmn-d+1, ymn, xmn-d+1))
4168                                         {
4169                                                 if (n + d >= wn) break;
4170                                         }
4171
4172                                         /* Track most distant "non-blockage" */
4173                                         else
4174                                         {
4175                                                 k = n + d;
4176                                         }
4177                                 }
4178
4179                                 /* Limit the next strip */
4180                                 wn = k + 1;
4181                         }
4182                 }
4183         }
4184
4185
4186         /*** Step 5 -- Complete the algorithm ***/
4187
4188         /* Update all the new grids */
4189         for (n = 0; n < view_n; n++)
4190         {
4191                 y = view_y[n];
4192                 x = view_x[n];
4193
4194                 /* Access the grid */
4195                 c_ptr = &cave[y][x];
4196
4197                 /* Clear the "CAVE_XTRA" flag */
4198                 c_ptr->info &= ~(CAVE_XTRA);
4199
4200                 /* Update only newly viewed grids */
4201                 if (c_ptr->info & (CAVE_TEMP)) continue;
4202
4203                 /* Note */
4204                 note_spot(y, x);
4205
4206                 /* Redraw */
4207                 lite_spot(y, x);
4208         }
4209
4210         /* Wipe the old grids, update as needed */
4211         for (n = 0; n < temp_n; n++)
4212         {
4213                 y = temp_y[n];
4214                 x = temp_x[n];
4215
4216                 /* Access the grid */
4217                 c_ptr = &cave[y][x];
4218
4219                 /* No longer in the array */
4220                 c_ptr->info &= ~(CAVE_TEMP);
4221
4222                 /* Update only non-viewable grids */
4223                 if (c_ptr->info & (CAVE_VIEW)) continue;
4224
4225                 /* Redraw */
4226                 lite_spot(y, x);
4227         }
4228
4229         /* None left */
4230         temp_n = 0;
4231 }
4232
4233
4234
4235
4236
4237
4238 /*
4239  * Hack -- provide some "speed" for the "flow" code
4240  * This entry is the "current index" for the "when" field
4241  * Note that a "when" value of "zero" means "not used".
4242  *
4243  * Note that the "cost" indexes from 1 to 127 are for
4244  * "old" data, and from 128 to 255 are for "new" data.
4245  *
4246  * This means that as long as the player does not "teleport",
4247  * then any monster up to 128 + MONSTER_FLOW_DEPTH will be
4248  * able to track down the player, and in general, will be
4249  * able to track down either the player or a position recently
4250  * occupied by the player.
4251  */
4252 static int flow_n = 0;
4253
4254
4255 /*
4256  * Hack -- forget the "flow" information
4257  */
4258 void forget_flow(void)
4259 {
4260         int x, y;
4261
4262         /* Nothing to forget */
4263         if (!flow_n) return;
4264
4265         /* Check the entire dungeon */
4266         for (y = 0; y < cur_hgt; y++)
4267         {
4268                 for (x = 0; x < cur_wid; x++)
4269                 {
4270                         /* Forget the old data */
4271                         cave[y][x].dist = 0;
4272                         cave[y][x].cost = 0;
4273                         cave[y][x].when = 0;
4274                 }
4275         }
4276
4277         /* Start over */
4278         flow_n = 0;
4279 }
4280
4281
4282 /*
4283  * Hack - speed up the update_flow algorithm by only doing
4284  * it everytime the player moves out of LOS of the last
4285  * "way-point".
4286  */
4287 static u16b flow_x = 0;
4288 static u16b flow_y = 0;
4289
4290
4291
4292 /*
4293  * Hack -- fill in the "cost" field of every grid that the player
4294  * can "reach" with the number of steps needed to reach that grid.
4295  * This also yields the "distance" of the player from every grid.
4296  *
4297  * In addition, mark the "when" of the grids that can reach
4298  * the player with the incremented value of "flow_n".
4299  *
4300  * Hack -- use the "seen" array as a "circular queue".
4301  *
4302  * We do not need a priority queue because the cost from grid
4303  * to grid is always "one" and we process them in order.
4304  */
4305 void update_flow(void)
4306 {
4307         int x, y, d;
4308         int flow_head = 1;
4309         int flow_tail = 0;
4310
4311         /* Hack -- disabled */
4312         if (stupid_monsters) return;
4313
4314         /* Paranoia -- make sure the array is empty */
4315         if (temp_n) return;
4316
4317         /* The last way-point is on the map */
4318         if (running && in_bounds(flow_y, flow_x))
4319         {
4320                 /* The way point is in sight - do not update.  (Speedup) */
4321                 if (cave[flow_y][flow_x].info & CAVE_VIEW) return;
4322         }
4323
4324         /* Erase all of the current flow information */
4325         for (y = 0; y < cur_hgt; y++)
4326         {
4327                 for (x = 0; x < cur_wid; x++)
4328                 {
4329                         cave[y][x].cost = 0;
4330                         cave[y][x].dist = 0;
4331                 }
4332         }
4333
4334         /* Save player position */
4335         flow_y = py;
4336         flow_x = px;
4337
4338         /* Add the player's grid to the queue */
4339         temp_y[0] = py;
4340         temp_x[0] = px;
4341
4342         /* Now process the queue */
4343         while (flow_head != flow_tail)
4344         {
4345                 int ty, tx;
4346
4347                 /* Extract the next entry */
4348                 ty = temp_y[flow_tail];
4349                 tx = temp_x[flow_tail];
4350
4351                 /* Forget that entry */
4352                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
4353
4354                 /* Add the "children" */
4355                 for (d = 0; d < 8; d++)
4356                 {
4357                         int old_head = flow_head;
4358                         int m = cave[ty][tx].cost + 1;
4359                         int n = cave[ty][tx].dist + 1;
4360                         cave_type *c_ptr;
4361
4362                         /* Child location */
4363                         y = ty + ddy_ddd[d];
4364                         x = tx + ddx_ddd[d];
4365
4366                         /* Ignore player's grid */
4367                         if (x == px && y == py) continue;
4368
4369                         c_ptr = &cave[y][x];
4370                                        
4371                         if ((c_ptr->feat >= FEAT_DOOR_HEAD) && (c_ptr->feat <= FEAT_SECRET)) m += 3;
4372
4373                         /* Ignore "pre-stamped" entries */
4374                         if (c_ptr->dist != 0 && c_ptr->dist <= n && c_ptr->cost <= m) continue;
4375
4376                         /* Ignore "walls" and "rubble" */
4377                         if ((c_ptr->feat > FEAT_SECRET) && (c_ptr->feat != FEAT_TREES) && !cave_floor_grid(c_ptr)) continue;
4378
4379                         /* Save the flow cost */
4380                         if (c_ptr->cost == 0 || c_ptr->cost > m) c_ptr->cost = m;
4381                         if (c_ptr->dist == 0 || c_ptr->dist > n) c_ptr->dist = n;
4382
4383                         /* Hack -- limit flow depth */
4384                         if (n == MONSTER_FLOW_DEPTH) continue;
4385
4386                         /* Enqueue that entry */
4387                         temp_y[flow_head] = y;
4388                         temp_x[flow_head] = x;
4389
4390                         /* Advance the queue */
4391                         if (++flow_head == TEMP_MAX) flow_head = 0;
4392
4393                         /* Hack -- notice overflow by forgetting new entry */
4394                         if (flow_head == flow_tail) flow_head = old_head;
4395                 }
4396         }
4397 }
4398
4399
4400 static int scent_when = 0;
4401
4402 /*
4403  * Characters leave scent trails for perceptive monsters to track.
4404  *
4405  * Smell is rather more limited than sound.  Many creatures cannot use 
4406  * it at all, it doesn't extend very far outwards from the character's 
4407  * current position, and monsters can use it to home in the character, 
4408  * but not to run away from him.
4409  *
4410  * Smell is valued according to age.  When a character takes his turn, 
4411  * scent is aged by one, and new scent of the current age is laid down.  
4412  * Speedy characters leave more scent, true, but it also ages faster, 
4413  * which makes it harder to hunt them down.
4414  *
4415  * Whenever the age count loops, most of the scent trail is erased and 
4416  * the age of the remainder is recalculated.
4417  */
4418 void update_smell(void)
4419 {
4420         int i, j;
4421         int y, x;
4422
4423         /* Create a table that controls the spread of scent */
4424         const int scent_adjust[5][5] = 
4425         {
4426                 { -1, 0, 0, 0,-1 },
4427                 {  0, 1, 1, 1, 0 },
4428                 {  0, 1, 2, 1, 0 },
4429                 {  0, 1, 1, 1, 0 },
4430                 { -1, 0, 0, 0,-1 },
4431         };
4432
4433         /* Loop the age and adjust scent values when necessary */
4434         if (++scent_when == 254)
4435         {
4436                 /* Scan the entire dungeon */
4437                 for (y = 0; y < cur_hgt; y++)
4438                 {
4439                         for (x = 0; x < cur_wid; x++)
4440                         {
4441                                 int w = cave[y][x].when;
4442                                 cave[y][x].when = (w > 128) ? (w - 128) : 0;
4443                         }
4444                 }
4445
4446                 /* Restart */
4447                 scent_when = 126;
4448         }
4449
4450
4451         /* Lay down new scent */
4452         for (i = 0; i < 5; i++)
4453         {
4454                 for (j = 0; j < 5; j++)
4455                 {
4456                         cave_type *c_ptr;
4457
4458                         /* Translate table to map grids */
4459                         y = i + py - 2;
4460                         x = j + px - 2;
4461
4462                         /* Check Bounds */
4463                         if (!in_bounds(y, x)) continue;
4464
4465                         c_ptr = &cave[y][x];
4466
4467                         /* Walls, water, and lava cannot hold scent. */
4468                         if ((c_ptr->feat > FEAT_SECRET) && (c_ptr->feat != FEAT_TREES) && !cave_floor_grid(c_ptr)) continue;
4469
4470                         /* Grid must not be blocked by walls from the character */
4471                         if (!player_has_los_bold(y, x)) continue;
4472
4473                         /* Note grids that are too far away */
4474                         if (scent_adjust[i][j] == -1) continue;
4475
4476                         /* Mark the grid with new scent */
4477                         c_ptr->when = scent_when + scent_adjust[i][j];
4478                 }
4479         }
4480 }
4481
4482
4483 /*
4484  * Hack -- map the current panel (plus some) ala "magic mapping"
4485  */
4486 void map_area(int range)
4487 {
4488         int             i, x, y;
4489
4490         cave_type       *c_ptr;
4491
4492         if (d_info[dungeon_type].flags1 & DF1_DARKNESS) range /= 3;
4493
4494         /* Scan that area */
4495         for (y = 1; y < cur_hgt - 1; y++)
4496         {
4497                 for (x = 1; x < cur_wid - 1; x++)
4498                 {
4499                         if (distance(py, px, y, x) > range) continue;
4500
4501                         c_ptr = &cave[y][x];
4502
4503                         /* All non-walls are "checked" */
4504                         if ((c_ptr->feat < FEAT_SECRET) ||
4505                             (c_ptr->feat == FEAT_RUBBLE) ||
4506                            ((c_ptr->feat >= FEAT_MINOR_GLYPH) &&
4507                             (c_ptr->feat <= FEAT_TREES)) ||
4508                             (c_ptr->feat >= FEAT_TOWN))
4509                         {
4510                                 /* Memorize normal features */
4511                                 if ((c_ptr->feat > FEAT_INVIS) && (c_ptr->feat != FEAT_DIRT) && (c_ptr->feat != FEAT_GRASS))
4512                                 {
4513                                         /* Memorize the object */
4514                                         c_ptr->info |= (CAVE_MARK);
4515                                 }
4516
4517                                 /* Memorize known walls */
4518                                 for (i = 0; i < 8; i++)
4519                                 {
4520                                         c_ptr = &cave[y + ddy_ddd[i]][x + ddx_ddd[i]];
4521
4522                                         /* Memorize walls (etc) */
4523                                         if ((c_ptr->feat >= FEAT_SECRET) && (c_ptr->feat != FEAT_DIRT) && (c_ptr->feat != FEAT_GRASS))
4524                                         {
4525                                                 /* Memorize the walls */
4526                                                 c_ptr->info |= (CAVE_MARK);
4527                                         }
4528                                 }
4529                         }
4530                 }
4531         }
4532
4533         /* Redraw map */
4534         p_ptr->redraw |= (PR_MAP);
4535
4536         /* Window stuff */
4537         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4538 }
4539
4540
4541
4542 /*
4543  * Light up the dungeon using "clairvoyance"
4544  *
4545  * This function "illuminates" every grid in the dungeon, memorizes all
4546  * "objects", memorizes all grids as with magic mapping, and, under the
4547  * standard option settings (view_perma_grids but not view_torch_grids)
4548  * memorizes all floor grids too.
4549  *
4550  * Note that if "view_perma_grids" is not set, we do not memorize floor
4551  * grids, since this would defeat the purpose of "view_perma_grids", not
4552  * that anyone seems to play without this option.
4553  *
4554  * Note that if "view_torch_grids" is set, we do not memorize floor grids,
4555  * since this would prevent the use of "view_torch_grids" as a method to
4556  * keep track of what grids have been observed directly.
4557  */
4558 void wiz_lite(bool wizard, bool ninja)
4559 {
4560         int i, y, x;
4561
4562         /* Memorize objects */
4563         for (i = 1; i < o_max; i++)
4564         {
4565                 object_type *o_ptr = &o_list[i];
4566
4567                 /* Skip dead objects */
4568                 if (!o_ptr->k_idx) continue;
4569
4570                 /* Skip held objects */
4571                 if (o_ptr->held_m_idx) continue;
4572
4573 #if 0
4574                 /* Skip objects in vaults, if not a wizard. -LM- */
4575                 if ((wizard == FALSE) && 
4576                         (cave[o_ptr->iy][o_ptr->ix].info & (CAVE_ICKY))) continue;
4577 #endif
4578
4579                 /* Memorize */
4580                 o_ptr->marked = TRUE;
4581         }
4582
4583         /* Scan all normal grids */
4584         for (y = 1; y < cur_hgt - 1; y++)
4585         {
4586                 /* Scan all normal grids */
4587                 for (x = 1; x < cur_wid - 1; x++)
4588                 {
4589                         cave_type *c_ptr = &cave[y][x];
4590
4591                         /* Process all non-walls */
4592                         if (cave_floor_bold(y, x) || (c_ptr->feat == FEAT_RUBBLE) || (c_ptr->feat == FEAT_TREES) || (c_ptr->feat == FEAT_MOUNTAIN))
4593                         {
4594                                 /* Scan all neighbors */
4595                                 for (i = 0; i < 9; i++)
4596                                 {
4597                                         int yy = y + ddy_ddd[i];
4598                                         int xx = x + ddx_ddd[i];
4599
4600                                         /* Get the grid */
4601                                         c_ptr = &cave[yy][xx];
4602
4603                                         /* Memorize normal features */
4604                                         if (ninja)
4605                                         {
4606                                                 /* Memorize the grid */
4607                                                 c_ptr->info |= (CAVE_MARK);
4608                                         }
4609                                         else
4610                                         {
4611                                                 if ((c_ptr->feat > FEAT_INVIS))
4612                                                 {
4613                                                         /* Memorize the grid */
4614                                                         c_ptr->info |= (CAVE_MARK);
4615                                                 }
4616
4617                                                 /* Perma-lite the grid */
4618                                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS))
4619                                                 {
4620                                                         c_ptr->info |= (CAVE_GLOW);
4621
4622                                                         /* Normally, memorize floors (see above) */
4623                                                         if (view_perma_grids && !view_torch_grids)
4624                                                         {
4625                                                                 /* Memorize the grid */
4626                                                                 c_ptr->info |= (CAVE_MARK);
4627                                                         }
4628                                                 }
4629                                         }
4630                                 }
4631                         }
4632                 }
4633         }
4634
4635         /* Update the monsters */
4636         p_ptr->update |= (PU_MONSTERS);
4637
4638         /* Redraw map */
4639         p_ptr->redraw |= (PR_MAP);
4640
4641         /* Window stuff */
4642         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4643 }
4644
4645
4646 /*
4647  * Forget the dungeon map (ala "Thinking of Maud...").
4648  */
4649 void wiz_dark(void)
4650 {
4651         int i, y, x;
4652
4653
4654         /* Forget every grid */
4655         for (y = 0; y < cur_hgt; y++)
4656         {
4657                 for (x = 0; x < cur_wid; x++)
4658                 {
4659                         cave_type *c_ptr = &cave[y][x];
4660
4661                         /* Process the grid */
4662                         c_ptr->info &= ~(CAVE_MARK);
4663                 }
4664         }
4665
4666         /* Forget all objects */
4667         for (i = 1; i < o_max; i++)
4668         {
4669                 object_type *o_ptr = &o_list[i];
4670
4671                 /* Skip dead objects */
4672                 if (!o_ptr->k_idx) continue;
4673
4674                 /* Skip held objects */
4675                 if (o_ptr->held_m_idx) continue;
4676
4677                 /* Forget the object */
4678                 o_ptr->marked = FALSE;
4679         }
4680
4681         /* Mega-Hack -- Forget the view and lite */
4682         p_ptr->update |= (PU_UN_VIEW | PU_UN_LITE);
4683
4684         /* Update the view and lite */
4685         p_ptr->update |= (PU_VIEW | PU_LITE);
4686
4687         /* Update the monsters */
4688         p_ptr->update |= (PU_MONSTERS);
4689
4690         /* Redraw map */
4691         p_ptr->redraw |= (PR_MAP);
4692
4693         /* Window stuff */
4694         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4695 }
4696
4697
4698
4699
4700
4701 /*
4702  * Change the "feat" flag for a grid, and notice/redraw the grid
4703  */
4704 void cave_set_feat(int y, int x, int feat)
4705 {
4706         cave_type *c_ptr = &cave[y][x];
4707
4708         /* Change the feature */
4709         c_ptr->feat = feat;
4710
4711         /* Notice */
4712         note_spot(y, x);
4713
4714         /* Redraw */
4715         lite_spot(y, x);
4716 }
4717
4718
4719
4720 /*
4721  * Calculate "incremental motion". Used by project() and shoot().
4722  * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2).
4723  */
4724 void mmove2(int *y, int *x, int y1, int x1, int y2, int x2)
4725 {
4726         int dy, dx, dist, shift;
4727
4728         /* Extract the distance travelled */
4729         dy = (*y < y1) ? y1 - *y : *y - y1;
4730         dx = (*x < x1) ? x1 - *x : *x - x1;
4731
4732         /* Number of steps */
4733         dist = (dy > dx) ? dy : dx;
4734
4735         /* We are calculating the next location */
4736         dist++;
4737
4738
4739         /* Calculate the total distance along each axis */
4740         dy = (y2 < y1) ? (y1 - y2) : (y2 - y1);
4741         dx = (x2 < x1) ? (x1 - x2) : (x2 - x1);
4742
4743         /* Paranoia -- Hack -- no motion */
4744         if (!dy && !dx) return;
4745
4746
4747         /* Move mostly vertically */
4748         if (dy > dx)
4749         {
4750                 /* Extract a shift factor */
4751                 shift = (dist * dx + (dy - 1) / 2) / dy;
4752
4753                 /* Sometimes move along the minor axis */
4754                 (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift);
4755
4756                 /* Always move along major axis */
4757                 (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist);
4758         }
4759
4760         /* Move mostly horizontally */
4761         else
4762         {
4763                 /* Extract a shift factor */
4764                 shift = (dist * dy + (dx - 1) / 2) / dx;
4765
4766                 /* Sometimes move along the minor axis */
4767                 (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift);
4768
4769                 /* Always move along major axis */
4770                 (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist);
4771         }
4772 }
4773
4774
4775
4776 /*
4777  * Determine if a bolt spell cast from (y1,x1) to (y2,x2) will arrive
4778  * at the final destination, assuming no monster gets in the way.
4779  *
4780  * This is slightly (but significantly) different from "los(y1,x1,y2,x2)".
4781  */
4782 bool projectable(int y1, int x1, int y2, int x2)
4783 {
4784         int y, x;
4785
4786         int grid_n = 0;
4787         u16b grid_g[512];
4788
4789         /* Check the projection path */
4790         grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, 0);
4791
4792         /* No grid is ever projectable from itself */
4793         if (!grid_n) return (FALSE);
4794
4795         /* Final grid */
4796         y = GRID_Y(grid_g[grid_n - 1]);
4797         x = GRID_X(grid_g[grid_n - 1]);
4798
4799         /* May not end in an unrequested grid */
4800         if ((y != y2) || (x != x2)) return (FALSE);
4801
4802         /* Assume okay */
4803         return (TRUE);
4804 }
4805
4806
4807 /*
4808  * Standard "find me a location" function
4809  *
4810  * Obtains a legal location within the given distance of the initial
4811  * location, and with "los()" from the source to destination location.
4812  *
4813  * This function is often called from inside a loop which searches for
4814  * locations while increasing the "d" distance.
4815  *
4816  * Currently the "m" parameter is unused.
4817  */
4818 void scatter(int *yp, int *xp, int y, int x, int d, int m)
4819 {
4820         int nx, ny;
4821
4822         /* Unused */
4823         m = m;
4824
4825         /* Pick a location */
4826         while (TRUE)
4827         {
4828                 /* Pick a new location */
4829                 ny = rand_spread(y, d);
4830                 nx = rand_spread(x, d);
4831
4832                 /* Ignore annoying locations */
4833                 if (!in_bounds(ny, nx)) continue;
4834
4835                 /* Ignore "excessively distant" locations */
4836                 if ((d > 1) && (distance(y, x, ny, nx) > d)) continue;
4837
4838                 /* Require "line of sight" */
4839                 if (los(y, x, ny, nx)) break;
4840         }
4841
4842         /* Save the location */
4843         (*yp) = ny;
4844         (*xp) = nx;
4845 }
4846
4847
4848
4849
4850 /*
4851  * Track a new monster
4852  */
4853 void health_track(int m_idx)
4854 {
4855         /* Track a new guy */
4856         p_ptr->health_who = m_idx;
4857
4858         /* Redraw (later) */
4859         p_ptr->redraw |= (PR_HEALTH);
4860 }
4861
4862
4863
4864 /*
4865  * Hack -- track the given monster race
4866  */
4867 void monster_race_track(bool kage, int r_idx)
4868 {
4869         if (kage) r_idx = MON_KAGE;
4870
4871         /* Save this monster ID */
4872         p_ptr->monster_race_idx = r_idx;
4873
4874         /* Window stuff */
4875         p_ptr->window |= (PW_MONSTER);
4876 }
4877
4878
4879
4880 /*
4881  * Hack -- track the given object kind
4882  */
4883 void object_kind_track(int k_idx)
4884 {
4885         /* Save this monster ID */
4886         p_ptr->object_kind_idx = k_idx;
4887
4888         /* Window stuff */
4889         p_ptr->window |= (PW_OBJECT);
4890 }
4891
4892
4893
4894 /*
4895  * Something has happened to disturb the player.
4896  *
4897  * The first arg indicates a major disturbance, which affects search.
4898  *
4899  * The second arg is currently unused, but could induce output flush.
4900  *
4901  * All disturbance cancels repeated commands, resting, and running.
4902  */
4903 void disturb(int stop_search, int unused_flag)
4904 {
4905         /* Unused */
4906         unused_flag = unused_flag;
4907
4908         /* Cancel auto-commands */
4909         /* command_new = 0; */
4910
4911         /* Cancel repeated commands */
4912         if (command_rep)
4913         {
4914                 /* Cancel */
4915                 command_rep = 0;
4916
4917                 /* Redraw the state (later) */
4918                 p_ptr->redraw |= (PR_STATE);
4919         }
4920
4921         /* Cancel Resting */
4922         if ((p_ptr->action == ACTION_REST) || (p_ptr->action == ACTION_FISH) || (stop_search && (p_ptr->action == ACTION_SEARCH)))
4923         {
4924                 /* Cancel */
4925                 set_action(ACTION_NONE);
4926         }
4927
4928         /* Cancel running */
4929         if (running)
4930         {
4931                 /* Cancel */
4932                 running = 0;
4933
4934                 /* Check for new panel if appropriate */
4935                 if (center_player && !center_running) verify_panel();
4936
4937                 /* Calculate torch radius */
4938                 p_ptr->update |= (PU_TORCH);
4939
4940                 /* Update monster flow */
4941                 p_ptr->update |= (PU_FLOW);
4942         }
4943
4944         /* Flush the input if requested */
4945         if (flush_disturb) flush();
4946 }