OSDN Git Service

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