OSDN Git Service

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