OSDN Git Service

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