OSDN Git Service

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