OSDN Git Service

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