OSDN Git Service

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