OSDN Git Service

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