OSDN Git Service

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