OSDN Git Service

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