OSDN Git Service

[Refactor] #37353 コメント整理。 / Refactor comments, (Duplicated meaning "Prepare allocatio...
[hengband/hengband.git] / src / wild.c
index 9369564..cc83444 100644 (file)
@@ -1,19 +1,24 @@
-/* File: wild.c */
-
-/*
- * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke,
- * Robert Ruehlmann
- *
- * This software may be copied and distributed for educational, research,
- * and not for profit purposes provided that this copyright and statement
- * are included in all such copies.  Other copyrights may also apply.
+/*!
+ * @file wild.c
+ * @brief 荒野マップの生成とルール管理 / Wilderness generation
+ * @date 2014/02/13
+ * @author
+ * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke\n
+ * This software may be copied and distributed for educational, research, and\n
+ * not for profit purposes provided that this copyright and statement are\n
+ * included in all such copies.\n
+ * 2013 Deskull rearranged comment for Doxygen.
  */
 
-/* Purpose: Wilderness generation */
-
 #include "angband.h"
+#include "world.h"
 
-
+/*!
+ * @brief 地形生成確率を決める要素100の配列を確率テーブルから作成する
+ * @param feat_type 非一様確率を再現するための要素数100の配列
+ * @param prob 元の確率テーブル
+ * @return なし
+ */
 static void set_floor_and_wall_aux(s16b feat_type[100], feat_prob prob[DUNGEON_FEAT_PROB_NUM])
 {
        int lim[DUNGEON_FEAT_PROB_NUM], cur = 0, i;
@@ -31,12 +36,15 @@ static void set_floor_and_wall_aux(s16b feat_type[100], feat_prob prob[DUNGEON_F
        }
 }
 
-/*
- * Fill the arrays of floors and walls in the good proportions
+/*!
+ * @brief ダンジョンの地形を指定確率に応じて各マスへランダムに敷き詰める
+ * / Fill the arrays of floors and walls in the good proportions
+ * @param type ダンジョンID
+ * @return なし
  */
-void set_floor_and_wall(byte type)
+void set_floor_and_wall(DUNGEON_IDX type)
 {
-       static byte cur_type = 255;
+       DUNGEON_IDX cur_type = 255;
        dungeon_info_type *d_ptr;
 
        /* Already filled */
@@ -54,11 +62,20 @@ void set_floor_and_wall(byte type)
 }
 
 
-/*
- * Helper for plasma generation.
+/*!
+ * @brief プラズマフラクタル的地形生成の再帰中間処理
+ * / Helper for plasma generation.
+ * @param x1 左上端の深み
+ * @param x2 右上端の深み
+ * @param x3 左下端の深み
+ * @param x4 右下端の深み
+ * @param xmid 中央座標X
+ * @param ymid 中央座標Y
+ * @param rough ランダム幅
+ * @param depth_max 深みの最大値
+ * @return なし
  */
-static void perturb_point_mid(int x1, int x2, int x3, int x4,
-                         int xmid, int ymid, int rough, int depth_max)
+static void perturb_point_mid(POSITION x1, POSITION x2, POSITION x3, POSITION x4, POSITION xmid, POSITION ymid, POSITION rough, POSITION depth_max)
 {
        /*
         * Average the four corners & perturb it a bit.
@@ -78,10 +95,22 @@ static void perturb_point_mid(int x1, int x2, int x3, int x4,
        if (avg > depth_max) avg = depth_max;
 
        /* Set the new value. */
-       cave[ymid][xmid].feat = avg;
+       cave[ymid][xmid].feat = (s16b)avg;
 }
 
 
+/*!
+ * @brief プラズマフラクタル的地形生成の再帰末端処理
+ * / Helper for plasma generation.
+ * @param x1 中間末端部1の重み
+ * @param x2 中間末端部2の重み
+ * @param x3 中間末端部3の重み
+ * @param xmid 最終末端部座標X
+ * @param ymid 最終末端部座標Y
+ * @param rough ランダム幅
+ * @param depth_max 深みの最大値
+ * @return なし
+ */
 static void perturb_point_end(int x1, int x2, int x3,
                          int xmid, int ymid, int rough, int depth_max)
 {
@@ -102,23 +131,34 @@ static void perturb_point_end(int x1, int x2, int x3,
        if (avg > depth_max) avg = depth_max;
 
        /* Set the new value. */
-       cave[ymid][xmid].feat = avg;
+       cave[ymid][xmid].feat = (s16b)avg;
 }
 
 
-/*
+/*!
+ * @brief プラズマフラクタル的地形生成の開始処理
+ * / Helper for plasma generation.
+ * @param x1 処理範囲の左上X座標
+ * @param y1 処理範囲の左上Y座標
+ * @param x2 処理範囲の右下X座標
+ * @param y2 処理範囲の右下Y座標
+ * @param depth_max 深みの最大値
+ * @param rough ランダム幅
+ * @return なし
+ * @details
+ * <pre>
  * A generic function to generate the plasma fractal.
  * Note that it uses ``cave_feat'' as temporary storage.
  * The values in ``cave_feat'' after this function
  * are NOT actual features; They are raw heights which
  * need to be converted to features.
+ * </pre>
  */
-static void plasma_recursive(int x1, int y1, int x2, int y2,
-                            int depth_max, int rough)
+static void plasma_recursive(POSITION x1, POSITION y1, POSITION x2, POSITION y2, POSITION depth_max, POSITION rough)
 {
        /* Find middle */
-       int xmid = (x2 - x1) / 2 + x1;
-       int ymid = (y2 - y1) / 2 + y1;
+       POSITION xmid = (x2 - x1) / 2 + x1;
+       POSITION ymid = (y2 - y1) / 2 + y1;
 
        /* Are we done? */
        if (x1 + 1 == x2) return;
@@ -147,332 +187,27 @@ static void plasma_recursive(int x1, int y1, int x2, int y2,
 }
 
 
+#define MAX_FEAT_IN_TERRAIN 18
+
 /*
  * The default table in terrain level generation.
  */
-static int terrain_table[MAX_WILDERNESS][18] =
-{
-       /* TERRAIN_EDGE */
-       {
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-
-                       FEAT_PERM,
-                       FEAT_PERM,
-                       FEAT_PERM,
-       },
-       /* TERRAIN_TOWN */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-       },
-       /* TERRAIN_DEEP_WATER */
-       {
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-       },
-       /* TERRAIN_SHALLOW_WATER */
-       {
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-                       FEAT_DEEP_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_FLOOR,
-                       FEAT_DIRT,
-                       FEAT_GRASS,
-       },
-       /* TERRAIN_SWAMP */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-                       FEAT_SHAL_WATER,
-
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DEEP_GRASS,
-                       FEAT_TREES,
-       },
-       /* TERRAIN_DIRT */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_FLOWER,
-                       FEAT_DEEP_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_TREES,
-                       FEAT_TREES,
-       },
-       /* TERRAIN_GRASS */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_FLOWER,
-                       FEAT_DEEP_GRASS,
-
-                       FEAT_DEEP_GRASS,
-                       FEAT_TREES,
-                       FEAT_TREES,
-       },
-       /* TERRAIN_TREES */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_DIRT,
-
-                       FEAT_TREES,
-                       FEAT_TREES,
-                       FEAT_TREES,
-
-                       FEAT_TREES,
-                       FEAT_TREES,
-                       FEAT_TREES,
-
-                       FEAT_TREES,
-                       FEAT_TREES,
-                       FEAT_TREES,
-
-                       FEAT_TREES,
-                       FEAT_TREES,
-                       FEAT_DEEP_GRASS,
-
-                       FEAT_DEEP_GRASS,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-       },
-       /* TERRAIN_DESERT */
-       {
-                       FEAT_FLOOR,
-                       FEAT_FLOOR,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-                       FEAT_GRASS,
-       },
-       /* TERRAIN_SHALLOW_LAVA */
-       {
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_DEEP_LAVA,
-
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-                       FEAT_MOUNTAIN,
-       },
-       /* TERRAIN_DEEP_LAVA */
-       {
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-                       FEAT_SHAL_LAVA,
-
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-                       FEAT_DEEP_LAVA,
-
-                       FEAT_DEEP_LAVA,
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-       },
-       /* TERRAIN_MOUNTAIN */
-       {
-                       FEAT_FLOOR,
-                       FEAT_DEEP_GRASS,
-                       FEAT_GRASS,
-
-                       FEAT_GRASS,
-                       FEAT_DIRT,
-                       FEAT_DIRT,
-
-                       FEAT_TREES,
-                       FEAT_TREES,
-                       FEAT_MOUNTAIN,
-
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-                       FEAT_MOUNTAIN,
-       },
-
-};
-
-
+static s16b terrain_table[MAX_WILDERNESS][MAX_FEAT_IN_TERRAIN];
+
+/*!
+ * @brief 荒野フロア生成のサブルーチン
+ * @param terrain 荒野地形ID
+ * @param seed 乱数の固定シード
+ * @param border 未使用
+ * @param corner 広域マップの角部分としての生成ならばTRUE
+ * @return なし
+ */
 static void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
 {
-       int x1, y1;
-       int table_size = sizeof(terrain_table[0]) / sizeof(int);
+       POSITION x1, y1;
+       int table_size = sizeof(terrain_table[0]) / sizeof(s16b);
        int roughness = 1; /* The roughness of the level. */
+       u32b state_backup[4];
 
        /* Unused */
        (void)border;
@@ -485,7 +220,7 @@ static void generate_wilderness_area(int terrain, u32b seed, bool border, bool c
                {
                        for (x1 = 0; x1 < MAX_WID; x1++)
                        {
-                               cave[y1][x1].feat = FEAT_PERM;
+                               cave[y1][x1].feat = feat_permanent;
                        }
                }
 
@@ -494,18 +229,21 @@ static void generate_wilderness_area(int terrain, u32b seed, bool border, bool c
        }
 
 
-       /* Hack -- Use the "simple" RNG */
-       Rand_quick = TRUE;
+       /* Hack -- Backup the RNG state */
+       Rand_state_backup(state_backup);
 
-       /* Hack -- Induce consistant town layout */
-       Rand_value = seed;
+       /* Hack -- Induce consistant flavors */
+       Rand_state_set(seed);
 
-       /* Create level background */
-       for (y1 = 0; y1 < MAX_HGT; y1++)
+       if (!corner)
        {
-               for (x1 = 0; x1 < MAX_WID; x1++)
+               /* Create level background */
+               for (y1 = 0; y1 < MAX_HGT; y1++)
                {
-                       cave[y1][x1].feat = table_size / 2;
+                       for (x1 = 0; x1 < MAX_WID; x1++)
+                       {
+                               cave[y1][x1].feat = table_size / 2;
+                       }
                }
        }
 
@@ -514,47 +252,74 @@ static void generate_wilderness_area(int terrain, u32b seed, bool border, bool c
         * ToDo: calculate the medium height of the adjacent
         * terrains for every corner.
         */
-       cave[1][1].feat = randint0(table_size);
-       cave[MAX_HGT-2][1].feat = randint0(table_size);
-       cave[1][MAX_WID-2].feat = randint0(table_size);
-       cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
+       cave[1][1].feat = (s16b)randint0(table_size);
+       cave[MAX_HGT-2][1].feat = (s16b)randint0(table_size);
+       cave[1][MAX_WID-2].feat = (s16b)randint0(table_size);
+       cave[MAX_HGT-2][MAX_WID-2].feat = (s16b)randint0(table_size);
 
        if (!corner)
        {
+               /* Hack -- preserve four corners */
+               s16b north_west = cave[1][1].feat;
+               s16b south_west = cave[MAX_HGT - 2][1].feat;
+               s16b north_east = cave[1][MAX_WID - 2].feat;
+               s16b south_east = cave[MAX_HGT - 2][MAX_WID - 2].feat;
+
                /* x1, y1, x2, y2, num_depths, roughness */
                plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
-       }
 
-       /* Use the complex RNG */
-       Rand_quick = FALSE;
+               /* Hack -- copyback four corners */
+               cave[1][1].feat = north_west;
+               cave[MAX_HGT - 2][1].feat = south_west;
+               cave[1][MAX_WID - 2].feat = north_east;
+               cave[MAX_HGT - 2][MAX_WID - 2].feat = south_east;
 
-       for (y1 = 1; y1 < MAX_HGT-1; y1++)
-       {
-               for (x1 = 1; x1 < MAX_WID-1; x1++)
+               for (y1 = 1; y1 < MAX_HGT - 1; y1++)
                {
-                       cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
+                       for (x1 = 1; x1 < MAX_WID - 1; x1++)
+                       {
+                               cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
+                       }
                }
        }
+       else /* Hack -- only four corners */
+       {
+               cave[1][1].feat = terrain_table[terrain][cave[1][1].feat];
+               cave[MAX_HGT - 2][1].feat = terrain_table[terrain][cave[MAX_HGT - 2][1].feat];
+               cave[1][MAX_WID - 2].feat = terrain_table[terrain][cave[1][MAX_WID - 2].feat];
+               cave[MAX_HGT - 2][MAX_WID - 2].feat = terrain_table[terrain][cave[MAX_HGT - 2][MAX_WID - 2].feat];
+       }
+
+       /* Hack -- Restore the RNG state */
+       Rand_state_restore(state_backup);
 }
 
 
 
-/*
+/*!
+ * @brief 荒野フロア生成のメインルーチン /
  * Load a town or generate a terrain level using "plasma" fractals.
- *
+ * @param y 広域マップY座標
+ * @param x 広域マップY座標
+ * @param border 広域マップの辺部分としての生成ならばTRUE
+ * @param corner 広域マップの角部分としての生成ならばTRUE
+ * @return なし
+ * @details
+ * <pre>
  * x and y are the coordinates of the area in the wilderness.
  * Border and corner are optimization flags to speed up the
  * generation of the fractal terrain.
  * If border is set then only the border of the terrain should
  * be generated (for initializing the border structure).
  * If corner is set then only the corners of the area are needed.
+ * </pre>
  */
-static void generate_area(int y, int x, bool border, bool corner)
+static void generate_area(POSITION y, POSITION x, bool border, bool corner)
 {
-       int x1, y1;
+       POSITION x1, y1;
 
        /* Number of the town (if any) */
-       p_ptr->town_num = wilderness[y][x].town;
+       p_ptr->town_num = (s16b)wilderness[y][x].town;
 
        /* Set the base level */
        base_level = wilderness[y][x].level;
@@ -601,7 +366,7 @@ static void generate_area(int y, int x, bool border, bool corner)
                 */
                if (wilderness[y][x].road)
                {
-                       cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
+                       cave[MAX_HGT/2][MAX_WID/2].feat = feat_floor;
 
                        if (wilderness[y-1][x].road)
                        {
@@ -609,7 +374,7 @@ static void generate_area(int y, int x, bool border, bool corner)
                                for (y1 = 1; y1 < MAX_HGT/2; y1++)
                                {
                                        x1 = MAX_WID/2;
-                                       cave[y1][x1].feat = FEAT_FLOOR;
+                                       cave[y1][x1].feat = feat_floor;
                                }
                        }
 
@@ -619,7 +384,7 @@ static void generate_area(int y, int x, bool border, bool corner)
                                for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
                                {
                                        x1 = MAX_WID/2;
-                                       cave[y1][x1].feat = FEAT_FLOOR;
+                                       cave[y1][x1].feat = feat_floor;
                                }
                        }
 
@@ -629,7 +394,7 @@ static void generate_area(int y, int x, bool border, bool corner)
                                for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
                                {
                                        y1 = MAX_HGT/2;
-                                       cave[y1][x1].feat = FEAT_FLOOR;
+                                       cave[y1][x1].feat = feat_floor;
                                }
                        }
 
@@ -639,7 +404,7 @@ static void generate_area(int y, int x, bool border, bool corner)
                                for (x1 = 1; x1 < MAX_WID/2; x1++)
                                {
                                        y1 = MAX_HGT/2;
-                                       cave[y1][x1].feat = FEAT_FLOOR;
+                                       cave[y1][x1].feat = feat_floor;
                                }
                        }
                }
