OSDN Git Service

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