OSDN Git Service

branch-nothere-terrainflags、゙。シ・ク.
[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 /*
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 = randint0(table_size);
522         cave[MAX_HGT-2][1].feat = randint0(table_size);
523         cave[1][MAX_WID-2].feat = randint0(table_size);
524         cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
525
526         if (!corner)
527         {
528                 /* x1, y1, x2, y2, num_depths, roughness */
529                 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
530         }
531
532         /* Use the complex RNG */
533         Rand_quick = FALSE;
534
535         for (y1 = 1; y1 < MAX_HGT-1; y1++)
536         {
537                 for (x1 = 1; x1 < MAX_WID-1; x1++)
538                 {
539                         cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
540                 }
541         }
542 }
543
544
545
546 /*
547  * Load a town or generate a terrain level using "plasma" fractals.
548  *
549  * x and y are the coordinates of the area in the wilderness.
550  * Border and corner are optimization flags to speed up the
551  * generation of the fractal terrain.
552  * If border is set then only the border of the terrain should
553  * be generated (for initializing the border structure).
554  * If corner is set then only the corners of the area are needed.
555  */
556 static void generate_area(int y, int x, bool border, bool corner)
557 {
558         int x1, y1;
559
560         /* Number of the town (if any) */
561         p_ptr->town_num = wilderness[y][x].town;
562
563         /* Set the base level */
564         base_level = wilderness[y][x].level;
565
566         /* Set the dungeon level */
567         dun_level = 0;
568
569         /* Set the monster generation level */
570         monster_level = base_level;
571
572         /* Set the object generation level */
573         object_level = base_level;
574
575
576         /* Create the town */
577         if (p_ptr->town_num)
578         {
579                 /* Reset the buildings */
580                 init_buildings();
581
582                 /* Initialize the town */
583                 if (border | corner)
584                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
585                 else
586                         init_flags = INIT_CREATE_DUNGEON;
587
588                 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
589
590                 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
591         }
592         else
593         {
594                 int terrain = wilderness[y][x].terrain;
595                 u32b seed = wilderness[y][x].seed;
596
597                 generate_wilderness_area(terrain, seed, border, corner);
598         }
599
600         if (!corner && !wilderness[y][x].town)
601         {
602                 /*
603                  * Place roads in the wilderness
604                  * ToDo: make the road a bit more interresting
605                  */
606                 if (wilderness[y][x].road)
607                 {
608                         cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
609
610                         if (wilderness[y-1][x].road)
611                         {
612                                 /* North road */
613                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
614                                 {
615                                         x1 = MAX_WID/2;
616                                         cave[y1][x1].feat = FEAT_FLOOR;
617                                 }
618                         }
619
620                         if (wilderness[y+1][x].road)
621                         {
622                                 /* North road */
623                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
624                                 {
625                                         x1 = MAX_WID/2;
626                                         cave[y1][x1].feat = FEAT_FLOOR;
627                                 }
628                         }
629
630                         if (wilderness[y][x+1].road)
631                         {
632                                 /* East road */
633                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
634                                 {
635                                         y1 = MAX_HGT/2;
636                                         cave[y1][x1].feat = FEAT_FLOOR;
637                                 }
638                         }
639
640                         if (wilderness[y][x-1].road)
641                         {
642                                 /* West road */
643                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
644                                 {
645                                         y1 = MAX_HGT/2;
646                                         cave[y1][x1].feat = FEAT_FLOOR;
647                                 }
648                         }
649                 }
650         }
651
652         if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
653         {
654                 int dy, dx;
655
656                 /* Hack -- Use the "simple" RNG */
657                 Rand_quick = TRUE;
658
659                 /* Hack -- Induce consistant town layout */
660                 Rand_value = wilderness[y][x].seed;
661
662                 dy = rand_range(6, cur_hgt - 6);
663                 dx = rand_range(6, cur_wid - 6);
664
665                 cave[dy][dx].feat = FEAT_ENTRANCE;
666                 cave[dy][dx].special = wilderness[y][x].entrance;
667
668                 /* Use the complex RNG */
669                 Rand_quick = FALSE;
670         }
671 }
672
673
674 /*
675  * Border of the wilderness area
676  */
677 static border_type border;
678
679
680 /*
681  * Build the wilderness area outside of the town.
682  */
683 void wilderness_gen(void)
684 {
685         int i, y, x, lim;
686         cave_type *c_ptr;
687         feature_type *f_ptr;
688
689         /* Big town */
690         cur_hgt = MAX_HGT;
691         cur_wid = MAX_WID;
692
693         /* Assume illegal panel */
694         panel_row_min = cur_hgt;
695         panel_col_min = cur_wid;
696
697         /* Init the wilderness */
698
699         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
700
701         x = p_ptr->wilderness_x;
702         y = p_ptr->wilderness_y;
703
704         /* Prepare allocation table */
705         get_mon_num_prep(get_monster_hook(), NULL);
706
707         /* North border */
708         generate_area(y - 1, x, TRUE, FALSE);
709
710         for (i = 1; i < MAX_WID - 1; i++)
711         {
712                 border.north[i] = cave[MAX_HGT - 2][i].feat;
713         }
714
715         /* South border */
716         generate_area(y + 1, x, TRUE, FALSE);
717
718         for (i = 1; i < MAX_WID - 1; i++)
719         {
720                 border.south[i] = cave[1][i].feat;
721         }
722
723         /* West border */
724         generate_area(y, x - 1, TRUE, FALSE);
725
726         for (i = 1; i < MAX_HGT - 1; i++)
727         {
728                 border.west[i] = cave[i][MAX_WID - 2].feat;
729         }
730
731         /* East border */
732         generate_area(y, x + 1, TRUE, FALSE);
733
734         for (i = 1; i < MAX_HGT - 1; i++)
735         {
736                 border.east[i] = cave[i][1].feat;
737         }
738
739         /* North west corner */
740         generate_area(y - 1, x - 1, FALSE, TRUE);
741         border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
742
743         /* North east corner */
744         generate_area(y - 1, x + 1, FALSE, TRUE);
745         border.north_east = cave[MAX_HGT - 2][1].feat;
746
747         /* South west corner */
748         generate_area(y + 1, x - 1, FALSE, TRUE);
749         border.south_west = cave[1][MAX_WID - 2].feat;
750
751         /* South east corner */
752         generate_area(y + 1, x + 1, FALSE, TRUE);
753         border.south_east = cave[1][1].feat;
754
755
756         /* Create terrain of the current area */
757         generate_area(y, x, FALSE, FALSE);
758
759
760         /* Special boundary walls -- North */
761         for (i = 0; i < MAX_WID; i++)
762         {
763                 cave[0][i].feat = FEAT_PERM_SOLID;
764                 cave[0][i].mimic = border.north[i];
765         }
766
767         /* Special boundary walls -- South */
768         for (i = 0; i < MAX_WID; i++)
769         {
770                 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
771                 cave[MAX_HGT - 1][i].mimic = border.south[i];
772         }
773
774         /* Special boundary walls -- West */
775         for (i = 0; i < MAX_HGT; i++)
776         {
777                 cave[i][0].feat = FEAT_PERM_SOLID;
778                 cave[i][0].mimic = border.west[i];
779         }
780
781         /* Special boundary walls -- East */
782         for (i = 0; i < MAX_HGT; i++)
783         {
784                 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
785                 cave[i][MAX_WID - 1].mimic = border.east[i];
786         }
787
788         /* North west corner */
789         cave[0][0].mimic = border.north_west;
790
791         /* North east corner */
792         cave[0][MAX_WID - 1].mimic = border.north_east;
793
794         /* South west corner */
795         cave[MAX_HGT - 1][0].mimic = border.south_west;
796
797         /* South east corner */
798         cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
799
800         /* Light up or darken the area */
801         for (y = 0; y < cur_hgt; y++)
802         {
803                 for (x = 0; x < cur_wid; x++)
804                 {
805                         /* Get the cave grid */
806                         c_ptr = &cave[y][x];
807
808                         if (is_daytime())
809                         {
810                                 /* Assume lit */
811                                 c_ptr->info |= (CAVE_GLOW);
812
813                                 /* Hack -- Memorize lit grids if allowed */
814                                 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
815                         }
816                         else
817                         {
818                                 /* Feature code (applying "mimic" field) */
819                                 f_ptr = &f_info[get_feat_mimic(c_ptr)];
820
821                                 if (!is_mirror_grid(c_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
822                                     !have_flag(f_ptr->flags, FF_ENTRANCE))
823                                 {
824                                         /* Assume dark */
825                                         c_ptr->info &= ~(CAVE_GLOW);
826
827                                         /* Darken "boring" features */
828                                         if (!have_flag(f_ptr->flags, FF_REMEMBER))
829                                         {
830                                                 /* Forget the grid */
831                                                 c_ptr->info &= ~(CAVE_MARK);
832                                         }
833                                 }
834                                 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
835                                 {
836                                         /* Assume lit */
837                                         c_ptr->info |= (CAVE_GLOW);
838
839                                         /* Hack -- Memorize lit grids if allowed */
840                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
841                                 }
842                         }
843                 }
844         }
845
846         if (p_ptr->teleport_town)
847         {
848                 for (y = 0; y < cur_hgt; y++)
849                 {
850                         for (x = 0; x < cur_wid; x++)
851                         {
852                                 /* Get the cave grid */
853                                 c_ptr = &cave[y][x];
854
855                                 /* Seeing true feature code (ignore mimic) */
856                                 f_ptr = &f_info[c_ptr->feat];
857
858                                 if (have_flag(f_ptr->flags, FF_BLDG))
859                                 {
860                                         if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
861                                         {
862                                                 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
863                                                 p_ptr->oldpy = y;
864                                                 p_ptr->oldpx = x;
865                                         }
866                                 }
867                         }
868                 }
869                 p_ptr->teleport_town = FALSE;
870         }
871
872         else if (p_ptr->leaving_dungeon)
873         {
874                 for (y = 0; y < cur_hgt; y++)
875                 {
876                         for (x = 0; x < cur_wid; x++)
877                         {
878                                 /* Get the cave grid */
879                                 c_ptr = &cave[y][x];
880
881                                 if (have_flag(f_flags_grid(c_ptr), FF_ENTRANCE))
882                                 {
883                                         if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
884                                         p_ptr->oldpy = y;
885                                         p_ptr->oldpx = x;
886                                 }
887                         }
888                 }
889                 p_ptr->teleport_town = FALSE;
890         }
891
892         player_place(p_ptr->oldpy, p_ptr->oldpx);
893         /* p_ptr->leaving_dungeon = FALSE;*/
894
895         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
896
897         /* Make some residents */
898         for (i = 0; i < lim; i++)
899         {
900                 u32b mode = 0;
901
902                 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
903                         mode |= PM_ALLOW_SLEEP;
904
905                 /* Make a resident */
906                 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
907         }
908
909         if(generate_encounter) ambush_flag = TRUE;
910         generate_encounter = FALSE;
911
912         /* Fill the arrays of floors and walls in the good proportions */
913         set_floor_and_wall(0);
914
915         /* Set rewarded quests to finished */
916         for (i = 0; i < max_quests; i++)
917         {
918                 if (quest[i].status == QUEST_STATUS_REWARDED)
919                         quest[i].status = QUEST_STATUS_FINISHED;
920         }
921 }
922
923
924 /*
925  * Build the wilderness area.
926  * -DG-
927  */
928 void wilderness_gen_small()
929 {
930         int i, j;
931
932         /* To prevent stupid things */
933         for (i = 0; i < MAX_WID; i++)
934         for (j = 0; j < MAX_HGT; j++)
935         {
936                 cave[j][i].feat = FEAT_PERM_SOLID;
937         }
938
939         /* Init the wilderness */
940         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
941
942         /* Fill the map */
943         for (i = 0; i < max_wild_x; i++)
944         for (j = 0; j < max_wild_y; j++)
945         {
946                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
947                 {
948                         cave[j][i].feat = FEAT_TOWN;
949                         cave[j][i].special = wilderness[j][i].town;
950                 }
951                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
952                 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
953                 {
954                         cave[j][i].feat = FEAT_ENTRANCE;
955                         cave[j][i].special = (byte)wilderness[j][i].entrance;
956                 }
957                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
958
959                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
960         }
961
962         cur_hgt = (s16b) max_wild_y;
963         cur_wid = (s16b) max_wild_x;
964
965         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
966         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
967
968         /* Assume illegal panel */
969         panel_row_min = cur_hgt;
970         panel_col_min = cur_wid;
971
972         /* Place the player */
973         px = p_ptr->wilderness_x;
974         py = p_ptr->wilderness_y;
975
976         p_ptr->town_num = 0;
977 }
978
979
980 typedef struct wilderness_grid wilderness_grid;
981
982 struct wilderness_grid
983 {
984         int             terrain;    /* Terrain type */
985         int             town;       /* Town number */
986         s16b    level;          /* Level of the wilderness */
987         byte    road;       /* Road */
988         char    name[32];       /* Name of the town/wilderness */
989 };
990
991
992 static wilderness_grid w_letter[255];
993
994
995 /*
996  * Parse a sub-file of the "extra info"
997  */
998 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
999 {
1000         int i, num;
1001         char *zz[33];
1002
1003         /* Unused */
1004         (void)ymin;
1005         (void)ymax;
1006
1007         /* Paranoia */
1008         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1009
1010         switch (buf[2])
1011         {
1012                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1013 #ifdef JP
1014         case 'E':
1015                 return 0;
1016         case 'F':
1017         case 'J':
1018 #else
1019         case 'J':
1020                 return 0;
1021         case 'F':
1022         case 'E':
1023 #endif
1024         {
1025                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1026                 {
1027                         int index = zz[0][0];
1028                         
1029                         if (num > 1)
1030                                 w_letter[index].terrain = atoi(zz[1]);
1031                         else
1032                                 w_letter[index].terrain = 0;
1033                         
1034                         if (num > 2)
1035                                 w_letter[index].level = atoi(zz[2]);
1036                         else
1037                                 w_letter[index].level = 0;
1038                         
1039                         if (num > 3)
1040                                 w_letter[index].town = atoi(zz[3]);
1041                         else
1042                                 w_letter[index].town = 0;
1043                         
1044                         if (num > 4)
1045                                 w_letter[index].road = atoi(zz[4]);
1046                         else
1047                                 w_letter[index].road = 0;
1048                         
1049                         if (num > 5)
1050                                 strcpy(w_letter[index].name, zz[5]);
1051                         else
1052                                 w_letter[index].name[0] = 0;
1053                 }
1054                 else
1055                 {
1056                                 /* Failure */
1057                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1058                 }
1059                 
1060                 break;
1061         }
1062         
1063         /* Process "W:D:<layout> */
1064         /* Layout of the wilderness */
1065         case 'D':
1066         {
1067                 /* Acquire the text */
1068                 char *s = buf+4;
1069                 
1070                 /* Length of the text */
1071                 int len = strlen(s);
1072                 
1073                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1074                 {
1075                         int idx = s[0];
1076                         
1077                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1078                         
1079                         wilderness[*y][*x].level = w_letter[idx].level;
1080                         
1081                         wilderness[*y][*x].town = w_letter[idx].town;
1082                         
1083                         wilderness[*y][*x].road = w_letter[idx].road;
1084                         
1085                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1086                 }
1087                 
1088                 (*y)++;
1089                 
1090                 break;
1091         }
1092         
1093         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1094         case 'P':
1095         {
1096                 if ((p_ptr->wilderness_x == 0) &&
1097                     (p_ptr->wilderness_y == 0))
1098                 {
1099                         if (tokenize(buf+4, 2, zz, 0) == 2)
1100                         {
1101                                 p_ptr->wilderness_y = atoi(zz[0]);
1102                                 p_ptr->wilderness_x = atoi(zz[1]);
1103                                 
1104                                 if ((p_ptr->wilderness_x < 1) ||
1105                                     (p_ptr->wilderness_x > max_wild_x) ||
1106                                     (p_ptr->wilderness_y < 1) ||
1107                                     (p_ptr->wilderness_y > max_wild_y))
1108                                 {
1109                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1110                                 }
1111                         }
1112                         else
1113                         {
1114                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1115                         }
1116                 }
1117                 
1118                 break;
1119         }
1120         
1121         default:
1122                 /* Failure */
1123                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1124         }
1125         
1126         for (i = 1; i < max_d_idx; i++)
1127         {
1128                 if (!d_info[i].maxdepth) continue;
1129                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1130                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1131                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1132         }
1133
1134         /* Success */
1135         return (0);
1136 }
1137
1138
1139 /*
1140  * Generate the random seeds for the wilderness
1141  */
1142 void seed_wilderness(void)
1143 {
1144         int x, y;
1145
1146         /* Init wilderness seeds */
1147         for (x = 0; x < max_wild_x; x++)
1148         {
1149                 for (y = 0; y < max_wild_y; y++)
1150                 {
1151                         wilderness[y][x].seed = randint0(0x10000000);
1152                         wilderness[y][x].entrance = 0;
1153                 }
1154         }
1155 }
1156
1157
1158 /*
1159  * Pointer to wilderness_type
1160  */
1161 typedef wilderness_type *wilderness_type_ptr;
1162
1163 /*
1164  * Initialize wilderness array
1165  */
1166 errr init_wilderness(void)
1167 {
1168         int i;
1169
1170         /* Allocate the wilderness (two-dimension array) */
1171         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1172         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1173
1174         /* Init the other pointers */
1175         for (i = 1; i < max_wild_y; i++)
1176                 wilderness[i] = wilderness[0] + i * max_wild_x;
1177
1178         generate_encounter = FALSE;
1179
1180         return 0;
1181 }
1182
1183
1184 bool change_wild_mode(void)
1185 {
1186         int i;
1187         bool have_pet = FALSE;
1188
1189         if (lite_town || vanilla_town)
1190         {
1191 #ifdef JP
1192                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1193 #else
1194                 msg_print("No global map.");
1195 #endif
1196                 return FALSE;
1197         }
1198         if (!p_ptr->wild_mode)
1199         {
1200                 for (i = 1; i < m_max; i++)
1201                 {
1202                         monster_type *m_ptr = &m_list[i];
1203
1204                         if (!m_ptr->r_idx) continue;
1205                         if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1206                         if (m_ptr->csleep) continue;
1207                         if (m_ptr->cdis > MAX_SIGHT) continue;
1208                         if (!is_hostile(m_ptr)) continue;
1209 #ifdef JP
1210                         msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1211 #else
1212                         msg_print("You cannot enter global map, since there is some monsters nearby!");
1213 #endif
1214                         energy_use = 0;
1215                         return FALSE;
1216                 }
1217
1218                 if (have_pet)
1219                 {
1220 #ifdef JP
1221                         if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹­°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1222 #else
1223                         if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1224 #endif
1225                         {
1226                                 energy_use = 0;
1227                                 return FALSE;
1228                         }
1229                 }
1230
1231                 energy_use = 1000;
1232         }
1233
1234         set_action(ACTION_NONE);
1235
1236         p_ptr->wild_mode = !p_ptr->wild_mode;
1237
1238         /* Leaving */
1239         p_ptr->leaving = TRUE;
1240
1241         return TRUE;
1242 }