OSDN Git Service

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