OSDN Git Service

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