OSDN Git Service

bcc0cee7a817b7d6ae7e3dd5de9920911105ba6a
[hengband/hengband.git] / src / floor-streams.c
1 /*!
2  * @file streams.c
3  * @brief ダンジョン生成に利用する関数群 / Used by dungeon generation.
4  * @date 2014/07/15
5  * @author
6  * <pre>
7  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
8  * This software may be copied and distributed for educational, research,
9  * and not for profit purposes provided that this copyright and statement
10  * are included in all such copies.  Other copyrights may also apply.
11  * </pre>
12  * @details
13  * Purpose:  This file holds all the
14  * functions that are applied to a level after the rest has been
15  * generated, ie streams and level destruction.
16  */
17
18 #include "angband.h"
19 #include "generate.h"
20 #include "floor-streams.h"
21 #include "grid.h"
22
23
24 /*!
25  * @brief 再帰フラクタルアルゴリズムによりダンジョン内に川を配置する /
26  * Recursive fractal algorithm to place water through the dungeon.
27  * @param x1 起点x座標
28  * @param y1 起点y座標
29  * @param x2 終点x座標
30  * @param y2 終点y座標
31  * @param feat1 中央部地形ID
32  * @param feat2 境界部地形ID
33  * @param width 基本幅
34  * @return なし
35  */
36 static void recursive_river(POSITION x1, POSITION y1, POSITION x2, POSITION y2, FEAT_IDX feat1, FEAT_IDX feat2, POSITION width)
37 {
38         POSITION dx, dy, length, l, x, y;
39         POSITION changex, changey;
40         POSITION ty, tx;
41         bool done;
42         cave_type *c_ptr;
43
44         length = distance(x1, y1, x2, y2);
45
46         if (length > 4)
47         {
48                 /*
49                  * Divide path in half and call routine twice.
50                  * There is a small chance of splitting the river
51                  */
52                 dx = (x2 - x1) / 2;
53                 dy = (y2 - y1) / 2;
54
55                 if (dy != 0)
56                 {
57                         /* perturbation perpendicular to path */
58                         changex = randint1(abs(dy)) * 2 - abs(dy);
59                 }
60                 else
61                 {
62                         changex = 0;
63                 }
64
65                 if (dx != 0)
66                 {
67                         /* perturbation perpendicular to path */
68                         changey = randint1(abs(dx)) * 2 - abs(dx);
69                 }
70                 else
71                 {
72                         changey = 0;
73                 }
74
75                 if (!in_bounds(y1 + dy + changey, x1 + dx + changex))
76                 {
77                         changex = 0;
78                         changey = 0;
79                 }
80
81                 /* construct river out of two smaller ones */
82                 recursive_river(x1, y1, x1 + dx + changex, y1 + dy + changey, feat1, feat2, width);
83                 recursive_river(x1 + dx + changex, y1 + dy + changey, x2, y2, feat1, feat2, width);
84
85                 /* Split the river some of the time - junctions look cool */
86                 if (one_in_(DUN_WAT_CHG) && (width > 0))
87                 {
88                         recursive_river(x1 + dx + changex, y1 + dy + changey,
89                                         x1 + 8 * (dx + changex), y1 + 8 * (dy + changey),
90                                         feat1, feat2, width - 1);
91                 }
92         }
93         else
94         {
95                 /* Actually build the river */
96                 for (l = 0; l < length; l++)
97                 {
98                         x = x1 + l * (x2 - x1) / length;
99                         y = y1 + l * (y2 - y1) / length;
100
101                         done = FALSE;
102
103                         while (!done)
104                         {
105                                 for (ty = y - width - 1; ty <= y + width + 1; ty++)
106                                 {
107                                         for (tx = x - width - 1; tx <= x + width + 1; tx++)
108                                         {
109                                                 if (!in_bounds2(ty, tx)) continue;
110
111                                                 c_ptr = &cave[ty][tx];
112
113                                                 if (c_ptr->feat == feat1) continue;
114                                                 if (c_ptr->feat == feat2) continue;
115
116                                                 if (distance(ty, tx, y, x) > rand_spread(width, 1)) continue;
117
118                                                 /* Do not convert permanent features */
119                                                 if (cave_perma_grid(c_ptr)) continue;
120
121                                                 /*
122                                                  * Clear previous contents, add feature
123                                                  * The border mainly gets feat2, while the center gets feat1
124                                                  */
125                                                 if (distance(ty, tx, y, x) > width)
126                                                         c_ptr->feat = feat2;
127                                                 else
128                                                         c_ptr->feat = feat1;
129
130                                                 /* Clear garbage of hidden trap or door */
131                                                 c_ptr->mimic = 0;
132
133                                                 /* Lava terrain glows */
134                                                 if (have_flag(f_info[feat1].flags, FF_LAVA))
135                                                 {
136                                                         if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) c_ptr->info |= CAVE_GLOW;
137                                                 }
138
139                                                 /* Hack -- don't teleport here */
140                                                 c_ptr->info |= CAVE_ICKY;
141                                         }
142                                 }
143
144                                 done = TRUE;
145                         }
146                 }
147         }
148 }
149
150
151 /*!
152  * @brief ランダムに川/溶岩流をダンジョンに配置する /
153  * Places water /lava through dungeon.
154  * @param feat1 中央部地形ID
155  * @param feat2 境界部地形ID
156  * @return なし
157  */
158 void add_river(IDX feat1, IDX feat2)
159 {
160         POSITION y2, x2;
161         POSITION y1 = 0, x1 = 0;
162         POSITION wid;
163
164
165         /* Hack -- Choose starting point */
166         y2 = randint1(cur_hgt / 2 - 2) + cur_hgt / 2;
167         x2 = randint1(cur_wid / 2 - 2) + cur_wid / 2;
168
169         /* Hack -- Choose ending point somewhere on boundary */
170         switch(randint1(4))
171         {
172                 case 1:
173                 {
174                         /* top boundary */
175                         x1 = randint1(cur_wid-2)+1;
176                         y1 = 1;
177                         break;
178                 }
179                 case 2:
180                 {
181                         /* left boundary */
182                         x1 = 1;
183                         y1 = randint1(cur_hgt-2)+1;
184                         break;
185                 }
186                 case 3:
187                 {
188                         /* right boundary */
189                         x1 = cur_wid-1;
190                         y1 = randint1(cur_hgt-2)+1;
191                         break;
192                 }
193                 case 4:
194                 {
195                         /* bottom boundary */
196                         x1 = randint1(cur_wid-2)+1;
197                         y1 = cur_hgt-1;
198                         break;
199                 }
200         }
201
202         wid = randint1(DUN_WAT_RNG);
203         recursive_river(x1, y1, x2, y2, feat1, feat2, wid);
204
205         /* Hack - Save the location as a "room" */
206         if (dun->cent_n < CENT_MAX)
207         {
208                 dun->cent[dun->cent_n].y = y2;
209                 dun->cent[dun->cent_n].x = x2;
210                 dun->cent_n++;
211         }
212 }
213
214
215 /*!
216  * @brief ダンジョンの壁部にストリーマー(地質の変化)を与える /
217  * Places "streamers" of rock through dungeon
218  * @param feat ストリーマー地形ID
219  * @param chance 生成密度
220  * @return なし
221  * @details
222  * <pre>
223  * Note that their are actually six different terrain features used
224  * to represent streamers.  Three each of magma and quartz, one for
225  * basic vein, one with hidden gold, and one with known gold.  The
226  * hidden gold types are currently unused.
227  * </pre>
228  */
229 void build_streamer(IDX feat, int chance)
230 {
231         int             i, tx, ty;
232         int             y, x, dir;
233         int dummy = 0;
234
235         cave_type *c_ptr;
236         feature_type *f_ptr;
237
238         feature_type *streamer_ptr = &f_info[feat];
239         bool streamer_is_wall = have_flag(streamer_ptr->flags, FF_WALL) && !have_flag(streamer_ptr->flags, FF_PERMANENT);
240         bool streamer_may_have_gold = have_flag(streamer_ptr->flags, FF_MAY_HAVE_GOLD);
241
242         /* Hack -- Choose starting point */
243         y = rand_spread(cur_hgt / 2, cur_hgt / 6);
244         x = rand_spread(cur_wid / 2, cur_wid / 6);
245
246         /* Choose a random compass direction */
247         dir = randint0(8);
248
249         /* Place streamer into dungeon */
250         while (dummy < SAFE_MAX_ATTEMPTS)
251         {
252                 dummy++;
253
254                 /* One grid per density */
255                 for (i = 0; i < DUN_STR_DEN; i++)
256                 {
257                         int d = DUN_STR_RNG;
258
259                         /* Pick a nearby grid */
260                         while (1)
261                         {
262                                 ty = rand_spread(y, d);
263                                 tx = rand_spread(x, d);
264                                 if (!in_bounds2(ty, tx)) continue;
265                                 break;
266                         }
267
268                         /* Access the grid */
269                         c_ptr = &cave[ty][tx];
270                         f_ptr = &f_info[c_ptr->feat];
271
272                         if (have_flag(f_ptr->flags, FF_MOVE) && (have_flag(f_ptr->flags, FF_WATER) || have_flag(f_ptr->flags, FF_LAVA)))
273                                 continue;
274
275                         /* Do not convert permanent features */
276                         if (have_flag(f_ptr->flags, FF_PERMANENT)) continue;
277
278                         /* Only convert "granite" walls */
279                         if (streamer_is_wall)
280                         {
281                                 if (!is_extra_grid(c_ptr) && !is_inner_grid(c_ptr) && !is_outer_grid(c_ptr) && !is_solid_grid(c_ptr)) continue;
282                                 if (is_closed_door(c_ptr->feat)) continue;
283                         }
284
285                         if (c_ptr->m_idx && !(have_flag(streamer_ptr->flags, FF_PLACE) && monster_can_cross_terrain(feat, &r_info[m_list[c_ptr->m_idx].r_idx], 0)))
286                         {
287                                 /* Delete the monster (if any) */
288                                 delete_monster(ty, tx);
289                         }
290
291                         if (c_ptr->o_idx && !have_flag(streamer_ptr->flags, FF_DROP))
292                         {
293                                 OBJECT_IDX this_o_idx, next_o_idx = 0;
294
295                                 /* Scan all objects in the grid */
296                                 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
297                                 {
298                                         /* Acquire object */
299                                         object_type *o_ptr = &o_list[this_o_idx];
300
301                                         /* Acquire next object */
302                                         next_o_idx = o_ptr->next_o_idx;
303
304                                         /* Hack -- Preserve unknown artifacts */
305                                         if (object_is_fixed_artifact(o_ptr))
306                                         {
307                                                 /* Mega-Hack -- Preserve the artifact */
308                                                 a_info[o_ptr->name1].cur_num = 0;
309
310                                                 if (cheat_peek)
311                                                 {
312                                                         char o_name[MAX_NLEN];
313                                                         object_desc(o_name, o_ptr, (OD_NAME_ONLY | OD_STORE));
314                                                         msg_format(_("伝説のアイテム (%s) はストリーマーにより削除された。",
315                                                                 "Artifact (%s) was deleted by streamer."), o_name);
316                                                 }
317                                         }
318                                         else if (cheat_peek && o_ptr->art_name)
319                                         {
320                                                 msg_print(_("ランダム・アーティファクトの1つはストリーマーにより削除された。",
321                                                         "One of the random artifacts was deleted by streamer."));
322                                         }
323                                 }
324
325                                 /* Delete objects */
326                                 delete_object(ty, tx);
327                         }
328
329                         /* Clear previous contents, add proper vein type */
330                         c_ptr->feat = feat;
331
332                         /* Paranoia: Clear mimic field */
333                         c_ptr->mimic = 0;
334
335                         if (streamer_may_have_gold)
336                         {
337                                 /* Hack -- Add some known treasure */
338                                 if (one_in_(chance))
339                                 {
340                                         cave_alter_feat(ty, tx, FF_MAY_HAVE_GOLD);
341                                 }
342
343                                 /* Hack -- Add some hidden treasure */
344                                 else if (one_in_(chance / 4))
345                                 {
346                                         cave_alter_feat(ty, tx, FF_MAY_HAVE_GOLD);
347                                         cave_alter_feat(ty, tx, FF_ENSECRET);
348                                 }
349                         }
350                 }
351
352                 if (dummy >= SAFE_MAX_ATTEMPTS)
353                 {
354                         msg_print_wizard(CHEAT_DUNGEON, _("地形のストリーマー処理に失敗しました。", "Failed to place streamer."));
355                         return;
356                 }
357
358                 /* Advance the streamer */
359                 y += ddy[cdd[dir]];
360                 x += ddx[cdd[dir]];
361
362                 if(one_in_(10))
363                 {
364                         if(one_in_(2)) dir = (dir + 1) % 8;
365                         else dir = (dir > 0) ? dir - 1 : 7; 
366                 }
367
368                 /* Quit before leaving the dungeon */
369                 if (!in_bounds(y, x)) break;
370         }
371 }
372
373
374 /*!
375  * @brief ダンジョンの指定位置近辺に森林を配置する /
376  * Places "streamers" of rock through dungeon
377  * @param x 指定X座標
378  * @param y 指定Y座標
379  * @return なし
380  * @details
381  * <pre>
382  * Put trees near a hole in the dungeon roof  (rubble on ground + up stairway)
383  * This happens in real world lava tubes.
384  * </pre>
385  */
386 void place_trees(POSITION x, POSITION y)
387 {
388         int i, j;
389         cave_type *c_ptr;
390
391         /* place trees/ rubble in ovalish distribution */
392         for (i = x - 3; i < x + 4; i++)
393         {
394                 for (j = y - 3; j < y + 4; j++)
395                 {
396                         if (!in_bounds(j, i)) continue;
397                         c_ptr = &cave[j][i];
398
399                         if (c_ptr->info & CAVE_ICKY) continue;
400                         if (c_ptr->o_idx) continue;
401
402                         /* Want square to be in the circle and accessable. */
403                         if ((distance(j, i, y, x) < 4) && !cave_perma_grid(c_ptr))
404                         {
405                                 /*
406                                  * Clear previous contents, add feature
407                                  * The border mainly gets trees, while the center gets rubble
408                                  */
409                                 if ((distance(j, i, y, x) > 1) || (randint1(100) < 25))
410                                 {
411                                         if (randint1(100) < 75)
412                                                 cave[j][i].feat = feat_tree;
413                                 }
414                                 else
415                                 {
416                                         cave[j][i].feat = feat_rubble;
417                                 }
418
419                                 /* Clear garbage of hidden trap or door */
420                                 c_ptr->mimic = 0;
421
422                                 /* Light area since is open above */
423                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[j][i].info |= (CAVE_GLOW | CAVE_ROOM);
424                         }
425                 }
426         }
427
428         /* No up stairs in ironman mode */
429         if (!ironman_downward && one_in_(3))
430         {
431                 /* up stair */
432                 cave[y][x].feat = feat_up_stair;
433         }
434 }
435
436
437 /*!
438  * @brief ダンジョンに*破壊*済み地形ランダムに施す /
439  * Build a destroyed level
440  * @return なし
441  */
442 void destroy_level(void)
443 {
444         POSITION y1, x1;
445         int n;
446
447         /* Note destroyed levels */
448         msg_print_wizard(CHEAT_DUNGEON, _("階に*破壊*の痕跡を生成しました。", "Destroyed Level."));
449
450         /* Drop a few epi-centers (usually about two) */
451         for (n = 0; n < randint1(5); n++)
452         {
453                 /* Pick an epi-center */
454                 x1 = rand_range(5, cur_wid - 1 - 5);
455                 y1 = rand_range(5, cur_hgt - 1 - 5);
456
457                 (void)destroy_area(y1, x1, 15, TRUE);
458         }
459 }