OSDN Git Service

[Refactor] #38997 update_local_illumination_aux() に player_type * 引数を追加. / Add player...
[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 "util.h"
22 #include "term.h"
23
24 #include "floor.h"
25 #include "world.h"
26 #include "object-flavor.h"
27 #include "object-hook.h"
28 #include "dungeon.h"
29 #include "floor-generate.h"
30 #include "grid.h"
31 #include "trap.h"
32 #include "rooms.h"
33 #include "monster.h"
34 #include "quest.h"
35 #include "feature.h"
36 #include "monster-status.h"
37 #include "player-status.h"
38 #include "player-effects.h"
39 #include "player-class.h"
40 #include "spells.h"
41 #include "view-mainwindow.h"
42 #include "realm-song.h"
43
44 #define MONSTER_FLOW_DEPTH 32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
45
46 /*
47  * Feature action flags
48  */
49 #define FAF_DESTROY     0x01
50 #define FAF_NO_DROP     0x02
51 #define FAF_CRASH_GLASS 0x04
52
53 /*!
54  * @brief 地形状態フラグテーブル /
55  * The table of features' actions
56  */
57 static const byte feature_action_flags[FF_FLAG_MAX] =
58 {
59         0, /* LOS */
60         0, /* PROJECT */
61         0, /* MOVE */
62         0, /* PLACE */
63         0, /* DROP */
64         0, /* SECRET */
65         0, /* NOTICE */
66         0, /* REMEMBER */
67         0, /* OPEN */
68         0, /* CLOSE */
69         FAF_CRASH_GLASS, /* BASH */
70         0, /* SPIKE */
71         FAF_DESTROY, /* DISARM */
72         0, /* STORE */
73         FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
74         0, /* MAY_HAVE_GOLD */
75         0, /* HAS_GOLD */
76         0, /* HAS_ITEM */
77         0, /* DOOR */
78         0, /* TRAP */
79         0, /* STAIRS */
80         0, /* GLYPH */
81         0, /* LESS */
82         0, /* MORE */
83         0, /* RUN */
84         0, /* FLOOR */
85         0, /* WALL */
86         0, /* PERMANENT */
87         0, /* INNER */
88         0, /* OUTER */
89         0, /* SOLID */
90         0, /* HIT_TRAP */
91
92         0, /* BRIDGE */
93         0, /* RIVER */
94         0, /* LAKE */
95         0, /* BRIDGED */
96         0, /* COVERED */
97         0, /* GLOW */
98         0, /* ENSECRET */
99         0, /* WATER */
100         0, /* LAVA */
101         0, /* SHALLOW */
102         0, /* DEEP */
103         0, /* FILLED */
104         FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
105         0, /* HURT_FIRE */
106         0, /* HURT_COLD */
107         0, /* HURT_ACID */
108         0, /* ICE */
109         0, /* ACID */
110         0, /* OIL */
111         0, /* XXX04 */
112         0, /* CAN_CLIMB */
113         0, /* CAN_FLY */
114         0, /* CAN_SWIM */
115         0, /* CAN_PASS */
116         0, /* CAN_OOZE */
117         0, /* CAN_DIG */
118         0, /* HIDE_ITEM */
119         0, /* HIDE_SNEAK */
120         0, /* HIDE_SWIM */
121         0, /* HIDE_DIG */
122         0, /* KILL_HUGE */
123         0, /* KILL_MOVE */
124
125         0, /* PICK_TRAP */
126         0, /* PICK_DOOR */
127         0, /* ALLOC */
128         0, /* CHEST */
129         0, /* DROP_1D2 */
130         0, /* DROP_2D2 */
131         0, /* DROP_GOOD */
132         0, /* DROP_GREAT */
133         0, /* HURT_POIS */
134         0, /* HURT_ELEC */
135         0, /* HURT_WATER */
136         0, /* HURT_BWATER */
137         0, /* USE_FEAT */
138         0, /* GET_FEAT */
139         0, /* GROUND */
140         0, /* OUTSIDE */
141         0, /* EASY_HIDE */
142         0, /* EASY_CLIMB */
143         0, /* MUST_CLIMB */
144         0, /* TREE */
145         0, /* NEED_TREE */
146         0, /* BLOOD */
147         0, /* DUST */
148         0, /* SLIME */
149         0, /* PLANT */
150         0, /* XXX2 */
151         0, /* INSTANT */
152         0, /* EXPLODE */
153         0, /* TIMED */
154         0, /* ERUPT */
155         0, /* STRIKE */
156         0, /* SPREAD */
157
158         0, /* SPECIAL */
159         FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
160         0, /* QUEST_ENTER */
161         0, /* QUEST_EXIT */
162         0, /* QUEST */
163         0, /* SHAFT */
164         0, /* MOUNTAIN */
165         0, /* BLDG */
166         0, /* MINOR_GLYPH */
167         0, /* PATTERN */
168         0, /* TOWN */
169         0, /* ENTRANCE */
170         0, /* MIRROR */
171         0, /* UNPERM */
172         0, /* TELEPORTABLE */
173         0, /* CONVERT */
174         0, /* GLASS */
175 };
176
177 /*!
178  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
179  * @param creature_ptr 配置したいクリーチャーの参照ポインタ
180  * @return 配置に成功したらTRUEを返す
181  */
182 bool new_player_spot(player_type *creature_ptr)
183 {
184         POSITION y = 0, x = 0;
185         int max_attempts = 10000;
186
187         grid_type *g_ptr;
188         feature_type *f_ptr;
189
190         while (max_attempts--)
191         {
192                 /* Pick a legal spot */
193                 y = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->height - 2);
194                 x = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->width - 2);
195
196                 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
197
198                 /* Must be a "naked" floor grid */
199                 if (g_ptr->m_idx) continue;
200                 if (creature_ptr->current_floor_ptr->dun_level)
201                 {
202                         f_ptr = &f_info[g_ptr->feat];
203
204                         if (max_attempts > 5000) /* Rule 1 */
205                         {
206                                 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
207                         }
208                         else /* Rule 2 */
209                         {
210                                 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
211                                 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
212                         }
213
214                         /* Refuse to start on anti-teleport grids in dungeon */
215                         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
216                 }
217                 if (!player_can_enter(g_ptr->feat, 0)) continue;
218                 if (!in_bounds(creature_ptr->current_floor_ptr, y, x)) continue;
219
220                 /* Refuse to start on anti-teleport grids */
221                 if (g_ptr->info & (CAVE_ICKY)) continue;
222
223                 break;
224         }
225
226         if (max_attempts < 1) /* Should be -1, actually if we failed... */
227                 return FALSE;
228
229         /* Save the new player grid */
230         creature_ptr->y = y;
231         creature_ptr->x = x;
232
233         return TRUE;
234 }
235
236
237
238 /*!
239  * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
240  * @param y 配置を試みたいマスのY座標
241  * @param x 配置を試みたいマスのX座標
242  * @return なし
243  */
244 void place_random_stairs(POSITION y, POSITION x)
245 {
246         bool up_stairs = TRUE;
247         bool down_stairs = TRUE;
248         grid_type *g_ptr;
249         g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
250         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) return;
251
252         if (!p_ptr->current_floor_ptr->dun_level) up_stairs = FALSE;
253         if (ironman_downward) up_stairs = FALSE;
254         if (p_ptr->current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) down_stairs = FALSE;
255         if (quest_number(p_ptr->current_floor_ptr->dun_level) && (p_ptr->current_floor_ptr->dun_level > 1)) down_stairs = FALSE;
256
257         /* We can't place both */
258         if (down_stairs && up_stairs)
259         {
260                 /* Choose a staircase randomly */
261                 if (randint0(100) < 50) up_stairs = FALSE;
262                 else down_stairs = FALSE;
263         }
264
265         /* Place the stairs */
266         if (up_stairs) set_cave_feat(p_ptr->current_floor_ptr, y, x, feat_up_stair);
267         else if (down_stairs) set_cave_feat(p_ptr->current_floor_ptr, y, x, feat_down_stair);
268 }
269
270 /*!
271  * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
272  * @param y ドアの配置を試みたいマスのY座標
273  * @param x ドアの配置を試みたいマスのX座標
274  * @param room 部屋に接している場合向けのドア生成か否か
275  * @return なし
276  */
277 void place_random_door(POSITION y, POSITION x, bool room)
278 {
279         int tmp, type;
280         FEAT_IDX feat = feat_none;
281         grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
282
283         /* Initialize mimic info */
284         g_ptr->mimic = 0;
285
286         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
287         {
288                 place_floor_bold(y, x);
289                 return;
290         }
291
292         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
293                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
294                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
295
296         /* Choose an object */
297         tmp = randint0(1000);
298
299         /* Open doors (300/1000) */
300         if (tmp < 300)
301         {
302                 /* Create open door */
303                 feat = feat_door[type].open;
304         }
305
306         /* Broken doors (100/1000) */
307         else if (tmp < 400)
308         {
309                 /* Create broken door */
310                 feat = feat_door[type].broken;
311         }
312
313         /* Secret doors (200/1000) */
314         else if (tmp < 600)
315         {
316                 /* Create secret door */
317                 place_closed_door(y, x, type);
318
319                 if (type != DOOR_CURTAIN)
320                 {
321                         /* Hide. If on the edge of room, use outer wall. */
322                         g_ptr->mimic = room ? feat_wall_outer : feat_wall_type[randint0(100)];
323
324                         /* Floor type terrain cannot hide a door */
325                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
326                         {
327                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
328                                 {
329                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
330                                 }
331                                 g_ptr->mimic = 0;
332                         }
333                 }
334         }
335
336         /* Closed, locked, or stuck doors (400/1000) */
337         else place_closed_door(y, x, type);
338
339         if (tmp < 400)
340         {
341                 if (feat != feat_none)
342                 {
343                         set_cave_feat(p_ptr->current_floor_ptr, y, x, feat);
344                 }
345                 else
346                 {
347                         place_floor_bold(y, x);
348                 }
349         }
350
351         delete_monster(y, x);
352 }
353
354 /*!
355  * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
356  * @param y ドアの配置を試みたいマスのY座標
357  * @param x ドアの配置を試みたいマスのX座標
358  * @param type ドアの地形ID
359  * @return なし
360  */
361 void place_closed_door(POSITION y, POSITION x, int type)
362 {
363         int tmp;
364         FEAT_IDX feat = feat_none;
365
366         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
367         {
368                 place_floor_bold(y, x);
369                 return;
370         }
371
372         /* Choose an object */
373         tmp = randint0(400);
374
375         /* Closed doors (300/400) */
376         if (tmp < 300)
377         {
378                 /* Create closed door */
379                 feat = feat_door[type].closed;
380         }
381
382         /* Locked doors (99/400) */
383         else if (tmp < 399)
384         {
385                 /* Create locked door */
386                 feat = feat_locked_door_random(type);
387         }
388
389         /* Stuck doors (1/400) */
390         else
391         {
392                 /* Create jammed door */
393                 feat = feat_jammed_door_random(type);
394         }
395
396         if (feat != feat_none)
397         {
398                 cave_set_feat(y, x, feat);
399
400                 /* Now it is not floor */
401                 p_ptr->current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
402         }
403         else
404         {
405                 place_floor_bold(y, x);
406         }
407 }
408
409 /*
410  * Routine used by the random vault creators to add a door to a location
411  * Note that range checking has to be done in the calling routine.
412  *
413  * The doors must be INSIDE the allocated region.
414  */
415 void add_door(POSITION x, POSITION y)
416 {
417         /* Need to have a wall in the center square */
418         if (!is_outer_bold(p_ptr->current_floor_ptr, y, x)) return;
419
420         /* look at:
421         *  x#x
422         *  .#.
423         *  x#x
424         *
425         *  where x=don't care
426         *  .=floor, #=wall
427         */
428
429         if (is_floor_bold(p_ptr->current_floor_ptr, y - 1, x) && is_floor_bold(p_ptr->current_floor_ptr, y + 1, x) &&
430                 (is_outer_bold(p_ptr->current_floor_ptr, y, x - 1) && is_outer_bold(p_ptr->current_floor_ptr, y, x + 1)))
431         {
432                 /* secret door */
433                 place_secret_door(p_ptr->current_floor_ptr, y, x, DOOR_DEFAULT);
434
435                 /* set boundarys so don't get wide doors */
436                 place_solid_bold(y, x - 1);
437                 place_solid_bold(y, x + 1);
438         }
439
440
441         /* look at:
442         *  x#x
443         *  .#.
444         *  x#x
445         *
446         *  where x = don't care
447         *  .=floor, #=wall
448         */
449         if (is_outer_bold(p_ptr->current_floor_ptr, y - 1, x) && is_outer_bold(p_ptr->current_floor_ptr, y + 1, x) &&
450                 is_floor_bold(p_ptr->current_floor_ptr, y, x - 1) && is_floor_bold(p_ptr->current_floor_ptr, y, x + 1))
451         {
452                 /* secret door */
453                 place_secret_door(p_ptr->current_floor_ptr, y, x, DOOR_DEFAULT);
454
455                 /* set boundarys so don't get wide doors */
456                 place_solid_bold(y - 1, x);
457                 place_solid_bold(y + 1, x);
458         }
459 }
460
461 /*!
462 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
463 * @param y1 基準となるマスのY座標
464 * @param x1 基準となるマスのX座標
465 * @return 通路の数
466 * @note Assumes "in_bounds(p_ptr->current_floor_ptr, y1, x1)"
467 * @details
468 * XXX XXX This routine currently only counts actual "empty floor"\n
469 * grids which are not in rooms.  We might want to also count stairs,\n
470 * open doors, closed doors, etc.
471 */
472 static int next_to_corr(POSITION y1, POSITION x1)
473 {
474         int i, k = 0;
475         POSITION y, x;
476
477         grid_type *g_ptr;
478
479         /* Scan adjacent grids */
480         for (i = 0; i < 4; i++)
481         {
482                 y = y1 + ddy_ddd[i];
483                 x = x1 + ddx_ddd[i];
484                 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
485
486                 /* Skip non floors */
487                 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
488
489                 /* Skip non "empty floor" grids */
490                 if (!is_floor_grid(g_ptr))
491                         continue;
492
493                 /* Skip grids inside rooms */
494                 if (g_ptr->info & (CAVE_ROOM)) continue;
495
496                 /* Count these grids */
497                 k++;
498         }
499
500         /* Return the number of corridors */
501         return (k);
502 }
503
504 /*!
505 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
506 * @param y 判定を行いたいマスのY座標
507 * @param x 判定を行いたいマスのX座標
508 * @return ドアを設置可能ならばTRUEを返す
509 * @note Assumes "in_bounds(p_ptr->current_floor_ptr, y1, x1)"
510 * @details
511 * \n
512 * Assumes "in_bounds(p_ptr->current_floor_ptr, y, x)"\n
513 */
514 static bool possible_doorway(POSITION y, POSITION x)
515 {
516         /* Count the adjacent corridors */
517         if (next_to_corr(y, x) >= 2)
518         {
519                 /* Check Vertical */
520                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
521                         cave_have_flag_bold(y + 1, x, FF_WALL))
522                 {
523                         return (TRUE);
524                 }
525
526                 /* Check Horizontal */
527                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
528                         cave_have_flag_bold(y, x + 1, FF_WALL))
529                 {
530                         return (TRUE);
531                 }
532         }
533
534         /* No doorway */
535         return (FALSE);
536 }
537
538 /*!
539 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
540 * @param y 設置を行いたいマスのY座標
541 * @param x 設置を行いたいマスのX座標
542 * @return なし
543 */
544 void try_door(POSITION y, POSITION x)
545 {       if (!in_bounds(p_ptr->current_floor_ptr, y, x)) return;
546
547         /* Ignore walls */
548         if (cave_have_flag_bold(y, x, FF_WALL)) return;
549
550         /* Ignore room grids */
551         if (p_ptr->current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
552
553         /* Occasional door (if allowed) */
554         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
555         {
556                 /* Place a door */
557                 place_random_door(y, x, FALSE);
558         }
559 }
560
561
562 /*!
563  * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
564  * @param x1 長方形の左端X座標(-1)
565  * @param x2 長方形の右端X座標(+1)
566  * @param y1 長方形の上端Y座標(-1)
567  * @param y2 長方形の下端Y座標(+1)
568  * @param light 照明の有無
569  * @return なし
570  */
571 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
572 {
573         POSITION x, y;
574
575         /* Place a full floor under the room */
576         for (y = y1 - 1; y <= y2 + 1; y++)
577         {
578                 for (x = x1 - 1; x <= x2 + 1; x++)
579                 {
580                         place_floor_bold(y, x);
581                         add_cave_info(p_ptr->current_floor_ptr, y, x, CAVE_ROOM);
582                         if (light) add_cave_info(p_ptr->current_floor_ptr, y, x, CAVE_GLOW);
583                 }
584         }
585 }
586
587
588 /*!
589  * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
590  * @param x1 長方形の左端X座標(-1)
591  * @param x2 長方形の右端X座標(+1)
592  * @param y1 長方形の上端Y座標(-1)
593  * @param y2 長方形の下端Y座標(+1)
594  * @param light 照明の有無
595  * @return なし
596  */
597 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
598 {
599         POSITION y, x;
600
601         place_floor(x1, x2, y1, y2, light);
602
603         /* Walls around the room */
604         for (y = y1 - 1; y <= y2 + 1; y++)
605         {
606                 place_outer_bold(y, x1 - 1);
607                 place_outer_bold(y, x2 + 1);
608         }
609         for (x = x1 - 1; x <= x2 + 1; x++)
610         {
611                 place_outer_bold(y1 - 1, x);
612                 place_outer_bold(y2 + 1, x);
613         }
614 }
615
616
617 /*!
618  * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
619  * @param y 配置したい中心マスのY座標
620  * @param x 配置したい中心マスのX座標
621  * @param num 配置したい数
622  * @return なし
623  * @details
624  * Only really called by some of the "vault" routines.
625  */
626 void vault_objects(POSITION y, POSITION x, int num)
627 {
628         int dummy = 0;
629         int i = 0, j = y, k = x;
630
631         grid_type *g_ptr;
632
633
634         /* Attempt to place 'num' objects */
635         for (; num > 0; --num)
636         {
637                 /* Try up to 11 spots looking for empty space */
638                 for (i = 0; i < 11; ++i)
639                 {
640                         /* Pick a random location */
641                         while (dummy < SAFE_MAX_ATTEMPTS)
642                         {
643                                 j = rand_spread(y, 2);
644                                 k = rand_spread(x, 3);
645                                 dummy++;
646                                 if (!in_bounds(p_ptr->current_floor_ptr, j, k)) continue;
647                                 break;
648                         }
649
650                         if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
651                         {
652                                 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
653                         }
654
655                         /* Require "clean" floor space */
656                         g_ptr = &p_ptr->current_floor_ptr->grid_array[j][k];
657                         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
658
659                         if (randint0(100) < 75)
660                         {
661                                 place_object(j, k, 0L);
662                         }
663                         else
664                         {
665                                 place_gold(j, k);
666                         }
667
668                         /* Placement accomplished */
669                         break;
670                 }
671         }
672 }
673
674 /*!
675  * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
676  * @param y トラップを配置したいマスの中心Y座標
677  * @param x トラップを配置したいマスの中心X座標
678  * @param yd Y方向の配置分散マス数
679  * @param xd X方向の配置分散マス数
680  * @return なし
681  * @details
682  * Only really called by some of the "vault" routines.
683  */
684 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
685 {
686         int count = 0, y1 = y, x1 = x;
687         int dummy = 0;
688
689         grid_type *g_ptr;
690
691         /* Place traps */
692         for (count = 0; count <= 5; count++)
693         {
694                 /* Get a location */
695                 while (dummy < SAFE_MAX_ATTEMPTS)
696                 {
697                         y1 = rand_spread(y, yd);
698                         x1 = rand_spread(x, xd);
699                         dummy++;
700                         if (!in_bounds(p_ptr->current_floor_ptr, y1, x1)) continue;
701                         break;
702                 }
703
704                 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
705                 {
706                         msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
707                 }
708
709                 /* Require "naked" floor grids */
710                 g_ptr = &p_ptr->current_floor_ptr->grid_array[y1][x1];
711                 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
712
713                 /* Place the trap */
714                 place_trap(y1, x1);
715
716                 break;
717         }
718 }
719
720 /*!
721  * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
722  * @param y トラップを配置したいマスの中心Y座標
723  * @param x トラップを配置したいマスの中心X座標
724  * @param yd Y方向の配置分散マス数
725  * @param xd X方向の配置分散マス数
726  * @param num 配置したいトラップの数
727  * @return なし
728  * @details
729  * Only really called by some of the "vault" routines.
730  */
731 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
732 {
733         int i;
734
735         for (i = 0; i < num; i++)
736         {
737                 vault_trap_aux(y, x, yd, xd);
738         }
739 }
740
741 /*!
742  * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
743  * @param y1 モンスターを配置したいマスの中心Y座標
744  * @param x1 モンスターを配置したいマスの中心X座標
745  * @param num 配置したいモンスターの数
746  * @return なし
747  * @details
748  * Only really called by some of the "vault" routines.
749  */
750 void vault_monsters(POSITION y1, POSITION x1, int num)
751 {
752         int k, i;
753         POSITION y, x;
754         grid_type *g_ptr;
755
756         /* Try to summon "num" monsters "near" the given location */
757         for (k = 0; k < num; k++)
758         {
759                 /* Try nine locations */
760                 for (i = 0; i < 9; i++)
761                 {
762                         int d = 1;
763
764                         /* Pick a nearby location */
765                         scatter(&y, &x, y1, x1, d, 0);
766
767                         /* Require "empty" floor grids */
768                         g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
769                         if (!cave_empty_grid(g_ptr)) continue;
770
771                         /* Place the monster (allow groups) */
772                         p_ptr->current_floor_ptr->monster_level = p_ptr->current_floor_ptr->base_level + 2;
773                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
774                         p_ptr->current_floor_ptr->monster_level = p_ptr->current_floor_ptr->base_level;
775                 }
776         }
777 }
778
779 /*!
780  * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
781  * @param x チェックするマスのX座標
782  * @param y チェックするマスのY座標
783  * @return 床系地形ならばTRUE
784  */
785 bool get_is_floor(POSITION x, POSITION y)
786 {
787         if (!in_bounds(p_ptr->current_floor_ptr, y, x))
788         {
789                 /* Out of bounds */
790                 return (FALSE);
791         }
792
793         /* Do the real check */
794         if (is_floor_bold(p_ptr->current_floor_ptr, y, x)) return (TRUE);
795
796         return (FALSE);
797 }
798
799 /*!
800  * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
801  * @param x 地形を変えたいマスのX座標
802  * @param y 地形を変えたいマスのY座標
803  * @return なし
804  */
805 void set_floor(POSITION x, POSITION y)
806 {
807         if (!in_bounds(p_ptr->current_floor_ptr, y, x))
808         {
809                 /* Out of bounds */
810                 return;
811         }
812
813         if (p_ptr->current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
814         {
815                 /* A room border don't touch. */
816                 return;
817         }
818
819         /* Set to be floor if is a wall (don't touch lakes). */
820         if (is_extra_bold(p_ptr->current_floor_ptr, y, x))
821                 place_floor_bold(y, x);
822 }
823
824 /*!
825  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
826  * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
827  * @return なし
828  */
829 void place_bound_perm_wall(grid_type *g_ptr)
830 {
831         if (bound_walls_perm)
832         {
833                 /* Clear boundary mimic */
834                 g_ptr->mimic = 0;
835         }
836         else
837         {
838                 feature_type *f_ptr = &f_info[g_ptr->feat];
839
840                 /* Hack -- Decline boundary walls with known treasure  */
841                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
842                         !have_flag(f_ptr->flags, FF_SECRET))
843                         g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
844
845                 /* Set boundary mimic */
846                 g_ptr->mimic = g_ptr->feat;
847         }
848
849         /* Add "solid" perma-wall */
850         place_solid_perm_grid(g_ptr);
851 }
852
853 /*!
854  * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
855  * @param g_ptr マス構造体の参照ポインタ
856  * @return 看破済みの罠があるならTRUEを返す。
857  */
858 bool is_known_trap(grid_type *g_ptr)
859 {
860         if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
861                 is_trap(g_ptr->feat)) return TRUE;
862         else
863                 return FALSE;
864 }
865
866
867
868 /*!
869  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
870  * @param g_ptr マス構造体の参照ポインタ
871  * @return 隠されたドアがあるならTRUEを返す。
872  */
873 bool is_hidden_door(grid_type *g_ptr)
874 {
875         if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
876                 is_closed_door(g_ptr->feat))
877                 return TRUE;
878         else
879                 return FALSE;
880 }
881
882 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
883
884
885 /*!
886  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
887  * @param y y座標
888  * @param x x座標
889  * @return 指定された座標に照明がかかっているならTRUEを返す。。
890  */
891 bool check_local_illumination(POSITION y, POSITION x)
892 {
893         /* Hack -- move towards player */
894         POSITION yy = (y < p_ptr->y) ? (y + 1) : (y > p_ptr->y) ? (y - 1) : y;
895         POSITION xx = (x < p_ptr->x) ? (x + 1) : (x > p_ptr->x) ? (x - 1) : x;
896
897         /* Check for "local" illumination */
898
899 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
900
901         /* Check for "complex" illumination */
902         if ((feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[yy][xx])) &&
903                 (p_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
904                 (feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[y][xx])) &&
905                 (p_ptr->current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
906                         (feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[yy][x])) &&
907                 (p_ptr->current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
908         {
909                 return TRUE;
910         }
911         else return FALSE;
912
913 #else /* COMPLEX_WALL_ILLUMINATION */
914
915         /* Check for "simple" illumination */
916         return (p_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
917
918 #endif /* COMPLEX_WALL_ILLUMINATION */
919 }
920
921
922 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
923 #define update_local_illumination_aux(C, Y, X) \
924 { \
925         if (player_has_los_bold((C), (Y), (X))) \
926         { \
927                 /* Update the monster */ \
928                 if ((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx) update_monster((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE); \
929 \
930                 /* Notice and redraw */ \
931                 note_spot((Y), (X)); \
932                 lite_spot((Y), (X)); \
933         } \
934 }
935
936 /*!
937  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
938  * @param creature_ptr 視界元のクリーチャー
939  * @param y 視界先y座標
940  * @param x 視界先x座標
941  * @return なし
942  */
943 void update_local_illumination(player_type * creature_ptr, POSITION y, POSITION x)
944 {
945         int i;
946         POSITION yy, xx;
947
948         if (!in_bounds(p_ptr->current_floor_ptr, y, x)) return;
949
950 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
951
952         if ((y != creature_ptr->y) && (x != creature_ptr->x))
953         {
954                 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
955                 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
956                 update_local_illumination_aux(creature_ptr, yy, xx);
957                 update_local_illumination_aux(creature_ptr, y, xx);
958                 update_local_illumination_aux(creature_ptr, yy, x);
959         }
960         else if (x != creature_ptr->x) /* y == creature_ptr->y */
961         {
962                 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
963                 for (i = -1; i <= 1; i++)
964                 {
965                         yy = y + i;
966                         update_local_illumination_aux(creature_ptr, yy, xx);
967                 }
968                 yy = y - 1;
969                 update_local_illumination_aux(creature_ptr, yy, x);
970                 yy = y + 1;
971                 update_local_illumination_aux(creature_ptr, yy, x);
972         }
973         else if (y != creature_ptr->y) /* x == creature_ptr->x */
974         {
975                 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
976                 for (i = -1; i <= 1; i++)
977                 {
978                         xx = x + i;
979                         update_local_illumination_aux(creature_ptr, yy, xx);
980                 }
981                 xx = x - 1;
982                 update_local_illumination_aux(creature_ptr, y, xx);
983                 xx = x + 1;
984                 update_local_illumination_aux(creature_ptr, y, xx);
985         }
986         else /* Player's grid */
987         {
988                 for (i = 0; i < 8; i++)
989                 {
990                         yy = y + ddy_cdd[i];
991                         xx = x + ddx_cdd[i];
992                         update_local_illumination_aux(creature_ptr, yy, xx);
993                 }
994         }
995
996 #else /* COMPLEX_WALL_ILLUMINATION */
997
998         if ((y != creature_ptr->y) && (x != creature_ptr->x))
999         {
1000                 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
1001                 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
1002                 update_local_illumination_aux(creature_ptr, yy, xx);
1003         }
1004         else if (x != creature_ptr->x) /* y == creature_ptr->y */
1005         {
1006                 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
1007                 for (i = -1; i <= 1; i++)
1008                 {
1009                         yy = y + i;
1010                         update_local_illumination_aux(creature_ptr, yy, xx);
1011                 }
1012         }
1013         else if (y != creature_ptr->y) /* x == creature_ptr->x */
1014         {
1015                 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
1016                 for (i = -1; i <= 1; i++)
1017                 {
1018                         xx = x + i;
1019                         update_local_illumination_aux(creature_ptr, yy, xx);
1020                 }
1021         }
1022         else /* Player's grid */
1023         {
1024                 for (i = 0; i < 8; i++)
1025                 {
1026                         yy = y + ddy_cdd[i];
1027                         xx = x + ddx_cdd[i];
1028                         update_local_illumination_aux(creature_ptr, yy, xx);
1029                 }
1030         }
1031
1032 #endif /* COMPLEX_WALL_ILLUMINATION */
1033 }
1034
1035
1036 /*!
1037  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
1038  * @return 視覚に収められていないならTRUEを返す
1039  * @details player_can_see_bold()関数の返り値の否定を返している。
1040  */
1041 bool no_lite(void)
1042 {
1043         return (!player_can_see_bold(p_ptr->y, p_ptr->x));
1044 }
1045
1046
1047 /*!
1048  * @brief 指定された座標が地震や階段生成の対象となるマスかを返す。 / Determine if a given location may be "destroyed"
1049  * @param y y座標
1050  * @param x x座標
1051  * @return 各種の変更が可能ならTRUEを返す。
1052  * @details
1053  * 条件は永久地形でなく、なおかつ該当のマスにアーティファクトが存在しないか、である。英語の旧コメントに反して*破壊*の抑止判定には現在使われていない。
1054  */
1055 bool cave_valid_bold(POSITION y, POSITION x)
1056 {
1057         grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1058         OBJECT_IDX this_o_idx, next_o_idx = 0;
1059
1060         /* Forbid perma-grids */
1061         if (cave_perma_grid(g_ptr)) return (FALSE);
1062
1063         /* Check objects */
1064         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1065         {
1066                 object_type *o_ptr;
1067                 o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
1068                 next_o_idx = o_ptr->next_o_idx;
1069
1070                 /* Forbid artifact grids */
1071                 if (object_is_artifact(o_ptr)) return (FALSE);
1072         }
1073
1074         /* Accept */
1075         return (TRUE);
1076 }
1077
1078
1079
1080 /*
1081  * Place an attr/char pair at the given map coordinate, if legal.
1082  */
1083 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
1084 {
1085         /* Only do "legal" locations */
1086         if (panel_contains(y, x))
1087         {
1088                 /* Hack -- fake monochrome */
1089                 if (!use_graphics)
1090                 {
1091                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1092                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1093                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1094                 }
1095
1096                 /* Draw the char using the attr */
1097                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
1098         }
1099 }
1100
1101
1102
1103
1104
1105 /*
1106  * Memorize interesting viewable object/features in the given grid
1107  *
1108  * This function should only be called on "legal" grids.
1109  *
1110  * This function will memorize the object and/or feature in the given
1111  * grid, if they are (1) viewable and (2) interesting.  Note that all
1112  * objects are interesting, all terrain features except floors (and
1113  * invisible traps) are interesting, and floors (and invisible traps)
1114  * are interesting sometimes (depending on various options involving
1115  * the illumination of floor grids).
1116  *
1117  * The automatic memorization of all objects and non-floor terrain
1118  * features as soon as they are displayed allows incredible amounts
1119  * of optimization in various places, especially "map_info()".
1120  *
1121  * Note that the memorization of objects is completely separate from
1122  * the memorization of terrain features, preventing annoying floor
1123  * memorization when a detected object is picked up from a dark floor,
1124  * and object memorization when an object is dropped into a floor grid
1125  * which is memorized but out-of-sight.
1126  *
1127  * This function should be called every time the "memorization" of
1128  * a grid (or the object in a grid) is called into question, such
1129  * as when an object is created in a grid, when a terrain feature
1130  * "changes" from "floor" to "non-floor", when any grid becomes
1131  * "illuminated" or "viewable", and when a "floor" grid becomes
1132  * "torch-lit".
1133  *
1134  * Note the relatively efficient use of this function by the various
1135  * "update_view()" and "update_lite()" calls, to allow objects and
1136  * terrain features to be memorized (and drawn) whenever they become
1137  * viewable or illuminated in any way, but not when they "maintain"
1138  * or "lose" their previous viewability or illumination.
1139  *
1140  * Note the butchered "internal" version of "player_can_see_bold()",
1141  * optimized primarily for the most common cases, that is, for the
1142  * non-marked floor grids.
1143  */
1144 void note_spot(POSITION y, POSITION x)
1145 {
1146         grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1147         OBJECT_IDX this_o_idx, next_o_idx = 0;
1148
1149         /* Blind players see nothing */
1150         if (p_ptr->blind) return;
1151
1152         /* Analyze non-torch-lit grids */
1153         if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
1154         {
1155                 /* Require line of sight to the grid */
1156                 if (!(g_ptr->info & (CAVE_VIEW))) return;
1157
1158                 /* Require "perma-lite" of the grid */
1159                 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1160                 {
1161                         /* Not Ninja */
1162                         if (!p_ptr->see_nocto) return;
1163                 }
1164         }
1165
1166
1167         /* Hack -- memorize objects */
1168         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1169         {
1170                 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
1171                 next_o_idx = o_ptr->next_o_idx;
1172
1173                 /* Memorize objects */
1174                 o_ptr->marked |= OM_FOUND;
1175         }
1176
1177
1178         /* Hack -- memorize grids */
1179         if (!(g_ptr->info & (CAVE_MARK)))
1180         {
1181                 /* Feature code (applying "mimic" field) */
1182                 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
1183
1184                 /* Memorize some "boring" grids */
1185                 if (!have_flag(f_ptr->flags, FF_REMEMBER))
1186                 {
1187                         /* Option -- memorize all torch-lit floors */
1188                         if (view_torch_grids &&
1189                                 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
1190                         {
1191                                 g_ptr->info |= (CAVE_MARK);
1192                         }
1193
1194                         /* Option -- memorize all perma-lit floors */
1195                         else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
1196                         {
1197                                 g_ptr->info |= (CAVE_MARK);
1198                         }
1199                 }
1200
1201                 /* Memorize normal grids */
1202                 else if (have_flag(f_ptr->flags, FF_LOS))
1203                 {
1204                         g_ptr->info |= (CAVE_MARK);
1205                 }
1206
1207                 /* Memorize torch-lit walls */
1208                 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1209                 {
1210                         g_ptr->info |= (CAVE_MARK);
1211                 }
1212
1213                 /* Memorize walls seen by noctovision of Ninja */
1214                 else if (p_ptr->see_nocto)
1215                 {
1216                         g_ptr->info |= (CAVE_MARK);
1217                 }
1218
1219                 /* Memorize certain non-torch-lit wall grids */
1220                 else if (check_local_illumination(y, x))
1221                 {
1222                         g_ptr->info |= (CAVE_MARK);
1223                 }
1224         }
1225
1226         /* Memorize terrain of the grid */
1227         g_ptr->info |= (CAVE_KNOWN);
1228 }
1229
1230 /*
1231  * Redraw (on the screen) a given MAP location
1232  *
1233  * This function should only be called on "legal" grids
1234  */
1235 void lite_spot(POSITION y, POSITION x)
1236 {
1237         /* Redraw if on screen */
1238         if (panel_contains(y, x) && in_bounds2(p_ptr->current_floor_ptr, y, x))
1239         {
1240                 TERM_COLOR a;
1241                 SYMBOL_CODE c;
1242                 TERM_COLOR ta;
1243                 SYMBOL_CODE tc;
1244
1245                 map_info(y, x, &a, &c, &ta, &tc);
1246
1247                 /* Hack -- fake monochrome */
1248                 if (!use_graphics)
1249                 {
1250                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1251                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1252                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1253                 }
1254
1255                 /* Hack -- Queue it */
1256                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
1257
1258                 /* Update sub-windows */
1259                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1260         }
1261 }
1262
1263 /*
1264  * Some comments on the grid flags.  -BEN-
1265  *
1266  *
1267  * One of the major bottlenecks in previous versions of Angband was in
1268  * the calculation of "line of sight" from the player to various grids,
1269  * such as monsters.  This was such a nasty bottleneck that a lot of
1270  * silly things were done to reduce the dependancy on "line of sight",
1271  * for example, you could not "see" any grids in a lit room until you
1272  * actually entered the room, and there were all kinds of bizarre grid
1273  * flags to enable this behavior.  This is also why the "call light"
1274  * spells always lit an entire room.
1275  *
1276  * The code below provides functions to calculate the "field of view"
1277  * for the player, which, once calculated, provides extremely fast
1278  * calculation of "line of sight from the player", and to calculate
1279  * the "field of torch lite", which, again, once calculated, provides
1280  * extremely fast calculation of "which grids are lit by the player's
1281  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
1282  * "GRID_LITE", as appropriate, these functions maintain an array for
1283  * each of these two flags, each array containing the locations of all
1284  * of the grids marked with the appropriate flag, which can be used to
1285  * very quickly scan through all of the grids in a given set.
1286  *
1287  * To allow more "semantically valid" field of view semantics, whenever
1288  * the field of view (or the set of torch lit grids) changes, all of the
1289  * grids in the field of view (or the set of torch lit grids) are "drawn"
1290  * so that changes in the world will become apparent as soon as possible.
1291  * This has been optimized so that only grids which actually "change" are
1292  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
1293  * of the grids which are entering or leaving the relevent set of grids.
1294  *
1295  * These new methods are so efficient that the old nasty code was removed.
1296  *
1297  * Note that there is no reason to "update" the "viewable space" unless
1298  * the player "moves", or walls/doors are created/destroyed, and there
1299  * is no reason to "update" the "torch lit grids" unless the field of
1300  * view changes, or the "light radius" changes.  This means that when
1301  * the player is resting, or digging, or doing anything that does not
1302  * involve movement or changing the state of the dungeon, there is no
1303  * need to update the "view" or the "lite" regions, which is nice.
1304  *
1305  * Note that the calls to the nasty "los()" function have been reduced
1306  * to a bare minimum by the use of the new "field of view" calculations.
1307  *
1308  * I wouldn't be surprised if slight modifications to the "update_view()"
1309  * function would allow us to determine "reverse line-of-sight" as well
1310  * as "normal line-of-sight", which would allow monsters to use a more
1311  * "correct" calculation to determine if they can "see" the player.  For
1312  * now, monsters simply "cheat" somewhat and assume that if the player
1313  * has "line of sight" to the monster, then the monster can "pretend"
1314  * that it has "line of sight" to the player.
1315  *
1316  *
1317  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
1318  * grid and maintains an array of all "CAVE_LITE" grids.
1319  *
1320  * This set of grids is the complete set of all grids which are lit by
1321  * the players light source, which allows the "player_can_see_bold()"
1322  * function to work very quickly.
1323  *
1324  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
1325  * fact, the player (unless blind) can always "see" all grids which are
1326  * marked as "CAVE_LITE", unless they are "off screen".
1327  *
1328  *
1329  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
1330  * grid and maintains an array of all "CAVE_VIEW" grids.
1331  *
1332  * This set of grids is the complete set of all grids within line of sight
1333  * of the player, allowing the "player_has_los_bold(p_ptr, )" macro to work very
1334  * quickly.
1335  *
1336  *
1337  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
1338  * temporary internal flag to mark those grids which are not only in view,
1339  * but which are also "easily" in line of sight of the player.  This flag
1340  * is always cleared when we are done.
1341  *
1342  *
1343  * The current "update_lite()" and "update_view()" algorithms use the
1344  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
1345  * to keep track of which grids were previously marked as "CAVE_LITE" or
1346  * "CAVE_VIEW", which allows us to optimize the "screen updates".
1347  *
1348  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
1349  * for various other purposes, such as spreading lite or darkness during
1350  * "lite_room()" / "unlite_room()", and for calculating monster flow.
1351  *
1352  *
1353  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
1354  * in some way permanently lit.  However, for the player to "see" anything
1355  * in the grid, as determined by "player_can_see()", the player must not be
1356  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
1357  * grids, even if marked as "perma lit", are only illuminated if they touch
1358  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
1359  *
1360  *
1361  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
1362  * that even if the player cannot "see" the grid, he "knows" the terrain in
1363  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
1364  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite(p_ptr, )",
1365  * or when one of the "memorize floor grids" options induces memorization.
1366  *
1367  * Objects are "memorized" in a different way, using a special "marked" flag
1368  * on the object itself, which is set when an object is observed or detected.
1369  *
1370  *
1371  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
1372  * and should be illuminated by "lite room" and "darkness" spells.
1373  *
1374  *
1375  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
1376  * and should be unavailable for "teleportation" destinations.
1377  *
1378  *
1379  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
1380  * which is observed, and the "view_torch_grids" allows the player to memorize
1381  * every torch-lit grid.  The player will always memorize important walls,
1382  * doors, stairs, and other terrain features, as well as any "detected" grids.
1383  *
1384  * Note that the new "update_view()" method allows, among other things, a room
1385  * to be "partially" seen as the player approaches it, with a growing cone of
1386  * floor appearing as the player gets closer to the door.  Also, by not turning
1387  * on the "memorize perma-lit grids" option, the player will only "see" those
1388  * floor grids which are actually in line of sight.
1389  *
1390  * And my favorite "plus" is that you can now use a special option to draw the
1391  * "floors" in the "viewable region" brightly (actually, to draw the *other*
1392  * grids dimly), providing a "pretty" effect as the player runs around, and
1393  * to efficiently display the "torch lite" in a special color.
1394  *
1395  *
1396  * Some comments on the "update_view()" algorithm...
1397  *
1398  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
1399  * and only has to call "los()" on the borderline cases.  The major axes/diags
1400  * even terminate early when they hit walls.  I need to find a quick way
1401  * to "terminate" the other scans.
1402  *
1403  * Note that in the worst case (a big empty area with say 5% scattered walls),
1404  * each of the 1500 or so nearby grids is checked once, most of them getting
1405  * an "instant" rating, and only a small portion requiring a call to "los()".
1406  *
1407  * The only time that the algorithm appears to be "noticeably" too slow is
1408  * when running, and this is usually only important in town, since the town
1409  * provides about the worst scenario possible, with large open regions and
1410  * a few scattered obstructions.  There is a special "efficiency" option to
1411  * allow the player to reduce his field of view in town, if needed.
1412  *
1413  * In the "best" case (say, a normal stretch of corridor), the algorithm
1414  * makes one check for each viewable grid, and makes no calls to "los()".
1415  * So running in corridors is very fast, and if a lot of monsters are
1416  * nearby, it is much faster than the old methods.
1417  *
1418  * Note that resting, most normal commands, and several forms of running,
1419  * plus all commands executed near large groups of monsters, are strictly
1420  * more efficient with "update_view()" that with the old "compute los() on
1421  * demand" method, primarily because once the "field of view" has been
1422  * calculated, it does not have to be recalculated until the player moves
1423  * (or a wall or door is created or destroyed).
1424  *
1425  * Note that we no longer have to do as many "los()" checks, since once the
1426  * "view" region has been built, very few things cause it to be "changed"
1427  * (player movement, and the opening/closing of doors, changes in wall status).
1428  * Note that door/wall changes are only relevant when the door/wall itself is
1429  * in the "view" region.
1430  *
1431  * The algorithm seems to only call "los()" from zero to ten times, usually
1432  * only when coming down a corridor into a room, or standing in a room, just
1433  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
1434  * we will be reducing the calls to "los()".
1435  *
1436  * I am thinking in terms of an algorithm that "walks" from the central point
1437  * out to the maximal "distance", at each point, determining the "view" code
1438  * (above).  For each grid not on a major axis or diagonal, the "view" code
1439  * depends on the "cave_los_bold(p_ptr->current_floor_ptr, )" and "view" of exactly two other grids
1440  * (the one along the nearest diagonal, and the one next to that one, see
1441  * "update_view_aux()"...).
1442  *
1443  * We "memorize" the viewable space array, so that at the cost of under 3000
1444  * bytes, we reduce the time taken by "forget_view()" to one assignment for
1445  * each grid actually in the "viewable space".  And for another 3000 bytes,
1446  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
1447  * are also used by other routines, thus reducing the cost to almost nothing.
1448  *
1449  * A similar thing is done for "forget_lite()" in which case the savings are
1450  * much less, but save us from doing bizarre maintenance checking.
1451  *
1452  * In the worst "normal" case (in the middle of the town), the reachable space
1453  * actually reaches to more than half of the largest possible "circle" of view,
1454  * or about 800 grids, and in the worse case (in the middle of a dungeon level
1455  * where all the walls have been removed), the reachable space actually reaches
1456  * the theoretical maximum size of just under 1500 grids.
1457  *
1458  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
1459  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
1460  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
1461  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
1462  * entire possible space (including initialization) in one step per grid.  If
1463  * we do the "clearing" as a separate step (and use an array of "view" grids),
1464  * then the clearing will take as many steps as grids that were viewed, and the
1465  * algorithm will be able to "stop" scanning at various points.
1466  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
1467  */
1468
1469 /*
1470  * Mega-Hack -- Delayed visual update
1471  * Only used if update_view(), update_lite() or update_mon_lite() was called
1472  */
1473 void delayed_visual_update(void)
1474 {
1475         int i;
1476         POSITION y, x;
1477         grid_type *g_ptr;
1478
1479         /* Update needed grids */
1480         for (i = 0; i < p_ptr->current_floor_ptr->redraw_n; i++)
1481         {
1482                 y = p_ptr->current_floor_ptr->redraw_y[i];
1483                 x = p_ptr->current_floor_ptr->redraw_x[i];
1484                 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1485
1486                 /* Update only needed grids (prevent multiple updating) */
1487                 if (!(g_ptr->info & CAVE_REDRAW)) continue;
1488
1489                 /* If required, note */
1490                 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
1491
1492                 lite_spot(y, x);
1493
1494                 /* Hack -- Visual update of monster on this grid */
1495                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1496
1497                 /* No longer in the array */
1498                 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
1499         }
1500
1501         /* None left */
1502         p_ptr->current_floor_ptr->redraw_n = 0;
1503 }
1504
1505
1506 /*
1507  * Hack - speed up the update_flow algorithm by only doing
1508  * it everytime the player moves out of LOS of the last
1509  * "way-point".
1510  */
1511 static POSITION flow_x = 0;
1512 static POSITION flow_y = 0;
1513
1514
1515
1516 /*
1517  * Hack -- fill in the "cost" field of every grid that the player
1518  * can "reach" with the number of steps needed to reach that grid.
1519  * This also yields the "distance" of the player from every grid.
1520  *
1521  * In addition, mark the "when" of the grids that can reach
1522  * the player with the incremented value of "flow_n".
1523  *
1524  * Hack -- use the "seen" array as a "circular queue".
1525  *
1526  * We do not need a priority queue because the cost from grid
1527  * to grid is always "one" and we process them in order.
1528  */
1529 void update_flow(void)
1530 {
1531         POSITION x, y;
1532         DIRECTION d;
1533         int flow_head = 1;
1534         int flow_tail = 0;
1535
1536         /* Paranoia -- make sure the array is empty */
1537         if (tmp_pos.n) return;
1538
1539         /* The last way-point is on the map */
1540         if (p_ptr->running && in_bounds(p_ptr->current_floor_ptr, flow_y, flow_x))
1541         {
1542                 /* The way point is in sight - do not update.  (Speedup) */
1543                 if (p_ptr->current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
1544         }
1545
1546         /* Erase all of the current flow information */
1547         for (y = 0; y < p_ptr->current_floor_ptr->height; y++)
1548         {
1549                 for (x = 0; x < p_ptr->current_floor_ptr->width; x++)
1550                 {
1551                         p_ptr->current_floor_ptr->grid_array[y][x].cost = 0;
1552                         p_ptr->current_floor_ptr->grid_array[y][x].dist = 0;
1553                 }
1554         }
1555
1556         /* Save player position */
1557         flow_y = p_ptr->y;
1558         flow_x = p_ptr->x;
1559
1560         /* Add the player's grid to the queue */
1561         tmp_pos.y[0] = p_ptr->y;
1562         tmp_pos.x[0] = p_ptr->x;
1563
1564         /* Now process the queue */
1565         while (flow_head != flow_tail)
1566         {
1567                 int ty, tx;
1568
1569                 /* Extract the next entry */
1570                 ty = tmp_pos.y[flow_tail];
1571                 tx = tmp_pos.x[flow_tail];
1572
1573                 /* Forget that entry */
1574                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
1575
1576                 /* Add the "children" */
1577                 for (d = 0; d < 8; d++)
1578                 {
1579                         int old_head = flow_head;
1580                         byte_hack m = p_ptr->current_floor_ptr->grid_array[ty][tx].cost + 1;
1581                         byte_hack n = p_ptr->current_floor_ptr->grid_array[ty][tx].dist + 1;
1582                         grid_type *g_ptr;
1583
1584                         /* Child location */
1585                         y = ty + ddy_ddd[d];
1586                         x = tx + ddx_ddd[d];
1587
1588                         /* Ignore player's grid */
1589                         if (player_bold(p_ptr, y, x)) continue;
1590
1591                         g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1592
1593                         if (is_closed_door(g_ptr->feat)) m += 3;
1594
1595                         /* Ignore "pre-stamped" entries */
1596                         if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
1597
1598                         /* Ignore "walls" and "rubble" */
1599                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1600
1601                         /* Save the flow cost */
1602                         if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
1603                         if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
1604
1605                         /* Hack -- limit flow depth */
1606                         if (n == MONSTER_FLOW_DEPTH) continue;
1607
1608                         /* Enqueue that entry */
1609                         tmp_pos.y[flow_head] = y;
1610                         tmp_pos.x[flow_head] = x;
1611
1612                         /* Advance the queue */
1613                         if (++flow_head == TEMP_MAX) flow_head = 0;
1614
1615                         /* Hack -- notice overflow by forgetting new entry */
1616                         if (flow_head == flow_tail) flow_head = old_head;
1617                 }
1618         }
1619 }
1620
1621 /*
1622  * Change the "feat" flag for a grid, and notice/redraw the grid
1623  */
1624 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
1625 {
1626         grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1627         feature_type *f_ptr = &f_info[feat];
1628         bool old_los, old_mirror;
1629
1630         if (!current_world_ptr->character_dungeon)
1631         {
1632                 /* Clear mimic type */
1633                 g_ptr->mimic = 0;
1634
1635                 /* Change the feature */
1636                 g_ptr->feat = feat;
1637
1638                 /* Hack -- glow the GLOW terrain */
1639                 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1640                 {
1641                         DIRECTION i;
1642                         POSITION yy, xx;
1643
1644                         for (i = 0; i < 9; i++)
1645                         {
1646                                 yy = y + ddy_ddd[i];
1647                                 xx = x + ddx_ddd[i];
1648                                 if (!in_bounds2(p_ptr->current_floor_ptr, yy, xx)) continue;
1649                                 p_ptr->current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
1650                         }
1651                 }
1652
1653                 return;
1654         }
1655
1656         old_los = cave_have_flag_bold(y, x, FF_LOS);
1657         old_mirror = is_mirror_grid(g_ptr);
1658
1659         /* Clear mimic type */
1660         g_ptr->mimic = 0;
1661
1662         /* Change the feature */
1663         g_ptr->feat = feat;
1664
1665         /* Remove flag for mirror/glyph */
1666         g_ptr->info &= ~(CAVE_OBJECT);
1667
1668         if (old_mirror && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1669         {
1670                 g_ptr->info &= ~(CAVE_GLOW);
1671                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1672
1673                 update_local_illumination(p_ptr, y, x);
1674         }
1675
1676         /* Check for change to boring grid */
1677         if (!have_flag(f_ptr->flags, FF_REMEMBER)) g_ptr->info &= ~(CAVE_MARK);
1678         if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1679
1680         note_spot(y, x);
1681         lite_spot(y, x);
1682
1683         /* Check if los has changed */
1684         if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
1685         {
1686
1687 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1688
1689                 update_local_illumination(p_ptr, y, x);
1690
1691 #endif /* COMPLEX_WALL_ILLUMINATION */
1692
1693                 /* Update the visuals */
1694                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
1695         }
1696
1697         /* Hack -- glow the GLOW terrain */
1698         if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1699         {
1700                 DIRECTION i;
1701                 POSITION yy, xx;
1702                 grid_type *cc_ptr;
1703
1704                 for (i = 0; i < 9; i++)
1705                 {
1706                         yy = y + ddy_ddd[i];
1707                         xx = x + ddx_ddd[i];
1708                         if (!in_bounds2(p_ptr->current_floor_ptr, yy, xx)) continue;
1709                         cc_ptr = &p_ptr->current_floor_ptr->grid_array[yy][xx];
1710                         cc_ptr->info |= CAVE_GLOW;
1711
1712                         if (player_has_los_grid(cc_ptr))
1713                         {
1714                                 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
1715
1716                                 note_spot(yy, xx);
1717
1718                                 lite_spot(yy, xx);
1719                         }
1720
1721                         update_local_illumination(p_ptr, yy, xx);
1722                 }
1723
1724                 if (p_ptr->special_defense & NINJA_S_STEALTH)
1725                 {
1726                         if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(p_ptr, FALSE);
1727                 }
1728         }
1729 }
1730
1731 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
1732 {
1733         feature_type *f_ptr = &f_info[newfeat];
1734
1735         if (have_flag(f_ptr->flags, FF_CONVERT))
1736         {
1737                 switch (f_ptr->subtype)
1738                 {
1739                 case CONVERT_TYPE_FLOOR:
1740                         return feat_ground_type[randint0(100)];
1741                 case CONVERT_TYPE_WALL:
1742                         return feat_wall_type[randint0(100)];
1743                 case CONVERT_TYPE_INNER:
1744                         return feat_wall_inner;
1745                 case CONVERT_TYPE_OUTER:
1746                         return feat_wall_outer;
1747                 case CONVERT_TYPE_SOLID:
1748                         return feat_wall_solid;
1749                 case CONVERT_TYPE_STREAM1:
1750                         return d_info[p_ptr->dungeon_idx].stream1;
1751                 case CONVERT_TYPE_STREAM2:
1752                         return d_info[p_ptr->dungeon_idx].stream2;
1753                 default:
1754                         return newfeat;
1755                 }
1756         }
1757         else return newfeat;
1758 }
1759
1760
1761 /*
1762  * Take a feature, determine what that feature becomes
1763  * through applying the given action.
1764  */
1765 FEAT_IDX feat_state(FEAT_IDX feat, int action)
1766 {
1767         feature_type *f_ptr = &f_info[feat];
1768         int i;
1769
1770         /* Get the new feature */
1771         for (i = 0; i < MAX_FEAT_STATES; i++)
1772         {
1773                 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
1774         }
1775
1776         if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
1777
1778         return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
1779 }
1780
1781 /*
1782  * Takes a location and action and changes the feature at that
1783  * location through applying the given action.
1784  */
1785 void cave_alter_feat(POSITION y, POSITION x, int action)
1786 {
1787         /* Set old feature */
1788         FEAT_IDX oldfeat = p_ptr->current_floor_ptr->grid_array[y][x].feat;
1789
1790         /* Get the new feat */
1791         FEAT_IDX newfeat = feat_state(oldfeat, action);
1792
1793         /* No change */
1794         if (newfeat == oldfeat) return;
1795
1796         /* Set the new feature */
1797         cave_set_feat(y, x, newfeat);
1798
1799         if (!(feature_action_flags[action] & FAF_NO_DROP))
1800         {
1801                 feature_type *old_f_ptr = &f_info[oldfeat];
1802                 feature_type *f_ptr = &f_info[newfeat];
1803                 bool found = FALSE;
1804
1805                 /* Handle gold */
1806                 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
1807                 {
1808                         /* Place some gold */
1809                         place_gold(y, x);
1810                         found = TRUE;
1811                 }
1812
1813                 /* Handle item */
1814                 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - p_ptr->current_floor_ptr->dun_level / 2)))
1815                 {
1816                         /* Place object */
1817                         place_object(y, x, 0L);
1818                         found = TRUE;
1819                 }
1820
1821                 if (found && current_world_ptr->character_dungeon && player_can_see_bold(y, x))
1822                 {
1823                         msg_print(_("何かを発見した!", "You have found something!"));
1824                 }
1825         }
1826
1827         if (feature_action_flags[action] & FAF_CRASH_GLASS)
1828         {
1829                 feature_type *old_f_ptr = &f_info[oldfeat];
1830
1831                 if (have_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon)
1832                 {
1833                         project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(p_ptr->current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
1834                                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1835                 }
1836         }
1837 }
1838
1839
1840 /* Remove a mirror */
1841 void remove_mirror(POSITION y, POSITION x)
1842 {
1843         grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1844
1845         /* Remove the mirror */
1846         g_ptr->info &= ~(CAVE_OBJECT);
1847         g_ptr->mimic = 0;
1848
1849         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
1850         {
1851                 g_ptr->info &= ~(CAVE_GLOW);
1852                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1853                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1854
1855                 update_local_illumination(p_ptr, y, x);
1856         }
1857
1858         note_spot(y, x);
1859
1860         lite_spot(y, x);
1861 }
1862
1863
1864 /*
1865  *  Return TRUE if there is a mirror on the grid.
1866  */
1867 bool is_mirror_grid(grid_type *g_ptr)
1868 {
1869         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1870                 return TRUE;
1871         else
1872                 return FALSE;
1873 }
1874
1875
1876 /*
1877  *  Return TRUE if there is a mirror on the grid.
1878  */
1879 bool is_glyph_grid(grid_type *g_ptr)
1880 {
1881         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1882                 return TRUE;
1883         else
1884                 return FALSE;
1885 }
1886
1887
1888 /*
1889  *  Return TRUE if there is a mirror on the grid.
1890  */
1891 bool is_explosive_rune_grid(grid_type *g_ptr)
1892 {
1893         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
1894                 return TRUE;
1895         else
1896                 return FALSE;
1897 }
1898
1899 /*!
1900 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
1901 * @param m_idx モンスターID
1902 * @param y 移動先Y座標
1903 * @param x 移動先X座標
1904 * @param mode オプション
1905 * @return テレポート先として妥当ならばtrue
1906 */
1907 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
1908 {
1909         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1910         grid_type    *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1911         feature_type *f_ptr = &f_info[g_ptr->feat];
1912
1913         /* Require "teleportable" space */
1914         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1915
1916         if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
1917         if (player_bold(p_ptr, y, x)) return FALSE;
1918
1919         /* Hack -- no teleport onto glyph of warding */
1920         if (is_glyph_grid(g_ptr)) return FALSE;
1921         if (is_explosive_rune_grid(g_ptr)) return FALSE;
1922
1923         if (!(mode & TELEPORT_PASSIVE))
1924         {
1925                 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
1926         }
1927
1928         return TRUE;
1929 }
1930
1931 /*!
1932 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1933 * @param y 移動先Y座標
1934 * @param x 移動先X座標
1935 * @param mode オプション
1936 * @return テレポート先として妥当ならばtrue
1937 */
1938 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
1939 {
1940         grid_type    *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1941         feature_type *f_ptr = &f_info[g_ptr->feat];
1942
1943         /* Require "teleportable" space */
1944         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1945
1946         /* No magical teleporting into vaults and such */
1947         if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
1948
1949         if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
1950
1951         /* don't teleport on a trap. */
1952         if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
1953
1954         if (!(mode & TELEPORT_PASSIVE))
1955         {
1956                 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
1957
1958                 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
1959                 {
1960                         if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
1961                 }
1962
1963                 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
1964                 {
1965                         /* Always forbid deep lava */
1966                         if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
1967
1968                         /* Forbid shallow lava when the player don't have levitation */
1969                         if (!p_ptr->levitation) return FALSE;
1970                 }
1971
1972         }
1973
1974         return TRUE;
1975 }
1976
1977 /*!
1978  * @brief 地形は開くものであって、かつ開かれているかを返す /
1979  * Attempt to open the given chest at the given location
1980  * @param feat 地形ID
1981  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
1982  */
1983 bool is_open(FEAT_IDX feat)
1984 {
1985         return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
1986 }
1987
1988 /*!
1989  * @brief プレイヤーが地形踏破可能かを返す
1990  * @param feature 判定したい地形ID
1991  * @param mode 移動に関するオプションフラグ
1992  * @return 移動可能ならばTRUEを返す
1993  */
1994 bool player_can_enter(FEAT_IDX feature, BIT_FLAGS16 mode)
1995 {
1996         feature_type *f_ptr = &f_info[feature];
1997
1998         if (p_ptr->riding) return monster_can_cross_terrain(feature, &r_info[p_ptr->current_floor_ptr->m_list[p_ptr->riding].r_idx], mode | CEM_RIDING);
1999
2000         if (have_flag(f_ptr->flags, FF_PATTERN))
2001         {
2002                 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
2003         }
2004
2005         if (have_flag(f_ptr->flags, FF_CAN_FLY) && p_ptr->levitation) return TRUE;
2006         if (have_flag(f_ptr->flags, FF_CAN_SWIM) && p_ptr->can_swim) return TRUE;
2007         if (have_flag(f_ptr->flags, FF_CAN_PASS) && p_ptr->pass_wall) return TRUE;
2008
2009         if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;
2010
2011         return TRUE;
2012 }
2013