OSDN Git Service

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