OSDN Git Service

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