OSDN Git Service

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