OSDN Git Service

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