@@ -648,21 +413,22 @@ static void generate_area(int y, int x, bool border, bool corner)
        if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
        {
                int dy, dx;
+               u32b state_backup[4];
 
-               /* Hack -- Use the "simple" RNG */
-               Rand_quick = TRUE;
+               /* Hack -- Backup the RNG state */
+               Rand_state_backup(state_backup);
 
-               /* Hack -- Induce consistant town layout */
-               Rand_value = wilderness[y][x].seed;
+               /* Hack -- Induce consistant flavors */
+               Rand_state_set(wilderness[y][x].seed);
 
                dy = rand_range(6, cur_hgt - 6);
                dx = rand_range(6, cur_wid - 6);
 
-               cave[dy][dx].feat = FEAT_ENTRANCE;
+               cave[dy][dx].feat = feat_entrance;
                cave[dy][dx].special = wilderness[y][x].entrance;
 
-               /* Use the complex RNG */
-               Rand_quick = FALSE;
+               /* Hack -- Restore the RNG state */
+               Rand_state_restore(state_backup);
        }
 }
 
@@ -673,8 +439,10 @@ static void generate_area(int y, int x, bool border, bool corner)
 static border_type border;
 
 
-/*
+/*!
+ * @brief 広域マップの生成 /
  * Build the wilderness area outside of the town.
+ * @return なし
  */
 void wilderness_gen(void)
 {
@@ -696,8 +464,6 @@ void wilderness_gen(void)
 
        x = p_ptr->wilderness_x;
        y = p_ptr->wilderness_y;
-
-       /* Prepare allocation table */
        get_mon_num_prep(get_monster_hook(), NULL);
 
        /* North border */
@@ -756,28 +522,28 @@ void wilderness_gen(void)
        /* Special boundary walls -- North */
        for (i = 0; i < MAX_WID; i++)
        {
-               cave[0][i].feat = FEAT_PERM;
+               cave[0][i].feat = feat_permanent;
                cave[0][i].mimic = border.north[i];
        }
 
        /* Special boundary walls -- South */
        for (i = 0; i < MAX_WID; i++)
        {
-               cave[MAX_HGT - 1][i].feat = FEAT_PERM;
+               cave[MAX_HGT - 1][i].feat = feat_permanent;
                cave[MAX_HGT - 1][i].mimic = border.south[i];
        }
 
        /* Special boundary walls -- West */
        for (i = 0; i < MAX_HGT; i++)
        {
-               cave[i][0].feat = FEAT_PERM;
+               cave[i][0].feat = feat_permanent;
                cave[i][0].mimic = border.west[i];
        }
 
        /* Special boundary walls -- East */
        for (i = 0; i < MAX_HGT; i++)
        {
-               cave[i][MAX_WID - 1].feat = FEAT_PERM;
+               cave[i][MAX_WID - 1].feat = feat_permanent;
                cave[i][MAX_WID - 1].mimic = border.east[i];
        }
 
@@ -853,11 +619,11 @@ void wilderness_gen(void)
 
                                if (have_flag(f_ptr->flags, FF_BLDG))
                                {
-                                       if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
+                                       if ((f_ptr->subtype == 4) || ((p_ptr->town_num == 1) && (f_ptr->subtype == 0)))
                                        {
                                                if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
-                                               p_ptr->oldpy = y;
-                                               p_ptr->oldpx = x;
+                                               p_ptr->oldpy = (s16b)y;
+                                               p_ptr->oldpx = (s16b)x;
                                        }
                                }
                        }
