OSDN Git Service

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