OSDN Git Service

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