OSDN Git Service

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