OSDN Git Service

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