OSDN Git Service

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