OSDN Git Service

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