OSDN Git Service

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