OSDN Git Service

[Refactor] #37353 rooms, grid, generate間のドア処理整理。 / Refactor between door process...
[hengband/hengband.git] / src / grid.c
index a782ffc..9cd60cf 100644 (file)
@@ -1,14 +1,15 @@
-/*
- * File: grid.c
- * Purpose: low-level dungeon creation primitives
- */
-
-/*
- * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
- *
- * This software may be copied and distributed for educational, research, and
- * not for profit purposes provided that this copyright and statement are
- * included in all such copies.
+/*!
+ * @file grid.c
+ * @brief ダンジョンの生成処理の基幹部分 / low-level dungeon creation primitives
+ * @date 2014/01/04
+ * @author
+ * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
+ *\n
+ * This software may be copied and distributed for educational, research,\n
+ * and not for profit purposes provided that this copyright and statement\n
+ * are included in all such copies.  Other copyrights may also apply.\n
+ * \n
+ * 2014 Deskull Doxygen向けのコメント整理\n
  */
 
 #include "angband.h"
 #include "grid.h"
 
 
-/*
- * Returns random co-ordinates for player/monster/object
+/*!
+ * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
+ * @return 配置に成功したらTRUEを返す
  */
 bool new_player_spot(void)
 {
-       int     y, x;
-       int max_attempts = 5000;
+       POSITION y = 0, x = 0;
+       int max_attempts = 10000;
 
        cave_type *c_ptr;
+       feature_type *f_ptr;
 
        /* Place the player */
        while (max_attempts--)
        {
                /* Pick a legal spot */
-               y = rand_range(1, cur_hgt - 2);
-               x = rand_range(1, cur_wid - 2);
+               y = (POSITION)rand_range(1, cur_hgt - 2);
+               x = (POSITION)rand_range(1, cur_wid - 2);
 
                c_ptr = &cave[y][x];
 
                /* Must be a "naked" floor grid */
-               if (!cave_clean_bold(y, x) || c_ptr->m_idx) continue;
-               if (!in_bounds(y,x)) continue;
+               if (c_ptr->m_idx) continue;
+               if (dun_level)
+               {
+                       f_ptr = &f_info[c_ptr->feat];
+
+                       if (max_attempts > 5000) /* Rule 1 */
+                       {
+                               if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
+                       }
+                       else /* Rule 2 */
+                       {
+                               if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
+                               if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
+                       }
+
+                       /* Refuse to start on anti-teleport grids in dungeon */
+                       if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
+               }
+               if (!player_can_enter(c_ptr->feat, 0)) continue;
+               if (!in_bounds(y, x)) continue;
 
                /* Refuse to start on anti-teleport grids */
                if (c_ptr->info & (CAVE_ICKY)) continue;
@@ -50,15 +71,19 @@ bool new_player_spot(void)
                return FALSE;
 
        /* Save the new player grid */
-       py = y;
-       px = x;
+       p_ptr->y = y;
+       p_ptr->x = x;
 
        return TRUE;
 }
 
 
-/*
- * Place an up/down staircase at given location
+
+/*!
+ * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
+ * @param y 配置を試みたいマスのY座標
+ * @param x 配置を試みたいマスのX座標
+ * @return なし
  */
 void place_random_stairs(int y, int x)
 {
@@ -103,13 +128,21 @@ void place_random_stairs(int y, int x)
                place_down_stairs(y, x);
 }
 
-
-/*
- * Place a random type of door at the given location
+/*!
+ * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
+ * @param y ドアの配置を試みたいマスのY座標
+ * @param x ドアの配置を試みたいマスのX座標
+ * @param room 部屋に接している場合向けのドア生成か否か
+ * @return なし
  */
-void place_random_door(int y, int x)
+void place_random_door(int y, int x, bool room)
 {
-       int tmp;
+       int tmp, type;
+       s16b feat = feat_none;
+       cave_type *c_ptr = &cave[y][x];
+
+       /* Initialize mimic info */
+       c_ptr->mimic = 0;
 
        if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
        {
@@ -117,6 +150,10 @@ void place_random_door(int y, int x)
                return;
        }
 
+       type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
+               one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
+               ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
+
        /* Choose an object */
        tmp = randint0(1000);
 
@@ -124,39 +161,68 @@ void place_random_door(int y, int x)
        if (tmp < 300)
        {
                /* Create open door */
-               set_cave_feat(y, x, FEAT_OPEN);
+               feat = feat_door[type].open;
        }
 
        /* Broken doors (100/1000) */
        else if (tmp < 400)
        {
                /* Create broken door */
-               set_cave_feat(y, x, FEAT_BROKEN);
+               feat = feat_door[type].broken;
        }
 
        /* Secret doors (200/1000) */
        else if (tmp < 600)
        {
-                cave_type *c_ptr = &cave[y][x];
-
                /* Create secret door */
-                place_closed_door(y, x);
+               place_closed_door(y, x, type);
+
+               if (type != DOOR_CURTAIN)
+               {
+                       /* Hide. If on the edge of room, use outer wall. */
+                       c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
 
-                /* Hide */
-                c_ptr->mimic = fill_type[randint0(100)];
+                       /* Floor type terrain cannot hide a door */
+                       if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
+                       {
+                               if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
+                               {
+                                       c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
+                               }
+                               c_ptr->mimic = 0;
+                       }
+               }
        }
 
        /* Closed, locked, or stuck doors (400/1000) */
-       else place_closed_door(y, x);
-}
+       else place_closed_door(y, x, type);
 
+       if (tmp < 400)
+       {
+               if (feat != feat_none)
+               {
+                       set_cave_feat(y, x, feat);
+               }
+               else
+               {
+                       place_floor_bold(y, x);
+               }
+       }
 
-/*
- * Place a random type of normal door at the given location.
+       delete_monster(y, x);
+}
+
+/*!
+ * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
+ * @param y ドアの配置を試みたいマスのY座標
+ * @param x ドアの配置を試みたいマスのX座標
+ * @param type ドアの地形ID
+ * @return なし
  */
-void place_closed_door(int y, int x)
+void place_closed_door(int y, int x, int type)
 {
        int tmp;
+       s16b feat = feat_none;
 
        if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
        {
@@ -171,27 +237,270 @@ void place_closed_door(int y, int x)
        if (tmp < 300)
        {
                /* Create closed door */
-               cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
+               feat = feat_door[type].closed;
        }
 
        /* Locked doors (99/400) */
        else if (tmp < 399)
        {
                /* Create locked door */
-               cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
+               feat = feat_locked_door_random(type);
        }
 
        /* Stuck doors (1/400) */
        else
        {
                /* Create jammed door */
-               cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
+               feat = feat_jammed_door_random(type);
+       }
+
+       if (feat != feat_none)
+       {
+               cave_set_feat(y, x, feat);
+
+               /* Now it is not floor */
+               cave[y][x].info &= ~(CAVE_MASK);
+       }
+       else
+       {
+               place_floor_bold(y, x);
+       }
+}
+
+/*!
+* @brief 鍵のかかったドアを配置する
+* @param y 配置したいフロアのY座標
+* @param x 配置したいフロアのX座標
+* @return なし
+*/
+void place_locked_door(int y, int x)
+{
+       if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
+       {
+               place_floor_bold(y, x);
+       }
+       else
+       {
+               set_cave_feat(y, x, feat_locked_door_random((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
+               cave[y][x].info &= ~(CAVE_FLOOR);
+               delete_monster(y, x);
        }
 }
 
 
+/*!
+* @brief 隠しドアを配置する
+* @param y 配置したいフロアのY座標
+* @param x 配置したいフロアのX座標
+* @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
+* @return なし
+*/
+void place_secret_door(int y, int x, int type)
+{
+       if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
+       {
+               place_floor_bold(y, x);
+       }
+       else
+       {
+               cave_type *c_ptr = &cave[y][x];
+
+               if (type == DOOR_DEFAULT)
+               {
+                       type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
+                               one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
+                               ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
+               }
+
+               /* Create secret door */
+               place_closed_door(y, x, type);
+
+               if (type != DOOR_CURTAIN)
+               {
+                       /* Hide by inner wall because this is used in rooms only */
+                       c_ptr->mimic = feat_wall_inner;
+
+                       /* Floor type terrain cannot hide a door */
+                       if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
+                       {
+                               if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
+                               {
+                                       c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
+                               }
+                               c_ptr->mimic = 0;
+                       }
+               }
+
+               c_ptr->info &= ~(CAVE_FLOOR);
+               delete_monster(y, x);
+       }
+}
+
 /*
- * Make an empty square floor, for the middle of rooms
+ * Routine used by the random vault creators to add a door to a location
+ * Note that range checking has to be done in the calling routine.
+ *
+ * The doors must be INSIDE the allocated region.
+ */
+void add_door(int x, int y)
+{
+       /* Need to have a wall in the center square */
+       if (!is_outer_bold(y, x)) return;
+
+       /* look at:
+       *  x#x
+       *  .#.
+       *  x#x
+       *
+       *  where x=don't care
+       *  .=floor, #=wall
+       */
+
+       if (is_floor_bold(y - 1, x) && is_floor_bold(y + 1, x) &&
+               (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
+       {
+               /* secret door */
+               place_secret_door(y, x, DOOR_DEFAULT);
+
+               /* set boundarys so don't get wide doors */
+               place_solid_bold(y, x - 1);
+               place_solid_bold(y, x + 1);
+       }
+
+
+       /* look at:
+       *  x#x
+       *  .#.
+       *  x#x
+       *
+       *  where x = don't care
+       *  .=floor, #=wall
+       */
+       if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
+               is_floor_bold(y, x - 1) && is_floor_bold(y, x + 1))
+       {
+               /* secret door */
+               place_secret_door(y, x, DOOR_DEFAULT);
+
+               /* set boundarys so don't get wide doors */
+               place_solid_bold(y - 1, x);
+               place_solid_bold(y + 1, x);
+       }
+}
+
+/*!
+* @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
+* @param y1 基準となるマスのY座標
+* @param x1 基準となるマスのX座標
+* @return 通路の数
+* @note Assumes "in_bounds(y1, x1)"
+* @details
+* XXX XXX This routine currently only counts actual "empty floor"\n
+* grids which are not in rooms.  We might want to also count stairs,\n
+* open doors, closed doors, etc.
+*/
+static int next_to_corr(int y1, int x1)
+{
+       int i, y, x, k = 0;
+
+       cave_type *c_ptr;
+
+       /* Scan adjacent grids */
+       for (i = 0; i < 4; i++)
+       {
+               /* Extract the location */
+               y = y1 + ddy_ddd[i];
+               x = x1 + ddx_ddd[i];
+
+               /* Access the grid */
+               c_ptr = &cave[y][x];
+
+               /* Skip non floors */
+               if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
+
+               /* Skip non "empty floor" grids */
+               if (!is_floor_grid(c_ptr))
+                       continue;
+
+               /* Skip grids inside rooms */
+               if (c_ptr->info & (CAVE_ROOM)) continue;
+
+               /* Count these grids */
+               k++;
+       }
+
+       /* Return the number of corridors */
+       return (k);
+}
+
+/*!
+* @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
+* @param y 判定を行いたいマスのY座標
+* @param x 判定を行いたいマスのX座標
+* @return ドアを設置可能ならばTRUEを返す
+* @note Assumes "in_bounds(y1, x1)"
+* @details
+* XXX XXX XXX\n
+* Assumes "in_bounds(y, x)"\n
+*/
+static bool possible_doorway(int y, int x)
+{
+       /* Count the adjacent corridors */
+       if (next_to_corr(y, x) >= 2)
+       {
+               /* Check Vertical */
+               if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
+                       cave_have_flag_bold(y + 1, x, FF_WALL))
+               {
+                       return (TRUE);
+               }
+
+               /* Check Horizontal */
+               if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
+                       cave_have_flag_bold(y, x + 1, FF_WALL))
+               {
+                       return (TRUE);
+               }
+       }
+
+       /* No doorway */
+       return (FALSE);
+}
+
+/*!
+* @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
+* @param y 設置を行いたいマスのY座標
+* @param x 設置を行いたいマスのX座標
+* @return なし
+*/
+void try_door(int y, int x)
+{
+       /* Paranoia */
+       if (!in_bounds(y, x)) return;
+
+       /* Ignore walls */
+       if (cave_have_flag_bold(y, x, FF_WALL)) return;
+
+       /* Ignore room grids */
+       if (cave[y][x].info & (CAVE_ROOM)) return;
+
+       /* Occasional door (if allowed) */
+       if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
+       {
+               /* Place a door */
+               place_random_door(y, x, FALSE);
+       }
+}
+
+
+/*!
+ * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
+ * @param x1 長方形の左端X座標(-1)
+ * @param x2 長方形の右端X座標(+1)
+ * @param y1 長方形の上端Y座標(-1)
+ * @param y2 長方形の下端Y座標(+1)
+ * @param light 照明の有無
+ * @return なし
  */
 void place_floor(int x1, int x2, int y1, int y2, bool light)
 {
@@ -210,8 +519,14 @@ void place_floor(int x1, int x2, int y1, int y2, bool light)
 }
 
 
-/*
- * Make an empty square room, only floor and wall grids
+/*!
+ * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
+ * @param x1 長方形の左端X座標(-1)
+ * @param x2 長方形の右端X座標(+1)
+ * @param y1 長方形の上端Y座標(-1)
+ * @param y2 長方形の下端Y座標(+1)
+ * @param light 照明の有無
+ * @return なし
  */
 void place_room(int x1, int x2, int y1, int y2, bool light)
 {
@@ -233,8 +548,13 @@ void place_room(int x1, int x2, int y1, int y2, bool light)
 }
 
 
-/*
- * Create up to "num" objects near the given coordinates
+/*!
+ * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
+ * @param y 配置したい中心マスのY座標
+ * @param x 配置したい中心マスのX座標
+ * @param num 配置したい数
+ * @return なし
+ * @details
  * Only really called by some of the "vault" routines.
  */
 void vault_objects(int y, int x, int num)
@@ -267,7 +587,7 @@ void vault_objects(int y, int x, int num)
                                if (cheat_room)
                                {
 #ifdef JP
-msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
+msg_print("警告!地下室のアイテムを配置できません!");
 #else
                                        msg_print("Warning! Could not place vault object!");
 #endif
@@ -283,7 +603,7 @@ msg_print("
                        /* Place an item */
                        if (randint0(100) < 75)
                        {
-                               place_object(j, k, FALSE, FALSE);
+                               place_object(j, k, 0L);
                        }
 
                        /* Place gold */
@@ -298,9 +618,15 @@ msg_print("
        }
 }
 
-
-/*
- * Place a trap with a given displacement of point
+/*!
+ * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
+ * @param y トラップを配置したいマスの中心Y座標
+ * @param x トラップを配置したいマスの中心X座標
+ * @param yd Y方向の配置分散マス数
+ * @param xd X方向の配置分散マス数
+ * @return なし
+ * @details
+ * Only really called by some of the "vault" routines.
  */
 void vault_trap_aux(int y, int x, int yd, int xd)
 {
@@ -327,7 +653,7 @@ void vault_trap_aux(int y, int x, int yd, int xd)
                        if (cheat_room)
                        {
 #ifdef JP
-msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
+msg_print("警告!地下室のトラップを配置できません!");
 #else
                                msg_print("Warning! Could not place vault trap!");
 #endif
@@ -347,9 +673,16 @@ msg_print("
        }
 }
 
-
-/*
- * Place some traps with a given displacement of given location
+/*!
+ * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
+ * @param y トラップを配置したいマスの中心Y座標
+ * @param x トラップを配置したいマスの中心X座標
+ * @param yd Y方向の配置分散マス数
+ * @param xd X方向の配置分散マス数
+ * @param num 配置したいトラップの数
+ * @return なし
+ * @details
+ * Only really called by some of the "vault" routines.
  */
 void vault_traps(int y, int x, int yd, int xd, int num)
 {
@@ -361,13 +694,19 @@ void vault_traps(int y, int x, int yd, int xd, int num)
        }
 }
 
-
-/*
- * Hack -- Place some sleeping monsters near the given location
+/*!
+ * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
+ * @param y1 モンスターを配置したいマスの中心Y座標
+ * @param x1 モンスターを配置したいマスの中心X座標
+ * @param num 配置したいモンスターの数
+ * @return なし
+ * @details
+ * Only really called by some of the "vault" routines.
  */
 void vault_monsters(int y1, int x1, int num)
 {
-       int k, i, y, x;
+       int k, i;
+       POSITION y, x;
        cave_type *c_ptr;
 
        /* Try to summon "num" monsters "near" the given location */
@@ -394,28 +733,15 @@ void vault_monsters(int y1, int x1, int num)
 }
 
 
-/*
- * Count the number of walls adjacent to the given grid.
- *
- * Note -- Assumes "in_bounds(y, x)"
- *
- * We count only granite walls and permanent walls.
- */
-int next_to_walls(int y, int x)
-{
-       int     k = 0;
-
-       if (cave_floor_grid(&cave[y + 1][x])) k++;
-       if (cave_floor_grid(&cave[y - 1][x])) k++;
-       if (cave_floor_grid(&cave[y][x + 1])) k++;
-       if (cave_floor_grid(&cave[y][x - 1])) k++;
-
-       return (k);
-}
-
-
-/*
- * Always picks a correct direction
+/*!
+ * @brief build_tunnel用に通路を掘るための方向を位置関係通りに決める / Always picks a correct direction
+ * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
+ * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
+ * @param y1 始点Y座標
+ * @param x1 始点X座標
+ * @param y2 終点Y座標
+ * @param x2 終点X座標
+ * @return なし
  */
 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
 {
@@ -433,10 +759,11 @@ void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
        }
 }
 
-
-
-/*
- * Pick a random direction
+/*!
+ * @brief build_tunnel用に通路を掘るための方向をランダムに決める / Pick a random direction
+ * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
+ * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
+ * @return なし
  */
 void rand_dir(int *rdir, int *cdir)
 {
@@ -448,8 +775,12 @@ void rand_dir(int *rdir, int *cdir)
        *cdir = ddx_ddd[i];
 }
 
-
-/* Function that sees if a square is a floor.  (Includes range checking.) */
+/*!
+ * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
+ * @param x チェックするマスのX座標
+ * @param y チェックするマスのY座標
+ * @return 床系地形ならばTRUE
+ */
 bool get_is_floor(int x, int y)
 {
        if (!in_bounds(y, x))
@@ -464,8 +795,12 @@ bool get_is_floor(int x, int y)
        return (FALSE);
 }
 
-
-/* Set a square to be floor.  (Includes range checking.) */
+/*!
+ * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
+ * @param x 地形を変えたいマスのX座標
+ * @param y 地形を変えたいマスのY座標
+ * @return なし
+ */
 void set_floor(int x, int y)
 {
        if (!in_bounds(y, x))
@@ -486,45 +821,45 @@ void set_floor(int x, int y)
 }
 
 
-
-/*
- * Constructs a tunnel between two points
- *
- * This function must be called BEFORE any streamers are created,
- * since we use the special "granite wall" sub-types to keep track
- * of legal places for corridors to pierce rooms.
- *
- * We use "door_flag" to prevent excessive construction of doors
- * along overlapping corridors.
- *
- * We queue the tunnel grids to prevent door creation along a corridor
- * which intersects itself.
- *
- * We queue the wall piercing grids to prevent a corridor from leaving
- * a room and then coming back in through the same entrance.
- *
- * We "pierce" grids which are "outer" walls of rooms, and when we
- * do so, we change all adjacent "outer" walls of rooms into "solid"
- * walls so that no two corridors may use adjacent grids for exits.
- *
- * The "solid" wall check prevents corridors from "chopping" the
- * corners of rooms off, as well as "silly" door placement, and
- * "excessively wide" room entrances.
- *
- * Useful "feat" values:
- *   FEAT_WALL_EXTRA -- granite walls
- *   FEAT_WALL_INNER -- inner room walls
- *   FEAT_WALL_OUTER -- outer room walls
- *   FEAT_WALL_SOLID -- solid room walls
- *   FEAT_PERM_EXTRA -- shop walls (perma)
- *   FEAT_PERM_INNER -- inner room walls (perma)
- *   FEAT_PERM_OUTER -- outer room walls (perma)
- *   FEAT_PERM_SOLID -- dungeon border (perma)
+/*!
+ * @brief 部屋間のトンネルを生成する / Constructs a tunnel between two points
+ * @param row1 始点Y座標
+ * @param col1 始点X座標
+ * @param row2 終点Y座標
+ * @param col2 終点X座標
+ * @return 生成に成功したらTRUEを返す
+ * @details
+ * This function must be called BEFORE any streamers are created,\n
+ * since we use the special "granite wall" sub-types to keep track\n
+ * of legal places for corridors to pierce rooms.\n
+ *\n
+ * We use "door_flag" to prevent excessive construction of doors\n
+ * along overlapping corridors.\n
+ *\n
+ * We queue the tunnel grids to prevent door creation along a corridor\n
+ * which intersects itself.\n
+ *\n
+ * We queue the wall piercing grids to prevent a corridor from leaving\n
+ * a room and then coming back in through the same entrance.\n
+ *\n
+ * We "pierce" grids which are "outer" walls of rooms, and when we\n
+ * do so, we change all adjacent "outer" walls of rooms into "solid"\n
+ * walls so that no two corridors may use adjacent grids for exits.\n
+ *\n
+ * The "solid" wall check prevents corridors from "chopping" the\n
+ * corners of rooms off, as well as "silly" door placement, and\n
+ * "excessively wide" room entrances.\n
+ *\n
+ * Kind of walls:\n
+ *   extra -- walls\n
+ *   inner -- inner room walls\n
+ *   outer -- outer room walls\n
+ *   solid -- solid room walls\n
  */
-void build_tunnel(int row1, int col1, int row2, int col2)
+bool build_tunnel(POSITION row1, POSITION col1, POSITION row2, POSITION col2)
 {
        int y, x;
-       int tmp_row, tmp_col;
+       POSITION tmp_row, tmp_col;
        int row_dir, col_dir;
        int start_row, start_col;
        int main_loop_count = 0;
@@ -544,7 +879,7 @@ void build_tunnel(int row1, int col1, int row2, int col2)
        while ((row1 != row2) || (col1 != col2))
        {
                /* Mega-Hack -- Paranoia -- prevent infinite loops */
-               if (main_loop_count++ > 2000) break;
+               if (main_loop_count++ > 2000) return FALSE;
 
                /* Allow bends in the tunnel */
                if (randint0(100) < dun_tun_chg)
@@ -585,14 +920,7 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                /* Access the location */
                c_ptr = &cave[tmp_row][tmp_col];
 
-
-               /* Avoid the edge of the dungeon */
-               if (c_ptr->feat == FEAT_PERM_SOLID) continue;
-
-               /* Avoid the edge of vaults */
-               if (c_ptr->feat == FEAT_PERM_OUTER) continue;
-
-               /* Avoid "solid" granite walls */
+               /* Avoid "solid" walls */
                if (is_solid_grid(c_ptr)) continue;
 
                /* Pierce "outer" walls of rooms */
@@ -602,17 +930,13 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                        y = tmp_row + row_dir;
                        x = tmp_col + col_dir;
 
-                       /* Hack -- Avoid outer/solid permanent walls */
-                       if (cave[y][x].feat == FEAT_PERM_SOLID) continue;
-                       if (cave[y][x].feat == FEAT_PERM_OUTER) continue;
-
-                       /* Hack -- Avoid outer/solid granite walls */
+                       /* Hack -- Avoid outer/solid walls */
                        if (is_outer_bold(y, x)) continue;
                        if (is_solid_bold(y, x)) continue;
 
                        /* Accept this location */
-                       row1 = tmp_row;
-                       col1 = tmp_col;
+                       row1 = (POSITION)tmp_row;
+                       col1 = (POSITION)tmp_col;
 
                        /* Save the wall location */
                        if (dun->wall_n < WALL_MAX)
@@ -621,6 +945,7 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                                dun->wall[dun->wall_n].x = col1;
                                dun->wall_n++;
                        }
+                       else return FALSE;
 
                        /* Forbid re-entry near this piercing */
                        for (y = row1 - 1; y <= row1 + 1; y++)
@@ -659,6 +984,7 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                                dun->tunn[dun->tunn_n].x = col1;
                                dun->tunn_n++;
                        }
+                       else return FALSE;
 
                        /* Allow door in next grid */
                        door_flag = FALSE;
@@ -681,6 +1007,7 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                                        dun->door[dun->door_n].x = col1;
                                        dun->door_n++;
                                }
+                               else return FALSE;
 
                                /* No door in next grid */
                                door_flag = TRUE;
@@ -702,38 +1029,39 @@ void build_tunnel(int row1, int col1, int row2, int col2)
                        }
                }
        }
