OSDN Git Service

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