OSDN Git Service

Merge remote-tracking branch 'remotes/origin/feature/Fix-Sanity-Blast-by-AutoSave...
[hengband/hengband.git] / src / grid / grid.c
1 /*!
2  * @brief グリッドの実装 / low level dungeon routines -BEN-
3  * @date 2013/12/30
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  *\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * \n
11  * Support for Adam Bolt's tileset, lighting and transparency effects\n
12  * by Robert Ruehlmann (rr9@angband.org)\n
13  * \n
14  * 2013 Deskull Doxygen向けのコメント整理\n
15  */
16
17 #include "grid/grid.h"
18 #include "core/window-redrawer.h"
19 #include "dungeon/dungeon-flag-types.h"
20 #include "dungeon/dungeon.h"
21 #include "dungeon/quest.h"
22 #include "effect/effect-characteristics.h"
23 #include "effect/effect-processor.h"
24 #include "floor/cave.h"
25 #include "floor/floor-generator.h"
26 #include "game-option/game-play-options.h"
27 #include "game-option/map-screen-options.h"
28 #include "game-option/special-options.h"
29 #include "grid/feature.h"
30 #include "grid/object-placer.h"
31 #include "grid/trap.h"
32 #include "io/screen-util.h"
33 #include "monster-floor/monster-remover.h"
34 #include "monster-race/monster-race.h"
35 #include "monster/monster-info.h"
36 #include "monster/monster-status.h"
37 #include "monster/monster-update.h"
38 #include "object/item-tester-hooker.h"
39 #include "object/object-mark-types.h"
40 #include "player/player-class.h"
41 #include "player/player-status.h"
42 #include "player/player-status-flags.h"
43 #include "room/rooms-builder.h"
44 #include "spell/spell-types.h"
45 #include "system/floor-type-definition.h"
46 #include "term/term-color-types.h"
47 #include "util/bit-flags-calculator.h"
48 #include "view/display-map.h"
49 #include "view/display-messages.h"
50 #include "window/main-window-util.h"
51 #include "world/world.h"
52
53 #define MONSTER_FLOW_DEPTH                                                                                                                                     \
54     32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
55
56 /*
57  * Feature action flags
58  */
59 #define FAF_DESTROY 0x01
60 #define FAF_NO_DROP 0x02
61 #define FAF_CRASH_GLASS 0x04
62
63 pos_list tmp_pos;
64
65 /*!
66  * @brief 地形状態フラグテーブル /
67  * The table of features' actions
68  */
69 static const byte feature_action_flags[FF_FLAG_MAX] = {
70     0, /* LOS */
71     0, /* PROJECT */
72     0, /* MOVE */
73     0, /* PLACE */
74     0, /* DROP */
75     0, /* SECRET */
76     0, /* NOTICE */
77     0, /* REMEMBER */
78     0, /* OPEN */
79     0, /* CLOSE */
80     FAF_CRASH_GLASS, /* BASH */
81     0, /* SPIKE */
82     FAF_DESTROY, /* DISARM */
83     0, /* STORE */
84     FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
85     0, /* MAY_HAVE_GOLD */
86     0, /* HAS_GOLD */
87     0, /* HAS_ITEM */
88     0, /* DOOR */
89     0, /* TRAP */
90     0, /* STAIRS */
91     0, /* GLYPH */
92     0, /* LESS */
93     0, /* MORE */
94     0, /* RUN */
95     0, /* FLOOR */
96     0, /* WALL */
97     0, /* PERMANENT */
98     0, /* INNER */
99     0, /* OUTER */
100     0, /* SOLID */
101     0, /* HIT_TRAP */
102
103     0, /* BRIDGE */
104     0, /* RIVER */
105     0, /* LAKE */
106     0, /* BRIDGED */
107     0, /* COVERED */
108     0, /* GLOW */
109     0, /* ENSECRET */
110     0, /* WATER */
111     0, /* LAVA */
112     0, /* SHALLOW */
113     0, /* DEEP */
114     0, /* FILLED */
115     FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
116     0, /* HURT_FIRE */
117     0, /* HURT_COLD */
118     0, /* HURT_ACID */
119     0, /* ICE */
120     0, /* ACID */
121     0, /* OIL */
122     0, /* XXX04 */
123     0, /* CAN_CLIMB */
124     0, /* CAN_FLY */
125     0, /* CAN_SWIM */
126     0, /* CAN_PASS */
127     0, /* CAN_OOZE */
128     0, /* CAN_DIG */
129     0, /* HIDE_ITEM */
130     0, /* HIDE_SNEAK */
131     0, /* HIDE_SWIM */
132     0, /* HIDE_DIG */
133     0, /* KILL_HUGE */
134     0, /* KILL_MOVE */
135
136     0, /* PICK_TRAP */
137     0, /* PICK_DOOR */
138     0, /* ALLOC */
139     0, /* CHEST */
140     0, /* DROP_1D2 */
141     0, /* DROP_2D2 */
142     0, /* DROP_GOOD */
143     0, /* DROP_GREAT */
144     0, /* HURT_POIS */
145     0, /* HURT_ELEC */
146     0, /* HURT_WATER */
147     0, /* HURT_BWATER */
148     0, /* USE_FEAT */
149     0, /* GET_FEAT */
150     0, /* GROUND */
151     0, /* OUTSIDE */
152     0, /* EASY_HIDE */
153     0, /* EASY_CLIMB */
154     0, /* MUST_CLIMB */
155     0, /* TREE */
156     0, /* NEED_TREE */
157     0, /* BLOOD */
158     0, /* DUST */
159     0, /* SLIME */
160     0, /* PLANT */
161     0, /* XXX2 */
162     0, /* INSTANT */
163     0, /* EXPLODE */
164     0, /* TIMED */
165     0, /* ERUPT */
166     0, /* STRIKE */
167     0, /* SPREAD */
168
169     0, /* SPECIAL */
170     FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
171     0, /* QUEST_ENTER */
172     0, /* QUEST_EXIT */
173     0, /* QUEST */
174     0, /* SHAFT */
175     0, /* MOUNTAIN */
176     0, /* BLDG */
177     0, /* MINOR_GLYPH */
178     0, /* PATTERN */
179     0, /* TOWN */
180     0, /* ENTRANCE */
181     0, /* MIRROR */
182     0, /* UNPERM */
183     0, /* TELEPORTABLE */
184     0, /* CONVERT */
185     0, /* GLASS */
186 };
187
188 /*!
189  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
190  * @param creature_ptr 配置したいクリーチャーの参照ポインタ
191  * @return 配置に成功したらTRUEを返す
192  */
193 bool new_player_spot(player_type *creature_ptr)
194 {
195     POSITION y = 0, x = 0;
196     int max_attempts = 10000;
197
198     grid_type *g_ptr;
199     feature_type *f_ptr;
200
201     while (max_attempts--) {
202         /* Pick a legal spot */
203         y = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->height - 2);
204         x = (POSITION)rand_range(1, creature_ptr->current_floor_ptr->width - 2);
205
206         g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
207
208         /* Must be a "naked" floor grid */
209         if (g_ptr->m_idx)
210             continue;
211         if (creature_ptr->current_floor_ptr->dun_level) {
212             f_ptr = &f_info[g_ptr->feat];
213
214             if (max_attempts > 5000) /* Rule 1 */
215             {
216                 if (!has_flag(f_ptr->flags, FF_FLOOR))
217                     continue;
218             } else /* Rule 2 */
219             {
220                 if (!has_flag(f_ptr->flags, FF_MOVE))
221                     continue;
222                 if (has_flag(f_ptr->flags, FF_HIT_TRAP))
223                     continue;
224             }
225
226             /* Refuse to start on anti-teleport grids in dungeon */
227             if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
228                 continue;
229         }
230         if (!player_can_enter(creature_ptr, g_ptr->feat, 0))
231             continue;
232         if (!in_bounds(creature_ptr->current_floor_ptr, y, x))
233             continue;
234
235         /* Refuse to start on anti-teleport grids */
236         if (g_ptr->info & (CAVE_ICKY))
237             continue;
238
239         break;
240     }
241
242     if (max_attempts < 1) /* Should be -1, actually if we failed... */
243         return FALSE;
244
245     /* Save the new player grid */
246     creature_ptr->y = y;
247     creature_ptr->x = x;
248
249     return TRUE;
250 }
251
252 /*!
253  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
254  * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
255  * @return なし
256  */
257 void place_bound_perm_wall(player_type *player_ptr, grid_type *g_ptr)
258 {
259     if (bound_walls_perm) {
260         /* Clear boundary mimic */
261         g_ptr->mimic = 0;
262     } else {
263         feature_type *f_ptr = &f_info[g_ptr->feat];
264
265         /* Hack -- Decline boundary walls with known treasure  */
266         if ((has_flag(f_ptr->flags, FF_HAS_GOLD) || has_flag(f_ptr->flags, FF_HAS_ITEM)) && !has_flag(f_ptr->flags, FF_SECRET))
267             g_ptr->feat = feat_state(player_ptr, g_ptr->feat, FF_ENSECRET);
268
269         /* Set boundary mimic */
270         g_ptr->mimic = g_ptr->feat;
271     }
272
273     /* Add "solid" perma-wall */
274     place_grid(player_ptr, g_ptr, GB_SOLID_PERM);
275 }
276
277 /*!
278  * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
279  * @param player_ptr プレーヤーへの参照ポインタ
280  * @param g_ptr マス構造体の参照ポインタ
281  * @return 看破済みの罠があるならTRUEを返す。
282  */
283 bool is_known_trap(player_type *player_ptr, grid_type *g_ptr)
284 {
285     if (!g_ptr->mimic && !cave_has_flag_grid(g_ptr, FF_SECRET) && is_trap(player_ptr, g_ptr->feat))
286         return TRUE;
287     else
288         return FALSE;
289 }
290
291 /*!
292  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
293  * @param player_ptr プレーヤーへの参照ポインタ
294  * @param g_ptr マス構造体の参照ポインタ
295  * @return 隠されたドアがあるならTRUEを返す。
296  */
297 bool is_hidden_door(player_type *player_ptr, grid_type *g_ptr)
298 {
299     if ((g_ptr->mimic || cave_has_flag_grid(g_ptr, FF_SECRET)) && is_closed_door(player_ptr, g_ptr->feat))
300         return TRUE;
301     else
302         return FALSE;
303 }
304
305 /*!
306  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
307  * @param y y座標
308  * @param x x座標
309  * @return 指定された座標に照明がかかっているならTRUEを返す。。
310  */
311 bool check_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
312 {
313     /* Hack -- move towards player */
314     POSITION yy = (y < creature_ptr->y) ? (y + 1) : (y > creature_ptr->y) ? (y - 1) : y;
315     POSITION xx = (x < creature_ptr->x) ? (x + 1) : (x > creature_ptr->x) ? (x - 1) : x;
316
317     /* Check for "local" illumination */
318
319     /* Check for "complex" illumination */
320     if ((feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][xx]))
321             && (creature_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW))
322         || (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[y][xx]))
323             && (creature_ptr->current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW))
324         || (feat_supports_los(get_feat_mimic(&creature_ptr->current_floor_ptr->grid_array[yy][x]))
325             && (creature_ptr->current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW))) {
326         return TRUE;
327     } else
328         return FALSE;
329 }
330
331 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
332 #define update_local_illumination_aux(C, Y, X)                                                                                                                 \
333     {                                                                                                                                                          \
334         if (player_has_los_bold((C), (Y), (X))) {                                                                                                              \
335             /* Update the monster */                                                                                                                           \
336             if ((C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx)                                                                                            \
337                 update_monster((C), (C)->current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE);                                                                \
338                                                                                                                                                                \
339             /* Notice and redraw */                                                                                                                            \
340             note_spot((C), (Y), (X));                                                                                                                          \
341             lite_spot((C), (Y), (X));                                                                                                                          \
342         }                                                                                                                                                      \
343     }
344
345 /*!
346  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
347  * @param creature_ptr 視界元のクリーチャー
348  * @param y 視界先y座標
349  * @param x 視界先x座標
350  * @return なし
351  */
352 void update_local_illumination(player_type *creature_ptr, POSITION y, POSITION x)
353 {
354     int i;
355     POSITION yy, xx;
356
357     if (!in_bounds(creature_ptr->current_floor_ptr, y, x))
358         return;
359
360     if ((y != creature_ptr->y) && (x != creature_ptr->x)) {
361         yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
362         xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
363         update_local_illumination_aux(creature_ptr, yy, xx);
364         update_local_illumination_aux(creature_ptr, y, xx);
365         update_local_illumination_aux(creature_ptr, yy, x);
366     } else if (x != creature_ptr->x) /* y == creature_ptr->y */
367     {
368         xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
369         for (i = -1; i <= 1; i++) {
370             yy = y + i;
371             update_local_illumination_aux(creature_ptr, yy, xx);
372         }
373         yy = y - 1;
374         update_local_illumination_aux(creature_ptr, yy, x);
375         yy = y + 1;
376         update_local_illumination_aux(creature_ptr, yy, x);
377     } else if (y != creature_ptr->y) /* x == creature_ptr->x */
378     {
379         yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
380         for (i = -1; i <= 1; i++) {
381             xx = x + i;
382             update_local_illumination_aux(creature_ptr, yy, xx);
383         }
384         xx = x - 1;
385         update_local_illumination_aux(creature_ptr, y, xx);
386         xx = x + 1;
387         update_local_illumination_aux(creature_ptr, y, xx);
388     } else /* Player's grid */
389     {
390         for (i = 0; i < 8; i++) {
391             yy = y + ddy_cdd[i];
392             xx = x + ddx_cdd[i];
393             update_local_illumination_aux(creature_ptr, yy, xx);
394         }
395     }
396 }
397
398 /*!
399  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
400  * @return 視覚に収められていないならTRUEを返す
401  * @details player_can_see_bold()関数の返り値の否定を返している。
402  */
403 bool no_lite(player_type *creature_ptr) { return (!player_can_see_bold(creature_ptr, creature_ptr->y, creature_ptr->x)); }
404
405 /*
406  * Place an attr/char pair at the given map coordinate, if legal.
407  */
408 void print_rel(player_type *subject_ptr, SYMBOL_CODE c, TERM_COLOR a, POSITION y, POSITION x)
409 {
410     /* Only do "legal" locations */
411     if (panel_contains(y, x)) {
412         /* Hack -- fake monochrome */
413         if (!use_graphics) {
414             if (current_world_ptr->timewalk_m_idx)
415                 a = TERM_DARK;
416             else if (is_invuln(subject_ptr) || subject_ptr->timewalk)
417                 a = TERM_WHITE;
418             else if (subject_ptr->wraith_form)
419                 a = TERM_L_DARK;
420         }
421
422         /* Draw the char using the attr */
423         term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
424     }
425 }
426
427 /*
428  * todo ここにplayer_type を追加した時のコンパイルエラーに対処できなかったので保留
429  * Memorize interesting viewable object/features in the given grid
430  *
431  * This function should only be called on "legal" grids.
432  *
433  * This function will memorize the object and/or feature in the given
434  * grid, if they are (1) viewable and (2) interesting.  Note that all
435  * objects are interesting, all terrain features except floors (and
436  * invisible traps) are interesting, and floors (and invisible traps)
437  * are interesting sometimes (depending on various options involving
438  * the illumination of floor grids).
439  *
440  * The automatic memorization of all objects and non-floor terrain
441  * features as soon as they are displayed allows incredible amounts
442  * of optimization in various places, especially "map_info()".
443  *
444  * Note that the memorization of objects is completely separate from
445  * the memorization of terrain features, preventing annoying floor
446  * memorization when a detected object is picked up from a dark floor,
447  * and object memorization when an object is dropped into a floor grid
448  * which is memorized but out-of-sight.
449  *
450  * This function should be called every time the "memorization" of
451  * a grid (or the object in a grid) is called into question, such
452  * as when an object is created in a grid, when a terrain feature
453  * "changes" from "floor" to "non-floor", when any grid becomes
454  * "illuminated" or "viewable", and when a "floor" grid becomes
455  * "torch-lit".
456  *
457  * Note the relatively efficient use of this function by the various
458  * "update_view()" and "update_lite()" calls, to allow objects and
459  * terrain features to be memorized (and drawn) whenever they become
460  * viewable or illuminated in any way, but not when they "maintain"
461  * or "lose" their previous viewability or illumination.
462  *
463  * Note the butchered "internal" version of "player_can_see_bold()",
464  * optimized primarily for the most common cases, that is, for the
465  * non-marked floor grids.
466  */
467 void note_spot(player_type *player_ptr, POSITION y, POSITION x)
468 {
469     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
470     OBJECT_IDX this_o_idx, next_o_idx = 0;
471
472     /* Blind players see nothing */
473     if (player_ptr->blind)
474         return;
475
476     /* Analyze non-torch-lit grids */
477     if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT))) {
478         /* Require line of sight to the grid */
479         if (!(g_ptr->info & (CAVE_VIEW)))
480             return;
481
482         /* Require "perma-lite" of the grid */
483         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) {
484             /* Not Ninja */
485             if (!player_ptr->see_nocto)
486                 return;
487         }
488     }
489
490     /* Hack -- memorize objects */
491     for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx) {
492         object_type *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
493         next_o_idx = o_ptr->next_o_idx;
494
495         /* Memorize objects */
496         o_ptr->marked |= OM_FOUND;
497     }
498
499     /* Hack -- memorize grids */
500     if (!(g_ptr->info & (CAVE_MARK))) {
501         /* Feature code (applying "mimic" field) */
502         feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
503
504         /* Memorize some "boring" grids */
505         if (!has_flag(f_ptr->flags, FF_REMEMBER)) {
506             /* Option -- memorize all torch-lit floors */
507             if (view_torch_grids && ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || player_ptr->see_nocto)) {
508                 g_ptr->info |= (CAVE_MARK);
509             }
510
511             /* Option -- memorize all perma-lit floors */
512             else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW)) {
513                 g_ptr->info |= (CAVE_MARK);
514             }
515         }
516
517         /* Memorize normal grids */
518         else if (has_flag(f_ptr->flags, FF_LOS)) {
519             g_ptr->info |= (CAVE_MARK);
520         }
521
522         /* Memorize torch-lit walls */
523         else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) {
524             g_ptr->info |= (CAVE_MARK);
525         }
526
527         /* Memorize walls seen by noctovision of Ninja */
528         else if (player_ptr->see_nocto) {
529             g_ptr->info |= (CAVE_MARK);
530         }
531
532         /* Memorize certain non-torch-lit wall grids */
533         else if (check_local_illumination(player_ptr, y, x)) {
534             g_ptr->info |= (CAVE_MARK);
535         }
536     }
537
538     /* Memorize terrain of the grid */
539     g_ptr->info |= (CAVE_KNOWN);
540 }
541
542 /*
543  * Redraw (on the screen) a given MAP location
544  *
545  * This function should only be called on "legal" grids
546  */
547 void lite_spot(player_type *player_ptr, POSITION y, POSITION x)
548 {
549     /* Redraw if on screen */
550     if (panel_contains(y, x) && in_bounds2(player_ptr->current_floor_ptr, y, x)) {
551         TERM_COLOR a;
552         SYMBOL_CODE c;
553         TERM_COLOR ta;
554         SYMBOL_CODE tc;
555
556         map_info(player_ptr, y, x, &a, &c, &ta, &tc);
557
558         /* Hack -- fake monochrome */
559         if (!use_graphics) {
560             if (current_world_ptr->timewalk_m_idx)
561                 a = TERM_DARK;
562             else if (is_invuln(player_ptr) || player_ptr->timewalk)
563                 a = TERM_WHITE;
564             else if (player_ptr->wraith_form)
565                 a = TERM_L_DARK;
566         }
567
568         /* Hack -- Queue it */
569         term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
570
571         /* Update sub-windows */
572         player_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
573     }
574 }
575
576 /*
577  * Some comments on the grid flags.  -BEN-
578  *
579  *
580  * One of the major bottlenecks in previous versions of Angband was in
581  * the calculation of "line of sight" from the player to various grids,
582  * such as monsters.  This was such a nasty bottleneck that a lot of
583  * silly things were done to reduce the dependancy on "line of sight",
584  * for example, you could not "see" any grids in a lit room until you
585  * actually entered the room, and there were all kinds of bizarre grid
586  * flags to enable this behavior.  This is also why the "call light"
587  * spells always lit an entire room.
588  *
589  * The code below provides functions to calculate the "field of view"
590  * for the player, which, once calculated, provides extremely fast
591  * calculation of "line of sight from the player", and to calculate
592  * the "field of torch lite", which, again, once calculated, provides
593  * extremely fast calculation of "which grids are lit by the player's
594  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
595  * "GRID_LITE", as appropriate, these functions maintain an array for
596  * each of these two flags, each array containing the locations of all
597  * of the grids marked with the appropriate flag, which can be used to
598  * very quickly scan through all of the grids in a given set.
599  *
600  * To allow more "semantically valid" field of view semantics, whenever
601  * the field of view (or the set of torch lit grids) changes, all of the
602  * grids in the field of view (or the set of torch lit grids) are "drawn"
603  * so that changes in the world will become apparent as soon as possible.
604  * This has been optimized so that only grids which actually "change" are
605  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
606  * of the grids which are entering or leaving the relevent set of grids.
607  *
608  * These new methods are so efficient that the old nasty code was removed.
609  *
610  * Note that there is no reason to "update" the "viewable space" unless
611  * the player "moves", or walls/doors are created/destroyed, and there
612  * is no reason to "update" the "torch lit grids" unless the field of
613  * view changes, or the "light radius" changes.  This means that when
614  * the player is resting, or digging, or doing anything that does not
615  * involve movement or changing the state of the dungeon, there is no
616  * need to update the "view" or the "lite" regions, which is nice.
617  *
618  * Note that the calls to the nasty "los()" function have been reduced
619  * to a bare minimum by the use of the new "field of view" calculations.
620  *
621  * I wouldn't be surprised if slight modifications to the "update_view()"
622  * function would allow us to determine "reverse line-of-sight" as well
623  * as "normal line-of-sight", which would allow monsters to use a more
624  * "correct" calculation to determine if they can "see" the player.  For
625  * now, monsters simply "cheat" somewhat and assume that if the player
626  * has "line of sight" to the monster, then the monster can "pretend"
627  * that it has "line of sight" to the player.
628  *
629  *
630  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
631  * grid and maintains an array of all "CAVE_LITE" grids.
632  *
633  * This set of grids is the complete set of all grids which are lit by
634  * the players light source, which allows the "player_can_see_bold()"
635  * function to work very quickly.
636  *
637  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
638  * fact, the player (unless blind) can always "see" all grids which are
639  * marked as "CAVE_LITE", unless they are "off screen".
640  *
641  *
642  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
643  * grid and maintains an array of all "CAVE_VIEW" grids.
644  *
645  * This set of grids is the complete set of all grids within line of sight
646  * of the player, allowing the "player_has_los_bold()" macro to work very
647  * quickly.
648  *
649  *
650  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
651  * temporary internal flag to mark those grids which are not only in view,
652  * but which are also "easily" in line of sight of the player.  This flag
653  * is always cleared when we are done.
654  *
655  *
656  * The current "update_lite()" and "update_view()" algorithms use the
657  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
658  * to keep track of which grids were previously marked as "CAVE_LITE" or
659  * "CAVE_VIEW", which allows us to optimize the "screen updates".
660  *
661  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
662  * for various other purposes, such as spreading lite or darkness during
663  * "lite_room()" / "unlite_room()", and for calculating monster flow.
664  *
665  *
666  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
667  * in some way permanently lit.  However, for the player to "see" anything
668  * in the grid, as determined by "player_can_see()", the player must not be
669  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
670  * grids, even if marked as "perma lit", are only illuminated if they touch
671  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
672  *
673  *
674  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
675  * that even if the player cannot "see" the grid, he "knows" the terrain in
676  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
677  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
678  * or when one of the "memorize floor grids" options induces memorization.
679  *
680  * Objects are "memorized" in a different way, using a special "marked" flag
681  * on the object itself, which is set when an object is observed or detected.
682  *
683  *
684  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
685  * and should be illuminated by "lite room" and "darkness" spells.
686  *
687  *
688  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
689  * and should be unavailable for "teleportation" destinations.
690  *
691  *
692  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
693  * which is observed, and the "view_torch_grids" allows the player to memorize
694  * every torch-lit grid.  The player will always memorize important walls,
695  * doors, stairs, and other terrain features, as well as any "detected" grids.
696  *
697  * Note that the new "update_view()" method allows, among other things, a room
698  * to be "partially" seen as the player approaches it, with a growing cone of
699  * floor appearing as the player gets closer to the door.  Also, by not turning
700  * on the "memorize perma-lit grids" option, the player will only "see" those
701  * floor grids which are actually in line of sight.
702  *
703  * And my favorite "plus" is that you can now use a special option to draw the
704  * "floors" in the "viewable region" brightly (actually, to draw the *other*
705  * grids dimly), providing a "pretty" effect as the player runs around, and
706  * to efficiently display the "torch lite" in a special color.
707  *
708  *
709  * Some comments on the "update_view()" algorithm...
710  *
711  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
712  * and only has to call "los()" on the borderline cases.  The major axes/diags
713  * even terminate early when they hit walls.  I need to find a quick way
714  * to "terminate" the other scans.
715  *
716  * Note that in the worst case (a big empty area with say 5% scattered walls),
717  * each of the 1500 or so nearby grids is checked once, most of them getting
718  * an "instant" rating, and only a small portion requiring a call to "los()".
719  *
720  * The only time that the algorithm appears to be "noticeably" too slow is
721  * when running, and this is usually only important in town, since the town
722  * provides about the worst scenario possible, with large open regions and
723  * a few scattered obstructions.  There is a special "efficiency" option to
724  * allow the player to reduce his field of view in town, if needed.
725  *
726  * In the "best" case (say, a normal stretch of corridor), the algorithm
727  * makes one check for each viewable grid, and makes no calls to "los()".
728  * So running in corridors is very fast, and if a lot of monsters are
729  * nearby, it is much faster than the old methods.
730  *
731  * Note that resting, most normal commands, and several forms of running,
732  * plus all commands executed near large groups of monsters, are strictly
733  * more efficient with "update_view()" that with the old "compute los() on
734  * demand" method, primarily because once the "field of view" has been
735  * calculated, it does not have to be recalculated until the player moves
736  * (or a wall or door is created or destroyed).
737  *
738  * Note that we no longer have to do as many "los()" checks, since once the
739  * "view" region has been built, very few things cause it to be "changed"
740  * (player movement, and the opening/closing of doors, changes in wall status).
741  * Note that door/wall changes are only relevant when the door/wall itself is
742  * in the "view" region.
743  *
744  * The algorithm seems to only call "los()" from zero to ten times, usually
745  * only when coming down a corridor into a room, or standing in a room, just
746  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
747  * we will be reducing the calls to "los()".
748  *
749  * I am thinking in terms of an algorithm that "walks" from the central point
750  * out to the maximal "distance", at each point, determining the "view" code
751  * (above).  For each grid not on a major axis or diagonal, the "view" code
752  * depends on the "cave_los_bold()" and "view" of exactly two other grids
753  * (the one along the nearest diagonal, and the one next to that one, see
754  * "update_view_aux()"...).
755  *
756  * We "memorize" the viewable space array, so that at the cost of under 3000
757  * bytes, we reduce the time taken by "forget_view()" to one assignment for
758  * each grid actually in the "viewable space".  And for another 3000 bytes,
759  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
760  * are also used by other routines, thus reducing the cost to almost nothing.
761  *
762  * A similar thing is done for "forget_lite()" in which case the savings are
763  * much less, but save us from doing bizarre maintenance checking.
764  *
765  * In the worst "normal" case (in the middle of the town), the reachable space
766  * actually reaches to more than half of the largest possible "circle" of view,
767  * or about 800 grids, and in the worse case (in the middle of a dungeon level
768  * where all the walls have been removed), the reachable space actually reaches
769  * the theoretical maximum size of just under 1500 grids.
770  *
771  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
772  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
773  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
774  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
775  * entire possible space (including initialization) in one step per grid.  If
776  * we do the "clearing" as a separate step (and use an array of "view" grids),
777  * then the clearing will take as many steps as grids that were viewed, and the
778  * algorithm will be able to "stop" scanning at various points.
779  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
780  */
781
782 /*
783  * Hack - speed up the update_flow algorithm by only doing
784  * it everytime the player moves out of LOS of the last
785  * "way-point".
786  */
787 static POSITION flow_x = 0;
788 static POSITION flow_y = 0;
789
790 /*
791  * Hack -- fill in the "cost" field of every grid that the player
792  * can "reach" with the number of steps needed to reach that grid.
793  * This also yields the "distance" of the player from every grid.
794  *
795  * In addition, mark the "when" of the grids that can reach
796  * the player with the incremented value of "flow_n".
797  *
798  * Hack -- use the "seen" array as a "circular queue".
799  *
800  * We do not need a priority queue because the cost from grid
801  * to grid is always "one" and we process them in order.
802  */
803 void update_flow(player_type *subject_ptr)
804 {
805     POSITION x, y;
806     DIRECTION d;
807     int flow_head_grid = 1;
808     int flow_tail_grid = 0;
809
810     /* Paranoia -- make sure the array is empty */
811     if (tmp_pos.n)
812         return;
813
814     /* The last way-point is on the map */
815     if (subject_ptr->running && in_bounds(subject_ptr->current_floor_ptr, flow_y, flow_x)) {
816         /* The way point is in sight - do not update.  (Speedup) */
817         if (subject_ptr->current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW)
818             return;
819     }
820
821     /* Erase all of the current flow information */
822     for (y = 0; y < subject_ptr->current_floor_ptr->height; y++) {
823         for (x = 0; x < subject_ptr->current_floor_ptr->width; x++) {
824             subject_ptr->current_floor_ptr->grid_array[y][x].cost = 0;
825             subject_ptr->current_floor_ptr->grid_array[y][x].dist = 0;
826         }
827     }
828
829     /* Save player position */
830     flow_y = subject_ptr->y;
831     flow_x = subject_ptr->x;
832
833     /* Add the player's grid to the queue */
834     tmp_pos.y[0] = subject_ptr->y;
835     tmp_pos.x[0] = subject_ptr->x;
836
837     /* Now process the queue */
838     while (flow_head_grid != flow_tail_grid) {
839         int ty, tx;
840
841         /* Extract the next entry */
842         ty = tmp_pos.y[flow_tail_grid];
843         tx = tmp_pos.x[flow_tail_grid];
844
845         /* Forget that entry */
846         if (++flow_tail_grid == TEMP_MAX)
847             flow_tail_grid = 0;
848
849         /* Add the "children" */
850         for (d = 0; d < 8; d++) {
851             int old_head = flow_head_grid;
852             byte m = subject_ptr->current_floor_ptr->grid_array[ty][tx].cost + 1;
853             byte n = subject_ptr->current_floor_ptr->grid_array[ty][tx].dist + 1;
854             grid_type *g_ptr;
855
856             /* Child location */
857             y = ty + ddy_ddd[d];
858             x = tx + ddx_ddd[d];
859
860             /* Ignore player's grid */
861             if (player_bold(subject_ptr, y, x))
862                 continue;
863
864             g_ptr = &subject_ptr->current_floor_ptr->grid_array[y][x];
865
866             if (is_closed_door(subject_ptr, g_ptr->feat))
867                 m += 3;
868
869             /* Ignore "pre-stamped" entries */
870             if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m)
871                 continue;
872
873             /* Ignore "walls" and "rubble" */
874             if (!cave_has_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(subject_ptr, g_ptr->feat))
875                 continue;
876
877             /* Save the flow cost */
878             if (g_ptr->cost == 0 || g_ptr->cost > m)
879                 g_ptr->cost = m;
880             if (g_ptr->dist == 0 || g_ptr->dist > n)
881                 g_ptr->dist = n;
882
883             /* Hack -- limit flow depth */
884             if (n == MONSTER_FLOW_DEPTH)
885                 continue;
886
887             /* Enqueue that entry */
888             tmp_pos.y[flow_head_grid] = y;
889             tmp_pos.x[flow_head_grid] = x;
890
891             /* Advance the queue */
892             if (++flow_head_grid == TEMP_MAX)
893                 flow_head_grid = 0;
894
895             /* Hack -- notice overflow by forgetting new entry */
896             if (flow_head_grid == flow_tail_grid)
897                 flow_head_grid = old_head;
898         }
899     }
900 }
901
902 /*
903  * Take a feature, determine what that feature becomes
904  * through applying the given action.
905  */
906 FEAT_IDX feat_state(player_type *player_ptr, FEAT_IDX feat, int action)
907 {
908     feature_type *f_ptr = &f_info[feat];
909     int i;
910
911     /* Get the new feature */
912     floor_type *floor_ptr = player_ptr->current_floor_ptr;
913     for (i = 0; i < MAX_FEAT_STATES; i++) {
914         if (f_ptr->state[i].action == action)
915             return conv_dungeon_feat(floor_ptr, f_ptr->state[i].result);
916     }
917
918     if (has_flag(f_ptr->flags, FF_PERMANENT))
919         return feat;
920
921     return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(floor_ptr, f_ptr->destroyed) : feat;
922 }
923
924 /*
925  * Takes a location and action and changes the feature at that
926  * location through applying the given action.
927  */
928 void cave_alter_feat(player_type *player_ptr, POSITION y, POSITION x, int action)
929 {
930     /* Set old feature */
931     floor_type *floor_ptr = player_ptr->current_floor_ptr;
932     FEAT_IDX oldfeat = floor_ptr->grid_array[y][x].feat;
933
934     /* Get the new feat */
935     FEAT_IDX newfeat = feat_state(player_ptr, oldfeat, action);
936
937     /* No change */
938     if (newfeat == oldfeat)
939         return;
940
941     /* Set the new feature */
942     cave_set_feat(player_ptr, y, x, newfeat);
943
944     if (!(feature_action_flags[action] & FAF_NO_DROP)) {
945         feature_type *old_f_ptr = &f_info[oldfeat];
946         feature_type *f_ptr = &f_info[newfeat];
947         bool found = FALSE;
948
949         /* Handle gold */
950         if (has_flag(old_f_ptr->flags, FF_HAS_GOLD) && !has_flag(f_ptr->flags, FF_HAS_GOLD)) {
951             /* Place some gold */
952             place_gold(player_ptr, y, x);
953             found = TRUE;
954         }
955
956         /* Handle item */
957         if (has_flag(old_f_ptr->flags, FF_HAS_ITEM) && !has_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - floor_ptr->dun_level / 2))) {
958             /* Place object */
959             place_object(player_ptr, y, x, 0L);
960             found = TRUE;
961         }
962
963         if (found && current_world_ptr->character_dungeon && player_can_see_bold(player_ptr, y, x)) {
964             msg_print(_("何かを発見した!", "You have found something!"));
965         }
966     }
967
968     if (feature_action_flags[action] & FAF_CRASH_GLASS) {
969         feature_type *old_f_ptr = &f_info[oldfeat];
970
971         if (has_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon) {
972             project(player_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(floor_ptr->dun_level, 100) / 4, GF_SHARDS,
973                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
974         }
975     }
976 }
977
978 /* Remove a mirror */
979 void remove_mirror(player_type *caster_ptr, POSITION y, POSITION x)
980 {
981     grid_type *g_ptr = &caster_ptr->current_floor_ptr->grid_array[y][x];
982
983     /* Remove the mirror */
984     g_ptr->info &= ~(CAVE_OBJECT);
985     g_ptr->mimic = 0;
986
987     if (d_info[caster_ptr->dungeon_idx].flags1 & DF1_DARKNESS) {
988         g_ptr->info &= ~(CAVE_GLOW);
989         if (!view_torch_grids)
990             g_ptr->info &= ~(CAVE_MARK);
991         if (g_ptr->m_idx)
992             update_monster(caster_ptr, g_ptr->m_idx, FALSE);
993
994         update_local_illumination(caster_ptr, y, x);
995     }
996
997     note_spot(caster_ptr, y, x);
998
999     lite_spot(caster_ptr, y, x);
1000 }
1001
1002 /*
1003  *  Return TRUE if there is a mirror on the grid.
1004  */
1005 bool is_mirror_grid(grid_type *g_ptr)
1006 {
1007     if ((g_ptr->info & CAVE_OBJECT) && has_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1008         return TRUE;
1009     else
1010         return FALSE;
1011 }
1012
1013 /*
1014  *  Return TRUE if there is a mirror on the grid.
1015  */
1016 bool is_glyph_grid(grid_type *g_ptr)
1017 {
1018     if ((g_ptr->info & CAVE_OBJECT) && has_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1019         return TRUE;
1020     else
1021         return FALSE;
1022 }
1023
1024 /*
1025  *  Return TRUE if there is a mirror on the grid.
1026  */
1027 bool is_explosive_rune_grid(grid_type *g_ptr)
1028 {
1029     if ((g_ptr->info & CAVE_OBJECT) && has_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
1030         return TRUE;
1031     else
1032         return FALSE;
1033 }
1034
1035 /*!
1036  * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
1037  * @param player_ptr プレーヤーへの参照ポインタ
1038  * @param m_idx モンスターID
1039  * @param y 移動先Y座標
1040  * @param x 移動先X座標
1041  * @param mode オプション
1042  * @return テレポート先として妥当ならばtrue
1043  */
1044 bool cave_monster_teleportable_bold(player_type *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, teleport_flags mode)
1045 {
1046     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
1047     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1048     feature_type *f_ptr = &f_info[g_ptr->feat];
1049
1050     /* Require "teleportable" space */
1051     if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
1052         return FALSE;
1053
1054     if (g_ptr->m_idx && (g_ptr->m_idx != m_idx))
1055         return FALSE;
1056     if (player_bold(player_ptr, y, x))
1057         return FALSE;
1058
1059     /* Hack -- no teleport onto glyph of warding */
1060     if (is_glyph_grid(g_ptr))
1061         return FALSE;
1062     if (is_explosive_rune_grid(g_ptr))
1063         return FALSE;
1064
1065     if (!(mode & TELEPORT_PASSIVE)) {
1066         if (!monster_can_cross_terrain(player_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], 0))
1067             return FALSE;
1068     }
1069
1070     return TRUE;
1071 }
1072
1073 /*!
1074  * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1075  * @param player_ptr プレーヤーへの参照ポインタ
1076  * @param y 移動先Y座標
1077  * @param x 移動先X座標
1078  * @param mode オプション
1079  * @return テレポート先として妥当ならばtrue
1080  */
1081 bool cave_player_teleportable_bold(player_type *player_ptr, POSITION y, POSITION x, teleport_flags mode)
1082 {
1083     grid_type *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
1084     feature_type *f_ptr = &f_info[g_ptr->feat];
1085
1086     /* Require "teleportable" space */
1087     if (!has_flag(f_ptr->flags, FF_TELEPORTABLE))
1088         return FALSE;
1089
1090     /* No magical teleporting into vaults and such */
1091     if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY))
1092         return FALSE;
1093
1094     if (g_ptr->m_idx && (g_ptr->m_idx != player_ptr->riding))
1095         return FALSE;
1096
1097     /* don't teleport on a trap. */
1098     if (has_flag(f_ptr->flags, FF_HIT_TRAP))
1099         return FALSE;
1100
1101     if (!(mode & TELEPORT_PASSIVE)) {
1102         if (!player_can_enter(player_ptr, g_ptr->feat, 0))
1103             return FALSE;
1104
1105         if (has_flag(f_ptr->flags, FF_WATER) && has_flag(f_ptr->flags, FF_DEEP)) {
1106             if (!player_ptr->levitation && !player_ptr->can_swim)
1107                 return FALSE;
1108         }
1109
1110         if (has_flag(f_ptr->flags, FF_LAVA) && !has_immune_fire(player_ptr) && !is_invuln(player_ptr)) {
1111             /* Always forbid deep lava */
1112             if (has_flag(f_ptr->flags, FF_DEEP))
1113                 return FALSE;
1114
1115             /* Forbid shallow lava when the player don't have levitation */
1116             if (!player_ptr->levitation)
1117                 return FALSE;
1118         }
1119     }
1120
1121     return TRUE;
1122 }
1123
1124 /*!
1125  * @brief 地形は開くものであって、かつ開かれているかを返す /
1126  * Attempt to open the given chest at the given location
1127  * @param feat 地形ID
1128  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
1129  */
1130 bool is_open(player_type *player_ptr, FEAT_IDX feat) { return has_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(player_ptr, feat, FF_CLOSE)); }
1131
1132 /*!
1133  * @brief プレイヤーが地形踏破可能かを返す
1134  * @param feature 判定したい地形ID
1135  * @param mode 移動に関するオプションフラグ
1136  * @return 移動可能ならばTRUEを返す
1137  */
1138 bool player_can_enter(player_type *creature_ptr, FEAT_IDX feature, BIT_FLAGS16 mode)
1139 {
1140     feature_type *f_ptr = &f_info[feature];
1141
1142     if (creature_ptr->riding)
1143         return monster_can_cross_terrain(
1144             creature_ptr, feature, &r_info[creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].r_idx], mode | CEM_RIDING);
1145
1146     if (has_flag(f_ptr->flags, FF_PATTERN)) {
1147         if (!(mode & CEM_P_CAN_ENTER_PATTERN))
1148             return FALSE;
1149     }
1150
1151     if (has_flag(f_ptr->flags, FF_CAN_FLY) && creature_ptr->levitation)
1152         return TRUE;
1153     if (has_flag(f_ptr->flags, FF_CAN_SWIM) && creature_ptr->can_swim)
1154         return TRUE;
1155     if (has_flag(f_ptr->flags, FF_CAN_PASS) && has_pass_wall(creature_ptr))
1156         return TRUE;
1157
1158     if (!has_flag(f_ptr->flags, FF_MOVE))
1159         return FALSE;
1160
1161     return TRUE;
1162 }
1163
1164 void place_grid(player_type *player_ptr, grid_type *g_ptr, grid_bold_type gb_type)
1165 {
1166     switch (gb_type) {
1167     case GB_FLOOR: {
1168         g_ptr->feat = feat_ground_type[randint0(100)];
1169         g_ptr->info &= ~(CAVE_MASK);
1170         g_ptr->info |= CAVE_FLOOR;
1171         break;
1172     }
1173     case GB_EXTRA: {
1174         g_ptr->feat = feat_wall_type[randint0(100)];
1175         g_ptr->info &= ~(CAVE_MASK);
1176         g_ptr->info |= CAVE_EXTRA;
1177         break;
1178     }
1179     case GB_EXTRA_PERM: {
1180         // No such grid
1181         return;
1182     }
1183     case GB_INNER: {
1184         g_ptr->feat = feat_wall_inner;
1185         g_ptr->info &= ~(CAVE_MASK);
1186         g_ptr->info |= CAVE_INNER;
1187         break;
1188     }
1189     case GB_INNER_PERM: {
1190         g_ptr->feat = feat_permanent;
1191         g_ptr->info &= ~(CAVE_MASK);
1192         g_ptr->info |= CAVE_INNER;
1193         break;
1194     }
1195     case GB_OUTER: {
1196         g_ptr->feat = feat_wall_outer;
1197         g_ptr->info &= ~(CAVE_MASK);
1198         g_ptr->info |= CAVE_OUTER;
1199         break;
1200     }
1201     case GB_OUTER_NOPERM: {
1202         feature_type *f_ptr = &f_info[feat_wall_outer];
1203         if (permanent_wall(f_ptr)) {
1204             g_ptr->feat = (s16b)feat_state(player_ptr, feat_wall_outer, FF_UNPERM);
1205         } else {
1206             g_ptr->feat = feat_wall_outer;
1207         }
1208
1209         g_ptr->info &= ~(CAVE_MASK);
1210         g_ptr->info |= (CAVE_OUTER | CAVE_VAULT);
1211         break;
1212     }
1213     case GB_SOLID: {
1214         // No such grid
1215         return;
1216     }
1217     case GB_SOLID_PERM: {
1218         g_ptr->feat = feat_permanent;
1219         g_ptr->info &= ~(CAVE_MASK);
1220         g_ptr->info |= CAVE_SOLID;
1221         break;
1222     }
1223     case GB_SOLID_NOPERM: {
1224         // No such grid
1225         return;
1226     }
1227     default:
1228         return;
1229     }
1230
1231     if (g_ptr->m_idx > 0)
1232         delete_monster_idx(player_ptr, g_ptr->m_idx);
1233 }
1234
1235 /*!
1236  * モンスターにより照明が消されている地形か否かを判定する。 / Is this grid "darkened" by monster?
1237  * @param player_ptr プレーヤーへの参照ポインタ
1238  * @param g_ptr グリッドへの参照ポインタ
1239  * @return 照明が消されている地形ならばTRUE
1240  */
1241 bool darkened_grid(player_type *player_ptr, grid_type *g_ptr)
1242 {
1243     return ((g_ptr->info & (CAVE_VIEW | CAVE_LITE | CAVE_MNLT | CAVE_MNDK)) == (CAVE_VIEW | CAVE_MNDK)) && !player_ptr->see_nocto;
1244 }
1245
1246 void place_bold(player_type *player_ptr, POSITION y, POSITION x, grid_bold_type gb_type)
1247 {
1248     floor_type *floor_ptr = player_ptr->current_floor_ptr;
1249     switch (gb_type) {
1250     case GB_FLOOR: {
1251         set_cave_feat(floor_ptr, y, x, feat_ground_type[randint0(100)]);
1252         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1253         add_cave_info(floor_ptr, y, x, CAVE_FLOOR);
1254         break;
1255     }
1256     case GB_EXTRA: {
1257         set_cave_feat(floor_ptr, y, x, feat_wall_type[randint0(100)]);
1258         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1259         add_cave_info(floor_ptr, y, x, CAVE_EXTRA);
1260         break;
1261     }
1262     case GB_EXTRA_PERM: {
1263         set_cave_feat(floor_ptr, y, x, feat_permanent);
1264         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1265         add_cave_info(floor_ptr, y, x, CAVE_EXTRA);
1266         break;
1267     }
1268     case GB_INNER: {
1269         set_cave_feat(floor_ptr, y, x, feat_wall_inner);
1270         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1271         add_cave_info(floor_ptr, y, x, CAVE_INNER);
1272         break;
1273     }
1274     case GB_INNER_PERM: {
1275         set_cave_feat(floor_ptr, y, x, feat_permanent);
1276         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1277         add_cave_info(floor_ptr, y, x, CAVE_INNER);
1278         break;
1279     }
1280     case GB_OUTER: {
1281         set_cave_feat(floor_ptr, y, x, feat_wall_outer);
1282         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1283         add_cave_info(floor_ptr, y, x, CAVE_OUTER);
1284         break;
1285     }
1286     case GB_OUTER_NOPERM: {
1287         feature_type *f_ptr = &f_info[feat_wall_outer];
1288         if (permanent_wall(f_ptr))
1289             set_cave_feat(floor_ptr, y, x, (s16b)feat_state(player_ptr, feat_wall_outer, FF_UNPERM));
1290         else
1291             set_cave_feat(floor_ptr, y, x, feat_wall_outer);
1292         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1293         add_cave_info(floor_ptr, y, x, (CAVE_OUTER | CAVE_VAULT));
1294         break;
1295     }
1296     case GB_SOLID: {
1297         set_cave_feat(floor_ptr, y, x, feat_wall_solid);
1298         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1299         add_cave_info(floor_ptr, y, x, CAVE_SOLID);
1300         break;
1301     }
1302     case GB_SOLID_PERM: {
1303         feature_type *f_ptr = &f_info[feat_wall_solid];
1304         if ((floor_ptr->grid_array[y][x].info & CAVE_VAULT) && permanent_wall(f_ptr))
1305             set_cave_feat(floor_ptr, y, x, feat_state(player_ptr, feat_wall_solid, FF_UNPERM));
1306         else
1307             set_cave_feat(floor_ptr, y, x, feat_wall_solid);
1308         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1309         add_cave_info(floor_ptr, y, x, CAVE_SOLID);
1310         break;
1311     }
1312     case GB_SOLID_NOPERM: {
1313         feature_type *f_ptr = &f_info[feat_wall_solid];
1314         if ((floor_ptr->grid_array[y][x].info & CAVE_VAULT) && permanent_wall(f_ptr))
1315             set_cave_feat(floor_ptr, y, x, feat_state(player_ptr, feat_wall_solid, FF_UNPERM));
1316         else
1317             set_cave_feat(floor_ptr, y, x, feat_wall_solid);
1318         floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1319         add_cave_info(floor_ptr, y, x, CAVE_SOLID);
1320         delete_monster(player_ptr, y, x);
1321     }
1322     default:
1323         return;
1324     }
1325
1326     delete_monster(player_ptr, y, x);
1327 }
1328
1329 void set_cave_feat(floor_type *floor_ptr, POSITION y, POSITION x, FEAT_IDX feature_idx) { floor_ptr->grid_array[y][x].feat = feature_idx; }
1330
1331 /*!
1332  * todo intをenumに変更する
1333  */
1334 void add_cave_info(floor_type *floor_ptr, POSITION y, POSITION x, int cave_mask) { floor_ptr->grid_array[y][x].info |= cave_mask; }
1335
1336 /*
1337  * @brief Get feature mimic from f_info[] (applying "mimic" field)
1338  * @param g_ptr グリッドへの参照ポインタ
1339  * @return 地形情報
1340  */
1341 FEAT_IDX get_feat_mimic(grid_type *g_ptr) { return (f_info[g_ptr->mimic ? g_ptr->mimic : g_ptr->feat].mimic); }
1342
1343 /*!
1344  * @brief プレイヤーの周辺9マスに該当する地形がいくつあるかを返す /
1345  * Attempt to open the given chest at the given location
1346  * @param y 該当する地形の中から1つのY座標を返す参照ポインタ
1347  * @param x 該当する地形の中から1つのX座標を返す参照ポインタ
1348  * @param test 地形条件を判定するための関数ポインタ
1349  * @param under TRUEならばプレイヤーの直下の座標も走査対象にする
1350  * @return 該当する地形の数
1351  * @details Return the number of features around (or under) the character.
1352  * Usually look for doors and floor traps.
1353  */
1354 int count_dt(player_type *creature_ptr, POSITION *y, POSITION *x, bool (*test)(player_type *, FEAT_IDX), bool under)
1355 {
1356     int count = 0;
1357     for (DIRECTION d = 0; d < 9; d++) {
1358         grid_type *g_ptr;
1359         FEAT_IDX feat;
1360         if ((d == 8) && !under)
1361             continue;
1362
1363         POSITION yy = creature_ptr->y + ddy_ddd[d];
1364         POSITION xx = creature_ptr->x + ddx_ddd[d];
1365         g_ptr = &creature_ptr->current_floor_ptr->grid_array[yy][xx];
1366         if (!(g_ptr->info & (CAVE_MARK)))
1367             continue;
1368
1369         feat = get_feat_mimic(g_ptr);
1370         if (!((*test)(creature_ptr, feat)))
1371             continue;
1372
1373         ++count;
1374         *y = yy;
1375         *x = xx;
1376     }
1377
1378     return count;
1379 }
1380
1381 bool feat_uses_special(FEAT_IDX f_idx) { return has_flag(f_info[(f_idx)].flags, FF_SPECIAL); }