OSDN Git Service

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