OSDN Git Service

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