OSDN Git Service

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