OSDN Git Service

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