OSDN Git Service

[Refactor] #37353 is_open() を cave.c へ移動。 / Move is_open() to cave.c.
[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         byte c;
959
960         /* Boring grids (floors, etc) */
961         if (!have_flag(f_ptr->flags, FF_REMEMBER))
962         {
963                 /*
964                  * Handle Memorized or visible floor
965                  *
966                  * No visual when blinded.
967                  *   (to prevent strange effects on darkness breath)
968                  * otherwise,
969                  * - Can see grids with CAVE_MARK.
970                  * - Can see grids with CAVE_LITE or CAVE_MNLT.
971                  *   (Such grids also have CAVE_VIEW)
972                  * - Can see grids with CAVE_VIEW unless darkened by monsters.
973                  */
974                 if (!p_ptr->blind &&
975                     ((c_ptr->info & (CAVE_MARK | CAVE_LITE | CAVE_MNLT)) ||
976                      ((c_ptr->info & CAVE_VIEW) && (((c_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW) || p_ptr->see_nocto))))
977                 {
978                         /* Normal attr/char */
979                         a = f_ptr->x_attr[F_LIT_STANDARD];
980                         c = f_ptr->x_char[F_LIT_STANDARD];
981
982                         if (p_ptr->wild_mode)
983                         {
984                                 /* Special lighting effects */
985                                 /* Handle "night" */
986                                 if (view_special_lite && !is_daytime())
987                                 {
988                                         /* Use a darkened colour/tile */
989                                         a = f_ptr->x_attr[F_LIT_DARK];
990                                         c = f_ptr->x_char[F_LIT_DARK];
991                                 }
992                         }
993
994                         /* Mega-Hack -- Handle "in-sight" and "darkened" grids */
995                         else if (darkened_grid(c_ptr))
996                         {
997                                 /* Unsafe cave grid -- idea borrowed from Unangband */
998                                 feat = (view_unsafe_grids && (c_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
999
1000                                 /* Access darkness */
1001                                 f_ptr = &f_info[feat];
1002
1003                                 /* Char and attr of darkness */
1004                                 a = f_ptr->x_attr[F_LIT_STANDARD];
1005                                 c = f_ptr->x_char[F_LIT_STANDARD];
1006                         }
1007
1008                         /* Special lighting effects */
1009                         else if (view_special_lite)
1010                         {
1011                                 /* Handle "torch-lit" grids */
1012                                 if (c_ptr->info & (CAVE_LITE | CAVE_MNLT))
1013                                 {
1014                                         /* Torch lite */
1015                                         if (view_yellow_lite)
1016                                         {
1017                                                 /* Use a brightly lit colour/tile */
1018                                                 a = f_ptr->x_attr[F_LIT_LITE];
1019                                                 c = f_ptr->x_char[F_LIT_LITE];
1020                                         }
1021                                 }
1022
1023                                 /* Handle "dark" grids */
1024                                 else if ((c_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1025                                 {
1026                                         /* Use a darkened colour/tile */
1027                                         a = f_ptr->x_attr[F_LIT_DARK];
1028                                         c = f_ptr->x_char[F_LIT_DARK];
1029                                 }
1030
1031                                 /* Handle "out-of-sight" grids */
1032                                 else if (!(c_ptr->info & CAVE_VIEW))
1033                                 {
1034                                         /* Special flag */
1035                                         if (view_bright_lite)
1036                                         {
1037                                                 /* Use a darkened colour/tile */
1038                                                 a = f_ptr->x_attr[F_LIT_DARK];
1039                                                 c = f_ptr->x_char[F_LIT_DARK];
1040                                         }
1041                                 }
1042                         }
1043                 }
1044
1045                 /* Unknown */
1046                 else
1047                 {
1048                         /* Unsafe cave grid -- idea borrowed from Unangband */
1049                         feat = (view_unsafe_grids && (c_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1050
1051                         /* Access darkness */
1052                         f_ptr = &f_info[feat];
1053
1054                         /* Normal attr/char */
1055                         a = f_ptr->x_attr[F_LIT_STANDARD];
1056                         c = f_ptr->x_char[F_LIT_STANDARD];
1057                 }
1058         }
1059
1060         /* Interesting grids (non-floors) */
1061         else
1062         {
1063                 /* Memorized grids */
1064                 if (c_ptr->info & CAVE_MARK)
1065                 {
1066                         /* Normal attr/char */
1067                         a = f_ptr->x_attr[F_LIT_STANDARD];
1068                         c = f_ptr->x_char[F_LIT_STANDARD];
1069
1070                         if (p_ptr->wild_mode)
1071                         {
1072                                 /* Special lighting effects */
1073                                 /* Handle "blind" or "night" */
1074                                 if (view_granite_lite && (p_ptr->blind || !is_daytime()))
1075                                 {
1076                                         /* Use a darkened colour/tile */
1077                                         a = f_ptr->x_attr[F_LIT_DARK];
1078                                         c = f_ptr->x_char[F_LIT_DARK];
1079                                 }
1080                         }
1081
1082                         /* Mega-Hack -- Handle "in-sight" and "darkened" grids */
1083                         else if (darkened_grid(c_ptr) && !p_ptr->blind)
1084                         {
1085                                 if (have_flag(f_ptr->flags, FF_LOS) && have_flag(f_ptr->flags, FF_PROJECT))
1086                                 {
1087                                         /* Unsafe cave grid -- idea borrowed from Unangband */
1088                                         feat = (view_unsafe_grids && (c_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1089
1090                                         /* Access darkness */
1091                                         f_ptr = &f_info[feat];
1092
1093                                         /* Char and attr of darkness */
1094                                         a = f_ptr->x_attr[F_LIT_STANDARD];
1095                                         c = f_ptr->x_char[F_LIT_STANDARD];
1096                                 }
1097                                 else if (view_granite_lite && view_bright_lite)
1098                                 {
1099                                         /* Use a darkened colour/tile */
1100                                         a = f_ptr->x_attr[F_LIT_DARK];
1101                                         c = f_ptr->x_char[F_LIT_DARK];
1102                                 }
1103                         }
1104
1105                         /* Special lighting effects */
1106                         else if (view_granite_lite)
1107                         {
1108                                 /* Handle "blind" */
1109                                 if (p_ptr->blind)
1110                                 {
1111                                         /* Use a darkened colour/tile */
1112                                         a = f_ptr->x_attr[F_LIT_DARK];
1113                                         c = f_ptr->x_char[F_LIT_DARK];
1114                                 }
1115
1116                                 /* Handle "torch-lit" grids */
1117                                 else if (c_ptr->info & (CAVE_LITE | CAVE_MNLT))
1118                                 {
1119                                         /* Torch lite */
1120                                         if (view_yellow_lite)
1121                                         {
1122                                                 /* Use a brightly lit colour/tile */
1123                                                 a = f_ptr->x_attr[F_LIT_LITE];
1124                                                 c = f_ptr->x_char[F_LIT_LITE];
1125                                         }
1126                                 }
1127
1128                                 /* Handle "view_bright_lite" */
1129                                 else if (view_bright_lite)
1130                                 {
1131                                         /* Not viewable */
1132                                         if (!(c_ptr->info & CAVE_VIEW))
1133                                         {
1134                                                 /* Use a darkened colour/tile */
1135                                                 a = f_ptr->x_attr[F_LIT_DARK];
1136                                                 c = f_ptr->x_char[F_LIT_DARK];
1137                                         }
1138
1139                                         /* Not glowing */
1140                                         else if ((c_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1141                                         {
1142                                                 /* Use a darkened colour/tile */
1143                                                 a = f_ptr->x_attr[F_LIT_DARK];
1144                                                 c = f_ptr->x_char[F_LIT_DARK];
1145                                         }
1146
1147                                         /* Not glowing correctly */
1148                                         else if (!have_flag(f_ptr->flags, FF_LOS) && !check_local_illumination(y, x))
1149                                         {
1150                                                 /* Use a darkened colour/tile */
1151                                                 a = f_ptr->x_attr[F_LIT_DARK];
1152                                                 c = f_ptr->x_char[F_LIT_DARK];
1153                                         }
1154                                 }
1155                         }
1156                 }
1157
1158                 /* Unknown */
1159                 else
1160                 {
1161                         /* Unsafe cave grid -- idea borrowed from Unangband */
1162                         feat = (view_unsafe_grids && (c_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
1163
1164                         /* Access feature */
1165                         f_ptr = &f_info[feat];
1166
1167                         /* Normal attr/char */
1168                         a = f_ptr->x_attr[F_LIT_STANDARD];
1169                         c = f_ptr->x_char[F_LIT_STANDARD];
1170                 }
1171         }
1172
1173         if (feat_priority == -1) feat_priority = f_ptr->priority;
1174
1175         /* Save the terrain info for the transparency effects */
1176         (*tap) = a;
1177         (*tcp) = c;
1178
1179         /* Save the info */
1180         (*ap) = a;
1181         (*cp) = c;
1182
1183         /* Hack -- rare random hallucination, except on outer dungeon walls */
1184         if (p_ptr->image)
1185         {
1186                 if (one_in_(256))
1187                 {
1188                         /* Hallucinate */
1189                         image_random(ap, cp);
1190                 }
1191         }
1192
1193         /* Objects */
1194         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1195         {
1196                 object_type *o_ptr;
1197                 o_ptr = &o_list[this_o_idx];
1198
1199                 /* Acquire next object */
1200                 next_o_idx = o_ptr->next_o_idx;
1201
1202                 /* Memorized objects */
1203                 if (o_ptr->marked & OM_FOUND)
1204                 {
1205                         if (display_autopick)
1206                         {
1207                                 byte act;
1208
1209                                 match_autopick = is_autopick(o_ptr);
1210                                 if(match_autopick == -1)
1211                                         continue;
1212
1213                                 act = autopick_list[match_autopick].action;
1214
1215                                 if ((act & DO_DISPLAY) && (act & display_autopick))
1216                                 {
1217                                         autopick_obj = o_ptr;
1218                                 }
1219                                 else
1220                                 {
1221                                         match_autopick = -1;
1222                                         continue;
1223                                 }
1224                         }
1225                         /* Normal char */
1226                         (*cp) = object_char(o_ptr);
1227
1228                         /* Normal attr */
1229                         (*ap) = object_attr(o_ptr);
1230
1231                         feat_priority = 20;
1232
1233                         /* Hack -- hallucination */
1234                         if (p_ptr->image) image_object(ap, cp);
1235
1236                         break;
1237                 }
1238         }
1239
1240
1241         /* Handle monsters */
1242         if (c_ptr->m_idx && display_autopick == 0 )
1243         {
1244                 monster_type *m_ptr = &m_list[c_ptr->m_idx];
1245
1246                 /* Visible monster */
1247                 if (m_ptr->ml)
1248                 {
1249                         monster_race *r_ptr = &r_info[m_ptr->ap_r_idx];
1250
1251                         feat_priority = 30;
1252
1253                         /* Hallucination */
1254                         if (p_ptr->image)
1255                         {
1256                                 /*
1257                                  * Monsters with both CHAR_CLEAR and ATTR_CLEAR
1258                                  * flags are always unseen.
1259                                  */
1260                                 if ((r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR)) == (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR))
1261                                 {
1262                                         /* Do nothing */
1263                                 }
1264                                 else
1265                                 {
1266                                         /* Hallucinatory monster */
1267                                         image_monster(ap, cp);
1268                                 }
1269                         }
1270                         else
1271                         {
1272                                 /* Monster attr/char */
1273                                 a = r_ptr->x_attr;
1274                                 c = r_ptr->x_char;
1275
1276                                 /* Normal monsters */
1277                                 if (!(r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_SHAPECHANGER | RF1_ATTR_CLEAR
1278                                                            | RF1_ATTR_MULTI | RF1_ATTR_SEMIRAND)))
1279                                 {
1280                                         /* Desired monster attr/char */
1281                                         *ap = a;
1282                                         *cp = c;
1283                                 }
1284
1285                                 /*
1286                                  * Monsters with both CHAR_CLEAR and ATTR_CLEAR
1287                                  * flags are always unseen.
1288                                  */
1289                                 else if ((r_ptr->flags1 & (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR)) == (RF1_CHAR_CLEAR | RF1_ATTR_CLEAR))
1290                                 {
1291                                         /* Do nothing */
1292                                 }
1293
1294                                 else
1295                                 {
1296                                         /***  Monster's attr  ***/
1297                                         if ((r_ptr->flags1 & RF1_ATTR_CLEAR) && (*ap != TERM_DARK) && !use_graphics)
1298                                         {
1299                                                 /* Clear-attr */
1300                                                 /* Do nothing */
1301                                         }
1302                                         else if ((r_ptr->flags1 & RF1_ATTR_MULTI) && !use_graphics)
1303                                         {
1304                                                 /* Multi-hued attr */
1305                                                 if (r_ptr->flags2 & RF2_ATTR_ANY) *ap = randint1(15);
1306                                                 else switch (randint1(7))
1307                                                 {
1308                                                 case 1: *ap = TERM_RED;     break;
1309                                                 case 2: *ap = TERM_L_RED;   break;
1310                                                 case 3: *ap = TERM_WHITE;   break;
1311                                                 case 4: *ap = TERM_L_GREEN; break;
1312                                                 case 5: *ap = TERM_BLUE;    break;
1313                                                 case 6: *ap = TERM_L_DARK;  break;
1314                                                 case 7: *ap = TERM_GREEN;   break;
1315                                                 }
1316                                         }
1317                                         else if ((r_ptr->flags1 & RF1_ATTR_SEMIRAND) && !use_graphics)
1318                                         {
1319                                                 /* Use semi-random attr (usually mimics' colors vary) */
1320                                                 *ap = c_ptr->m_idx % 15 + 1;
1321                                         }
1322                                         else
1323                                         {
1324                                                 /* Normal case */
1325                                                 *ap = a;
1326                                         }
1327
1328                                         /***  Monster's char  ***/
1329                                         if ((r_ptr->flags1 & RF1_CHAR_CLEAR) && (*cp != ' ') && !use_graphics)
1330                                         {
1331                                                 /* Clear-char */
1332                                                 /* Do nothing */
1333                                         }
1334                                         else if (r_ptr->flags1 & RF1_SHAPECHANGER)
1335                                         {
1336                                                 if (use_graphics)
1337                                                 {
1338                                                         monster_race *tmp_r_ptr = &r_info[randint1(max_r_idx - 1)];
1339                                                         *cp = tmp_r_ptr->x_char;
1340                                                         *ap = tmp_r_ptr->x_attr;
1341                                                 }
1342                                                 else
1343                                                 {
1344                                                         *cp = (one_in_(25) ?
1345                                                                image_object_hack[randint0(sizeof(image_object_hack) - 1)] :
1346                                                                image_monster_hack[randint0(sizeof(image_monster_hack) - 1)]);
1347                                                 }
1348                                         }
1349                                         else
1350                                         {
1351                                                 /* Normal case */
1352                                                 *cp = c;
1353                                         }
1354                                 }
1355                         }
1356                 }
1357         }
1358
1359         /* Handle "player" */
1360         if (player_bold(y, x))
1361         {
1362                 monster_race *r_ptr = &r_info[0];
1363                 *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() || world_player) 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() || world_player) 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() || world_player) 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() || world_player) 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() || world_player) 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() || world_player) 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         /* Note */
2180         prt(_("お待ち下さい...", "Please wait..."), 0, 0);
2181
2182         Term_fresh();
2183         Term_clear();
2184
2185         display_autopick = 0;
2186
2187         /* Display the map */
2188         display_map(&cy, &cx);
2189
2190         /* Wait for it */
2191         if(max_autopick && !p_ptr->wild_mode)
2192         {
2193                 display_autopick = ITEM_DISPLAY;
2194
2195                 while (1)
2196                 {
2197                         int i;
2198                         byte flag;
2199
2200                         int wid, hgt, row_message;
2201
2202                         Term_get_size(&wid, &hgt);
2203                         row_message = hgt - 1;
2204
2205                         put_str(_("何かキーを押してください('M':拾う 'N':放置 'D':M+N 'K':壊すアイテムを表示)",
2206                                           " Hit M, N(for ~), K(for !), or D(same as M+N) to display auto-picker items."), row_message, 1);
2207
2208                         /* Hilite the player */
2209                         move_cursor(cy, cx);
2210
2211                         i = inkey();
2212
2213                         if ('M' == i)
2214                                 flag = (DO_AUTOPICK | DO_QUERY_AUTOPICK);
2215                         else if ('N' == i)
2216                                 flag = DONT_AUTOPICK;
2217                         else if ('K' == i)
2218                                 flag = DO_AUTODESTROY;
2219                         else if ('D' == i)
2220                                 flag = (DO_AUTOPICK | DO_QUERY_AUTOPICK | DONT_AUTOPICK);
2221                         else
2222                                 break;
2223
2224                         Term_fresh();
2225                         
2226                         if (~display_autopick & flag)
2227                                 display_autopick |= flag;
2228                         else
2229                                 display_autopick &= ~flag;
2230                         /* Display the map */
2231                         display_map(&cy, &cx);
2232                 }
2233                 
2234                 display_autopick = 0;
2235
2236         }
2237         else
2238         {
2239                 put_str(_("何かキーを押すとゲームに戻ります", "Hit any key to continue"), 23, 30);              
2240                 /* Hilite the player */
2241                 move_cursor(cy, cx);
2242                 /* Get any key */
2243                 inkey();
2244         }
2245         screen_load();
2246 }
2247
2248
2249
2250
2251
2252 /*
2253  * Some comments on the cave grid flags.  -BEN-
2254  *
2255  *
2256  * One of the major bottlenecks in previous versions of Angband was in
2257  * the calculation of "line of sight" from the player to various grids,
2258  * such as monsters.  This was such a nasty bottleneck that a lot of
2259  * silly things were done to reduce the dependancy on "line of sight",
2260  * for example, you could not "see" any grids in a lit room until you
2261  * actually entered the room, and there were all kinds of bizarre grid
2262  * flags to enable this behavior.  This is also why the "call light"
2263  * spells always lit an entire room.
2264  *
2265  * The code below provides functions to calculate the "field of view"
2266  * for the player, which, once calculated, provides extremely fast
2267  * calculation of "line of sight from the player", and to calculate
2268  * the "field of torch lite", which, again, once calculated, provides
2269  * extremely fast calculation of "which grids are lit by the player's
2270  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
2271  * "GRID_LITE", as appropriate, these functions maintain an array for
2272  * each of these two flags, each array containing the locations of all
2273  * of the grids marked with the appropriate flag, which can be used to
2274  * very quickly scan through all of the grids in a given set.
2275  *
2276  * To allow more "semantically valid" field of view semantics, whenever
2277  * the field of view (or the set of torch lit grids) changes, all of the
2278  * grids in the field of view (or the set of torch lit grids) are "drawn"
2279  * so that changes in the world will become apparent as soon as possible.
2280  * This has been optimized so that only grids which actually "change" are
2281  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
2282  * of the grids which are entering or leaving the relevent set of grids.
2283  *
2284  * These new methods are so efficient that the old nasty code was removed.
2285  *
2286  * Note that there is no reason to "update" the "viewable space" unless
2287  * the player "moves", or walls/doors are created/destroyed, and there
2288  * is no reason to "update" the "torch lit grids" unless the field of
2289  * view changes, or the "light radius" changes.  This means that when
2290  * the player is resting, or digging, or doing anything that does not
2291  * involve movement or changing the state of the dungeon, there is no
2292  * need to update the "view" or the "lite" regions, which is nice.
2293  *
2294  * Note that the calls to the nasty "los()" function have been reduced
2295  * to a bare minimum by the use of the new "field of view" calculations.
2296  *
2297  * I wouldn't be surprised if slight modifications to the "update_view()"
2298  * function would allow us to determine "reverse line-of-sight" as well
2299  * as "normal line-of-sight", which would allow monsters to use a more
2300  * "correct" calculation to determine if they can "see" the player.  For
2301  * now, monsters simply "cheat" somewhat and assume that if the player
2302  * has "line of sight" to the monster, then the monster can "pretend"
2303  * that it has "line of sight" to the player.
2304  *
2305  *
2306  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
2307  * grid and maintains an array of all "CAVE_LITE" grids.
2308  *
2309  * This set of grids is the complete set of all grids which are lit by
2310  * the players light source, which allows the "player_can_see_bold()"
2311  * function to work very quickly.
2312  *
2313  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
2314  * fact, the player (unless blind) can always "see" all grids which are
2315  * marked as "CAVE_LITE", unless they are "off screen".
2316  *
2317  *
2318  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
2319  * grid and maintains an array of all "CAVE_VIEW" grids.
2320  *
2321  * This set of grids is the complete set of all grids within line of sight
2322  * of the player, allowing the "player_has_los_bold()" macro to work very
2323  * quickly.
2324  *
2325  *
2326  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
2327  * temporary internal flag to mark those grids which are not only in view,
2328  * but which are also "easily" in line of sight of the player.  This flag
2329  * is always cleared when we are done.
2330  *
2331  *
2332  * The current "update_lite()" and "update_view()" algorithms use the
2333  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
2334  * to keep track of which grids were previously marked as "CAVE_LITE" or
2335  * "CAVE_VIEW", which allows us to optimize the "screen updates".
2336  *
2337  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
2338  * for various other purposes, such as spreading lite or darkness during
2339  * "lite_room()" / "unlite_room()", and for calculating monster flow.
2340  *
2341  *
2342  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
2343  * in some way permanently lit.  However, for the player to "see" anything
2344  * in the grid, as determined by "player_can_see()", the player must not be
2345  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
2346  * grids, even if marked as "perma lit", are only illuminated if they touch
2347  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
2348  *
2349  *
2350  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
2351  * that even if the player cannot "see" the grid, he "knows" the terrain in
2352  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
2353  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
2354  * or when one of the "memorize floor grids" options induces memorization.
2355  *
2356  * Objects are "memorized" in a different way, using a special "marked" flag
2357  * on the object itself, which is set when an object is observed or detected.
2358  *
2359  *
2360  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
2361  * and should be illuminated by "lite room" and "darkness" spells.
2362  *
2363  *
2364  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
2365  * and should be unavailable for "teleportation" destinations.
2366  *
2367  *
2368  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
2369  * which is observed, and the "view_torch_grids" allows the player to memorize
2370  * every torch-lit grid.  The player will always memorize important walls,
2371  * doors, stairs, and other terrain features, as well as any "detected" grids.
2372  *
2373  * Note that the new "update_view()" method allows, among other things, a room
2374  * to be "partially" seen as the player approaches it, with a growing cone of
2375  * floor appearing as the player gets closer to the door.  Also, by not turning
2376  * on the "memorize perma-lit grids" option, the player will only "see" those
2377  * floor grids which are actually in line of sight.
2378  *
2379  * And my favorite "plus" is that you can now use a special option to draw the
2380  * "floors" in the "viewable region" brightly (actually, to draw the *other*
2381  * grids dimly), providing a "pretty" effect as the player runs around, and
2382  * to efficiently display the "torch lite" in a special color.
2383  *
2384  *
2385  * Some comments on the "update_view()" algorithm...
2386  *
2387  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
2388  * and only has to call "los()" on the borderline cases.  The major axes/diags
2389  * even terminate early when they hit walls.  I need to find a quick way
2390  * to "terminate" the other scans.
2391  *
2392  * Note that in the worst case (a big empty area with say 5% scattered walls),
2393  * each of the 1500 or so nearby grids is checked once, most of them getting
2394  * an "instant" rating, and only a small portion requiring a call to "los()".
2395  *
2396  * The only time that the algorithm appears to be "noticeably" too slow is
2397  * when running, and this is usually only important in town, since the town
2398  * provides about the worst scenario possible, with large open regions and
2399  * a few scattered obstructions.  There is a special "efficiency" option to
2400  * allow the player to reduce his field of view in town, if needed.
2401  *
2402  * In the "best" case (say, a normal stretch of corridor), the algorithm
2403  * makes one check for each viewable grid, and makes no calls to "los()".
2404  * So running in corridors is very fast, and if a lot of monsters are
2405  * nearby, it is much faster than the old methods.
2406  *
2407  * Note that resting, most normal commands, and several forms of running,
2408  * plus all commands executed near large groups of monsters, are strictly
2409  * more efficient with "update_view()" that with the old "compute los() on
2410  * demand" method, primarily because once the "field of view" has been
2411  * calculated, it does not have to be recalculated until the player moves
2412  * (or a wall or door is created or destroyed).
2413  *
2414  * Note that we no longer have to do as many "los()" checks, since once the
2415  * "view" region has been built, very few things cause it to be "changed"
2416  * (player movement, and the opening/closing of doors, changes in wall status).
2417  * Note that door/wall changes are only relevant when the door/wall itself is
2418  * in the "view" region.
2419  *
2420  * The algorithm seems to only call "los()" from zero to ten times, usually
2421  * only when coming down a corridor into a room, or standing in a room, just
2422  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
2423  * we will be reducing the calls to "los()".
2424  *
2425  * I am thinking in terms of an algorithm that "walks" from the central point
2426  * out to the maximal "distance", at each point, determining the "view" code
2427  * (above).  For each grid not on a major axis or diagonal, the "view" code
2428  * depends on the "cave_los_bold()" and "view" of exactly two other grids
2429  * (the one along the nearest diagonal, and the one next to that one, see
2430  * "update_view_aux()"...).
2431  *
2432  * We "memorize" the viewable space array, so that at the cost of under 3000
2433  * bytes, we reduce the time taken by "forget_view()" to one assignment for
2434  * each grid actually in the "viewable space".  And for another 3000 bytes,
2435  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
2436  * are also used by other routines, thus reducing the cost to almost nothing.
2437  *
2438  * A similar thing is done for "forget_lite()" in which case the savings are
2439  * much less, but save us from doing bizarre maintenance checking.
2440  *
2441  * In the worst "normal" case (in the middle of the town), the reachable space
2442  * actually reaches to more than half of the largest possible "circle" of view,
2443  * or about 800 grids, and in the worse case (in the middle of a dungeon level
2444  * where all the walls have been removed), the reachable space actually reaches
2445  * the theoretical maximum size of just under 1500 grids.
2446  *
2447  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
2448  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
2449  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
2450  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
2451  * entire possible space (including initialization) in one step per grid.  If
2452  * we do the "clearing" as a separate step (and use an array of "view" grids),
2453  * then the clearing will take as many steps as grids that were viewed, and the
2454  * algorithm will be able to "stop" scanning at various points.
2455  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
2456  */
2457
2458
2459
2460
2461
2462
2463
2464
2465 /*
2466  * Actually erase the entire "lite" array, redrawing every grid
2467  */
2468 void forget_lite(void)
2469 {
2470         int i, x, y;
2471
2472         /* None to forget */
2473         if (!lite_n) return;
2474
2475         /* Clear them all */
2476         for (i = 0; i < lite_n; i++)
2477         {
2478                 y = lite_y[i];
2479                 x = lite_x[i];
2480
2481                 /* Forget "LITE" flag */
2482                 cave[y][x].info &= ~(CAVE_LITE);
2483
2484                 /* lite_spot(y, x); Perhaps don't need? */
2485         }
2486
2487         /* None left */
2488         lite_n = 0;
2489 }
2490
2491
2492 /*
2493  * For delayed visual update
2494  */
2495 #define cave_note_and_redraw_later(C,Y,X) \
2496 {\
2497         (C)->info |= CAVE_NOTE; \
2498         cave_redraw_later((C), (Y), (X)); \
2499 }
2500
2501
2502 /*
2503  * For delayed visual update
2504  */
2505 #define cave_redraw_later(C,Y,X) \
2506 {\
2507         if (!((C)->info & CAVE_REDRAW)) \
2508         { \
2509                 (C)->info |= CAVE_REDRAW; \
2510                 redraw_y[redraw_n] = (Y); \
2511                 redraw_x[redraw_n++] = (X); \
2512         } \
2513 }
2514
2515
2516 /*
2517  * This macro allows us to efficiently add a grid to the "lite" array,
2518  * note that we are never called for illegal grids, or for grids which
2519  * have already been placed into the "lite" array, and we are never
2520  * called when the "lite" array is full.
2521  */
2522 #define cave_lite_hack(Y,X) \
2523 {\
2524         if (!(cave[Y][X].info & (CAVE_LITE))) \
2525         { \
2526                 cave[Y][X].info |= (CAVE_LITE); \
2527                 lite_y[lite_n] = (Y); \
2528                 lite_x[lite_n++] = (X); \
2529         } \
2530 }
2531
2532
2533 /*
2534  * Update the set of grids "illuminated" by the player's lite.
2535  *
2536  * This routine needs to use the results of "update_view()"
2537  *
2538  * Note that "blindness" does NOT affect "torch lite".  Be careful!
2539  *
2540  * We optimize most lites (all non-artifact lites) by using "obvious"
2541  * facts about the results of "small" lite radius, and we attempt to
2542  * list the "nearby" grids before the more "distant" ones in the
2543  * array of torch-lit grids.
2544  *
2545  * We assume that "radius zero" lite is in fact no lite at all.
2546  *
2547  *     Torch     Lantern     Artifacts
2548  *     (etc)
2549  *                              ***
2550  *                 ***         *****
2551  *      ***       *****       *******
2552  *      *@*       **@**       ***@***
2553  *      ***       *****       *******
2554  *                 ***         *****
2555  *                              ***
2556  */
2557 void update_lite(void)
2558 {
2559         int i, x, y, min_x, max_x, min_y, max_y;
2560         int p = p_ptr->cur_lite;
2561         cave_type *c_ptr;
2562
2563         /*** Special case ***/
2564
2565 #if 0
2566         /* Hack -- Player has no lite */
2567         if (p <= 0)
2568         {
2569                 /* Forget the old lite */
2570                 /* forget_lite(); Perhaps don't need? */
2571
2572                 /* Add it to later visual update */
2573                 cave_redraw_later(&cave[p_ptr->y][p_ptr->x], p_ptr->y, p_ptr->x);
2574         }
2575 #endif
2576
2577         /*** Save the old "lite" grids for later ***/
2578
2579         /* Clear them all */
2580         for (i = 0; i < lite_n; i++)
2581         {
2582                 y = lite_y[i];
2583                 x = lite_x[i];
2584
2585                 /* Mark the grid as not "lite" */
2586                 cave[y][x].info &= ~(CAVE_LITE);
2587
2588                 /* Mark the grid as "seen" */
2589                 cave[y][x].info |= (CAVE_TEMP);
2590
2591                 /* Add it to the "seen" set */
2592                 temp_y[temp_n] = y;
2593                 temp_x[temp_n] = x;
2594                 temp_n++;
2595         }
2596
2597         /* None left */
2598         lite_n = 0;
2599
2600
2601         /*** Collect the new "lite" grids ***/
2602
2603         /* Radius 1 -- torch radius */
2604         if (p >= 1)
2605         {
2606                 /* Player grid */
2607                 cave_lite_hack(p_ptr->y, p_ptr->x);
2608
2609                 /* Adjacent grid */
2610                 cave_lite_hack(p_ptr->y+1, p_ptr->x);
2611                 cave_lite_hack(p_ptr->y-1, p_ptr->x);
2612                 cave_lite_hack(p_ptr->y, p_ptr->x+1);
2613                 cave_lite_hack(p_ptr->y, p_ptr->x-1);
2614
2615                 /* Diagonal grids */
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                 cave_lite_hack(p_ptr->y-1, p_ptr->x-1);
2620         }
2621
2622         /* Radius 2 -- lantern radius */
2623         if (p >= 2)
2624         {
2625                 /* South of the player */
2626                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x))
2627                 {
2628                         cave_lite_hack(p_ptr->y+2, p_ptr->x);
2629                         cave_lite_hack(p_ptr->y+2, p_ptr->x+1);
2630                         cave_lite_hack(p_ptr->y+2, p_ptr->x-1);
2631                 }
2632
2633                 /* North of the player */
2634                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x))
2635                 {
2636                         cave_lite_hack(p_ptr->y-2, p_ptr->x);
2637                         cave_lite_hack(p_ptr->y-2, p_ptr->x+1);
2638                         cave_lite_hack(p_ptr->y-2, p_ptr->x-1);
2639                 }
2640
2641                 /* East of the player */
2642                 if (cave_los_bold(p_ptr->y, p_ptr->x + 1))
2643                 {
2644                         cave_lite_hack(p_ptr->y, p_ptr->x+2);
2645                         cave_lite_hack(p_ptr->y+1, p_ptr->x+2);
2646                         cave_lite_hack(p_ptr->y-1, p_ptr->x+2);
2647                 }
2648
2649                 /* West of the player */
2650                 if (cave_los_bold(p_ptr->y, p_ptr->x - 1))
2651                 {
2652                         cave_lite_hack(p_ptr->y, p_ptr->x-2);
2653                         cave_lite_hack(p_ptr->y+1, p_ptr->x-2);
2654                         cave_lite_hack(p_ptr->y-1, p_ptr->x-2);
2655                 }
2656         }
2657
2658         /* Radius 3+ -- artifact radius */
2659         if (p >= 3)
2660         {
2661                 int d;
2662
2663                 /* Paranoia -- see "LITE_MAX" */
2664                 if (p > 14) p = 14;
2665
2666                 /* South-East of the player */
2667                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x + 1))
2668                 {
2669                         cave_lite_hack(p_ptr->y+2, p_ptr->x+2);
2670                 }
2671
2672                 /* South-West of the player */
2673                 if (cave_los_bold(p_ptr->y + 1, p_ptr->x - 1))
2674                 {
2675                         cave_lite_hack(p_ptr->y+2, p_ptr->x-2);
2676                 }
2677
2678                 /* North-East of the player */
2679                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x + 1))
2680                 {
2681                         cave_lite_hack(p_ptr->y-2, p_ptr->x+2);
2682                 }
2683
2684                 /* North-West of the player */
2685                 if (cave_los_bold(p_ptr->y - 1, p_ptr->x - 1))
2686                 {
2687                         cave_lite_hack(p_ptr->y-2, p_ptr->x-2);
2688                 }
2689
2690                 /* Maximal north */
2691                 min_y = p_ptr->y - p;
2692                 if (min_y < 0) min_y = 0;
2693
2694                 /* Maximal south */
2695                 max_y = p_ptr->y + p;
2696                 if (max_y > cur_hgt-1) max_y = cur_hgt-1;
2697
2698                 /* Maximal west */
2699                 min_x = p_ptr->x - p;
2700                 if (min_x < 0) min_x = 0;
2701
2702                 /* Maximal east */
2703                 max_x = p_ptr->x + p;
2704                 if (max_x > cur_wid-1) max_x = cur_wid-1;
2705
2706                 /* Scan the maximal box */
2707                 for (y = min_y; y <= max_y; y++)
2708                 {
2709                         for (x = min_x; x <= max_x; x++)
2710                         {
2711                                 int dy = (p_ptr->y > y) ? (p_ptr->y - y) : (y - p_ptr->y);
2712                                 int dx = (p_ptr->x > x) ? (p_ptr->x - x) : (x - p_ptr->x);
2713
2714                                 /* Skip the "central" grids (above) */
2715                                 if ((dy <= 2) && (dx <= 2)) continue;
2716
2717                                 /* Hack -- approximate the distance */
2718                                 d = (dy > dx) ? (dy + (dx>>1)) : (dx + (dy>>1));
2719
2720                                 /* Skip distant grids */
2721                                 if (d > p) continue;
2722
2723                                 /* Viewable, nearby, grids get "torch lit" */
2724                                 if (cave[y][x].info & CAVE_VIEW)
2725                                 {
2726                                         /* This grid is "torch lit" */
2727                                         cave_lite_hack(y, x);
2728                                 }
2729                         }
2730                 }
2731         }
2732
2733
2734         /*** Complete the algorithm ***/
2735
2736         /* Draw the new grids */
2737         for (i = 0; i < lite_n; i++)
2738         {
2739                 y = lite_y[i];
2740                 x = lite_x[i];
2741
2742                 c_ptr = &cave[y][x];
2743
2744                 /* Update fresh grids */
2745                 if (c_ptr->info & (CAVE_TEMP)) continue;
2746
2747                 /* Add it to later visual update */
2748                 cave_note_and_redraw_later(c_ptr, y, x);
2749         }
2750
2751         /* Clear them all */
2752         for (i = 0; i < temp_n; i++)
2753         {
2754                 y = temp_y[i];
2755                 x = temp_x[i];
2756
2757                 c_ptr = &cave[y][x];
2758
2759                 /* No longer in the array */
2760                 c_ptr->info &= ~(CAVE_TEMP);
2761
2762                 /* Update stale grids */
2763                 if (c_ptr->info & (CAVE_LITE)) continue;
2764
2765                 /* Add it to later visual update */
2766                 cave_redraw_later(c_ptr, y, x);
2767         }
2768
2769         /* None left */
2770         temp_n = 0;
2771
2772         /* Mega-Hack -- Visual update later */
2773         p_ptr->update |= (PU_DELAY_VIS);
2774 }
2775
2776
2777 static bool mon_invis;
2778 static POSITION mon_fy, mon_fx;
2779
2780 /*
2781  * Add a square to the changes array
2782  */
2783 static void mon_lite_hack(POSITION y, POSITION x)
2784 {
2785         cave_type *c_ptr;
2786         int dpf, d;
2787         POSITION midpoint;
2788
2789         /* We trust this grid is in bounds */
2790         /* if (!in_bounds2(y, x)) return; */
2791
2792         c_ptr = &cave[y][x];
2793
2794         /* Want a unlit square in view of the player */
2795         if ((c_ptr->info & (CAVE_MNLT | CAVE_VIEW)) != CAVE_VIEW) return;
2796
2797         if (!cave_los_grid(c_ptr))
2798         {
2799                 /* Hack -- Prevent monster lite leakage in walls */
2800
2801                 /* Horizontal walls between player and a monster */
2802                 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
2803                 {
2804                         dpf = p_ptr->y - mon_fy;
2805                         d = y - mon_fy;
2806                         midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
2807
2808                         /* Only first wall viewed from mid-x is lit */
2809                         if (x < midpoint)
2810                         {
2811                                 if (!cave_los_bold(y, x + 1)) return;
2812                         }
2813                         else if (x > midpoint)
2814                         {
2815                                 if (!cave_los_bold(y, x - 1)) return;
2816                         }
2817
2818                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2819                         else if (mon_invis) return;
2820                 }
2821
2822                 /* Vertical walls between player and a monster */
2823                 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
2824                 {
2825                         dpf = p_ptr->x - mon_fx;
2826                         d = x - mon_fx;
2827                         midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
2828
2829                         /* Only first wall viewed from mid-y is lit */
2830                         if (y < midpoint)
2831                         {
2832                                 if (!cave_los_bold(y + 1, x)) return;
2833                         }
2834                         else if (y > midpoint)
2835                         {
2836                                 if (!cave_los_bold(y - 1, x)) return;
2837                         }
2838
2839                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2840                         else if (mon_invis) return;
2841                 }
2842         }
2843
2844         /* We trust temp_n does not exceed TEMP_MAX */
2845
2846         /* New grid */
2847         if (!(c_ptr->info & CAVE_MNDK))
2848         {
2849                 /* Save this square */
2850                 temp_x[temp_n] = x;
2851                 temp_y[temp_n] = y;
2852                 temp_n++;
2853         }
2854
2855         /* Darkened grid */
2856         else
2857         {
2858                 /* No longer dark */
2859                 c_ptr->info &= ~(CAVE_MNDK);
2860         }
2861
2862         /* Light it */
2863         c_ptr->info |= CAVE_MNLT;
2864 }
2865
2866
2867 /*
2868  * Add a square to the changes array
2869  */
2870 static void mon_dark_hack(POSITION y, POSITION x)
2871 {
2872         cave_type *c_ptr;
2873         int       midpoint, dpf, d;
2874
2875         /* We trust this grid is in bounds */
2876         /* if (!in_bounds2(y, x)) return; */
2877
2878         c_ptr = &cave[y][x];
2879
2880         /* Want a unlit and undarkened square in view of the player */
2881         if ((c_ptr->info & (CAVE_LITE | CAVE_MNLT | CAVE_MNDK | CAVE_VIEW)) != CAVE_VIEW) return;
2882
2883         if (!cave_los_grid(c_ptr) && !cave_have_flag_grid(c_ptr, FF_PROJECT))
2884         {
2885                 /* Hack -- Prevent monster dark lite leakage in walls */
2886
2887                 /* Horizontal walls between player and a monster */
2888                 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
2889                 {
2890                         dpf = p_ptr->y - mon_fy;
2891                         d = y - mon_fy;
2892                         midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
2893
2894                         /* Only first wall viewed from mid-x is lit */
2895                         if (x < midpoint)
2896                         {
2897                                 if (!cave_los_bold(y, x + 1) && !cave_have_flag_bold(y, x + 1, FF_PROJECT)) return;
2898                         }
2899                         else if (x > midpoint)
2900                         {
2901                                 if (!cave_los_bold(y, x - 1) && !cave_have_flag_bold(y, x - 1, FF_PROJECT)) return;
2902                         }
2903
2904                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2905                         else if (mon_invis) return;
2906                 }
2907
2908                 /* Vertical walls between player and a monster */
2909                 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
2910                 {
2911                         dpf = p_ptr->x - mon_fx;
2912                         d = x - mon_fx;
2913                         midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
2914
2915                         /* Only first wall viewed from mid-y is lit */
2916                         if (y < midpoint)
2917                         {
2918                                 if (!cave_los_bold(y + 1, x) && !cave_have_flag_bold(y + 1, x, FF_PROJECT)) return;
2919                         }
2920                         else if (y > midpoint)
2921                         {
2922                                 if (!cave_los_bold(y - 1, x) && !cave_have_flag_bold(y - 1, x, FF_PROJECT)) return;
2923                         }
2924
2925                         /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2926                         else if (mon_invis) return;
2927                 }
2928         }
2929
2930         /* We trust temp_n does not exceed TEMP_MAX */
2931
2932         /* Save this square */
2933         temp_x[temp_n] = x;
2934         temp_y[temp_n] = y;
2935         temp_n++;
2936
2937         /* Darken it */
2938         c_ptr->info |= CAVE_MNDK;
2939 }
2940
2941
2942 /*
2943  * Update squares illuminated or darkened by monsters.
2944  *
2945  * Hack - use the CAVE_ROOM flag (renamed to be CAVE_MNLT) to
2946  * denote squares illuminated by monsters.
2947  *
2948  * The CAVE_TEMP and CAVE_XTRA flag are used to store the state during the
2949  * updating.  Only squares in view of the player, whos state
2950  * changes are drawn via lite_spot().
2951  */
2952 void update_mon_lite(void)
2953 {
2954         int i, rad;
2955         cave_type *c_ptr;
2956
2957         POSITION fx, fy;
2958         void (*add_mon_lite)(POSITION, POSITION);
2959         int f_flag;
2960
2961         s16b end_temp;
2962
2963         /* Non-Ninja player in the darkness */
2964         int dis_lim = ((d_info[dungeon_type].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto) ?
2965                 (MAX_SIGHT / 2 + 1) : (MAX_SIGHT + 3);
2966
2967         /* Clear all monster lit squares */
2968         for (i = 0; i < mon_lite_n; i++)
2969         {
2970                 /* Point to grid */
2971                 c_ptr = &cave[mon_lite_y[i]][mon_lite_x[i]];
2972
2973                 /* Set temp or xtra flag */
2974                 c_ptr->info |= (c_ptr->info & CAVE_MNLT) ? CAVE_TEMP : CAVE_XTRA;
2975
2976                 /* Clear monster illumination flag */
2977                 c_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
2978         }
2979
2980         /* Empty temp list of new squares to lite up */
2981         temp_n = 0;
2982
2983         /* If a monster stops time, don't process */
2984         if (!world_monster)
2985         {
2986                 monster_type *m_ptr;
2987                 monster_race *r_ptr;
2988
2989                 /* Loop through monsters, adding newly lit squares to changes list */
2990                 for (i = 1; i < m_max; i++)
2991                 {
2992                         m_ptr = &m_list[i];
2993                         r_ptr = &r_info[m_ptr->r_idx];
2994
2995                         /* Skip dead monsters */
2996                         if (!m_ptr->r_idx) continue;
2997
2998                         /* Is it too far away? */
2999                         if (m_ptr->cdis > dis_lim) continue;
3000
3001                         /* Get lite radius */
3002                         rad = 0;
3003
3004                         /* Note the radii are cumulative */
3005                         if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_SELF_LITE_1)) rad++;
3006                         if (r_ptr->flags7 & (RF7_HAS_LITE_2 | RF7_SELF_LITE_2)) rad += 2;
3007                         if (r_ptr->flags7 & (RF7_HAS_DARK_1 | RF7_SELF_DARK_1)) rad--;
3008                         if (r_ptr->flags7 & (RF7_HAS_DARK_2 | RF7_SELF_DARK_2)) rad -= 2;
3009
3010                         /* Exit if has no light */
3011                         if (!rad) continue;
3012                         else if (rad > 0)
3013                         {
3014                                 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;
3015                                 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) rad = 1;
3016                                 add_mon_lite = mon_lite_hack;
3017                                 f_flag = FF_LOS;
3018                         }
3019                         else
3020                         {
3021                                 if (!(r_ptr->flags7 & (RF7_SELF_DARK_1 | RF7_SELF_DARK_2)) && (MON_CSLEEP(m_ptr) || (!dun_level && !is_daytime()))) continue;
3022                                 add_mon_lite = mon_dark_hack;
3023                                 f_flag = FF_PROJECT;
3024                                 rad = -rad; /* Use absolute value */
3025                         }
3026
3027                         /* Access the location */
3028                         mon_fx = m_ptr->fx;
3029                         mon_fy = m_ptr->fy;
3030
3031                         /* Is the monster visible? */
3032                         mon_invis = !(cave[mon_fy][mon_fx].info & CAVE_VIEW);
3033
3034                         /* The square it is on */
3035                         add_mon_lite(mon_fy, mon_fx);
3036
3037                         /* Adjacent squares */
3038                         add_mon_lite(mon_fy + 1, mon_fx);
3039                         add_mon_lite(mon_fy - 1, mon_fx);
3040                         add_mon_lite(mon_fy, mon_fx + 1);
3041                         add_mon_lite(mon_fy, 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                         add_mon_lite(mon_fy - 1, mon_fx - 1);
3046
3047                         /* Radius 2 */
3048                         if (rad >= 2)
3049                         {
3050                                 /* South of the monster */
3051                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx, f_flag))
3052                                 {
3053                                         add_mon_lite(mon_fy + 2, mon_fx + 1);
3054                                         add_mon_lite(mon_fy + 2, mon_fx);
3055                                         add_mon_lite(mon_fy + 2, mon_fx - 1);
3056
3057                                         c_ptr = &cave[mon_fy + 2][mon_fx];
3058
3059                                         /* Radius 3 */
3060                                         if ((rad == 3) && cave_have_flag_grid(c_ptr, f_flag))
3061                                         {
3062                                                 add_mon_lite(mon_fy + 3, mon_fx + 1);
3063                                                 add_mon_lite(mon_fy + 3, mon_fx);
3064                                                 add_mon_lite(mon_fy + 3, mon_fx - 1);
3065                                         }
3066                                 }
3067
3068                                 /* North of the monster */
3069                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx, f_flag))
3070                                 {
3071                                         add_mon_lite(mon_fy - 2, mon_fx + 1);
3072                                         add_mon_lite(mon_fy - 2, mon_fx);
3073                                         add_mon_lite(mon_fy - 2, mon_fx - 1);
3074
3075                                         c_ptr = &cave[mon_fy - 2][mon_fx];
3076
3077                                         /* Radius 3 */
3078                                         if ((rad == 3) && cave_have_flag_grid(c_ptr, f_flag))
3079                                         {
3080                                                 add_mon_lite(mon_fy - 3, mon_fx + 1);
3081                                                 add_mon_lite(mon_fy - 3, mon_fx);
3082                                                 add_mon_lite(mon_fy - 3, mon_fx - 1);
3083                                         }
3084                                 }
3085
3086                                 /* East of the monster */
3087                                 if (cave_have_flag_bold(mon_fy, mon_fx + 1, f_flag))
3088                                 {
3089                                         add_mon_lite(mon_fy + 1, mon_fx + 2);
3090                                         add_mon_lite(mon_fy, mon_fx + 2);
3091                                         add_mon_lite(mon_fy - 1, mon_fx + 2);
3092
3093                                         c_ptr = &cave[mon_fy][mon_fx + 2];
3094
3095                                         /* Radius 3 */
3096                                         if ((rad == 3) && cave_have_flag_grid(c_ptr, f_flag))
3097                                         {
3098                                                 add_mon_lite(mon_fy + 1, mon_fx + 3);
3099                                                 add_mon_lite(mon_fy, mon_fx + 3);
3100                                                 add_mon_lite(mon_fy - 1, mon_fx + 3);
3101                                         }
3102                                 }
3103
3104                                 /* West of the monster */
3105                                 if (cave_have_flag_bold(mon_fy, mon_fx - 1, f_flag))
3106                                 {
3107                                         add_mon_lite(mon_fy + 1, mon_fx - 2);
3108                                         add_mon_lite(mon_fy, mon_fx - 2);
3109                                         add_mon_lite(mon_fy - 1, mon_fx - 2);
3110
3111                                         c_ptr = &cave[mon_fy][mon_fx - 2];
3112
3113                                         /* Radius 3 */
3114                                         if ((rad == 3) && cave_have_flag_grid(c_ptr, f_flag))
3115                                         {
3116                                                 add_mon_lite(mon_fy + 1, mon_fx - 3);
3117                                                 add_mon_lite(mon_fy, mon_fx - 3);
3118                                                 add_mon_lite(mon_fy - 1, mon_fx - 3);
3119                                         }
3120                                 }
3121                         }
3122
3123                         /* Radius 3 */
3124                         if (rad == 3)
3125                         {
3126                                 /* South-East of the monster */
3127                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx + 1, f_flag))
3128                                 {
3129                                         add_mon_lite(mon_fy + 2, mon_fx + 2);
3130                                 }
3131
3132                                 /* South-West of the monster */
3133                                 if (cave_have_flag_bold(mon_fy + 1, mon_fx - 1, f_flag))
3134                                 {
3135                                         add_mon_lite(mon_fy + 2, mon_fx - 2);
3136                                 }
3137
3138                                 /* North-East of the monster */
3139                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx + 1, f_flag))
3140                                 {
3141                                         add_mon_lite(mon_fy - 2, mon_fx + 2);
3142                                 }
3143
3144                                 /* North-West of the monster */
3145                                 if (cave_have_flag_bold(mon_fy - 1, mon_fx - 1, f_flag))
3146                                 {
3147                                         add_mon_lite(mon_fy - 2, mon_fx - 2);
3148                                 }
3149                         }
3150                 }
3151         }
3152
3153         /* Save end of list of new squares */
3154         end_temp = temp_n;
3155
3156         /*
3157          * Look at old set flags to see if there are any changes.
3158          */
3159         for (i = 0; i < mon_lite_n; i++)
3160         {
3161                 fx = mon_lite_x[i];
3162                 fy = mon_lite_y[i];
3163
3164                 /* We trust this grid is in bounds */
3165
3166                 /* Point to grid */
3167                 c_ptr = &cave[fy][fx];
3168
3169                 if (c_ptr->info & CAVE_TEMP) /* Pervious lit */
3170                 {
3171                         /* It it no longer lit? */
3172                         if ((c_ptr->info & (CAVE_VIEW | CAVE_MNLT)) == CAVE_VIEW)
3173                         {
3174                                 /* It is now unlit */
3175                                 /* Add it to later visual update */
3176                                 cave_note_and_redraw_later(c_ptr, fy, fx);
3177                         }
3178                 }
3179                 else /* Pervious darkened */
3180                 {
3181                         /* It it no longer darken? */
3182                         if ((c_ptr->info & (CAVE_VIEW | CAVE_MNDK)) == CAVE_VIEW)
3183                         {
3184                                 /* It is now undarken */
3185                                 /* Add it to later visual update */
3186                                 cave_note_and_redraw_later(c_ptr, fy, fx);
3187                         }
3188                 }
3189
3190                 /* Add to end of temp array */
3191                 temp_x[temp_n] = fx;
3192                 temp_y[temp_n] = fy;
3193                 temp_n++;
3194         }
3195
3196         /* Clear the lite array */
3197         mon_lite_n = 0;
3198
3199         /* Copy the temp array into the lit array lighting the new squares. */
3200         for (i = 0; i < end_temp; i++)
3201         {
3202                 fx = temp_x[i];
3203                 fy = temp_y[i];
3204
3205                 /* We trust this grid is in bounds */
3206
3207                 /* Point to grid */
3208                 c_ptr = &cave[fy][fx];
3209
3210                 if (c_ptr->info & CAVE_MNLT) /* Lit */
3211                 {
3212                         /* The is the square newly lit and visible? */
3213                         if ((c_ptr->info & (CAVE_VIEW | CAVE_TEMP)) == CAVE_VIEW)
3214                         {
3215                                 /* It is now lit */
3216                                 /* Add it to later visual update */
3217                                 cave_note_and_redraw_later(c_ptr, fy, fx);
3218                         }
3219                 }
3220                 else /* Darkened */
3221                 {
3222                         /* The is the square newly darkened and visible? */
3223                         if ((c_ptr->info & (CAVE_VIEW | CAVE_XTRA)) == CAVE_VIEW)
3224                         {
3225                                 /* It is now darkened */
3226                                 /* Add it to later visual update */
3227                                 cave_note_and_redraw_later(c_ptr, fy, fx);
3228                         }
3229                 }
3230
3231                 /* Save in the monster lit or darkened array */
3232                 mon_lite_x[mon_lite_n] = fx;
3233                 mon_lite_y[mon_lite_n] = fy;
3234                 mon_lite_n++;
3235         }
3236
3237         /* Clear the temp flag for the old lit or darken grids */
3238         for (i = end_temp; i < temp_n; i++)
3239         {
3240                 /* We trust this grid is in bounds */
3241
3242                 cave[temp_y[i]][temp_x[i]].info &= ~(CAVE_TEMP | CAVE_XTRA);
3243         }
3244
3245         /* Finished with temp_n */
3246         temp_n = 0;
3247
3248         /* Mega-Hack -- Visual update later */
3249         p_ptr->update |= (PU_DELAY_VIS);
3250
3251         p_ptr->monlite = (cave[p_ptr->y][p_ptr->x].info & CAVE_MNLT) ? TRUE : FALSE;
3252
3253         if (p_ptr->special_defense & NINJA_S_STEALTH)
3254         {
3255                 if (p_ptr->old_monlite != p_ptr->monlite)
3256                 {
3257                         if (p_ptr->monlite)
3258                         {
3259                                 msg_print(_("影の覆いが薄れた気がする。", "Your mantle of shadow become thin."));
3260                         }
3261                         else
3262                         {
3263                                 msg_print(_("影の覆いが濃くなった!", "Your mantle of shadow restored its original darkness."));
3264                         }
3265                 }
3266         }
3267         p_ptr->old_monlite = p_ptr->monlite;
3268 }
3269
3270 void clear_mon_lite(void)
3271 {
3272         int i;
3273         cave_type *c_ptr;
3274
3275         /* Clear all monster lit squares */
3276         for (i = 0; i < mon_lite_n; i++)
3277         {
3278                 /* Point to grid */
3279                 c_ptr = &cave[mon_lite_y[i]][mon_lite_x[i]];
3280
3281                 /* Clear monster illumination flag */
3282                 c_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
3283         }
3284
3285         /* Empty the array */
3286         mon_lite_n = 0;
3287 }
3288
3289
3290
3291 /*
3292  * Clear the viewable space
3293  */
3294 void forget_view(void)
3295 {
3296         int i;
3297
3298         cave_type *c_ptr;
3299
3300         /* None to forget */
3301         if (!view_n) return;
3302
3303         /* Clear them all */
3304         for (i = 0; i < view_n; i++)
3305         {
3306                 POSITION y = view_y[i];
3307                 POSITION x = view_x[i];
3308                 c_ptr = &cave[y][x];
3309
3310                 /* Forget that the grid is viewable */
3311                 c_ptr->info &= ~(CAVE_VIEW);
3312
3313                 /* if (!panel_contains(y, x)) continue; */
3314
3315                 /* Update the screen */
3316                 /* lite_spot(y, x); Perhaps don't need? */
3317         }
3318
3319         /* None left */
3320         view_n = 0;
3321 }
3322
3323
3324
3325 /*
3326  * This macro allows us to efficiently add a grid to the "view" array,
3327  * note that we are never called for illegal grids, or for grids which
3328  * have already been placed into the "view" array, and we are never
3329  * called when the "view" array is full.
3330  */
3331 #define cave_view_hack(C,Y,X) \
3332 {\
3333     if (!((C)->info & (CAVE_VIEW))){\
3334     (C)->info |= (CAVE_VIEW); \
3335     view_y[view_n] = (Y); \
3336     view_x[view_n] = (X); \
3337     view_n++;}\
3338 }
3339
3340
3341
3342 /*
3343  * Helper function for "update_view()" below
3344  *
3345  * We are checking the "viewability" of grid (y,x) by the player.
3346  *
3347  * This function assumes that (y,x) is legal (i.e. on the map).
3348  *
3349  * Grid (y1,x1) is on the "diagonal" between (p_ptr->y,p_ptr->x) and (y,x)
3350  * Grid (y2,x2) is "adjacent", also between (p_ptr->y,p_ptr->x) and (y,x).
3351  *
3352  * Note that we are using the "CAVE_XTRA" field for marking grids as
3353  * "easily viewable".  This bit is cleared at the end of "update_view()".
3354  *
3355  * This function adds (y,x) to the "viewable set" if necessary.
3356  *
3357  * This function now returns "TRUE" if vision is "blocked" by grid (y,x).
3358  */
3359 static bool update_view_aux(POSITION y, POSITION x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
3360 {
3361         bool f1, f2, v1, v2, z1, z2, wall;
3362
3363         cave_type *c_ptr;
3364
3365         cave_type *g1_c_ptr;
3366         cave_type *g2_c_ptr;
3367
3368         /* Access the grids */
3369         g1_c_ptr = &cave[y1][x1];
3370         g2_c_ptr = &cave[y2][x2];
3371
3372
3373         /* Check for walls */
3374         f1 = (cave_los_grid(g1_c_ptr));
3375         f2 = (cave_los_grid(g2_c_ptr));
3376
3377         /* Totally blocked by physical walls */
3378         if (!f1 && !f2) return (TRUE);
3379
3380
3381         /* Check for visibility */
3382         v1 = (f1 && (g1_c_ptr->info & (CAVE_VIEW)));
3383         v2 = (f2 && (g2_c_ptr->info & (CAVE_VIEW)));
3384
3385         /* Totally blocked by "unviewable neighbors" */
3386         if (!v1 && !v2) return (TRUE);
3387
3388         c_ptr = &cave[y][x];
3389
3390
3391         /* Check for walls */
3392         wall = (!cave_los_grid(c_ptr));
3393
3394
3395         /* Check the "ease" of visibility */
3396         z1 = (v1 && (g1_c_ptr->info & (CAVE_XTRA)));
3397         z2 = (v2 && (g2_c_ptr->info & (CAVE_XTRA)));
3398
3399         /* Hack -- "easy" plus "easy" yields "easy" */
3400         if (z1 && z2)
3401         {
3402                 c_ptr->info |= (CAVE_XTRA);
3403
3404                 cave_view_hack(c_ptr, y, x);
3405
3406                 return (wall);
3407         }
3408
3409         /* Hack -- primary "easy" yields "viewed" */
3410         if (z1)
3411         {
3412                 cave_view_hack(c_ptr, y, x);
3413
3414                 return (wall);
3415         }
3416
3417         /* Hack -- "view" plus "view" yields "view" */
3418         if (v1 && v2)
3419         {
3420                 /* c_ptr->info |= (CAVE_XTRA); */
3421
3422                 cave_view_hack(c_ptr, y, x);
3423
3424                 return (wall);
3425         }
3426
3427
3428         /* Mega-Hack -- the "los()" function works poorly on walls */
3429         if (wall)
3430         {
3431                 cave_view_hack(c_ptr, y, x);
3432
3433                 return (wall);
3434         }
3435
3436
3437         /* Hack -- check line of sight */
3438         if (los(p_ptr->y, p_ptr->x, y, x))
3439         {
3440                 cave_view_hack(c_ptr, y, x);
3441
3442                 return (wall);
3443         }
3444
3445
3446         /* Assume no line of sight. */
3447         return (TRUE);
3448 }
3449
3450
3451
3452 /*
3453  * Calculate the viewable space
3454  *
3455  *  1: Process the player
3456  *  1a: The player is always (easily) viewable
3457  *  2: Process the diagonals
3458  *  2a: The diagonals are (easily) viewable up to the first wall
3459  *  2b: But never go more than 2/3 of the "full" distance
3460  *  3: Process the main axes
3461  *  3a: The main axes are (easily) viewable up to the first wall
3462  *  3b: But never go more than the "full" distance
3463  *  4: Process sequential "strips" in each of the eight octants
3464  *  4a: Each strip runs along the previous strip
3465  *  4b: The main axes are "previous" to the first strip
3466  *  4c: Process both "sides" of each "direction" of each strip
3467  *  4c1: Each side aborts as soon as possible
3468  *  4c2: Each side tells the next strip how far it has to check
3469  *
3470  * Note that the octant processing involves some pretty interesting
3471  * observations involving when a grid might possibly be viewable from
3472  * a given grid, and on the order in which the strips are processed.
3473  *
3474  * Note the use of the mathematical facts shown below, which derive
3475  * from the fact that (1 < sqrt(2) < 1.5), and that the length of the
3476  * hypotenuse of a right triangle is primarily determined by the length
3477  * of the longest side, when one side is small, and is strictly less
3478  * than one-and-a-half times as long as the longest side when both of
3479  * the sides are large.
3480  *
3481  *   if (manhatten(dy,dx) < R) then (hypot(dy,dx) < R)
3482  *   if (manhatten(dy,dx) > R*3/2) then (hypot(dy,dx) > R)
3483  *
3484  *   hypot(dy,dx) is approximated by (dx+dy+MAX(dx,dy)) / 2
3485  *
3486  * These observations are important because the calculation of the actual
3487  * value of "hypot(dx,dy)" is extremely expensive, involving square roots,
3488  * while for small values (up to about 20 or so), the approximations above
3489  * are correct to within an error of at most one grid or so.
3490  *
3491  * Observe the use of "full" and "over" in the code below, and the use of
3492  * the specialized calculation involving "limit", all of which derive from
3493  * the observations given above.  Basically, we note that the "circle" of
3494  * view is completely contained in an "octagon" whose bounds are easy to
3495  * determine, and that only a few steps are needed to derive the actual
3496  * bounds of the circle given the bounds of the octagon.
3497  *
3498  * Note that by skipping all the grids in the corners of the octagon, we
3499  * place an upper limit on the number of grids in the field of view, given
3500  * that "full" is never more than 20.  Of the 1681 grids in the "square" of
3501  * view, only about 1475 of these are in the "octagon" of view, and even
3502  * fewer are in the "circle" of view, so 1500 or 1536 is more than enough
3503  * entries to completely contain the actual field of view.
3504  *
3505  * Note also the care taken to prevent "running off the map".  The use of
3506  * explicit checks on the "validity" of the "diagonal", and the fact that
3507  * the loops are never allowed to "leave" the map, lets "update_view_aux()"
3508  * use the optimized "cave_los_bold()" macro, and to avoid the overhead
3509  * of multiple checks on the validity of grids.
3510  *
3511  * Note the "optimizations" involving the "se","sw","ne","nw","es","en",
3512  * "ws","wn" variables.  They work like this: While travelling down the
3513  * south-bound strip just to the east of the main south axis, as soon as
3514  * we get to a grid which does not "transmit" viewing, if all of the strips
3515  * preceding us (in this case, just the main axis) had terminated at or before
3516  * the same point, then we can stop, and reset the "max distance" to ourself.
3517  * So, each strip (named by major axis plus offset, thus "se" in this case)
3518  * maintains a "blockage" variable, initialized during the main axis step,
3519  * and checks it whenever a blockage is observed.  After processing each
3520  * strip as far as the previous strip told us to process, the next strip is
3521  * told not to go farther than the current strip's farthest viewable grid,
3522  * unless open space is still available.  This uses the "k" variable.
3523  *
3524  * Note the use of "inline" macros for efficiency.  The "cave_los_grid()"
3525  * macro is a replacement for "cave_los_bold()" which takes a pointer to
3526  * a cave grid instead of its location.  The "cave_view_hack()" macro is a
3527  * chunk of code which adds the given location to the "view" array if it
3528  * is not already there, using both the actual location and a pointer to
3529  * the cave grid.  See above.
3530  *
3531  * By the way, the purpose of this code is to reduce the dependancy on the
3532  * "los()" function which is slow, and, in some cases, not very accurate.
3533  *
3534  * It is very possible that I am the only person who fully understands this
3535  * function, and for that I am truly sorry, but efficiency was very important
3536  * and the "simple" version of this function was just not fast enough.  I am
3537  * more than willing to replace this function with a simpler one, if it is
3538  * equally efficient, and especially willing if the new function happens to
3539  * derive "reverse-line-of-sight" at the same time, since currently monsters
3540  * just use an optimized hack of "you see me, so I see you", and then use the
3541  * actual "projectable()" function to check spell attacks.
3542  */
3543 void update_view(void)
3544 {
3545         int n, m, d, k, z;
3546         POSITION y, x;
3547
3548         int se, sw, ne, nw, es, en, ws, wn;
3549
3550         int full, over;
3551
3552         POSITION y_max = cur_hgt - 1;
3553         POSITION x_max = cur_wid - 1;
3554
3555         cave_type *c_ptr;
3556
3557         /*** Initialize ***/
3558
3559         /* Optimize */
3560         if (view_reduce_view && !dun_level)
3561         {
3562                 /* Full radius (10) */
3563                 full = MAX_SIGHT / 2;
3564
3565                 /* Octagon factor (15) */
3566                 over = MAX_SIGHT * 3 / 4;
3567         }
3568
3569         /* Normal */
3570         else
3571         {
3572                 /* Full radius (20) */
3573                 full = MAX_SIGHT;
3574
3575                 /* Octagon factor (30) */
3576                 over = MAX_SIGHT * 3 / 2;
3577         }
3578
3579
3580         /*** Step 0 -- Begin ***/
3581
3582         /* Save the old "view" grids for later */
3583         for (n = 0; n < view_n; n++)
3584         {
3585                 y = view_y[n];
3586                 x = view_x[n];
3587                 c_ptr = &cave[y][x];
3588
3589                 /* Mark the grid as not in "view" */
3590                 c_ptr->info &= ~(CAVE_VIEW);
3591
3592                 /* Mark the grid as "seen" */
3593                 c_ptr->info |= (CAVE_TEMP);
3594
3595                 /* Add it to the "seen" set */
3596                 temp_y[temp_n] = y;
3597                 temp_x[temp_n] = x;
3598                 temp_n++;
3599         }
3600
3601         /* Start over with the "view" array */
3602         view_n = 0;
3603
3604         /*** Step 1 -- adjacent grids ***/
3605
3606         /* Now start on the player */
3607         y = p_ptr->y;
3608         x = p_ptr->x;
3609         c_ptr = &cave[y][x];
3610
3611         /* Assume the player grid is easily viewable */
3612         c_ptr->info |= (CAVE_XTRA);
3613
3614         /* Assume the player grid is viewable */
3615         cave_view_hack(c_ptr, y, x);
3616
3617
3618         /*** Step 2 -- Major Diagonals ***/
3619
3620         /* Hack -- Limit */
3621         z = full * 2 / 3;
3622
3623         /* Scan south-east */
3624         for (d = 1; d <= z; d++)
3625         {
3626                 c_ptr = &cave[y+d][x+d];
3627                 c_ptr->info |= (CAVE_XTRA);
3628                 cave_view_hack(c_ptr, y+d, x+d);
3629                 if (!cave_los_grid(c_ptr)) break;
3630         }
3631
3632         /* Scan south-west */
3633         for (d = 1; d <= z; d++)
3634         {
3635                 c_ptr = &cave[y+d][x-d];
3636                 c_ptr->info |= (CAVE_XTRA);
3637                 cave_view_hack(c_ptr, y+d, x-d);
3638                 if (!cave_los_grid(c_ptr)) break;
3639         }
3640
3641         /* Scan north-east */
3642         for (d = 1; d <= z; d++)
3643         {
3644                 c_ptr = &cave[y-d][x+d];
3645                 c_ptr->info |= (CAVE_XTRA);
3646                 cave_view_hack(c_ptr, y-d, x+d);
3647                 if (!cave_los_grid(c_ptr)) break;
3648         }
3649
3650         /* Scan north-west */
3651         for (d = 1; d <= z; d++)
3652         {
3653                 c_ptr = &cave[y-d][x-d];
3654                 c_ptr->info |= (CAVE_XTRA);
3655                 cave_view_hack(c_ptr, y-d, x-d);
3656                 if (!cave_los_grid(c_ptr)) break;
3657         }
3658
3659
3660         /*** Step 3 -- major axes ***/
3661
3662         /* Scan south */
3663         for (d = 1; d <= full; d++)
3664         {
3665                 c_ptr = &cave[y+d][x];
3666                 c_ptr->info |= (CAVE_XTRA);
3667                 cave_view_hack(c_ptr, y+d, x);
3668                 if (!cave_los_grid(c_ptr)) break;
3669         }
3670
3671         /* Initialize the "south strips" */
3672         se = sw = d;
3673
3674         /* Scan north */
3675         for (d = 1; d <= full; d++)
3676         {
3677                 c_ptr = &cave[y-d][x];
3678                 c_ptr->info |= (CAVE_XTRA);
3679                 cave_view_hack(c_ptr, y-d, x);
3680                 if (!cave_los_grid(c_ptr)) break;
3681         }
3682
3683         /* Initialize the "north strips" */
3684         ne = nw = d;
3685
3686         /* Scan east */
3687         for (d = 1; d <= full; d++)
3688         {
3689                 c_ptr = &cave[y][x+d];
3690                 c_ptr->info |= (CAVE_XTRA);
3691                 cave_view_hack(c_ptr, y, x+d);
3692                 if (!cave_los_grid(c_ptr)) break;
3693         }
3694
3695         /* Initialize the "east strips" */
3696         es = en = d;
3697
3698         /* Scan west */
3699         for (d = 1; d <= full; d++)
3700         {
3701                 c_ptr = &cave[y][x-d];
3702                 c_ptr->info |= (CAVE_XTRA);
3703                 cave_view_hack(c_ptr, y, x-d);
3704                 if (!cave_los_grid(c_ptr)) break;
3705         }
3706
3707         /* Initialize the "west strips" */
3708         ws = wn = d;
3709
3710
3711         /*** Step 4 -- Divide each "octant" into "strips" ***/
3712
3713         /* Now check each "diagonal" (in parallel) */
3714         for (n = 1; n <= over / 2; n++)
3715         {
3716                 int ypn, ymn, xpn, xmn;
3717
3718
3719                 /* Acquire the "bounds" of the maximal circle */
3720                 z = over - n - n;
3721                 if (z > full - n) z = full - n;
3722                 while ((z + n + (n>>1)) > full) z--;
3723
3724
3725                 /* Access the four diagonal grids */
3726                 ypn = y + n;
3727                 ymn = y - n;
3728                 xpn = x + n;
3729                 xmn = x - n;
3730
3731
3732                 /* South strip */
3733                 if (ypn < y_max)
3734                 {
3735                         /* Maximum distance */
3736                         m = MIN(z, y_max - ypn);
3737
3738                         /* East side */
3739                         if ((xpn <= x_max) && (n < se))
3740                         {
3741                                 /* Scan */
3742                                 for (k = n, d = 1; d <= m; d++)
3743                                 {
3744                                         /* Check grid "d" in strip "n", notice "blockage" */
3745                                         if (update_view_aux(ypn+d, xpn, ypn+d-1, xpn-1, ypn+d-1, xpn))
3746                                         {
3747                                                 if (n + d >= se) break;
3748                                         }
3749
3750                                         /* Track most distant "non-blockage" */
3751                                         else
3752                                         {
3753                                                 k = n + d;
3754                                         }
3755                                 }
3756
3757                                 /* Limit the next strip */
3758                                 se = k + 1;
3759                         }
3760
3761                         /* West side */
3762                         if ((xmn >= 0) && (n < sw))
3763                         {
3764                                 /* Scan */
3765                                 for (k = n, d = 1; d <= m; d++)
3766                                 {
3767                                         /* Check grid "d" in strip "n", notice "blockage" */
3768                                         if (update_view_aux(ypn+d, xmn, ypn+d-1, xmn+1, ypn+d-1, xmn))
3769                                         {
3770                                                 if (n + d >= sw) break;
3771                                         }
3772
3773                                         /* Track most distant "non-blockage" */
3774                                         else
3775                                         {
3776                                                 k = n + d;
3777                                         }
3778                                 }
3779
3780                                 /* Limit the next strip */
3781                                 sw = k + 1;
3782                         }
3783                 }
3784
3785
3786                 /* North strip */
3787                 if (ymn > 0)
3788                 {
3789                         /* Maximum distance */
3790                         m = MIN(z, ymn);
3791
3792                         /* East side */
3793                         if ((xpn <= x_max) && (n < ne))
3794                         {
3795                                 /* Scan */
3796                                 for (k = n, d = 1; d <= m; d++)
3797                                 {
3798                                         /* Check grid "d" in strip "n", notice "blockage" */
3799                                         if (update_view_aux(ymn-d, xpn, ymn-d+1, xpn-1, ymn-d+1, xpn))
3800                                         {
3801                                                 if (n + d >= ne) break;
3802                                         }
3803
3804                                         /* Track most distant "non-blockage" */
3805                                         else
3806                                         {
3807                                                 k = n + d;
3808                                         }
3809                                 }
3810
3811                                 /* Limit the next strip */
3812                                 ne = k + 1;
3813                         }
3814
3815                         /* West side */
3816                         if ((xmn >= 0) && (n < nw))
3817                         {
3818                                 /* Scan */
3819                                 for (k = n, d = 1; d <= m; d++)
3820                                 {
3821                                         /* Check grid "d" in strip "n", notice "blockage" */
3822                                         if (update_view_aux(ymn-d, xmn, ymn-d+1, xmn+1, ymn-d+1, xmn))
3823                                         {
3824                                                 if (n + d >= nw) break;
3825                                         }
3826
3827                                         /* Track most distant "non-blockage" */
3828                                         else
3829                                         {
3830                                                 k = n + d;
3831                                         }
3832                                 }
3833
3834                                 /* Limit the next strip */
3835                                 nw = k + 1;
3836                         }
3837                 }
3838
3839
3840                 /* East strip */
3841                 if (xpn < x_max)
3842                 {
3843                         /* Maximum distance */
3844                         m = MIN(z, x_max - xpn);
3845
3846                         /* South side */
3847                         if ((ypn <= x_max) && (n < es))
3848                         {
3849                                 /* Scan */
3850                                 for (k = n, d = 1; d <= m; d++)
3851                                 {
3852                                         /* Check grid "d" in strip "n", notice "blockage" */
3853                                         if (update_view_aux(ypn, xpn+d, ypn-1, xpn+d-1, ypn, xpn+d-1))
3854                                         {
3855                                                 if (n + d >= es) break;
3856                                         }
3857
3858                                         /* Track most distant "non-blockage" */
3859                                         else
3860                                         {
3861                                                 k = n + d;
3862                                         }
3863                                 }
3864
3865                                 /* Limit the next strip */
3866                                 es = k + 1;
3867                         }
3868
3869                         /* North side */
3870                         if ((ymn >= 0) && (n < en))
3871                         {
3872                                 /* Scan */
3873                                 for (k = n, d = 1; d <= m; d++)
3874                                 {
3875                                         /* Check grid "d" in strip "n", notice "blockage" */
3876                                         if (update_view_aux(ymn, xpn+d, ymn+1, xpn+d-1, ymn, xpn+d-1))
3877                                         {
3878                                                 if (n + d >= en) break;
3879                                         }
3880
3881                                         /* Track most distant "non-blockage" */
3882                                         else
3883                                         {
3884                                                 k = n + d;
3885                                         }
3886                                 }
3887
3888                                 /* Limit the next strip */
3889                                 en = k + 1;
3890                         }
3891                 }
3892
3893
3894                 /* West strip */
3895                 if (xmn > 0)
3896                 {
3897                         /* Maximum distance */
3898                         m = MIN(z, xmn);
3899
3900                         /* South side */
3901                         if ((ypn <= y_max) && (n < ws))
3902                         {
3903                                 /* Scan */
3904                                 for (k = n, d = 1; d <= m; d++)
3905                                 {
3906                                         /* Check grid "d" in strip "n", notice "blockage" */
3907                                         if (update_view_aux(ypn, xmn-d, ypn-1, xmn-d+1, ypn, xmn-d+1))
3908                                         {
3909                                                 if (n + d >= ws) break;
3910                                         }
3911
3912                                         /* Track most distant "non-blockage" */
3913                                         else
3914                                         {
3915                                                 k = n + d;
3916                                         }
3917                                 }
3918
3919                                 /* Limit the next strip */
3920                                 ws = k + 1;
3921                         }
3922
3923                         /* North side */
3924                         if ((ymn >= 0) && (n < wn))
3925                         {
3926                                 /* Scan */
3927                                 for (k = n, d = 1; d <= m; d++)
3928                                 {
3929                                         /* Check grid "d" in strip "n", notice "blockage" */
3930                                         if (update_view_aux(ymn, xmn-d, ymn+1, xmn-d+1, ymn, xmn-d+1))
3931                                         {
3932                                                 if (n + d >= wn) break;
3933                                         }
3934
3935                                         /* Track most distant "non-blockage" */
3936                                         else
3937                                         {
3938                                                 k = n + d;
3939                                         }
3940                                 }
3941
3942                                 /* Limit the next strip */
3943                                 wn = k + 1;
3944                         }
3945                 }
3946         }
3947
3948
3949         /*** Step 5 -- Complete the algorithm ***/
3950
3951         /* Update all the new grids */
3952         for (n = 0; n < view_n; n++)
3953         {
3954                 y = view_y[n];
3955                 x = view_x[n];
3956                 c_ptr = &cave[y][x];
3957
3958                 /* Clear the "CAVE_XTRA" flag */
3959                 c_ptr->info &= ~(CAVE_XTRA);
3960
3961                 /* Update only newly viewed grids */
3962                 if (c_ptr->info & (CAVE_TEMP)) continue;
3963
3964                 /* Add it to later visual update */
3965                 cave_note_and_redraw_later(c_ptr, y, x);
3966         }
3967
3968         /* Wipe the old grids, update as needed */
3969         for (n = 0; n < temp_n; n++)
3970         {
3971                 y = temp_y[n];
3972                 x = temp_x[n];
3973                 c_ptr = &cave[y][x];
3974
3975                 /* No longer in the array */
3976                 c_ptr->info &= ~(CAVE_TEMP);
3977
3978                 /* Update only non-viewable grids */
3979                 if (c_ptr->info & (CAVE_VIEW)) continue;
3980
3981                 /* Add it to later visual update */
3982                 cave_redraw_later(c_ptr, y, x);
3983         }
3984
3985         /* None left */
3986         temp_n = 0;
3987
3988         /* Mega-Hack -- Visual update later */
3989         p_ptr->update |= (PU_DELAY_VIS);
3990 }
3991
3992
3993 /*
3994  * Mega-Hack -- Delayed visual update
3995  * Only used if update_view(), update_lite() or update_mon_lite() was called
3996  */
3997 void delayed_visual_update(void)
3998 {
3999         int       i, y, x;
4000         cave_type *c_ptr;
4001
4002         /* Update needed grids */
4003         for (i = 0; i < redraw_n; i++)
4004         {
4005                 y = redraw_y[i];
4006                 x = redraw_x[i];
4007                 c_ptr = &cave[y][x];
4008
4009                 /* Update only needed grids (prevent multiple updating) */
4010                 if (!(c_ptr->info & CAVE_REDRAW)) continue;
4011
4012                 /* If required, note */
4013                 if (c_ptr->info & CAVE_NOTE) note_spot(y, x);
4014
4015                 lite_spot(y, x);
4016
4017                 /* Hack -- Visual update of monster on this grid */
4018                 if (c_ptr->m_idx) update_monster(c_ptr->m_idx, FALSE);
4019
4020                 /* No longer in the array */
4021                 c_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
4022         }
4023
4024         /* None left */
4025         redraw_n = 0;
4026 }
4027
4028
4029 /*
4030  * Hack -- forget the "flow" information
4031  */
4032 void forget_flow(void)
4033 {
4034         POSITION x, y;
4035
4036         /* Check the entire dungeon */
4037         for (y = 0; y < cur_hgt; y++)
4038         {
4039                 for (x = 0; x < cur_wid; x++)
4040                 {
4041                         /* Forget the old data */
4042                         cave[y][x].dist = 0;
4043                         cave[y][x].cost = 0;
4044                         cave[y][x].when = 0;
4045                 }
4046         }
4047 }
4048
4049
4050 /*
4051  * Hack - speed up the update_flow algorithm by only doing
4052  * it everytime the player moves out of LOS of the last
4053  * "way-point".
4054  */
4055 static POSITION flow_x = 0;
4056 static POSITION flow_y = 0;
4057
4058
4059
4060 /*
4061  * Hack -- fill in the "cost" field of every grid that the player
4062  * can "reach" with the number of steps needed to reach that grid.
4063  * This also yields the "distance" of the player from every grid.
4064  *
4065  * In addition, mark the "when" of the grids that can reach
4066  * the player with the incremented value of "flow_n".
4067  *
4068  * Hack -- use the "seen" array as a "circular queue".
4069  *
4070  * We do not need a priority queue because the cost from grid
4071  * to grid is always "one" and we process them in order.
4072  */
4073 void update_flow(void)
4074 {
4075         POSITION x, y, d;
4076         int flow_head = 1;
4077         int flow_tail = 0;
4078
4079         /* Paranoia -- make sure the array is empty */
4080         if (temp_n) return;
4081
4082         /* The last way-point is on the map */
4083         if (running && in_bounds(flow_y, flow_x))
4084         {
4085                 /* The way point is in sight - do not update.  (Speedup) */
4086                 if (cave[flow_y][flow_x].info & CAVE_VIEW) return;
4087         }
4088
4089         /* Erase all of the current flow information */
4090         for (y = 0; y < cur_hgt; y++)
4091         {
4092                 for (x = 0; x < cur_wid; x++)
4093                 {
4094                         cave[y][x].cost = 0;
4095                         cave[y][x].dist = 0;
4096                 }
4097         }
4098
4099         /* Save player position */
4100         flow_y = p_ptr->y;
4101         flow_x = p_ptr->x;
4102
4103         /* Add the player's grid to the queue */
4104         temp_y[0] = p_ptr->y;
4105         temp_x[0] = p_ptr->x;
4106
4107         /* Now process the queue */
4108         while (flow_head != flow_tail)
4109         {
4110                 int ty, tx;
4111
4112                 /* Extract the next entry */
4113                 ty = temp_y[flow_tail];
4114                 tx = temp_x[flow_tail];
4115
4116                 /* Forget that entry */
4117                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
4118
4119                 /* Add the "children" */
4120                 for (d = 0; d < 8; d++)
4121                 {
4122                         int old_head = flow_head;
4123                         byte_hack m = cave[ty][tx].cost + 1;
4124                         byte_hack n = cave[ty][tx].dist + 1;
4125                         cave_type *c_ptr;
4126
4127                         /* Child location */
4128                         y = ty + ddy_ddd[d];
4129                         x = tx + ddx_ddd[d];
4130
4131                         /* Ignore player's grid */
4132                         if (player_bold(y, x)) continue;
4133
4134                         c_ptr = &cave[y][x];
4135
4136                         if (is_closed_door(c_ptr->feat)) m += 3;
4137
4138                         /* Ignore "pre-stamped" entries */
4139                         if (c_ptr->dist != 0 && c_ptr->dist <= n && c_ptr->cost <= m) continue;
4140
4141                         /* Ignore "walls" and "rubble" */
4142                         if (!cave_have_flag_grid(c_ptr, FF_MOVE) && !is_closed_door(c_ptr->feat)) continue;
4143
4144                         /* Save the flow cost */
4145                         if (c_ptr->cost == 0 || c_ptr->cost > m) c_ptr->cost = m;
4146                         if (c_ptr->dist == 0 || c_ptr->dist > n) c_ptr->dist = n;
4147
4148                         /* Hack -- limit flow depth */
4149                         if (n == MONSTER_FLOW_DEPTH) continue;
4150
4151                         /* Enqueue that entry */
4152                         temp_y[flow_head] = y;
4153                         temp_x[flow_head] = x;
4154
4155                         /* Advance the queue */
4156                         if (++flow_head == TEMP_MAX) flow_head = 0;
4157
4158                         /* Hack -- notice overflow by forgetting new entry */
4159                         if (flow_head == flow_tail) flow_head = old_head;
4160                 }
4161         }
4162 }
4163
4164
4165 static int scent_when = 0;
4166
4167 /*
4168  * Characters leave scent trails for perceptive monsters to track.
4169  *
4170  * Smell is rather more limited than sound.  Many creatures cannot use 
4171  * it at all, it doesn't extend very far outwards from the character's 
4172  * current position, and monsters can use it to home in the character, 
4173  * but not to run away from him.
4174  *
4175  * Smell is valued according to age.  When a character takes his turn, 
4176  * scent is aged by one, and new scent of the current age is laid down.  
4177  * Speedy characters leave more scent, true, but it also ages faster, 
4178  * which makes it harder to hunt them down.
4179  *
4180  * Whenever the age count loops, most of the scent trail is erased and 
4181  * the age of the remainder is recalculated.
4182  */
4183 void update_smell(void)
4184 {
4185         POSITION i, j;
4186         POSITION y, x;
4187
4188         /* Create a table that controls the spread of scent */
4189         const int scent_adjust[5][5] = 
4190         {
4191                 { -1, 0, 0, 0,-1 },
4192                 {  0, 1, 1, 1, 0 },
4193                 {  0, 1, 2, 1, 0 },
4194                 {  0, 1, 1, 1, 0 },
4195                 { -1, 0, 0, 0,-1 },
4196         };
4197
4198         /* Loop the age and adjust scent values when necessary */
4199         if (++scent_when == 254)
4200         {
4201                 /* Scan the entire dungeon */
4202                 for (y = 0; y < cur_hgt; y++)
4203                 {
4204                         for (x = 0; x < cur_wid; x++)
4205                         {
4206                                 int w = cave[y][x].when;
4207                                 cave[y][x].when = (w > 128) ? (w - 128) : 0;
4208                         }
4209                 }
4210
4211                 /* Restart */
4212                 scent_when = 126;
4213         }
4214
4215
4216         /* Lay down new scent */
4217         for (i = 0; i < 5; i++)
4218         {
4219                 for (j = 0; j < 5; j++)
4220                 {
4221                         cave_type *c_ptr;
4222
4223                         /* Translate table to map grids */
4224                         y = i + p_ptr->y - 2;
4225                         x = j + p_ptr->x - 2;
4226
4227                         /* Check Bounds */
4228                         if (!in_bounds(y, x)) continue;
4229
4230                         c_ptr = &cave[y][x];
4231
4232                         /* Walls, water, and lava cannot hold scent. */
4233                         if (!cave_have_flag_grid(c_ptr, FF_MOVE) && !is_closed_door(c_ptr->feat)) continue;
4234
4235                         /* Grid must not be blocked by walls from the character */
4236                         if (!player_has_los_bold(y, x)) continue;
4237
4238                         /* Note grids that are too far away */
4239                         if (scent_adjust[i][j] == -1) continue;
4240
4241                         /* Mark the grid with new scent */
4242                         c_ptr->when = scent_when + scent_adjust[i][j];
4243                 }
4244         }
4245 }
4246
4247
4248 /*
4249  * Hack -- map the current panel (plus some) ala "magic mapping"
4250  */
4251 void map_area(POSITION range)
4252 {
4253         int i;
4254         POSITION x, y;
4255         cave_type *c_ptr;
4256         FEAT_IDX feat;
4257         feature_type *f_ptr;
4258
4259         if (d_info[dungeon_type].flags1 & DF1_DARKNESS) range /= 3;
4260
4261         /* Scan that area */
4262         for (y = 1; y < cur_hgt - 1; y++)
4263         {
4264                 for (x = 1; x < cur_wid - 1; x++)
4265                 {
4266                         if (distance(p_ptr->y, p_ptr->x, y, x) > range) continue;
4267
4268                         c_ptr = &cave[y][x];
4269
4270                         /* Memorize terrain of the grid */
4271                         c_ptr->info |= (CAVE_KNOWN);
4272
4273                         /* Feature code (applying "mimic" field) */
4274                         feat = get_feat_mimic(c_ptr);
4275                         f_ptr = &f_info[feat];
4276
4277                         /* All non-walls are "checked" */
4278                         if (!have_flag(f_ptr->flags, FF_WALL))
4279                         {
4280                                 /* Memorize normal features */
4281                                 if (have_flag(f_ptr->flags, FF_REMEMBER))
4282                                 {
4283                                         /* Memorize the object */
4284                                         c_ptr->info |= (CAVE_MARK);
4285                                 }
4286
4287                                 /* Memorize known walls */
4288                                 for (i = 0; i < 8; i++)
4289                                 {
4290                                         c_ptr = &cave[y + ddy_ddd[i]][x + ddx_ddd[i]];
4291
4292                                         /* Feature code (applying "mimic" field) */
4293                                         feat = get_feat_mimic(c_ptr);
4294                                         f_ptr = &f_info[feat];
4295
4296                                         /* Memorize walls (etc) */
4297                                         if (have_flag(f_ptr->flags, FF_REMEMBER))
4298                                         {
4299                                                 /* Memorize the walls */
4300                                                 c_ptr->info |= (CAVE_MARK);
4301                                         }
4302                                 }
4303                         }
4304                 }
4305         }
4306
4307         p_ptr->redraw |= (PR_MAP);
4308
4309         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4310 }
4311
4312
4313
4314 /*
4315  * Light up the dungeon using "clairvoyance"
4316  *
4317  * This function "illuminates" every grid in the dungeon, memorizes all
4318  * "objects", memorizes all grids as with magic mapping, and, under the
4319  * standard option settings (view_perma_grids but not view_torch_grids)
4320  * memorizes all floor grids too.
4321  *
4322  * Note that if "view_perma_grids" is not set, we do not memorize floor
4323  * grids, since this would defeat the purpose of "view_perma_grids", not
4324  * that anyone seems to play without this option.
4325  *
4326  * Note that if "view_torch_grids" is set, we do not memorize floor grids,
4327  * since this would prevent the use of "view_torch_grids" as a method to
4328  * keep track of what grids have been observed directly.
4329  */
4330 void wiz_lite(bool ninja)
4331 {
4332         OBJECT_IDX i;
4333         POSITION y, x;
4334         FEAT_IDX feat;
4335         feature_type *f_ptr;
4336
4337         /* Memorize objects */
4338         for (i = 1; i < o_max; i++)
4339         {
4340                 object_type *o_ptr = &o_list[i];
4341
4342                 /* Skip dead objects */
4343                 if (!o_ptr->k_idx) continue;
4344
4345                 /* Skip held objects */
4346                 if (o_ptr->held_m_idx) continue;
4347
4348                 /* Memorize */
4349                 o_ptr->marked |= OM_FOUND;
4350         }
4351
4352         /* Scan all normal grids */
4353         for (y = 1; y < cur_hgt - 1; y++)
4354         {
4355                 /* Scan all normal grids */
4356                 for (x = 1; x < cur_wid - 1; x++)
4357                 {
4358                         cave_type *c_ptr = &cave[y][x];
4359
4360                         /* Memorize terrain of the grid */
4361                         c_ptr->info |= (CAVE_KNOWN);
4362
4363                         /* Feature code (applying "mimic" field) */
4364                         feat = get_feat_mimic(c_ptr);
4365                         f_ptr = &f_info[feat];
4366
4367                         /* Process all non-walls */
4368                         if (!have_flag(f_ptr->flags, FF_WALL))
4369                         {
4370                                 /* Scan all neighbors */
4371                                 for (i = 0; i < 9; i++)
4372                                 {
4373                                         POSITION yy = y + ddy_ddd[i];
4374                                         POSITION xx = x + ddx_ddd[i];
4375
4376                                         /* Get the grid */
4377                                         c_ptr = &cave[yy][xx];
4378
4379                                         /* Feature code (applying "mimic" field) */
4380                                         f_ptr = &f_info[get_feat_mimic(c_ptr)];
4381
4382                                         /* Perma-lite the grid */
4383                                         if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS) && !ninja)
4384                                         {
4385                                                 c_ptr->info |= (CAVE_GLOW);
4386                                         }
4387
4388                                         /* Memorize normal features */
4389                                         if (have_flag(f_ptr->flags, FF_REMEMBER))
4390                                         {
4391                                                 /* Memorize the grid */
4392                                                 c_ptr->info |= (CAVE_MARK);
4393                                         }
4394
4395                                         /* Perma-lit grids (newly and previously) */
4396                                         else if (c_ptr->info & CAVE_GLOW)
4397                                         {
4398                                                 /* Normally, memorize floors (see above) */
4399                                                 if (view_perma_grids && !view_torch_grids)
4400                                                 {
4401                                                         /* Memorize the grid */
4402                                                         c_ptr->info |= (CAVE_MARK);
4403                                                 }
4404                                         }
4405                                 }
4406                         }
4407                 }
4408         }
4409
4410         p_ptr->update |= (PU_MONSTERS);
4411         p_ptr->redraw |= (PR_MAP);
4412         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4413
4414         if (p_ptr->special_defense & NINJA_S_STEALTH)
4415         {
4416                 if (cave[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
4417         }
4418 }
4419
4420
4421 /*
4422  * Forget the dungeon map (ala "Thinking of Maud...").
4423  */
4424 void wiz_dark(void)
4425 {
4426         OBJECT_IDX i;
4427         POSITION y, x;
4428
4429         /* Forget every grid */
4430         for (y = 1; y < cur_hgt - 1; y++)
4431         {
4432                 for (x = 1; x < cur_wid - 1; x++)
4433                 {
4434                         cave_type *c_ptr = &cave[y][x];
4435
4436                         /* Process the grid */
4437                         c_ptr->info &= ~(CAVE_MARK | CAVE_IN_DETECT | CAVE_KNOWN);
4438                         c_ptr->info |= (CAVE_UNSAFE);
4439                 }
4440         }
4441
4442         /* Forget every grid on horizontal edge */
4443         for (x = 0; x < cur_wid; x++)
4444         {
4445                 cave[0][x].info &= ~(CAVE_MARK);
4446                 cave[cur_hgt - 1][x].info &= ~(CAVE_MARK);
4447         }
4448
4449         /* Forget every grid on vertical edge */
4450         for (y = 1; y < (cur_hgt - 1); y++)
4451         {
4452                 cave[y][0].info &= ~(CAVE_MARK);
4453                 cave[y][cur_wid - 1].info &= ~(CAVE_MARK);
4454         }
4455
4456         /* Forget all objects */
4457         for (i = 1; i < o_max; i++)
4458         {
4459                 object_type *o_ptr = &o_list[i];
4460
4461                 /* Skip dead objects */
4462                 if (!o_ptr->k_idx) continue;
4463
4464                 /* Skip held objects */
4465                 if (o_ptr->held_m_idx) continue;
4466
4467                 /* Forget the object */
4468                 o_ptr->marked &= OM_TOUCHED;
4469         }
4470
4471         /* Forget travel route when we have forgotten map */
4472         forget_travel_flow();
4473
4474         p_ptr->update |= (PU_UN_VIEW | PU_UN_LITE);
4475         p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
4476         p_ptr->update |= (PU_MONSTERS);
4477         p_ptr->redraw |= (PR_MAP);
4478         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4479 }
4480
4481 /*
4482  * Change the "feat" flag for a grid, and notice/redraw the grid
4483  */
4484 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
4485 {
4486         cave_type *c_ptr = &cave[y][x];
4487         feature_type *f_ptr = &f_info[feat];
4488         bool old_los, old_mirror;
4489
4490         if (!character_dungeon)
4491         {
4492                 /* Clear mimic type */
4493                 c_ptr->mimic = 0;
4494
4495                 /* Change the feature */
4496                 c_ptr->feat = feat;
4497
4498                 /* Hack -- glow the GLOW terrain */
4499                 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
4500                 {
4501                         DIRECTION i;
4502                         POSITION yy, xx;
4503
4504                         for (i = 0; i < 9; i++)
4505                         {
4506                                 yy = y + ddy_ddd[i];
4507                                 xx = x + ddx_ddd[i];
4508                                 if (!in_bounds2(yy, xx)) continue;
4509                                 cave[yy][xx].info |= CAVE_GLOW;
4510                         }
4511                 }
4512
4513                 return;
4514         }
4515
4516         old_los = cave_have_flag_bold(y, x, FF_LOS);
4517         old_mirror = is_mirror_grid(c_ptr);
4518
4519         /* Clear mimic type */
4520         c_ptr->mimic = 0;
4521
4522         /* Change the feature */
4523         c_ptr->feat = feat;
4524
4525         /* Remove flag for mirror/glyph */
4526         c_ptr->info &= ~(CAVE_OBJECT);
4527
4528         if (old_mirror && (d_info[dungeon_type].flags1 & DF1_DARKNESS))
4529         {
4530                 c_ptr->info &= ~(CAVE_GLOW);
4531                 if (!view_torch_grids) c_ptr->info &= ~(CAVE_MARK);
4532
4533                 update_local_illumination(y, x);
4534         }
4535
4536         /* Check for change to boring grid */
4537         if (!have_flag(f_ptr->flags, FF_REMEMBER)) c_ptr->info &= ~(CAVE_MARK);
4538         if (c_ptr->m_idx) update_monster(c_ptr->m_idx, FALSE);
4539
4540         note_spot(y, x);
4541
4542         lite_spot(y, x);
4543
4544         /* Check if los has changed */
4545         if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
4546         {
4547
4548 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
4549
4550                 update_local_illumination(y, x);
4551
4552 #endif /* COMPLEX_WALL_ILLUMINATION */
4553
4554                 /* Update the visuals */
4555                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
4556         }
4557
4558         /* Hack -- glow the GLOW terrain */
4559         if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
4560         {
4561                 DIRECTION i;
4562                 POSITION yy, xx;
4563                 cave_type *cc_ptr;
4564
4565                 for (i = 0; i < 9; i++)
4566                 {
4567                         yy = y + ddy_ddd[i];
4568                         xx = x + ddx_ddd[i];
4569                         if (!in_bounds2(yy, xx)) continue;
4570                         cc_ptr = &cave[yy][xx];
4571                         cc_ptr->info |= CAVE_GLOW;
4572
4573                         if (player_has_los_grid(cc_ptr))
4574                         {
4575                                 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
4576
4577                                 note_spot(yy, xx);
4578
4579                                 lite_spot(yy, xx);
4580                         }
4581
4582                         update_local_illumination(yy, xx);
4583                 }
4584
4585                 if (p_ptr->special_defense & NINJA_S_STEALTH)
4586                 {
4587                         if (cave[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
4588                 }
4589         }
4590 }
4591
4592
4593 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
4594 {
4595         feature_type *f_ptr = &f_info[newfeat];
4596
4597         if (have_flag(f_ptr->flags, FF_CONVERT))
4598         {
4599                 switch (f_ptr->subtype)
4600                 {
4601                 case CONVERT_TYPE_FLOOR:
4602                         return floor_type[randint0(100)];
4603                 case CONVERT_TYPE_WALL:
4604                         return fill_type[randint0(100)];
4605                 case CONVERT_TYPE_INNER:
4606                         return feat_wall_inner;
4607                 case CONVERT_TYPE_OUTER:
4608                         return feat_wall_outer;
4609                 case CONVERT_TYPE_SOLID:
4610                         return feat_wall_solid;
4611                 case CONVERT_TYPE_STREAM1:
4612                         return d_info[dungeon_type].stream1;
4613                 case CONVERT_TYPE_STREAM2:
4614                         return d_info[dungeon_type].stream2;
4615                 default:
4616                         return newfeat;
4617                 }
4618         }
4619         else return newfeat;
4620 }
4621
4622
4623 /*
4624  * Take a feature, determine what that feature becomes
4625  * through applying the given action.
4626  */
4627 FEAT_IDX feat_state(FEAT_IDX feat, int action)
4628 {
4629         feature_type *f_ptr = &f_info[feat];
4630         int i;
4631
4632         /* Get the new feature */
4633         for (i = 0; i < MAX_FEAT_STATES; i++)
4634         {
4635                 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
4636         }
4637
4638         if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
4639
4640         return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
4641 }
4642
4643 /*
4644  * Takes a location and action and changes the feature at that 
4645  * location through applying the given action.
4646  */
4647 void cave_alter_feat(POSITION y, POSITION x, int action)
4648 {
4649         /* Set old feature */
4650         FEAT_IDX oldfeat = cave[y][x].feat;
4651
4652         /* Get the new feat */
4653         FEAT_IDX newfeat = feat_state(oldfeat, action);
4654
4655         /* No change */
4656         if (newfeat == oldfeat) return;
4657
4658         /* Set the new feature */
4659         cave_set_feat(y, x, newfeat);
4660
4661         if (!(feature_action_flags[action] & FAF_NO_DROP))
4662         {
4663                 feature_type *old_f_ptr = &f_info[oldfeat];
4664                 feature_type *f_ptr = &f_info[newfeat];
4665                 bool found = FALSE;
4666
4667                 /* Handle gold */
4668                 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
4669                 {
4670                         /* Place some gold */
4671                         place_gold(y, x);
4672                         found = TRUE;
4673                 }
4674
4675                 /* Handle item */
4676                 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - dun_level / 2)))
4677                 {
4678                         /* Place object */
4679                         place_object(y, x, 0L);
4680                         found = TRUE;
4681                 }
4682
4683                 if (found && character_dungeon && player_can_see_bold(y, x))
4684                 {
4685                         msg_print(_("何かを発見した!", "You have found something!"));
4686                 }
4687         }
4688
4689         if (feature_action_flags[action] & FAF_CRASH_GLASS)
4690         {
4691                 feature_type *old_f_ptr = &f_info[oldfeat];
4692
4693                 if (have_flag(old_f_ptr->flags, FF_GLASS) && character_dungeon)
4694                 {
4695                         project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(dun_level, 100) / 4, GF_SHARDS,
4696                                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
4697                 }
4698         }
4699 }
4700
4701
4702 /* Remove a mirror */
4703 void remove_mirror(POSITION y, POSITION x)
4704 {
4705         cave_type *c_ptr = &cave[y][x];
4706
4707         /* Remove the mirror */
4708         c_ptr->info &= ~(CAVE_OBJECT);
4709         c_ptr->mimic = 0;
4710
4711         if (d_info[dungeon_type].flags1 & DF1_DARKNESS)
4712         {
4713                 c_ptr->info &= ~(CAVE_GLOW);
4714                 if (!view_torch_grids) c_ptr->info &= ~(CAVE_MARK);
4715                 if (c_ptr->m_idx) update_monster(c_ptr->m_idx, FALSE);
4716
4717                 update_local_illumination(y, x);
4718         }
4719
4720         note_spot(y, x);
4721
4722         lite_spot(y, x);
4723 }
4724
4725
4726 /*
4727  *  Return TRUE if there is a mirror on the grid.
4728  */
4729 bool is_mirror_grid(cave_type *c_ptr)
4730 {
4731         if ((c_ptr->info & CAVE_OBJECT) && have_flag(f_info[c_ptr->mimic].flags, FF_MIRROR))
4732                 return TRUE;
4733         else
4734                 return FALSE;
4735 }
4736
4737
4738 /*
4739  *  Return TRUE if there is a mirror on the grid.
4740  */
4741 bool is_glyph_grid(cave_type *c_ptr)
4742 {
4743         if ((c_ptr->info & CAVE_OBJECT) && have_flag(f_info[c_ptr->mimic].flags, FF_GLYPH))
4744                 return TRUE;
4745         else
4746                 return FALSE;
4747 }
4748
4749
4750 /*
4751  *  Return TRUE if there is a mirror on the grid.
4752  */
4753 bool is_explosive_rune_grid(cave_type *c_ptr)
4754 {
4755         if ((c_ptr->info & CAVE_OBJECT) && have_flag(f_info[c_ptr->mimic].flags, FF_MINOR_GLYPH))
4756                 return TRUE;
4757         else
4758                 return FALSE;
4759 }
4760
4761
4762 /*
4763  * Calculate "incremental motion". Used by project() and shoot().
4764  * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2).
4765  */
4766 void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
4767 {
4768         POSITION dy, dx, dist, shift;
4769
4770         /* Extract the distance travelled */
4771         dy = (*y < y1) ? y1 - *y : *y - y1;
4772         dx = (*x < x1) ? x1 - *x : *x - x1;
4773
4774         /* Number of steps */
4775         dist = (dy > dx) ? dy : dx;
4776
4777         /* We are calculating the next location */
4778         dist++;
4779
4780
4781         /* Calculate the total distance along each axis */
4782         dy = (y2 < y1) ? (y1 - y2) : (y2 - y1);
4783         dx = (x2 < x1) ? (x1 - x2) : (x2 - x1);
4784
4785         /* Paranoia -- Hack -- no motion */
4786         if (!dy && !dx) return;
4787
4788
4789         /* Move mostly vertically */
4790         if (dy > dx)
4791         {
4792                 /* Extract a shift factor */
4793                 shift = (dist * dx + (dy - 1) / 2) / dy;
4794
4795                 /* Sometimes move along the minor axis */
4796                 (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift);
4797
4798                 /* Always move along major axis */
4799                 (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist);
4800         }
4801
4802         /* Move mostly horizontally */
4803         else
4804         {
4805                 /* Extract a shift factor */
4806                 shift = (dist * dy + (dx - 1) / 2) / dx;
4807
4808                 /* Sometimes move along the minor axis */
4809                 (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift);
4810
4811                 /* Always move along major axis */
4812                 (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist);
4813         }
4814 }
4815
4816
4817
4818 /*
4819  * Determine if a bolt spell cast from (y1,x1) to (y2,x2) will arrive
4820  * at the final destination, assuming no monster gets in the way.
4821  *
4822  * This is slightly (but significantly) different from "los(y1,x1,y2,x2)".
4823  */
4824 bool projectable(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
4825 {
4826         POSITION y, x;
4827
4828         int grid_n = 0;
4829         u16b grid_g[512];
4830
4831         /* Check the projection path */
4832         grid_n = project_path(grid_g, (project_length ? project_length : MAX_RANGE), y1, x1, y2, x2, 0);
4833
4834         /* Identical grid */
4835         if (!grid_n) return TRUE;
4836
4837         /* Final grid */
4838         y = GRID_Y(grid_g[grid_n - 1]);
4839         x = GRID_X(grid_g[grid_n - 1]);
4840
4841         /* May not end in an unrequested grid */
4842         if ((y != y2) || (x != x2)) return (FALSE);
4843
4844         /* Assume okay */
4845         return (TRUE);
4846 }
4847
4848
4849 /*
4850  * Standard "find me a location" function
4851  *
4852  * Obtains a legal location within the given distance of the initial
4853  * location, and with "los()" from the source to destination location.
4854  *
4855  * This function is often called from inside a loop which searches for
4856  * locations while increasing the "d" distance.
4857  *
4858  * Currently the "m" parameter is unused.
4859  */
4860 void scatter(POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION d, BIT_FLAGS mode)
4861 {
4862         POSITION nx, ny;
4863
4864         /* Pick a location */
4865         while (TRUE)
4866         {
4867                 /* Pick a new location */
4868                 ny = rand_spread(y, d);
4869                 nx = rand_spread(x, d);
4870
4871                 /* Ignore annoying locations */
4872                 if (!in_bounds(ny, nx)) continue;
4873
4874                 /* Ignore "excessively distant" locations */
4875                 if ((d > 1) && (distance(y, x, ny, nx) > d)) continue;
4876
4877                 if (mode & PROJECT_LOS)
4878                 {
4879                         if(los(y, x, ny, nx)) break;
4880                 }
4881                 else
4882                 {
4883                         if(projectable(y, x, ny, nx)) break;
4884                 }
4885
4886         }
4887
4888         /* Save the location */
4889         (*yp) = ny;
4890         (*xp) = nx;
4891 }
4892
4893
4894
4895
4896 /*
4897  * Track a new monster
4898  */
4899 void health_track(MONSTER_IDX m_idx)
4900 {
4901         /* Mount monster is already tracked */
4902         if (m_idx && m_idx == p_ptr->riding) return;
4903
4904         /* Track a new guy */
4905         p_ptr->health_who = m_idx;
4906
4907         /* Redraw (later) */
4908         p_ptr->redraw |= (PR_HEALTH);
4909 }
4910
4911
4912
4913 /*
4914  * Hack -- track the given monster race
4915  */
4916 void monster_race_track(MONRACE_IDX r_idx)
4917 {
4918         /* Save this monster ID */
4919         p_ptr->monster_race_idx = r_idx;
4920
4921         p_ptr->window |= (PW_MONSTER);
4922 }
4923
4924
4925
4926 /*
4927  * Hack -- track the given object kind
4928  */
4929 void object_kind_track(KIND_OBJECT_IDX k_idx)
4930 {
4931         /* Save this monster ID */
4932         p_ptr->object_kind_idx = k_idx;
4933
4934         p_ptr->window |= (PW_OBJECT);
4935 }
4936
4937
4938
4939 /*
4940  * Something has happened to disturb the player.
4941  *
4942  * The first arg indicates a major disturbance, which affects search.
4943  *
4944  * The second arg is currently unused, but could induce output flush.
4945  *
4946  * All disturbance cancels repeated commands, resting, and running.
4947  */
4948 void disturb(bool stop_search, bool stop_travel)
4949 {
4950 #ifndef TRAVEL
4951         /* Unused */
4952         stop_travel = stop_travel;
4953 #endif
4954
4955         /* Cancel auto-commands */
4956         /* command_new = 0; */
4957
4958         /* Cancel repeated commands */
4959         if (command_rep)
4960         {
4961                 /* Cancel */
4962                 command_rep = 0;
4963
4964                 /* Redraw the state (later) */
4965                 p_ptr->redraw |= (PR_STATE);
4966         }
4967
4968         /* Cancel Resting */
4969         if ((p_ptr->action == ACTION_REST) || (p_ptr->action == ACTION_FISH) || (stop_search && (p_ptr->action == ACTION_SEARCH)))
4970         {
4971                 /* Cancel */
4972                 set_action(ACTION_NONE);
4973         }
4974
4975         /* Cancel running */
4976         if (running)
4977         {
4978                 /* Cancel */
4979                 running = 0;
4980
4981                 /* Check for new panel if appropriate */
4982                 if (center_player && !center_running) verify_panel();
4983
4984                 /* Calculate torch radius */
4985                 p_ptr->update |= (PU_TORCH);
4986
4987                 /* Update monster flow */
4988                 p_ptr->update |= (PU_FLOW);
4989         }
4990
4991 #ifdef TRAVEL
4992         if (stop_travel)
4993         {
4994                 /* Cancel */
4995                 travel.run = 0;
4996
4997                 /* Check for new panel if appropriate */
4998                 if (center_player && !center_running) verify_panel();
4999
5000                 /* Calculate torch radius */
5001                 p_ptr->update |= (PU_TORCH);
5002         }
5003 #endif
5004
5005         /* Flush the input if requested */
5006         if (flush_disturb) flush();
5007 }
5008
5009
5010 /*
5011  * Glow deep lava and building entrances in the floor
5012  */
5013 void glow_deep_lava_and_bldg(void)
5014 {
5015         POSITION y, x, yy, xx;
5016         DIRECTION i;
5017         cave_type *c_ptr;
5018
5019         /* Not in the darkness dungeon */
5020         if (d_info[dungeon_type].flags1 & DF1_DARKNESS) return;
5021
5022         for (y = 0; y < cur_hgt; y++)
5023         {
5024                 for (x = 0; x < cur_wid; x++)
5025                 {
5026                         c_ptr = &cave[y][x];
5027
5028                         /* Feature code (applying "mimic" field) */
5029
5030                         if (have_flag(f_info[get_feat_mimic(c_ptr)].flags, FF_GLOW))
5031                         {
5032                                 for (i = 0; i < 9; i++)
5033                                 {
5034                                         yy = y + ddy_ddd[i];
5035                                         xx = x + ddx_ddd[i];
5036                                         if (!in_bounds2(yy, xx)) continue;
5037                                         cave[yy][xx].info |= CAVE_GLOW;
5038                                 }
5039                         }
5040                 }
5041         }
5042
5043         /* Update the view and lite */
5044         p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE);
5045
5046         p_ptr->redraw |= (PR_MAP);
5047 }
5048
5049 /*!
5050 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
5051 * @param m_idx モンスターID
5052 * @param y 移動先Y座標
5053 * @param x 移動先X座標
5054 * @param mode オプション
5055 * @return テレポート先として妥当ならばtrue
5056 */
5057 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
5058 {
5059         monster_type *m_ptr = &m_list[m_idx];
5060         cave_type    *c_ptr = &cave[y][x];
5061         feature_type *f_ptr = &f_info[c_ptr->feat];
5062
5063         /* Require "teleportable" space */
5064         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
5065
5066         if (c_ptr->m_idx && (c_ptr->m_idx != m_idx)) return FALSE;
5067         if (player_bold(y, x)) return FALSE;
5068
5069         /* Hack -- no teleport onto glyph of warding */
5070         if (is_glyph_grid(c_ptr)) return FALSE;
5071         if (is_explosive_rune_grid(c_ptr)) return FALSE;
5072
5073         if (!(mode & TELEPORT_PASSIVE))
5074         {
5075                 if (!monster_can_cross_terrain(c_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
5076         }
5077
5078         return TRUE;
5079 }
5080
5081 /*!
5082 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
5083 * @param y 移動先Y座標
5084 * @param x 移動先X座標
5085 * @param mode オプション
5086 * @return テレポート先として妥当ならばtrue
5087 */
5088 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
5089 {
5090         cave_type    *c_ptr = &cave[y][x];
5091         feature_type *f_ptr = &f_info[c_ptr->feat];
5092
5093         /* Require "teleportable" space */
5094         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
5095
5096         /* No magical teleporting into vaults and such */
5097         if (!(mode & TELEPORT_NONMAGICAL) && (c_ptr->info & CAVE_ICKY)) return FALSE;
5098
5099         if (c_ptr->m_idx && (c_ptr->m_idx != p_ptr->riding)) return FALSE;
5100
5101         /* don't teleport on a trap. */
5102         if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
5103
5104         if (!(mode & TELEPORT_PASSIVE))
5105         {
5106                 if (!player_can_enter(c_ptr->feat, 0)) return FALSE;
5107
5108                 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
5109                 {
5110                         if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
5111                 }
5112
5113                 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
5114                 {
5115                         /* Always forbid deep lava */
5116                         if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
5117
5118                         /* Forbid shallow lava when the player don't have levitation */
5119                         if (!p_ptr->levitation) return FALSE;
5120                 }
5121
5122         }
5123
5124         return TRUE;
5125 }
5126
5127 /*!
5128  * @brief 地形は開くものであって、かつ開かれているかを返す /
5129  * Attempt to open the given chest at the given location
5130  * @param feat 地形ID
5131  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
5132  */
5133 bool is_open(FEAT_IDX feat)
5134 {
5135         return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
5136 }