+
+       return TRUE;
 }
 
 
-/*
- * This routine adds the square to the tunnel
- * It also checks for SOLID walls - and returns a nearby
- * non-SOLID square in (x,y) so that a simple avoiding
- * routine can be used. The returned boolean value reflects
- * whether or not this routine hit a SOLID wall.
- *
- * "affectwall" toggles whether or not this new square affects
- * the boundaries of rooms. - This is used by the catacomb
- * routine.
+/*!
+ * @brief トンネル生成のための基準点を指定する。
+ * @param x 基準点を指定するX座標の参照ポインタ、適時値が修正される。
+ * @param y 基準点を指定するY座標の参照ポインタ、適時値が修正される。
+ * @param affectwall (調査中)
+ * @return なし
+ * @details
+ * This routine adds the square to the tunnel\n
+ * It also checks for SOLID walls - and returns a nearby\n
+ * non-SOLID square in (x,y) so that a simple avoiding\n
+ * routine can be used. The returned boolean value reflects\n
+ * whether or not this routine hit a SOLID wall.\n
+ *\n
+ * "affectwall" toggles whether or not this new square affects\n
+ * the boundaries of rooms. - This is used by the catacomb\n
+ * routine.\n
+ * @todo 特に詳細な処理の意味を調査すべし
  */
