OSDN Git Service

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