OSDN Git Service

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