OSDN Git Service

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