OSDN Git Service

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