OSDN Git Service

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