OSDN Git Service

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