OSDN Git Service

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