OSDN Git Service

Hengband 108 fix2 revision 4
[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  * Helper for plasma generation.
19  */
20 static void perturb_point_mid(int x1, int x2, int x3, int x4,
21                           int xmid, int ymid, int rough, int depth_max)
22 {
23         /*
24          * Average the four corners & perturb it a bit.
25          * tmp is a random int +/- rough
26          */
27         int tmp2 = rough*2 + 1;
28         int tmp = randint(tmp2) - (rough + 1);
29
30         int avg = ((x1 + x2 + x3 + x4) / 4) + tmp;
31
32         /* Division always rounds down, so we round up again */
33         if (((x1 + x2 + x3 + x4) % 4) > 1)
34                 avg++;
35
36         /* Normalize */
37         if (avg < 0) avg = 0;
38         if (avg > depth_max) avg = depth_max;
39
40         /* Set the new value. */
41         cave[ymid][xmid].feat = avg;
42 }
43
44
45 static void perturb_point_end(int x1, int x2, int x3,
46                           int xmid, int ymid, int rough, int depth_max)
47 {
48         /*
49          * Average the three corners & perturb it a bit.
50          * tmp is a random int +/- rough
51          */
52         int tmp2 = rough * 2 + 1;
53         int tmp = rand_int(tmp2) - rough;
54
55         int avg = ((x1 + x2 + x3) / 3) + tmp;
56
57         /* Division always rounds down, so we round up again */
58         if ((x1 + x2 + x3) % 3) avg++;
59
60         /* Normalize */
61         if (avg < 0) avg = 0;
62         if (avg > depth_max) avg = depth_max;
63
64         /* Set the new value. */
65         cave[ymid][xmid].feat = avg;
66 }
67
68
69 /*
70  * A generic function to generate the plasma fractal.
71  * Note that it uses ``cave_feat'' as temporary storage.
72  * The values in ``cave_feat'' after this function
73  * are NOT actual features; They are raw heights which
74  * need to be converted to features.
75  */
76 static void plasma_recursive(int x1, int y1, int x2, int y2,
77                              int depth_max, int rough)
78 {
79         /* Find middle */
80         int xmid = (x2 - x1) / 2 + x1;
81         int ymid = (y2 - y1) / 2 + y1;
82
83         /* Are we done? */
84         if (x1 + 1 == x2) return;
85
86         perturb_point_mid(cave[y1][x1].feat, cave[y2][x1].feat, cave[y1][x2].feat,
87                 cave[y2][x2].feat, xmid, ymid, rough, depth_max);
88
89         perturb_point_end(cave[y1][x1].feat, cave[y1][x2].feat, cave[ymid][xmid].feat,
90                 xmid, y1, rough, depth_max);
91
92         perturb_point_end(cave[y1][x2].feat, cave[y2][x2].feat, cave[ymid][xmid].feat,
93                 x2, ymid, rough, depth_max);
94
95         perturb_point_end(cave[y2][x2].feat, cave[y2][x1].feat, cave[ymid][xmid].feat,
96                 xmid, y2, rough, depth_max);
97
98         perturb_point_end(cave[y2][x1].feat, cave[y1][x1].feat, cave[ymid][xmid].feat,
99                 x1, ymid, rough, depth_max);
100
101
102         /* Recurse the four quadrants */
103         plasma_recursive(x1, y1, xmid, ymid, depth_max, rough);
104         plasma_recursive(xmid, y1, x2, ymid, depth_max, rough);
105         plasma_recursive(x1, ymid, xmid, y2, depth_max, rough);
106         plasma_recursive(xmid, ymid, x2, y2, depth_max, rough);
107 }
108
109
110 /*
111  * The default table in terrain level generation.
112  */
113 static int terrain_table[MAX_WILDERNESS][18] =
114 {
115         /* TERRAIN_EDGE */
116         {
117                         FEAT_PERM_SOLID,
118                         FEAT_PERM_SOLID,
119                         FEAT_PERM_SOLID,
120
121                         FEAT_PERM_SOLID,
122                         FEAT_PERM_SOLID,
123                         FEAT_PERM_SOLID,
124
125                         FEAT_PERM_SOLID,
126                         FEAT_PERM_SOLID,
127                         FEAT_PERM_SOLID,
128
129                         FEAT_PERM_SOLID,
130                         FEAT_PERM_SOLID,
131                         FEAT_PERM_SOLID,
132
133                         FEAT_PERM_SOLID,
134                         FEAT_PERM_SOLID,
135                         FEAT_PERM_SOLID,
136
137                         FEAT_PERM_SOLID,
138                         FEAT_PERM_SOLID,
139                         FEAT_PERM_SOLID,
140         },
141         /* TERRAIN_TOWN */
142         {
143                         FEAT_FLOOR,
144                         FEAT_FLOOR,
145                         FEAT_FLOOR,
146
147                         FEAT_FLOOR,
148                         FEAT_FLOOR,
149                         FEAT_FLOOR,
150
151                         FEAT_FLOOR,
152                         FEAT_FLOOR,
153                         FEAT_FLOOR,
154
155                         FEAT_FLOOR,
156                         FEAT_FLOOR,
157                         FEAT_FLOOR,
158
159                         FEAT_FLOOR,
160                         FEAT_FLOOR,
161                         FEAT_FLOOR,
162
163                         FEAT_FLOOR,
164                         FEAT_FLOOR,
165                         FEAT_FLOOR,
166         },
167         /* TERRAIN_DEEP_WATER */
168         {
169                         FEAT_DEEP_WATER,
170                         FEAT_DEEP_WATER,
171                         FEAT_DEEP_WATER,
172
173                         FEAT_DEEP_WATER,
174                         FEAT_DEEP_WATER,
175                         FEAT_DEEP_WATER,
176
177                         FEAT_DEEP_WATER,
178                         FEAT_DEEP_WATER,
179                         FEAT_DEEP_WATER,
180
181                         FEAT_DEEP_WATER,
182                         FEAT_DEEP_WATER,
183                         FEAT_DEEP_WATER,
184
185                         FEAT_SHAL_WATER,
186                         FEAT_SHAL_WATER,
187                         FEAT_SHAL_WATER,
188
189                         FEAT_SHAL_WATER,
190                         FEAT_SHAL_WATER,
191                         FEAT_SHAL_WATER,
192         },
193         /* TERRAIN_SHALLOW_WATER */
194         {
195                         FEAT_DEEP_WATER,
196                         FEAT_DEEP_WATER,
197                         FEAT_DEEP_WATER,
198
199                         FEAT_SHAL_WATER,
200                         FEAT_SHAL_WATER,
201                         FEAT_SHAL_WATER,
202
203                         FEAT_SHAL_WATER,
204                         FEAT_SHAL_WATER,
205                         FEAT_SHAL_WATER,
206
207                         FEAT_SHAL_WATER,
208                         FEAT_SHAL_WATER,
209                         FEAT_SHAL_WATER,
210
211                         FEAT_SHAL_WATER,
212                         FEAT_SHAL_WATER,
213                         FEAT_SHAL_WATER,
214
215                         FEAT_FLOOR,
216                         FEAT_DIRT,
217                         FEAT_GRASS,
218         },
219         /* TERRAIN_SWAMP */
220         {
221                         FEAT_FLOOR,
222                         FEAT_FLOOR,
223                         FEAT_FLOOR,
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                         FEAT_GRASS,
234                         FEAT_GRASS,
235                         FEAT_GRASS,
236
237                         FEAT_GRASS,
238                         FEAT_DIRT,
239                         FEAT_DIRT,
240
241                         FEAT_DIRT,
242                         FEAT_DEEP_GRASS,
243                         FEAT_TREES,
244         },
245         /* TERRAIN_DIRT */
246         {
247                         FEAT_FLOOR,
248                         FEAT_FLOOR,
249                         FEAT_FLOOR,
250
251                         FEAT_DIRT,
252                         FEAT_DIRT,
253                         FEAT_DIRT,
254
255                         FEAT_DIRT,
256                         FEAT_DIRT,
257                         FEAT_DIRT,
258
259                         FEAT_DIRT,
260                         FEAT_DIRT,
261                         FEAT_DIRT,
262
263                         FEAT_DIRT,
264                         FEAT_FLOWER,
265                         FEAT_DEEP_GRASS,
266
267                         FEAT_GRASS,
268                         FEAT_TREES,
269                         FEAT_TREES,
270         },
271         /* TERRAIN_GRASS */
272         {
273                         FEAT_FLOOR,
274                         FEAT_FLOOR,
275                         FEAT_DIRT,
276
277                         FEAT_DIRT,
278                         FEAT_GRASS,
279                         FEAT_GRASS,
280
281                         FEAT_GRASS,
282                         FEAT_GRASS,
283                         FEAT_GRASS,
284
285                         FEAT_GRASS,
286                         FEAT_GRASS,
287                         FEAT_GRASS,
288
289                         FEAT_GRASS,
290                         FEAT_FLOWER,
291                         FEAT_DEEP_GRASS,
292
293                         FEAT_DEEP_GRASS,
294                         FEAT_TREES,
295                         FEAT_TREES,
296         },
297         /* TERRAIN_TREES */
298         {
299                         FEAT_FLOOR,
300                         FEAT_FLOOR,
301                         FEAT_DIRT,
302
303                         FEAT_TREES,
304                         FEAT_TREES,
305                         FEAT_TREES,
306
307                         FEAT_TREES,
308                         FEAT_TREES,
309                         FEAT_TREES,
310
311                         FEAT_TREES,
312                         FEAT_TREES,
313                         FEAT_TREES,
314
315                         FEAT_TREES,
316                         FEAT_TREES,
317                         FEAT_DEEP_GRASS,
318
319                         FEAT_DEEP_GRASS,
320                         FEAT_GRASS,
321                         FEAT_GRASS,
322         },
323         /* TERRAIN_DESERT */
324         {
325                         FEAT_FLOOR,
326                         FEAT_FLOOR,
327                         FEAT_DIRT,
328
329                         FEAT_DIRT,
330                         FEAT_DIRT,
331                         FEAT_DIRT,
332
333                         FEAT_DIRT,
334                         FEAT_DIRT,
335                         FEAT_DIRT,
336
337                         FEAT_DIRT,
338                         FEAT_DIRT,
339                         FEAT_DIRT,
340
341                         FEAT_DIRT,
342                         FEAT_DIRT,
343                         FEAT_DIRT,
344
345                         FEAT_GRASS,
346                         FEAT_GRASS,
347                         FEAT_GRASS,
348         },
349         /* TERRAIN_SHALLOW_LAVA */
350         {
351                         FEAT_SHAL_LAVA,
352                         FEAT_SHAL_LAVA,
353                         FEAT_SHAL_LAVA,
354
355                         FEAT_SHAL_LAVA,
356                         FEAT_SHAL_LAVA,
357                         FEAT_SHAL_LAVA,
358
359                         FEAT_SHAL_LAVA,
360                         FEAT_SHAL_LAVA,
361                         FEAT_SHAL_LAVA,
362
363                         FEAT_SHAL_LAVA,
364                         FEAT_SHAL_LAVA,
365                         FEAT_SHAL_LAVA,
366
367                         FEAT_SHAL_LAVA,
368                         FEAT_SHAL_LAVA,
369                         FEAT_DEEP_LAVA,
370
371                         FEAT_DEEP_LAVA,
372                         FEAT_DEEP_LAVA,
373                         FEAT_MOUNTAIN,
374         },
375         /* TERRAIN_DEEP_LAVA */
376         {
377                         FEAT_DIRT,
378                         FEAT_DIRT,
379                         FEAT_DIRT,
380
381                         FEAT_SHAL_LAVA,
382                         FEAT_SHAL_LAVA,
383                         FEAT_SHAL_LAVA,
384
385                         FEAT_DEEP_LAVA,
386                         FEAT_DEEP_LAVA,
387                         FEAT_DEEP_LAVA,
388
389                         FEAT_DEEP_LAVA,
390                         FEAT_DEEP_LAVA,
391                         FEAT_DEEP_LAVA,
392
393                         FEAT_DEEP_LAVA,
394                         FEAT_DEEP_LAVA,
395                         FEAT_DEEP_LAVA,
396
397                         FEAT_DEEP_LAVA,
398                         FEAT_MOUNTAIN,
399                         FEAT_MOUNTAIN,
400         },
401         /* TERRAIN_MOUNTAIN */
402         {
403                         FEAT_FLOOR,
404                         FEAT_DEEP_GRASS,
405                         FEAT_GRASS,
406
407                         FEAT_GRASS,
408                         FEAT_DIRT,
409                         FEAT_DIRT,
410
411                         FEAT_TREES,
412                         FEAT_TREES,
413                         FEAT_MOUNTAIN,
414
415                         FEAT_MOUNTAIN,
416                         FEAT_MOUNTAIN,
417                         FEAT_MOUNTAIN,
418
419                         FEAT_MOUNTAIN,
420                         FEAT_MOUNTAIN,
421                         FEAT_MOUNTAIN,
422
423                         FEAT_MOUNTAIN,
424                         FEAT_MOUNTAIN,
425                         FEAT_MOUNTAIN,
426         },
427
428 };
429
430
431 void generate_wilderness_area(int terrain, u32b seed, bool border, bool corner)
432 {
433         int x1, y1;
434         int table_size = sizeof(terrain_table[0]) / sizeof(int);
435         int roughness = 1; /* The roughness of the level. */
436
437         /* The outer wall is easy */
438         if (terrain == TERRAIN_EDGE)
439         {
440                 /* Create level background */
441                 for (y1 = 0; y1 < MAX_HGT; y1++)
442                 {
443                         for (x1 = 0; x1 < MAX_WID; x1++)
444                         {
445                                 cave[y1][x1].feat = FEAT_PERM_SOLID;
446                         }
447                 }
448
449                 /* We are done already */
450                 return;
451         }
452
453
454         /* Hack -- Use the "simple" RNG */
455         Rand_quick = TRUE;
456
457         /* Hack -- Induce consistant town layout */
458         Rand_value = seed;
459
460         if (!corner)
461         {
462                 /* Create level background */
463                 for (y1 = 0; y1 < MAX_HGT; y1++)
464                 {
465                         for (x1 = 0; x1 < MAX_WID; x1++)
466                         {
467                                 cave[y1][x1].feat = table_size / 2;
468                         }
469                 }
470         }
471
472         /*
473          * Initialize the four corners
474          * ToDo: calculate the medium height of the adjacent
475          * terrains for every corner.
476          */
477         cave[1][1].feat = (byte)rand_int(table_size);
478         cave[MAX_HGT-2][1].feat = (byte)rand_int(table_size);
479         cave[1][MAX_WID-2].feat = (byte)rand_int(table_size);
480         cave[MAX_HGT-2][MAX_WID-2].feat = (byte)rand_int(table_size);
481
482         if (!corner)
483         {
484                 /* x1, y1, x2, y2, num_depths, roughness */
485                 plasma_recursive(1, 1, MAX_WID-2, MAX_HGT-2, table_size-1, roughness);
486         }
487
488         /* Use the complex RNG */
489         Rand_quick = FALSE;
490
491         for (y1 = 1; y1 < MAX_HGT-1; y1++)
492         {
493                 for (x1 = 1; x1 < MAX_WID-1; x1++)
494                 {
495                         cave[y1][x1].feat = terrain_table[terrain][cave[y1][x1].feat];
496                 }
497         }
498 }
499
500
501
502 /*
503  * Load a town or generate a terrain level using "plasma" fractals.
504  *
505  * x and y are the coordinates of the area in the wilderness.
506  * Border and corner are optimization flags to speed up the
507  * generation of the fractal terrain.
508  * If border is set then only the border of the terrain should
509  * be generated (for initializing the border structure).
510  * If corner is set then only the corners of the area are needed.
511  */
512 static void generate_area(int y, int x, bool border, bool corner)
513 {
514         int x1, y1;
515
516         /* Number of the town (if any) */
517         p_ptr->town_num = wilderness[y][x].town;
518
519         /* Set the base level */
520         base_level = wilderness[y][x].level;
521
522         /* Set the dungeon level */
523         dun_level = 0;
524
525         /* Set the monster generation level */
526         monster_level = base_level;
527
528         /* Set the object generation level */
529         object_level = base_level;
530
531 #ifdef USE_SCRIPT
532         if (generate_wilderness_callback(y, x)) return;
533 #endif /* USE_SCRIPT */
534
535
536         /* Create the town */
537         if (p_ptr->town_num)
538         {
539                 /* Reset the buildings */
540                 init_buildings();
541
542                 /* Initialize the town */
543                 if (border | corner)
544                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
545                 else
546                         init_flags = INIT_CREATE_DUNGEON;
547
548                 process_dungeon_file("t_info_j.txt", 0, 0, MAX_HGT, MAX_WID);
549
550                 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
551         }
552         else
553         {
554                 int terrain = wilderness[y][x].terrain;
555                 u32b seed = wilderness[y][x].seed;
556
557                 generate_wilderness_area(terrain, seed, border, corner);
558         }
559
560         if (!corner && !wilderness[y][x].town)
561         {
562                 /*
563                  * Place roads in the wilderness
564                  * ToDo: make the road a bit more interresting
565                  */
566                 if (wilderness[y][x].road)
567                 {
568                         cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
569
570                         if (wilderness[y-1][x].road)
571                         {
572                                 /* North road */
573                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
574                                 {
575                                         x1 = MAX_WID/2;
576                                         cave[y1][x1].feat = FEAT_FLOOR;
577                                 }
578                         }
579
580                         if (wilderness[y+1][x].road)
581                         {
582                                 /* North road */
583                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
584                                 {
585                                         x1 = MAX_WID/2;
586                                         cave[y1][x1].feat = FEAT_FLOOR;
587                                 }
588                         }
589
590                         if (wilderness[y][x+1].road)
591                         {
592                                 /* East road */
593                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
594                                 {
595                                         y1 = MAX_HGT/2;
596                                         cave[y1][x1].feat = FEAT_FLOOR;
597                                 }
598                         }
599
600                         if (wilderness[y][x-1].road)
601                         {
602                                 /* West road */
603                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
604                                 {
605                                         y1 = MAX_HGT/2;
606                                         cave[y1][x1].feat = FEAT_FLOOR;
607                                 }
608                         }
609                 }
610         }
611
612         if (wilderness[y][x].entrance && !wilderness[y][x].town && (total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
613         {
614                 int dy, dx;
615
616                 /* Hack -- Use the "simple" RNG */
617                 Rand_quick = TRUE;
618
619                 /* Hack -- Induce consistant town layout */
620                 Rand_value = wilderness[y][x].seed;
621
622                 dy = rand_range(6, cur_hgt - 6);
623                 dx = rand_range(6, cur_wid - 6);
624
625                 cave[dy][dx].feat = FEAT_ENTRANCE;
626                 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
627
628                 /* Use the complex RNG */
629                 Rand_quick = FALSE;
630         }
631 }
632
633
634 /*
635  * Border of the wilderness area
636  */
637 static border_type border;
638
639
640 /*
641  * Build the wilderness area outside of the town.
642  */
643 void wilderness_gen(void)
644 {
645         int i, y, x, lim;
646         bool daytime;
647         cave_type *c_ptr;
648
649         /* Big town */
650         cur_hgt = MAX_HGT;
651         cur_wid = MAX_WID;
652
653         /* Determine number of panels */
654         max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
655         max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
656
657         /* Assume illegal panel */
658         panel_row = max_panel_rows;
659         panel_col = max_panel_cols;
660
661         /* Init the wilderness */
662
663         process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
664
665         x = p_ptr->wilderness_x;
666         y = p_ptr->wilderness_y;
667
668         /* Prepare allocation table */
669         get_mon_num_prep(get_monster_hook(), NULL);
670
671         /* North border */
672         generate_area(y - 1, x, TRUE, FALSE);
673
674         for (i = 1; i < MAX_WID - 1; i++)
675         {
676                 border.north[i] = cave[MAX_HGT - 2][i].feat;
677         }
678
679         /* South border */
680         generate_area(y + 1, x, TRUE, FALSE);
681
682         for (i = 1; i < MAX_WID - 1; i++)
683         {
684                 border.south[i] = cave[1][i].feat;
685         }
686
687         /* West border */
688         generate_area(y, x - 1, TRUE, FALSE);
689
690         for (i = 1; i < MAX_HGT - 1; i++)
691         {
692                 border.west[i] = cave[i][MAX_WID - 2].feat;
693         }
694
695         /* East border */
696         generate_area(y, x + 1, TRUE, FALSE);
697
698         for (i = 1; i < MAX_HGT - 1; i++)
699         {
700                 border.east[i] = cave[i][1].feat;
701         }
702
703         /* North west corner */
704         generate_area(y - 1, x - 1, FALSE, TRUE);
705         border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
706
707         /* North east corner */
708         generate_area(y - 1, x + 1, FALSE, TRUE);
709         border.north_east = cave[MAX_HGT - 2][1].feat;
710
711         /* South west corner */
712         generate_area(y + 1, x - 1, FALSE, TRUE);
713         border.south_west = cave[1][MAX_WID - 2].feat;
714
715         /* South east corner */
716         generate_area(y + 1, x + 1, FALSE, TRUE);
717         border.south_east = cave[1][1].feat;
718
719
720         /* Create terrain of the current area */
721         generate_area(y, x, FALSE, FALSE);
722
723
724         /* Special boundary walls -- North */
725         for (i = 0; i < MAX_WID; i++)
726         {
727                 cave[0][i].feat = FEAT_PERM_SOLID;
728                 cave[0][i].mimic = border.north[i];
729         }
730
731         /* Special boundary walls -- South */
732         for (i = 0; i < MAX_WID; i++)
733         {
734                 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
735                 cave[MAX_HGT - 1][i].mimic = border.south[i];
736         }
737
738         /* Special boundary walls -- West */
739         for (i = 0; i < MAX_HGT; i++)
740         {
741                 cave[i][0].feat = FEAT_PERM_SOLID;
742                 cave[i][0].mimic = border.west[i];
743         }
744
745         /* Special boundary walls -- East */
746         for (i = 0; i < MAX_HGT; i++)
747         {
748                 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
749                 cave[i][MAX_WID - 1].mimic = border.east[i];
750         }
751
752         /* North west corner */
753         cave[0][0].mimic = border.north_west;
754
755         /* North east corner */
756         cave[0][MAX_WID - 1].mimic = border.north_east;
757
758         /* South west corner */
759         cave[MAX_HGT - 1][0].mimic = border.south_west;
760
761         /* South east corner */
762         cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
763
764
765         /* Day time */
766         if ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2))
767                 daytime = TRUE;
768         else
769                 daytime = FALSE;
770
771         /* Light up or darken the area */
772         for (y = 0; y < cur_hgt; y++)
773         {
774                 for (x = 0; x < cur_wid; x++)
775                 {
776                         /* Get the cave grid */
777                         c_ptr = &cave[y][x];
778
779                         if (daytime)
780                         {
781                                 /* Assume lit */
782                                 c_ptr->info |= (CAVE_GLOW);
783
784                                 /* Hack -- Memorize lit grids if allowed */
785                                 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
786                         }
787                         else
788                         {
789                                 /* Darken "boring" features */
790                                 if ((c_ptr->feat <= FEAT_INVIS) ||
791                                     ((c_ptr->feat >= FEAT_DEEP_WATER) &&
792                                         (c_ptr->feat <= FEAT_TREES) &&
793                                      (c_ptr->feat != FEAT_MUSEUM)) ||
794                                     (x == 0) || (x == cur_wid-1) ||
795                                     (y == 0) || (y == cur_hgt-1))
796                                 {
797                                         /* Forget the grid */
798                                         c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
799                                 }
800                                 else if (c_ptr->feat == FEAT_ENTRANCE)
801                                 {
802                                         /* Assume lit */
803                                         c_ptr->info |= (CAVE_GLOW);
804
805                                         /* Hack -- Memorize lit grids if allowed */
806                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
807                                 }
808                         }
809                 }
810         }
811
812         if (p_ptr->teleport_town)
813         {
814                 for (y = 0; y < cur_hgt; y++)
815                 {
816                         for (x = 0; x < cur_wid; x++)
817                         {
818                                 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
819                                 {
820                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
821                                         p_ptr->oldpy = y;
822                                         p_ptr->oldpx = x;
823                                 }
824                         }
825                 }
826                 p_ptr->teleport_town = FALSE;
827         }
828
829         else if (p_ptr->leaving_dungeon)
830         {
831                 for (y = 0; y < cur_hgt; y++)
832                 {
833                         for (x = 0; x < cur_wid; x++)
834                         {
835                                 if (cave[y][x].feat == FEAT_ENTRANCE)
836                                 {
837                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
838                                         p_ptr->oldpy = y;
839                                         p_ptr->oldpx = x;
840                                 }
841                         }
842                 }
843                 p_ptr->teleport_town = FALSE;
844         }
845
846         player_place(p_ptr->oldpy, p_ptr->oldpx);
847         p_ptr->leftbldg = FALSE;
848         /* p_ptr->leaving_dungeon = FALSE;*/
849
850         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
851
852         /* Make some residents */
853         for (i = 0; i < lim; i++)
854         {
855                 /* Make a resident */
856                 (void)alloc_monster(generate_encounter ? 0 : 3, (bool)!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))));
857         }
858
859         if(generate_encounter) ambush_flag = TRUE;
860         generate_encounter = FALSE;
861
862         for (i = 0; i < 100; i++)
863         {
864                 floor_type[i] = FEAT_FLOOR;
865                 fill_type[i] = FEAT_WALL_EXTRA;
866         }
867
868         /* Set rewarded quests to finished */
869         for (i = 0; i < max_quests; i++)
870         {
871                 if (quest[i].status == QUEST_STATUS_REWARDED)
872                         quest[i].status = QUEST_STATUS_FINISHED;
873         }
874 }
875
876
877 /*
878  * Build the wilderness area.
879  * -DG-
880  */
881 void wilderness_gen_small()
882 {
883         int i, j;
884
885         /* To prevent stupid things */
886         for (i = 0; i < MAX_WID; i++)
887         for (j = 0; j < MAX_HGT; j++)
888         {
889                 cave[j][i].feat = FEAT_PERM_SOLID;
890         }
891
892         /* Init the wilderness */
893         process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
894
895         /* Fill the map */
896         for (i = 0; i < max_wild_x; i++)
897         for (j = 0; j < max_wild_y; j++)
898         {
899                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
900                 {
901                         cave[j][i].feat = FEAT_TOWN;
902                         cave[j][i].special = wilderness[j][i].town;
903                 }
904                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
905                 else if (wilderness[j][i].entrance && (total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
906                 {
907                         cave[j][i].feat = FEAT_ENTRANCE;
908                         cave[j][i].special = (byte)wilderness[j][i].entrance;
909                 }
910                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
911
912                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
913         }
914
915         cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
916         cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
917
918         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
919         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
920
921         /* Determine number of panels */
922         max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
923         max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
924
925         /* Assume illegal panel */
926         panel_row = max_panel_rows;
927         panel_col = max_panel_cols;
928
929         /* Place the player */
930         px = p_ptr->wilderness_x;
931         py = p_ptr->wilderness_y;
932
933         p_ptr->town_num = 0;
934 }
935
936
937 typedef struct wilderness_grid wilderness_grid;
938
939 struct wilderness_grid
940 {
941         int             terrain;    /* Terrain type */
942         int             town;       /* Town number */
943         s16b    level;          /* Level of the wilderness */
944         byte    road;       /* Road */
945         char    name[32];       /* Name of the town/wilderness */
946 };
947
948
949 static wilderness_grid w_letter[255];
950
951
952 /*
953  * Parse a sub-file of the "extra info"
954  */
955 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
956 {
957         int i, num;
958         char *zz[33];
959
960
961         /* Paranoia */
962         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
963
964         switch (buf[2])
965         {
966                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
967 #ifdef JP
968         case 'E':
969                 return 0;
970         case 'F':
971         case 'J':
972 #else
973         case 'J':
974                 return 0;
975         case 'F':
976         case 'E':
977 #endif
978         {
979                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
980                 {
981                         int index = zz[0][0];
982                         
983                         if (num > 1)
984                                 w_letter[index].terrain = atoi(zz[1]);
985                         else
986                                 w_letter[index].terrain = 0;
987                         
988                         if (num > 2)
989                                 w_letter[index].level = atoi(zz[2]);
990                         else
991                                 w_letter[index].level = 0;
992                         
993                         if (num > 3)
994                                 w_letter[index].town = atoi(zz[3]);
995                         else
996                                 w_letter[index].town = 0;
997                         
998                         if (num > 4)
999                                 w_letter[index].road = atoi(zz[4]);
1000                         else
1001                                 w_letter[index].road = 0;
1002                         
1003                         if (num > 5)
1004                                 strcpy(w_letter[index].name, zz[5]);
1005                         else
1006                                 w_letter[index].name[0] = 0;
1007                 }
1008                 else
1009                 {
1010                                 /* Failure */
1011                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1012                 }
1013                 
1014                 break;
1015         }
1016         
1017         /* Process "W:D:<layout> */
1018         /* Layout of the wilderness */
1019         case 'D':
1020         {
1021                 /* Acquire the text */
1022                 char *s = buf+4;
1023                 
1024                 /* Length of the text */
1025                 int len = strlen(s);
1026                 
1027                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1028                 {
1029                         int idx = s[0];
1030                         
1031                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1032                         
1033                         wilderness[*y][*x].level = w_letter[idx].level;
1034                         
1035                         wilderness[*y][*x].town = w_letter[idx].town;
1036                         
1037                         wilderness[*y][*x].road = w_letter[idx].road;
1038                         
1039                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1040                 }
1041                 
1042                 (*y)++;
1043                 
1044                 break;
1045         }
1046         
1047         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1048         case 'P':
1049         {
1050                 if ((p_ptr->wilderness_x == 0) &&
1051                     (p_ptr->wilderness_y == 0))
1052                 {
1053                         if (tokenize(buf+4, 2, zz, 0) == 2)
1054                         {
1055                                 p_ptr->wilderness_y = atoi(zz[0]);
1056                                 p_ptr->wilderness_x = atoi(zz[1]);
1057                                 
1058                                 if ((p_ptr->wilderness_x < 1) ||
1059                                     (p_ptr->wilderness_x > max_wild_x) ||
1060                                     (p_ptr->wilderness_y < 1) ||
1061                                     (p_ptr->wilderness_y > max_wild_y))
1062                                 {
1063                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1064                                 }
1065                         }
1066                         else
1067                         {
1068                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1069                         }
1070                 }
1071                 
1072                 break;
1073         }
1074         
1075         default:
1076                 /* Failure */
1077                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1078         }
1079         
1080         for (i = 1; i < max_d_idx; i++)
1081         {
1082                 if (!d_info[i].maxdepth) continue;
1083                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1084                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1085                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1086         }
1087
1088         /* Success */
1089         return (0);
1090 }
1091
1092
1093 /*
1094  * Generate the random seeds for the wilderness
1095  */
1096 void seed_wilderness(void)
1097 {
1098         int x, y;
1099
1100 #ifdef USE_SCRIPT
1101         if (!wilderness_init_callback())
1102 #endif /* USE_SCRIPT */
1103         {
1104                 /* Init wilderness seeds */
1105                 for (x = 0; x < max_wild_x; x++)
1106                 {
1107                         for (y = 0; y < max_wild_y; y++)
1108                         {
1109                                 wilderness[y][x].seed = rand_int(0x10000000);
1110                                 wilderness[y][x].entrance = 0;
1111                         }
1112                 }
1113         }
1114 }
1115
1116
1117 /*
1118  * Pointer to wilderness_type
1119  */
1120 typedef wilderness_type *wilderness_type_ptr;
1121
1122 /*
1123  * Initialize wilderness array
1124  */
1125 errr init_wilderness(void)
1126 {
1127         int i;
1128
1129         /* Allocate the wilderness (two-dimension array) */
1130         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1131         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1132
1133         /* Init the other pointers */
1134         for (i = 1; i < max_wild_y; i++)
1135                 wilderness[i] = wilderness[0] + i * max_wild_x;
1136
1137         generate_encounter = FALSE;
1138
1139         return 0;
1140 }
1141
1142
1143 bool change_wild_mode(void)
1144 {
1145         int i;
1146
1147         if (lite_town || vanilla_town)
1148         {
1149 #ifdef JP
1150                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1151 #else
1152                 msg_print("No global mal");
1153 #endif
1154                 return FALSE;
1155         }
1156         if (!p_ptr->wild_mode)
1157         {
1158                 for (i = 1; i < m_max; i++)
1159                 {
1160                         monster_type *m_ptr = &m_list[i];
1161
1162                         if (!m_ptr->r_idx) continue;
1163                         if (m_ptr->csleep) continue;
1164                         if (m_ptr->cdis > MAX_SIGHT) continue;
1165                         if (!is_hostile(m_ptr)) continue;
1166 #ifdef JP
1167                         msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1168 #else
1169                         msg_print("You cannot enter global map, since there is some monsters nearby!");
1170 #endif
1171                         energy_use = 0;
1172                         return FALSE;
1173                 }
1174                 energy_use = 1000;
1175         }
1176
1177         set_action(ACTION_NONE);
1178
1179         p_ptr->wild_mode = !p_ptr->wild_mode;
1180
1181         /* Leaving */
1182         p_ptr->leaving = TRUE;
1183
1184         return TRUE;
1185 }