OSDN Git Service

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