OSDN Git Service

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