OSDN Git Service

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