OSDN Git Service

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