-static bool set_tunnel(int *x, int *y, bool affectwall)
+static bool set_tunnel(POSITION *x, POSITION *y, bool affectwall)
 {
-       int feat, i, j, dx, dy;
+       int i, j, dx, dy;
 
        cave_type *c_ptr = &cave[*y][*x];
 
        if (!in_bounds(*y, *x)) return TRUE;
 
-       feat = c_ptr->feat;
-
-       if ((feat == FEAT_PERM_OUTER) ||
-           (feat == FEAT_PERM_INNER) ||
-           is_inner_grid(c_ptr))
+       if (is_inner_grid(c_ptr))
        {
-               /*
-                * Ignore permanent walls - sometimes cannot tunnel around them anyway
-                * so don't try - it just complicates things unnecessarily.
-                */
                return TRUE;
        }
 
@@ -742,12 +1070,13 @@ static bool set_tunnel(int *x, int *y, bool affectwall)
                /* Save the tunnel location */
                if (dun->tunn_n < TUNN_MAX)
                {
-                               dun->tunn[dun->tunn_n].y = *y;
-                               dun->tunn[dun->tunn_n].x = *x;
-                               dun->tunn_n++;
-               }
+                       dun->tunn[dun->tunn_n].y = *y;
+                       dun->tunn[dun->tunn_n].x = *x;
+                       dun->tunn_n++;
 
-               return TRUE;
+                       return TRUE;
+               }
+               else return FALSE;
        }
 
        if (is_floor_bold(*y, *x))
