OSDN Git Service

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