OSDN Git Service

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