OSDN Git Service

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