OSDN Git Service

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