OSDN Git Service

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