@@ -765,6 +1094,7 @@ static bool set_tunnel(int *x, int *y, bool affectwall)
                        dun->wall[dun->wall_n].x = *x;
                        dun->wall_n++;
                }
+               else return FALSE;
 
                /* Forbid re-entry near this piercing */
                for (j = *y - 1; j <= *y + 1; j++)
@@ -779,6 +1109,10 @@ static bool set_tunnel(int *x, int *y, bool affectwall)
                                }
                        }
                }
+
+               /* Clear mimic type */
+               cave[*y][*x].mimic = 0;
+
                place_floor_bold(*y, *x);
 
                return TRUE;
@@ -827,14 +1161,18 @@ static bool set_tunnel(int *x, int *y, bool affectwall)
 }
 
 
-/*
- * This routine creates the catacomb-like tunnels by removing extra rock.
+/*!
+ * @brief 外壁を削って「カタコンベ状」の通路を作成する / This routine creates the catacomb-like tunnels by removing extra rock.
+ * @param x 基準点のX座標
+ * @param y 基準点のY座標
+ * @return なし
+ * @details
  * Note that this routine is only called on "even" squares - so it gives
  * a natural checkerboard pattern.
  */
-static void create_cata_tunnel(int x, int y)
+static void create_cata_tunnel(POSITION x, POSITION y)
 {
-       int x1, y1;
+       POSITION x1, y1;
 
        /* Build tunnel */
        x1 = x - 1;
@@ -855,28 +1193,32 @@ static void create_cata_tunnel(int x, int y)
 }
 
 
