OSDN Git Service

ダンジョン情報のfloor1-3, fill_type1-3の配列化. set_floor_and_wall()
[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
52
53 /*
54  * Helper for plasma generation.
55  */
56 static void perturb_point_mid(int x1, int x2, int x3, int x4,
57                           int xmid, int ymid, int rough, int depth_max)
58 {
59         /*
60          * Average the four corners & perturb it a bit.
61          * tmp is a random int +/- rough
62          */
63         int tmp2 = rough*2 + 1;
64         int tmp = randint1(tmp2) - (rough + 1);
65
66         int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
67
68         /* Division always rounds down, so we round up again */
69         if (((x1 + x2 + x3 + x4) % 4) > 1)
70                 avg++;
71
72         /* Normalize */
73         if (avg < 0) avg = 0;
74         if (avg > depth_max) avg = depth_max;
75
76         /* Set the new value. */
77         cave[ymid][xmid].feat = avg;
78 }
79
80
81 static void perturb_point_end(int x1, int x2, int x3,
82                           int xmid, int ymid, int rough, int depth_max)
83 {
84         /*
85          * Average the three corners & perturb it a bit.
86          * tmp is a random int +/- rough
87          */
88         int tmp2 = rough * 2 + 1;
89         int tmp = randint0(tmp2) - rough;
90
91         int avg = ((x1 + x2 + x3) / 3) + tmp;
92
93         /* Division always rounds down, so we round up again */
94         if ((x1 + x2 + x3) % 3) avg++;
95
96         /* Normalize */
97         if (avg < 0) avg = 0;
98         if (avg > depth_max) avg = depth_max;
99
100         /* Set the new value. */
101         cave[ymid][xmid].feat = avg;
102 }
103
104
105 /*
106  * A generic function to generate the plasma fractal.
107  * Note that it uses ``cave_feat'' as temporary storage.
108  * The values in ``cave_feat'' after this function
109  * are NOT actual features; They are raw heights which
110  * need to be converted to features.
111  */
112 static void plasma_recursive(int x1, int y1, int x2, int y2,
113                              int depth_max, int rough)
114 {
115         /* Find middle */
116         int xmid = (x2 - x1) / 2 + x1;
117         int ymid = (y2 - y1) / 2 + y1;
118
119         /* Are we done? */
120         if (x1 + 1 == x2) return;
121
122         perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
123                 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
124
125         perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
126                 xmid, y1, rough, depth_max);
127
128         perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
129                 x2, ymid, rough, depth_max);
130
131         perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
132                 xmid, y2, rough, depth_max);
133
134         perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
135                 x1, ymid, rough, depth_max);
136
137
138         /* Recurse the four quadrants */
139         plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
140         plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
141         plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
142         plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
143 }
144
145
146 /*
147  * The default table in terrain level generation.
148  */
149 static int terrain_table[MAX_WILDERNESS][18] =
150 {
151         /* TERRAIN_EDGE */
152         {
153                         FEAT_PERM,
154                         FEAT_PERM,
155                         FEAT_PERM,
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         /* TERRAIN_TOWN */
178         {
179                         FEAT_FLOOR,
180                         FEAT_FLOOR,
181                         FEAT_FLOOR,
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         /* TERRAIN_DEEP_WATER */
204         {
205                         FEAT_DEEP_WATER,
206                         FEAT_DEEP_WATER,
207                         FEAT_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_SHAL_WATER,
222                         FEAT_SHAL_WATER,
223                         FEAT_SHAL_WATER,
224
225                         FEAT_SHAL_WATER,
226                         FEAT_SHAL_WATER,
227                         FEAT_SHAL_WATER,
228         },
229         /* TERRAIN_SHALLOW_WATER */
230         {
231                         FEAT_DEEP_WATER,
232                         FEAT_DEEP_WATER,
233                         FEAT_DEEP_WATER,
234
235                         FEAT_SHAL_WATER,
236                         FEAT_SHAL_WATER,
237                         FEAT_SHAL_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_FLOOR,
252                         FEAT_DIRT,
253                         FEAT_GRASS,
254         },
255         /* TERRAIN_SWAMP */
256         {
257                         FEAT_FLOOR,
258                         FEAT_FLOOR,
259                         FEAT_FLOOR,
260
261                         FEAT_SHAL_WATER,
262                         FEAT_SHAL_WATER,
263                         FEAT_SHAL_WATER,
264
265                         FEAT_SHAL_WATER,
266                         FEAT_SHAL_WATER,
267                         FEAT_SHAL_WATER,
268
269                         FEAT_GRASS,
270                         FEAT_GRASS,
271                         FEAT_GRASS,
272
273                         FEAT_GRASS,
274                         FEAT_DIRT,
275                         FEAT_DIRT,
276
277                         FEAT_DIRT,
278                         FEAT_DEEP_GRASS,
279                         FEAT_TREES,
280         },
281         /* TERRAIN_DIRT */
282         {
283                         FEAT_FLOOR,
284                         FEAT_FLOOR,
285                         FEAT_FLOOR,
286
287                         FEAT_DIRT,
288                         FEAT_DIRT,
289                         FEAT_DIRT,
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_FLOWER,
301                         FEAT_DEEP_GRASS,
302
303                         FEAT_GRASS,
304                         FEAT_TREES,
305                         FEAT_TREES,
306         },
307         /* TERRAIN_GRASS */
308         {
309                         FEAT_FLOOR,
310                         FEAT_FLOOR,
311                         FEAT_DIRT,
312
313                         FEAT_DIRT,
314                         FEAT_GRASS,
315                         FEAT_GRASS,
316
317                         FEAT_GRASS,
318                         FEAT_GRASS,
319                         FEAT_GRASS,
320
321                         FEAT_GRASS,
322                         FEAT_GRASS,
323                         FEAT_GRASS,
324
325                         FEAT_GRASS,
326                         FEAT_FLOWER,
327                         FEAT_DEEP_GRASS,
328
329                         FEAT_DEEP_GRASS,
330                         FEAT_TREES,
331                         FEAT_TREES,
332         },
333         /* TERRAIN_TREES */
334         {
335                         FEAT_FLOOR,
336                         FEAT_FLOOR,
337                         FEAT_DIRT,
338
339                         FEAT_TREES,
340                         FEAT_TREES,
341                         FEAT_TREES,
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_DEEP_GRASS,
354
355                         FEAT_DEEP_GRASS,
356                         FEAT_GRASS,
357                         FEAT_GRASS,
358         },
359         /* TERRAIN_DESERT */
360         {
361                         FEAT_FLOOR,
362                         FEAT_FLOOR,
363                         FEAT_DIRT,
364
365                         FEAT_DIRT,
366                         FEAT_DIRT,
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_GRASS,
382                         FEAT_GRASS,
383                         FEAT_GRASS,
384         },
385         /* TERRAIN_SHALLOW_LAVA */
386         {
387                         FEAT_SHAL_LAVA,
388                         FEAT_SHAL_LAVA,
389                         FEAT_SHAL_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_DEEP_LAVA,
406
407                         FEAT_DEEP_LAVA,
408                         FEAT_DEEP_LAVA,
409                         FEAT_MOUNTAIN,
410         },
411         /* TERRAIN_DEEP_LAVA */
412         {
413                         FEAT_DIRT,
414                         FEAT_DIRT,
415                         FEAT_DIRT,
416
417                         FEAT_SHAL_LAVA,
418                         FEAT_SHAL_LAVA,
419                         FEAT_SHAL_LAVA,
420
421                         FEAT_DEEP_LAVA,
422                         FEAT_DEEP_LAVA,
423                         FEAT_DEEP_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_MOUNTAIN,
435                         FEAT_MOUNTAIN,
436         },
437         /* TERRAIN_MOUNTAIN */
438         {
439                         FEAT_FLOOR,
440                         FEAT_DEEP_GRASS,
441                         FEAT_GRASS,
442
443                         FEAT_GRASS,
444                         FEAT_DIRT,
445                         FEAT_DIRT,
446
447                         FEAT_TREES,
448                         FEAT_TREES,
449                         FEAT_MOUNTAIN,
450
451                         FEAT_MOUNTAIN,
452                         FEAT_MOUNTAIN,
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
464 };
465
466
467 static void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
468 {
469         int x1, y1;
470         int table_size = sizeof(terrain_table[0]) / sizeof(int);
471         int roughness = 1; /* The roughness of the level. */
472
473         /* Unused */
474         (void)border;
475
476         /* The outer wall is easy */
477         if (terrain == TERRAIN_EDGE)
478         {
479                 /* Create level background */
480                 for (y1 = 0; y1 < MAX_HGT; y1++)
481                 {
482                         for (x1 = 0; x1 < MAX_WID; x1++)
483                         {
484                                 cave[y1][x1].feat = FEAT_PERM;
485                         }
486                 }
487
488                 /* We are done already */
489                 return;
490         }
491
492
493         /* Hack -- Use the "simple" RNG */
494         Rand_quick = TRUE;
495
496         /* Hack -- Induce consistant town layout */
497         Rand_value = seed;
498
499         if (!corner)
500         {
501                 /* Create level background */
502                 for (y1 = 0; y1 < MAX_HGT; y1++)
503                 {
504                         for (x1 = 0; x1 < MAX_WID; x1++)
505                         {
506                                 cave[y1][x1].feat = table_size / 2;
507                         }
508                 }
509         }
510
511         /*
512          * Initialize the four corners
513          * ToDo: calculate the medium height of the adjacent
514          * terrains for every corner.
515          */
516         cave[1][1].feat = randint0(table_size);
517         cave[MAX_HGT-2][1].feat = randint0(table_size);
518         cave[1][MAX_WID-2].feat = randint0(table_size);
519         cave[MAX_HGT-2][MAX_WID-2].feat = randint0(table_size);
520
521         if (!corner)
522         {
523                 /* x1, y1, x2, y2, num_depths, roughness */
524                 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
525         }
526
527         /* Use the complex RNG */
528         Rand_quick = FALSE;
529
530         for (y1 = 1; y1 < MAX_HGT-1; y1++)
531         {
532                 for (x1 = 1; x1 < MAX_WID-1; x1++)
533                 {
534                         cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
535                 }
536         }
537 }
538
539
540
541 /*
542  * Load a town or generate a terrain level using "plasma" fractals.
543  *
544  * x and y are the coordinates of the area in the wilderness.
545  * Border and corner are optimization flags to speed up the
546  * generation of the fractal terrain.
547  * If border is set then only the border of the terrain should
548  * be generated (for initializing the border structure).
549  * If corner is set then only the corners of the area are needed.
550  */
551 static void generate_area(int y, int x, bool border, bool corner)
552 {
553         int x1, y1;
554
555         /* Number of the town (if any) */
556         p_ptr->town_num = wilderness[y][x].town;
557
558         /* Set the base level */
559         base_level = wilderness[y][x].level;
560
561         /* Set the dungeon level */
562         dun_level = 0;
563
564         /* Set the monster generation level */
565         monster_level = base_level;
566
567         /* Set the object generation level */
568         object_level = base_level;
569
570
571         /* Create the town */
572         if (p_ptr->town_num)
573         {
574                 /* Reset the buildings */
575                 init_buildings();
576
577                 /* Initialize the town */
578                 if (border | corner)
579                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
580                 else
581                         init_flags = INIT_CREATE_DUNGEON;
582
583                 process_dungeon_file("t_info.txt", 0, 0, MAX_HGT, MAX_WID);
584
585                 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
586         }
587         else
588         {
589                 int terrain = wilderness[y][x].terrain;
590                 u32b seed = wilderness[y][x].seed;
591
592                 generate_wilderness_area(terrain, seed, border, corner);
593         }
594
595         if (!corner && !wilderness[y][x].town)
596         {
597                 /*
598                  * Place roads in the wilderness
599                  * ToDo: make the road a bit more interresting
600                  */
601                 if (wilderness[y][x].road)
602                 {
603                         cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
604
605                         if (wilderness[y-1][x].road)
606                         {
607                                 /* North road */
608                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
609                                 {
610                                         x1 = MAX_WID/2;
611                                         cave[y1][x1].feat = FEAT_FLOOR;
612                                 }
613                         }
614
615                         if (wilderness[y+1][x].road)
616                         {
617                                 /* North road */
618                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
619                                 {
620                                         x1 = MAX_WID/2;
621                                         cave[y1][x1].feat = FEAT_FLOOR;
622                                 }
623                         }
624
625                         if (wilderness[y][x+1].road)
626                         {
627                                 /* East road */
628                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
629                                 {
630                                         y1 = MAX_HGT/2;
631                                         cave[y1][x1].feat = FEAT_FLOOR;
632                                 }
633                         }
634
635                         if (wilderness[y][x-1].road)
636                         {
637                                 /* West road */
638                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
639                                 {
640                                         y1 = MAX_HGT/2;
641                                         cave[y1][x1].feat = FEAT_FLOOR;
642                                 }
643                         }
644                 }
645         }
646
647         if (wilderness[y][x].entrance && !wilderness[y][x].town && (p_ptr->total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
648         {
649                 int dy, dx;
650
651                 /* Hack -- Use the "simple" RNG */
652                 Rand_quick = TRUE;
653
654                 /* Hack -- Induce consistant town layout */
655                 Rand_value = wilderness[y][x].seed;
656
657                 dy = rand_range(6, cur_hgt - 6);
658                 dx = rand_range(6, cur_wid - 6);
659
660                 cave[dy][dx].feat = FEAT_ENTRANCE;
661                 cave[dy][dx].special = wilderness[y][x].entrance;
662
663                 /* Use the complex RNG */
664                 Rand_quick = FALSE;
665         }
666 }
667
668
669 /*
670  * Border of the wilderness area
671  */
672 static border_type border;
673
674
675 /*
676  * Build the wilderness area outside of the town.
677  */
678 void wilderness_gen(void)
679 {
680         int i, y, x, lim;
681         cave_type *c_ptr;
682         feature_type *f_ptr;
683
684         /* Big town */
685         cur_hgt = MAX_HGT;
686         cur_wid = MAX_WID;
687
688         /* Assume illegal panel */
689         panel_row_min = cur_hgt;
690         panel_col_min = cur_wid;
691
692         /* Init the wilderness */
693
694         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
695
696         x = p_ptr->wilderness_x;
697         y = p_ptr->wilderness_y;
698
699         /* Prepare allocation table */
700         get_mon_num_prep(get_monster_hook(), NULL);
701
702         /* North border */
703         generate_area(y - 1, x, TRUE, FALSE);
704
705         for (i = 1; i < MAX_WID - 1; i++)
706         {
707                 border.north[i] = cave[MAX_HGT - 2][i].feat;
708         }
709
710         /* South border */
711         generate_area(y + 1, x, TRUE, FALSE);
712
713         for (i = 1; i < MAX_WID - 1; i++)
714         {
715                 border.south[i] = cave[1][i].feat;
716         }
717
718         /* West border */
719         generate_area(y, x - 1, TRUE, FALSE);
720
721         for (i = 1; i < MAX_HGT - 1; i++)
722         {
723                 border.west[i] = cave[i][MAX_WID - 2].feat;
724         }
725
726         /* East border */
727         generate_area(y, x + 1, TRUE, FALSE);
728
729         for (i = 1; i < MAX_HGT - 1; i++)
730         {
731                 border.east[i] = cave[i][1].feat;
732         }
733
734         /* North west corner */
735         generate_area(y - 1, x - 1, FALSE, TRUE);
736         border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
737
738         /* North east corner */
739         generate_area(y - 1, x + 1, FALSE, TRUE);
740         border.north_east = cave[MAX_HGT - 2][1].feat;
741
742         /* South west corner */
743         generate_area(y + 1, x - 1, FALSE, TRUE);
744         border.south_west = cave[1][MAX_WID - 2].feat;
745
746         /* South east corner */
747         generate_area(y + 1, x + 1, FALSE, TRUE);
748         border.south_east = cave[1][1].feat;
749
750
751         /* Create terrain of the current area */
752         generate_area(y, x, FALSE, FALSE);
753
754
755         /* Special boundary walls -- North */
756         for (i = 0; i < MAX_WID; i++)
757         {
758                 cave[0][i].feat = FEAT_PERM;
759                 cave[0][i].mimic = border.north[i];
760         }
761
762         /* Special boundary walls -- South */
763         for (i = 0; i < MAX_WID; i++)
764         {
765                 cave[MAX_HGT - 1][i].feat = FEAT_PERM;
766                 cave[MAX_HGT - 1][i].mimic = border.south[i];
767         }
768
769         /* Special boundary walls -- West */
770         for (i = 0; i < MAX_HGT; i++)
771         {
772                 cave[i][0].feat = FEAT_PERM;
773                 cave[i][0].mimic = border.west[i];
774         }
775
776         /* Special boundary walls -- East */
777         for (i = 0; i < MAX_HGT; i++)
778         {
779                 cave[i][MAX_WID - 1].feat = FEAT_PERM;
780                 cave[i][MAX_WID - 1].mimic = border.east[i];
781         }
782
783         /* North west corner */
784         cave[0][0].mimic = border.north_west;
785
786         /* North east corner */
787         cave[0][MAX_WID - 1].mimic = border.north_east;
788
789         /* South west corner */
790         cave[MAX_HGT - 1][0].mimic = border.south_west;
791
792         /* South east corner */
793         cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
794
795         /* Light up or darken the area */
796         for (y = 0; y < cur_hgt; y++)
797         {
798                 for (x = 0; x < cur_wid; x++)
799                 {
800                         /* Get the cave grid */
801                         c_ptr = &cave[y][x];
802
803                         if (is_daytime())
804                         {
805                                 /* Assume lit */
806                                 c_ptr->info |= (CAVE_GLOW);
807
808                                 /* Hack -- Memorize lit grids if allowed */
809                                 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
810                         }
811                         else
812                         {
813                                 /* Feature code (applying "mimic" field) */
814                                 f_ptr = &f_info[get_feat_mimic(c_ptr)];
815
816                                 if (!is_mirror_grid(c_ptr) && !have_flag(f_ptr->flags, FF_QUEST_ENTER) &&
817                                     !have_flag(f_ptr->flags, FF_ENTRANCE))
818                                 {
819                                         /* Assume dark */
820                                         c_ptr->info &= ~(CAVE_GLOW);
821
822                                         /* Darken "boring" features */
823                                         if (!have_flag(f_ptr->flags, FF_REMEMBER))
824                                         {
825                                                 /* Forget the grid */
826                                                 c_ptr->info &= ~(CAVE_MARK);
827                                         }
828                                 }
829                                 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
830                                 {
831                                         /* Assume lit */
832                                         c_ptr->info |= (CAVE_GLOW);
833
834                                         /* Hack -- Memorize lit grids if allowed */
835                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
836                                 }
837                         }
838                 }
839         }
840
841         if (p_ptr->teleport_town)
842         {
843                 for (y = 0; y < cur_hgt; y++)
844                 {
845                         for (x = 0; x < cur_wid; x++)
846                         {
847                                 /* Get the cave grid */
848                                 c_ptr = &cave[y][x];
849
850                                 /* Seeing true feature code (ignore mimic) */
851                                 f_ptr = &f_info[c_ptr->feat];
852
853                                 if (have_flag(f_ptr->flags, FF_BLDG))
854                                 {
855                                         if ((f_ptr->power == 4) || ((p_ptr->town_num == 1) && (f_ptr->power == 0)))
856                                         {
857                                                 if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
858                                                 p_ptr->oldpy = y;
859                                                 p_ptr->oldpx = x;
860                                         }
861                                 }
862                         }
863                 }
864                 p_ptr->teleport_town = FALSE;
865         }
866
867         else if (p_ptr->leaving_dungeon)
868         {
869                 for (y = 0; y < cur_hgt; y++)
870                 {
871                         for (x = 0; x < cur_wid; x++)
872                         {
873                                 /* Get the cave grid */
874                                 c_ptr = &cave[y][x];
875
876                                 if (have_flag(f_flags_grid(c_ptr), FF_ENTRANCE))
877                                 {
878                                         if (c_ptr->m_idx) delete_monster_idx(c_ptr->m_idx);
879                                         p_ptr->oldpy = y;
880                                         p_ptr->oldpx = x;
881                                 }
882                         }
883                 }
884                 p_ptr->teleport_town = FALSE;
885         }
886
887         player_place(p_ptr->oldpy, p_ptr->oldpx);
888         /* p_ptr->leaving_dungeon = FALSE;*/
889
890         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
891
892         /* Make some residents */
893         for (i = 0; i < lim; i++)
894         {
895                 u32b mode = 0;
896
897                 if (!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))))
898                         mode |= PM_ALLOW_SLEEP;
899
900                 /* Make a resident */
901                 (void)alloc_monster(generate_encounter ? 0 : 3, mode);
902         }
903
904         if(generate_encounter) ambush_flag = TRUE;
905         generate_encounter = FALSE;
906
907         /* Fill the arrays of floors and walls in the good proportions */
908         set_floor_and_wall(0);
909
910         /* Set rewarded quests to finished */
911         for (i = 0; i < max_quests; i++)
912         {
913                 if (quest[i].status == QUEST_STATUS_REWARDED)
914                         quest[i].status = QUEST_STATUS_FINISHED;
915         }
916 }
917
918
919 /*
920  * Build the wilderness area.
921  * -DG-
922  */
923 void wilderness_gen_small()
924 {
925         int i, j;
926
927         /* To prevent stupid things */
928         for (i = 0; i < MAX_WID; i++)
929         for (j = 0; j < MAX_HGT; j++)
930         {
931                 cave[j][i].feat = FEAT_PERM;
932         }
933
934         /* Init the wilderness */
935         process_dungeon_file("w_info.txt", 0, 0, max_wild_y, max_wild_x);
936
937         /* Fill the map */
938         for (i = 0; i < max_wild_x; i++)
939         for (j = 0; j < max_wild_y; j++)
940         {
941                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
942                 {
943                         cave[j][i].feat = FEAT_TOWN;
944                         cave[j][i].special = wilderness[j][i].town;
945                 }
946                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
947                 else if (wilderness[j][i].entrance && (p_ptr->total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
948                 {
949                         cave[j][i].feat = FEAT_ENTRANCE;
950                         cave[j][i].special = (byte)wilderness[j][i].entrance;
951                 }
952                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
953
954                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
955         }
956
957         cur_hgt = (s16b) max_wild_y;
958         cur_wid = (s16b) max_wild_x;
959
960         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
961         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
962
963         /* Assume illegal panel */
964         panel_row_min = cur_hgt;
965         panel_col_min = cur_wid;
966
967         /* Place the player */
968         px = p_ptr->wilderness_x;
969         py = p_ptr->wilderness_y;
970
971         p_ptr->town_num = 0;
972 }
973
974
975 typedef struct wilderness_grid wilderness_grid;
976
977 struct wilderness_grid
978 {
979         int             terrain;    /* Terrain type */
980         int             town;       /* Town number */
981         s16b    level;          /* Level of the wilderness */
982         byte    road;       /* Road */
983         char    name[32];       /* Name of the town/wilderness */
984 };
985
986
987 static wilderness_grid w_letter[255];
988
989
990 /*
991  * Parse a sub-file of the "extra info"
992  */
993 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
994 {
995         int i, num;
996         char *zz[33];
997
998         /* Unused */
999         (void)ymin;
1000         (void)ymax;
1001
1002         /* Paranoia */
1003         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
1004
1005         switch (buf[2])
1006         {
1007                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
1008 #ifdef JP
1009         case 'E':
1010                 return 0;
1011         case 'F':
1012         case 'J':
1013 #else
1014         case 'J':
1015                 return 0;
1016         case 'F':
1017         case 'E':
1018 #endif
1019         {
1020                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
1021                 {
1022                         int index = zz[0][0];
1023                         
1024                         if (num > 1)
1025                                 w_letter[index].terrain = atoi(zz[1]);
1026                         else
1027                                 w_letter[index].terrain = 0;
1028                         
1029                         if (num > 2)
1030                                 w_letter[index].level = atoi(zz[2]);
1031                         else
1032                                 w_letter[index].level = 0;
1033                         
1034                         if (num > 3)
1035                                 w_letter[index].town = atoi(zz[3]);
1036                         else
1037                                 w_letter[index].town = 0;
1038                         
1039                         if (num > 4)
1040                                 w_letter[index].road = atoi(zz[4]);
1041                         else
1042                                 w_letter[index].road = 0;
1043                         
1044                         if (num > 5)
1045                                 strcpy(w_letter[index].name, zz[5]);
1046                         else
1047                                 w_letter[index].name[0] = 0;
1048                 }
1049                 else
1050                 {
1051                                 /* Failure */
1052                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1053                 }
1054                 
1055                 break;
1056         }
1057         
1058         /* Process "W:D:<layout> */
1059         /* Layout of the wilderness */
1060         case 'D':
1061         {
1062                 /* Acquire the text */
1063                 char *s = buf+4;
1064                 
1065                 /* Length of the text */
1066                 int len = strlen(s);
1067                 
1068                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1069                 {
1070                         int idx = s[0];
1071                         
1072                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1073                         
1074                         wilderness[*y][*x].level = w_letter[idx].level;
1075                         
1076                         wilderness[*y][*x].town = w_letter[idx].town;
1077                         
1078                         wilderness[*y][*x].road = w_letter[idx].road;
1079                         
1080                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1081                 }
1082                 
1083                 (*y)++;
1084                 
1085                 break;
1086         }
1087         
1088         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1089         case 'P':
1090         {
1091                 if ((p_ptr->wilderness_x == 0) &&
1092                     (p_ptr->wilderness_y == 0))
1093                 {
1094                         if (tokenize(buf+4, 2, zz, 0) == 2)
1095                         {
1096                                 p_ptr->wilderness_y = atoi(zz[0]);
1097                                 p_ptr->wilderness_x = atoi(zz[1]);
1098                                 
1099                                 if ((p_ptr->wilderness_x < 1) ||
1100                                     (p_ptr->wilderness_x > max_wild_x) ||
1101                                     (p_ptr->wilderness_y < 1) ||
1102                                     (p_ptr->wilderness_y > max_wild_y))
1103                                 {
1104                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1105                                 }
1106                         }
1107                         else
1108                         {
1109                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1110                         }
1111                 }
1112                 
1113                 break;
1114         }
1115         
1116         default:
1117                 /* Failure */
1118                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1119         }
1120         
1121         for (i = 1; i < max_d_idx; i++)
1122         {
1123                 if (!d_info[i].maxdepth) continue;
1124                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1125                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1126                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1127         }
1128
1129         /* Success */
1130         return (0);
1131 }
1132
1133
1134 /*
1135  * Generate the random seeds for the wilderness
1136  */
1137 void seed_wilderness(void)
1138 {
1139         int x, y;
1140
1141         /* Init wilderness seeds */
1142         for (x = 0; x < max_wild_x; x++)
1143         {
1144                 for (y = 0; y < max_wild_y; y++)
1145                 {
1146                         wilderness[y][x].seed = randint0(0x10000000);
1147                         wilderness[y][x].entrance = 0;
1148                 }
1149         }
1150 }
1151
1152
1153 /*
1154  * Pointer to wilderness_type
1155  */
1156 typedef wilderness_type *wilderness_type_ptr;
1157
1158 /*
1159  * Initialize wilderness array
1160  */
1161 errr init_wilderness(void)
1162 {
1163         int i;
1164
1165         /* Allocate the wilderness (two-dimension array) */
1166         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1167         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1168
1169         /* Init the other pointers */
1170         for (i = 1; i < max_wild_y; i++)
1171                 wilderness[i] = wilderness[0] + i * max_wild_x;
1172
1173         generate_encounter = FALSE;
1174
1175         return 0;
1176 }
1177
1178
1179 bool change_wild_mode(void)
1180 {
1181         int i;
1182         bool have_pet = FALSE;
1183
1184         /* It is in the middle of changing map */
1185         if (p_ptr->leaving) return FALSE;
1186
1187
1188         if (lite_town || vanilla_town)
1189         {
1190 #ifdef JP
1191                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1192 #else
1193                 msg_print("No global map.");
1194 #endif
1195                 return FALSE;
1196         }
1197
1198         if (p_ptr->wild_mode)
1199         {
1200                 /* Save the location in the global map */
1201                 p_ptr->wilderness_x = px;
1202                 p_ptr->wilderness_y = py;
1203
1204                 /* Give first move to the player */
1205                 p_ptr->energy_need = 0;
1206
1207                 /* Go back to the ordinary map */
1208                 p_ptr->wild_mode = FALSE;
1209
1210                 /* Leaving */
1211                 p_ptr->leaving = TRUE;
1212
1213                 /* Succeed */
1214                 return TRUE;
1215         }
1216
1217         for (i = 1; i < m_max; i++)
1218         {
1219                 monster_type *m_ptr = &m_list[i];
1220
1221                 if (!m_ptr->r_idx) continue;
1222                 if (is_pet(m_ptr) && i != p_ptr->riding) have_pet = TRUE;
1223                 if (m_ptr->csleep) continue;
1224                 if (m_ptr->cdis > MAX_SIGHT) continue;
1225                 if (!is_hostile(m_ptr)) continue;
1226 #ifdef JP
1227                 msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1228 #else
1229                 msg_print("You cannot enter global map, since there is some monsters nearby!");
1230 #endif
1231                 energy_use = 0;
1232                 return FALSE;
1233         }
1234
1235         if (have_pet)
1236         {
1237 #ifdef JP
1238                 cptr msg = "¥Ú¥Ã¥È¤òÃÖ¤¤¤Æ¹­°è¥Þ¥Ã¥×¤ËÆþ¤ê¤Þ¤¹¤«¡©";
1239 #else
1240                 cptr msg = "Do you leave your pets behind? ";
1241 #endif
1242
1243                 if (!get_check_strict(msg, CHECK_OKAY_CANCEL))
1244                 {
1245                         energy_use = 0;
1246                         return FALSE;
1247                 }
1248         }
1249
1250         /* HACK */
1251         energy_use = 1000;
1252
1253         /* Remember the position */
1254         p_ptr->oldpx = px;
1255         p_ptr->oldpy = py;
1256
1257         /* Cancel any special action */
1258         set_action(ACTION_NONE);
1259
1260         /* Go into the global map */
1261         p_ptr->wild_mode = TRUE;
1262
1263         /* Leaving */
1264         p_ptr->leaving = TRUE;
1265
1266         /* Succeed */
1267         return TRUE;
1268 }