OSDN Git Service

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