-/*
- * This routine does the bulk of the work in creating the new types of tunnels.
- * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
- * It doesn't need to add any complexity - straight lines are fine.
- * The SOLID walls are avoided by a recursive algorithm which tries random ways
- * around the obstical until it works.  The number of itterations is counted, and it
- * this gets too large the routine exits. This should stop any crashes - but may leave
- * small gaps in the tunnel where there are too many SOLID walls.
- *
- * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used
- * as a part of the dodge SOLID walls algorithm.
- *
- * Type 2 tunnels are made of two straight lines at right angles. When this is used with
- * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
- *
- * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
- * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
- * the surface.
+/*!
+ * @brief トンネル生成処理(詳細調査中)/ This routine does the bulk of the work in creating the new types of tunnels.
+ * @return なし
+ * @todo 詳細用調査
+ * @details
+ * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)\n
+ * It doesn't need to add any complexity - straight lines are fine.\n
+ * The SOLID walls are avoided by a recursive algorithm which tries random ways\n
+ * around the obstical until it works.  The number of itterations is counted, and it\n
+ * this gets too large the routine exits. This should stop any crashes - but may leave\n
+ * small gaps in the tunnel where there are too many SOLID walls.\n
+ *\n
+ * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used\n
+ * as a part of the dodge SOLID walls algorithm.\n
+ *\n
+ * Type 2 tunnels are made of two straight lines at right angles. When this is used with\n
+ * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.\n
+ *\n
+ * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.\n
+ * This, when used with longer line segments gives the "catacomb-like" tunnels seen near\n
+ * the surface.\n
  */
 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
 {
-       int i, x, y;
+       int i;
+       POSITION x, y;
        int length;
 
        /* Check for early exit */
@@ -987,22 +1329,24 @@ static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count,
 }
 
 
