OSDN Git Service

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