OSDN Git Service

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