OSDN Git Service

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