OSDN Git Service

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