OSDN Git Service

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