-/*
- * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
- * Permanent rock is ignored in this path finding- sometimes there is no
- * path around anyway -so there will be a crash if we try to find one.
- * This routine is much like the river creation routine in Zangband.
- * It works by dividing a line segment into two.  The segments are divided
- * until they are less than "cutoff" - when the corresponding routine from
- * "short_seg_hack" is called.
- * Note it is VERY important that the "stop if hit another passage" logic
- * stays as is.  Without this the dungeon turns into Swiss Cheese...
+/*!
+ * @brief 特定の壁(永久壁など)を避けながら部屋間の通路を作成する / This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
+ * @return なし
+ * @todo 詳細要調査
+ * @details
+ * Permanent rock is ignored in this path finding- sometimes there is no\n
+ * path around anyway -so there will be a crash if we try to find one.\n
+ * This routine is much like the river creation routine in Zangband.\n
+ * It works by dividing a line segment into two.  The segments are divided\n
+ * until they are less than "cutoff" - when the corresponding routine from\n
+ * "short_seg_hack" is called.\n
+ * Note it is VERY important that the "stop if hit another passage" logic\n
+ * stays as is.  Without this the dungeon turns into Swiss Cheese...\n
  */
-bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
+bool build_tunnel2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int cutoff)
 {
-       int x3, y3, dx, dy;
+       POSITION x3, y3, dx, dy;
        int changex, changey;
-       int midval;
        int length;
        int i;
        bool retval, firstsuccede;
@@ -1020,8 +1364,6 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
 
                /* perturbation perpendicular to path */
                changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
-
-               /* perturbation perpendicular to path */
                changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
 
                /* Work out "mid" ponit */
@@ -1034,9 +1376,8 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
                        x3 = (x1 + x2) / 2;
                        y3 = (y1 + y2) / 2;
                }