@@ -877,8 +643,8 @@ void wilderness_gen(void)
                                if (cave_have_flag_grid(c_ptr, FF_ENTRANCE))
                                {
                                        if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
-                                       p_ptr->oldpy = y;
-                                       p_ptr->oldpx = x;
+                                       p_ptr->oldpy = (s16b)y;
+                                       p_ptr->oldpx = (s16b)x;
                                }
                        }
                }
@@ -888,12 +654,12 @@ void wilderness_gen(void)
        player_place(p_ptr->oldpy, p_ptr->oldpx);
        /* p_ptr->leaving_dungeon = FALSE;*/
 
-       lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
+       lim = (generate_encounter == TRUE) ? 40 : MIN_M_ALLOC_TN;
 
        /* Make some residents */
        for (i = 0; i < lim; i++)
        {
-               u32b mode = 0;
+               BIT_FLAGS mode = 0;
 
                if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
                        mode |= PM_ALLOW_SLEEP;
@@ -909,7 +675,7 @@ void wilderness_gen(void)
        set_floor_and_wall(0);
 
        /* Set rewarded quests to finished */
-       for (i = 0; i < max_quests; i++)
+       for (i = 0; i < max_q_idx; i++)
        {
                if (quest[i].status == QUEST_STATUS_REWARDED)
                        quest[i].status = QUEST_STATUS_FINISHED;
@@ -917,11 +683,14 @@ void wilderness_gen(void)
 }
 
 
-/*
- * Build the wilderness area.
- * -DG-
+static s16b conv_terrain2feat[MAX_WILDERNESS];
+
+/*!
+ * @brief 広域マップの生成(簡易処理版) /
+ * Build the wilderness area. -DG-
+ * @return なし
  */
-void wilderness_gen_small()
+void wilderness_gen_small(void)
 {
        int i, j;
 
@@ -929,7 +698,7 @@ void wilderness_gen_small()
        for (i = 0; i < MAX_WID; i++)
        for (j = 0; j < MAX_HGT; j++)
        {
-               cave[j][i].feat = FEAT_PERM;
+               cave[j][i].feat = feat_permanent;
        }
 
        /* Init the wilderness */
@@ -941,13 +710,13 @@ void wilderness_gen_small()
        {
                if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
                {
-                       cave[j][i].feat = FEAT_TOWN;
-                       cave[j][i].special = wilderness[j][i].town;
+                       cave[j][i].feat = (s16b)feat_town;
+                       cave[j][i].special = (s16b)wilderness[j][i].town;
                }
-               else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
+               else if (wilderness[j][i].road) cave[j][i].feat = feat_floor;
                else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
                {
-                       cave[j][i].feat = FEAT_ENTRANCE;
+                       cave[j][i].feat = feat_entrance;
                        cave[j][i].special = (byte)wilderness[j][i].entrance;
                }
                else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
@@ -966,8 +735,8 @@ void wilderness_gen_small()
        panel_col_min = cur_wid;
 
        /* Place the player */
-       px = p_ptr->wilderness_x;
-       py = p_ptr->wilderness_y;
+       p_ptr->x = p_ptr->wilderness_x;
+       p_ptr->y = p_ptr->wilderness_y;
 
        p_ptr->town_num = 0;
 }
@@ -988,8 +757,17 @@ struct wilderness_grid
 static wilderness_grid w_letter[255];
 
 
-/*
+/*!
+ * @brief w_info.txtのデータ解析 /
  * Parse a sub-file of the "extra info"
+ * @param buf 読み取ったデータ行のバッファ
+ * @param ymin 未使用
+ * @param xmin 広域地形マップを読み込みたいx座標の開始位置
+ * @param ymax 未使用
+ * @param xmax 広域地形マップを読み込みたいx座標の終了位置
+ * @param y 広域マップの高さを返す参照ポインタ
+ * @param x 広域マップの幅を返す参照ポインタ
+ * @return なし
  */
 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
 {
@@ -1028,7 +806,7 @@ errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, in
                                w_letter[index].terrain = 0;
                        
                        if (num > 2)
-                               w_letter[index].level = atoi(zz[2]);
+                               w_letter[index].level = (s16b)atoi(zz[2]);
                        else
                                w_letter[index].level = 0;
                        
@@ -1038,7 +816,7 @@ errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, in
                                w_letter[index].town = 0;
                        
                        if (num > 4)
-                               w_letter[index].road = atoi(zz[4]);
+                               w_letter[index].road = (byte_hack)atoi(zz[4]);
                        else
                                w_letter[index].road = 0;
                        
@@ -1068,17 +846,12 @@ errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, in
                
                for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
                {
-                       int idx = s[0];
-                       
-                       wilderness[*y][*x].terrain = w_letter[idx].terrain;
-                       
-                       wilderness[*y][*x].level = w_letter[idx].level;
-                       
-                       wilderness[*y][*x].town = w_letter[idx].town;
-                       
-                       wilderness[*y][*x].road = w_letter[idx].road;
-                       
-                       strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
+                       int id = s[0];
+                       wilderness[*y][*x].terrain = w_letter[id].terrain;
+                       wilderness[*y][*x].level = w_letter[id].level;
+                       wilderness[*y][*x].town = w_letter[id].town;
+                       wilderness[*y][*x].road = w_letter[id].road;
+                       strcpy(town[w_letter[id].town].name, w_letter[id].name);
                }
                
                (*y)++;
@@ -1122,7 +895,7 @@ errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, in
        for (i = 1; i < max_d_idx; i++)
        {
                if (!d_info[i].maxdepth) continue;
-               wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
+               wilderness[d_info[i].dy][d_info[i].dx].entrance = (byte_hack)i;
                if (!wilderness[d_info[i].dy][d_info[i].dx].town)
                        wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
        }
@@ -1132,12 +905,15 @@ errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, in
 }
 
 
-/*
+
+/*!
+ * @brief ゲーム開始時に各荒野フロアの乱数シードを指定する /
  * Generate the random seeds for the wilderness
+ * @return なし
  */
 void seed_wilderness(void)
 {
-       int x, y;
+       POSITION x, y;
 
        /* Init wilderness seeds */
        for (x = 0; x < max_wild_x; x++)
@@ -1156,8 +932,11 @@ void seed_wilderness(void)
  */
 typedef wilderness_type *wilderness_type_ptr;
 
-/*
+
+/*!
+ * @brief ゲーム開始時の荒野初期化メインルーチン /
  * Initialize wilderness array
+ * @return エラーコード
  */
 errr init_wilderness(void)
 {
@@ -1176,7 +955,148 @@ errr init_wilderness(void)
        return 0;
 }
 
+/*!
+ * @brief 荒野の地勢設定を初期化する /
+ * Initialize wilderness array
+ * @param terrain 初期化したい地勢ID
+ * @param feat_global 基本的な地形ID
+ * @param fmt 地勢内の地形数を参照するための独自フォーマット
+ * @return なし
+ */
+static void init_terrain_table(int terrain, s16b feat_global, concptr fmt, ...)
+{
+       va_list vp;
+       concptr    p;
+       int     cur = 0;
+       char    check = 'a';
+       FEAT_IDX feat;
+       int     num;
+
+       /* Begin the varargs stuff */
+       va_start(vp, fmt);
 
+       /* Wilderness terrains on global map */
+       conv_terrain2feat[terrain] = feat_global;
+
+       /* Wilderness terrains on local map */
+       for (p = fmt; *p; p++)
+       {
+               if (*p == check)
+               {
+                       int lim;
+
+                       feat = (s16b)va_arg(vp, int);
+                       num = va_arg(vp, int);
+                       lim = cur + num;
+
+                       for (; (cur < lim) && (cur < MAX_FEAT_IN_TERRAIN); cur++)
+                               terrain_table[terrain][cur] = feat;
+                       if (cur >= MAX_FEAT_IN_TERRAIN) break;
+
+                       check++;
+               }
+               else /* Paranoia */
+               {
+                       plog_fmt("Format error");
+               }
+       }
+
+       /* Paranoia */
+       if (cur < MAX_FEAT_IN_TERRAIN)
+       {
+               plog_fmt("Too few parameters");
+       }
+
+       /* End the varargs stuff */
+       va_end(vp);
+}
+
+
+/*!
+ * @brief 荒野の地勢設定全体を初期化するメインルーチン /
+ * Initialize arrays for wilderness terrains
+ * @return なし
+ */
+void init_wilderness_terrains(void)
+{
+       init_terrain_table(TERRAIN_EDGE, feat_permanent, "a",
+               feat_permanent, MAX_FEAT_IN_TERRAIN);
+
+       init_terrain_table(TERRAIN_TOWN, feat_town, "a",
+               feat_floor, MAX_FEAT_IN_TERRAIN);
+
+       init_terrain_table(TERRAIN_DEEP_WATER, feat_deep_water, "ab",
+               feat_deep_water, 12,
+               feat_shallow_water, MAX_FEAT_IN_TERRAIN - 12);
+
+       init_terrain_table(TERRAIN_SHALLOW_WATER, feat_shallow_water, "abcde",
+               feat_deep_water, 3,
+               feat_shallow_water, 12,
+               feat_floor, 1,
+               feat_dirt, 1,
+               feat_grass, MAX_FEAT_IN_TERRAIN - 17);
+
+       init_terrain_table(TERRAIN_SWAMP, feat_swamp, "abcdef",
+               feat_dirt, 2,
+               feat_grass, 3,
+               feat_tree, 1,
+               feat_brake, 1,
+               feat_shallow_water, 4,
+               feat_swamp, MAX_FEAT_IN_TERRAIN - 11);
+
+       init_terrain_table(TERRAIN_DIRT, feat_dirt, "abcdef",
+               feat_floor, 3,
+               feat_dirt, 10,
+               feat_flower, 1,
+               feat_brake, 1,
+               feat_grass, 1,
+               feat_tree, MAX_FEAT_IN_TERRAIN - 16);
+
+       init_terrain_table(TERRAIN_GRASS, feat_grass, "abcdef",
+               feat_floor, 2,
+               feat_dirt, 2,
+               feat_grass, 9,
+               feat_flower, 1,
+               feat_brake, 2,
+               feat_tree, MAX_FEAT_IN_TERRAIN - 16);
+
+       init_terrain_table(TERRAIN_TREES, feat_tree, "abcde",
+               feat_floor, 2,
+               feat_dirt, 1,
+               feat_tree, 11,
+               feat_brake, 2,
+               feat_grass, MAX_FEAT_IN_TERRAIN - 16);
+
+       init_terrain_table(TERRAIN_DESERT, feat_dirt, "abc",
+               feat_floor, 2,
+               feat_dirt, 13,
+               feat_grass, MAX_FEAT_IN_TERRAIN - 15);
+
+       init_terrain_table(TERRAIN_SHALLOW_LAVA, feat_shallow_lava, "abc",
+               feat_shallow_lava, 14,
+               feat_deep_lava, 3,
+               feat_mountain, MAX_FEAT_IN_TERRAIN - 17);
+
+       init_terrain_table(TERRAIN_DEEP_LAVA, feat_deep_lava, "abcd",
+               feat_dirt, 3,
+               feat_shallow_lava, 3,
+               feat_deep_lava, 10,
+               feat_mountain, MAX_FEAT_IN_TERRAIN - 16);
+
+       init_terrain_table(TERRAIN_MOUNTAIN, feat_mountain, "abcdef",
+               feat_floor, 1,
+               feat_brake, 1,
+               feat_grass, 2,
+               feat_dirt, 2,
+               feat_tree, 2,
+               feat_mountain, MAX_FEAT_IN_TERRAIN - 8);
+}
+
+/*!
+ * @brief 荒野から広域マップへの切り替え処理 /
+ * Initialize arrays for wilderness terrains
+ * @return 切り替えが行われた場合はTRUEを返す。
+ */
 bool change_wild_mode(void)
 {
        int i;
@@ -1189,7 +1109,7 @@ bool change_wild_mode(void)
        if (lite_town || vanilla_town)
        {
 #ifdef JP
-               msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
+               msg_print("荒野なんてない。");
 #else
                msg_print("No global map.");
 #endif
@@ -1199,8 +1119,8 @@ bool change_wild_mode(void)
        if (p_ptr->wild_mode)
        {
                /* Save the location in the global map */
-               p_ptr->wilderness_x = px;
-               p_ptr->wilderness_y = py;
+               p_ptr->wilderness_x = p_ptr->x;
+               p_ptr->wilderness_y = p_ptr->y;
 
                /* Give first move to the player */
                p_ptr->energy_need = 0;
@@ -1225,35 +1145,38 @@ bool change_wild_mode(void)
                if (m_ptr->cdis > MAX_SIGHT) continue;
                if (!is_hostile(m_ptr)) continue;
 #ifdef JP
-               msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
+               msg_print("敵がすぐ近くにいるときは広域マップに入れない!");
 #else
                msg_print("You cannot enter global map, since there is some monsters nearby!");
 #endif
-               energy_use = 0;
+               p_ptr->energy_use = 0;
                return FALSE;
        }
 
        if (have_pet)
        {
 #ifdef JP
-               cptr msg = "¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹­°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©";
+               concptr msg = "ペットを置いて広域マップに入りますか?";
 #else
-               cptr msg = "Do you leave your pets behind? ";
+               concptr msg = "Do you leave your pets behind? ";
 #endif
 
                if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
                {
-                       energy_use = 0;
+                       p_ptr->energy_use = 0;
                        return FALSE;
                }
        }
 
        /* HACK */
-       energy_use = 1000;
+       p_ptr->energy_use = 1000;
 
        /* Remember the position */
-       p_ptr->oldpx = px;
-       p_ptr->oldpy = py;
+       p_ptr->oldpx = p_ptr->x;
+       p_ptr->oldpy = p_ptr->y;
+
+       /* Cancel hex spelling */
+       if (hex_spelling_any()) stop_hex_spell_all();
 
        /* Cancel any special action */
        set_action(ACTION_NONE);