OSDN Git Service

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