OSDN Git Service

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