OSDN Git Service

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