OSDN Git Service

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