OSDN Git Service

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