OSDN Git Service

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