OSDN Git Service

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