OSDN Git Service

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