OSDN Git Service

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