OSDN Git Service

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