OSDN Git Service

未使用変数による警告を除去。
[hengband/hengband.git] / src / wild.c
1 /* File: wild.c */
2
3 /* Purpose: Wilderness generation */
4
5 /*
6  * Copyright (c) 1989, 1999 James E. Wilson, Robert A. Koeneke,
7  * Robert Ruehlmann
8  *
9  * This software may be copied and distributed for educational, research, and
10  * not for profit purposes provided that this copyright and statement are
11  * included in all such copies.
12  */
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         /* The outer wall is easy */
479         if (terrain == TERRAIN_EDGE)
480         {
481                 /* Create level background */
482                 for (y1 = 0; y1 < MAX_HGT; y1++)
483                 {
484                         for (x1 = 0; x1 < MAX_WID; x1++)
485                         {
486                                 cave[y1][x1].feat = FEAT_PERM_SOLID;
487                         }
488                 }
489
490                 /* We are done already */
491                 return;
492         }
493
494
495         /* Hack -- Use the "simple" RNG */
496         Rand_quick = TRUE;
497
498         /* Hack -- Induce consistant town layout */
499         Rand_value = seed;
500
501         if (!corner)
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         /*
514          * Initialize the four corners
515          * ToDo: calculate the medium height of the adjacent
516          * terrains for every corner.
517          */
518         cave[1][1].feat = (byte)randint0(table_size);
519         cave[MAX_HGT-2][1].feat = (byte)randint0(table_size);
520         cave[1][MAX_WID-2].feat = (byte)randint0(table_size);
521         cave[MAX_HGT-2][MAX_WID-2].feat = (byte)randint0(table_size);
522
523         if (!corner)
524         {
525                 /* x1, y1, x2, y2, num_depths, roughness */
526                 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
527         }
528
529         /* Use the complex RNG */
530         Rand_quick = FALSE;
531
532         for (y1 = 1; y1 < MAX_HGT-1; y1++)
533         {
534                 for (x1 = 1; x1 < MAX_WID-1; x1++)
535                 {
536                         cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
537                 }
538         }
539 }
540
541
542
543 /*
544  * Load a town or generate a terrain level using "plasma" fractals.
545  *
546  * x and y are the coordinates of the area in the wilderness.
547  * Border and corner are optimization flags to speed up the
548  * generation of the fractal terrain.
549  * If border is set then only the border of the terrain should
550  * be generated (for initializing the border structure).
551  * If corner is set then only the corners of the area are needed.
552  */
553 static void generate_area(int y, int x, bool border, bool corner)
554 {
555         int x1, y1;
556
557         /* Number of the town (if any) */
558         p_ptr->town_num = wilderness[y][x].town;
559
560         /* Set the base level */
561         base_level = wilderness[y][x].level;
562
563         /* Set the dungeon level */
564         dun_level = 0;
565
566         /* Set the monster generation level */
567         monster_level = base_level;
568
569         /* Set the object generation level */
570         object_level = base_level;
571
572
573         /* Create the town */
574         if (p_ptr->town_num)
575         {
576                 /* Reset the buildings */
577                 init_buildings();
578
579                 /* Initialize the town */
580                 if (border | corner)
581                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
582                 else
583                         init_flags = INIT_CREATE_DUNGEON;
584
585                 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
586
587                 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
588         }
589         else
590         {
591                 int terrain = wilderness[y][x].terrain;
592                 u32b seed = wilderness[y][x].seed;
593
594                 generate_wilderness_area(terrain, seed, border, corner);
595         }
596
597         if (!corner && !wilderness[y][x].town)
598         {
599                 /*
600                  * Place roads in the wilderness
601                  * ToDo: make the road a bit more interresting
602                  */
603                 if (wilderness[y][x].road)
604                 {
605                         cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
606
607                         if (wilderness[y-1][x].road)
608                         {
609                                 /* North road */
610                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
611                                 {
612                                         x1 = MAX_WID/2;
613                                         cave[y1][x1].feat = FEAT_FLOOR;
614                                 }
615                         }
616
617                         if (wilderness[y+1][x].road)
618                         {
619                                 /* North road */
620                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
621                                 {
622                                         x1 = MAX_WID/2;
623                                         cave[y1][x1].feat = FEAT_FLOOR;
624                                 }
625                         }
626
627                         if (wilderness[y][x+1].road)
628                         {
629                                 /* East road */
630                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
631                                 {
632                                         y1 = MAX_HGT/2;
633                                         cave[y1][x1].feat = FEAT_FLOOR;
634                                 }
635                         }
636
637                         if (wilderness[y][x-1].road)
638                         {
639                                 /* West road */
640                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
641                                 {
642                                         y1 = MAX_HGT/2;
643                                         cave[y1][x1].feat = FEAT_FLOOR;
644                                 }
645                         }
646                 }
647         }
648
649         if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
650         {
651                 int dy, dx;
652
653                 /* Hack -- Use the "simple" RNG */
654                 Rand_quick = TRUE;
655
656                 /* Hack -- Induce consistant town layout */
657                 Rand_value = wilderness[y][x].seed;
658
659                 dy = rand_range(6, cur_hgt - 6);
660                 dx = rand_range(6, cur_wid - 6);
661
662                 cave[dy][dx].feat = FEAT_ENTRANCE;
663                 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
664
665                 /* Use the complex RNG */
666                 Rand_quick = FALSE;
667         }
668 }
669
670
671 /*
672  * Border of the wilderness area
673  */
674 static border_type border;
675
676
677 /*
678  * Build the wilderness area outside of the town.
679  */
680 void wilderness_gen(void)
681 {
682         int i, y, x, lim;
683         cave_type *c_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_SOLID;
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_SOLID;
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_SOLID;
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_SOLID;
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                                 /* Darken "boring" features */
815                                 if ((c_ptr->feat <= FEAT_INVIS) ||
816                                     ((c_ptr->feat >= FEAT_DEEP_WATER) &&
817                                         (c_ptr->feat <= FEAT_MOUNTAIN) &&
818                                      (c_ptr->feat != FEAT_MUSEUM)) ||
819                                     (x == 0) || (x == cur_wid-1) ||
820                                     (y == 0) || (y == cur_hgt-1))
821                                 {
822                                         /* Forget the grid */
823                                         c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
824                                 }
825                                 else if (c_ptr->feat == FEAT_ENTRANCE)
826                                 {
827                                         /* Assume lit */
828                                         c_ptr->info |= (CAVE_GLOW);
829
830                                         /* Hack -- Memorize lit grids if allowed */
831                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
832                                 }
833                         }
834                 }
835         }
836
837         if (p_ptr->teleport_town)
838         {
839                 for (y = 0; y < cur_hgt; y++)
840                 {
841                         for (x = 0; x < cur_wid; x++)
842                         {
843                                 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
844                                 {
845                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
846                                         p_ptr->oldpy = y;
847                                         p_ptr->oldpx = x;
848                                 }
849                         }
850                 }
851                 p_ptr->teleport_town = FALSE;
852         }
853
854         else if (p_ptr->leaving_dungeon)
855         {
856                 for (y = 0; y < cur_hgt; y++)
857                 {
858                         for (x = 0; x < cur_wid; x++)
859                         {
860                                 if (cave[y][x].feat == FEAT_ENTRANCE)
861                                 {
862                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
863                                         p_ptr->oldpy = y;
864                                         p_ptr->oldpx = x;
865                                 }
866                         }
867                 }
868                 p_ptr->teleport_town = FALSE;
869         }
870
871         player_place(p_ptr->oldpy, p_ptr->oldpx);
872         p_ptr->leftbldg = FALSE;
873         /* p_ptr->leaving_dungeon = FALSE;*/
874
875         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
876
877         /* Make some residents */
878         for (i = 0; i < lim; i++)
879         {
880                 u32b mode = 0;
881
882                 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
883                         mode |= PM_ALLOW_SLEEP;
884
885                 /* Make a resident */
886                 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
887         }
888
889         if(generate_encounter) ambush_flag = TRUE;
890         generate_encounter = FALSE;
891
892         /* Fill the arrays of floors and walls in the good proportions */
893         set_floor_and_wall(0);
894
895         /* Set rewarded quests to finished */
896         for (i = 0; i < max_quests; i++)
897         {
898                 if (quest[i].status == QUEST_STATUS_REWARDED)
899                         quest[i].status = QUEST_STATUS_FINISHED;
900         }
901 }
902
903
904 /*
905  * Build the wilderness area.
906  * -DG-
907  */
908 void wilderness_gen_small()
909 {
910         int i, j;
911
912         /* To prevent stupid things */
913         for (i = 0; i < MAX_WID; i++)
914         for (j = 0; j < MAX_HGT; j++)
915         {
916                 cave[j][i].feat = FEAT_PERM_SOLID;
917         }
918
919         /* Init the wilderness */
920         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
921
922         /* Fill the map */
923         for (i = 0; i < max_wild_x; i++)
924         for (j = 0; j < max_wild_y; j++)
925         {
926                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
927                 {
928                         cave[j][i].feat = FEAT_TOWN;
929                         cave[j][i].special = wilderness[j][i].town;
930                 }
931                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
932                 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
933                 {
934                         cave[j][i].feat = FEAT_ENTRANCE;
935                         cave[j][i].special = (byte)wilderness[j][i].entrance;
936                 }
937                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
938
939                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
940         }
941
942         cur_hgt = (s16b) max_wild_y;
943         cur_wid = (s16b) max_wild_x;
944
945         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
946         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
947
948         /* Assume illegal panel */
949         panel_row_min = cur_hgt;
950         panel_col_min = cur_wid;
951
952         /* Place the player */
953         px = p_ptr->wilderness_x;
954         py = p_ptr->wilderness_y;
955
956         p_ptr->town_num = 0;
957 }
958
959
960 typedef struct wilderness_grid wilderness_grid;
961
962 struct wilderness_grid
963 {
964         int             terrain;    /* Terrain type */
965         int             town;       /* Town number */
966         s16b    level;          /* Level of the wilderness */
967         byte    road;       /* Road */
968         char    name[32];       /* Name of the town/wilderness */
969 };
970
971
972 static wilderness_grid w_letter[255];
973
974
975 /*
976  * Parse a sub-file of the "extra info"
977  */
978 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
979 {
980         int i, num;
981         char *zz[33];
982
983
984         /* Paranoia */
985         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
986
987         switch (buf[2])
988         {
989                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
990 #ifdef JP
991         case 'E':
992                 return 0;
993         case 'F':
994         case 'J':
995 #else
996         case 'J':
997                 return 0;
998         case 'F':
999         case 'E':
1000 #endif
1001         {
1002                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1003                 {
1004                         int index = zz[0][0];
1005                         
1006                         if (num > 1)
1007                                 w_letter[index].terrain = atoi(zz[1]);
1008                         else
1009                                 w_letter[index].terrain = 0;
1010                         
1011                         if (num > 2)
1012                                 w_letter[index].level = atoi(zz[2]);
1013                         else
1014                                 w_letter[index].level = 0;
1015                         
1016                         if (num > 3)
1017                                 w_letter[index].town = atoi(zz[3]);
1018                         else
1019                                 w_letter[index].town = 0;
1020                         
1021                         if (num > 4)
1022                                 w_letter[index].road = atoi(zz[4]);
1023                         else
1024                                 w_letter[index].road = 0;
1025                         
1026                         if (num > 5)
1027                                 strcpy(w_letter[index].name, zz[5]);
1028                         else
1029                                 w_letter[index].name[0] = 0;
1030                 }
1031                 else
1032                 {
1033                                 /* Failure */
1034                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1035                 }
1036                 
1037                 break;
1038         }
1039         
1040         /* Process "W:D:<layout> */
1041         /* Layout of the wilderness */
1042         case 'D':
1043         {
1044                 /* Acquire the text */
1045                 char *s = buf+4;
1046                 
1047                 /* Length of the text */
1048                 int len = strlen(s);
1049                 
1050                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1051                 {
1052                         int idx = s[0];
1053                         
1054                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1055                         
1056                         wilderness[*y][*x].level = w_letter[idx].level;
1057                         
1058                         wilderness[*y][*x].town = w_letter[idx].town;
1059                         
1060                         wilderness[*y][*x].road = w_letter[idx].road;
1061                         
1062                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1063                 }
1064                 
1065                 (*y)++;
1066                 
1067                 break;
1068         }
1069         
1070         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1071         case 'P':
1072         {
1073                 if ((p_ptr->wilderness_x == 0) &&
1074                     (p_ptr->wilderness_y == 0))
1075                 {
1076                         if (tokenize(buf+4, 2, zz, 0) == 2)
1077                         {
1078                                 p_ptr->wilderness_y = atoi(zz[0]);
1079                                 p_ptr->wilderness_x = atoi(zz[1]);
1080                                 
1081                                 if ((p_ptr->wilderness_x < 1) ||
1082                                     (p_ptr->wilderness_x > max_wild_x) ||
1083                                     (p_ptr->wilderness_y < 1) ||
1084                                     (p_ptr->wilderness_y > max_wild_y))
1085                                 {
1086                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1087                                 }
1088                         }
1089                         else
1090                         {
1091                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1092                         }
1093                 }
1094                 
1095                 break;
1096         }
1097         
1098         default:
1099                 /* Failure */
1100                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1101         }
1102         
1103         for (i = 1; i < max_d_idx; i++)
1104         {
1105                 if (!d_info[i].maxdepth) continue;
1106                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1107                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1108                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1109         }
1110
1111         /* Success */
1112         return (0);
1113 }
1114
1115
1116 /*
1117  * Generate the random seeds for the wilderness
1118  */
1119 void seed_wilderness(void)
1120 {
1121         int x, y;
1122
1123         /* Init wilderness seeds */
1124         for (x = 0; x < max_wild_x; x++)
1125         {
1126                 for (y = 0; y < max_wild_y; y++)
1127                 {
1128                         wilderness[y][x].seed = randint0(0x10000000);
1129                         wilderness[y][x].entrance = 0;
1130                 }
1131         }
1132 }
1133
1134
1135 /*
1136  * Pointer to wilderness_type
1137  */
1138 typedef wilderness_type *wilderness_type_ptr;
1139
1140 /*
1141  * Initialize wilderness array
1142  */
1143 errr init_wilderness(void)
1144 {
1145         int i;
1146
1147         /* Allocate the wilderness (two-dimension array) */
1148         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1149         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1150
1151         /* Init the other pointers */
1152         for (i = 1; i < max_wild_y; i++)
1153                 wilderness[i] = wilderness[0] + i * max_wild_x;
1154
1155         generate_encounter = FALSE;
1156
1157         return 0;
1158 }
1159
1160
1161 bool change_wild_mode(void)
1162 {
1163         int i;
1164         bool have_pet = FALSE;
1165
1166         if (lite_town || vanilla_town)
1167         {
1168 #ifdef JP
1169                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1170 #else
1171                 msg_print("No global mal");
1172 #endif
1173                 return FALSE;
1174         }
1175         if (!p_ptr->wild_mode)
1176         {
1177                 for (i = 1; i < m_max; i++)
1178                 {
1179                         monster_type *m_ptr = &m_list[i];
1180
1181                         if (!m_ptr->r_idx) continue;
1182                         if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1183                         if (m_ptr->csleep) continue;
1184                         if (m_ptr->cdis > MAX_SIGHT) continue;
1185                         if (!is_hostile(m_ptr)) continue;
1186 #ifdef JP
1187                         msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1188 #else
1189                         msg_print("You cannot enter global map, since there is some monsters nearby!");
1190 #endif
1191                         energy_use = 0;
1192                         return FALSE;
1193                 }
1194
1195                 if (have_pet)
1196                 {
1197 #ifdef JP
1198                         if(!get_check_strict("¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹­°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©", CHECK_OKAY_CANCEL))
1199 #else
1200                         if(!get_check_strict("Do you leave your pets behind? ", CHECK_OKAY_CANCEL))
1201 #endif
1202                         {
1203                                 energy_use = 0;
1204                                 return FALSE;
1205                         }
1206                 }
1207                         
1208                 energy_use = 1000;
1209         }
1210
1211         set_action(ACTION_NONE);
1212
1213         p_ptr->wild_mode = !p_ptr->wild_mode;
1214
1215         /* Leaving */
1216         p_ptr->leaving = TRUE;
1217
1218         return TRUE;
1219 }