OSDN Git Service

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