OSDN Git Service

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