OSDN Git Service

447302edea670f64ec67f973c1c510c3a4332b49
[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                 /* 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 = (byte)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
688         /* Big town */
689         cur_hgt = MAX_HGT;
690         cur_wid = MAX_WID;
691
692         /* Assume illegal panel */
693         panel_row_min = cur_hgt;
694         panel_col_min = cur_wid;
695
696         /* Init the wilderness */
697
698         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
699
700         x = p_ptr->wilderness_x;
701         y = p_ptr->wilderness_y;
702
703         /* Prepare allocation table */
704         get_mon_num_prep(get_monster_hook(), NULL);
705
706         /* North border */
707         generate_area(y - 1, x, TRUE, FALSE);
708
709         for (i = 1; i < MAX_WID - 1; i++)
710         {
711                 border.north[i] = cave[MAX_HGT - 2][i].feat;
712         }
713
714         /* South border */
715         generate_area(y + 1, x, TRUE, FALSE);
716
717         for (i = 1; i < MAX_WID - 1; i++)
718         {
719                 border.south[i] = cave[1][i].feat;
720         }
721
722         /* West border */
723         generate_area(y, x - 1, TRUE, FALSE);
724
725         for (i = 1; i < MAX_HGT - 1; i++)
726         {
727                 border.west[i] = cave[i][MAX_WID - 2].feat;
728         }
729
730         /* East border */
731         generate_area(y, x + 1, TRUE, FALSE);
732
733         for (i = 1; i < MAX_HGT - 1; i++)
734         {
735                 border.east[i] = cave[i][1].feat;
736         }
737
738         /* North west corner */
739         generate_area(y - 1, x - 1, FALSE, TRUE);
740         border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
741
742         /* North east corner */
743         generate_area(y - 1, x + 1, FALSE, TRUE);
744         border.north_east = cave[MAX_HGT - 2][1].feat;
745
746         /* South west corner */
747         generate_area(y + 1, x - 1, FALSE, TRUE);
748         border.south_west = cave[1][MAX_WID - 2].feat;
749
750         /* South east corner */
751         generate_area(y + 1, x + 1, FALSE, TRUE);
752         border.south_east = cave[1][1].feat;
753
754
755         /* Create terrain of the current area */
756         generate_area(y, x, FALSE, FALSE);
757
758
759         /* Special boundary walls -- North */
760         for (i = 0; i < MAX_WID; i++)
761         {
762                 cave[0][i].feat = FEAT_PERM_SOLID;
763                 cave[0][i].mimic = border.north[i];
764         }
765
766         /* Special boundary walls -- South */
767         for (i = 0; i < MAX_WID; i++)
768         {
769                 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
770                 cave[MAX_HGT - 1][i].mimic = border.south[i];
771         }
772
773         /* Special boundary walls -- West */
774         for (i = 0; i < MAX_HGT; i++)
775         {
776                 cave[i][0].feat = FEAT_PERM_SOLID;
777                 cave[i][0].mimic = border.west[i];
778         }
779
780         /* Special boundary walls -- East */
781         for (i = 0; i < MAX_HGT; i++)
782         {
783                 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
784                 cave[i][MAX_WID - 1].mimic = border.east[i];
785         }
786
787         /* North west corner */
788         cave[0][0].mimic = border.north_west;
789
790         /* North east corner */
791         cave[0][MAX_WID - 1].mimic = border.north_east;
792
793         /* South west corner */
794         cave[MAX_HGT - 1][0].mimic = border.south_west;
795
796         /* South east corner */
797         cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
798
799         /* Light up or darken the area */
800         for (y = 0; y < cur_hgt; y++)
801         {
802                 for (x = 0; x < cur_wid; x++)
803                 {
804                         /* Get the cave grid */
805                         c_ptr = &cave[y][x];
806
807                         if (is_daytime())
808                         {
809                                 /* Assume lit */
810                                 c_ptr->info |= (CAVE_GLOW);
811
812                                 /* Hack -- Memorize lit grids if allowed */
813                                 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
814                         }
815                         else
816                         {
817                                 /* Feature code (applying "mimic" field) */
818                                 byte feat = c_ptr->mimic ? f_info[c_ptr->mimic].mimic : f_info[c_ptr->feat].mimic;
819
820                                 if (!is_mirror_grid(c_ptr) && (feat != FEAT_QUEST_ENTER) && (feat != FEAT_ENTRANCE))
821                                 {
822                                         /* Assume dark */
823                                         c_ptr->info &= ~(CAVE_GLOW);
824
825                                         /* Darken "boring" features */
826                                         if ((feat <= FEAT_INVIS) ||
827                                            ((feat >= FEAT_DEEP_WATER) &&
828                                             (feat <= FEAT_MOUNTAIN) &&
829                                             (feat != FEAT_MUSEUM)))
830                                         {
831                                                 /* Forget the grid */
832                                                 c_ptr->info &= ~(CAVE_MARK);
833                                         }
834                                 }
835                                 else if (feat == FEAT_ENTRANCE)
836                                 {
837                                         /* Assume lit */
838                                         c_ptr->info |= (CAVE_GLOW);
839
840                                         /* Hack -- Memorize lit grids if allowed */
841                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
842                                 }
843                         }
844                 }
845         }
846
847         if (p_ptr->teleport_town)
848         {
849                 for (y = 0; y < cur_hgt; y++)
850                 {
851                         for (x = 0; x < cur_wid; x++)
852                         {
853                                 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
854                                 {
855                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
856                                         p_ptr->oldpy = y;
857                                         p_ptr->oldpx = x;
858                                 }
859                         }
860                 }
861                 p_ptr->teleport_town = FALSE;
862         }
863
864         else if (p_ptr->leaving_dungeon)
865         {
866                 for (y = 0; y < cur_hgt; y++)
867                 {
868                         for (x = 0; x < cur_wid; x++)
869                         {
870                                 if (cave[y][x].feat == FEAT_ENTRANCE)
871                                 {
872                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
873                                         p_ptr->oldpy = y;
874                                         p_ptr->oldpx = x;
875                                 }
876                         }
877                 }
878                 p_ptr->teleport_town = FALSE;
879         }
880
881         player_place(p_ptr->oldpy, p_ptr->oldpx);
882         p_ptr->leftbldg = FALSE;
883         /* p_ptr->leaving_dungeon = FALSE;*/
884
885         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
886
887         /* Make some residents */
888         for (i = 0; i < lim; i++)
889         {
890                 u32b mode = 0;
891
892                 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
893                         mode |= PM_ALLOW_SLEEP;
894
895                 /* Make a resident */
896                 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
897         }
898
899         if(generate_encounter) ambush_flag = TRUE;
900         generate_encounter = FALSE;
901
902         /* Fill the arrays of floors and walls in the good proportions */
903         set_floor_and_wall(0);
904
905         /* Set rewarded quests to finished */
906         for (i = 0; i < max_quests; i++)
907         {
908                 if (quest[i].status == QUEST_STATUS_REWARDED)
909                         quest[i].status = QUEST_STATUS_FINISHED;
910         }
911 }
912
913
914 /*
915  * Build the wilderness area.
916  * -DG-
917  */
918 void wilderness_gen_small()
919 {
920         int i, j;
921
922         /* To prevent stupid things */
923         for (i = 0; i < MAX_WID; i++)
924         for (j = 0; j < MAX_HGT; j++)
925         {
926                 cave[j][i].feat = FEAT_PERM_SOLID;
927         }
928
929         /* Init the wilderness */
930         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
931
932         /* Fill the map */
933         for (i = 0; i < max_wild_x; i++)
934         for (j = 0; j < max_wild_y; j++)
935         {
936                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
937                 {
938                         cave[j][i].feat = FEAT_TOWN;
939                         cave[j][i].special = wilderness[j][i].town;
940                 }
941                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
942                 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
943                 {
944                         cave[j][i].feat = FEAT_ENTRANCE;
945                         cave[j][i].special = (byte)wilderness[j][i].entrance;
946                 }
947                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
948
949                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
950         }
951
952         cur_hgt = (s16b) max_wild_y;
953         cur_wid = (s16b) max_wild_x;
954
955         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
956         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
957
958         /* Assume illegal panel */
959         panel_row_min = cur_hgt;
960         panel_col_min = cur_wid;
961
962         /* Place the player */
963         px = p_ptr->wilderness_x;
964         py = p_ptr->wilderness_y;
965
966         p_ptr->town_num = 0;
967 }
968
969
970 typedef struct wilderness_grid wilderness_grid;
971
972 struct wilderness_grid
973 {
974         int             terrain;    /* Terrain type */
975         int             town;       /* Town number */
976         s16b    level;          /* Level of the wilderness */
977         byte    road;       /* Road */
978         char    name[32];       /* Name of the town/wilderness */
979 };
980
981
982 static wilderness_grid w_letter[255];
983
984
985 /*
986  * Parse a sub-file of the "extra info"
987  */
988 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
989 {
990         int i, num;
991         char *zz[33];
992
993         /* Unused */
994         (void)ymin;
995         (void)ymax;
996
997         /* Paranoia */
998         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
999
1000         switch (buf[2])
1001         {
1002                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1003 #ifdef JP
1004         case 'E':
1005                 return 0;
1006         case 'F':
1007         case 'J':
1008 #else
1009         case 'J':
1010                 return 0;
1011         case 'F':
1012         case 'E':
1013 #endif
1014         {
1015                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1016                 {
1017                         int index = zz[0][0];
1018                         
1019                         if (num > 1)
1020                                 w_letter[index].terrain = atoi(zz[1]);
1021                         else
1022                                 w_letter[index].terrain = 0;
1023                         
1024                         if (num > 2)
1025                                 w_letter[index].level = atoi(zz[2]);
1026                         else
1027                                 w_letter[index].level = 0;
1028                         
1029                         if (num > 3)
1030                                 w_letter[index].town = atoi(zz[3]);
1031                         else
1032                                 w_letter[index].town = 0;
1033                         
1034                         if (num > 4)
1035                                 w_letter[index].road = atoi(zz[4]);
1036                         else
1037                                 w_letter[index].road = 0;
1038                         
1039                         if (num > 5)
1040                                 strcpy(w_letter[index].name, zz[5]);
1041                         else
1042                                 w_letter[index].name[0] = 0;
1043                 }
1044                 else
1045                 {
1046                                 /* Failure */
1047                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1048                 }
1049                 
1050                 break;
1051         }
1052         
1053         /* Process "W:D:<layout> */
1054         /* Layout of the wilderness */
1055         case 'D':
1056         {
1057                 /* Acquire the text */
1058                 char *s = buf+4;
1059                 
1060                 /* Length of the text */
1061                 int len = strlen(s);
1062                 
1063                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1064                 {
1065                         int idx = s[0];
1066                         
1067                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1068                         
1069                         wilderness[*y][*x].level = w_letter[idx].level;
1070                         
1071                         wilderness[*y][*x].town = w_letter[idx].town;
1072                         
1073                         wilderness[*y][*x].road = w_letter[idx].road;
1074                         
1075                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1076                 }
1077                 
1078                 (*y)++;
1079                 
1080                 break;
1081         }
1082         
1083         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1084         case 'P':
1085         {
1086                 if ((p_ptr->wilderness_x == 0) &&
1087                     (p_ptr->wilderness_y == 0))
1088                 {
1089                         if (tokenize(buf+4, 2, zz, 0) == 2)
1090                         {
1091                                 p_ptr->wilderness_y = atoi(zz[0]);
1092                                 p_ptr->wilderness_x = atoi(zz[1]);
1093                                 
1094                                 if ((p_ptr->wilderness_x < 1) ||
1095                                     (p_ptr->wilderness_x > max_wild_x) ||
1096                                     (p_ptr->wilderness_y < 1) ||
1097                                     (p_ptr->wilderness_y > max_wild_y))
1098                                 {
1099                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1100                                 }
1101                         }
1102                         else
1103                         {
1104                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1105                         }
1106                 }
1107                 
1108                 break;
1109         }
1110         
1111         default:
1112                 /* Failure */
1113                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1114         }
1115         
1116         for (i = 1; i < max_d_idx; i++)
1117         {
1118                 if (!d_info[i].maxdepth) continue;
1119                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1120                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1121                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1122         }
1123
1124         /* Success */
1125         return (0);
1126 }
1127
1128
1129 /*
1130  * Generate the random seeds for the wilderness
1131  */
1132 void seed_wilderness(void)
1133 {
1134         int x, y;
1135
1136         /* Init wilderness seeds */
1137         for (x = 0; x < max_wild_x; x++)
1138         {
1139                 for (y = 0; y < max_wild_y; y++)
1140                 {
1141                         wilderness[y][x].seed = randint0(0x10000000);
1142                         wilderness[y][x].entrance = 0;
1143                 }
1144         }
1145 }
1146
1147
1148 /*
1149  * Pointer to wilderness_type
1150  */
1151 typedef wilderness_type *wilderness_type_ptr;
1152
1153 /*
1154  * Initialize wilderness array
1155  */
1156 errr init_wilderness(void)
1157 {
1158         int i;
1159
1160         /* Allocate the wilderness (two-dimension array) */
1161         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1162         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1163
1164         /* Init the other pointers */
1165         for (i = 1; i < max_wild_y; i++)
1166                 wilderness[i] = wilderness[0] + i * max_wild_x;
1167
1168         generate_encounter = FALSE;
1169
1170         return 0;
1171 }
1172
1173
1174 bool change_wild_mode(void)
1175 {
1176         int i;
1177         bool have_pet = FALSE;
1178
1179         if (lite_town || vanilla_town)
1180         {
1181 #ifdef JP
1182                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1183 #else
1184                 msg_print("No global mal");
1185 #endif
1186                 return FALSE;
1187         }
1188         if (!p_ptr->wild_mode)
1189         {
1190                 for (i = 1; i < m_max; i++)
1191                 {
1192                         monster_type *m_ptr = &m_list[i];
1193
1194                         if (!m_ptr->r_idx) continue;
1195                         if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1196                         if (m_ptr->csleep) continue;
1197                         if (m_ptr->cdis > MAX_SIGHT) continue;
1198                         if (!is_hostile(m_ptr)) continue;
1199 #ifdef JP
1200                         msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1201 #else
1202                         msg_print("You cannot enter global map, since there is some monsters nearby!");
1203 #endif
1204                         energy_use = 0;
1205                         return FALSE;
1206                 }
1207
1208                 if (have_pet)
1209                 {
1210 #ifdef JP
1211                         if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹­°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1212 #else
1213                         if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1214 #endif
1215                         {
1216                                 energy_use = 0;
1217                                 return FALSE;
1218                         }
1219                 }
1220
1221                 energy_use = 1000;
1222         }
1223
1224         set_action(ACTION_NONE);
1225
1226         p_ptr->wild_mode = !p_ptr->wild_mode;
1227
1228         /* Leaving */
1229         p_ptr->leaving = TRUE;
1230
1231         return TRUE;
1232 }