OSDN Git Service

[Refactor] #37353 コメント整理。 / Refactor comments.
[hengband/hengband.git] / src / rooms.c
index 42fa0de..5135bfa 100644 (file)
 #include "grid.h"
 #include "rooms.h"
 
+#include "rooms-city.h"
+#include "rooms-fractal.h"
 #include "rooms-normal.h"
 #include "rooms-pitnest.h"
+#include "rooms-special.h"
+#include "rooms-trap.h"
+#include "rooms-vault.h"
+
+#include "trap.h"
+
+#include "monster.h"
 
 
 /*!
  * appear above their minimum depth.  Tiny levels will not have space\n
  * for all the rooms you ask for.\n
  */
+
+#if 1
+
 static room_info_type room_info_normal[ROOM_T_MAX] =
 {
        /* Depth */
        /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
-
        {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
        {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
        {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
@@ -82,8 +93,37 @@ static room_info_type room_info_normal[ROOM_T_MAX] =
        {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
        {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
        {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
+       {{  1,  8, 16, 24, 32, 40, 48, 56, 64, 72, 80},  1}, /*FIX      */
+};
+
+#endif
+
+#if 0
+static room_info_type room_info_normal[ROOM_T_MAX] =
+{
+/*デバッグ用配列*/
+{ {999, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0}, 0}, /*NORMAL   */
+{ {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  1 }, /*OVERLAP  */
+{ {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  3 }, /*CROSS    */
+{ {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  3 }, /*INNER_F  */
+{ {  0,  1,  1,  1,  2,  3,  5,  6,  8, 10,  0}, 10 }, /*NEST     */
+{ {  0,  1,  1,  2,  3,  4,  6,  8, 10, 13,  0}, 10 }, /*PIT      */
+{ {  0,  1,  1,  1,  2,  2,  3,  5,  6,  8,  0}, 10 }, /*LESSER_V */
+{ {  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  0}, 20 }, /*GREATER_V*/
+{ {  0,100,200,300,400,500,600,700,800,900,  0}, 10 }, /*FRACAVE  */
+{ {  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  0}, 10 }, /*RANDOM_V */
+{ {  0,  4,  8, 12, 16, 20, 24, 28, 32, 36,  0},  3 }, /*OVAL     */
+{ {  1,  6, 12, 18, 24, 30, 36, 42, 48, 54,  0}, 10 }, /*CRYPT    */
+{ {  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  0}, 20 }, /*TRAP_PIT */
+{ {  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  0}, 20 }, /*TRAP     */
+{ {  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  0}, 40 }, /*GLASS    */
+{ {  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  0},  1 }, /*ARCADE   */
+{ { 20, 40, 60, 80,100,100,100,100,100,100,100},  1 }, /*FIX      */
 };
 
+#endif
+
+
 
 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
 static byte room_build_order[ROOM_T_MAX] = {
@@ -97,6 +137,7 @@ static byte room_build_order[ROOM_T_MAX] = {
        ROOM_T_TRAP,
        ROOM_T_GLASS,
        ROOM_T_INNER_FEAT,
+       ROOM_T_FIXED,
        ROOM_T_OVAL,
        ROOM_T_CRYPT,
        ROOM_T_OVERLAP,
@@ -106,74 +147,6 @@ static byte room_build_order[ROOM_T_MAX] = {
 };
 
 /*!
- * @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);
-       }
-}
-
-/*!
  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
  * @param y0 配置したい中心のY座標
  * @param x0 配置したい中心のX座標
@@ -184,9 +157,9 @@ void place_secret_door(int y, int x, int type)
  * Note - this should be used only on allocated regions
  * within another room.
  */
-static void build_small_room(int x0, int y0)
+void build_small_room(POSITION x0, POSITION y0)
 {
-       int x, y;
+       POSITION x, y;
 
        for (y = y0 - 1; y <= y0 + 1; y++)
        {
@@ -226,13 +199,11 @@ static void build_small_room(int x0, int y0)
  * @param y2 範囲の下端
  * @return なし
  */
-static void check_room_boundary(int x1, int y1, int x2, int y2)
+static void check_room_boundary(POSITION x1, POSITION y1, POSITION x2, POSITION y2)
 {
-       int count, x, y;
+       int count;
+       POSITION x, y;
        bool old_is_floor, new_is_floor;
-
-
-       /* Initialize */
        count = 0;
 
        old_is_floor = get_is_floor(x1 - 1, y1);
@@ -312,9 +283,9 @@ static void check_room_boundary(int x1, int y1, int x2, int y2)
  * @param block_x 範囲の左端
  * @return なし
  */
-static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int block_x)
+static bool find_space_aux(POSITION blocks_high, POSITION blocks_wide, POSITION block_y, POSITION block_x)
 {
-       int by1, bx1, by2, bx2, by, bx;
+       POSITION by1, bx1, by2, bx2, by, bx;
 
        /* Itty-bitty rooms must shift about within their rectangle */
        if (blocks_wide < 3)
@@ -357,8 +328,8 @@ static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int bl
        }
 
        /* Extract blocks */
-       by1 = block_y + 0;
-       bx1 = block_x + 0;
+       by1 = block_y;
+       bx1 = block_x;
        by2 = block_y + blocks_high;
        bx2 = block_x + blocks_wide;
 
@@ -406,13 +377,12 @@ static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int bl
 bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
 {
        int candidates, pick;
-       int by, bx, by1, bx1, by2, bx2;
-       int block_y = 0, block_x = 0;
-
+       POSITION by, bx, by1, bx1, by2, bx2;
+       POSITION block_y = 0, block_x = 0;
 
        /* Find out how many blocks we need. */
-       int blocks_high = 1 + ((height - 1) / BLOCK_HGT);
-       int blocks_wide = 1 + ((width - 1) / BLOCK_WID);
+       POSITION blocks_high = 1 + ((height - 1) / BLOCK_HGT);
+       POSITION blocks_wide = 1 + ((width - 1) / BLOCK_WID);
 
        /* There are no way to allocate such huge space */
        if (dun->row_rooms < blocks_high) return FALSE;
@@ -472,8 +442,8 @@ bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
        }
 
        /* Extract blocks */
-       by1 = block_y + 0;
-       bx1 = block_x + 0;
+       by1 = block_y;
+       bx1 = block_x;
        by2 = block_y + blocks_high;
        bx2 = block_x + blocks_wide;
 
@@ -503,7 +473,6 @@ bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
                }
        }
 
-
        /*
         * Hack- See if room will cut off a cavern.
         *
@@ -512,3645 +481,1594 @@ bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
         */
        check_room_boundary(*x - width / 2 - 1, *y - height / 2 - 1, *x + (width - 1) / 2 + 1, *y + (height - 1) / 2 + 1);
 
-
        /* Success. */
        return TRUE;
 }
 
 
-
-
-
-
-/*!
- * @brief Vault地形を回転、上下左右反転するための座標変換を返す / coordinate translation code
- * @param x 変換したい点のX座標参照ポインタ
- * @param y 変換したい点のY座標参照ポインタ
- * @param xoffset Vault生成時の基準X座標
- * @param yoffset Vault生成時の基準Y座標
- * @param transno 処理ID
- * @return なし
+/*
+ * Structure to hold all "fill" data
  */
-static void coord_trans(POSITION *x, POSITION *y, POSITION xoffset, POSITION yoffset, int transno)
-{
-       int i;
-       int temp;
-
-       /*
-        * transno specifies what transformation is required. (0-7)
-        * The lower two bits indicate by how much the vault is rotated,
-        * and the upper bit indicates a reflection.
-        * This is done by using rotation matrices... however since
-        * these are mostly zeros for rotations by 90 degrees this can
-        * be expressed simply in terms of swapping and inverting the
-        * x and y coordinates.
-        */
-       for (i = 0; i < transno % 4; i++)
-       {
-               /* rotate by 90 degrees */
-               temp = *x;
-               *x = -(*y);
-               *y = temp;
-       }
-
-       if (transno / 4)
-       {
-               /* Reflect depending on status of 3rd bit. */
-               *x = -(*x);
-       }
 
-       /* Add offsets so vault stays in the first quadrant */
-       *x += xoffset;
-       *y += yoffset;
-}
+typedef struct fill_data_type fill_data_type;
 
-/*!
- * @brief Vaultをフロアに配置する / Hack -- fill in "vault" rooms
- * @param yval 生成基準Y座標
- * @param xval 生成基準X座標
- * @param ymax VaultのYサイズ
- * @param xmax VaultのXサイズ
- * @param data Vaultのデータ文字列
- * @param xoffset 変換基準X座標
- * @param yoffset 変換基準Y座標
- * @param transno 変換ID
- * @return なし
- */
-static void build_vault(POSITION yval, POSITION xval, POSITION ymax, POSITION xmax, cptr data,
-               POSITION xoffset, POSITION yoffset, int transno)
+struct fill_data_type
 {
-       POSITION dx, dy, x, y, i, j;
-       cptr t;
-       cave_type *c_ptr;
-
-       /* Place dungeon features and objects */
-       for (t = data, dy = 0; dy < ymax; dy++)
-       {
-               for (dx = 0; dx < xmax; dx++, t++)
-               {
-                       /* prevent loop counter from being overwritten */
-                       i = dx;
-                       j = dy;
-
-                       /* Flip / rotate */
-                       coord_trans(&i, &j, xoffset, yoffset, transno);
-
-                       /* Extract the location */
-                       if (transno % 2 == 0)
-                       {
-                               /* no swap of x/y */
-                               x = xval - (xmax / 2) + i;
-                               y = yval - (ymax / 2) + j;
-                       }
-                       else
-                       {
-                               /* swap of x/y */
-                               x = xval - (ymax / 2) + i;
-                               y = yval - (xmax / 2) + j;
-                       }
+       /* area size */
+       POSITION xmin;
+       POSITION ymin;
+       POSITION xmax;
+       POSITION ymax;
 
-                       /* Hack -- skip "non-grids" */
-                       if (*t == ' ') continue;
+       /* cutoffs */
+       int c1;
+       int c2;
+       int c3;
 
-                       /* Access the grid */
-                       c_ptr = &cave[y][x];
+       /* features to fill with */
+       FEAT_IDX feat1;
+       FEAT_IDX feat2;
+       FEAT_IDX feat3;
 
-                       /* Lay down a floor */
-                       place_floor_grid(c_ptr);
+       int info1;
+       int info2;
+       int info3;
 
-                       /* Remove any mimic */
-                       c_ptr->mimic = 0;
+       /* number of filled squares */
+       int amount;
+};
 
-                       /* Part of a vault */
-                       c_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
+static fill_data_type fill_data;
 
-                       /* Analyze the grid */
-                       switch (*t)
-                       {
-                               /* Granite wall (outer) */
-                       case '%':
-                               place_outer_noperm_grid(c_ptr);
-                               break;
 
-                               /* Granite wall (inner) */
-                       case '#':
-                               place_inner_grid(c_ptr);
-                               break;
+/* Store routine for the fractal cave generator */
+/* this routine probably should be an inline function or a macro. */
+static void store_height(POSITION x, POSITION y, FEAT_IDX val)
+{
+       /* if on boundary set val > cutoff so walls are not as square */
+       if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
+               (x == fill_data.xmax) || (y == fill_data.ymax)) &&
+               (val <= fill_data.c1)) val = fill_data.c1 + 1;
 
-                               /* Glass wall (inner) */
-                       case '$':
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
-                               break;
+       /* store the value in height-map format */
+       cave[y][x].feat = val;
 
-                               /* Permanent wall (inner) */
-                       case 'X':
-                               place_inner_perm_grid(c_ptr);
-                               break;
+       return;
+}
 
-                               /* Permanent glass wall (inner) */
-                       case 'Y':
-                               place_inner_perm_grid(c_ptr);
-                               c_ptr->feat = feat_permanent_glass_wall;
-                               break;
 
-                               /* Treasure/trap */
-                       case '*':
-                               if (randint0(100) < 75)
-                               {
-                                       place_object(y, x, 0L);
-                               }
-                               else
-                               {
-                                       place_trap(y, x);
-                               }
-                               break;
+/*
+* Explanation of the plasma fractal algorithm:
+*
+* A grid of points is created with the properties of a 'height-map'
+* This is done by making the corners of the grid have a random value.
+* The grid is then subdivided into one with twice the resolution.
+* The new points midway between two 'known' points can be calculated
+* by taking the average value of the 'known' ones and randomly adding
+* or subtracting an amount proportional to the distance between those
+* points.  The final 'middle' points of the grid are then calculated
+* by averaging all four of the originally 'known' corner points.  An
+* random amount is added or subtracted from this to get a value of the
+* height at that point.  The scaling factor here is adjusted to the
+* slightly larger distance diagonally as compared to orthogonally.
+*
+* This is then repeated recursively to fill an entire 'height-map'
+* A rectangular map is done the same way, except there are different
+* scaling factors along the x and y directions.
+*
+* A hack to change the amount of correlation between points is done using
+* the grd variable.  If the current step size is greater than grd then
+* the point will be random, otherwise it will be calculated by the
+* above algorithm.  This makes a maximum distance at which two points on
+* the height map can affect each other.
+*
+* How fractal caves are made:
+*
+* When the map is complete, a cut-off value is used to create a cave.
+* Heights below this value are "floor", and heights above are "wall".
+* This also can be used to create lakes, by adding more height levels
+* representing shallow and deep water/ lava etc.
+*
+* The grd variable affects the width of passages.
+* The roug variable affects the roughness of those passages
+*
+* The tricky part is making sure the created cave is connected.  This
+* is done by 'filling' from the inside and only keeping the 'filled'
+* floor.  Walls bounding the 'filled' floor are also kept.  Everything
+* else is converted to the normal _extra_.
+ */
 
-                               /* Secret doors */
-                       case '+':
-                               place_secret_door(y, x, DOOR_DEFAULT);
-                               break;
 
-                               /* Secret glass doors */
-                       case '-':
-                               place_secret_door(y, x, DOOR_GLASS_DOOR);
-                               if (is_closed_door(c_ptr->feat)) c_ptr->mimic = feat_glass_wall;
-                               break;
+/*
+ *  Note that this uses the cave.feat array in a very hackish way
+ *  the values are first set to zero, and then each array location
+ *  is used as a "heightmap"
+ *  The heightmap then needs to be converted back into the "feat" format.
+ *
+ *  grd=level at which fractal turns on.  smaller gives more mazelike caves
+ *  roug=roughness level.  16=normal.  higher values make things more convoluted
+ *    small values are good for smooth walls.
+ *  size=length of the side of the square cave system.
+ */
+void generate_hmap(POSITION y0, POSITION x0, POSITION xsiz, POSITION ysiz, int grd, int roug, int cutoff)
+{
+       POSITION xhsize, yhsize, xsize, ysize, maxsize;
 
-                               /* Curtains */
-                       case '\'':
-                               place_secret_door(y, x, DOOR_CURTAIN);
-                               break;
+       /*
+        * fixed point variables- these are stored as 256 x normal value
+        * this gives 8 binary places of fractional part + 8 places of normal part
+        */
 
-                               /* Trap */
-                       case '^':
-                               place_trap(y, x);
-                               break;
+       POSITION xstep, xhstep, ystep, yhstep;
+       POSITION xstep2, xhstep2, ystep2, yhstep2;
+       POSITION i, j, ii, jj, diagsize, xxsize, yysize;
+       
+       /* Cache for speed */
+       POSITION xm, xp, ym, yp;
 
-                               /* Black market in a dungeon */
-                       case 'S':
-                               set_cave_feat(y, x, feat_black_market);
-                               store_init(NO_TOWN, STORE_BLACK);
-                               break;
+       /* redefine size so can change the value if out of range */
+       xsize = xsiz;
+       ysize = ysiz;
 
-                               /* The Pattern */
-                       case 'p':
-                               set_cave_feat(y, x, feat_pattern_start);
-                               break;
+       /* Paranoia about size of the system of caves */
+       if (xsize > 254) xsize = 254;
+       if (xsize < 4) xsize = 4;
+       if (ysize > 254) ysize = 254;
+       if (ysize < 4) ysize = 4;
 
-                       case 'a':
-                               set_cave_feat(y, x, feat_pattern_1);
-                               break;
+       /* get offsets to middle of array */
+       xhsize = xsize / 2;
+       yhsize = ysize / 2;
 
-                       case 'b':
-                               set_cave_feat(y, x, feat_pattern_2);
-                               break;
+       /* fix rounding problem */
+       xsize = xhsize * 2;
+       ysize = yhsize * 2;
 
-                       case 'c':
-                               set_cave_feat(y, x, feat_pattern_3);
-                               break;
+       /* get limits of region */
+       fill_data.xmin = x0 - xhsize;
+       fill_data.ymin = y0 - yhsize;
+       fill_data.xmax = x0 + xhsize;
+       fill_data.ymax = y0 + yhsize;
 
-                       case 'd':
-                               set_cave_feat(y, x, feat_pattern_4);
-                               break;
+       /* Store cutoff in global for quick access */
+       fill_data.c1 = cutoff;
 
-                       case 'P':
-                               set_cave_feat(y, x, feat_pattern_end);
-                               break;
+       /*
+       * Scale factor for middle points:
+       * About sqrt(2) * 256 - correct for a square lattice
+       * approximately correct for everything else.
+        */
+       diagsize = 362;
 
-                       case 'B':
-                               set_cave_feat(y, x, feat_pattern_exit);
-                               break;
+       /* maximum of xsize and ysize */
+       maxsize = (xsize > ysize) ? xsize : ysize;
 
-                       case 'A':
-                               /* Reward for Pattern walk */
-                               object_level = base_level + 12;
-                               place_object(y, x, AM_GOOD | AM_GREAT);
-                               object_level = base_level;
-                               break;
-                       }
+       /* Clear the section */
+       for (i = 0; i <= xsize; i++)
+       {
+               for (j = 0; j <= ysize; j++)
+               {
+                       /* -1 is a flag for "not done yet" */
+                       cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
+                       /* Clear icky flag because may be redoing the cave */
+                       cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
                }
        }
 
+       /* Boundaries are walls */
+       cave[fill_data.ymin][fill_data.xmin].feat = (s16b)maxsize;
+       cave[fill_data.ymax][fill_data.xmin].feat = (s16b)maxsize;
+       cave[fill_data.ymin][fill_data.xmax].feat = (s16b)maxsize;
+       cave[fill_data.ymax][fill_data.xmax].feat = (s16b)maxsize;
 
-       /* Place dungeon monsters and objects */
-       for (t = data, dy = 0; dy < ymax; dy++)
-       {
-               for (dx = 0; dx < xmax; dx++, t++)
-               {
-                       /* prevent loop counter from being overwritten */
-                       i = dx;
-                       j = dy;
+       /* Set the middle square to be an open area. */
+       cave[y0][x0].feat = 0;
 
-                       /* Flip / rotate */
-                       coord_trans(&i, &j, xoffset, yoffset, transno);
+       /* Initialize the step sizes */
+       xstep = xhstep = xsize * 256;
+       ystep = yhstep = ysize * 256;
+       xxsize = xsize * 256;
+       yysize = ysize * 256;
 
-                       /* Extract the location */
-                       if (transno % 2 == 0)
-                       {
-                               /* no swap of x/y */
-                               x = xval - (xmax / 2) + i;
-                               y = yval - (ymax / 2) + j;
-                       }
-                       else
-                       {
-                               /* swap of x/y */
-                               x = xval - (ymax / 2) + i;
-                               y = yval - (xmax / 2) + j;
-                       }
+       /*
+        * Fill in the rectangle with fractal height data -
+        * like the 'plasma fractal' in fractint.
+        */
+       while ((xhstep > 256) || (yhstep > 256))
+       {
+               /* Halve the step sizes */
+               xstep = xhstep;
+               xhstep /= 2;
+               ystep = yhstep;
+               yhstep /= 2;
+
+               /* cache well used values */
+               xstep2 = xstep / 256;
+               ystep2 = ystep / 256;
 
-                       /* Hack -- skip "non-grids" */
-                       if (*t == ' ') continue;
+               xhstep2 = xhstep / 256;
+               yhstep2 = yhstep / 256;
 
-                       /* Analyze the symbol */
-                       switch (*t)
+               /* middle top to bottom. */
+               for (i = xhstep; i <= xxsize - xhstep; i += xstep)
+               {
+                       for (j = 0; j <= yysize; j += ystep)
                        {
-                               /* Monster */
-                               case '&':
-                               {
-                                       monster_level = base_level + 5;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                                       break;
-                               }
+                               /* cache often used values */
+                               ii = i / 256 + fill_data.xmin;
+                               jj = j / 256 + fill_data.ymin;
 
-                               /* Meaner monster */
-                               case '@':
+                               /* Test square */
+                               if (cave[jj][ii].feat == -1)
                                {
-                                       monster_level = base_level + 11;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                                       break;
+                                       if (xhstep2 > grd)
+                                       {
+                                               /* If greater than 'grid' level then is random */
+                                               store_height(ii, jj, randint1(maxsize));
+                                       }
+                                       else
+                                       {
+                                               /* Average of left and right points +random bit */
+                                               store_height(ii, jj,
+                                                       (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
+                                                        + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
+                                                        + (randint1(xstep2) - xhstep2) * roug / 16);
+                                       }
                                }
+                       }
+               }
 
-                               /* Meaner monster, plus treasure */
-                               case '9':
-                               {
-                                       monster_level = base_level + 9;
-                                       place_monster(y, x, PM_ALLOW_SLEEP);
-                                       monster_level = base_level;
-                                       object_level = base_level + 7;
-                                       place_object(y, x, AM_GOOD);
-                                       object_level = base_level;
-                                       break;
-                               }
 
-                               /* Nasty monster and treasure */
-                               case '8':
+               /* middle left to right. */
+               for (j = yhstep; j <= yysize - yhstep; j += ystep)
+               {
+                       for (i = 0; i <= xxsize; i += xstep)
+                       {
+                               /* cache often used values */
+                               ii = i / 256 + fill_data.xmin;
+                               jj = j / 256 + fill_data.ymin;
+
+                               /* Test square */
+                               if (cave[jj][ii].feat == -1)
                                {
-                                       monster_level = base_level + 40;
-                                       place_monster(y, x, PM_ALLOW_SLEEP);
-                                       monster_level = base_level;
-                                       object_level = base_level + 20;
-                                       place_object(y, x, AM_GOOD | AM_GREAT);
-                                       object_level = base_level;
-                                       break;
+                                       if (xhstep2 > grd)
+                                       {
+                                               /* If greater than 'grid' level then is random */
+                                               store_height(ii, jj, randint1(maxsize));
+                                       }
+                                       else
+                                       {
+                                               /* Average of up and down points +random bit */
+                                               store_height(ii, jj,
+                                                       (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
+                                                       + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
+                                                       + (randint1(ystep2) - yhstep2) * roug / 16);
+                                       }
                                }
+                       }
+               }
+
+               /* center. */
+               for (i = xhstep; i <= xxsize - xhstep; i += xstep)
+               {
+                       for (j = yhstep; j <= yysize - yhstep; j += ystep)
+                       {
+                               /* cache often used values */
+                               ii = i / 256 + fill_data.xmin;
+                               jj = j / 256 + fill_data.ymin;
 
-                               /* Monster and/or object */
-                               case ',':
+                               /* Test square */
+                               if (cave[jj][ii].feat == -1)
                                {
-                                       if (randint0(100) < 50)
+                                       if (xhstep2 > grd)
                                        {
-                                               monster_level = base_level + 3;
-                                               place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                               monster_level = base_level;
+                                               /* If greater than 'grid' level then is random */
+                                               store_height(ii, jj, randint1(maxsize));
                                        }
-                                       if (randint0(100) < 50)
+                                       else
                                        {
-                                               object_level = base_level + 7;
-                                               place_object(y, x, 0L);
-                                               object_level = base_level;
+                                               /* Cache reused values. */
+                                               xm = fill_data.xmin + (i - xhstep) / 256;
+                                               xp = fill_data.xmin + (i + xhstep) / 256;
+                                               ym = fill_data.ymin + (j - yhstep) / 256;
+                                               yp = fill_data.ymin + (j + yhstep) / 256;
+
+                                               /* 
+                                                * Average over all four corners + scale by diagsize to
+                                                * reduce the effect of the square grid on the shape of the fractal
+                                                */
+                                               store_height(ii, jj,
+                                                       (cave[ym][xm].feat + cave[yp][xm].feat
+                                                       + cave[ym][xp].feat + cave[yp][xp].feat) / 4
+                                                       + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
                                        }
-                                       break;
                                }
-
                        }
                }
        }
 }
 
 
-/*!
- * @brief タイプ7の部屋…v_info.txtより小型vaultを生成する / Type 7 -- simple vaults (see "v_info.txt")
- * @return なし
- */
-static bool build_type7(void)
+static bool hack_isnt_wall(POSITION y, POSITION x, int c1, int c2, int c3, FEAT_IDX feat1, FEAT_IDX feat2, FEAT_IDX feat3, BIT_FLAGS info1, BIT_FLAGS info2, BIT_FLAGS info3)
 {
-       vault_type *v_ptr = NULL;
-       int dummy;
-       POSITION x, y;
-       POSITION xval, yval;
-       POSITION xoffset, yoffset;
-       int transno;
-
-       /* Pick a lesser vault */
-       for (dummy = 0; dummy < SAFE_MAX_ATTEMPTS; dummy++)
-       {
-               /* Access a random vault record */
-               v_ptr = &v_info[randint0(max_v_idx)];
-
-               /* Accept the first lesser vault */
-               if (v_ptr->typ == 7) break;
-       }
-
-       /* No lesser vault found */
-       if (dummy >= SAFE_MAX_ATTEMPTS)
+       /*
+        * function used to convert from height-map back to the
+        *  normal angband cave format
+        */
+       if (cave[y][x].info & CAVE_ICKY)
        {
-               msg_print_wizard(CHEAT_DUNGEON, _("小型固定Vaultを配置できませんでした。", "Could not place lesser vault."));
+               /* already done */
                return FALSE;
        }
-
-       /* pick type of transformation (0-7) */
-       transno = randint0(8);
-
-       /* calculate offsets */
-       x = v_ptr->wid;
-       y = v_ptr->hgt;
-
-       /* Some huge vault cannot be ratated to fit in the dungeon */
-       if (x+2 > cur_hgt-2)
-       {
-               /* Forbid 90 or 270 degree ratation */
-               transno &= ~1;
-       }
-
-       coord_trans(&x, &y, 0, 0, transno);
-
-       if (x < 0)
-       {
-               xoffset = -x - 1;
-       }
        else
        {
-               xoffset = 0;
-       }
+               /* Show that have looked at this square */
+               cave[y][x].info|= (CAVE_ICKY);
 
-       if (y < 0)
-       {
-               yoffset = -y - 1;
-       }
-       else
-       {
-               yoffset = 0;
+               /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
+               if (cave[y][x].feat <= c1)
+               {
+                       /* 25% of the time use the other tile : it looks better this way */
+                       if (randint1(100) < 75)
+                       {
+                               cave[y][x].feat = feat1;
+                               cave[y][x].info &= ~(CAVE_MASK);
+                               cave[y][x].info |= info1;
+                               return TRUE;
+                       }
+                       else
+                       {
+                               cave[y][x].feat = feat2;
+                               cave[y][x].info &= ~(CAVE_MASK);
+                               cave[y][x].info |= info2;
+                               return TRUE;
+                       }
+               }
+               else if (cave[y][x].feat <= c2)
+               {
+                       /* 25% of the time use the other tile : it looks better this way */
+                       if (randint1(100) < 75)
+                       {
+                               cave[y][x].feat = feat2;
+                               cave[y][x].info &= ~(CAVE_MASK);
+                               cave[y][x].info |= info2;
+                               return TRUE;
+                       }
+                       else
+                       {
+                               cave[y][x].feat = feat1;
+                               cave[y][x].info &= ~(CAVE_MASK);
+                               cave[y][x].info |= info1;
+                               return TRUE;
+                       }
+               }
+               else if (cave[y][x].feat <= c3)
+               {
+                       cave[y][x].feat = feat3;
+                       cave[y][x].info &= ~(CAVE_MASK);
+                       cave[y][x].info |= info3;
+                       return TRUE;
+               }
+               /* if greater than cutoff then is a wall */
+               else
+               {
+                       place_outer_bold(y, x);
+                       return FALSE;
+               }
        }
+}
 
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&yval, &xval, abs(y), abs(x))) return FALSE;
-
-#ifdef FORCE_V_IDX
-       v_ptr = &v_info[2];
-#endif
-
-       /* Message */
-       msg_format_wizard(CHEAT_DUNGEON, _("小型Vault(%s)を生成しました。", "Lesser vault (%s)."), v_name + v_ptr->name);
 
-       /* Hack -- Build the vault */
-       build_vault(yval, xval, v_ptr->hgt, v_ptr->wid,
-                   v_text + v_ptr->text, xoffset, yoffset, transno);
 
-       return TRUE;
-}
 
-/*!
- * @brief タイプ8の部屋…v_info.txtより大型vaultを生成する / Type 8 -- greater vaults (see "v_info.txt")
- * @return なし
+/*
+ * Quick and nasty fill routine used to find the connected region
+ * of floor in the middle of the cave
  */
-static bool build_type8(void)
+static void cave_fill(POSITION y, POSITION x)
 {
-       vault_type *v_ptr;
-       int dummy;
-       POSITION xval, yval;
-       POSITION x, y;
-       int transno;
-       int xoffset, yoffset;
-
-       /* Pick a greater vault */
-       for (dummy = 0; dummy < SAFE_MAX_ATTEMPTS; dummy++)
-       {
-               /* Access a random vault record */
-               v_ptr = &v_info[randint0(max_v_idx)];
+       int i, j, d;
+       POSITION ty, tx;
 
-               /* Accept the first greater vault */
-               if (v_ptr->typ == 8) break;
-       }
+       int flow_tail = 1;
+       int flow_head = 0;
 
-       /* No greater vault found */
-       if (dummy >= SAFE_MAX_ATTEMPTS)
-       {
-               msg_print_wizard(CHEAT_DUNGEON, _("大型固定Vaultを配置できませんでした。", "Could not place greater vault."));
-               return FALSE;
-       }
 
-       /* pick type of transformation (0-7) */
-       transno = randint0(8);
+       /*** Start Grid ***/
 
-       /* calculate offsets */
-       x = v_ptr->wid;
-       y = v_ptr->hgt;
+       /* Enqueue that entry */
+       temp_y[0] = y;
+       temp_x[0] = x;
 
-       /* Some huge vault cannot be ratated to fit in the dungeon */
-       if (x+2 > cur_hgt-2)
+       /* Now process the queue */
+       while (flow_head != flow_tail)
        {
-               /* Forbid 90 or 270 degree ratation */
-               transno &= ~1;
-       }
+               /* Extract the next entry */
+               ty = temp_y[flow_head];
+               tx = temp_x[flow_head];
 
-       coord_trans(&x, &y, 0, 0, transno);
+               /* Forget that entry */
+               if (++flow_head == TEMP_MAX) flow_head = 0;
 
-       if (x < 0)
-       {
-               xoffset = - x - 1;
-       }
-       else
-       {
-               xoffset = 0;
-       }
+               /* Add the "children" */
+               for (d = 0; d < 8; d++)
+               {
+                       int old_head = flow_tail;
 
-       if (y < 0)
-       {
-               yoffset = - y - 1;
-       }
-       else
-       {
-               yoffset = 0;
-       }
+                       /* Child location */
+                       j = ty + ddy_ddd[d];
+                       i = tx + ddx_ddd[d];
 
-       /*
-        * Try to allocate space for room.  If fails, exit
-        *
-        * Hack -- Prepare a bit larger space (+2, +2) to 
-        * prevent generation of vaults with no-entrance.
-        */
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&yval, &xval, (POSITION)(abs(y) + 2), (POSITION)(abs(x) + 2))) return FALSE;
-
-#ifdef FORCE_V_IDX
-       v_ptr = &v_info[76 + randint1(3)];
-#endif
-
-       msg_format_wizard(CHEAT_DUNGEON, _("大型固定Vault(%s)を生成しました。", "Greater vault (%s)."), v_name + v_ptr->name);
-
-       /* Hack -- Build the vault */
-       build_vault(yval, xval, v_ptr->hgt, v_ptr->wid,
-                   v_text + v_ptr->text, xoffset, yoffset, transno);
-
-       return TRUE;
-}
-
-/*
- * Structure to hold all "fill" data
- */
-
-typedef struct fill_data_type fill_data_type;
-
-struct fill_data_type
-{
-       /* area size */
-       int xmin;
-       int ymin;
-       int xmax;
-       int ymax;
-
-       /* cutoffs */
-       int c1;
-       int c2;
-       int c3;
-
-       /* features to fill with */
-       int feat1;
-       int feat2;
-       int feat3;
-
-       int info1;
-       int info2;
-       int info3;
-
-       /* number of filled squares */
-       int amount;
-};
-
-static fill_data_type fill_data;
-
-
-/* Store routine for the fractal cave generator */
-/* this routine probably should be an inline function or a macro. */
-static void store_height(int x, int y, int val)
-{
-       /* if on boundary set val > cutoff so walls are not as square */
-       if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
-            (x == fill_data.xmax) || (y == fill_data.ymax)) &&
-           (val <= fill_data.c1)) val = fill_data.c1 + 1;
-
-       /* store the value in height-map format */
-       cave[y][x].feat = (s16b)val;
-
-       return;
-}
-
-
-/*
-* Explanation of the plasma fractal algorithm:
-*
-* A grid of points is created with the properties of a 'height-map'
-* This is done by making the corners of the grid have a random value.
-* The grid is then subdivided into one with twice the resolution.
-* The new points midway between two 'known' points can be calculated
-* by taking the average value of the 'known' ones and randomly adding
-* or subtracting an amount proportional to the distance between those
-* points.  The final 'middle' points of the grid are then calculated
-* by averaging all four of the originally 'known' corner points.  An
-* random amount is added or subtracted from this to get a value of the
-* height at that point.  The scaling factor here is adjusted to the
-* slightly larger distance diagonally as compared to orthogonally.
-*
-* This is then repeated recursively to fill an entire 'height-map'
-* A rectangular map is done the same way, except there are different
-* scaling factors along the x and y directions.
-*
-* A hack to change the amount of correlation between points is done using
-* the grd variable.  If the current step size is greater than grd then
-* the point will be random, otherwise it will be calculated by the
-* above algorithm.  This makes a maximum distance at which two points on
-* the height map can affect each other.
-*
-* How fractal caves are made:
-*
-* When the map is complete, a cut-off value is used to create a cave.
-* Heights below this value are "floor", and heights above are "wall".
-* This also can be used to create lakes, by adding more height levels
-* representing shallow and deep water/ lava etc.
-*
-* The grd variable affects the width of passages.
-* The roug variable affects the roughness of those passages
-*
-* The tricky part is making sure the created cave is connected.  This
-* is done by 'filling' from the inside and only keeping the 'filled'
-* floor.  Walls bounding the 'filled' floor are also kept.  Everything
-* else is converted to the normal _extra_.
- */
-
-
-/*
- *  Note that this uses the cave.feat array in a very hackish way
- *  the values are first set to zero, and then each array location
- *  is used as a "heightmap"
- *  The heightmap then needs to be converted back into the "feat" format.
- *
- *  grd=level at which fractal turns on.  smaller gives more mazelike caves
- *  roug=roughness level.  16=normal.  higher values make things more convoluted
- *    small values are good for smooth walls.
- *  size=length of the side of the square cave system.
- */
-static void generate_hmap(int y0, int x0, int xsiz, int ysiz, int grd, int roug, int cutoff)
-{
-       int xhsize, yhsize, xsize, ysize, maxsize;
-
-       /*
-        * fixed point variables- these are stored as 256 x normal value
-        * this gives 8 binary places of fractional part + 8 places of normal part
-        */
-
-       u16b xstep, xhstep, ystep, yhstep;
-       u16b xstep2, xhstep2, ystep2, yhstep2;
-       u16b i, j, ii, jj, diagsize, xxsize, yysize;
-       
-       /* Cache for speed */
-       u16b xm, xp, ym, yp;
-
-       /* redefine size so can change the value if out of range */
-       xsize = xsiz;
-       ysize = ysiz;
-
-       /* Paranoia about size of the system of caves */
-       if (xsize > 254) xsize = 254;
-       if (xsize < 4) xsize = 4;
-       if (ysize > 254) ysize = 254;
-       if (ysize < 4) ysize = 4;
-
-       /* get offsets to middle of array */
-       xhsize = xsize / 2;
-       yhsize = ysize / 2;
-
-       /* fix rounding problem */
-       xsize = xhsize * 2;
-       ysize = yhsize * 2;
-
-       /* get limits of region */
-       fill_data.xmin = x0 - xhsize;
-       fill_data.ymin = y0 - yhsize;
-       fill_data.xmax = x0 + xhsize;
-       fill_data.ymax = y0 + yhsize;
-
-       /* Store cutoff in global for quick access */
-       fill_data.c1 = cutoff;
-
-       /*
-       * Scale factor for middle points:
-       * About sqrt(2) * 256 - correct for a square lattice
-       * approximately correct for everything else.
-        */
-       diagsize = 362;
-
-       /* maximum of xsize and ysize */
-       maxsize = (xsize > ysize) ? xsize : ysize;
-
-       /* Clear the section */
-       for (i = 0; i <= xsize; i++)
-       {
-               for (j = 0; j <= ysize; j++)
-               {
-                       /* -1 is a flag for "not done yet" */
-                       cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
-                       /* Clear icky flag because may be redoing the cave */
-                       cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
-               }
-       }
-
-       /* Boundaries are walls */
-       cave[fill_data.ymin][fill_data.xmin].feat = (s16b)maxsize;
-       cave[fill_data.ymax][fill_data.xmin].feat = (s16b)maxsize;
-       cave[fill_data.ymin][fill_data.xmax].feat = (s16b)maxsize;
-       cave[fill_data.ymax][fill_data.xmax].feat = (s16b)maxsize;
-
-       /* Set the middle square to be an open area. */
-       cave[y0][x0].feat = 0;
-
-       /* Initialize the step sizes */
-       xstep = xhstep = xsize * 256;
-       ystep = yhstep = ysize * 256;
-       xxsize = xsize * 256;
-       yysize = ysize * 256;
-
-       /*
-        * Fill in the rectangle with fractal height data -
-        * like the 'plasma fractal' in fractint.
-        */
-       while ((xhstep > 256) || (yhstep > 256))
-       {
-               /* Halve the step sizes */
-               xstep = xhstep;
-               xhstep /= 2;
-               ystep = yhstep;
-               yhstep /= 2;
-
-               /* cache well used values */
-               xstep2 = xstep / 256;
-               ystep2 = ystep / 256;
-
-               xhstep2 = xhstep / 256;
-               yhstep2 = yhstep / 256;
-
-               /* middle top to bottom. */
-               for (i = xhstep; i <= xxsize - xhstep; i += xstep)
-               {
-                       for (j = 0; j <= yysize; j += ystep)
-                       {
-                               /* cache often used values */
-                               ii = i / 256 + fill_data.xmin;
-                               jj = j / 256 + fill_data.ymin;
-
-                               /* Test square */
-                               if (cave[jj][ii].feat == -1)
-                               {
-                                       if (xhstep2 > grd)
-                                       {
-                                               /* If greater than 'grid' level then is random */
-                                               store_height(ii, jj, randint1(maxsize));
-                                       }
-                                       else
-                                       {
-                                               /* Average of left and right points +random bit */
-                                               store_height(ii, jj,
-                                                       (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
-                                                        + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
-                                                        + (randint1(xstep2) - xhstep2) * roug / 16);
-                                       }
-                               }
-                       }
-               }
-
-
-               /* middle left to right. */
-               for (j = yhstep; j <= yysize - yhstep; j += ystep)
-               {
-                       for (i = 0; i <= xxsize; i += xstep)
-                       {
-                               /* cache often used values */
-                               ii = i / 256 + fill_data.xmin;
-                               jj = j / 256 + fill_data.ymin;
-
-                               /* Test square */
-                               if (cave[jj][ii].feat == -1)
-                               {
-                                       if (xhstep2 > grd)
-                                       {
-                                               /* If greater than 'grid' level then is random */
-                                               store_height(ii, jj, randint1(maxsize));
-                                       }
-                                       else
-                                       {
-                                               /* Average of up and down points +random bit */
-                                               store_height(ii, jj,
-                                                       (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
-                                                       + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
-                                                       + (randint1(ystep2) - yhstep2) * roug / 16);
-                                       }
-                               }
-                       }
-               }
-
-               /* center. */
-               for (i = xhstep; i <= xxsize - xhstep; i += xstep)
-               {
-                       for (j = yhstep; j <= yysize - yhstep; j += ystep)
-                       {
-                               /* cache often used values */
-                               ii = i / 256 + fill_data.xmin;
-                               jj = j / 256 + fill_data.ymin;
-
-                               /* Test square */
-                               if (cave[jj][ii].feat == -1)
-                               {
-                                       if (xhstep2 > grd)
-                                       {
-                                               /* If greater than 'grid' level then is random */
-                                               store_height(ii, jj, randint1(maxsize));
-                                       }
-                                       else
-                                       {
-                                               /* Cache reused values. */
-                                               xm = fill_data.xmin + (i - xhstep) / 256;
-                                               xp = fill_data.xmin + (i + xhstep) / 256;
-                                               ym = fill_data.ymin + (j - yhstep) / 256;
-                                               yp = fill_data.ymin + (j + yhstep) / 256;
-
-                                               /* 
-                                                * Average over all four corners + scale by diagsize to
-                                                * reduce the effect of the square grid on the shape of the fractal
-                                                */
-                                               store_height(ii, jj,
-                                                       (cave[ym][xm].feat + cave[yp][xm].feat
-                                                       + cave[ym][xp].feat + cave[yp][xp].feat) / 4
-                                                       + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
-                                       }
-                               }
-                       }
-               }
-       }
-}
-
-
-static bool hack_isnt_wall(int y, int x, int c1, int c2, int c3, int feat1, int feat2, int feat3, int info1, int info2, int info3)
-{
-       /*
-        * function used to convert from height-map back to the
-        *  normal angband cave format
-        */
-       if (cave[y][x].info & CAVE_ICKY)
-       {
-               /* already done */
-               return FALSE;
-       }
-       else
-       {
-               /* Show that have looked at this square */
-               cave[y][x].info|= (CAVE_ICKY);
-
-               /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
-               if (cave[y][x].feat <= c1)
-               {
-                       /* 25% of the time use the other tile : it looks better this way */
-                       if (randint1(100) < 75)
-                       {
-                               cave[y][x].feat = (s16b)feat1;
-                               cave[y][x].info &= ~(CAVE_MASK);
-                               cave[y][x].info |= info1;
-                               return TRUE;
-                       }
-                       else
-                       {
-                               cave[y][x].feat = (s16b)feat2;
-                               cave[y][x].info &= ~(CAVE_MASK);
-                               cave[y][x].info |= info2;
-                               return TRUE;
-                       }
-               }
-               else if (cave[y][x].feat <= c2)
-               {
-                       /* 25% of the time use the other tile : it looks better this way */
-                       if (randint1(100) < 75)
-                       {
-                               cave[y][x].feat = (s16b)feat2;
-                               cave[y][x].info &= ~(CAVE_MASK);
-                               cave[y][x].info |= info2;
-                               return TRUE;
-                       }
-                       else
-                       {
-                               cave[y][x].feat = (s16b)feat1;
-                               cave[y][x].info &= ~(CAVE_MASK);
-                               cave[y][x].info |= info1;
-                               return TRUE;
-                       }
-               }
-               else if (cave[y][x].feat <= c3)
-               {
-                       cave[y][x].feat = (s16b)feat3;
-                       cave[y][x].info &= ~(CAVE_MASK);
-                       cave[y][x].info |= info3;
-                       return TRUE;
-               }
-               /* if greater than cutoff then is a wall */
-               else
-               {
-                       place_outer_bold(y, x);
-                       return FALSE;
-               }
-       }
-}
-
-
-
-
-/*
- * Quick and nasty fill routine used to find the connected region
- * of floor in the middle of the cave
- */
-static void cave_fill(POSITION y, POSITION x)
-{
-       int i, j, d;
-       int ty, tx;
-
-       int flow_tail = 1;
-       int flow_head = 0;
-
-
-       /*** Start Grid ***/
-
-       /* Enqueue that entry */
-       temp_y[0] = y;
-       temp_x[0] = x;
-
-
-       /* Now process the queue */
-       while (flow_head != flow_tail)
-       {
-               /* Extract the next entry */
-               ty = temp_y[flow_head];
-               tx = temp_x[flow_head];
-
-               /* Forget that entry */
-               if (++flow_head == TEMP_MAX) flow_head = 0;
-
-               /* Add the "children" */
-               for (d = 0; d < 8; d++)
-               {
-                       int old_head = flow_tail;
-
-                       /* Child location */
-                       j = ty + ddy_ddd[d];
-                       i = tx + ddx_ddd[d];
-
-                       /* Paranoia Don't leave the cave */
-                       if (!in_bounds(j, i))
-                       {
-                               /* affect boundary */
-                               cave[j][i].info |= CAVE_ICKY;
-/*                             return; */
-                       }
-
-                       /* If within bounds */
-                       else if ((i > fill_data.xmin) && (i < fill_data.xmax)
-                               && (j > fill_data.ymin) && (j < fill_data.ymax))
-                       {
-                               /* If not a wall or floor done before */
-                               if (hack_isnt_wall(j, i,
-                                       fill_data.c1, fill_data.c2, fill_data.c3,
-                                       fill_data.feat1, fill_data.feat2, fill_data.feat3,
-                                       fill_data.info1, fill_data.info2, fill_data.info3))
-                               {
-                                       /* Enqueue that entry */
-                                       temp_y[flow_tail] = (byte_hack)j;
-                                       temp_x[flow_tail] = (byte_hack)i;
-
-                                       /* Advance the queue */
-                                       if (++flow_tail == TEMP_MAX) flow_tail = 0;
-
-                                       /* Hack -- Overflow by forgetting new entry */
-                                       if (flow_tail == flow_head)
-                                       {
-                                               flow_tail = old_head;
-                                       }
-                                       else
-                                       {
-                                               /* keep tally of size of cave system */
-                                               (fill_data.amount)++;
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               /* affect boundary */
-                               cave[j][i].info |= CAVE_ICKY;
-                       }
-               }
-       }
-}
-
-
-static bool generate_fracave(int y0, int x0, int xsize, int ysize, int cutoff, bool light, bool room)
-{
-       int x, y, i, xhsize, yhsize;
-
-       /* offsets to middle from corner */
-       xhsize = xsize / 2;
-       yhsize = ysize / 2;
-
-
-       /*
-        * select region connected to center of cave system
-        * this gets rid of alot of isolated one-sqaures that
-        * can make teleport traps instadeaths...
-        */
-
-       /* cutoffs */
-       fill_data.c1 = cutoff;
-       fill_data.c2 = 0;
-       fill_data.c3 = 0;
-
-       /* features to fill with */
-       fill_data.feat1 = floor_type[randint0(100)];
-       fill_data.feat2 = floor_type[randint0(100)];
-       fill_data.feat3 = floor_type[randint0(100)];
-
-       fill_data.info1 = CAVE_FLOOR;
-       fill_data.info2 = CAVE_FLOOR;
-       fill_data.info3 = CAVE_FLOOR;
-
-       /* number of filled squares */
-       fill_data.amount = 0;
-
-       cave_fill((byte)y0, (byte)x0);
-
-       /* if tally too small, try again */
-       if (fill_data.amount < 10)
-       {
-               /* too small - clear area and try again later */
-               for (x = 0; x <= xsize; ++x)
-               {
-                       for (y = 0; y <= ysize; ++y)
-                       {
-                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
-                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
-                       }
-               }
-               return FALSE;
-       }
-
-       /*
-        * Do boundarys-check to see if they are next to a filled region
-        * If not then they are set to normal granite
-        * If so then they are marked as room walls.
-        */
-       for (i = 0; i <= xsize; ++i)
-       {
-               /* top boundary */
-               if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
-               {
-                       /* Next to a 'filled' region? - set to be room walls */
-                       place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
-                       if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
-                       cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
-                       place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
-               }
-               else
-               {
-                       /* set to be normal granite */
-                       place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
-               }
-
-               /* bottom boundary */
-               if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
-               {
-                       /* Next to a 'filled' region? - set to be room walls */
-                       place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
-                       if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
-                       cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
-                       place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
-               }
-               else
-               {
-                       /* set to be normal granite */
-                       place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
-               }
-
-               /* clear the icky flag-don't need it any more */
-               cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
-               cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
-       }
-
-       /* Do the left and right boundaries minus the corners (done above) */
-       for (i = 1; i < ysize; ++i)
-       {
-               /* left boundary */
-               if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
-               {
-                       /* room boundary */
-                       place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
-                       if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
-                       cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
-                       place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
-               }
-               else
-               {
-                       /* outside room */
-                       place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
-               }
-               /* right boundary */
-               if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
-               {
-                       /* room boundary */
-                       place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
-                       if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
-                       cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
-                       place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
-               }
-               else
-               {
-                       /* outside room */
-                       place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
-               }
-
-               /* clear icky flag -done with it */
-               cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
-               cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
-       }
-
-
-       /* Do the rest: convert back to the normal format */
-       for (x = 1; x < xsize; ++x)
-       {
-               for (y = 1; y < ysize; ++y)
-               {
-                       if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
-                           (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
-                       {
-                               /* Clear the icky flag in the filled region */
-                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
-
-                               /* Set appropriate flags */
-                               if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
-                               if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
-                       }
-                       else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
-                                (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
-                       {
-                               /* Walls */
-                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
-                               if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
-                               if (room)
-                               {
-                                       cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
-                               }
-                               else
-                               {
-
-                                       place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
-                                       cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
-                               }
-                       }
-                       else
-                       {
-                               /* Clear the unconnected regions */
-                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
-                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
-                       }
-               }
-       }
-
-       /*
-        * XXX XXX XXX There is a slight problem when tunnels pierce the caves:
-        * Extra doors appear inside the system.  (Its not very noticeable though.)
-        * This can be removed by "filling" from the outside in.  This allows a separation
-        * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
-        * The extra effort for what seems to be only a minor thing (even non-existant if you
-        * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
-        * worth it.
-        */
-
-       return TRUE;
-}
-
-
-/*!
- * @brief タイプ9の部屋…フラクタルカーブによる洞窟生成 / Type 9 -- Driver routine to create fractal cave system
- * @return なし
- */
-static bool build_type9(void)
-{
-       int grd, roug, cutoff;
-       POSITION xsize, ysize, y0, x0;
-
-       bool done, light, room;
-
-       /* get size: note 'Evenness'*/
-       xsize = randint1(22) * 2 + 6;
-       ysize = randint1(15) * 2 + 6;
-
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
-       {
-               /* Limit to the minimum room size, and retry */
-               xsize = 8;
-               ysize = 8;
-
-               /* Find and reserve some space in the dungeon.  Get center of room. */
-               if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
-               {
-                       /*
-                        * Still no space?!
-                        * Try normal room
-                        */
-                       return build_type1();
-               }
-       }
-
-       light = done = FALSE;
-       room = TRUE;
-
-       if ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
-
-       while (!done)
-       {
-               /* Note: size must be even or there are rounding problems
-               * This causes the tunnels not to connect properly to the room */
-
-               /* testing values for these parameters feel free to adjust */
-               grd = 1 << (randint0(4));
-
-               /* want average of about 16 */
-               roug = randint1(8) * randint1(4);
-
-               /* about size/2 */
-               cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
-                        randint1(xsize / 4) + randint1(ysize / 4);
-
-               /* make it */
-               generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
-
-               /* Convert to normal format + clean up */
-               done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
-       }
-
-       return TRUE;
-}
-
-#ifdef ALLOW_CAVERNS_AND_LAKES
-/*
- * Builds a cave system in the center of the dungeon.
- */
-void build_cavern(void)
-{
-       int grd, roug, cutoff, xsize, ysize, x0, y0;
-       bool done, light;
-
-       light = done = FALSE;
-       if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
-
-       /* Make a cave the size of the dungeon */
-       xsize = cur_wid - 1;
-       ysize = cur_hgt - 1;
-       x0 = xsize / 2;
-       y0 = ysize / 2;
-
-       /* Paranoia: make size even */
-       xsize = x0 * 2;
-       ysize = y0 * 2;
-
-       while (!done)
-       {
-               /* testing values for these parameters: feel free to adjust */
-               grd = randint1(4) + 4;
-
-               /* want average of about 16 */
-               roug = randint1(8) * randint1(4);
-
-               /* about size/2 */
-               cutoff = xsize / 2;
-
-                /* make it */
-               generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
-
-               /* Convert to normal format+ clean up */
-               done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
-       }
-}
-
-static bool generate_lake(int y0, int x0, int xsize, int ysize, int c1, int c2, int c3, int type)
-{
-       int x, y, i, xhsize, yhsize;
-       int feat1, feat2, feat3;
-
-       /* offsets to middle from corner */
-       xhsize = xsize / 2;
-       yhsize = ysize / 2;
-
-       /* Get features based on type */
-       switch (type)
-       {
-       case LAKE_T_LAVA: /* Lava */
-               feat1 = feat_deep_lava;
-               feat2 = feat_shallow_lava;
-               feat3 = floor_type[randint0(100)];
-               break;
-       case LAKE_T_WATER: /* Water */
-               feat1 = feat_deep_water;
-               feat2 = feat_shallow_water;
-               feat3 = floor_type[randint0(100)];
-               break;
-       case LAKE_T_CAVE: /* Collapsed cave */
-               feat1 = floor_type[randint0(100)];
-               feat2 = floor_type[randint0(100)];
-               feat3 = feat_rubble;
-               break;
-       case LAKE_T_EARTH_VAULT: /* Earth vault */
-               feat1 = feat_rubble;
-               feat2 = floor_type[randint0(100)];
-               feat3 = feat_rubble;
-               break;
-       case LAKE_T_AIR_VAULT: /* Air vault */
-               feat1 = feat_grass;
-               feat2 = feat_tree;
-               feat3 = feat_grass;
-               break;
-       case LAKE_T_WATER_VAULT: /* Water vault */
-               feat1 = feat_shallow_water;
-               feat2 = feat_deep_water;
-               feat3 = feat_shallow_water;
-               break;
-       case LAKE_T_FIRE_VAULT: /* Fire Vault */
-               feat1 = feat_shallow_lava;
-               feat2 = feat_deep_lava;
-               feat3 = feat_shallow_lava;
-               break;
-
-       /* Paranoia */
-       default: return FALSE;
-       }
-
-       /*
-        * select region connected to center of cave system
-        * this gets rid of alot of isolated one-sqaures that
-        * can make teleport traps instadeaths...
-        */
-
-       /* cutoffs */
-       fill_data.c1 = c1;
-       fill_data.c2 = c2;
-       fill_data.c3 = c3;
-
-       /* features to fill with */
-       fill_data.feat1 = feat1;
-       fill_data.feat2 = feat2;
-       fill_data.feat3 = feat3;
-
-       fill_data.info1 = 0;
-       fill_data.info2 = 0;
-       fill_data.info3 = 0;
-
-       /* number of filled squares */
-       fill_data.amount = 0;
-
-       /* select region connected to center of cave system
-       * this gets rid of alot of isolated one-sqaures that
-       * can make teleport traps instadeaths... */
-       cave_fill((byte)y0, (byte)x0);
-
-       /* if tally too small, try again */
-       if (fill_data.amount < 10)
-       {
-               /* too small -clear area and try again later */
-               for (x = 0; x <= xsize; ++x)
-               {
-                       for (y = 0; y <= ysize; ++y)
-                       {
-                               place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
-                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
-                       }
-               }
-               return FALSE;
-       }
-
-       /* Do boundarys- set to normal granite */
-       for (i = 0; i <= xsize; ++i)
-       {
-               place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
-               place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
-
-               /* clear the icky flag-don't need it any more */
-               cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
-               cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
-       }
-
-       /* Do the left and right boundaries minus the corners (done above) */
-
-       for (i = 1; i < ysize; ++i)
-       {
-               place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
-               place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
-
-               /* clear icky flag -done with it */
-               cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
-               cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
-       }
-
-
-       /* Do the rest: convert back to the normal format */
-       for (x = 1; x < xsize; ++x)
-       {
-               for (y = 1; y < ysize; ++y)
-               {
-                       /* Fill unconnected regions with granite */
-                       if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
-                               is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
-                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
-
-                       /* turn off icky flag (no longer needed.) */
-                       cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
-
-                       /* Light lava */
-                       if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
-                       {
-                               if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
-                       }
-               }
-       }
-
-       return TRUE;
-}
-
-
-/*
- * makes a lake/collapsed cave system in the center of the dungeon
- */
-void build_lake(int type)
-{
-       int grd, roug, xsize, ysize, x0, y0;
-       bool done = FALSE;
-       int c1, c2, c3;
-
-       /* paranoia - exit if lake type out of range. */
-       if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
-       {
-               msg_format("Invalid lake type (%d)", type);
-               return;
-       }
-
-       /* Make the size of the dungeon */
-       xsize = cur_wid - 1;
-       ysize = cur_hgt - 1;
-       x0 = xsize / 2;
-       y0 = ysize / 2;
-
-       /* Paranoia: make size even */
-       xsize = x0 * 2;
-       ysize = y0 * 2;
-
-       while (!done)
-       {
-               /* testing values for these parameters: feel free to adjust */
-               grd = randint1(3) + 4;
-
-               /* want average of about 16 */
-               roug = randint1(8) * randint1(4);
-
-               /* Make up size of various componants */
-               /* Floor */
-               c3 = 3 * xsize / 4;
-
-               /* Deep water/lava */
-               c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
-
-               /* Shallow boundary */
-               c2 = (c1 + c3) / 2;
-
-               /* make it */
-               generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
-
-               /* Convert to normal format+ clean up */
-               done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
-       }
-}
-#endif /* ALLOW_CAVERNS_AND_LAKES */
-
-
-/*
- * 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.
- */
-static 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);
-       }
-}
-
-
-/*
- * Routine that fills the empty areas of a room with treasure and monsters.
- */
-static void fill_treasure(int x1, int x2, int y1, int y2, int difficulty)
-{
-       int x, y, cx, cy, size;
-       s32b value;
-
-       /* center of room:*/
-       cx = (x1 + x2) / 2;
-       cy = (y1 + y2) / 2;
-
-       /* Rough measure of size of vault= sum of lengths of sides */
-       size = abs(x2 - x1) + abs(y2 - y1);
-
-       for (x = x1; x <= x2; x++)
-       {
-               for (y = y1; y <= y2; y++)
-               {
-                       /* Thing added based on distance to center of vault
-                        * Difficulty is 1-easy to 10-hard */
-                       value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
-
-                       /* hack- empty square part of the time */
-                       if ((randint1(100) - difficulty * 3) > 50) value = 20;
-
-                        /* if floor, shallow water and lava */
-                       if (is_floor_bold(y, x) ||
-                           (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
-                       {
-                               /* The smaller 'value' is, the better the stuff */
-                               if (value < 0)
-                               {
-                                       /* Meanest monster + treasure */
-                                       monster_level = base_level + 40;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                                       object_level = base_level + 20;
-                                       place_object(y, x, AM_GOOD);
-                                       object_level = base_level;
-                               }
-                               else if (value < 5)
-                               {
-                                       /* Mean monster +treasure */
-                                       monster_level = base_level + 20;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                                       object_level = base_level + 10;
-                                       place_object(y, x, AM_GOOD);
-                                       object_level = base_level;
-                               }
-                               else if (value < 10)
-                               {
-                                       /* Monster */
-                                       monster_level = base_level + 9;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                               }
-                               else if (value < 17)
-                               {
-                                       /* Intentional Blank space */
-
-                                       /*
-                                        * (Want some of the vault to be empty
-                                        * so have room for group monsters.
-                                        * This is used in the hack above to lower
-                                        * the density of stuff in the vault.)
-                                        */
-                               }
-                               else if (value < 23)
-                               {
-                                       /* Object or trap */
-                                       if (randint0(100) < 25)
-                                       {
-                                               place_object(y, x, 0L);
-                                       }
-                                       else
-                                       {
-                                               place_trap(y, x);
-                                       }
-                               }
-                               else if (value < 30)
-                               {
-                                       /* Monster and trap */
-                                       monster_level = base_level + 5;
-                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       monster_level = base_level;
-                                       place_trap(y, x);
-                               }
-                               else if (value < 40)
-                               {
-                                       /* Monster or object */
-                                       if (randint0(100) < 50)
-                                       {
-                                               monster_level = base_level + 3;
-                                               place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                               monster_level = base_level;
-                                       }
-                                       if (randint0(100) < 50)
-                                       {
-                                               object_level = base_level + 7;
-                                               place_object(y, x, 0L);
-                                               object_level = base_level;
-                                       }
-                               }
-                               else if (value < 50)
-                               {
-                                       /* Trap */
-                                       place_trap(y, x);
-                               }
-                               else
-                               {
-                                       /* Various Stuff */
-
-                                       /* 20% monster, 40% trap, 20% object, 20% blank space */
-                                       if (randint0(100) < 20)
-                                       {
-                                               place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
-                                       }
-                                       else if (randint0(100) < 50)
-                                       {
-                                               place_trap(y, x);
-                                       }
-                                       else if (randint0(100) < 50)
-                                       {
-                                               place_object(y, x, 0L);
-                                       }
-                               }
-
-                       }
-               }
-       }
-}
-
-
-/*
- * This function creates a random vault that looks like a collection of bubbles.
- * It works by getting a set of coordinates that represent the center of each
- * bubble.  The entire room is made by seeing which bubble center is closest. If
- * two centers are equidistant then the square is a wall, otherwise it is a floor.
- * The only exception is for squares really near a center, these are always floor.
- * (It looks better than without this check.)
- *
- * Note: If two centers are on the same point then this algorithm will create a
- *       blank bubble filled with walls. - This is prevented from happening.
- */
-static void build_bubble_vault(int x0, int y0, int xsize, int ysize)
-{
-       #define BUBBLENUM 10            /* number of bubbles */
-
-       /* array of center points of bubbles */
-       coord center[BUBBLENUM];
-
-       int i, j, x, y;
-       u16b min1, min2, temp;
-       bool done;
-
-       /* Offset from center to top left hand corner */
-       int xhsize = xsize / 2;
-       int yhsize = ysize / 2;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("泡型ランダムVaultを生成しました。", "Room Vault."));
-
-       /* Allocate center of bubbles */
-       center[0].x = (byte)randint1(xsize - 3) + 1;
-       center[0].y = (byte)randint1(ysize - 3) + 1;
-
-       for (i = 1; i < BUBBLENUM; i++)
-       {
-               done = FALSE;
-
-               /* get center and check to see if it is unique */
-               while (!done)
-               {
-                       done = TRUE;
-
-                       x = randint1(xsize - 3) + 1;
-                       y = randint1(ysize - 3) + 1;
-
-                       for (j = 0; j < i; j++)
-                       {
-                               /* rough test to see if there is an overlap */
-                               if ((x == center[j].x) && (y == center[j].y)) done = FALSE;
-                       }
-               }
-
-               center[i].x = (byte_hack)x;
-               center[i].y = (byte_hack)y;
-       }
-
-
-       /* Top and bottom boundaries */
-       for (i = 0; i < xsize; i++)
-       {
-               int side_x = x0 - xhsize + i;
-
-               place_outer_noperm_bold(y0 - yhsize + 0, side_x);
-               cave[y0 - yhsize + 0][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
-               place_outer_noperm_bold(y0 - yhsize + ysize - 1, side_x);
-               cave[y0 - yhsize + ysize - 1][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
-       }
-
-       /* Left and right boundaries */
-       for (i = 1; i < ysize - 1; i++)
-       {
-               int side_y = y0 - yhsize + i;
-
-               place_outer_noperm_bold(side_y, x0 - xhsize + 0);
-               cave[side_y][x0 - xhsize + 0].info |= (CAVE_ROOM | CAVE_ICKY);
-               place_outer_noperm_bold(side_y, x0 - xhsize + xsize - 1);
-               cave[side_y][x0 - xhsize + xsize - 1].info |= (CAVE_ROOM | CAVE_ICKY);
-       }
-
-       /* Fill in middle with bubbles */
-       for (x = 1; x < xsize - 1; x++)
-       {
-               for (y = 1; y < ysize - 1; y++)
-               {
-                       /* Get distances to two closest centers */
-
-                       /* initialize */
-                       min1 = (u16b)distance(x, y, center[0].x, center[0].y);
-                       min2 = (u16b)distance(x, y, center[1].x, center[1].y);
-
-                       if (min1 > min2)
-                       {
-                               /* swap if in wrong order */
-                               temp = min1;
-                               min1 = min2;
-                               min2 = temp;
-                       }
-
-                       /* Scan the rest */
-                       for (i = 2; i < BUBBLENUM; i++)
-                       {
-                               temp = (u16b)distance(x, y, center[i].x, center[i].y);
-
-                               if (temp < min1)
-                               {
-                                       /* smallest */
-                                       min2 = min1;
-                                       min1 = temp;
-                               }
-                               else if (temp < min2)
-                               {
-                                       /* second smallest */
-                                       min2 = temp;
-                               }
-                       }
-                       if (((min2 - min1) <= 2) && (!(min1 < 3)))
-                       {
-                               /* Boundary at midpoint+ not at inner region of bubble */
-                               place_outer_noperm_bold(y0 - yhsize + y, x0 - xhsize + x);
-                       }
-                       else
-                       {
-                               /* middle of a bubble */
-                               place_floor_bold(y0 - yhsize + y, x0 - xhsize + x);
-                       }
-
-                       /* clean up rest of flags */
-                       cave[y0 - yhsize + y][x0 - xhsize + x].info |= (CAVE_ROOM | CAVE_ICKY);
-               }
-       }
-
-       /* Try to add some random doors */
-       for (i = 0; i < 500; i++)
-       {
-               x = randint1(xsize - 3) - xhsize + x0 + 1;
-               y = randint1(ysize - 3) - yhsize + y0 + 1;
-               add_door(x, y);
-       }
-
-       /* Fill with monsters and treasure, low difficulty */
-       fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5));
-}
-
-
-/*
- * Overlay a rectangular room given its bounds
- * This routine is used by build_room_vault
- * The area inside the walls is not touched:
- * only granite is removed- normal walls stay
- */
-static void build_room(int x1, int x2, int y1, int y2)
-{
-       int x, y, i, xsize, ysize, temp;
-
-       /* Check if rectangle has no width */
-       if ((x1 == x2) || (y1 == y2)) return;
-
-       /* initialize */
-       if (x1 > x2)
-       {
-               /* Swap boundaries if in wrong order */
-               temp = x1;
-               x1 = x2;
-               x2 = temp;
-       }
-
-       if (y1 > y2)
-       {
-               /* Swap boundaries if in wrong order */
-               temp = y1;
-               y1 = y2;
-               y2 = temp;
-       }
-
-       /* get total widths */
-       xsize = x2 - x1;
-       ysize = y2 - y1;
-
-
-       /* Top and bottom boundaries */
-       for (i = 0; i <= xsize; i++)
-       {
-               place_outer_noperm_bold(y1, x1 + i);
-               cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
-               place_outer_noperm_bold(y2, x1 + i);
-               cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
-       }
-
-       /* Left and right boundaries */
-       for (i = 1; i < ysize; i++)
-       {
-               place_outer_noperm_bold(y1 + i, x1);
-               cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
-               place_outer_noperm_bold(y1 + i, x2);
-               cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
-       }
-
-       /* Middle */
-       for (x = 1; x < xsize; x++)
-       {
-               for (y = 1; y < ysize; y++)
-               {
-                       if (is_extra_bold(y1+y, x1+x))
-                       {
-                               /* clear the untouched region */
-                               place_floor_bold(y1 + y, x1 + x);
-                               cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
-                       }
-                       else
-                       {
-                               /* make it a room- but don't touch */
-                               cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
-                       }
-               }
-       }
-}
-
-
-/* Create a random vault that looks like a collection of overlapping rooms */
-
-static void build_room_vault(int x0, int y0, int xsize, int ysize)
-{
-       int i, x1, x2, y1, y2, xhsize, yhsize;
-
-       /* get offset from center */
-       xhsize = xsize / 2;
-       yhsize = ysize / 2;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("部屋型ランダムVaultを生成しました。", "Room Vault."));
-
-       /* fill area so don't get problems with arena levels */
-       for (x1 = 0; x1 < xsize; x1++)
-       {
-               int x = x0 - xhsize + x1;
-
-               for (y1 = 0; y1 < ysize; y1++)
-               {
-                       int y = y0 - yhsize + y1;
-
-                       place_extra_bold(y, x);
-                       cave[y][x].info &= (~CAVE_ICKY);
-               }
-       }
-
-       /* add ten random rooms */
-       for (i = 0; i < 10; i++)
-       {
-               x1 = randint1(xhsize) * 2 + x0 - xhsize;
-               x2 = randint1(xhsize) * 2 + x0 - xhsize;
-               y1 = randint1(yhsize) * 2 + y0 - yhsize;
-               y2 = randint1(yhsize) * 2 + y0 - yhsize;
-               build_room(x1, x2, y1, y2);
-       }
-
-       /* Add some random doors */
-       for (i = 0; i < 500; i++)
-       {
-               x1 = randint1(xsize - 3) - xhsize + x0 + 1;
-               y1 = randint1(ysize - 3) - yhsize + y0 + 1;
-               add_door(x1, y1);
-       }
-
-       /* Fill with monsters and treasure, high difficulty */
-       fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5) + 5);
-}
-
-
-/* Create a random vault out of a fractal cave */
-static void build_cave_vault(int x0, int y0, int xsiz, int ysiz)
-{
-       int grd, roug, cutoff, xhsize, yhsize, xsize, ysize, x, y;
-       bool done, light, room;
-
-       /* round to make sizes even */
-       xhsize = xsiz / 2;
-       yhsize = ysiz / 2;
-       xsize = xhsize * 2;
-       ysize = yhsize * 2;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("洞穴ランダムVaultを生成しました。", "Cave Vault."));
-
-       light = done = FALSE;
-       room = TRUE;
-
-       while (!done)
-       {
-               /* testing values for these parameters feel free to adjust */
-               grd = 1 << randint0(4);
-
-               /* want average of about 16 */
-               roug = randint1(8) * randint1(4);
-
-               /* about size/2 */
-               cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
-                        randint1(xsize / 4) + randint1(ysize / 4);
-
-               /* make it */
-               generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
-
-               /* Convert to normal format+ clean up */
-               done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
-       }
-
-       /* Set icky flag because is a vault */
-       for (x = 0; x <= xsize; x++)
-       {
-               for (y = 0; y <= ysize; y++)
-               {
-                       cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
-               }
-       }
-
-       /* Fill with monsters and treasure, low difficulty */
-       fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1, y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
-}
-
-/*
- * maze vault -- rectangular labyrinthine rooms
- *
- * maze vault uses two routines:
- *    r_visit - a recursive routine that builds the labyrinth
- *    build_maze_vault - a driver routine that calls r_visit and adds
- *                   monsters, traps and treasure
- *
- * The labyrinth is built by creating a spanning tree of a graph.
- * The graph vertices are at
- *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
- * and the edges are the vertical and horizontal nearest neighbors.
- *
- * The spanning tree is created by performing a suitably randomized
- * depth-first traversal of the graph. The only adjustable parameter
- * is the randint0(3) below; it governs the relative density of
- * twists and turns in the labyrinth: smaller number, more twists.
- */
-static void r_visit(int y1, int x1, int y2, int x2,
-                   int node, int dir, int *visited)
-{
-       int i, j, m, n, temp, x, y, adj[4];
-
-       /* dimensions of vertex array */
-       m = (x2 - x1) / 2 + 1;
-       n = (y2 - y1) / 2 + 1;
-
-       /* mark node visited and set it to a floor */
-       visited[node] = 1;
-       x = 2 * (node % m) + x1;
-       y = 2 * (node / m) + y1;
-       place_floor_bold(y, x);
-
-       /* setup order of adjacent node visits */
-       if (one_in_(3))
-       {
-               /* pick a random ordering */
-               for (i = 0; i < 4; i++)
-                       adj[i] = i;
-               for (i = 0; i < 4; i++)
-               {
-                       j = randint0(4);
-                       temp = adj[i];
-                       adj[i] = adj[j];
-                       adj[j] = temp;
-               }
-               dir = adj[0];
-       }
-       else
-       {
-               /* pick a random ordering with dir first */
-               adj[0] = dir;
-               for (i = 1; i < 4; i++)
-                       adj[i] = i;
-               for (i = 1; i < 4; i++)
-               {
-                       j = 1 + randint0(3);
-                       temp = adj[i];
-                       adj[i] = adj[j];
-                       adj[j] = temp;
-               }
-       }
-
-       for (i = 0; i < 4; i++)
-       {
-               switch (adj[i])
-               {
-                       case 0:
-                               /* (0,+) - check for bottom boundary */
-                               if ((node / m < n - 1) && (visited[node + m] == 0))
-                               {
-                                       place_floor_bold(y + 1, x);
-                                       r_visit(y1, x1, y2, x2, node + m, dir, visited);
-                               }
-                               break;
-                       case 1:
-                               /* (0,-) - check for top boundary */
-                               if ((node / m > 0) && (visited[node - m] == 0))
-                               {
-                                       place_floor_bold(y - 1, x);
-                                       r_visit(y1, x1, y2, x2, node - m, dir, visited);
-                               }
-                               break;
-                       case 2:
-                               /* (+,0) - check for right boundary */
-                               if ((node % m < m - 1) && (visited[node + 1] == 0))
-                               {
-                                       place_floor_bold(y, x + 1);
-                                       r_visit(y1, x1, y2, x2, node + 1, dir, visited);
-                               }
-                               break;
-                       case 3:
-                               /* (-,0) - check for left boundary */
-                               if ((node % m > 0) && (visited[node - 1] == 0))
-                               {
-                                       place_floor_bold(y, x - 1);
-                                       r_visit(y1, x1, y2, x2, node - 1, dir, visited);
-                               }
-               } /* end switch */
-       }
-}
-
-
-void build_maze_vault(int x0, int y0, int xsize, int ysize, bool is_vault)
-{
-       int y, x, dy, dx;
-       int y1, x1, y2, x2;
-       int m, n, num_vertices, *visited;
-       bool light;
-       cave_type *c_ptr;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
-
-       /* Choose lite or dark */
-       light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
-
-       /* Pick a random room size - randomized by calling routine */
-       dy = ysize / 2 - 1;
-       dx = xsize / 2 - 1;
-
-       y1 = y0 - dy;
-       x1 = x0 - dx;
-       y2 = y0 + dy;
-       x2 = x0 + dx;
-
-       /* generate the room */
-       for (y = y1 - 1; y <= y2 + 1; y++)
-       {
-               for (x = x1 - 1; x <= x2 + 1; x++)
-               {
-                       c_ptr = &cave[y][x];
-                       c_ptr->info |= CAVE_ROOM;
-                       if (is_vault) c_ptr->info |= CAVE_ICKY;
-                       if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
+                       /* Paranoia Don't leave the cave */
+                       if (!in_bounds(j, i))
                        {
-                               place_outer_grid(c_ptr);
+                               /* affect boundary */
+                               cave[j][i].info |= CAVE_ICKY;
+/*                             return; */
                        }
-                       else if (!is_vault)
+
+                       /* If within bounds */
+                       else if ((i > fill_data.xmin) && (i < fill_data.xmax)
+                               && (j > fill_data.ymin) && (j < fill_data.ymax))
                        {
-                               place_extra_grid(c_ptr);
+                               /* If not a wall or floor done before */
+                               if (hack_isnt_wall(j, i,
+                                       fill_data.c1, fill_data.c2, fill_data.c3,
+                                       fill_data.feat1, fill_data.feat2, fill_data.feat3,
+                                       fill_data.info1, fill_data.info2, fill_data.info3))
+                               {
+                                       /* Enqueue that entry */
+                                       temp_y[flow_tail] = (byte_hack)j;
+                                       temp_x[flow_tail] = (byte_hack)i;
+
+                                       /* Advance the queue */
+                                       if (++flow_tail == TEMP_MAX) flow_tail = 0;
+
+                                       /* Hack -- Overflow by forgetting new entry */
+                                       if (flow_tail == flow_head)
+                                       {
+                                               flow_tail = old_head;
+                                       }
+                                       else
+                                       {
+                                               /* keep tally of size of cave system */
+                                               (fill_data.amount)++;
+                                       }
+                               }
                        }
                        else
                        {
-                               place_inner_grid(c_ptr);
+                               /* affect boundary */
+                               cave[j][i].info |= CAVE_ICKY;
                        }
-                       if (light) c_ptr->info |= (CAVE_GLOW);
                }
        }
-
-       /* dimensions of vertex array */
-       m = dx + 1;
-       n = dy + 1;
-       num_vertices = m * n;
-
-       /* initialize array of visited vertices */
-       C_MAKE(visited, num_vertices, int);
-
-       /* traverse the graph to create a spaning tree, pick a random root */
-       r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
-
-       /* Fill with monsters and treasure, low difficulty */
-       if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
-
-       C_KILL(visited, num_vertices, int);
 }
 
 
-/* Build a "mini" checkerboard vault
- *
- * This is done by making a permanent wall maze and setting
- * the diagonal sqaures of the checker board to be granite.
- * The vault has two entrances on opposite sides to guarantee
- * a way to get in even if the vault abuts a side of the dungeon.
- */
-static void build_mini_c_vault(int x0, int y0, int xsize, int ysize)
+bool generate_fracave(POSITION y0, POSITION x0, POSITION xsize, POSITION ysize, int cutoff, bool light, bool room)
 {
-       int dy, dx;
-       int y1, x1, y2, x2, y, x, total;
-       int m, n, num_vertices;
-       int *visited;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("小型チェッカーランダムVaultを生成しました。", "Mini Checker Board Vault."));
-
-       /* Pick a random room size */
-       dy = ysize / 2 - 1;
-       dx = xsize / 2 - 1;
-
-       y1 = y0 - dy;
-       x1 = x0 - dx;
-       y2 = y0 + dy;
-       x2 = x0 + dx;
-
-
-       /* generate the room */
-       for (x = x1 - 2; x <= x2 + 2; x++)
-       {
-               if (!in_bounds(y1-2,x)) break;
+       POSITION x, y, xhsize, yhsize;
+       int i;
 
-               cave[y1-2][x].info |= (CAVE_ROOM | CAVE_ICKY);
+       /* offsets to middle from corner */
+       xhsize = xsize / 2;
+       yhsize = ysize / 2;
 
-               place_outer_noperm_bold(y1-2, x);
-       }
 
-       for (x = x1 - 2; x <= x2 + 2; x++)
-       {
-               if (!in_bounds(y2+2,x)) break;
+       /*
+        * select region connected to center of cave system
+        * this gets rid of alot of isolated one-sqaures that
+        * can make teleport traps instadeaths...
+        */
 
-               cave[y2+2][x].info |= (CAVE_ROOM | CAVE_ICKY);
+       /* cutoffs */
+       fill_data.c1 = cutoff;
+       fill_data.c2 = 0;
+       fill_data.c3 = 0;
 
-               place_outer_noperm_bold(y2+2, x);
-       }
+       /* features to fill with */
+       fill_data.feat1 = floor_type[randint0(100)];
+       fill_data.feat2 = floor_type[randint0(100)];
+       fill_data.feat3 = floor_type[randint0(100)];
 
-       for (y = y1 - 2; y <= y2 + 2; y++)
-       {
-               if (!in_bounds(y,x1-2)) break;
+       fill_data.info1 = CAVE_FLOOR;
+       fill_data.info2 = CAVE_FLOOR;
+       fill_data.info3 = CAVE_FLOOR;
 
-               cave[y][x1-2].info |= (CAVE_ROOM | CAVE_ICKY);
+       /* number of filled squares */
+       fill_data.amount = 0;
 
-               place_outer_noperm_bold(y, x1-2);
-       }
+       cave_fill((byte)y0, (byte)x0);
 
-       for (y = y1 - 2; y <= y2 + 2; y++)
+       /* if tally too small, try again */
+       if (fill_data.amount < 10)
        {
-               if (!in_bounds(y,x2+2)) break;
-
-               cave[y][x2+2].info |= (CAVE_ROOM | CAVE_ICKY);
-
-               place_outer_noperm_bold(y, x2+2);
+               /* too small - clear area and try again later */
+               for (x = 0; x <= xsize; ++x)
+               {
+                       for (y = 0; y <= ysize; ++y)
+                       {
+                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
+                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
+                       }
+               }
+               return FALSE;
        }
 
-       for (y = y1 - 1; y <= y2 + 1; y++)
+       /*
+        * Do boundarys-check to see if they are next to a filled region
+        * If not then they are set to normal granite
+        * If so then they are marked as room walls.
+        */
+       for (i = 0; i <= xsize; ++i)
        {
-               for (x = x1 - 1; x <= x2 + 1; x++)
+               /* top boundary */
+               if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
                {
-                       cave_type *c_ptr = &cave[y][x];
-
-                       c_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
-
-                       /* Permanent walls */
-                       place_inner_perm_grid(c_ptr);
+                       /* Next to a 'filled' region? - set to be room walls */
+                       place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
+                       if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
+                       cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
+                       place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
+               }
+               else
+               {
+                       /* set to be normal granite */
+                       place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
                }
-       }
-
-
-       /* dimensions of vertex array */
-       m = dx + 1;
-       n = dy + 1;
-       num_vertices = m * n;
-
-       /* initialize array of visited vertices */
-       C_MAKE(visited, num_vertices, int);
-
-       /* traverse the graph to create a spannng tree, pick a random root */
-       r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
 
-       /* Make it look like a checker board vault */
-       for (x = x1; x <= x2; x++)
-       {
-               for (y = y1; y <= y2; y++)
+               /* bottom boundary */
+               if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
                {
-                       total = x - x1 + y - y1;
-                       /* If total is odd- and is a floor then make a wall */
-                       if ((total % 2 == 1) && is_floor_bold(y, x))
-                       {
-                               place_inner_bold(y, x);
-                       }
+                       /* Next to a 'filled' region? - set to be room walls */
+                       place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
+                       if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
+                       cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
+                       place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
+               }
+               else
+               {
+                       /* set to be normal granite */
+                       place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
                }
-       }
 
-       /* Make a couple of entrances */
-       if (one_in_(2))
-       {
-               /* left and right */
-               y = randint1(dy) + dy / 2;
-               place_inner_bold(y1 + y, x1 - 1);
-               place_inner_bold(y1 + y, x2 + 1);
-       }
-       else
-       {
-               /* top and bottom */
-               x = randint1(dx) + dx / 2;
-               place_inner_bold(y1 - 1, x1 + x);
-               place_inner_bold(y2 + 1, x1 + x);
+               /* clear the icky flag-don't need it any more */
+               cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
+               cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
        }
 
-       /* Fill with monsters and treasure, highest difficulty */
-       fill_treasure(x1, x2, y1, y2, 10);
-
-       C_KILL(visited, num_vertices, int);
-}
-
-
-/* Build a town/ castle by using a recursive algorithm.
- * Basically divide each region in a probalistic way to create
- * smaller regions.  When the regions get too small stop.
- *
- * The power variable is a measure of how well defended a region is.
- * This alters the possible choices.
- */
-static void build_recursive_room(int x1, int y1, int x2, int y2, int power)
-{
-       int xsize, ysize;
-       int x, y;
-       int choice;
-
-       /* Temp variables */
-       int t1, t2, t3, t4;
-
-       xsize = x2 - x1;
-       ysize = y2 - y1;
-
-       if ((power < 3) && (xsize > 12) && (ysize > 12))
-       {
-               /* Need outside wall +keep */
-               choice = 1;
-       }
-       else
+       /* Do the left and right boundaries minus the corners (done above) */
+       for (i = 1; i < ysize; ++i)
        {
-               if (power < 10)
+               /* left boundary */
+               if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
                {
-                       /* Make rooms + subdivide */
-                       if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
-                       {
-                               choice = 4;
-                       }
-                       else
-                       {
-                               choice = randint1(2) + 1;
-                       }
+                       /* room boundary */
+                       place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
+                       if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
+                       cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
+                       place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
+               }
+               else
+               {
+                       /* outside room */
+                       place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
+               }
+               /* right boundary */
+               if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
+               {
+                       /* room boundary */
+                       place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
+                       if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
+                       cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
+                       place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
                }
                else
                {
-                       /* Mostly subdivide */
-                       choice = randint1(3) + 1;
+                       /* outside room */
+                       place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
                }
+
+               /* clear icky flag -done with it */
+               cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
+               cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
        }
 
-       /* Based on the choice made above, do something */
 
-       switch (choice)
+       /* Do the rest: convert back to the normal format */
+       for (x = 1; x < xsize; ++x)
        {
-               case 1:
+               for (y = 1; y < ysize; ++y)
                {
-                       /* Outer walls */
-
-                       /* top and bottom */
-                       for (x = x1; x <= x2; x++)
+                       if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
+                           (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
                        {
-                               place_outer_bold(y1, x);
-                               place_outer_bold(y2, x);
-                       }
+                               /* Clear the icky flag in the filled region */
+                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
 
-                       /* left and right */
-                       for (y = y1 + 1; y < y2; y++)
-                       {
-                               place_outer_bold(y, x1);
-                               place_outer_bold(y, x2);
+                               /* Set appropriate flags */
+                               if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
+                               if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
                        }
-
-                       /* Make a couple of entrances */
-                       if (one_in_(2))
+                       else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
+                                (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
                        {
-                               /* left and right */
-                               y = randint1(ysize) + y1;
-                               place_floor_bold(y, x1);
-                               place_floor_bold(y, x2);
+                               /* Walls */
+                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
+                               if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
+                               if (room)
+                               {
+                                       cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
+                               }
+                               else
+                               {
+
+                                       place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
+                                       cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
+                               }
                        }
                        else
                        {
-                               /* top and bottom */
-                               x = randint1(xsize) + x1;
-                               place_floor_bold(y1, x);
-                               place_floor_bold(y2, x);
+                               /* Clear the unconnected regions */
+                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
+                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
                        }
+               }
+       }
 
-                       /* Select size of keep */
-                       t1 = randint1(ysize / 3) + y1;
-                       t2 = y2 - randint1(ysize / 3);
-                       t3 = randint1(xsize / 3) + x1;
-                       t4 = x2 - randint1(xsize / 3);
+       /*
+        * There is a slight problem when tunnels pierce the caves:
+        * Extra doors appear inside the system.  (Its not very noticeable though.)
+        * This can be removed by "filling" from the outside in.  This allows a separation
+        * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
+        * The extra effort for what seems to be only a minor thing (even non-existant if you
+        * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
+        * worth it.
+        */
 
-                       /* Do outside areas */
+       return TRUE;
+}
 
-                       /* Above and below keep */
-                       build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
-                       build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
 
-                       /* Left and right of keep */
-                       build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
-                       build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
+#ifdef ALLOW_CAVERNS_AND_LAKES
+/*
+ * Builds a cave system in the center of the dungeon.
+ */
+void build_cavern(void)
+{
+       int grd, roug, cutoff;
+       POSITION xsize, ysize, x0, y0;
+       bool done, light;
 
-                       /* Make the keep itself: */
-                       x1 = t3;
-                       x2 = t4;
-                       y1 = t1;
-                       y2 = t2;
-                       xsize = x2 - x1;
-                       ysize = y2 - y1;
-                       power += 2;
+       light = done = FALSE;
+       if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
 
-                       /* Fall through */
-               }
-               case 4:
-               {
-                       /* Try to build a room */
-                       if ((xsize < 3) || (ysize < 3))
-                       {
-                               for (y = y1; y < y2; y++)
-                               {
-                                       for (x = x1; x < x2; x++)
-                                       {
-                                               place_inner_bold(y, x);
-                                       }
-                               }
+       /* Make a cave the size of the dungeon */
+       xsize = cur_wid - 1;
+       ysize = cur_hgt - 1;
+       x0 = xsize / 2;
+       y0 = ysize / 2;
 
-                               /* Too small */
-                               return;
-                       }
+       /* Paranoia: make size even */
+       xsize = x0 * 2;
+       ysize = y0 * 2;
 
-                       /* Make outside walls */
-                       /* top and bottom */
-                       for (x = x1 + 1; x <= x2 - 1; x++)
-                       {
-                               place_inner_bold(y1 + 1, x);
-                               place_inner_bold(y2 - 1, x);
-                       }
+       while (!done)
+       {
+               /* testing values for these parameters: feel free to adjust */
+               grd = randint1(4) + 4;
 
-                       /* left and right */
-                       for (y = y1 + 1; y <= y2 - 1; y++)
-                       {
-                               place_inner_bold(y, x1 + 1);
-                               place_inner_bold(y, x2 - 1);
-                       }
+               /* want average of about 16 */
+               roug = randint1(8) * randint1(4);
 
-                       /* Make a door */
-                       y = randint1(ysize - 3) + y1 + 1;
+               /* about size/2 */
+               cutoff = xsize / 2;
 
-                       if (one_in_(2))
-                       {
-                               /* left */
-                               place_floor_bold(y, x1 + 1);
-                       }
-                       else
-                       {
-                               /* right */
-                               place_floor_bold(y, x2 - 1);
-                       }
+                /* make it */
+               generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
 
-                       /* Build the room */
-                       build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
-                       break;
-               }
-               case 2:
-               {
-                       /* Try and divide vertically */
-                       if (xsize < 3)
-                       {
-                               /* Too small */
-                               for (y = y1; y < y2; y++)
-                               {
-                                       for (x = x1; x < x2; x++)
-                                       {
-                                               place_inner_bold(y, x);
-                                       }
-                               }
-                               return;
-                       }
+               /* Convert to normal format+ clean up */
+               done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
+       }
+}
 
-                       t1 = randint1(xsize - 2) + x1 + 1;
-                       build_recursive_room(x1, y1, t1, y2, power - 2);
-                       build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
-                       break;
-               }
-               case 3:
-               {
-                       /* Try and divide horizontally */
-                       if (ysize < 3)
-                       {
-                               /* Too small */
-                               for (y = y1; y < y2; y++)
-                               {
-                                       for (x = x1; x < x2; x++)
-                                       {
-                                               place_inner_bold(y, x);
-                                       }
-                               }
-                               return;
-                       }
+bool generate_lake(POSITION y0, POSITION x0, POSITION xsize, POSITION ysize, int c1, int c2, int c3, int type)
+{
+       POSITION x, y, xhsize, yhsize;
+       int i;
+       FEAT_IDX feat1, feat2, feat3;
+
+       /* offsets to middle from corner */
+       xhsize = xsize / 2;
+       yhsize = ysize / 2;
+
+       /* Get features based on type */
+       switch (type)
+       {
+       case LAKE_T_LAVA: /* Lava */
+               feat1 = feat_deep_lava;
+               feat2 = feat_shallow_lava;
+               feat3 = floor_type[randint0(100)];
+               break;
+       case LAKE_T_WATER: /* Water */
+               feat1 = feat_deep_water;
+               feat2 = feat_shallow_water;
+               feat3 = floor_type[randint0(100)];
+               break;
+       case LAKE_T_CAVE: /* Collapsed cave */
+               feat1 = floor_type[randint0(100)];
+               feat2 = floor_type[randint0(100)];
+               feat3 = feat_rubble;
+               break;
+       case LAKE_T_EARTH_VAULT: /* Earth vault */
+               feat1 = feat_rubble;
+               feat2 = floor_type[randint0(100)];
+               feat3 = feat_rubble;
+               break;
+       case LAKE_T_AIR_VAULT: /* Air vault */
+               feat1 = feat_grass;
+               feat2 = feat_tree;
+               feat3 = feat_grass;
+               break;
+       case LAKE_T_WATER_VAULT: /* Water vault */
+               feat1 = feat_shallow_water;
+               feat2 = feat_deep_water;
+               feat3 = feat_shallow_water;
+               break;
+       case LAKE_T_FIRE_VAULT: /* Fire Vault */
+               feat1 = feat_shallow_lava;
+               feat2 = feat_deep_lava;
+               feat3 = feat_shallow_lava;
+               break;
 
-                       t1 = randint1(ysize - 2) + y1 + 1;
-                       build_recursive_room(x1, y1, x2, t1, power - 2);
-                       build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
-                       break;
-               }
+       /* Paranoia */
+       default: return FALSE;
        }
-}
 
+       /*
+        * select region connected to center of cave system
+        * this gets rid of alot of isolated one-sqaures that
+        * can make teleport traps instadeaths...
+        */
 
-/* Build a castle */
+       /* cutoffs */
+       fill_data.c1 = c1;
+       fill_data.c2 = c2;
+       fill_data.c3 = c3;
 
-/* Driver routine: clear the region and call the recursive
-* room routine.
-*
-*This makes a vault that looks like a castle/ city in the dungeon.
-*/
-static void build_castle_vault(int x0, int y0, int xsize, int ysize)
-{
-       int dy, dx;
-       int y1, x1, y2, x2;
-       int y, x;
+       /* features to fill with */
+       fill_data.feat1 = feat1;
+       fill_data.feat2 = feat2;
+       fill_data.feat3 = feat3;
 
-       /* Pick a random room size */
-       dy = ysize / 2 - 1;
-       dx = xsize / 2 - 1;
+       fill_data.info1 = 0;
+       fill_data.info2 = 0;
+       fill_data.info3 = 0;
 
-       y1 = y0 - dy;
-       x1 = x0 - dx;
-       y2 = y0 + dy;
-       x2 = x0 + dx;
+       /* number of filled squares */
+       fill_data.amount = 0;
 
-       msg_print_wizard(CHEAT_DUNGEON, _("城型ランダムVaultを生成しました。", "Castle Vault"));
+       /* select region connected to center of cave system
+       * this gets rid of alot of isolated one-sqaures that
+       * can make teleport traps instadeaths... */
+       cave_fill((byte)y0, (byte)x0);
 
-       /* generate the room */
-       for (y = y1 - 1; y <= y2 + 1; y++)
+       /* if tally too small, try again */
+       if (fill_data.amount < 10)
        {
-               for (x = x1 - 1; x <= x2 + 1; x++)
+               /* too small -clear area and try again later */
+               for (x = 0; x <= xsize; ++x)
                {
-                       cave[y][x].info |= (CAVE_ROOM | CAVE_ICKY);
-                       /* Make everything a floor */
-                       place_floor_bold(y, x);
+                       for (y = 0; y <= ysize; ++y)
+                       {
+                               place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
+                               cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
+                       }
                }
+               return FALSE;
        }
 
-       /* Make the castle */
-       build_recursive_room(x1, y1, x2, y2, randint1(5));
-
-       /* Fill with monsters and treasure, low difficulty */
-       fill_treasure(x1, x2, y1, y2, randint1(3));
-}
-
-
-/*
- * Add outer wall to a floored region
- * Note: no range checking is done so must be inside dungeon
- * This routine also stomps on doors
- */
-static void add_outer_wall(int x, int y, int light, int x1, int y1, int x2, int y2)
-{
-       cave_type *c_ptr;
-       feature_type *f_ptr;
-       int i, j;
+       /* Do boundarys- set to normal granite */
+       for (i = 0; i <= xsize; ++i)
+       {
+               place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
+               place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
 
-       if (!in_bounds(y, x)) return;
+               /* clear the icky flag-don't need it any more */
+               cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
+               cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
+       }
 
-       c_ptr = &cave[y][x];
+       /* Do the left and right boundaries minus the corners (done above) */
 
-       /* hack- check to see if square has been visited before
-       * if so, then exit (use room flag to do this) */
-       if (c_ptr->info & CAVE_ROOM) return;
+       for (i = 1; i < ysize; ++i)
+       {
+               place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
+               place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
 
-       /* set room flag */
-       c_ptr->info |= CAVE_ROOM;
+               /* clear icky flag -done with it */
+               cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
+               cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
+       }
 
-       f_ptr = &f_info[c_ptr->feat];
 
-       if (is_floor_bold(y, x))
+       /* Do the rest: convert back to the normal format */
+       for (x = 1; x < xsize; ++x)
        {
-               for (i = -1; i <= 1; i++)
+               for (y = 1; y < ysize; ++y)
                {
-                       for (j = -1; j <= 1; j++)
+                       /* Fill unconnected regions with granite */
+                       if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
+                               is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
+                               place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
+
+                       /* turn off icky flag (no longer needed.) */
+                       cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
+
+                       /* Light lava */
+                       if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
                        {
-                               if ((x + i >= x1) && (x + i <= x2) &&
-                                        (y + j >= y1) && (y + j <= y2))
-                               {
-                                       add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
-                                       if (light) c_ptr->info |= CAVE_GLOW;
-                               }
+                               if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
                        }
                }
        }
-       else if (is_extra_bold(y, x))
-       {
-               /* Set bounding walls */
-               place_outer_bold(y, x);
-               if (light) c_ptr->info |= CAVE_GLOW;
-       }
-       else if (permanent_wall(f_ptr))
-       {
-               /* Set bounding walls */
-               if (light) c_ptr->info |= CAVE_GLOW;
-       }
+
+       return TRUE;
 }
 
 
 /*
- * Hacked distance formula - gives the 'wrong' answer.
- * Used to build crypts
+ * makes a lake/collapsed cave system in the center of the dungeon
  */
-static int dist2(int x1, int y1, int x2, int y2,
-                int h1, int h2, int h3, int h4)
+void build_lake(int type)
 {
-       int dx, dy;
-       dx = abs(x2 - x1);
-       dy = abs(y2 - y1);
+       int grd, roug, xsize, ysize, x0, y0;
+       bool done = FALSE;
+       int c1, c2, c3;
 
-       /* Basically this works by taking the normal pythagorean formula
-        * and using an expansion to express this in a way without the
-        * square root.  This approximate formula is then perturbed to give
-        * the distorted results.  (I found this by making a mistake when I was
-        * trying to fix the circular rooms.)
-        */
+       /* paranoia - exit if lake type out of range. */
+       if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
+       {
+               msg_format("Invalid lake type (%d)", type);
+               return;
+       }
 
-       /* h1-h4 are constants that describe the metric */
-       if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
-       if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
-       return (((dx + dy) * 128) / 181 +
-               (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
-       /* 128/181 is approx. 1/sqrt(2) */
+       /* Make the size of the dungeon */
+       xsize = cur_wid - 1;
+       ysize = cur_hgt - 1;
+       x0 = xsize / 2;
+       y0 = ysize / 2;
+
+       /* Paranoia: make size even */
+       xsize = x0 * 2;
+       ysize = y0 * 2;
+
+       while (!done)
+       {
+               /* testing values for these parameters: feel free to adjust */
+               grd = randint1(3) + 4;
+
+               /* want average of about 16 */
+               roug = randint1(8) * randint1(4);
+
+               /* Make up size of various componants */
+               /* Floor */
+               c3 = 3 * xsize / 4;
+
+               /* Deep water/lava */
+               c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
+
+               /* Shallow boundary */
+               c2 = (c1 + c3) / 2;
+
+               /* make it */
+               generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
+
+               /* Convert to normal format+ clean up */
+               done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
+       }
 }
+#endif /* ALLOW_CAVERNS_AND_LAKES */
+
 
 
 /*
- * Build target vault.
- * This is made by two concentric "crypts" with perpendicular
- * walls creating the cross-hairs.
+ * Routine that fills the empty areas of a room with treasure and monsters.
  */
-static void build_target_vault(int x0, int y0, int xsize, int ysize)
+void fill_treasure(POSITION x1, POSITION x2, POSITION y1, POSITION y2, int difficulty)
 {
-       int rad, x, y;
-
-       /* Make a random metric */
-       int h1, h2, h3, h4;
-       h1 = randint1(32) - 16;
-       h2 = randint1(16);
-       h3 = randint1(32);
-       h4 = randint1(32) - 16;
+       POSITION x, y, cx, cy, size;
+       s32b value;
 
-       msg_print_wizard(CHEAT_DUNGEON, _("対称形ランダムVaultを生成しました。", "Elemental Vault"));
+       /* center of room:*/
+       cx = (x1 + x2) / 2;
+       cy = (y1 + y2) / 2;
 
-       /* work out outer radius */
-       if (xsize > ysize)
-       {
-               rad = ysize / 2;
-       }
-       else
-       {
-               rad = xsize / 2;
-       }
+       /* Rough measure of size of vault= sum of lengths of sides */
+       size = abs(x2 - x1) + abs(y2 - y1);
 
-       /* Make floor */
-       for (x = x0 - rad; x <= x0 + rad; x++)
+       for (x = x1; x <= x2; x++)
        {
-               for (y = y0 - rad; y <= y0 + rad; y++)
+               for (y = y1; y <= y2; y++)
                {
-                       /* clear room flag */
-                       cave[y][x].info &= ~(CAVE_ROOM);
+                       /* Thing added based on distance to center of vault
+                        * Difficulty is 1-easy to 10-hard */
+                       value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
 
-                       /* Vault - so is "icky" */
-                       cave[y][x].info |= CAVE_ICKY;
+                       /* hack- empty square part of the time */
+                       if ((randint1(100) - difficulty * 3) > 50) value = 20;
 
-                       if (dist2(y0, x0, y, x, h1, h2, h3, h4) <= rad - 1)
-                       {
-                               /* inside- so is floor */
-                               place_floor_bold(y, x);
-                       }
-                       else
+                        /* if floor, shallow water and lava */
+                       if (is_floor_bold(y, x) ||
+                           (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
                        {
-                               /* make granite outside so arena works */
-                               place_extra_bold(y, x);
-                       }
+                               /* The smaller 'value' is, the better the stuff */
+                               if (value < 0)
+                               {
+                                       /* Meanest monster + treasure */
+                                       monster_level = base_level + 40;
+                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                       monster_level = base_level;
+                                       object_level = base_level + 20;
+                                       place_object(y, x, AM_GOOD);
+                                       object_level = base_level;
+                               }
+                               else if (value < 5)
+                               {
+                                       /* Mean monster +treasure */
+                                       monster_level = base_level + 20;
+                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                       monster_level = base_level;
+                                       object_level = base_level + 10;
+                                       place_object(y, x, AM_GOOD);
+                                       object_level = base_level;
+                               }
+                               else if (value < 10)
+                               {
+                                       /* Monster */
+                                       monster_level = base_level + 9;
+                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                       monster_level = base_level;
+                               }
+                               else if (value < 17)
+                               {
+                                       /* Intentional Blank space */
 
-                       /* proper boundary for arena */
-                       if (((y + rad) == y0) || ((y - rad) == y0) ||
-                           ((x + rad) == x0) || ((x - rad) == x0))
-                       {
-                               place_extra_bold(y, x);
-                       }
-               }
-       }
+                                       /*
+                                        * (Want some of the vault to be empty
+                                        * so have room for group monsters.
+                                        * This is used in the hack above to lower
+                                        * the density of stuff in the vault.)
+                                        */
+                               }
+                               else if (value < 23)
+                               {
+                                       /* Object or trap */
+                                       if (randint0(100) < 25)
+                                       {
+                                               place_object(y, x, 0L);
+                                       }
+                                       else
+                                       {
+                                               place_trap(y, x);
+                                       }
+                               }
+                               else if (value < 30)
+                               {
+                                       /* Monster and trap */
+                                       monster_level = base_level + 5;
+                                       place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                       monster_level = base_level;
+                                       place_trap(y, x);
+                               }
+                               else if (value < 40)
+                               {
+                                       /* Monster or object */
+                                       if (randint0(100) < 50)
+                                       {
+                                               monster_level = base_level + 3;
+                                               place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                               monster_level = base_level;
+                                       }
+                                       if (randint0(100) < 50)
+                                       {
+                                               object_level = base_level + 7;
+                                               place_object(y, x, 0L);
+                                               object_level = base_level;
+                                       }
+                               }
+                               else if (value < 50)
+                               {
+                                       /* Trap */
+                                       place_trap(y, x);
+                               }
+                               else
+                               {
+                                       /* Various Stuff */
 
-       /* Find visible outer walls and set to be FEAT_OUTER */
-       add_outer_wall(x0, y0, FALSE, x0 - rad - 1, y0 - rad - 1,
-                      x0 + rad + 1, y0 + rad + 1);
+                                       /* 20% monster, 40% trap, 20% object, 20% blank space */
+                                       if (randint0(100) < 20)
+                                       {
+                                               place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
+                                       }
+                                       else if (randint0(100) < 50)
+                                       {
+                                               place_trap(y, x);
+                                       }
+                                       else if (randint0(100) < 50)
+                                       {
+                                               place_object(y, x, 0L);
+                                       }
+                               }
 
-       /* Add inner wall */
-       for (x = x0 - rad / 2; x <= x0 + rad / 2; x++)
-       {
-               for (y = y0 - rad / 2; y <= y0 + rad / 2; y++)
-               {
-                       if (dist2(y0, x0, y, x, h1, h2, h3, h4) == rad / 2)
-                       {
-                               /* Make an internal wall */
-                               place_inner_bold(y, x);
                        }
                }
        }
-
-       /* Add perpendicular walls */
-       for (x = x0 - rad; x <= x0 + rad; x++)
-       {
-               place_inner_bold(y0, x);
-       }
-
-       for (y = y0 - rad; y <= y0 + rad; y++)
-       {
-               place_inner_bold(y, x0);
-       }
-
-       /* Make inner vault */
-       for (y = y0 - 1; y <= y0 + 1; y++)
-       {
-               place_inner_bold(y, x0 - 1);
-               place_inner_bold(y, x0 + 1);
-       }
-       for (x = x0 - 1; x <= x0 + 1; x++)
-       {
-               place_inner_bold(y0 - 1, x);
-               place_inner_bold(y0 + 1, x);
-       }
-
-       place_floor_bold(y0, x0);
-
-
-       /* Add doors to vault */
-       /* get two distances so can place doors relative to centre */
-       x = (rad - 2) / 4 + 1;
-       y = rad / 2 + x;
-
-       add_door(x0 + x, y0);
-       add_door(x0 + y, y0);
-       add_door(x0 - x, y0);
-       add_door(x0 - y, y0);
-       add_door(x0, y0 + x);
-       add_door(x0, y0 + y);
-       add_door(x0, y0 - x);
-       add_door(x0, y0 - y);
-
-       /* Fill with stuff - medium difficulty */
-       fill_treasure(x0 - rad, x0 + rad, y0 - rad, y0 + rad, randint1(3) + 3);
 }
 
 
-#ifdef ALLOW_CAVERNS_AND_LAKES
+
 /*
- * This routine uses a modified version of the lake code to make a
- * distribution of some terrain type over the vault.  This type
- * depends on the dungeon depth.
- *
- * Miniture rooms are then scattered across the vault.
+ * Overlay a rectangular room given its bounds
+ * This routine is used by build_room_vault
+ * The area inside the walls is not touched:
+ * only granite is removed- normal walls stay
  */
-static void build_elemental_vault(int x0, int y0, int xsiz, int ysiz)
+void build_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2)
 {
-       int grd, roug;
-       int c1, c2, c3;
-       bool done = FALSE;
-       int xsize, ysize, xhsize, yhsize, x, y, i;
-       int type;
-
-       msg_print_wizard(CHEAT_DUNGEON, _("精霊界ランダムVaultを生成しました。", "Elemental Vault"));
-
-       /* round to make sizes even */
-       xhsize = xsiz / 2;
-       yhsize = ysiz / 2;
-       xsize = xhsize * 2;
-       ysize = yhsize * 2;
-
-       if (dun_level < 25)
-       {
-               /* Earth vault  (Rubble) */
-               type = LAKE_T_EARTH_VAULT;
-       }
-       else if (dun_level < 50)
-       {
-               /* Air vault (Trees) */
-               type = LAKE_T_AIR_VAULT;
-       }
-       else if (dun_level < 75)
-       {
-               /* Water vault (shallow water) */
-               type = LAKE_T_WATER_VAULT;
-       }
-       else
-       {
-               /* Fire vault (shallow lava) */
-               type = LAKE_T_FIRE_VAULT;
-       }
-
-       while (!done)
-       {
-               /* testing values for these parameters: feel free to adjust */
-               grd = 1 << (randint0(3));
-
-               /* want average of about 16 */
-               roug = randint1(8) * randint1(4);
-
-               /* Make up size of various componants */
-               /* Floor */
-               c3 = 2 * xsize / 3;
-
-               /* Deep water/lava */
-               c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
-
-               /* Shallow boundary */
-               c2 = (c1 + c3) / 2;
-
-               /* make it */
-               generate_hmap(y0, x0, xsize, ysize, grd, roug, c3);
+       POSITION x, y, xsize, ysize;
+       int i, temp;
 
-               /* Convert to normal format+ clean up */
-               done = generate_lake(y0, x0, xsize, ysize, c1, c2, c3, type);
-       }
+       /* Check if rectangle has no width */
+       if ((x1 == x2) || (y1 == y2)) return;
 
-       /* Set icky flag because is a vault */
-       for (x = 0; x <= xsize; x++)
+       if (x1 > x2)
        {
-               for (y = 0; y <= ysize; y++)
-               {
-                       cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
-               }
+               /* Swap boundaries if in wrong order */
+               temp = x1;
+               x1 = x2;
+               x2 = temp;
        }
 
-       /* make a few rooms in the vault */
-       for (i = 1; i <= (xsize * ysize) / 50; i++)
+       if (y1 > y2)
        {
-               build_small_room(x0 + randint0(xsize - 4) - xsize / 2 + 2,
-                                y0 + randint0(ysize - 4) - ysize / 2 + 2);
+               /* Swap boundaries if in wrong order */
+               temp = y1;
+               y1 = y2;
+               y2 = temp;
        }
 
-       /* Fill with monsters and treasure, low difficulty */
-       fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1,
-                     y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
-}
-#endif /* ALLOW_CAVERNS_AND_LAKES */
-
-
-/*!
- * @brief タイプ10の部屋…ランダム生成vault / Type 10 -- Random vaults
- * @return なし
- */
-static bool build_type10(void)
-{
-       POSITION y0, x0, xsize, ysize, vtype;
-
-       /* Get size */
-       /* big enough to look good, small enough to be fairly common. */
-       xsize = randint1(22) + 22;
-       ysize = randint1(11) + 11;
+       /* get total widths */
+       xsize = x2 - x1;
+       ysize = y2 - y1;
 
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&y0, &x0, ysize + 1, xsize + 1)) return FALSE;
 
-       /* Select type of vault */
-#ifdef ALLOW_CAVERNS_AND_LAKES
-       do
-       {
-               vtype = randint1(15);
-       }
-       while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
-               ((vtype == 1) || (vtype == 3) || (vtype == 8) || (vtype == 9) || (vtype == 11)));
-#else /* ALLOW_CAVERNS_AND_LAKES */
-       do
+       /* Top and bottom boundaries */
+       for (i = 0; i <= xsize; i++)
        {
-               vtype = randint1(7);
+               place_outer_noperm_bold(y1, x1 + i);
+               cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
+               place_outer_noperm_bold(y2, x1 + i);
+               cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
        }
-       while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
-               ((vtype == 1) || (vtype == 3)));
-#endif /* ALLOW_CAVERNS_AND_LAKES */
 
-       switch (vtype)
+       /* Left and right boundaries */
+       for (i = 1; i < ysize; i++)
        {
-               /* Build an appropriate room */
-               case 1: case  9: build_bubble_vault(x0, y0, xsize, ysize); break;
-               case 2: case 10: build_room_vault(x0, y0, xsize, ysize); break;
-               case 3: case 11: build_cave_vault(x0, y0, xsize, ysize); break;
-               case 4: case 12: build_maze_vault(x0, y0, xsize, ysize, TRUE); break;
-               case 5: case 13: build_mini_c_vault(x0, y0, xsize, ysize); break;
-               case 6: case 14: build_castle_vault(x0, y0, xsize, ysize); break;
-               case 7: case 15: build_target_vault(x0, y0, xsize, ysize); break;
-#ifdef ALLOW_CAVERNS_AND_LAKES
-               case 8: build_elemental_vault(x0, y0, xsize, ysize); break;
-#endif /* ALLOW_CAVERNS_AND_LAKES */
-               /* I know how to add a few more... give me some time. */
-
-               /* Paranoia */
-               default: return FALSE;
+               place_outer_noperm_bold(y1 + i, x1);
+               cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
+               place_outer_noperm_bold(y1 + i, x2);
+               cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
        }
 
-       return TRUE;
-}
-
-
-/*!
- * @brief タイプ11の部屋…円形部屋の生成 / Type 11 -- Build an vertical oval room.
- * @return なし
- * @details
- * For every grid in the possible square, check the distance.\n
- * If it's less than the radius, make it a room square.\n
- *\n
- * When done fill from the inside to find the walls,\n
- */
-static bool build_type11(void)
-{
-       POSITION rad, x, y, x0, y0;
-       int light = FALSE;
-
-       /* Occasional light */
-       if ((randint1(dun_level) <= 15) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
-
-       rad = randint0(9);
-
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&y0, &x0, rad * 2 + 1, rad * 2 + 1)) return FALSE;
-
-       /* Make circular floor */
-       for (x = x0 - rad; x <= x0 + rad; x++)
+       /* Middle */
+       for (x = 1; x < xsize; x++)
        {
-               for (y = y0 - rad; y <= y0 + rad; y++)
+               for (y = 1; y < ysize; y++)
                {
-                       if (distance(y0, x0, y, x) <= rad - 1)
+                       if (is_extra_bold(y1+y, x1+x))
                        {
-                               /* inside- so is floor */
-                               place_floor_bold(y, x);
+                               /* clear the untouched region */
+                               place_floor_bold(y1 + y, x1 + x);
+                               cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
                        }
-                       else if (distance(y0, x0, y, x) <= rad + 1)
+                       else
                        {
-                               /* make granite outside so arena works */
-                               place_extra_bold(y, x);
+                               /* make it a room- but don't touch */
+                               cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
                        }
                }
        }
-
-       /* Find visible outer walls and set to be FEAT_OUTER */
-       add_outer_wall(x0, y0, light, x0 - rad, y0 - rad, x0 + rad, y0 + rad);
-
-       return TRUE;
 }
 
 
-/*!
- * @brief タイプ12の部屋…ドーム型部屋の生成 / Type 12 -- Build crypt room.
- * @return なし
- * @details
- * For every grid in the possible square, check the (fake) distance.\n
- * If it's less than the radius, make it a room square.\n
- *\n
- * When done fill from the inside to find the walls,\n
+
+/*
+ * maze vault -- rectangular labyrinthine rooms
+ *
+ * maze vault uses two routines:
+ *    r_visit - a recursive routine that builds the labyrinth
+ *    build_maze_vault - a driver routine that calls r_visit and adds
+ *                   monsters, traps and treasure
+ *
+ * The labyrinth is built by creating a spanning tree of a graph.
+ * The graph vertices are at
+ *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
+ * and the edges are the vertical and horizontal nearest neighbors.
+ *
+ * The spanning tree is created by performing a suitably randomized
+ * depth-first traversal of the graph. The only adjustable parameter
+ * is the randint0(3) below; it governs the relative density of
+ * twists and turns in the labyrinth: smaller number, more twists.
  */
-static bool build_type12(void)
+void r_visit(POSITION y1, POSITION x1, POSITION y2, POSITION x2, int node, DIRECTION dir, int *visited)
 {
-       POSITION rad, x, y, x0, y0;
-       int light = FALSE;
-       bool emptyflag = TRUE;
-
-       /* Make a random metric */
-       int h1, h2, h3, h4;
-       h1 = randint1(32) - 16;
-       h2 = randint1(16);
-       h3 = randint1(32);
-       h4 = randint1(32) - 16;
-
-       /* Occasional light */
-       if ((randint1(dun_level) <= 5) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
+       int i, j, m, n, temp, x, y, adj[4];
 
-       rad = randint1(9);
+       /* dimensions of vertex array */
+       m = (x2 - x1) / 2 + 1;
+       n = (y2 - y1) / 2 + 1;
 
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&y0, &x0, rad * 2 + 3, rad * 2 + 3)) return FALSE;
+       /* mark node visited and set it to a floor */
+       visited[node] = 1;
+       x = 2 * (node % m) + x1;
+       y = 2 * (node / m) + y1;
+       place_floor_bold(y, x);
 
-       /* Make floor */
-       for (x = x0 - rad; x <= x0 + rad; x++)
+       /* setup order of adjacent node visits */
+       if (one_in_(3))
        {
-               for (y = y0 - rad; y <= y0 + rad; y++)
-               {
-                       /* clear room flag */
-                       cave[y][x].info &= ~(CAVE_ROOM);
-
-                       if (dist2(y0, x0, y, x, h1, h2, h3, h4) <= rad - 1)
-                       {
-                               /* inside - so is floor */
-                               place_floor_bold(y, x);
-                       }
-                       else if (distance(y0, x0, y, x) < 3)
-                       {
-                               place_floor_bold(y, x);
-                       }
-                       else
-                       {
-                               /* make granite outside so arena works */
-                               place_extra_bold(y, x);
-                       }
-
-                       /* proper boundary for arena */
-                       if (((y + rad) == y0) || ((y - rad) == y0) ||
-                           ((x + rad) == x0) || ((x - rad) == x0))
-                       {
-                               place_extra_bold(y, x);
-                       }
+               /* pick a random ordering */
+               for (i = 0; i < 4; i++)
+                       adj[i] = i;
+               for (i = 0; i < 4; i++)
+               {
+                       j = randint0(4);
+                       temp = adj[i];
+                       adj[i] = adj[j];
+                       adj[j] = temp;
                }
+               dir = adj[0];
        }
-
-       /* Find visible outer walls and set to be FEAT_OUTER */
-       add_outer_wall(x0, y0, light, x0 - rad - 1, y0 - rad - 1,
-                      x0 + rad + 1, y0 + rad + 1);
-
-       /* Check to see if there is room for an inner vault */
-       for (x = x0 - 2; x <= x0 + 2; x++)
+       else
        {
-               for (y = y0 - 2; y <= y0 + 2; y++)
+               /* pick a random ordering with dir first */
+               adj[0] = dir;
+               for (i = 1; i < 4; i++)
+                       adj[i] = i;
+               for (i = 1; i < 4; i++)
                {
-                       if (!is_floor_bold(y, x))
-                       {
-                               /* Wall in the way */
-                               emptyflag = FALSE;
-                       }
+                       j = 1 + randint0(3);
+                       temp = adj[i];
+                       adj[i] = adj[j];
+                       adj[j] = temp;
                }
        }
 
-       if (emptyflag && one_in_(2))
+       for (i = 0; i < 4; i++)
        {
-               /* Build the vault */
-               build_small_room(x0, y0);
-
-               /* Place a treasure in the vault */
-               place_object(y0, x0, 0L);
-
-               /* Let's guard the treasure well */
-               vault_monsters(y0, x0, randint0(2) + 3);
-
-               /* Traps naturally */
-               vault_traps(y0, x0, 4, 4, randint0(3) + 2);
+               switch (adj[i])
+               {
+                       case 0:
+                               /* (0,+) - check for bottom boundary */
+                               if ((node / m < n - 1) && (visited[node + m] == 0))
+                               {
+                                       place_floor_bold(y + 1, x);
+                                       r_visit(y1, x1, y2, x2, node + m, dir, visited);
+                               }
+                               break;
+                       case 1:
+                               /* (0,-) - check for top boundary */
+                               if ((node / m > 0) && (visited[node - m] == 0))
+                               {
+                                       place_floor_bold(y - 1, x);
+                                       r_visit(y1, x1, y2, x2, node - m, dir, visited);
+                               }
+                               break;
+                       case 2:
+                               /* (+,0) - check for right boundary */
+                               if ((node % m < m - 1) && (visited[node + 1] == 0))
+                               {
+                                       place_floor_bold(y, x + 1);
+                                       r_visit(y1, x1, y2, x2, node + 1, dir, visited);
+                               }
+                               break;
+                       case 3:
+                               /* (-,0) - check for left boundary */
+                               if ((node % m > 0) && (visited[node - 1] == 0))
+                               {
+                                       place_floor_bold(y, x - 1);
+                                       r_visit(y1, x1, y2, x2, node - 1, dir, visited);
+                               }
+               } /* end switch */
        }
-
-       return TRUE;
 }
 
 
-
-/*!
- * @brief タイプ14の部屋…特殊トラップ部屋の生成 / Type 14 -- trapped rooms
- * @return なし
- * @details
- * A special trap is placed at center of the room
- */
-static bool build_type14(void)
+void build_maze_vault(POSITION x0, POSITION y0, POSITION xsize, POSITION ysize, bool is_vault)
 {
-       POSITION y, x, y2, x2, yval, xval;
-       POSITION y1, x1, xsize, ysize;
-
+       POSITION y, x, dy, dx;
+       POSITION y1, x1, y2, x2;
+       int m, n, num_vertices, *visited;
        bool light;
-
        cave_type *c_ptr;
-       s16b trap;
 
-       /* Pick a room size */
-       y1 = randint1(4);
-       x1 = randint1(11);
-       y2 = randint1(3);
-       x2 = randint1(11);
-
-       xsize = x1 + x2 + 1;
-       ysize = y1 + y2 + 1;
-
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
+       msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
 
        /* Choose lite or dark */
-       light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
-
+       light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
 
-       /* Get corner values */
-       y1 = yval - ysize / 2;
-       x1 = xval - xsize / 2;
-       y2 = yval + (ysize - 1) / 2;
-       x2 = xval + (xsize - 1) / 2;
+       /* Pick a random room size - randomized by calling routine */
+       dy = ysize / 2 - 1;
+       dx = xsize / 2 - 1;
 
+       y1 = y0 - dy;
+       x1 = x0 - dx;
+       y2 = y0 + dy;
+       x2 = x0 + dx;
 
-       /* Place a full floor under the room */
+       /* generate the room */
        for (y = y1 - 1; y <= y2 + 1; y++)
        {
                for (x = x1 - 1; x <= x2 + 1; x++)
                {
                        c_ptr = &cave[y][x];
-                       place_floor_grid(c_ptr);
-                       c_ptr->info |= (CAVE_ROOM);
+                       c_ptr->info |= CAVE_ROOM;
+                       if (is_vault) c_ptr->info |= CAVE_ICKY;
+                       if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
+                       {
+                               place_outer_grid(c_ptr);
+                       }
+                       else if (!is_vault)
+                       {
+                               place_extra_grid(c_ptr);
+                       }
+                       else
+                       {
+                               place_inner_grid(c_ptr);
+                       }
                        if (light) c_ptr->info |= (CAVE_GLOW);
                }
        }
 
-       /* Walls around the room */
-       for (y = y1 - 1; y <= y2 + 1; y++)
-       {
-               c_ptr = &cave[y][x1 - 1];
-               place_outer_grid(c_ptr);
-               c_ptr = &cave[y][x2 + 1];
-               place_outer_grid(c_ptr);
-       }
-       for (x = x1 - 1; x <= x2 + 1; x++)
-       {
-               c_ptr = &cave[y1 - 1][x];
-               place_outer_grid(c_ptr);
-               c_ptr = &cave[y2 + 1][x];
-               place_outer_grid(c_ptr);
-       }
-
-       if (dun_level < 30 + randint1(30))
-               trap = feat_trap_piranha;
-       else
-               trap = feat_trap_armageddon;
-
-       /* Place a special trap */
-       c_ptr = &cave[rand_spread(yval, ysize/4)][rand_spread(xval, xsize/4)];
-       c_ptr->mimic = c_ptr->feat;
-       c_ptr->feat = trap;
-
-       msg_format_wizard(CHEAT_DUNGEON, _("%sの部屋が生成されました。", "Room of %s was generated."), f_name + f_info[trap].name);
-
-       return TRUE;
-}
-
-
-/*
- * Helper function for "glass room"
- */
-static bool vault_aux_lite(MONRACE_IDX r_idx)
-{
-       monster_race *r_ptr = &r_info[r_idx];
-
-       /* Validate the monster */
-       if (!vault_monster_okay(r_idx)) return FALSE;
-
-       /* Require lite attack */
-       if (!(r_ptr->flags4 & RF4_BR_LITE) && !(r_ptr->a_ability_flags1 & RF5_BA_LITE)) return FALSE;
-
-       /* No wall passing monsters */
-       if (r_ptr->flags2 & (RF2_PASS_WALL | RF2_KILL_WALL)) return FALSE;
-
-       /* No disintegrating monsters */
-       if (r_ptr->flags4 & RF4_BR_DISI) return FALSE;
-
-       return TRUE;
-}
+       /* dimensions of vertex array */
+       m = dx + 1;
+       n = dy + 1;
+       num_vertices = m * n;
 
-/*
- * Helper function for "glass room"
- */
-static bool vault_aux_shards(MONRACE_IDX r_idx)
-{
-       monster_race *r_ptr = &r_info[r_idx];
+       /* initialize array of visited vertices */
+       C_MAKE(visited, num_vertices, int);
 
-       /* Validate the monster */
-       if (!vault_monster_okay(r_idx)) return FALSE;
+       /* traverse the graph to create a spaning tree, pick a random root */
+       r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
 
-       /* Require shards breath attack */
-       if (!(r_ptr->flags4 & RF4_BR_SHAR)) return FALSE;
+       /* Fill with monsters and treasure, low difficulty */
+       if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
 
-       return TRUE;
+       C_KILL(visited, num_vertices, int);
 }
 
-/*
- * Hack -- determine if a template is potion
- */
-static bool kind_is_potion(KIND_OBJECT_IDX k_idx)
-{
-       return k_info[k_idx].tval == TV_POTION;
-}
 
-/*!
- * @brief タイプ15の部屋…ガラス部屋の生成 / Type 15 -- glass rooms
- * @return なし
+/* Build a town/ castle by using a recursive algorithm.
+ * Basically divide each region in a probalistic way to create
+ * smaller regions.  When the regions get too small stop.
+ *
+ * The power variable is a measure of how well defended a region is.
+ * This alters the possible choices.
  */
-static bool build_type15(void)
+void build_recursive_room(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int power)
 {
-       POSITION y, x, y2, x2, yval, xval;
-       POSITION y1, x1, xsize, ysize;
-       bool light;
-
-       cave_type *c_ptr;
-
-       /* Pick a room size */
-       xsize = rand_range(9, 13);
-       ysize = rand_range(9, 13);
-
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
-
-       /* Choose lite or dark */
-       light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
+       POSITION xsize, ysize;
+       POSITION x, y;
+       int choice;
 
-       /* Get corner values */
-       y1 = yval - ysize / 2;
-       x1 = xval - xsize / 2;
-       y2 = yval + (ysize - 1) / 2;
-       x2 = xval + (xsize - 1) / 2;
+       /* Temp variables */
+       int t1, t2, t3, t4;
 
-       /* Place a full floor under the room */
-       for (y = y1 - 1; y <= y2 + 1; y++)
-       {
-               for (x = x1 - 1; x <= x2 + 1; x++)
-               {
-                       c_ptr = &cave[y][x];
-                       place_floor_grid(c_ptr);
-                       c_ptr->feat = feat_glass_floor;
-                       c_ptr->info |= (CAVE_ROOM);
-                       if (light) c_ptr->info |= (CAVE_GLOW);
-               }
-       }
+       xsize = x2 - x1;
+       ysize = y2 - y1;
 
-       /* Walls around the room */
-       for (y = y1 - 1; y <= y2 + 1; y++)
-       {
-               c_ptr = &cave[y][x1 - 1];
-               place_outer_grid(c_ptr);
-               c_ptr->feat = feat_glass_wall;
-               c_ptr = &cave[y][x2 + 1];
-               place_outer_grid(c_ptr);
-               c_ptr->feat = feat_glass_wall;
-       }
-       for (x = x1 - 1; x <= x2 + 1; x++)
+       if ((power < 3) && (xsize > 12) && (ysize > 12))
        {
-               c_ptr = &cave[y1 - 1][x];
-               place_outer_grid(c_ptr);
-               c_ptr->feat = feat_glass_wall;
-               c_ptr = &cave[y2 + 1][x];
-               place_outer_grid(c_ptr);
-               c_ptr->feat = feat_glass_wall;
+               /* Need outside wall +keep */
+               choice = 1;
        }
-
-       switch (randint1(3))
+       else
        {
-       case 1: /* 4 lite breathers + potion */
+               if (power < 10)
                {
-                       int dir1, dir2;
-
-                       /* Prepare allocation table */
-                       get_mon_num_prep(vault_aux_lite, NULL);
-
-                       /* Place fixed lite berathers */
-                       for (dir1 = 4; dir1 < 8; dir1++)
+                       /* Make rooms + subdivide */
+                       if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
                        {
-                               MONRACE_IDX r_idx = get_mon_num(dun_level);
-
-                               y = yval + 2 * ddy_ddd[dir1];
-                               x = xval + 2 * ddx_ddd[dir1];
-                               if (r_idx) place_monster_aux(0, y, x, r_idx, PM_ALLOW_SLEEP);
-
-                               /* Walls around the breather */
-                               for (dir2 = 0; dir2 < 8; dir2++)
-                               {
-                                       c_ptr = &cave[y + ddy_ddd[dir2]][x + ddx_ddd[dir2]];
-                                       place_inner_grid(c_ptr);
-                                       c_ptr->feat = feat_glass_wall;
-                               }
+                               choice = 4;
                        }
-
-                       /* Walls around the potion */
-                       for (dir1 = 0; dir1 < 4; dir1++)
+                       else
                        {
-                               y = yval + 2 * ddy_ddd[dir1];
-                               x = xval + 2 * ddx_ddd[dir1];
-                               c_ptr = &cave[y][x];
-                               place_inner_perm_grid(c_ptr);
-                               c_ptr->feat = feat_permanent_glass_wall;
-                               cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]].info |= (CAVE_ICKY);
+                               choice = randint1(2) + 1;
                        }
-
-                       /* Glass door */
-                       dir1 = randint0(4);
-                       y = yval + 2 * ddy_ddd[dir1];
-                       x = xval + 2 * ddx_ddd[dir1];
-                       place_secret_door(y, x, DOOR_GLASS_DOOR);
-                       c_ptr = &cave[y][x];
-                       if (is_closed_door(c_ptr->feat)) c_ptr->mimic = feat_glass_wall;
-
-                       /* Place a potion */
-                       get_obj_num_hook = kind_is_potion;
-                       place_object(yval, xval, AM_NO_FIXED_ART);
-                       cave[yval][xval].info |= (CAVE_ICKY);
                }
-               break;
-
-       case 2: /* 1 lite breather + random object */
+               else
                {
-                       MONRACE_IDX r_idx;
-                       DIRECTION dir1;
-
-                       /* Pillars */
-                       c_ptr = &cave[y1 + 1][x1 + 1];
-                       place_inner_grid(c_ptr);
-                       c_ptr->feat = feat_glass_wall;
-
-                       c_ptr = &cave[y1 + 1][x2 - 1];
-                       place_inner_grid(c_ptr);
-                       c_ptr->feat = feat_glass_wall;
-
-                       c_ptr = &cave[y2 - 1][x1 + 1];
-                       place_inner_grid(c_ptr);
-                       c_ptr->feat = feat_glass_wall;
+                       /* Mostly subdivide */
+                       choice = randint1(3) + 1;
+               }
+       }
 
-                       c_ptr = &cave[y2 - 1][x2 - 1];
-                       place_inner_grid(c_ptr);
-                       c_ptr->feat = feat_glass_wall;
+       /* Based on the choice made above, do something */
 
-                       /* Prepare allocation table */
-                       get_mon_num_prep(vault_aux_lite, NULL);
+       switch (choice)
+       {
+               case 1:
+               {
+                       /* Outer walls */
 
-                       r_idx = get_mon_num(dun_level);
-                       if (r_idx) place_monster_aux(0, yval, xval, r_idx, 0L);
+                       /* top and bottom */
+                       for (x = x1; x <= x2; x++)
+                       {
+                               place_outer_bold(y1, x);
+                               place_outer_bold(y2, x);
+                       }
 
-                       /* Walls around the breather */
-                       for (dir1 = 0; dir1 < 8; dir1++)
+                       /* left and right */
+                       for (y = y1 + 1; y < y2; y++)
                        {
-                               c_ptr = &cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
+                               place_outer_bold(y, x1);
+                               place_outer_bold(y, x2);
                        }
 
-                       /* Curtains around the breather */
-                       for (y = yval - 1; y <= yval + 1; y++)
+                       /* Make a couple of entrances */
+                       if (one_in_(2))
                        {
-                               place_closed_door(y, xval - 2, DOOR_CURTAIN);
-                               place_closed_door(y, xval + 2, DOOR_CURTAIN);
+                               /* left and right */
+                               y = randint1(ysize) + y1;
+                               place_floor_bold(y, x1);
+                               place_floor_bold(y, x2);
                        }
-                       for (x = xval - 1; x <= xval + 1; x++)
+                       else
                        {
-                               place_closed_door(yval - 2, x, DOOR_CURTAIN);
-                               place_closed_door(yval + 2, x, DOOR_CURTAIN);
+                               /* top and bottom */
+                               x = randint1(xsize) + x1;
+                               place_floor_bold(y1, x);
+                               place_floor_bold(y2, x);
                        }
 
-                       /* Place an object */
-                       place_object(yval, xval, AM_NO_FIXED_ART);
-                       cave[yval][xval].info |= (CAVE_ICKY);
+                       /* Select size of keep */
+                       t1 = randint1(ysize / 3) + y1;
+                       t2 = y2 - randint1(ysize / 3);
+                       t3 = randint1(xsize / 3) + x1;
+                       t4 = x2 - randint1(xsize / 3);
+
+                       /* Do outside areas */
+
+                       /* Above and below keep */
+                       build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
+                       build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
+
+                       /* Left and right of keep */
+                       build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
+                       build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
+
+                       /* Make the keep itself: */
+                       x1 = t3;
+                       x2 = t4;
+                       y1 = t1;
+                       y2 = t2;
+                       xsize = x2 - x1;
+                       ysize = y2 - y1;
+                       power += 2;
+
+                       /* Fall through */
                }
-               break;
-
-       case 3: /* 4 shards breathers + 2 potions */
+               case 4:
                {
-                       int dir1;
-
-                       /* Walls around the potion */
-                       for (y = yval - 2; y <= yval + 2; y++)
-                       {
-                               c_ptr = &cave[y][xval - 3];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
-                               c_ptr = &cave[y][xval + 3];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
-                       }
-                       for (x = xval - 2; x <= xval + 2; x++)
+                       /* Try to build a room */
+                       if ((xsize < 3) || (ysize < 3))
                        {
-                               c_ptr = &cave[yval - 3][x];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
-                               c_ptr = &cave[yval + 3][x];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
+                               for (y = y1; y < y2; y++)
+                               {
+                                       for (x = x1; x < x2; x++)
+                                       {
+                                               place_inner_bold(y, x);
+                                       }
+                               }
+
+                               /* Too small */
+                               return;
                        }
-                       for (dir1 = 4; dir1 < 8; dir1++)
+
+                       /* Make outside walls */
+                       /* top and bottom */
+                       for (x = x1 + 1; x <= x2 - 1; x++)
                        {
-                               c_ptr = &cave[yval + 2 * ddy_ddd[dir1]][xval + 2 * ddx_ddd[dir1]];
-                               place_inner_grid(c_ptr);
-                               c_ptr->feat = feat_glass_wall;
+                               place_inner_bold(y1 + 1, x);
+                               place_inner_bold(y2 - 1, x);
                        }
 
-                       /* Prepare allocation table */
-                       get_mon_num_prep(vault_aux_shards, NULL);
-
-                       /* Place shard berathers */
-                       for (dir1 = 4; dir1 < 8; dir1++)
+                       /* left and right */
+                       for (y = y1 + 1; y <= y2 - 1; y++)
                        {
-                               MONRACE_IDX r_idx = get_mon_num(dun_level);
-
-                               y = yval + ddy_ddd[dir1];
-                               x = xval + ddx_ddd[dir1];
-                               if (r_idx) place_monster_aux(0, y, x, r_idx, 0L);
+                               place_inner_bold(y, x1 + 1);
+                               place_inner_bold(y, x2 - 1);
                        }
 
-                       /* Place two potions */
+                       /* Make a door */
+                       y = randint1(ysize - 3) + y1 + 1;
+
                        if (one_in_(2))
                        {
-                               get_obj_num_hook = kind_is_potion;
-                               place_object(yval, xval - 1, AM_NO_FIXED_ART);
-                               get_obj_num_hook = kind_is_potion;
-                               place_object(yval, xval + 1, AM_NO_FIXED_ART);
+                               /* left */
+                               place_floor_bold(y, x1 + 1);
                        }
                        else
                        {
-                               get_obj_num_hook = kind_is_potion;
-                               place_object(yval - 1, xval, AM_NO_FIXED_ART);
-                               get_obj_num_hook = kind_is_potion;
-                               place_object(yval + 1, xval, AM_NO_FIXED_ART);
+                               /* right */
+                               place_floor_bold(y, x2 - 1);
                        }
 
-                       for (y = yval - 2; y <= yval + 2; y++)
-                               for (x = xval - 2; x <= xval + 2; x++)
-                                       cave[y][x].info |= (CAVE_ICKY);
-
-               }
-               break;
-       }
-
-       msg_print_wizard(CHEAT_DUNGEON, _("ガラスの部屋が生成されました。", "Glass room was generated."));
-
-       return TRUE;
-}
-
-
-/* Create a new floor room with optional light */
-void generate_room_floor(int y1, int x1, int y2, int x2, int light)
-{
-       int y, x;
-       
-       cave_type *c_ptr;
-
-       for (y = y1; y <= y2; y++)
-       {
-               for (x = x1; x <= x2; x++)
-               {
-                       /* Point to grid */
-                       c_ptr = &cave[y][x];
-                       place_floor_grid(c_ptr);
-                       c_ptr->info |= (CAVE_ROOM);
-                       if (light) c_ptr->info |= (CAVE_GLOW);
-               }
-       }
-}
-
-void generate_fill_perm_bold(int y1, int x1, int y2, int x2)
-{
-       int y, x;
-
-       for (y = y1; y <= y2; y++)
-       {
-               for (x = x1; x <= x2; x++)
-               {
-                       /* Point to grid */
-                       place_inner_perm_bold(y, x);
+                       /* Build the room */
+                       build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
+                       break;
                }
-       }
-}
-
-/* Minimum & maximum town size */
-#define MIN_TOWN_WID ((MAX_WID / 3) / 2)
-#define MIN_TOWN_HGT ((MAX_HGT / 3) / 2)
-#define MAX_TOWN_WID ((MAX_WID / 3) * 2 / 3)
-#define MAX_TOWN_HGT ((MAX_HGT / 3) * 2 / 3)
-
-/* Struct for build underground buildings */
-typedef struct
-{
-       int y0, x0; /* North-west corner (relative) */
-       int y1, x1; /* South-east corner (relative) */
-}
-ugbldg_type;
-
-ugbldg_type *ugbldg;
-
-/*
- * Precalculate buildings' location of underground arcade
- */
-static bool precalc_ugarcade(int town_hgt, int town_wid, int n)
-{
-       int i, y, x, center_y, center_x, tmp, attempt = 10000;
-       int max_bldg_hgt = 3 * town_hgt / MAX_TOWN_HGT;
-       int max_bldg_wid = 5 * town_wid / MAX_TOWN_WID;
-       ugbldg_type *cur_ugbldg;
-       bool **ugarcade_used, abort;
-
-       /* Allocate "ugarcade_used" array (2-dimension) */
-       C_MAKE(ugarcade_used, town_hgt, bool *);
-       C_MAKE(*ugarcade_used, town_hgt * town_wid, bool);
-       for (y = 1; y < town_hgt; y++) ugarcade_used[y] = *ugarcade_used + y * town_wid;
-
-       /* Calculate building locations */
-       for (i = 0; i < n; i++)
-       {
-               cur_ugbldg = &ugbldg[i];
-               (void)WIPE(cur_ugbldg, ugbldg_type);
-
-               do
+               case 2:
                {
-                       /* Find the "center" of the store */
-                       center_y = rand_range(2, town_hgt - 3);
-                       center_x = rand_range(2, town_wid - 3);
-
-                       /* Determine the store boundaries */
-                       tmp = center_y - randint1(max_bldg_hgt);
-                       cur_ugbldg->y0 = MAX(tmp, 1);
-                       tmp = center_x - randint1(max_bldg_wid);
-                       cur_ugbldg->x0 = MAX(tmp, 1);
-                       tmp = center_y + randint1(max_bldg_hgt);
-                       cur_ugbldg->y1 = MIN(tmp, town_hgt - 2);
-                       tmp = center_x + randint1(max_bldg_wid);
-                       cur_ugbldg->x1 = MIN(tmp, town_wid - 2);
-
-                       /* Scan this building's area */
-                       for (abort = FALSE, y = cur_ugbldg->y0; (y <= cur_ugbldg->y1) && !abort; y++)
+                       /* Try and divide vertically */
+                       if (xsize < 3)
                        {
-                               for (x = cur_ugbldg->x0; x <= cur_ugbldg->x1; x++)
+                               /* Too small */
+                               for (y = y1; y < y2; y++)
                                {
-                                       if (ugarcade_used[y][x])
+                                       for (x = x1; x < x2; x++)
                                        {
-                                               abort = TRUE;
-                                               break;
+                                               place_inner_bold(y, x);
                                        }
                                }
+                               return;
                        }
 
-                       attempt--;
+                       t1 = randint1(xsize - 2) + x1 + 1;
+                       build_recursive_room(x1, y1, t1, y2, power - 2);
+                       build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
+                       break;
                }
-               while (abort && attempt); /* Accept this building if no overlapping */
-
-               /* Failed to generate underground arcade */
-               if (!attempt) break;
-
-               /*
-                * Mark to ugarcade_used[][] as "used"
-                * Note: Building-adjacent grids are included for preventing
-                * connected bulidings.
-                */
-               for (y = cur_ugbldg->y0 - 1; y <= cur_ugbldg->y1 + 1; y++)
+               case 3:
                {
-                       for (x = cur_ugbldg->x0 - 1; x <= cur_ugbldg->x1 + 1; x++)
+                       /* Try and divide horizontally */
+                       if (ysize < 3)
                        {
-                               ugarcade_used[y][x] = TRUE;
+                               /* Too small */
+                               for (y = y1; y < y2; y++)
+                               {
+                                       for (x = x1; x < x2; x++)
+                                       {
+                                               place_inner_bold(y, x);
+                                       }
+                               }
+                               return;
                        }
+
+                       t1 = randint1(ysize - 2) + y1 + 1;
+                       build_recursive_room(x1, y1, x2, t1, power - 2);
+                       build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
+                       break;
                }
        }
-
-       /* Free "ugarcade_used" array (2-dimension) */
-       C_KILL(*ugarcade_used, town_hgt * town_wid, bool);
-       C_KILL(ugarcade_used, town_hgt, bool *);
-
-       /* If i < n, generation is not allowed */
-       return i == n;
 }
 
-/*!
- * @brief タイプ16の部屋…地下都市生成のサブルーチン / Actually create buildings
- * @return なし
- * @param ltcy 生成基準Y座標
- * @param ltcx 生成基準X座標
- * @param stotes[] 生成する店舗のリスト
- * @param n 生成する店舗の数
- * @note
- * Note: ltcy and ltcx indicate "left top corner".
+
+/*
+ * Add outer wall to a floored region
+ * Note: no range checking is done so must be inside dungeon
+ * This routine also stomps on doors
  */
-static void build_stores(int ltcy, int ltcx, int stores[], int n)
+void add_outer_wall(POSITION x, POSITION y, int light, POSITION x1, POSITION y1, POSITION x2, POSITION y2)
 {
-       int i, y, x;
-       IDX j;
-       ugbldg_type *cur_ugbldg;
-
-       for (i = 0; i < n; i++)
-       {
-               cur_ugbldg = &ugbldg[i];
-
-               /* Generate new room */
-               generate_room_floor(
-                       ltcy + cur_ugbldg->y0 - 2, ltcx + cur_ugbldg->x0 - 2,
-                       ltcy + cur_ugbldg->y1 + 2, ltcx + cur_ugbldg->x1 + 2,
-                       FALSE);
-       }
-
-       for (i = 0; i < n; i++)
-       {
-               cur_ugbldg = &ugbldg[i];
+       cave_type *c_ptr;
+       feature_type *f_ptr;
+       int i, j;
 
-               /* Build an invulnerable rectangular building */
-               generate_fill_perm_bold(
-                       ltcy + cur_ugbldg->y0, ltcx + cur_ugbldg->x0,
-                       ltcy + cur_ugbldg->y1, ltcx + cur_ugbldg->x1);
+       if (!in_bounds(y, x)) return;
 
-               /* Pick a door direction (S,N,E,W) */
-               switch (randint0(4))
-               {
-               /* Bottom side */
-               case 0:
-                       y = cur_ugbldg->y1;
-                       x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
-                       break;
+       c_ptr = &cave[y][x];
 
-               /* Top side */
-               case 1:
-                       y = cur_ugbldg->y0;
-                       x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
-                       break;
+       /* hack- check to see if square has been visited before
+       * if so, then exit (use room flag to do this) */
+       if (c_ptr->info & CAVE_ROOM) return;
 
-               /* Right side */
-               case 2:
-                       y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
-                       x = cur_ugbldg->x1;
-                       break;
+       /* set room flag */
+       c_ptr->info |= CAVE_ROOM;
 
-               /* Left side */
-               default:
-                       y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
-                       x = cur_ugbldg->x0;
-                       break;
-               }
+       f_ptr = &f_info[c_ptr->feat];
 
-               for (j = 0; j < max_f_idx; j++)
+       if (is_floor_bold(y, x))
+       {
+               for (i = -1; i <= 1; i++)
                {
-                       if (have_flag(f_info[j].flags, FF_STORE))
+                       for (j = -1; j <= 1; j++)
                        {
-                               if (f_info[j].subtype == stores[i]) break;
+                               if ((x + i >= x1) && (x + i <= x2) &&
+                                        (y + j >= y1) && (y + j <= y2))
+                               {
+                                       add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
+                                       if (light) c_ptr->info |= CAVE_GLOW;
+                               }
                        }
                }
-
-               /* Clear previous contents, add a store door */
-               if (j < max_f_idx)
-               {
-                       cave_set_feat(ltcy + y, ltcx + x, j);
-
-                       /* Init store */
-                       store_init(NO_TOWN, stores[i]);
-               }
+       }
+       else if (is_extra_bold(y, x))
+       {
+               /* Set bounding walls */
+               place_outer_bold(y, x);
+               if (light) c_ptr->info |= CAVE_GLOW;
+       }
+       else if (permanent_wall(f_ptr))
+       {
+               /* Set bounding walls */
+               if (light) c_ptr->info |= CAVE_GLOW;
        }
 }
 
 
-/*!
- * @brief タイプ16の部屋…地下都市の生成 / Type 16 -- Underground Arcade
- * @return なし
- * @details
- * Town logic flow for generation of new town\n
- * Originally from Vanilla 3.0.3\n
- *\n
- * We start with a fully wiped cave of normal floors.\n
- *\n
- * Note that town_gen_hack() plays games with the R.N.G.\n
- *\n
- * This function does NOT do anything about the owners of the stores,\n
- * nor the contents thereof.  It only handles the physical layout.\n
+/*
+ * Hacked distance formula - gives the 'wrong' answer.
+ * Used to build crypts
  */
-static bool build_type16(void)
+POSITION dist2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, POSITION h1, POSITION h2, POSITION h3, POSITION h4)
 {
-       int stores[] =
-       {
-               STORE_GENERAL, STORE_ARMOURY, STORE_WEAPON, STORE_TEMPLE,
-               STORE_ALCHEMIST, STORE_MAGIC, STORE_BLACK, STORE_BOOK,
-       };
-       int n = sizeof stores / sizeof (int);
-       POSITION i, y, x, y1, x1, yval, xval;
-       int town_hgt = rand_range(MIN_TOWN_HGT, MAX_TOWN_HGT);
-       int town_wid = rand_range(MIN_TOWN_WID, MAX_TOWN_WID);
-       bool prevent_bm = FALSE;
-
-       /* Hack -- If already exist black market, prevent building */
-       for (y = 0; (y < cur_hgt) && !prevent_bm; y++)
-       {
-               for (x = 0; x < cur_wid; x++)
-               {
-                       if (cave[y][x].feat == FF_STORE)
-                       {
-                               prevent_bm = (f_info[cave[y][x].feat].subtype == STORE_BLACK);
-                               break;
-                       }
-               }
-       }
-       for (i = 0; i < n; i++)
-       {
-               if ((stores[i] == STORE_BLACK) && prevent_bm) stores[i] = stores[--n];
-       }
-       if (!n) return FALSE;
+       POSITION dx, dy;
+       dx = abs(x2 - x1);
+       dy = abs(y2 - y1);
 
-       /* Allocate buildings array */
-       C_MAKE(ugbldg, n, ugbldg_type);
+       /* Basically this works by taking the normal pythagorean formula
+        * and using an expansion to express this in a way without the
+        * square root.  This approximate formula is then perturbed to give
+        * the distorted results.  (I found this by making a mistake when I was
+        * trying to fix the circular rooms.)
+        */
 
-       /* If cannot build stores, abort */
-       if (!precalc_ugarcade(town_hgt, town_wid, n))
-       {
-               /* Free buildings array */
-               C_KILL(ugbldg, n, ugbldg_type);
-               return FALSE;
-       }
+       /* h1-h4 are constants that describe the metric */
+       if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
+       if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
+       return (((dx + dy) * 128) / 181 +
+               (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
+       /* 128/181 is approx. 1/sqrt(2) */
+}
 
-       /* Find and reserve some space in the dungeon.  Get center of room. */
-       if (!find_space(&yval, &xval, town_hgt + 4, town_wid + 4))
-       {
-               /* Free buildings array */
-               C_KILL(ugbldg, n, ugbldg_type);
-               return FALSE;
-       }
 
-       /* Get top left corner */
-       y1 = yval - (town_hgt / 2);
-       x1 = xval - (town_wid / 2);
 
-       /* Generate new room */
-       generate_room_floor(
-               y1 + town_hgt / 3, x1 + town_wid / 3,
-               y1 + town_hgt * 2 / 3, x1 + town_wid * 2 / 3, FALSE);
 
-       /* Build stores */
-       build_stores(y1, x1, stores, n);
+/* Create a new floor room with optional light */
+void generate_room_floor(POSITION y1, POSITION x1, POSITION y2, POSITION x2, int light)
+{
+       POSITION y, x;
+       
+       cave_type *c_ptr;
 
-       msg_print_wizard(CHEAT_DUNGEON, _("地下街を生成しました", "Underground arcade was generated."));
+       for (y = y1; y <= y2; y++)
+       {
+               for (x = x1; x <= x2; x++)
+               {
+                       /* Point to grid */
+                       c_ptr = &cave[y][x];
+                       place_floor_grid(c_ptr);
+                       c_ptr->info |= (CAVE_ROOM);
+                       if (light) c_ptr->info |= (CAVE_GLOW);
+               }
+       }
+}
 
-       /* Free buildings array */
-       C_KILL(ugbldg, n, ugbldg_type);
+void generate_fill_perm_bold(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
+{
+       POSITION y, x;
 
-       return TRUE;
+       for (y = y1; y <= y2; y++)
+       {
+               for (x = x1; x <= x2; x++)
+               {
+                       /* Point to grid */
+                       place_inner_perm_bold(y, x);
+               }
+       }
 }
 
 
@@ -4160,7 +2078,7 @@ static bool build_type16(void)
  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
  * @return 部屋の精製に成功した場合 TRUE を返す。
  */
-static bool room_build(int typ)
+static bool room_build(EFFECT_ID typ)
 {
        /* Build a room */
        switch (typ)
@@ -4182,6 +2100,7 @@ static bool room_build(int typ)
        case ROOM_T_TRAP:          return build_type14();
        case ROOM_T_GLASS:         return build_type15();
        case ROOM_T_ARCADE:        return build_type16();
+       case ROOM_T_FIXED:         return build_type17();
        }
 
        /* Paranoia */
@@ -4239,7 +2158,9 @@ bool generate_rooms(void)
         * XXX -- Various dungeon types and options.
         */
 
-       /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、かつ「常に通常でない部屋を生成する」フラグがONならば、GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
+       /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、
+        * かつ「常に通常でない部屋を生成する」フラグがONならば、
+        * GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
        if (ironman_rooms && !((d_info[dungeon_type].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
        {
                for (i = 0; i < ROOM_T_MAX; i++)
@@ -4257,6 +2178,12 @@ bool generate_rooms(void)
                prob_list[ROOM_T_RANDOM_VAULT] = 0;
        }
 
+       /*! @details ダンジョンにBEGINNERフラグがあるならば、FIXED_ROOMを除外 / Forbidden vaults */
+       if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
+       {
+               prob_list[ROOM_T_FIXED] = 0;
+       }
+
        /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
        if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
        {