OSDN Git Service

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