OSDN Git Service

[Refactor] #38997 wild.cの整形 / Reshaped wild.c
[hengband/hengband.git] / src / wild.c
1 /*!
2  * @file wild.c
3  * @brief 荒野マップの生成とルール管理 / Wilderness generation
4  * @date 2014/02/13
5  * @author
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research, and\n
8  * not for profit purposes provided that this copyright and statement are\n
9  * included in all such copies.\n
10  * 2013 Deskull rearranged comment for Doxygen.
11  */
12
13 #include "angband.h"
14 #include "util.h"
15 #include "core.h"
16
17 #include "dungeon.h"
18 #include "floor.h"
19 #include "wild.h"
20 #include "world.h"
21 #include "monster.h"
22 #include "realm-hex.h"
23 #include "player-status.h"
24 #include "player-effects.h"
25 #include "grid.h"
26 #include "monster-status.h"
27 #include "quest.h"
28 #include "dungeon-file.h"
29 #include "files.h"
30 #include "feature.h"
31 #include "floor-town.h"
32 #include "realm.h"
33
34 #include "view-mainwindow.h"
35
36 #define MAX_FEAT_IN_TERRAIN 18
37
38  /*
39   * Wilderness
40   */
41 wilderness_type **wilderness;
42
43 bool generate_encounter;
44
45 /*!
46  * @brief 地形生成確率を決める要素100の配列を確率テーブルから作成する
47  * @param feat_type 非一様確率を再現するための要素数100の配列
48  * @param prob 元の確率テーブル
49  * @return なし
50  */
51 static void set_floor_and_wall_aux(s16b feat_type[100], feat_prob prob[DUNGEON_FEAT_PROB_NUM])
52 {
53         int lim[DUNGEON_FEAT_PROB_NUM], cur = 0, i;
54
55         lim[0] = prob[0].percent;
56         for (i = 1; i < DUNGEON_FEAT_PROB_NUM; i++) lim[i] = lim[i - 1] + prob[i].percent;
57         if (lim[DUNGEON_FEAT_PROB_NUM - 1] < 100) lim[DUNGEON_FEAT_PROB_NUM - 1] = 100;
58
59         for (i = 0; i < 100; i++)
60         {
61                 while (i == lim[cur]) cur++;
62                 feat_type[i] = prob[cur].feat;
63         }
64 }
65
66
67 /*!
68  * @brief ダンジョンの地形を指定確率に応じて各マスへランダムに敷き詰める
69  * / Fill the arrays of floors and walls in the good proportions
70  * @param type ダンジョンID
71  * @return なし
72  */
73 void set_floor_and_wall(DUNGEON_IDX type)
74 {
75         DUNGEON_IDX cur_type = 255;
76         dungeon_type *d_ptr;
77
78         /* Already filled */
79         if (cur_type == type) return;
80
81         cur_type = type;
82         d_ptr = &d_info[type];
83
84         set_floor_and_wall_aux(feat_ground_type, d_ptr->floor);
85         set_floor_and_wall_aux(feat_wall_type, d_ptr->fill);
86
87         feat_wall_outer = d_ptr->outer_wall;
88         feat_wall_inner = d_ptr->inner_wall;
89         feat_wall_solid = d_ptr->outer_wall;
90 }
91
92
93 /*!
94  * @brief プラズマフラクタル的地形生成の再帰中間処理
95  * / Helper for plasma generation.
96  * @param x1 左上端の深み
97  * @param x2 右上端の深み
98  * @param x3 左下端の深み
99  * @param x4 右下端の深み
100  * @param xmid 中央座標X
101  * @param ymid 中央座標Y
102  * @param rough ランダム幅
103  * @param depth_max 深みの最大値
104  * @return なし
105  */
106 static void perturb_point_mid(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)
107 {
108         /*
109          * Average the four corners & perturb it a bit.
110          * tmp is a random int +/- rough
111          */
112         FEAT_IDX tmp2 = rough*2 + 1;
113         FEAT_IDX tmp = randint1(tmp2) - (rough + 1);
114
115         FEAT_IDX avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
116
117         /* Division always rounds down, so we round up again */
118         if (((x1 + x2 + x3 + x4) % 4) > 1)
119                 avg++;
120
121         /* Normalize */
122         if (avg < 0) avg = 0;
123         if (avg > depth_max) avg = depth_max;
124
125         /* Set the new value. */
126         floor_ptr->grid_array[ymid][xmid].feat = (FEAT_IDX)avg;
127 }
128
129
130 /*!
131  * @brief プラズマフラクタル的地形生成の再帰末端処理
132  * / Helper for plasma generation.
133  * @param x1 中間末端部1の重み
134  * @param x2 中間末端部2の重み
135  * @param x3 中間末端部3の重み
136  * @param xmid 最終末端部座標X
137  * @param ymid 最終末端部座標Y
138  * @param rough ランダム幅
139  * @param depth_max 深みの最大値
140  * @return なし
141  */
142 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)
143 {
144         /*
145          * Average the three corners & perturb it a bit.
146          * tmp is a random int +/- rough
147          */
148         FEAT_IDX tmp2 = rough * 2 + 1;
149         FEAT_IDX tmp = randint0(tmp2) - rough;
150
151         FEAT_IDX avg = ((x1 + x2 + x3) / 3) + tmp;
152
153         /* Division always rounds down, so we round up again */
154         if ((x1 + x2 + x3) % 3) avg++;
155
156         /* Normalize */
157         if (avg < 0) avg = 0;
158         if (avg > depth_max) avg = depth_max;
159
160         /* Set the new value. */
161         floor_ptr->grid_array[ymid][xmid].feat = (FEAT_IDX)avg;
162 }
163
164
165 /*!
166  * @brief プラズマフラクタル的地形生成の開始処理
167  * / Helper for plasma generation.
168  * @param x1 処理範囲の左上X座標
169  * @param y1 処理範囲の左上Y座標
170  * @param x2 処理範囲の右下X座標
171  * @param y2 処理範囲の右下Y座標
172  * @param depth_max 深みの最大値
173  * @param rough ランダム幅
174  * @return なし
175  * @details
176  * <pre>
177  * A generic function to generate the plasma fractal.
178  * Note that it uses ``cave_feat'' as temporary storage.
179  * The values in ``cave_feat'' after this function
180  * are NOT actual features; They are raw heights which
181  * need to be converted to features.
182  * </pre>
183  */
184 static void plasma_recursive(floor_type *floor_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, FEAT_IDX depth_max, FEAT_IDX rough)
185 {
186         /* Find middle */
187         POSITION xmid = (x2 - x1) / 2 + x1;
188         POSITION ymid = (y2 - y1) / 2 + y1;
189
190         /* Are we done? */
191         if (x1 + 1 == x2) return;
192
193         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,
194                 floor_ptr->grid_array[y2][x2].feat, xmid, ymid, rough, depth_max);
195
196         perturb_point_end(floor_ptr, floor_ptr->grid_array[y1][x1].feat, floor_ptr->grid_array[y1][x2].feat, floor_ptr->grid_array[ymid][xmid].feat,
197                 xmid, y1, rough, depth_max);
198
199         perturb_point_end(floor_ptr, floor_ptr->grid_array[y1][x2].feat, floor_ptr->grid_array[y2][x2].feat, floor_ptr->grid_array[ymid][xmid].feat,
200                 x2, ymid, rough, depth_max);
201
202         perturb_point_end(floor_ptr, floor_ptr->grid_array[y2][x2].feat, floor_ptr->grid_array[y2][x1].feat, floor_ptr->grid_array[ymid][xmid].feat,
203                 xmid, y2, rough, depth_max);
204
205         perturb_point_end(floor_ptr, floor_ptr->grid_array[y2][x1].feat, floor_ptr->grid_array[y1][x1].feat, floor_ptr->grid_array[ymid][xmid].feat,
206                 x1, ymid, rough, depth_max);
207
208
209         /* Recurse the four quadrants */
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
217 /*
218  * The default table in terrain level generation.
219  */
220 static s16b terrain_table[MAX_WILDERNESS][MAX_FEAT_IN_TERRAIN];
221
222 /*!
223  * @brief 荒野フロア生成のサブルーチン
224  * @param terrain 荒野地形ID
225  * @param seed 乱数の固定シード
226  * @param border 未使用
227  * @param corner 広域マップの角部分としての生成ならばTRUE
228  * @return なし
229  */
230 static void generate_wilderness_area(floor_type *floor_ptr, int terrain, u32b seed, bool corner)
231 {
232         /* The outer wall is easy */
233         if (terrain == TERRAIN_EDGE)
234         {
235                 /* Create level background */
236                 for (POSITION y1 = 0; y1 < MAX_HGT; y1++)
237                 {
238                         for (POSITION x1 = 0; x1 < MAX_WID; x1++)
239                         {
240                                 floor_ptr->grid_array[y1][x1].feat = feat_permanent;
241                         }
242                 }
243
244                 return;
245         }
246
247         /* Hack -- Backup the RNG state */
248         u32b state_backup[4];
249         Rand_state_backup(state_backup);
250
251         /* Hack -- Induce consistant flavors */
252         Rand_state_set(seed);
253
254         int table_size = sizeof(terrain_table[0]) / sizeof(s16b);
255         if (!corner)
256         {
257                 /* Create level background */
258                 for (POSITION y1 = 0; y1 < MAX_HGT; y1++)
259                 {
260                         for (POSITION x1 = 0; x1 < MAX_WID; x1++)
261                         {
262                                 floor_ptr->grid_array[y1][x1].feat = table_size / 2;
263                         }
264                 }
265         }
266
267         /*
268          * Initialize the four corners
269          * ToDo: calculate the medium height of the adjacent
270          * terrains for every corner.
271          */
272         floor_ptr->grid_array[1][1].feat = (s16b)randint0(table_size);
273         floor_ptr->grid_array[MAX_HGT-2][1].feat = (s16b)randint0(table_size);
274         floor_ptr->grid_array[1][MAX_WID-2].feat = (s16b)randint0(table_size);
275         floor_ptr->grid_array[MAX_HGT-2][MAX_WID-2].feat = (s16b)randint0(table_size);
276
277         /* Hack -- only four corners */
278         if (corner)
279         {
280                 floor_ptr->grid_array[1][1].feat = terrain_table[terrain][floor_ptr->grid_array[1][1].feat];
281                 floor_ptr->grid_array[MAX_HGT - 2][1].feat = terrain_table[terrain][floor_ptr->grid_array[MAX_HGT - 2][1].feat];
282                 floor_ptr->grid_array[1][MAX_WID - 2].feat = terrain_table[terrain][floor_ptr->grid_array[1][MAX_WID - 2].feat];
283                 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];
284                 Rand_state_restore(state_backup);
285                 return;
286         }
287
288         /* Hack -- preserve four corners */
289         s16b north_west = floor_ptr->grid_array[1][1].feat;
290         s16b south_west = floor_ptr->grid_array[MAX_HGT - 2][1].feat;
291         s16b north_east = floor_ptr->grid_array[1][MAX_WID - 2].feat;
292         s16b south_east = floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat;
293
294         /* x1, y1, x2, y2, num_depths, roughness */
295         FEAT_IDX roughness = 1; /* The roughness of the level. */
296         plasma_recursive(floor_ptr, 1, 1, MAX_WID - 2, MAX_HGT - 2, table_size - 1, roughness);
297
298         /* Hack -- copyback four corners */
299         floor_ptr->grid_array[1][1].feat = north_west;
300         floor_ptr->grid_array[MAX_HGT - 2][1].feat = south_west;
301         floor_ptr->grid_array[1][MAX_WID - 2].feat = north_east;
302         floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat = south_east;
303
304         for (POSITION y1 = 1; y1 < MAX_HGT - 1; y1++)
305         {
306                 for (POSITION x1 = 1; x1 < MAX_WID - 1; x1++)
307                 {
308                         floor_ptr->grid_array[y1][x1].feat = terrain_table[terrain][floor_ptr->grid_array[y1][x1].feat];
309                 }
310         }
311
312         Rand_state_restore(state_backup);
313 }
314
315
316 /*!
317  * @brief 荒野フロア生成のメインルーチン /
318  * Load a town or generate a terrain level using "plasma" fractals.
319  * @param player_ptr プレーヤーへの参照ポインタ
320  * @param y 広域Y座標
321  * @param x 広域X座標
322  * @param border 広域マップの辺部分としての生成ならばTRUE
323  * @param corner 広域マップの角部分としての生成ならばTRUE
324  * @return なし
325  * @details
326  * <pre>
327  * x and y are the coordinates of the area in the wilderness.
328  * Border and corner are optimization flags to speed up the
329  * generation of the fractal terrain.
330  * If border is set then only the border of the terrain should
331  * be generated (for initializing the border structure).
332  * If corner is set then only the corners of the area are needed.
333  * </pre>
334  */
335 static void generate_area(player_type *player_ptr, POSITION y, POSITION x, bool border, bool corner)
336 {
337         /* Number of the town (if any) */
338         player_ptr->town_num = wilderness[y][x].town;
339
340         /* Set the base level */
341         floor_type *floor_ptr = player_ptr->current_floor_ptr;
342         floor_ptr->base_level = wilderness[y][x].level;
343
344         /* Set the dungeon level */
345         floor_ptr->dun_level = 0;
346
347         /* Set the monster generation level */
348         floor_ptr->monster_level = floor_ptr->base_level;
349
350         /* Set the object generation level */
351         floor_ptr->object_level = floor_ptr->base_level;
352
353
354         /* Create the town */
355         if (player_ptr->town_num)
356         {
357                 /* Reset the buildings */
358                 init_buildings();
359
360                 /* Initialize the town */
361                 if (border | corner)
362                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
363                 else
364                         init_flags = INIT_CREATE_DUNGEON;
365
366                 process_dungeon_file(player_ptr, "t_info.txt", 0, 0, MAX_HGT, MAX_WID);
367
368                 if (!corner && !border) player_ptr->visit |= (1L << (player_ptr->town_num - 1));
369         }
370         else
371         {
372                 int terrain = wilderness[y][x].terrain;
373                 u32b seed = wilderness[y][x].seed;
374
375                 generate_wilderness_area(floor_ptr, terrain, seed, corner);
376         }
377
378         if (!corner && !wilderness[y][x].town)
379         {
380                 /*
381                  * Place roads in the wilderness
382                  * ToDo: make the road a bit more interresting
383                  */
384                 if (wilderness[y][x].road)
385                 {
386                         floor_ptr->grid_array[MAX_HGT/2][MAX_WID/2].feat = feat_floor;
387
388                         POSITION x1, y1;
389                         if (wilderness[y-1][x].road)
390                         {
391                                 /* North road */
392                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
393                                 {
394                                         x1 = MAX_WID/2;
395                                         floor_ptr->grid_array[y1][x1].feat = feat_floor;
396                                 }
397                         }
398
399                         if (wilderness[y+1][x].road)
400                         {
401                                 /* North road */
402                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
403                                 {
404                                         x1 = MAX_WID/2;
405                                         floor_ptr->grid_array[y1][x1].feat = feat_floor;
406                                 }
407                         }
408
409                         if (wilderness[y][x+1].road)
410                         {
411                                 /* East road */
412                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
413                                 {
414                                         y1 = MAX_HGT/2;
415                                         floor_ptr->grid_array[y1][x1].feat = feat_floor;
416                                 }
417                         }
418
419                         if (wilderness[y][x-1].road)
420                         {
421                                 /* West road */
422                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
423                                 {
424                                         y1 = MAX_HGT/2;
425                                         floor_ptr->grid_array[y1][x1].feat = feat_floor;
426                                 }
427                         }
428                 }
429         }
430
431         bool is_winner = wilderness[y][x].entrance;
432         is_winner &= !wilderness[y][x].town;
433         is_winner &= (current_world_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER));
434         if (!is_winner) return;
435
436         /* Hack -- Backup the RNG state */
437         u32b state_backup[4];
438         Rand_state_backup(state_backup);
439
440         /* Hack -- Induce consistant flavors */
441         Rand_state_set(wilderness[y][x].seed);
442
443         int dy = rand_range(6, floor_ptr->height - 6);
444         int dx = rand_range(6, floor_ptr->width - 6);
445
446         floor_ptr->grid_array[dy][dx].feat = feat_entrance;
447         floor_ptr->grid_array[dy][dx].special = wilderness[y][x].entrance;
448
449         /* Hack -- Restore the RNG state */
450         Rand_state_restore(state_backup);
451 }
452
453
454 /*
455  * Border of the wilderness area
456  */
457 static border_type border;
458
459 /*!
460  * @brief 広域マップの生成 /
461  * Build the wilderness area outside of the town.
462  * @todo 広域マップは恒常生成にする予定、player_typeによる処理分岐は最終的に排除する。
463  * @param creature_ptr プレーヤーへの参照ポインタ
464  * @return なし
465  */
466 void wilderness_gen(player_type *creature_ptr)
467 {
468         /* Big town */
469         floor_type *floor_ptr = creature_ptr->current_floor_ptr;
470         floor_ptr->height = MAX_HGT;
471         floor_ptr->width = MAX_WID;
472
473         /* Assume illegal panel */
474         panel_row_min = floor_ptr->height;
475         panel_col_min = floor_ptr->width;
476
477         process_dungeon_file(creature_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
478         POSITION x = creature_ptr->wilderness_x;
479         POSITION y = creature_ptr->wilderness_y;
480         get_mon_num_prep(get_monster_hook(), NULL);
481
482         /* North border */
483         generate_area(creature_ptr, y - 1, x, TRUE, FALSE);
484
485         for (int i = 1; i < MAX_WID - 1; i++)
486         {
487                 border.north[i] = floor_ptr->grid_array[MAX_HGT - 2][i].feat;
488         }
489
490         /* South border */
491         generate_area(creature_ptr, y + 1, x, TRUE, FALSE);
492
493         for (int i = 1; i < MAX_WID - 1; i++)
494         {
495                 border.south[i] = floor_ptr->grid_array[1][i].feat;
496         }
497
498         /* West border */
499         generate_area(creature_ptr, y, x - 1, TRUE, FALSE);
500
501         for (int i = 1; i < MAX_HGT - 1; i++)
502         {
503                 border.west[i] = floor_ptr->grid_array[i][MAX_WID - 2].feat;
504         }
505
506         /* East border */
507         generate_area(creature_ptr, y, x + 1, TRUE, FALSE);
508
509         for (int i = 1; i < MAX_HGT - 1; i++)
510         {
511                 border.east[i] = floor_ptr->grid_array[i][1].feat;
512         }
513
514         /* North west corner */
515         generate_area(creature_ptr, y - 1, x - 1, FALSE, TRUE);
516         border.north_west = floor_ptr->grid_array[MAX_HGT - 2][MAX_WID - 2].feat;
517
518         /* North east corner */
519         generate_area(creature_ptr, y - 1, x + 1, FALSE, TRUE);
520         border.north_east = floor_ptr->grid_array[MAX_HGT - 2][1].feat;
521
522         /* South west corner */
523         generate_area(creature_ptr, y + 1, x - 1, FALSE, TRUE);
524         border.south_west = floor_ptr->grid_array[1][MAX_WID - 2].feat;
525
526         /* South east corner */
527         generate_area(creature_ptr, y + 1, x + 1, FALSE, TRUE);
528         border.south_east = floor_ptr->grid_array[1][1].feat;
529
530         /* Create terrain of the current area */
531         generate_area(creature_ptr, y, x, FALSE, FALSE);
532
533         /* Special boundary walls -- North */
534         for (int i = 0; i < MAX_WID; i++)
535         {
536                 floor_ptr->grid_array[0][i].feat = feat_permanent;
537                 floor_ptr->grid_array[0][i].mimic = border.north[i];
538         }
539
540         /* Special boundary walls -- South */
541         for (int i = 0; i < MAX_WID; i++)
542         {
543                 floor_ptr->grid_array[MAX_HGT - 1][i].feat = feat_permanent;
544                 floor_ptr->grid_array[MAX_HGT - 1][i].mimic = border.south[i];
545         }
546
547         /* Special boundary walls -- West */
548         for (int i = 0; i < MAX_HGT; i++)
549         {
550                 floor_ptr->grid_array[i][0].feat = feat_permanent;
551                 floor_ptr->grid_array[i][0].mimic = border.west[i];
552         }
553
554         /* Special boundary walls -- East */
555         for (int i = 0; i < MAX_HGT; i++)
556         {
557                 floor_ptr->grid_array[i][MAX_WID - 1].feat = feat_permanent;
558                 floor_ptr->grid_array[i][MAX_WID - 1].mimic = border.east[i];
559         }
560
561         floor_ptr->grid_array[0][0].mimic = border.north_west;
562         floor_ptr->grid_array[0][MAX_WID - 1].mimic = border.north_east;
563         floor_ptr->grid_array[MAX_HGT - 1][0].mimic = border.south_west;
564         floor_ptr->grid_array[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
565
566         /* Light up or darken the area */
567         for (y = 0; y < floor_ptr->height; y++)
568         {
569                 for (x = 0; x < floor_ptr->width; x++)
570                 {
571                         grid_type *g_ptr;
572                         g_ptr = &floor_ptr->grid_array[y][x];
573
574                         if (is_daytime())
575                         {
576                                 /* Assume lit */
577                                 g_ptr->info |= CAVE_GLOW;
578
579                                 /* Hack -- Memorize lit grids if allowed */
580                                 if (view_perma_grids) g_ptr->info |= CAVE_MARK;
581                                 continue;
582                         }
583
584                         /* Feature code (applying "mimic" field) */
585                         feature_type *f_ptr;
586                         f_ptr = &f_info[get_feat_mimic(g_ptr)];
587
588                         if (!is_mirror_grid(g_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
589                                 !have_flag(f_ptr->flags, FF_ENTRANCE))
590                         {
591                                 /* Assume dark */
592                                 g_ptr->info &= ~(CAVE_GLOW);
593
594                                 /* Darken "boring" features */
595                                 if (!have_flag(f_ptr->flags, FF_REMEMBER))
596                                 {
597                                         /* Forget the grid */
598                                         g_ptr->info &= ~(CAVE_MARK);
599                                 }
600
601                                 continue;
602                         }
603                         
604                         if (!have_flag(f_ptr->flags, FF_ENTRANCE)) continue;
605
606                         g_ptr->info |= CAVE_GLOW;
607
608                         /* Hack -- Memorize lit grids if allowed */
609                         if (view_perma_grids) g_ptr->info |= CAVE_MARK;
610                 }
611         }
612
613         if (creature_ptr->teleport_town)
614         {
615                 for (y = 0; y < floor_ptr->height; y++)
616                 {
617                         for (x = 0; x < floor_ptr->width; x++)
618                         {
619                                 grid_type *g_ptr;
620                                 g_ptr = &floor_ptr->grid_array[y][x];
621
622                                 /* Seeing true feature code (ignore mimic) */
623                                 feature_type *f_ptr;
624                                 f_ptr = &f_info[g_ptr->feat];
625
626                                 if (!have_flag(f_ptr->flags, FF_BLDG)) continue;
627
628                                 if ((f_ptr->subtype != 4) && !((creature_ptr->town_num == 1) && (f_ptr->subtype == 0)))
629                                         continue;
630
631                                 if (g_ptr->m_idx)
632                                 {
633                                         delete_monster_idx(g_ptr->m_idx);
634                                 }
635
636                                 creature_ptr->oldpy = y;
637                                 creature_ptr->oldpx = x;
638                         }
639                 }
640
641                 creature_ptr->teleport_town = FALSE;
642         }
643         else if (creature_ptr->leaving_dungeon)
644         {
645                 for (y = 0; y < floor_ptr->height; y++)
646                 {
647                         for (x = 0; x < floor_ptr->width; x++)
648                         {
649                                 grid_type *g_ptr;
650                                 g_ptr = &floor_ptr->grid_array[y][x];
651
652                                 if (!cave_have_flag_grid(g_ptr, FF_ENTRANCE)) continue;
653
654                                 if (g_ptr->m_idx)
655                                 {
656                                         delete_monster_idx(g_ptr->m_idx);
657                                 }
658
659                                 creature_ptr->oldpy = y;
660                                 creature_ptr->oldpx = x;
661                         }
662                 }
663
664                 creature_ptr->teleport_town = FALSE;
665         }
666
667         player_place(creature_ptr, creature_ptr->oldpy, creature_ptr->oldpx);
668         int lim = (generate_encounter == TRUE) ? 40 : MIN_M_ALLOC_TN;
669
670         /* Make some residents */
671         for (int i = 0; i < lim; i++)
672         {
673                 BIT_FLAGS mode = 0;
674
675                 if (!(generate_encounter || (one_in_(2) && (!creature_ptr->town_num))))
676                         mode |= PM_ALLOW_SLEEP;
677
678                 /* Make a resident */
679                 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
680         }
681
682         if(generate_encounter) creature_ptr->ambush_flag = TRUE;
683         generate_encounter = FALSE;
684
685         /* Fill the arrays of floors and walls in the good proportions */
686         set_floor_and_wall(0);
687
688         /* Set rewarded quests to finished */
689         for (int i = 0; i < max_q_idx; i++)
690         {
691                 if (quest[i].status == QUEST_STATUS_REWARDED)
692                         quest[i].status = QUEST_STATUS_FINISHED;
693         }
694 }
695
696 static s16b conv_terrain2feat[MAX_WILDERNESS];
697
698 /*!
699  * @brief 広域マップの生成(簡易処理版) /
700  * Build the wilderness area. -DG-
701  * @return なし
702  */
703 void wilderness_gen_small(player_type *creature_ptr)
704 {
705         /* To prevent stupid things */
706         floor_type *floor_ptr = creature_ptr->current_floor_ptr;
707         for (int i = 0; i < MAX_WID; i++)
708         {
709                 for (int j = 0; j < MAX_HGT; j++)
710                 {
711                         floor_ptr->grid_array[j][i].feat = feat_permanent;
712                 }
713         }
714
715         process_dungeon_file(creature_ptr, "w_info.txt", 0, 0, current_world_ptr->max_wild_y, current_world_ptr->max_wild_x);
716
717         /* Fill the map */
718         for (int i = 0; i < current_world_ptr->max_wild_x; i++)
719         {
720                 for (int j = 0; j < current_world_ptr->max_wild_y; j++)
721                 {
722                         if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
723                         {
724                                 floor_ptr->grid_array[j][i].feat = (s16b)feat_town;
725                                 floor_ptr->grid_array[j][i].special = (s16b)wilderness[j][i].town;
726                                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
727                                 continue;
728                         }
729                         
730                         if (wilderness[j][i].road)
731                         {
732                                 floor_ptr->grid_array[j][i].feat = feat_floor;
733                                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
734                                 continue;
735                         }
736                         
737                         if (wilderness[j][i].entrance && (current_world_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
738                         {
739                                 floor_ptr->grid_array[j][i].feat = feat_entrance;
740                                 floor_ptr->grid_array[j][i].special = (byte)wilderness[j][i].entrance;
741                                 floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
742                                 continue;
743                         }
744
745                         floor_ptr->grid_array[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
746                         floor_ptr->grid_array[j][i].info |= (CAVE_GLOW | CAVE_MARK);
747                 }
748         }
749
750         floor_ptr->height = (s16b) current_world_ptr->max_wild_y;
751         floor_ptr->width = (s16b) current_world_ptr->max_wild_x;
752
753         if (floor_ptr->height > MAX_HGT) floor_ptr->height = MAX_HGT;
754         if (floor_ptr->width > MAX_WID) floor_ptr->width = MAX_WID;
755
756         /* Assume illegal panel */
757         panel_row_min = floor_ptr->height;
758         panel_col_min = floor_ptr->width;
759
760         creature_ptr->x = creature_ptr->wilderness_x;
761         creature_ptr->y = creature_ptr->wilderness_y;
762         creature_ptr->town_num = 0;
763 }
764
765
766 typedef struct wilderness_grid wilderness_grid;
767
768 struct wilderness_grid
769 {
770         int             terrain;    /* Terrain type */
771         TOWN_IDX town;   /* Town number */
772         DEPTH level;     /* Level of the wilderness */
773         byte    road;       /* Road */
774         char    name[32];       /* Name of the town/wilderness */
775 };
776
777
778 static wilderness_grid w_letter[255];
779
780 /*!
781  * @brief w_info.txtのデータ解析 /
782  * Parse a sub-file of the "extra info"
783  * @param buf 読み取ったデータ行のバッファ
784  * @param ymin 未使用
785  * @param xmin 広域地形マップを読み込みたいx座標の開始位置
786  * @param ymax 未使用
787  * @param xmax 広域地形マップを読み込みたいx座標の終了位置
788  * @param y 広域マップの高さを返す参照ポインタ
789  * @param x 広域マップの幅を返す参照ポインタ
790  * @return なし
791  */
792 errr parse_line_wilderness(player_type *creature_ptr, char *buf, int xmin, int xmax, int *y, int *x)
793 {
794         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
795
796         int num;
797         char *zz[33];
798         switch (buf[2])
799         {
800                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
801 #ifdef JP
802         case 'E':
803                 return 0;
804         case 'F':
805         case 'J':
806 #else
807         case 'J':
808                 return 0;
809         case 'F':
810         case 'E':
811 #endif
812         {
813                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
814                 {
815                         int index = zz[0][0];
816                         
817                         if (num > 1)
818                                 w_letter[index].terrain = atoi(zz[1]);
819                         else
820                                 w_letter[index].terrain = 0;
821                         
822                         if (num > 2)
823                                 w_letter[index].level = (s16b)atoi(zz[2]);
824                         else
825                                 w_letter[index].level = 0;
826                         
827                         if (num > 3)
828                                 w_letter[index].town = (TOWN_IDX)atoi(zz[3]);
829                         else
830                                 w_letter[index].town = 0;
831                         
832                         if (num > 4)
833                                 w_letter[index].road = (byte_hack)atoi(zz[4]);
834                         else
835                                 w_letter[index].road = 0;
836                         
837                         if (num > 5)
838                                 strcpy(w_letter[index].name, zz[5]);
839                         else
840                                 w_letter[index].name[0] = 0;
841                 }
842                 else
843                 {
844                                 /* Failure */
845                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
846                 }
847                 
848                 break;
849         }
850         
851         /* Process "W:D:<layout> */
852         /* Layout of the wilderness */
853         case 'D':
854         {
855                 char *s = buf+4;
856                 int len = strlen(s);
857                 int i;
858                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
859                 {
860                         int id = s[0];
861                         wilderness[*y][*x].terrain = w_letter[id].terrain;
862                         wilderness[*y][*x].level = w_letter[id].level;
863                         wilderness[*y][*x].town = w_letter[id].town;
864                         wilderness[*y][*x].road = w_letter[id].road;
865                         strcpy(town_info[w_letter[id].town].name, w_letter[id].name);
866                 }
867                 
868                 (*y)++;
869                 break;
870         }
871         
872         /* Process "W:P:<x>:<y> - starting position in the wilderness */
873         case 'P':
874         {
875                 bool is_corner = creature_ptr->wilderness_x == 0;
876                 is_corner = creature_ptr->wilderness_y == 0;
877                 if (!is_corner) break;
878
879                 if (tokenize(buf + 4, 2, zz, 0) != 2)
880                 {
881                         return PARSE_ERROR_TOO_FEW_ARGUMENTS;
882                 }
883
884                 creature_ptr->wilderness_y = atoi(zz[0]);
885                 creature_ptr->wilderness_x = atoi(zz[1]);
886
887                 if ((creature_ptr->wilderness_x < 1) ||
888                         (creature_ptr->wilderness_x > current_world_ptr->max_wild_x) ||
889                         (creature_ptr->wilderness_y < 1) ||
890                         (creature_ptr->wilderness_y > current_world_ptr->max_wild_y))
891                 {
892                         return PARSE_ERROR_OUT_OF_BOUNDS;
893                 }
894
895                 break;
896         }
897         
898         default:
899                 return PARSE_ERROR_UNDEFINED_DIRECTIVE;
900         }
901         
902         for (int i = 1; i < current_world_ptr->max_d_idx; i++)
903         {
904                 if (!d_info[i].maxdepth) continue;
905                 wilderness[d_info[i].dy][d_info[i].dx].entrance = (byte_hack)i;
906                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
907                 {
908                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
909                 }
910         }
911
912         return 0;
913 }
914
915
916 /*!
917  * @brief ゲーム開始時に各荒野フロアの乱数シードを指定する /
918  * Generate the random seeds for the wilderness
919  * @return なし
920  */
921 void seed_wilderness(void)
922 {
923         for (POSITION x = 0; x < current_world_ptr->max_wild_x; x++)
924         {
925                 for (POSITION y = 0; y < current_world_ptr->max_wild_y; y++)
926                 {
927                         wilderness[y][x].seed = randint0(0x10000000);
928                         wilderness[y][x].entrance = 0;
929                 }
930         }
931 }
932
933
934 /*
935  * Pointer to wilderness_type
936  */
937 typedef wilderness_type *wilderness_type_ptr;
938
939 /*!
940  * @brief ゲーム開始時の荒野初期化メインルーチン /
941  * Initialize wilderness array
942  * @return エラーコード
943  */
944 errr init_wilderness(void)
945 {
946         /* Allocate the wilderness (two-dimension array) */
947         C_MAKE(wilderness, current_world_ptr->max_wild_y, wilderness_type_ptr);
948         C_MAKE(wilderness[0], current_world_ptr->max_wild_x * current_world_ptr->max_wild_y, wilderness_type);
949
950         /* Init the other pointers */
951         for (int i = 1; i < current_world_ptr->max_wild_y; i++)
952         {
953                 wilderness[i] = wilderness[0] + i * current_world_ptr->max_wild_x;
954         }
955
956         generate_encounter = FALSE;
957         return 0;
958 }
959
960
961 /*!
962  * @brief 荒野の地勢設定を初期化する /
963  * Initialize wilderness array
964  * @param terrain 初期化したい地勢ID
965  * @param feat_global 基本的な地形ID
966  * @param fmt 地勢内の地形数を参照するための独自フォーマット
967  * @return なし
968  */
969 static void init_terrain_table(int terrain, s16b feat_global, concptr fmt, ...)
970 {
971         /* Begin the varargs stuff */
972         va_list vp;
973         va_start(vp, fmt);
974
975         /* Wilderness terrains on global map */
976         conv_terrain2feat[terrain] = feat_global;
977
978         /* Wilderness terrains on local map */
979         int cur = 0;
980         char check = 'a';
981         for (concptr p = fmt; *p; p++)
982         {
983                 if (*p != check)
984                 {
985                         plog_fmt("Format error");
986                         continue;
987                 }
988
989                 FEAT_IDX feat = (s16b)va_arg(vp, int);
990                 int num = va_arg(vp, int);
991                 int lim = cur + num;
992
993                 for (; (cur < lim) && (cur < MAX_FEAT_IN_TERRAIN); cur++)
994                 {
995                         terrain_table[terrain][cur] = feat;
996                 }
997
998                 if (cur >= MAX_FEAT_IN_TERRAIN) break;
999
1000                 check++;
1001         }
1002
1003         if (cur < MAX_FEAT_IN_TERRAIN)
1004         {
1005                 plog_fmt("Too few parameters");
1006         }
1007
1008         va_end(vp);
1009 }
1010
1011
1012 /*!
1013  * @brief 荒野の地勢設定全体を初期化するメインルーチン /
1014  * Initialize arrays for wilderness terrains
1015  * @return なし
1016  */
1017 void init_wilderness_terrains(void)
1018 {
1019         init_terrain_table(TERRAIN_EDGE, feat_permanent, "a",
1020                 feat_permanent, MAX_FEAT_IN_TERRAIN);
1021
1022         init_terrain_table(TERRAIN_TOWN, feat_town, "a",
1023                 feat_floor, MAX_FEAT_IN_TERRAIN);
1024
1025         init_terrain_table(TERRAIN_DEEP_WATER, feat_deep_water, "ab",
1026                 feat_deep_water, 12,
1027                 feat_shallow_water, MAX_FEAT_IN_TERRAIN - 12);
1028
1029         init_terrain_table(TERRAIN_SHALLOW_WATER, feat_shallow_water, "abcde",
1030                 feat_deep_water, 3,
1031                 feat_shallow_water, 12,
1032                 feat_floor, 1,
1033                 feat_dirt, 1,
1034                 feat_grass, MAX_FEAT_IN_TERRAIN - 17);
1035
1036         init_terrain_table(TERRAIN_SWAMP, feat_swamp, "abcdef",
1037                 feat_dirt, 2,
1038                 feat_grass, 3,
1039                 feat_tree, 1,
1040                 feat_brake, 1,
1041                 feat_shallow_water, 4,
1042                 feat_swamp, MAX_FEAT_IN_TERRAIN - 11);
1043
1044         init_terrain_table(TERRAIN_DIRT, feat_dirt, "abcdef",
1045                 feat_floor, 3,
1046                 feat_dirt, 10,
1047                 feat_flower, 1,
1048                 feat_brake, 1,
1049                 feat_grass, 1,
1050                 feat_tree, MAX_FEAT_IN_TERRAIN - 16);
1051
1052         init_terrain_table(TERRAIN_GRASS, feat_grass, "abcdef",
1053                 feat_floor, 2,
1054                 feat_dirt, 2,
1055                 feat_grass, 9,
1056                 feat_flower, 1,
1057                 feat_brake, 2,
1058                 feat_tree, MAX_FEAT_IN_TERRAIN - 16);
1059
1060         init_terrain_table(TERRAIN_TREES, feat_tree, "abcde",
1061                 feat_floor, 2,
1062                 feat_dirt, 1,
1063                 feat_tree, 11,
1064                 feat_brake, 2,
1065                 feat_grass, MAX_FEAT_IN_TERRAIN - 16);
1066
1067         init_terrain_table(TERRAIN_DESERT, feat_dirt, "abc",
1068                 feat_floor, 2,
1069                 feat_dirt, 13,
1070                 feat_grass, MAX_FEAT_IN_TERRAIN - 15);
1071
1072         init_terrain_table(TERRAIN_SHALLOW_LAVA, feat_shallow_lava, "abc",
1073                 feat_shallow_lava, 14,
1074                 feat_deep_lava, 3,
1075                 feat_mountain, MAX_FEAT_IN_TERRAIN - 17);
1076
1077         init_terrain_table(TERRAIN_DEEP_LAVA, feat_deep_lava, "abcd",
1078                 feat_dirt, 3,
1079                 feat_shallow_lava, 3,
1080                 feat_deep_lava, 10,
1081                 feat_mountain, MAX_FEAT_IN_TERRAIN - 16);
1082
1083         init_terrain_table(TERRAIN_MOUNTAIN, feat_mountain, "abcdef",
1084                 feat_floor, 1,
1085                 feat_brake, 1,
1086                 feat_grass, 2,
1087                 feat_dirt, 2,
1088                 feat_tree, 2,
1089                 feat_mountain, MAX_FEAT_IN_TERRAIN - 8);
1090 }
1091
1092
1093 /*!
1094  * @brief 荒野から広域マップへの切り替え処理 /
1095  * Initialize arrays for wilderness terrains
1096  * @param encount 襲撃時TRUE
1097  * @return 切り替えが行われた場合はTRUEを返す。
1098  */
1099 bool change_wild_mode(player_type *creature_ptr, bool encount)
1100 {
1101         generate_encounter = encount;
1102
1103         /* It is in the middle of changing map */
1104         if (creature_ptr->leaving) return FALSE;
1105
1106         if (lite_town || vanilla_town)
1107         {
1108                 msg_print(_("荒野なんてない。", "No global map."));
1109                 return FALSE;
1110         }
1111
1112         if (creature_ptr->wild_mode)
1113         {
1114                 /* Save the location in the global map */
1115                 creature_ptr->wilderness_x = creature_ptr->x;
1116                 creature_ptr->wilderness_y = creature_ptr->y;
1117
1118                 /* Give first move to the player */
1119                 creature_ptr->energy_need = 0;
1120
1121                 /* Go back to the ordinary map */
1122                 creature_ptr->wild_mode = FALSE;
1123                 creature_ptr->leaving = TRUE;
1124                 return TRUE;
1125         }
1126
1127         bool have_pet = FALSE;
1128         for (int i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1129         {
1130                 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1131
1132                 if (!monster_is_valid(m_ptr)) continue;
1133                 if (is_pet(m_ptr) && i != creature_ptr->riding) have_pet = TRUE;
1134                 if (MON_CSLEEP(m_ptr)) continue;
1135                 if (m_ptr->cdis > MAX_SIGHT) continue;
1136                 if (!is_hostile(m_ptr)) continue;
1137                 msg_print(_("敵がすぐ近くにいるときは広域マップに入れない!",
1138                         "You cannot enter global map, since there is some monsters nearby!"));
1139                 free_turn(creature_ptr);
1140                 return FALSE;
1141         }
1142
1143         if (have_pet)
1144         {
1145                 concptr msg = _("ペットを置いて広域マップに入りますか?",
1146                         "Do you leave your pets behind? ");
1147
1148                 if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
1149                 {
1150                         free_turn(creature_ptr);
1151                         return FALSE;
1152                 }
1153         }
1154
1155         take_turn(creature_ptr, 1000);
1156
1157         /* Remember the position */
1158         creature_ptr->oldpx = creature_ptr->x;
1159         creature_ptr->oldpy = creature_ptr->y;
1160
1161         /* Cancel hex spelling */
1162         if (hex_spelling_any(creature_ptr)) stop_hex_spell_all(creature_ptr);
1163
1164         /* Cancel any special action */
1165         set_action(creature_ptr, ACTION_NONE);
1166
1167         /* Go into the global map */
1168         creature_ptr->wild_mode = TRUE;
1169         creature_ptr->leaving = TRUE;
1170         return TRUE;
1171 }