OSDN Git Service

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