OSDN Git Service

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