-               /* cache midvalue */
+               /* cache c_ptr */
                c_ptr = &cave[y3][x3];
-               midval = cave[y3][x3].feat;
                if (is_solid_grid(c_ptr))
                {
                        /* move midpoint a bit to avoid problem. */
@@ -1067,7 +1408,6 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
                        y3 += dy;
                        x3 += dx;
                        c_ptr = &cave[y3][x3];
-                       midval = cave[y3][x3].feat;
                }
 
                if (is_floor_grid(c_ptr))
@@ -1087,10 +1427,11 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
                                        /* Save the door location */
                                        if (dun->door_n < DOOR_MAX)
                                        {
-                                               dun->door[dun->door_n].y = y3;
-                                               dun->door[dun->door_n].x = x3;
+                                               dun->door[dun->door_n].y = (POSITION)y3;
+                                               dun->door[dun->door_n].x = (POSITION)x3;
                                                dun->door_n++;
                                        }
+                                       else return FALSE;
                                }
                                firstsuccede = TRUE;
                        }
@@ -1104,9 +1445,9 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
                else
                {
                        /* tunnel through walls */
-                       if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
+                       if (build_tunnel2(x1, y1, (POSITION)x3, (POSITION)y3, type, cutoff))
                        {
-                               retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
+                               retval = build_tunnel2((POSITION)x3, (POSITION)y3, x2, y2, type, cutoff);
                                firstsuccede = TRUE;
                        }
                        else
@@ -1134,3 +1475,4 @@ bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
                return TRUE;
        }
 }
+