OSDN Git Service

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