4 * @brief グリッドの実装 / low level dungeon routines -BEN-
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
9 * This software may be copied and distributed for educational, research,\n
10 * and not for profit purposes provided that this copyright and statement\n
11 * are included in all such copies. Other copyrights may also apply.\n
13 * Support for Adam Bolt's tileset, lighting and transparency effects\n
14 * by Robert Ruehlmann (rr9@angband.org)\n
16 * 2013 Deskull Doxygen向けのコメント整理\n
26 #include "object-flavor.h"
27 #include "object-hook.h"
29 #include "floor-generate.h"
36 #include "monster-status.h"
37 #include "player-status.h"
38 #include "player-effects.h"
39 #include "player-class.h"
41 #include "view-mainwindow.h"
42 #include "realm-song.h"
44 #define MONSTER_FLOW_DEPTH 32 /*!< 敵のプレイヤーに対する移動道のりの最大値(この値以上は処理を打ち切る) / OPTION: Maximum flow depth when using "MONSTER_FLOW" */
47 * Feature action flags
49 #define FAF_DESTROY 0x01
50 #define FAF_NO_DROP 0x02
51 #define FAF_CRASH_GLASS 0x04
54 * @brief 地形状態フラグテーブル /
55 * The table of features' actions
57 static const byte feature_action_flags[FF_FLAG_MAX] =
69 FAF_CRASH_GLASS, /* BASH */
71 FAF_DESTROY, /* DISARM */
73 FAF_DESTROY | FAF_CRASH_GLASS, /* TUNNEL */
74 0, /* MAY_HAVE_GOLD */
104 FAF_DESTROY | FAF_CRASH_GLASS, /* HURT_ROCK */
159 FAF_DESTROY | FAF_NO_DROP | FAF_CRASH_GLASS, /* HURT_DISI */
172 0, /* TELEPORTABLE */
178 * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
179 * @param creature_ptr 配置したいクリーチャーの参照ポインタ
180 * @return 配置に成功したらTRUEを返す
182 bool new_player_spot(player_type *creature_ptr)
184 POSITION y = 0, x = 0;
185 int max_attempts = 10000;
190 while (max_attempts--)
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);
196 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
198 /* Must be a "naked" floor grid */
199 if (g_ptr->m_idx) continue;
200 if (creature_ptr->current_floor_ptr->dun_level)
202 f_ptr = &f_info[g_ptr->feat];
204 if (max_attempts > 5000) /* Rule 1 */
206 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
210 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
211 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
214 /* Refuse to start on anti-teleport grids in dungeon */
215 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
217 if (!player_can_enter(g_ptr->feat, 0)) continue;
218 if (!in_bounds(creature_ptr->current_floor_ptr, y, x)) continue;
220 /* Refuse to start on anti-teleport grids */
221 if (g_ptr->info & (CAVE_ICKY)) continue;
226 if (max_attempts < 1) /* Should be -1, actually if we failed... */
229 /* Save the new player grid */
237 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
238 * @param y1 基準となるマスのY座標
239 * @param x1 基準となるマスのX座標
241 * @note Assumes "in_bounds(p_ptr->current_floor_ptr, y1, x1)"
243 * XXX XXX This routine currently only counts actual "empty floor"\n
244 * grids which are not in rooms. We might want to also count stairs,\n
245 * open doors, closed doors, etc.
247 static int next_to_corr(POSITION y1, POSITION x1)
254 /* Scan adjacent grids */
255 for (i = 0; i < 4; i++)
259 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
261 /* Skip non floors */
262 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
264 /* Skip non "empty floor" grids */
265 if (!is_floor_grid(g_ptr))
268 /* Skip grids inside rooms */
269 if (g_ptr->info & (CAVE_ROOM)) continue;
271 /* Count these grids */
275 /* Return the number of corridors */
280 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
281 * @param y 判定を行いたいマスのY座標
282 * @param x 判定を行いたいマスのX座標
283 * @return ドアを設置可能ならばTRUEを返す
284 * @note Assumes "in_bounds(p_ptr->current_floor_ptr, y1, x1)"
287 * Assumes "in_bounds(p_ptr->current_floor_ptr, y, x)"\n
289 static bool possible_doorway(POSITION y, POSITION x)
291 /* Count the adjacent corridors */
292 if (next_to_corr(y, x) >= 2)
295 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
296 cave_have_flag_bold(y + 1, x, FF_WALL))
301 /* Check Horizontal */
302 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
303 cave_have_flag_bold(y, x + 1, FF_WALL))
314 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
315 * @param y 設置を行いたいマスのY座標
316 * @param x 設置を行いたいマスのX座標
319 void try_door(POSITION y, POSITION x)
320 { if (!in_bounds(p_ptr->current_floor_ptr, y, x)) return;
323 if (cave_have_flag_bold(y, x, FF_WALL)) return;
325 /* Ignore room grids */
326 if (p_ptr->current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
328 /* Occasional door (if allowed) */
329 if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
332 place_random_door(p_ptr->current_floor_ptr, y, x, FALSE);
337 * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
338 * @param y 配置したい中心マスのY座標
339 * @param x 配置したい中心マスのX座標
343 * Only really called by some of the "vault" routines.
345 void vault_objects(POSITION y, POSITION x, int num)
348 int i = 0, j = y, k = x;
353 /* Attempt to place 'num' objects */
354 for (; num > 0; --num)
356 /* Try up to 11 spots looking for empty space */
357 for (i = 0; i < 11; ++i)
359 /* Pick a random location */
360 while (dummy < SAFE_MAX_ATTEMPTS)
362 j = rand_spread(y, 2);
363 k = rand_spread(x, 3);
365 if (!in_bounds(p_ptr->current_floor_ptr, j, k)) continue;
369 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
371 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
374 /* Require "clean" floor space */
375 g_ptr = &p_ptr->current_floor_ptr->grid_array[j][k];
376 if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
378 if (randint0(100) < 75)
380 place_object(j, k, 0L);
387 /* Placement accomplished */
394 * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
395 * @param y トラップを配置したいマスの中心Y座標
396 * @param x トラップを配置したいマスの中心X座標
397 * @param yd Y方向の配置分散マス数
398 * @param xd X方向の配置分散マス数
401 * Only really called by some of the "vault" routines.
403 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
405 int count = 0, y1 = y, x1 = x;
411 for (count = 0; count <= 5; count++)
414 while (dummy < SAFE_MAX_ATTEMPTS)
416 y1 = rand_spread(y, yd);
417 x1 = rand_spread(x, xd);
419 if (!in_bounds(p_ptr->current_floor_ptr, y1, x1)) continue;
423 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
425 msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
428 /* Require "naked" floor grids */
429 g_ptr = &p_ptr->current_floor_ptr->grid_array[y1][x1];
430 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
433 place_trap(p_ptr->current_floor_ptr, y1, x1);
440 * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
441 * @param y トラップを配置したいマスの中心Y座標
442 * @param x トラップを配置したいマスの中心X座標
443 * @param yd Y方向の配置分散マス数
444 * @param xd X方向の配置分散マス数
445 * @param num 配置したいトラップの数
448 * Only really called by some of the "vault" routines.
450 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
454 for (i = 0; i < num; i++)
456 vault_trap_aux(y, x, yd, xd);
461 * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor. (Includes range checking.)
462 * @param x チェックするマスのX座標
463 * @param y チェックするマスのY座標
464 * @return 床系地形ならばTRUE
466 bool get_is_floor(POSITION x, POSITION y)
468 if (!in_bounds(p_ptr->current_floor_ptr, y, x))
474 /* Do the real check */
475 if (is_floor_bold(p_ptr->current_floor_ptr, y, x)) return (TRUE);
481 * @brief 指定のマスを床地形に変える / Set a square to be floor. (Includes range checking.)
482 * @param x 地形を変えたいマスのX座標
483 * @param y 地形を変えたいマスのY座標
486 void set_floor(POSITION x, POSITION y)
488 if (!in_bounds(p_ptr->current_floor_ptr, y, x))
494 if (p_ptr->current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
496 /* A room border don't touch. */
500 /* Set to be floor if is a wall (don't touch lakes). */
501 if (is_extra_bold(p_ptr->current_floor_ptr, y, x))
502 place_floor_bold(p_ptr->current_floor_ptr, y, x);
506 * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
507 * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
510 void place_bound_perm_wall(grid_type *g_ptr)
512 if (bound_walls_perm)
514 /* Clear boundary mimic */
519 feature_type *f_ptr = &f_info[g_ptr->feat];
521 /* Hack -- Decline boundary walls with known treasure */
522 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
523 !have_flag(f_ptr->flags, FF_SECRET))
524 g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
526 /* Set boundary mimic */
527 g_ptr->mimic = g_ptr->feat;
530 /* Add "solid" perma-wall */
531 place_solid_perm_grid(g_ptr);
535 * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
536 * @param g_ptr マス構造体の参照ポインタ
537 * @return 看破済みの罠があるならTRUEを返す。
539 bool is_known_trap(grid_type *g_ptr)
541 if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
542 is_trap(g_ptr->feat)) return TRUE;
550 * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
551 * @param g_ptr マス構造体の参照ポインタ
552 * @return 隠されたドアがあるならTRUEを返す。
554 bool is_hidden_door(grid_type *g_ptr)
556 if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
557 is_closed_door(g_ptr->feat))
563 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
567 * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
570 * @return 指定された座標に照明がかかっているならTRUEを返す。。
572 bool check_local_illumination(POSITION y, POSITION x)
574 /* Hack -- move towards player */
575 POSITION yy = (y < p_ptr->y) ? (y + 1) : (y > p_ptr->y) ? (y - 1) : y;
576 POSITION xx = (x < p_ptr->x) ? (x + 1) : (x > p_ptr->x) ? (x - 1) : x;
578 /* Check for "local" illumination */
580 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
582 /* Check for "complex" illumination */
583 if ((feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[yy][xx])) &&
584 (p_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
585 (feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[y][xx])) &&
586 (p_ptr->current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
587 (feat_supports_los(get_feat_mimic(&p_ptr->current_floor_ptr->grid_array[yy][x])) &&
588 (p_ptr->current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
594 #else /* COMPLEX_WALL_ILLUMINATION */
596 /* Check for "simple" illumination */
597 return (p_ptr->current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
599 #endif /* COMPLEX_WALL_ILLUMINATION */
603 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
604 #define update_local_illumination_aux(C, Y, X) \
606 if (player_has_los_bold((C), (Y), (X))) \
608 /* Update the monster */ \
609 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); \
611 /* Notice and redraw */ \
612 note_spot((Y), (X)); \
613 lite_spot((Y), (X)); \
618 * @brief 指定された座標の照明状態を更新する / Update "local" illumination
619 * @param creature_ptr 視界元のクリーチャー
624 void update_local_illumination(player_type * creature_ptr, POSITION y, POSITION x)
629 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) return;
631 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
633 if ((y != creature_ptr->y) && (x != creature_ptr->x))
635 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
636 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
637 update_local_illumination_aux(creature_ptr, yy, xx);
638 update_local_illumination_aux(creature_ptr, y, xx);
639 update_local_illumination_aux(creature_ptr, yy, x);
641 else if (x != creature_ptr->x) /* y == creature_ptr->y */
643 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
644 for (i = -1; i <= 1; i++)
647 update_local_illumination_aux(creature_ptr, yy, xx);
650 update_local_illumination_aux(creature_ptr, yy, x);
652 update_local_illumination_aux(creature_ptr, yy, x);
654 else if (y != creature_ptr->y) /* x == creature_ptr->x */
656 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
657 for (i = -1; i <= 1; i++)
660 update_local_illumination_aux(creature_ptr, yy, xx);
663 update_local_illumination_aux(creature_ptr, y, xx);
665 update_local_illumination_aux(creature_ptr, y, xx);
667 else /* Player's grid */
669 for (i = 0; i < 8; i++)
673 update_local_illumination_aux(creature_ptr, yy, xx);
677 #else /* COMPLEX_WALL_ILLUMINATION */
679 if ((y != creature_ptr->y) && (x != creature_ptr->x))
681 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
682 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
683 update_local_illumination_aux(creature_ptr, yy, xx);
685 else if (x != creature_ptr->x) /* y == creature_ptr->y */
687 xx = (x < creature_ptr->x) ? (x - 1) : (x + 1);
688 for (i = -1; i <= 1; i++)
691 update_local_illumination_aux(creature_ptr, yy, xx);
694 else if (y != creature_ptr->y) /* x == creature_ptr->x */
696 yy = (y < creature_ptr->y) ? (y - 1) : (y + 1);
697 for (i = -1; i <= 1; i++)
700 update_local_illumination_aux(creature_ptr, yy, xx);
703 else /* Player's grid */
705 for (i = 0; i < 8; i++)
709 update_local_illumination_aux(creature_ptr, yy, xx);
713 #endif /* COMPLEX_WALL_ILLUMINATION */
718 * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
719 * @return 視覚に収められていないならTRUEを返す
720 * @details player_can_see_bold()関数の返り値の否定を返している。
724 return (!player_can_see_bold(p_ptr->y, p_ptr->x));
728 * Place an attr/char pair at the given map coordinate, if legal.
730 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
732 /* Only do "legal" locations */
733 if (panel_contains(y, x))
735 /* Hack -- fake monochrome */
738 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
739 else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
740 else if (p_ptr->wraith_form) a = TERM_L_DARK;
743 /* Draw the char using the attr */
744 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
750 * Memorize interesting viewable object/features in the given grid
752 * This function should only be called on "legal" grids.
754 * This function will memorize the object and/or feature in the given
755 * grid, if they are (1) viewable and (2) interesting. Note that all
756 * objects are interesting, all terrain features except floors (and
757 * invisible traps) are interesting, and floors (and invisible traps)
758 * are interesting sometimes (depending on various options involving
759 * the illumination of floor grids).
761 * The automatic memorization of all objects and non-floor terrain
762 * features as soon as they are displayed allows incredible amounts
763 * of optimization in various places, especially "map_info()".
765 * Note that the memorization of objects is completely separate from
766 * the memorization of terrain features, preventing annoying floor
767 * memorization when a detected object is picked up from a dark floor,
768 * and object memorization when an object is dropped into a floor grid
769 * which is memorized but out-of-sight.
771 * This function should be called every time the "memorization" of
772 * a grid (or the object in a grid) is called into question, such
773 * as when an object is created in a grid, when a terrain feature
774 * "changes" from "floor" to "non-floor", when any grid becomes
775 * "illuminated" or "viewable", and when a "floor" grid becomes
778 * Note the relatively efficient use of this function by the various
779 * "update_view()" and "update_lite()" calls, to allow objects and
780 * terrain features to be memorized (and drawn) whenever they become
781 * viewable or illuminated in any way, but not when they "maintain"
782 * or "lose" their previous viewability or illumination.
784 * Note the butchered "internal" version of "player_can_see_bold()",
785 * optimized primarily for the most common cases, that is, for the
786 * non-marked floor grids.
788 void note_spot(POSITION y, POSITION x)
790 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
791 OBJECT_IDX this_o_idx, next_o_idx = 0;
793 /* Blind players see nothing */
794 if (p_ptr->blind) return;
796 /* Analyze non-torch-lit grids */
797 if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
799 /* Require line of sight to the grid */
800 if (!(g_ptr->info & (CAVE_VIEW))) return;
802 /* Require "perma-lite" of the grid */
803 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
806 if (!p_ptr->see_nocto) return;
811 /* Hack -- memorize objects */
812 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
814 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
815 next_o_idx = o_ptr->next_o_idx;
817 /* Memorize objects */
818 o_ptr->marked |= OM_FOUND;
822 /* Hack -- memorize grids */
823 if (!(g_ptr->info & (CAVE_MARK)))
825 /* Feature code (applying "mimic" field) */
826 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
828 /* Memorize some "boring" grids */
829 if (!have_flag(f_ptr->flags, FF_REMEMBER))
831 /* Option -- memorize all torch-lit floors */
832 if (view_torch_grids &&
833 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
835 g_ptr->info |= (CAVE_MARK);
838 /* Option -- memorize all perma-lit floors */
839 else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
841 g_ptr->info |= (CAVE_MARK);
845 /* Memorize normal grids */
846 else if (have_flag(f_ptr->flags, FF_LOS))
848 g_ptr->info |= (CAVE_MARK);
851 /* Memorize torch-lit walls */
852 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
854 g_ptr->info |= (CAVE_MARK);
857 /* Memorize walls seen by noctovision of Ninja */
858 else if (p_ptr->see_nocto)
860 g_ptr->info |= (CAVE_MARK);
863 /* Memorize certain non-torch-lit wall grids */
864 else if (check_local_illumination(y, x))
866 g_ptr->info |= (CAVE_MARK);
870 /* Memorize terrain of the grid */
871 g_ptr->info |= (CAVE_KNOWN);
875 * Redraw (on the screen) a given MAP location
877 * This function should only be called on "legal" grids
879 void lite_spot(POSITION y, POSITION x)
881 /* Redraw if on screen */
882 if (panel_contains(y, x) && in_bounds2(p_ptr->current_floor_ptr, y, x))
889 map_info(y, x, &a, &c, &ta, &tc);
891 /* Hack -- fake monochrome */
894 if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
895 else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
896 else if (p_ptr->wraith_form) a = TERM_L_DARK;
899 /* Hack -- Queue it */
900 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
902 /* Update sub-windows */
903 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
908 * Some comments on the grid flags. -BEN-
911 * One of the major bottlenecks in previous versions of Angband was in
912 * the calculation of "line of sight" from the player to various grids,
913 * such as monsters. This was such a nasty bottleneck that a lot of
914 * silly things were done to reduce the dependancy on "line of sight",
915 * for example, you could not "see" any grids in a lit room until you
916 * actually entered the room, and there were all kinds of bizarre grid
917 * flags to enable this behavior. This is also why the "call light"
918 * spells always lit an entire room.
920 * The code below provides functions to calculate the "field of view"
921 * for the player, which, once calculated, provides extremely fast
922 * calculation of "line of sight from the player", and to calculate
923 * the "field of torch lite", which, again, once calculated, provides
924 * extremely fast calculation of "which grids are lit by the player's
925 * lite source". In addition to marking grids as "GRID_VIEW" and/or
926 * "GRID_LITE", as appropriate, these functions maintain an array for
927 * each of these two flags, each array containing the locations of all
928 * of the grids marked with the appropriate flag, which can be used to
929 * very quickly scan through all of the grids in a given set.
931 * To allow more "semantically valid" field of view semantics, whenever
932 * the field of view (or the set of torch lit grids) changes, all of the
933 * grids in the field of view (or the set of torch lit grids) are "drawn"
934 * so that changes in the world will become apparent as soon as possible.
935 * This has been optimized so that only grids which actually "change" are
936 * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
937 * of the grids which are entering or leaving the relevent set of grids.
939 * These new methods are so efficient that the old nasty code was removed.
941 * Note that there is no reason to "update" the "viewable space" unless
942 * the player "moves", or walls/doors are created/destroyed, and there
943 * is no reason to "update" the "torch lit grids" unless the field of
944 * view changes, or the "light radius" changes. This means that when
945 * the player is resting, or digging, or doing anything that does not
946 * involve movement or changing the state of the dungeon, there is no
947 * need to update the "view" or the "lite" regions, which is nice.
949 * Note that the calls to the nasty "los()" function have been reduced
950 * to a bare minimum by the use of the new "field of view" calculations.
952 * I wouldn't be surprised if slight modifications to the "update_view()"
953 * function would allow us to determine "reverse line-of-sight" as well
954 * as "normal line-of-sight", which would allow monsters to use a more
955 * "correct" calculation to determine if they can "see" the player. For
956 * now, monsters simply "cheat" somewhat and assume that if the player
957 * has "line of sight" to the monster, then the monster can "pretend"
958 * that it has "line of sight" to the player.
961 * The "update_lite()" function maintains the "CAVE_LITE" flag for each
962 * grid and maintains an array of all "CAVE_LITE" grids.
964 * This set of grids is the complete set of all grids which are lit by
965 * the players light source, which allows the "player_can_see_bold()"
966 * function to work very quickly.
968 * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
969 * fact, the player (unless blind) can always "see" all grids which are
970 * marked as "CAVE_LITE", unless they are "off screen".
973 * The "update_view()" function maintains the "CAVE_VIEW" flag for each
974 * grid and maintains an array of all "CAVE_VIEW" grids.
976 * This set of grids is the complete set of all grids within line of sight
977 * of the player, allowing the "player_has_los_bold(p_ptr, )" macro to work very
981 * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
982 * temporary internal flag to mark those grids which are not only in view,
983 * but which are also "easily" in line of sight of the player. This flag
984 * is always cleared when we are done.
987 * The current "update_lite()" and "update_view()" algorithms use the
988 * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
989 * to keep track of which grids were previously marked as "CAVE_LITE" or
990 * "CAVE_VIEW", which allows us to optimize the "screen updates".
992 * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
993 * for various other purposes, such as spreading lite or darkness during
994 * "lite_room()" / "unlite_room()", and for calculating monster flow.
997 * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
998 * in some way permanently lit. However, for the player to "see" anything
999 * in the grid, as determined by "player_can_see()", the player must not be
1000 * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
1001 * grids, even if marked as "perma lit", are only illuminated if they touch
1002 * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
1005 * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
1006 * that even if the player cannot "see" the grid, he "knows" the terrain in
1007 * that grid. This is used to "remember" walls/doors/stairs/floors when they
1008 * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite(p_ptr, )",
1009 * or when one of the "memorize floor grids" options induces memorization.
1011 * Objects are "memorized" in a different way, using a special "marked" flag
1012 * on the object itself, which is set when an object is observed or detected.
1015 * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
1016 * and should be illuminated by "lite room" and "darkness" spells.
1019 * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
1020 * and should be unavailable for "teleportation" destinations.
1023 * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
1024 * which is observed, and the "view_torch_grids" allows the player to memorize
1025 * every torch-lit grid. The player will always memorize important walls,
1026 * doors, stairs, and other terrain features, as well as any "detected" grids.
1028 * Note that the new "update_view()" method allows, among other things, a room
1029 * to be "partially" seen as the player approaches it, with a growing cone of
1030 * floor appearing as the player gets closer to the door. Also, by not turning
1031 * on the "memorize perma-lit grids" option, the player will only "see" those
1032 * floor grids which are actually in line of sight.
1034 * And my favorite "plus" is that you can now use a special option to draw the
1035 * "floors" in the "viewable region" brightly (actually, to draw the *other*
1036 * grids dimly), providing a "pretty" effect as the player runs around, and
1037 * to efficiently display the "torch lite" in a special color.
1040 * Some comments on the "update_view()" algorithm...
1042 * The algorithm is very fast, since it spreads "obvious" grids very quickly,
1043 * and only has to call "los()" on the borderline cases. The major axes/diags
1044 * even terminate early when they hit walls. I need to find a quick way
1045 * to "terminate" the other scans.
1047 * Note that in the worst case (a big empty area with say 5% scattered walls),
1048 * each of the 1500 or so nearby grids is checked once, most of them getting
1049 * an "instant" rating, and only a small portion requiring a call to "los()".
1051 * The only time that the algorithm appears to be "noticeably" too slow is
1052 * when running, and this is usually only important in town, since the town
1053 * provides about the worst scenario possible, with large open regions and
1054 * a few scattered obstructions. There is a special "efficiency" option to
1055 * allow the player to reduce his field of view in town, if needed.
1057 * In the "best" case (say, a normal stretch of corridor), the algorithm
1058 * makes one check for each viewable grid, and makes no calls to "los()".
1059 * So running in corridors is very fast, and if a lot of monsters are
1060 * nearby, it is much faster than the old methods.
1062 * Note that resting, most normal commands, and several forms of running,
1063 * plus all commands executed near large groups of monsters, are strictly
1064 * more efficient with "update_view()" that with the old "compute los() on
1065 * demand" method, primarily because once the "field of view" has been
1066 * calculated, it does not have to be recalculated until the player moves
1067 * (or a wall or door is created or destroyed).
1069 * Note that we no longer have to do as many "los()" checks, since once the
1070 * "view" region has been built, very few things cause it to be "changed"
1071 * (player movement, and the opening/closing of doors, changes in wall status).
1072 * Note that door/wall changes are only relevant when the door/wall itself is
1073 * in the "view" region.
1075 * The algorithm seems to only call "los()" from zero to ten times, usually
1076 * only when coming down a corridor into a room, or standing in a room, just
1077 * misaligned with a corridor. So if, say, there are five "nearby" monsters,
1078 * we will be reducing the calls to "los()".
1080 * I am thinking in terms of an algorithm that "walks" from the central point
1081 * out to the maximal "distance", at each point, determining the "view" code
1082 * (above). For each grid not on a major axis or diagonal, the "view" code
1083 * depends on the "cave_los_bold(p_ptr->current_floor_ptr, )" and "view" of exactly two other grids
1084 * (the one along the nearest diagonal, and the one next to that one, see
1085 * "update_view_aux()"...).
1087 * We "memorize" the viewable space array, so that at the cost of under 3000
1088 * bytes, we reduce the time taken by "forget_view()" to one assignment for
1089 * each grid actually in the "viewable space". And for another 3000 bytes,
1090 * we prevent "erase + redraw" ineffiencies via the "seen" set. These bytes
1091 * are also used by other routines, thus reducing the cost to almost nothing.
1093 * A similar thing is done for "forget_lite()" in which case the savings are
1094 * much less, but save us from doing bizarre maintenance checking.
1096 * In the worst "normal" case (in the middle of the town), the reachable space
1097 * actually reaches to more than half of the largest possible "circle" of view,
1098 * or about 800 grids, and in the worse case (in the middle of a dungeon level
1099 * where all the walls have been removed), the reachable space actually reaches
1100 * the theoretical maximum size of just under 1500 grids.
1102 * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
1103 * If G1 is lite, G is lite. Else if G2 is lite, G is half. Else if G1 and G2
1104 * are both half, G is half. Else G is dark. It only takes 2 (or 4) bits to
1105 * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
1106 * entire possible space (including initialization) in one step per grid. If
1107 * we do the "clearing" as a separate step (and use an array of "view" grids),
1108 * then the clearing will take as many steps as grids that were viewed, and the
1109 * algorithm will be able to "stop" scanning at various points.
1110 * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
1114 * Mega-Hack -- Delayed visual update
1115 * Only used if update_view(), update_lite() or update_mon_lite() was called
1117 void delayed_visual_update(void)
1123 /* Update needed grids */
1124 for (i = 0; i < p_ptr->current_floor_ptr->redraw_n; i++)
1126 y = p_ptr->current_floor_ptr->redraw_y[i];
1127 x = p_ptr->current_floor_ptr->redraw_x[i];
1128 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1130 /* Update only needed grids (prevent multiple updating) */
1131 if (!(g_ptr->info & CAVE_REDRAW)) continue;
1133 /* If required, note */
1134 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
1138 /* Hack -- Visual update of monster on this grid */
1139 if (g_ptr->m_idx) update_monster(p_ptr, g_ptr->m_idx, FALSE);
1141 /* No longer in the array */
1142 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
1146 p_ptr->current_floor_ptr->redraw_n = 0;
1151 * Hack - speed up the update_flow algorithm by only doing
1152 * it everytime the player moves out of LOS of the last
1155 static POSITION flow_x = 0;
1156 static POSITION flow_y = 0;
1161 * Hack -- fill in the "cost" field of every grid that the player
1162 * can "reach" with the number of steps needed to reach that grid.
1163 * This also yields the "distance" of the player from every grid.
1165 * In addition, mark the "when" of the grids that can reach
1166 * the player with the incremented value of "flow_n".
1168 * Hack -- use the "seen" array as a "circular queue".
1170 * We do not need a priority queue because the cost from grid
1171 * to grid is always "one" and we process them in order.
1173 void update_flow(void)
1180 /* Paranoia -- make sure the array is empty */
1181 if (tmp_pos.n) return;
1183 /* The last way-point is on the map */
1184 if (p_ptr->running && in_bounds(p_ptr->current_floor_ptr, flow_y, flow_x))
1186 /* The way point is in sight - do not update. (Speedup) */
1187 if (p_ptr->current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
1190 /* Erase all of the current flow information */
1191 for (y = 0; y < p_ptr->current_floor_ptr->height; y++)
1193 for (x = 0; x < p_ptr->current_floor_ptr->width; x++)
1195 p_ptr->current_floor_ptr->grid_array[y][x].cost = 0;
1196 p_ptr->current_floor_ptr->grid_array[y][x].dist = 0;
1200 /* Save player position */
1204 /* Add the player's grid to the queue */
1205 tmp_pos.y[0] = p_ptr->y;
1206 tmp_pos.x[0] = p_ptr->x;
1208 /* Now process the queue */
1209 while (flow_head != flow_tail)
1213 /* Extract the next entry */
1214 ty = tmp_pos.y[flow_tail];
1215 tx = tmp_pos.x[flow_tail];
1217 /* Forget that entry */
1218 if (++flow_tail == TEMP_MAX) flow_tail = 0;
1220 /* Add the "children" */
1221 for (d = 0; d < 8; d++)
1223 int old_head = flow_head;
1224 byte_hack m = p_ptr->current_floor_ptr->grid_array[ty][tx].cost + 1;
1225 byte_hack n = p_ptr->current_floor_ptr->grid_array[ty][tx].dist + 1;
1228 /* Child location */
1229 y = ty + ddy_ddd[d];
1230 x = tx + ddx_ddd[d];
1232 /* Ignore player's grid */
1233 if (player_bold(p_ptr, y, x)) continue;
1235 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1237 if (is_closed_door(g_ptr->feat)) m += 3;
1239 /* Ignore "pre-stamped" entries */
1240 if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
1242 /* Ignore "walls" and "rubble" */
1243 if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1245 /* Save the flow cost */
1246 if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
1247 if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
1249 /* Hack -- limit flow depth */
1250 if (n == MONSTER_FLOW_DEPTH) continue;
1252 /* Enqueue that entry */
1253 tmp_pos.y[flow_head] = y;
1254 tmp_pos.x[flow_head] = x;
1256 /* Advance the queue */
1257 if (++flow_head == TEMP_MAX) flow_head = 0;
1259 /* Hack -- notice overflow by forgetting new entry */
1260 if (flow_head == flow_tail) flow_head = old_head;
1266 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
1268 feature_type *f_ptr = &f_info[newfeat];
1270 if (have_flag(f_ptr->flags, FF_CONVERT))
1272 switch (f_ptr->subtype)
1274 case CONVERT_TYPE_FLOOR:
1275 return feat_ground_type[randint0(100)];
1276 case CONVERT_TYPE_WALL:
1277 return feat_wall_type[randint0(100)];
1278 case CONVERT_TYPE_INNER:
1279 return feat_wall_inner;
1280 case CONVERT_TYPE_OUTER:
1281 return feat_wall_outer;
1282 case CONVERT_TYPE_SOLID:
1283 return feat_wall_solid;
1284 case CONVERT_TYPE_STREAM1:
1285 return d_info[p_ptr->dungeon_idx].stream1;
1286 case CONVERT_TYPE_STREAM2:
1287 return d_info[p_ptr->dungeon_idx].stream2;
1292 else return newfeat;
1297 * Take a feature, determine what that feature becomes
1298 * through applying the given action.
1300 FEAT_IDX feat_state(FEAT_IDX feat, int action)
1302 feature_type *f_ptr = &f_info[feat];
1305 /* Get the new feature */
1306 for (i = 0; i < MAX_FEAT_STATES; i++)
1308 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
1311 if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
1313 return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
1317 * Takes a location and action and changes the feature at that
1318 * location through applying the given action.
1320 void cave_alter_feat(POSITION y, POSITION x, int action)
1322 /* Set old feature */
1323 FEAT_IDX oldfeat = p_ptr->current_floor_ptr->grid_array[y][x].feat;
1325 /* Get the new feat */
1326 FEAT_IDX newfeat = feat_state(oldfeat, action);
1329 if (newfeat == oldfeat) return;
1331 /* Set the new feature */
1332 cave_set_feat(p_ptr->current_floor_ptr, y, x, newfeat);
1334 if (!(feature_action_flags[action] & FAF_NO_DROP))
1336 feature_type *old_f_ptr = &f_info[oldfeat];
1337 feature_type *f_ptr = &f_info[newfeat];
1341 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
1343 /* Place some gold */
1349 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - p_ptr->current_floor_ptr->dun_level / 2)))
1352 place_object(y, x, 0L);
1356 if (found && current_world_ptr->character_dungeon && player_can_see_bold(y, x))
1358 msg_print(_("何かを発見した!", "You have found something!"));
1362 if (feature_action_flags[action] & FAF_CRASH_GLASS)
1364 feature_type *old_f_ptr = &f_info[oldfeat];
1366 if (have_flag(old_f_ptr->flags, FF_GLASS) && current_world_ptr->character_dungeon)
1368 project(p_ptr, PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(p_ptr->current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
1369 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1375 /* Remove a mirror */
1376 void remove_mirror(POSITION y, POSITION x)
1378 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1380 /* Remove the mirror */
1381 g_ptr->info &= ~(CAVE_OBJECT);
1384 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
1386 g_ptr->info &= ~(CAVE_GLOW);
1387 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1388 if (g_ptr->m_idx) update_monster(p_ptr, g_ptr->m_idx, FALSE);
1390 update_local_illumination(p_ptr, y, x);
1400 * Return TRUE if there is a mirror on the grid.
1402 bool is_mirror_grid(grid_type *g_ptr)
1404 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1412 * Return TRUE if there is a mirror on the grid.
1414 bool is_glyph_grid(grid_type *g_ptr)
1416 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1424 * Return TRUE if there is a mirror on the grid.
1426 bool is_explosive_rune_grid(grid_type *g_ptr)
1428 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
1435 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
1436 * @param m_idx モンスターID
1440 * @return テレポート先として妥当ならばtrue
1442 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
1444 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1445 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1446 feature_type *f_ptr = &f_info[g_ptr->feat];
1448 /* Require "teleportable" space */
1449 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1451 if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
1452 if (player_bold(p_ptr, y, x)) return FALSE;
1454 /* Hack -- no teleport onto glyph of warding */
1455 if (is_glyph_grid(g_ptr)) return FALSE;
1456 if (is_explosive_rune_grid(g_ptr)) return FALSE;
1458 if (!(mode & TELEPORT_PASSIVE))
1460 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
1467 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
1471 * @return テレポート先として妥当ならばtrue
1473 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
1475 grid_type *g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
1476 feature_type *f_ptr = &f_info[g_ptr->feat];
1478 /* Require "teleportable" space */
1479 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
1481 /* No magical teleporting into vaults and such */
1482 if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
1484 if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
1486 /* don't teleport on a trap. */
1487 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
1489 if (!(mode & TELEPORT_PASSIVE))
1491 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
1493 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
1495 if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
1498 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
1500 /* Always forbid deep lava */
1501 if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
1503 /* Forbid shallow lava when the player don't have levitation */
1504 if (!p_ptr->levitation) return FALSE;
1513 * @brief 地形は開くものであって、かつ開かれているかを返す /
1514 * Attempt to open the given chest at the given location
1516 * @return 開いた地形である場合TRUEを返す / Return TRUE if the given feature is an open door
1518 bool is_open(FEAT_IDX feat)
1520 return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
1524 * @brief プレイヤーが地形踏破可能かを返す
1525 * @param feature 判定したい地形ID
1526 * @param mode 移動に関するオプションフラグ
1527 * @return 移動可能ならばTRUEを返す
1529 bool player_can_enter(FEAT_IDX feature, BIT_FLAGS16 mode)
1531 feature_type *f_ptr = &f_info[feature];
1533 if (p_ptr->riding) return monster_can_cross_terrain(feature, &r_info[p_ptr->current_floor_ptr->m_list[p_ptr->riding].r_idx], mode | CEM_RIDING);
1535 if (have_flag(f_ptr->flags, FF_PATTERN))
1537 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
1540 if (have_flag(f_ptr->flags, FF_CAN_FLY) && p_ptr->levitation) return TRUE;
1541 if (have_flag(f_ptr->flags, FF_CAN_SWIM) && p_ptr->can_swim) return TRUE;
1542 if (have_flag(f_ptr->flags, FF_CAN_PASS) && p_ptr->pass_wall) return TRUE;
1544 if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;