OSDN Git Service

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