OSDN Git Service

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