OSDN Git Service

[Refactor] monster_idxと0との比較を関数化する
[hengbandforosx/hengbandosx.git] / src / floor / wild.cpp
1 /*!
2  * @brief 荒野マップの生成とルール管理 / Wilderness generation
3  * @date 2014/02/13
4  * @author
5  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
6  * This software may be copied and distributed for educational, research, and
7  * not for profit purposes provided that this copyright and statement are
8  * included in all such copies.
9  * 2013 Deskull rearranged comment for Doxygen.
10  */
11
12 #include "floor/wild.h"
13 #include "core/asking-player.h"
14 #include "dungeon/dungeon-flag-types.h"
15 #include "dungeon/quest.h"
16 #include "floor/cave.h"
17 #include "floor/floor-town.h"
18 #include "game-option/birth-options.h"
19 #include "game-option/map-screen-options.h"
20 #include "grid/feature.h"
21 #include "grid/grid.h"
22 #include "info-reader/fixed-map-parser.h"
23 #include "info-reader/parse-error-types.h"
24 #include "io/files-util.h"
25 #include "io/tokenizer.h"
26 #include "market/building-initializer.h"
27 #include "monster-floor/monster-generator.h"
28 #include "monster-floor/monster-remover.h"
29 #include "monster-floor/monster-summon.h"
30 #include "monster-floor/place-monster-types.h"
31 #include "monster/monster-info.h"
32 #include "monster/monster-status.h"
33 #include "monster/monster-util.h"
34 #include "player-status/player-energy.h"
35 #include "player/attack-defense-types.h"
36 #include "player/player-status.h"
37 #include "realm/realm-names-table.h"
38 #include "spell-realm/spells-hex.h"
39 #include "status/action-setter.h"
40 #include "system/dungeon-info.h"
41 #include "system/floor-type-definition.h"
42 #include "system/grid-type-definition.h"
43 #include "system/monster-entity.h"
44 #include "system/player-type-definition.h"
45 #include "system/system-variables.h"
46 #include "system/terrain-type-definition.h"
47 #include "util/bit-flags-calculator.h"
48 #include "view/display-messages.h"
49 #include "window/main-window-util.h"
50 #include "world/world.h"
51
52 constexpr auto MAX_FEAT_IN_TERRAIN = 18;
53
54 std::vector<std::vector<wilderness_type>> wilderness;
55 static bool generate_encounter;
56
57 struct border_type {
58     int16_t north[MAX_WID];
59     int16_t south[MAX_WID];
60     int16_t east[MAX_HGT];
61     int16_t west[MAX_HGT];
62     int16_t north_west;
63     int16_t north_east;
64     int16_t south_west;
65     int16_t south_east;
66 };
67
68 struct wilderness_grid {
69     wt_type terrain; /* Terrain type */
70     int16_t town; /* Town number */
71     DEPTH level; /* Level of the wilderness */
72     byte road; /* Road */
73     char name[32]; /* Name of the town/wilderness */
74 };
75
76 static border_type border;
77
78 static wilderness_grid w_letter[255];
79
80 /* The default table in terrain level generation. */
81 static int16_t terrain_table[MAX_WILDERNESS][MAX_FEAT_IN_TERRAIN];
82
83 static int16_t conv_terrain2feat[MAX_WILDERNESS];
84
85 /*!
86  * @brief 地形生成確率を決める要素100の配列を確率テーブルから作成する
87  * @param feat_type 非一様確率を再現するための要素数100の配列
88  * @param prob 元の確率テーブル
89  */
90 static void set_floor_and_wall_aux(int16_t feat_type[100], const std::array<feat_prob, DUNGEON_FEAT_PROB_NUM> &prob)
91 {
92     std::array<int, DUNGEON_FEAT_PROB_NUM> lim{};
93     lim[0] = prob[0].percent;
94     for (int i = 1; i < DUNGEON_FEAT_PROB_NUM; i++) {
95         lim[i] = lim[i - 1] + prob[i].percent;
96     }
97
98     if (lim[DUNGEON_FEAT_PROB_NUM - 1] < 100) {
99         lim[DUNGEON_FEAT_PROB_NUM - 1] = 100;
100     }
101
102     int cur = 0;
103     for (int i = 0; i < 100; i++) {
104         while (i == lim[cur]) {
105             cur++;
106         }
107
108         feat_type[i] = prob[cur].feat;
109     }
110 }
111
112 /*!
113  * @brief ダンジョンの地形を指定確率に応じて各マスへランダムに敷き詰める
114  * / Fill the arrays of floors and walls in the good proportions
115  * @param type ダンジョンID
116  */
117 void set_floor_and_wall(DUNGEON_IDX type)
118 {
119     DUNGEON_IDX cur_type = 255;
120     if (cur_type == type) {
121         return;
122     }
123
124     cur_type = type;
125     const auto &dungeon = dungeons_info[type];
126     set_floor_and_wall_aux(feat_ground_type, dungeon.floor);
127     set_floor_and_wall_aux(feat_wall_type, dungeon.fill);
128     feat_wall_outer = dungeon.outer_wall;
129     feat_wall_inner = dungeon.inner_wall;
130     feat_wall_solid = dungeon.outer_wall;
131 }
132
133 /*!
134  * @brief プラズマフラクタル的地形生成の再帰中間処理
135  * / Helper for plasma generation.
136  * @param x1 左上端の深み
137  * @param x2 右上端の深み
138  * @param x3 左下端の深み
139  * @param x4 右下端の深み
140  * @param xmid 中央座標X
141  * @param ymid 中央座標Y
142  * @param rough ランダム幅
143  * @param depth_max 深みの最大値
144  */
145 static void perturb_point_mid(
146     FloorType *floor_ptr, FEAT_IDX x1, FEAT_IDX x2, FEAT_IDX x3, FEAT_IDX x4, POSITION xmid, POSITION ymid, FEAT_IDX rough, FEAT_IDX depth_max)
147 {
148     FEAT_IDX tmp2 = rough * 2 + 1;
149     FEAT_IDX tmp = randint1(tmp2) - (rough + 1);
150     FEAT_IDX avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
151     if (((x1 + x2 + x3 + x4) % 4) > 1) {
152         avg++;
153     }
154
155     if (avg < 0) {
156         avg = 0;
157     }
158
159     if (avg > depth_max) {
160         avg = depth_max;
161     }
162
163     floor_ptr->grid_array[ymid][xmid].feat = (FEAT_IDX)avg;
164 }
165
166 /*!
167  * @brief プラズマフラクタル的地形生成の再帰末端処理
168  * / Helper for plasma generation.
169  * @param x1 中間末端部1の重み
170  * @param x2 中間末端部2の重み
171  * @param x3 中間末端部3の重み
172  * @param xmid 最終末端部座標X
173  * @param ymid 最終末端部座標Y
174  * @param rough ランダム幅
175  * @param depth_max 深みの最大値
176  */
177 static void perturb_point_end(FloorType *floor_ptr, FEAT_IDX x1, FEAT_IDX x2, FEAT_IDX x3, POSITION xmid, POSITION ymid, FEAT_IDX rough, FEAT_IDX depth_max)
178 {
179     FEAT_IDX tmp2 = rough * 2 + 1;
180     FEAT_IDX tmp = randint0(tmp2) - rough;
181     FEAT_IDX avg = ((x1 + x2 + x3) / 3) + tmp;
182     if ((x1 + x2 + x3) % 3) {
183         avg++;
184     }
185
186     if (avg < 0) {
187         avg = 0;
188     }
189
190     if (avg > depth_max) {
191         avg = depth_max;
192     }
193
194     floor_ptr->grid_array[ymid][xmid].feat = (FEAT_IDX)avg;
195 }
196
197 /*!
198  * @brief プラズマフラクタル的地形生成の開始処理
199  * / Helper for plasma generation.
200  * @param x1 処理範囲の左上X座標
201  * @param y1 処理範囲の左上Y座標
202  * @param x2 処理範囲の右下X座標
203  * @param y2 処理範囲の右下Y座標
204  * @param depth_max 深みの最大値
205  * @param rough ランダム幅
206  * @details
207  * <pre>
208  * A generic function to generate the plasma fractal.
209  * Note that it uses ``cave_feat'' as temporary storage.
210  * The values in ``cave_feat'' after this function
211  * are NOT actual features; They are raw heights which
212  * need to be converted to features.
213  * </pre>
214  */
215 static void plasma_recursive(FloorType *floor_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, FEAT_IDX depth_max, FEAT_IDX rough)
216 {
217     POSITION xmid = (x2 - x1) / 2 + x1;
218     POSITION ymid = (y2 - y1) / 2 + y1;
219     if (x1 + 1 == x2) {
220         return;
221     }
222
223     perturb_point_mid(floor_ptr, floor_ptr->grid_array[y1][x1].feat, floor_ptr->grid_array[y2][x1].feat, floor_ptr->grid_array[y1][x2].feat,
224         floor_ptr->grid_array[y2][x2].feat, xmid, ymid, rough, depth_max);
225     perturb_point_end(
226         floor_ptr, floor_ptr->grid_array[y1][x1].feat, floor_ptr->grid_array[y1][x2].feat, floor_ptr->grid_array[ymid][xmid].feat, xmid, y1, rough, depth_max);
227     perturb_point_end(
228         floor_ptr, floor_ptr->grid_array[y1][x2].feat, floor_ptr->grid_array[y2][x2].feat, floor_ptr->grid_array[ymid][xmid].feat, x2, ymid, rough, depth_max);
229     perturb_point_end(
230         floor_ptr, floor_ptr->grid_array[y2][x2].feat, floor_ptr->grid_array[y2][x1].feat, floor_ptr->grid_array[ymid][xmid].feat, xmid, y2, rough, depth_max);
231     perturb_point_end(
232         floor_ptr, floor_ptr->grid_array[y2][x1].feat, floor_ptr->grid_array[y1][x1].feat, floor_ptr->grid_array[ymid][xmid].feat, x1, ymid, rough, depth_max);
233     plasma_recursive(floor_ptr, x1, y1, xmid, ymid, depth_max, rough);
234     plasma_recursive(floor_ptr, xmid, y1, x2, ymid, depth_max, rough);
235     plasma_recursive(floor_ptr, x1, ymid, xmid, y2, depth_max, rough);
236     plasma_recursive(floor_ptr, xmid, ymid, x2, y2, depth_max, rough);
237 }
238
239 /*!
240  * @brief 荒野フロア生成のサブルーチン
241  * @param terrain 荒野地形ID
242  * @param seed 乱数の固定シード
243  * @param border 未使用
244  * @param corner 広域マップの角部分としての生成ならばTRUE
245  */
246 static void generate_wilderness_area(FloorType *floor_ptr, int terrain, uint32_t seed, bool corner)
247 {
248     if (terrain == TERRAIN_EDGE) {
249         for (POSITION y1 = 0; y1 < MAX_HGT; y1++) {
250             for (POSITION x1 = 0; x1 < MAX_WID; x1++) {
251                 floor_ptr->grid_array[y1][x1].feat = feat_permanent;
252             }
253         }
254
255         return;
256     }
257
258     const auto rng_backup = w_ptr->rng;
259     w_ptr->rng.set_state(seed);
260     int table_size = sizeof(terrain_table[0]) / sizeof(int16_t);
261     if (!corner) {
262         for (POSITION y1 = 0; y1 < MAX_HGT; y1++) {
263             for (POSITION x1 = 0; x1 < MAX_WID; x1++) {
264                 floor_ptr->grid_array[y1][x1].feat = table_size / 2;
265             }
266         }
267     }
268
269     floor_ptr->grid_array[1][1].feat = (int16_t)randint0(table_size);
270     floor_ptr->grid_array[MAX_HGT - 2][1].feat = (int16_t)randint0(table_size);
271     floor_ptr->grid_array[1][MAX_WID - 2].feat = (int16_t)randint0(table_size);
272     floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat = (int16_t)randint0(table_size);
273     if (corner) {
274         floor_ptr->grid_array[1][1].feat = terrain_table[terrain][floor_ptr->grid_array[1][1].feat];
275         floor_ptr->grid_array[MAX_HGT - 2][1].feat = terrain_table[terrain][floor_ptr->grid_array[MAX_HGT - 2][1].feat];
276         floor_ptr->grid_array[1][MAX_WID - 2].feat = terrain_table[terrain][floor_ptr->grid_array[1][MAX_WID - 2].feat];
277         floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat = terrain_table[terrain][floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat];
278         w_ptr->rng = rng_backup;
279         return;
280     }
281
282     int16_t north_west = floor_ptr->grid_array[1][1].feat;
283     int16_t south_west = floor_ptr->grid_array[MAX_HGT - 2][1].feat;
284     int16_t north_east = floor_ptr->grid_array[1][MAX_WID - 2].feat;
285     int16_t south_east = floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat;
286     FEAT_IDX roughness = 1; /* The roughness of the level. */
287     plasma_recursive(floor_ptr, 1, 1, MAX_WID - 2, MAX_HGT - 2, table_size - 1, roughness);
288     floor_ptr->grid_array[1][1].feat = north_west;
289     floor_ptr->grid_array[MAX_HGT - 2][1].feat = south_west;
290     floor_ptr->grid_array[1][MAX_WID - 2].feat = north_east;
291     floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat = south_east;
292     for (POSITION y1 = 1; y1 < MAX_HGT - 1; y1++) {
293         for (POSITION x1 = 1; x1 < MAX_WID - 1; x1++) {
294             floor_ptr->grid_array[y1][x1].feat = terrain_table[terrain][floor_ptr->grid_array[y1][x1].feat];
295         }
296     }
297
298     w_ptr->rng = rng_backup;
299 }
300
301 /*!
302  * @brief 荒野フロア生成のメインルーチン
303  * @param player_ptr プレイヤーへの参照ポインタ
304  * @param y 広域Y座標
305  * @param x 広域X座標
306  * @param is_border 広域マップの辺部分としての生成ならばTRUE
307  * @param is_corner 広域マップの角部分としての生成ならばTRUE
308  */
309 static void generate_area(PlayerType *player_ptr, POSITION y, POSITION x, bool is_border, bool is_corner)
310 {
311     player_ptr->town_num = wilderness[y][x].town;
312     auto *floor_ptr = player_ptr->current_floor_ptr;
313     floor_ptr->base_level = wilderness[y][x].level;
314     floor_ptr->dun_level = 0;
315     floor_ptr->monster_level = floor_ptr->base_level;
316     floor_ptr->object_level = floor_ptr->base_level;
317     if (player_ptr->town_num) {
318         init_buildings();
319         if (is_border || is_corner) {
320             init_flags = i2enum<init_flags_type>(INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES);
321         } else {
322             init_flags = INIT_CREATE_DUNGEON;
323         }
324
325         parse_fixed_map(player_ptr, TOWN_DEFINITION_LIST, 0, 0, MAX_HGT, MAX_WID);
326         if (!is_corner && !is_border) {
327             player_ptr->visit |= (1UL << (player_ptr->town_num - 1));
328         }
329     } else {
330         int terrain = wilderness[y][x].terrain;
331         uint32_t seed = wilderness[y][x].seed;
332         generate_wilderness_area(floor_ptr, terrain, seed, is_corner);
333     }
334
335     if (!is_corner && !wilderness[y][x].town) {
336         //!< @todo make the road a bit more interresting.
337         if (wilderness[y][x].road) {
338             floor_ptr->grid_array[MAX_HGT / 2][MAX_WID / 2].feat = feat_floor;
339             POSITION x1, y1;
340             if (wilderness[y - 1][x].road) {
341                 /* North road */
342                 for (y1 = 1; y1 < MAX_HGT / 2; y1++) {
343                     x1 = MAX_WID / 2;
344                     floor_ptr->grid_array[y1][x1].feat = feat_floor;
345                 }
346             }
347
348             if (wilderness[y + 1][x].road) {
349                 /* North road */
350                 for (y1 = MAX_HGT / 2; y1 < MAX_HGT - 1; y1++) {
351                     x1 = MAX_WID / 2;
352                     floor_ptr->grid_array[y1][x1].feat = feat_floor;
353                 }
354             }
355
356             if (wilderness[y][x + 1].road) {
357                 /* East road */
358                 for (x1 = MAX_WID / 2; x1 < MAX_WID - 1; x1++) {
359                     y1 = MAX_HGT / 2;
360                     floor_ptr->grid_array[y1][x1].feat = feat_floor;
361                 }
362             }
363
364             if (wilderness[y][x - 1].road) {
365                 /* West road */
366                 for (x1 = 1; x1 < MAX_WID / 2; x1++) {
367                     y1 = MAX_HGT / 2;
368                     floor_ptr->grid_array[y1][x1].feat = feat_floor;
369                 }
370             }
371         }
372     }
373
374     bool is_winner = wilderness[y][x].entrance > 0;
375     is_winner &= (wilderness[y][x].town == 0);
376     bool is_wild_winner = dungeons_info[wilderness[y][x].entrance].flags.has_not(DungeonFeatureType::WINNER);
377     is_winner &= ((w_ptr->total_winner != 0) || is_wild_winner);
378     if (!is_winner) {
379         return;
380     }
381
382     const auto rng_backup = w_ptr->rng;
383     w_ptr->rng.set_state(wilderness[y][x].seed);
384     int dy = rand_range(6, floor_ptr->height - 6);
385     int dx = rand_range(6, floor_ptr->width - 6);
386     floor_ptr->grid_array[dy][dx].feat = feat_entrance;
387     floor_ptr->grid_array[dy][dx].special = wilderness[y][x].entrance;
388     w_ptr->rng = rng_backup;
389 }
390
391 /*!
392  * @brief 地上マップにモンスターを生成する
393  * @param player_ptr プレイヤーへの参照ポインタ
394  * @details '>' キーで普通に入った時と、襲撃を受けた時でモンスター数は異なる.
395  * また、集団生成や護衛は、最初に生成された1体だけがカウント対象である.
396  * よって、実際に生成されるモンスターは、コードの見た目より多くなる.
397  */
398 static void generate_wild_monsters(PlayerType *player_ptr)
399 {
400     constexpr auto num_ambush_monsters = 40;
401     constexpr auto num_normal_monsters = 8;
402     const auto lim = generate_encounter ? num_ambush_monsters : num_normal_monsters;
403     for (auto i = 0; i < lim; i++) {
404         BIT_FLAGS mode = 0;
405         if (!(generate_encounter || (one_in_(2) && (!player_ptr->town_num)))) {
406             mode |= PM_ALLOW_SLEEP;
407         }
408
409         (void)alloc_monster(player_ptr, generate_encounter ? 0 : 3, mode, summon_specific);
410     }
411 }
412
413 /*!
414  * @brief 広域マップの生成 /
415  * Build the wilderness area outside of the town.
416  * @todo 広域マップは恒常生成にする予定、PlayerTypeによる処理分岐は最終的に排除する。
417  * @param player_ptr プレイヤーへの参照ポインタ
418  */
419 void wilderness_gen(PlayerType *player_ptr)
420 {
421     auto &floor = *player_ptr->current_floor_ptr;
422     floor.height = MAX_HGT;
423     floor.width = MAX_WID;
424     panel_row_min = floor.height;
425     panel_col_min = floor.width;
426     parse_fixed_map(player_ptr, WILDERNESS_DEFINITION, 0, 0, w_ptr->max_wild_y, w_ptr->max_wild_x);
427     const auto wild_y = player_ptr->wilderness_y;
428     const auto wild_x = player_ptr->wilderness_x;
429     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), nullptr);
430
431     /* North border */
432     generate_area(player_ptr, wild_y - 1, wild_x, true, false);
433     for (int i = 1; i < MAX_WID - 1; i++) {
434         border.north[i] = floor.grid_array[MAX_HGT - 2][i].feat;
435     }
436
437     /* South border */
438     generate_area(player_ptr, wild_y + 1, wild_x, true, false);
439     for (int i = 1; i < MAX_WID - 1; i++) {
440         border.south[i] = floor.grid_array[1][i].feat;
441     }
442
443     /* West border */
444     generate_area(player_ptr, wild_y, wild_x - 1, true, false);
445     for (int i = 1; i < MAX_HGT - 1; i++) {
446         border.west[i] = floor.grid_array[i][MAX_WID - 2].feat;
447     }
448
449     /* East border */
450     generate_area(player_ptr, wild_y, wild_x + 1, true, false);
451     for (int i = 1; i < MAX_HGT - 1; i++) {
452         border.east[i] = floor.grid_array[i][1].feat;
453     }
454
455     /* North west corner */
456     generate_area(player_ptr, wild_y - 1, wild_x - 1, false, true);
457     border.north_west = floor.grid_array[MAX_HGT - 2][MAX_WID - 2].feat;
458
459     /* North east corner */
460     generate_area(player_ptr, wild_y - 1, wild_x + 1, false, true);
461     border.north_east = floor.grid_array[MAX_HGT - 2][1].feat;
462
463     /* South west corner */
464     generate_area(player_ptr, wild_y + 1, wild_x - 1, false, true);
465     border.south_west = floor.grid_array[1][MAX_WID - 2].feat;
466
467     /* South east corner */
468     generate_area(player_ptr, wild_y + 1, wild_x + 1, false, true);
469     border.south_east = floor.grid_array[1][1].feat;
470
471     /* Create terrain of the current area */
472     generate_area(player_ptr, wild_y, wild_x, false, false);
473
474     /* Special boundary walls -- North */
475     for (auto i = 0; i < MAX_WID; i++) {
476         auto &grid = floor.get_grid({ 0, i });
477         grid.feat = feat_permanent;
478         grid.mimic = border.north[i];
479     }
480
481     /* Special boundary walls -- South */
482     for (auto i = 0; i < MAX_WID; i++) {
483         auto &grid = floor.get_grid({ MAX_HGT - 1, i });
484         grid.feat = feat_permanent;
485         grid.mimic = border.south[i];
486     }
487
488     /* Special boundary walls -- West */
489     for (auto i = 0; i < MAX_HGT; i++) {
490         auto &grid = floor.get_grid({ i, 0 });
491         grid.feat = feat_permanent;
492         grid.mimic = border.west[i];
493     }
494
495     /* Special boundary walls -- East */
496     for (auto i = 0; i < MAX_HGT; i++) {
497         auto &grid = floor.get_grid({ i, MAX_WID - 1 });
498         grid.feat = feat_permanent;
499         grid.mimic = border.east[i];
500     }
501
502     floor.get_grid({ 0, 0 }).mimic = border.north_west;
503     floor.get_grid({ 0, MAX_WID - 1 }).mimic = border.north_east;
504     floor.get_grid({ MAX_HGT - 1, 0 }).mimic = border.south_west;
505     floor.get_grid({ MAX_HGT - 1, MAX_WID - 1 }).mimic = border.south_east;
506     for (auto y = 0; y < floor.height; y++) {
507         for (auto x = 0; x < floor.width; x++) {
508             auto &grid = floor.get_grid({ y, x });
509             if (w_ptr->is_daytime()) {
510                 grid.info |= CAVE_GLOW;
511                 if (view_perma_grids) {
512                     grid.info |= CAVE_MARK;
513                 }
514
515                 continue;
516             }
517
518             const auto &terrain = grid.get_terrain_mimic();
519             auto can_darken = !grid.is_mirror();
520             can_darken &= terrain.flags.has_none_of({ TerrainCharacteristics::QUEST_ENTER, TerrainCharacteristics::ENTRANCE });
521             if (can_darken) {
522                 grid.info &= ~(CAVE_GLOW);
523                 if (terrain.flags.has_not(TerrainCharacteristics::REMEMBER)) {
524                     grid.info &= ~(CAVE_MARK);
525                 }
526
527                 continue;
528             }
529
530             if (terrain.flags.has_not(TerrainCharacteristics::ENTRANCE)) {
531                 continue;
532             }
533
534             grid.info |= CAVE_GLOW;
535             if (view_perma_grids) {
536                 grid.info |= CAVE_MARK;
537             }
538         }
539     }
540
541     if (player_ptr->teleport_town) {
542         for (auto y = 0; y < floor.height; y++) {
543             for (auto x = 0; x < floor.width; x++) {
544                 auto &grid = floor.get_grid({ y, x });
545                 const auto &terrain = grid.get_terrain();
546                 if (terrain.flags.has_not(TerrainCharacteristics::BLDG)) {
547                     continue;
548                 }
549
550                 if ((terrain.subtype != 4) && !((player_ptr->town_num == 1) && (terrain.subtype == 0))) {
551                     continue;
552                 }
553
554                 if (is_monster(grid.m_idx)) {
555                     delete_monster_idx(player_ptr, grid.m_idx);
556                 }
557
558                 player_ptr->oldpy = y;
559                 player_ptr->oldpx = x;
560             }
561         }
562
563         player_ptr->teleport_town = false;
564     } else if (player_ptr->leaving_dungeon) {
565         for (auto y = 0; y < floor.height; y++) {
566             for (auto x = 0; x < floor.width; x++) {
567                 auto &grid = floor.get_grid({ y, x });
568                 if (!grid.cave_has_flag(TerrainCharacteristics::ENTRANCE)) {
569                     continue;
570                 }
571
572                 if (is_monster(grid.m_idx)) {
573                     delete_monster_idx(player_ptr, grid.m_idx);
574                 }
575
576                 player_ptr->oldpy = y;
577                 player_ptr->oldpx = x;
578             }
579         }
580
581         player_ptr->teleport_town = false;
582     }
583
584     player_place(player_ptr, player_ptr->oldpy, player_ptr->oldpx);
585     generate_wild_monsters(player_ptr);
586     if (generate_encounter) {
587         player_ptr->ambush_flag = true;
588     }
589
590     generate_encounter = false;
591     set_floor_and_wall(0);
592     auto &quest_list = QuestList::get_instance();
593     for (auto &[q_idx, quest] : quest_list) {
594         if (quest.status == QuestStatusType::REWARDED) {
595             quest.status = QuestStatusType::FINISHED;
596         }
597     }
598 }
599
600 /*!
601  * @brief 広域マップの生成(簡易処理版) /
602  * Build the wilderness area. -DG-
603  */
604 void wilderness_gen_small(PlayerType *player_ptr)
605 {
606     auto *floor_ptr = player_ptr->current_floor_ptr;
607     for (int i = 0; i < MAX_WID; i++) {
608         for (int j = 0; j < MAX_HGT; j++) {
609             floor_ptr->grid_array[j][i].feat = feat_permanent;
610         }
611     }
612
613     parse_fixed_map(player_ptr, WILDERNESS_DEFINITION, 0, 0, w_ptr->max_wild_y, w_ptr->max_wild_x);
614     for (int i = 0; i < w_ptr->max_wild_x; i++) {
615         for (int j = 0; j < w_ptr->max_wild_y; j++) {
616             if (wilderness[j][i].town && (wilderness[j][i].town != VALID_TOWNS)) {
617                 floor_ptr->grid_array[j][i].feat = (int16_t)feat_town;
618                 floor_ptr->grid_array[j][i].special = (int16_t)wilderness[j][i].town;
619                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
620                 continue;
621             }
622
623             if (wilderness[j][i].road) {
624                 floor_ptr->grid_array[j][i].feat = feat_floor;
625                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
626                 continue;
627             }
628
629             if (wilderness[j][i].entrance && (w_ptr->total_winner || dungeons_info[wilderness[j][i].entrance].flags.has_not(DungeonFeatureType::WINNER))) {
630                 floor_ptr->grid_array[j][i].feat = feat_entrance;
631                 floor_ptr->grid_array[j][i].special = (byte)wilderness[j][i].entrance;
632                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
633                 continue;
634             }
635
636             floor_ptr->grid_array[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
637             floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
638         }
639     }
640
641     floor_ptr->height = (int16_t)w_ptr->max_wild_y;
642     floor_ptr->width = (int16_t)w_ptr->max_wild_x;
643     if (floor_ptr->height > MAX_HGT) {
644         floor_ptr->height = MAX_HGT;
645     }
646
647     if (floor_ptr->width > MAX_WID) {
648         floor_ptr->width = MAX_WID;
649     }
650
651     panel_row_min = floor_ptr->height;
652     panel_col_min = floor_ptr->width;
653     player_ptr->x = player_ptr->wilderness_x;
654     player_ptr->y = player_ptr->wilderness_y;
655     player_ptr->town_num = 0;
656 }
657
658 /*!
659  * @brief w_info.txtのデータ解析 /
660  * Parse a sub-file of the "extra info"
661  * @param buf 読み取ったデータ行のバッファ
662  * @param ymin 未使用
663  * @param xmin 広域地形マップを読み込みたいx座標の開始位置
664  * @param ymax 未使用
665  * @param xmax 広域地形マップを読み込みたいx座標の終了位置
666  * @param y 広域マップの高さを返す参照ポインタ
667  * @param x 広域マップの幅を返す参照ポインタ
668  */
669 parse_error_type parse_line_wilderness(PlayerType *player_ptr, char *buf, int xmin, int xmax, int *y, int *x)
670 {
671     if (!(buf[0] == 'W')) {
672         return PARSE_ERROR_GENERIC;
673     }
674
675     int num;
676     char *zz[33];
677     switch (buf[2]) {
678         /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
679 #ifdef JP
680     case 'E':
681         return PARSE_ERROR_NONE;
682     case 'F':
683     case 'J':
684 #else
685     case 'J':
686         return PARSE_ERROR_NONE;
687     case 'F':
688     case 'E':
689 #endif
690     {
691         if ((num = tokenize(buf + 4, 6, zz, 0)) > 1) {
692             int index = zz[0][0];
693
694             if (num > 1) {
695                 w_letter[index].terrain = i2enum<wt_type>(atoi(zz[1]));
696             } else {
697                 w_letter[index].terrain = TERRAIN_EDGE;
698             }
699
700             if (num > 2) {
701                 w_letter[index].level = (int16_t)atoi(zz[2]);
702             } else {
703                 w_letter[index].level = 0;
704             }
705
706             if (num > 3) {
707                 w_letter[index].town = static_cast<int16_t>(atoi(zz[3]));
708             } else {
709                 w_letter[index].town = 0;
710             }
711
712             if (num > 4) {
713                 w_letter[index].road = (byte)atoi(zz[4]);
714             } else {
715                 w_letter[index].road = 0;
716             }
717
718             if (num > 5) {
719                 strcpy(w_letter[index].name, zz[5]);
720             } else {
721                 w_letter[index].name[0] = 0;
722             }
723         } else {
724             /* Failure */
725             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
726         }
727
728         break;
729     }
730
731     /* Process "W:D:<layout> */
732     /* Layout of the wilderness */
733     case 'D': {
734         char *s = buf + 4;
735         int len = strlen(s);
736         int i;
737         for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++) {
738             int id = s[0];
739             wilderness[*y][*x].terrain = w_letter[id].terrain;
740             wilderness[*y][*x].level = w_letter[id].level;
741             wilderness[*y][*x].town = w_letter[id].town;
742             wilderness[*y][*x].road = w_letter[id].road;
743             towns_info[w_letter[id].town].name = w_letter[id].name;
744         }
745
746         (*y)++;
747         break;
748     }
749
750     /* Process "W:P:<x>:<y> - starting position in the wilderness */
751     case 'P': {
752         bool is_corner = player_ptr->wilderness_x == 0;
753         is_corner = player_ptr->wilderness_y == 0;
754         if (!is_corner) {
755             break;
756         }
757
758         if (tokenize(buf + 4, 2, zz, 0) != 2) {
759             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
760         }
761
762         player_ptr->wilderness_y = atoi(zz[0]);
763         player_ptr->wilderness_x = atoi(zz[1]);
764
765         auto out_of_bounds = (player_ptr->wilderness_x < 1);
766         out_of_bounds |= (player_ptr->wilderness_x > w_ptr->max_wild_x);
767         out_of_bounds |= (player_ptr->wilderness_y < 1);
768         out_of_bounds |= (player_ptr->wilderness_y > w_ptr->max_wild_y);
769         if (out_of_bounds) {
770             return PARSE_ERROR_OUT_OF_BOUNDS;
771         }
772
773         break;
774     }
775
776     default:
777         return PARSE_ERROR_UNDEFINED_DIRECTIVE;
778     }
779
780     for (const auto &d_ref : dungeons_info) {
781         if (d_ref.idx == 0 || !d_ref.maxdepth) {
782             continue;
783         }
784         wilderness[d_ref.dy][d_ref.dx].entrance = static_cast<byte>(d_ref.idx);
785         if (!wilderness[d_ref.dy][d_ref.dx].town) {
786             wilderness[d_ref.dy][d_ref.dx].level = d_ref.mindepth;
787         }
788     }
789
790     return PARSE_ERROR_NONE;
791 }
792
793 /*!
794  * @brief ゲーム開始時に各荒野フロアの乱数シードを指定する /
795  * Generate the random seeds for the wilderness
796  */
797 void seed_wilderness(void)
798 {
799     for (POSITION x = 0; x < w_ptr->max_wild_x; x++) {
800         for (POSITION y = 0; y < w_ptr->max_wild_y; y++) {
801             wilderness[y][x].seed = randint0(0x10000000);
802             wilderness[y][x].entrance = 0;
803         }
804     }
805 }
806
807 /*!
808  * @brief 荒野の地勢設定を初期化する /
809  * Initialize wilderness array
810  * @param terrain 初期化したい地勢ID
811  * @param feat_global 基本的な地形ID
812  * @param fmt 地勢内の地形数を参照するための独自フォーマット
813  */
814 static void init_terrain_table(int terrain, int16_t feat_global, concptr fmt, ...)
815 {
816     va_list vp;
817     va_start(vp, fmt);
818     conv_terrain2feat[terrain] = feat_global;
819     int cur = 0;
820     char check = 'a';
821     for (concptr p = fmt; *p; p++) {
822         if (*p != check) {
823             plog_fmt("Format error");
824             continue;
825         }
826
827         FEAT_IDX feat = (int16_t)va_arg(vp, int);
828         int num = va_arg(vp, int);
829         int lim = cur + num;
830         for (; (cur < lim) && (cur < MAX_FEAT_IN_TERRAIN); cur++) {
831             terrain_table[terrain][cur] = feat;
832         }
833
834         if (cur >= MAX_FEAT_IN_TERRAIN) {
835             break;
836         }
837
838         check++;
839     }
840
841     if (cur < MAX_FEAT_IN_TERRAIN) {
842         plog_fmt("Too few parameters");
843     }
844
845     va_end(vp);
846 }
847
848 /*!
849  * @brief 荒野の地勢設定全体を初期化するメインルーチン /
850  * Initialize arrays for wilderness terrains
851  */
852 void init_wilderness_terrains(void)
853 {
854     init_terrain_table(TERRAIN_EDGE, feat_permanent, "a", feat_permanent, MAX_FEAT_IN_TERRAIN);
855     init_terrain_table(TERRAIN_TOWN, feat_town, "a", feat_floor, MAX_FEAT_IN_TERRAIN);
856     init_terrain_table(TERRAIN_DEEP_WATER, feat_deep_water, "ab", feat_deep_water, 12, feat_shallow_water, MAX_FEAT_IN_TERRAIN - 12);
857     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,
858         MAX_FEAT_IN_TERRAIN - 17);
859     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,
860         MAX_FEAT_IN_TERRAIN - 11);
861     init_terrain_table(
862         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);
863     init_terrain_table(
864         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);
865     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);
866     init_terrain_table(TERRAIN_DESERT, feat_dirt, "abc", feat_floor, 2, feat_dirt, 13, feat_grass, MAX_FEAT_IN_TERRAIN - 15);
867     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);
868     init_terrain_table(
869         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);
870     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,
871         MAX_FEAT_IN_TERRAIN - 8);
872 }
873
874 void init_wilderness_encounter()
875 {
876     generate_encounter = false;
877 }
878
879 /*!
880  * @brief 荒野から広域マップへの切り替え処理 /
881  * Initialize arrays for wilderness terrains
882  * @param encount 襲撃時TRUE
883  * @return 切り替えが行われた場合はTRUEを返す。
884  */
885 bool change_wild_mode(PlayerType *player_ptr, bool encount)
886 {
887     generate_encounter = encount;
888     if (player_ptr->leaving) {
889         return false;
890     }
891
892     if (lite_town || vanilla_town) {
893         msg_print(_("荒野なんてない。", "No global map."));
894         return false;
895     }
896
897     if (player_ptr->wild_mode) {
898         player_ptr->wilderness_x = player_ptr->x;
899         player_ptr->wilderness_y = player_ptr->y;
900         player_ptr->energy_need = 0;
901         player_ptr->wild_mode = false;
902         player_ptr->leaving = true;
903         return true;
904     }
905
906     bool has_pet = false;
907     PlayerEnergy energy(player_ptr);
908     for (int i = 1; i < player_ptr->current_floor_ptr->m_max; i++) {
909         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[i];
910         if (!m_ptr->is_valid()) {
911             continue;
912         }
913
914         if (m_ptr->is_pet() && i != player_ptr->riding) {
915             has_pet = true;
916         }
917
918         if (m_ptr->is_asleep() || (m_ptr->cdis > MAX_PLAYER_SIGHT) || !m_ptr->is_hostile()) {
919             continue;
920         }
921
922         msg_print(_("敵がすぐ近くにいるときは広域マップに入れない!", "You cannot enter global map, since there are some monsters nearby!"));
923         energy.reset_player_turn();
924         return false;
925     }
926
927     if (has_pet) {
928         concptr msg = _("ペットを置いて広域マップに入りますか?", "Do you leave your pets behind? ");
929         if (!input_check_strict(player_ptr, msg, UserCheck::OKAY_CANCEL)) {
930             energy.reset_player_turn();
931             return false;
932         }
933     }
934
935     energy.set_player_turn_energy(1000);
936     player_ptr->oldpx = player_ptr->x;
937     player_ptr->oldpy = player_ptr->y;
938     SpellHex spell_hex(player_ptr);
939     if (spell_hex.is_spelling_any()) {
940         spell_hex.stop_all_spells();
941     }
942
943     set_action(player_ptr, ACTION_NONE);
944     player_ptr->wild_mode = true;
945     player_ptr->leaving = true;
946     return true;
947 }