OSDN Git Service

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