OSDN Git Service

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