OSDN Git Service

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