OSDN Git Service

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