OSDN Git Service

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