OSDN Git Service

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