OSDN Git Service

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