OSDN Git Service

無駄に残ってソース汚しになっていたPython関係のコードを削除。script.cと、z-config.hの中のUSE_SCRIPT辺りの記述は一応残している。
[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
532         /* Create the town */
533         if (p_ptr->town_num)
534         {
535                 /* Reset the buildings */
536                 init_buildings();
537
538                 /* Initialize the town */
539                 if (border | corner)
540                         init_flags = INIT_CREATE_DUNGEON | INIT_ONLY_FEATURES;
541                 else
542                         init_flags = INIT_CREATE_DUNGEON;
543
544                 process_dungeon_file("t_info_j.txt", 0, 0, MAX_HGT, MAX_WID);
545
546                 if (!corner && !border) p_ptr->visit |= (1L << (p_ptr->town_num - 1));
547         }
548         else
549         {
550                 int terrain = wilderness[y][x].terrain;
551                 u32b seed = wilderness[y][x].seed;
552
553                 generate_wilderness_area(terrain, seed, border, corner);
554         }
555
556         if (!corner && !wilderness[y][x].town)
557         {
558                 /*
559                  * Place roads in the wilderness
560                  * ToDo: make the road a bit more interresting
561                  */
562                 if (wilderness[y][x].road)
563                 {
564                         cave[MAX_HGT/2][MAX_WID/2].feat = FEAT_FLOOR;
565
566                         if (wilderness[y-1][x].road)
567                         {
568                                 /* North road */
569                                 for (y1 = 1; y1 < MAX_HGT/2; y1++)
570                                 {
571                                         x1 = MAX_WID/2;
572                                         cave[y1][x1].feat = FEAT_FLOOR;
573                                 }
574                         }
575
576                         if (wilderness[y+1][x].road)
577                         {
578                                 /* North road */
579                                 for (y1 = MAX_HGT/2; y1 < MAX_HGT - 1; y1++)
580                                 {
581                                         x1 = MAX_WID/2;
582                                         cave[y1][x1].feat = FEAT_FLOOR;
583                                 }
584                         }
585
586                         if (wilderness[y][x+1].road)
587                         {
588                                 /* East road */
589                                 for (x1 = MAX_WID/2; x1 < MAX_WID - 1; x1++)
590                                 {
591                                         y1 = MAX_HGT/2;
592                                         cave[y1][x1].feat = FEAT_FLOOR;
593                                 }
594                         }
595
596                         if (wilderness[y][x-1].road)
597                         {
598                                 /* West road */
599                                 for (x1 = 1; x1 < MAX_WID/2; x1++)
600                                 {
601                                         y1 = MAX_HGT/2;
602                                         cave[y1][x1].feat = FEAT_FLOOR;
603                                 }
604                         }
605                 }
606         }
607
608         if (wilderness[y][x].entrance && !wilderness[y][x].town && (total_winner || !(d_info[wilderness[y][x].entrance].flags1 & DF1_WINNER)))
609         {
610                 int dy, dx;
611
612                 /* Hack -- Use the "simple" RNG */
613                 Rand_quick = TRUE;
614
615                 /* Hack -- Induce consistant town layout */
616                 Rand_value = wilderness[y][x].seed;
617
618                 dy = rand_range(6, cur_hgt - 6);
619                 dx = rand_range(6, cur_wid - 6);
620
621                 cave[dy][dx].feat = FEAT_ENTRANCE;
622                 cave[dy][dx].special = (byte)wilderness[y][x].entrance;
623
624                 /* Use the complex RNG */
625                 Rand_quick = FALSE;
626         }
627 }
628
629
630 /*
631  * Border of the wilderness area
632  */
633 static border_type border;
634
635
636 /*
637  * Build the wilderness area outside of the town.
638  */
639 void wilderness_gen(void)
640 {
641         int i, y, x, lim;
642         bool daytime;
643         cave_type *c_ptr;
644
645         /* Big town */
646         cur_hgt = MAX_HGT;
647         cur_wid = MAX_WID;
648
649         /* Determine number of panels */
650         max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
651         max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
652
653         /* Assume illegal panel */
654         panel_row = max_panel_rows;
655         panel_col = max_panel_cols;
656
657         /* Init the wilderness */
658
659         process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
660
661         x = p_ptr->wilderness_x;
662         y = p_ptr->wilderness_y;
663
664         /* Prepare allocation table */
665         get_mon_num_prep(get_monster_hook(), NULL);
666
667         /* North border */
668         generate_area(y - 1, x, TRUE, FALSE);
669
670         for (i = 1; i < MAX_WID - 1; i++)
671         {
672                 border.north[i] = cave[MAX_HGT - 2][i].feat;
673         }
674
675         /* South border */
676         generate_area(y + 1, x, TRUE, FALSE);
677
678         for (i = 1; i < MAX_WID - 1; i++)
679         {
680                 border.south[i] = cave[1][i].feat;
681         }
682
683         /* West border */
684         generate_area(y, x - 1, TRUE, FALSE);
685
686         for (i = 1; i < MAX_HGT - 1; i++)
687         {
688                 border.west[i] = cave[i][MAX_WID - 2].feat;
689         }
690
691         /* East border */
692         generate_area(y, x + 1, TRUE, FALSE);
693
694         for (i = 1; i < MAX_HGT - 1; i++)
695         {
696                 border.east[i] = cave[i][1].feat;
697         }
698
699         /* North west corner */
700         generate_area(y - 1, x - 1, FALSE, TRUE);
701         border.north_west = cave[MAX_HGT - 2][MAX_WID - 2].feat;
702
703         /* North east corner */
704         generate_area(y - 1, x + 1, FALSE, TRUE);
705         border.north_east = cave[MAX_HGT - 2][1].feat;
706
707         /* South west corner */
708         generate_area(y + 1, x - 1, FALSE, TRUE);
709         border.south_west = cave[1][MAX_WID - 2].feat;
710
711         /* South east corner */
712         generate_area(y + 1, x + 1, FALSE, TRUE);
713         border.south_east = cave[1][1].feat;
714
715
716         /* Create terrain of the current area */
717         generate_area(y, x, FALSE, FALSE);
718
719
720         /* Special boundary walls -- North */
721         for (i = 0; i < MAX_WID; i++)
722         {
723                 cave[0][i].feat = FEAT_PERM_SOLID;
724                 cave[0][i].mimic = border.north[i];
725         }
726
727         /* Special boundary walls -- South */
728         for (i = 0; i < MAX_WID; i++)
729         {
730                 cave[MAX_HGT - 1][i].feat = FEAT_PERM_SOLID;
731                 cave[MAX_HGT - 1][i].mimic = border.south[i];
732         }
733
734         /* Special boundary walls -- West */
735         for (i = 0; i < MAX_HGT; i++)
736         {
737                 cave[i][0].feat = FEAT_PERM_SOLID;
738                 cave[i][0].mimic = border.west[i];
739         }
740
741         /* Special boundary walls -- East */
742         for (i = 0; i < MAX_HGT; i++)
743         {
744                 cave[i][MAX_WID - 1].feat = FEAT_PERM_SOLID;
745                 cave[i][MAX_WID - 1].mimic = border.east[i];
746         }
747
748         /* North west corner */
749         cave[0][0].mimic = border.north_west;
750
751         /* North east corner */
752         cave[0][MAX_WID - 1].mimic = border.north_east;
753
754         /* South west corner */
755         cave[MAX_HGT - 1][0].mimic = border.south_west;
756
757         /* South east corner */
758         cave[MAX_HGT - 1][MAX_WID - 1].mimic = border.south_east;
759
760
761         /* Day time */
762         if ((turn % (20L * TOWN_DAWN)) < ((20L * TOWN_DAWN) / 2))
763                 daytime = TRUE;
764         else
765                 daytime = FALSE;
766
767         /* Light up or darken the area */
768         for (y = 0; y < cur_hgt; y++)
769         {
770                 for (x = 0; x < cur_wid; x++)
771                 {
772                         /* Get the cave grid */
773                         c_ptr = &cave[y][x];
774
775                         if (daytime)
776                         {
777                                 /* Assume lit */
778                                 c_ptr->info |= (CAVE_GLOW);
779
780                                 /* Hack -- Memorize lit grids if allowed */
781                                 if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
782                         }
783                         else
784                         {
785                                 /* Darken "boring" features */
786                                 if ((c_ptr->feat <= FEAT_INVIS) ||
787                                     ((c_ptr->feat >= FEAT_DEEP_WATER) &&
788                                         (c_ptr->feat <= FEAT_TREES) &&
789                                      (c_ptr->feat != FEAT_MUSEUM)) ||
790                                     (x == 0) || (x == cur_wid-1) ||
791                                     (y == 0) || (y == cur_hgt-1))
792                                 {
793                                         /* Forget the grid */
794                                         c_ptr->info &= ~(CAVE_GLOW | CAVE_MARK);
795                                 }
796                                 else if (c_ptr->feat == FEAT_ENTRANCE)
797                                 {
798                                         /* Assume lit */
799                                         c_ptr->info |= (CAVE_GLOW);
800
801                                         /* Hack -- Memorize lit grids if allowed */
802                                         if (view_perma_grids) c_ptr->info |= (CAVE_MARK);
803                                 }
804                         }
805                 }
806         }
807
808         if (p_ptr->teleport_town)
809         {
810                 for (y = 0; y < cur_hgt; y++)
811                 {
812                         for (x = 0; x < cur_wid; x++)
813                         {
814                                 if (((cave[y][x].feat - FEAT_BLDG_HEAD) == 4) || ((p_ptr->town_num == 1) && ((cave[y][x].feat - FEAT_BLDG_HEAD) == 0)))
815                                 {
816                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
817                                         p_ptr->oldpy = y;
818                                         p_ptr->oldpx = x;
819                                 }
820                         }
821                 }
822                 p_ptr->teleport_town = FALSE;
823         }
824
825         else if (p_ptr->leaving_dungeon)
826         {
827                 for (y = 0; y < cur_hgt; y++)
828                 {
829                         for (x = 0; x < cur_wid; x++)
830                         {
831                                 if (cave[y][x].feat == FEAT_ENTRANCE)
832                                 {
833                                         if (cave[y][x].m_idx) delete_monster_idx(cave[y][x].m_idx);
834                                         p_ptr->oldpy = y;
835                                         p_ptr->oldpx = x;
836                                 }
837                         }
838                 }
839                 p_ptr->teleport_town = FALSE;
840         }
841
842         player_place(p_ptr->oldpy, p_ptr->oldpx);
843         p_ptr->leftbldg = FALSE;
844         /* p_ptr->leaving_dungeon = FALSE;*/
845
846         lim = (generate_encounter==TRUE)?40:MIN_M_ALLOC_TN;
847
848         /* Make some residents */
849         for (i = 0; i < lim; i++)
850         {
851                 /* Make a resident */
852                 (void)alloc_monster(generate_encounter ? 0 : 3, (bool)!(generate_encounter || (one_in_(2) && (!p_ptr->town_num))));
853         }
854
855         if(generate_encounter) ambush_flag = TRUE;
856         generate_encounter = FALSE;
857
858         for (i = 0; i < 100; i++)
859         {
860                 floor_type[i] = FEAT_FLOOR;
861                 fill_type[i] = FEAT_WALL_EXTRA;
862         }
863
864         /* Set rewarded quests to finished */
865         for (i = 0; i < max_quests; i++)
866         {
867                 if (quest[i].status == QUEST_STATUS_REWARDED)
868                         quest[i].status = QUEST_STATUS_FINISHED;
869         }
870 }
871
872
873 /*
874  * Build the wilderness area.
875  * -DG-
876  */
877 void wilderness_gen_small()
878 {
879         int i, j;
880
881         /* To prevent stupid things */
882         for (i = 0; i < MAX_WID; i++)
883         for (j = 0; j < MAX_HGT; j++)
884         {
885                 cave[j][i].feat = FEAT_PERM_SOLID;
886         }
887
888         /* Init the wilderness */
889         process_dungeon_file("w_info_j.txt", 0, 0, max_wild_y, max_wild_x);
890
891         /* Fill the map */
892         for (i = 0; i < max_wild_x; i++)
893         for (j = 0; j < max_wild_y; j++)
894         {
895                 if (wilderness[j][i].town && (wilderness[j][i].town != NO_TOWN))
896                 {
897                         cave[j][i].feat = FEAT_TOWN;
898                         cave[j][i].special = wilderness[j][i].town;
899                 }
900                 else if (wilderness[j][i].road) cave[j][i].feat = FEAT_FLOOR;
901                 else if (wilderness[j][i].entrance && (total_winner || !(d_info[wilderness[j][i].entrance].flags1 & DF1_WINNER)))
902                 {
903                         cave[j][i].feat = FEAT_ENTRANCE;
904                         cave[j][i].special = (byte)wilderness[j][i].entrance;
905                 }
906                 else cave[j][i].feat = conv_terrain2feat[wilderness[j][i].terrain];
907
908                 cave[j][i].info |= (CAVE_GLOW | CAVE_MARK);
909         }
910
911         cur_hgt = (max_wild_y / SCREEN_HGT + 1) * SCREEN_HGT;
912         cur_wid = (max_wild_x / SCREEN_WID + 1) * SCREEN_WID;
913
914         if (cur_hgt > MAX_HGT) cur_hgt = MAX_HGT;
915         if (cur_wid > MAX_WID) cur_wid = MAX_WID;
916
917         /* Determine number of panels */
918         max_panel_rows = (cur_hgt / SCREEN_HGT) * 2 - 2;
919         max_panel_cols = (cur_wid / SCREEN_WID) * 2 - 2;
920
921         /* Assume illegal panel */
922         panel_row = max_panel_rows;
923         panel_col = max_panel_cols;
924
925         /* Place the player */
926         px = p_ptr->wilderness_x;
927         py = p_ptr->wilderness_y;
928
929         p_ptr->town_num = 0;
930 }
931
932
933 typedef struct wilderness_grid wilderness_grid;
934
935 struct wilderness_grid
936 {
937         int             terrain;    /* Terrain type */
938         int             town;       /* Town number */
939         s16b    level;          /* Level of the wilderness */
940         byte    road;       /* Road */
941         char    name[32];       /* Name of the town/wilderness */
942 };
943
944
945 static wilderness_grid w_letter[255];
946
947
948 /*
949  * Parse a sub-file of the "extra info"
950  */
951 errr parse_line_wilderness(char *buf, int ymin, int xmin, int ymax, int xmax, int *y, int *x)
952 {
953         int i, num;
954         char *zz[33];
955
956
957         /* Paranoia */
958         if (!(buf[0] == 'W')) return (PARSE_ERROR_GENERIC);
959
960         switch (buf[2])
961         {
962                 /* Process "W:F:<letter>:<terrain>:<town>:<road>:<name> */
963 #ifdef JP
964         case 'E':
965                 return 0;
966         case 'F':
967         case 'J':
968 #else
969         case 'J':
970                 return 0;
971         case 'F':
972         case 'E':
973 #endif
974         {
975                 if ((num = tokenize(buf+4, 6, zz, 0)) > 1)
976                 {
977                         int index = zz[0][0];
978                         
979                         if (num > 1)
980                                 w_letter[index].terrain = atoi(zz[1]);
981                         else
982                                 w_letter[index].terrain = 0;
983                         
984                         if (num > 2)
985                                 w_letter[index].level = atoi(zz[2]);
986                         else
987                                 w_letter[index].level = 0;
988                         
989                         if (num > 3)
990                                 w_letter[index].town = atoi(zz[3]);
991                         else
992                                 w_letter[index].town = 0;
993                         
994                         if (num > 4)
995                                 w_letter[index].road = atoi(zz[4]);
996                         else
997                                 w_letter[index].road = 0;
998                         
999                         if (num > 5)
1000                                 strcpy(w_letter[index].name, zz[5]);
1001                         else
1002                                 w_letter[index].name[0] = 0;
1003                 }
1004                 else
1005                 {
1006                                 /* Failure */
1007                         return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1008                 }
1009                 
1010                 break;
1011         }
1012         
1013         /* Process "W:D:<layout> */
1014         /* Layout of the wilderness */
1015         case 'D':
1016         {
1017                 /* Acquire the text */
1018                 char *s = buf+4;
1019                 
1020                 /* Length of the text */
1021                 int len = strlen(s);
1022                 
1023                 for (*x = xmin, i = 0; ((*x < xmax) && (i < len)); (*x)++, s++, i++)
1024                 {
1025                         int idx = s[0];
1026                         
1027                         wilderness[*y][*x].terrain = w_letter[idx].terrain;
1028                         
1029                         wilderness[*y][*x].level = w_letter[idx].level;
1030                         
1031                         wilderness[*y][*x].town = w_letter[idx].town;
1032                         
1033                         wilderness[*y][*x].road = w_letter[idx].road;
1034                         
1035                         strcpy(town[w_letter[idx].town].name, w_letter[idx].name);
1036                 }
1037                 
1038                 (*y)++;
1039                 
1040                 break;
1041         }
1042         
1043         /* Process "W:P:<x>:<y> - starting position in the wilderness */
1044         case 'P':
1045         {
1046                 if ((p_ptr->wilderness_x == 0) &&
1047                     (p_ptr->wilderness_y == 0))
1048                 {
1049                         if (tokenize(buf+4, 2, zz, 0) == 2)
1050                         {
1051                                 p_ptr->wilderness_y = atoi(zz[0]);
1052                                 p_ptr->wilderness_x = atoi(zz[1]);
1053                                 
1054                                 if ((p_ptr->wilderness_x < 1) ||
1055                                     (p_ptr->wilderness_x > max_wild_x) ||
1056                                     (p_ptr->wilderness_y < 1) ||
1057                                     (p_ptr->wilderness_y > max_wild_y))
1058                                 {
1059                                         return (PARSE_ERROR_OUT_OF_BOUNDS);
1060                                 }
1061                         }
1062                         else
1063                         {
1064                                 return (PARSE_ERROR_TOO_FEW_ARGUMENTS);
1065                         }
1066                 }
1067                 
1068                 break;
1069         }
1070         
1071         default:
1072                 /* Failure */
1073                 return (PARSE_ERROR_UNDEFINED_DIRECTIVE);
1074         }
1075         
1076         for (i = 1; i < max_d_idx; i++)
1077         {
1078                 if (!d_info[i].maxdepth) continue;
1079                 wilderness[d_info[i].dy][d_info[i].dx].entrance = i;
1080                 if (!wilderness[d_info[i].dy][d_info[i].dx].town)
1081                         wilderness[d_info[i].dy][d_info[i].dx].level = d_info[i].mindepth;
1082         }
1083
1084         /* Success */
1085         return (0);
1086 }
1087
1088
1089 /*
1090  * Generate the random seeds for the wilderness
1091  */
1092 void seed_wilderness(void)
1093 {
1094         int x, y;
1095
1096         /* Init wilderness seeds */
1097         for (x = 0; x < max_wild_x; x++)
1098         {
1099                 for (y = 0; y < max_wild_y; y++)
1100                 {
1101                         wilderness[y][x].seed = rand_int(0x10000000);
1102                         wilderness[y][x].entrance = 0;
1103                 }
1104         }
1105 }
1106
1107
1108 /*
1109  * Pointer to wilderness_type
1110  */
1111 typedef wilderness_type *wilderness_type_ptr;
1112
1113 /*
1114  * Initialize wilderness array
1115  */
1116 errr init_wilderness(void)
1117 {
1118         int i;
1119
1120         /* Allocate the wilderness (two-dimension array) */
1121         C_MAKE(wilderness, max_wild_y, wilderness_type_ptr);
1122         C_MAKE(wilderness[0], max_wild_x * max_wild_y, wilderness_type);
1123
1124         /* Init the other pointers */
1125         for (i = 1; i < max_wild_y; i++)
1126                 wilderness[i] = wilderness[0] + i * max_wild_x;
1127
1128         generate_encounter = FALSE;
1129
1130         return 0;
1131 }
1132
1133
1134 bool change_wild_mode(void)
1135 {
1136         int i;
1137
1138         if (lite_town || vanilla_town)
1139         {
1140 #ifdef JP
1141                 msg_print("¹ÓÌî¤Ê¤ó¤Æ¤Ê¤¤¡£");
1142 #else
1143                 msg_print("No global mal");
1144 #endif
1145                 return FALSE;
1146         }
1147         if (!p_ptr->wild_mode)
1148         {
1149                 for (i = 1; i < m_max; i++)
1150                 {
1151                         monster_type *m_ptr = &m_list[i];
1152
1153                         if (!m_ptr->r_idx) continue;
1154                         if (m_ptr->csleep) continue;
1155                         if (m_ptr->cdis > MAX_SIGHT) continue;
1156                         if (!is_hostile(m_ptr)) continue;
1157 #ifdef JP
1158                         msg_print("Ũ¤¬¤¹¤°¶á¤¯¤Ë¤¤¤ë¤È¤­¤Ï¹­°è¥Þ¥Ã¥×¤ËÆþ¤ì¤Ê¤¤¡ª");
1159 #else
1160                         msg_print("You cannot enter global map, since there is some monsters nearby!");
1161 #endif
1162                         energy_use = 0;
1163                         return FALSE;
1164                 }
1165                 energy_use = 1000;
1166         }
1167
1168         set_action(ACTION_NONE);
1169
1170         p_ptr->wild_mode = !p_ptr->wild_mode;
1171
1172         /* Leaving */
1173         p_ptr->leaving = TRUE;
1174
1175         return TRUE;
1176 }