OSDN Git Service

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