OSDN Git Service

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