OSDN Git Service

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