OSDN Git Service

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