OSDN Git Service

ナミマソソホソィエヨー网、、ホス、タオ。」
[hengband/hengband.git] / src / streams.c
1 /*
2  * File: streams.c
3  * Purpose: Used by dungeon generation. This file holds all the
4  * functions that are applied to a level after the rest has been
5  * generated, ie streams and level destruction.
6  */
7
8 /*
9  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
10  *
11  * This software may be copied and distributed for educational, research, and
12  * not for profit purposes provided that this copyright and statement are
13  * included in all such copies.
14  */
15
16 #include "angband.h"
17 #include "generate.h"
18 #include "streams.h"
19 #include "grid.h"
20
21
22 /*
23  * Recursive fractal algorithm to place water through the dungeon.
24  */
25 static void recursive_river(int x1, int y1, int x2, int y2, int feat1, int feat2, int width)
26 {
27         int dx, dy, length, l, x, y;
28         int changex, changey;
29         int ty, tx;
30         bool done;
31         cave_type *c_ptr;
32
33         length = distance(x1, y1, x2, y2);
34
35         if (length > 4)
36         {
37                 /*
38                  * Divide path in half and call routine twice.
39                  * There is a small chance of splitting the river
40                  */
41                 dx = (x2 - x1) / 2;
42                 dy = (y2 - y1) / 2;
43
44                 if (dy != 0)
45                 {
46                         /* perturbation perpendicular to path */
47                         changex = randint(abs(dy)) * 2 - abs(dy);
48                 }
49                 else
50                 {
51                         changex = 0;
52                 }
53
54                 if (dx != 0)
55                 {
56                         /* perturbation perpendicular to path */
57                         changey = randint(abs(dx)) * 2 - abs(dx);
58                 }
59                 else
60                 {
61                         changey = 0;
62                 }
63
64                 if (!in_bounds(y1 + dy + changey, x1 + dx + changex))
65                 {
66                         changex = 0;
67                         changey = 0;
68                 }
69
70                 /* construct river out of two smaller ones */
71                 recursive_river(x1, y1, x1 + dx + changex, y1 + dy + changey, feat1, feat2, width);
72                 recursive_river(x1 + dx + changex, y1 + dy + changey, x2, y2, feat1, feat2, width);
73
74                 /* Split the river some of the time - junctions look cool */
75                 if ((randint(DUN_WAT_CHG) == 1) && (width > 0))
76                 {
77                         recursive_river(x1 + dx + changex, y1 + dy + changey,
78                                         x1 + 8 * (dx + changex), y1 + 8 * (dy + changey),
79                                         feat1, feat2, width - 1);
80                 }
81         }
82         else
83         {
84                 /* Actually build the river */
85                 for (l = 0; l < length; l++)
86                 {
87                         x = x1 + l * (x2 - x1) / length;
88                         y = y1 + l * (y2 - y1) / length;
89
90                         done = FALSE;
91
92                         while (!done)
93                         {
94                                 for (ty = y - width - 1; ty <= y + width + 1; ty++)
95                                 {
96                                         for (tx = x - width - 1; tx <= x + width + 1; tx++)
97                                         {
98                                                 if (!in_bounds(ty, tx)) continue;
99
100                                                 c_ptr = &cave[ty][tx];
101
102                                                 if (c_ptr->feat == feat1) continue;
103                                                 if (c_ptr->feat == feat2) continue;
104
105                                                 if (distance(ty, tx, y, x) > rand_spread(width, 1)) continue;
106
107                                                 /* Do not convert permanent features */
108                                                 if (cave_perma_grid(c_ptr) && (c_ptr->feat != FEAT_MOUNTAIN)) continue;
109
110                                                 /*
111                                                  * Clear previous contents, add feature
112                                                  * The border mainly gets feat2, while the center gets feat1
113                                                  */
114                                                 if (distance(ty, tx, y, x) > width)
115                                                         c_ptr->feat = feat2;
116                                                 else
117                                                         c_ptr->feat = feat1;
118
119                                                 /* Lava terrain glows */
120                                                 if ((feat1 == FEAT_DEEP_LAVA) ||  (feat1 == FEAT_SHAL_LAVA))
121                                                 {
122                                                         c_ptr->info |= CAVE_GLOW;
123                                                 }
124
125                                                 /* Hack -- don't teleport here */
126                                                 c_ptr->info |= CAVE_ICKY;
127                                         }
128                                 }
129
130                                 done = TRUE;
131                         }
132                 }
133         }
134 }
135
136
137 /*
138  * Places water /lava through dungeon.
139  */
140 void add_river(int feat1, int feat2)
141 {
142         int y2, x2;
143         int y1 = 0, x1 = 0;
144         int wid;
145
146
147         /* Hack -- Choose starting point */
148         y2 = randint(cur_hgt / 2 - 2) + cur_hgt / 2;
149         x2 = randint(cur_wid / 2 - 2) + cur_wid / 2;
150
151         /* Hack -- Choose ending point somewhere on boundary */
152         switch(randint(4))
153         {
154                 case 1:
155                 {
156                         /* top boundary */
157                         x1 = randint(cur_wid-2)+1;
158                         y1 = 1;
159                         break;
160                 }
161                 case 2:
162                 {
163                         /* left boundary */
164                         x1 = 1;
165                         y1 = randint(cur_hgt-2)+1;
166                         break;
167                 }
168                 case 3:
169                 {
170                         /* right boundary */
171                         x1 = cur_wid-1;
172                         y1 = randint(cur_hgt-2)+1;
173                         break;
174                 }
175                 case 4:
176                 {
177                         /* bottom boundary */
178                         x1 = randint(cur_wid-2)+1;
179                         y1 = cur_hgt-1;
180                         break;
181                 }
182         }
183
184         wid = randint(DUN_WAT_RNG);
185         recursive_river(x1, y1, x2, y2, feat1, feat2, wid);
186
187         /* Hack - Save the location as a "room" */
188         if (dun->cent_n < CENT_MAX)
189         {
190                 dun->cent[dun->cent_n].y = y2;
191                 dun->cent[dun->cent_n].x = x2;
192                 dun->cent_n++;
193         }
194 }
195
196
197 /*
198  * Places "streamers" of rock through dungeon
199  *
200  * Note that their are actually six different terrain features used
201  * to represent streamers.  Three each of magma and quartz, one for
202  * basic vein, one with hidden gold, and one with known gold.  The
203  * hidden gold types are currently unused.
204  */
205 void build_streamer(int feat, int chance)
206 {
207         int             i, tx, ty;
208         int             y, x, dir;
209         int dummy = 0;
210         bool treasure = FALSE;
211
212         cave_type *c_ptr;
213
214         /* Hack -- Choose starting point */
215         y = rand_spread(cur_hgt / 2, 10);
216         x = rand_spread(cur_wid / 2, 15);
217
218         /* Choose a random compass direction */
219         dir = ddd[rand_int(8)];
220
221         /* Place streamer into dungeon */
222         while (dummy < SAFE_MAX_ATTEMPTS)
223         {
224                 dummy++;
225
226                 /* One grid per density */
227                 for (i = 0; i < DUN_STR_DEN; i++)
228                 {
229                         int d = DUN_STR_RNG;
230
231                         /* Pick a nearby grid */
232                         while (1)
233                         {
234                                 ty = rand_spread(y, d);
235                                 tx = rand_spread(x, d);
236                                 if (!in_bounds(ty, tx)) continue;
237                                 break;
238                         }
239
240                         /* Access the grid */
241                         c_ptr = &cave[ty][tx];
242
243                         if ((c_ptr->feat >= FEAT_DEEP_WATER) && (c_ptr->feat <= FEAT_SHAL_LAVA)) continue;
244                         if ((c_ptr->feat >= FEAT_PERM_EXTRA) && (c_ptr->feat <= FEAT_PERM_SOLID)) continue;
245
246                         /* Only convert "granite" walls */
247                         if ((feat >= FEAT_MAGMA) && (feat <= FEAT_WALL_SOLID))
248                         {
249                                 if (!is_extra_grid(c_ptr) && !is_inner_grid(c_ptr) && !is_outer_grid(c_ptr) && !is_solid_grid(c_ptr)) continue;
250                                 if ((c_ptr->feat >= FEAT_DOOR_HEAD) && (c_ptr->feat <= FEAT_SECRET)) continue;
251                                 if ((feat == FEAT_MAGMA) || (feat == FEAT_QUARTZ)) treasure = TRUE;
252                         }
253                         else
254                         {
255                                 if (cave_perma_grid(c_ptr) && (c_ptr->feat != FEAT_MOUNTAIN)) continue;
256                         }
257
258                         /* Clear previous contents, add proper vein type */
259                         c_ptr->feat = feat;
260
261                         /* Hack -- Add some (known) treasure */
262                         if (treasure && (rand_int(chance) == 0)) c_ptr->feat += 0x04;
263                 }
264
265                 if (dummy >= SAFE_MAX_ATTEMPTS)
266                 {
267                         if (cheat_room)
268                         {
269 #ifdef JP
270 msg_print("·Ù¹ð¡ª¥¹¥È¥ê¡¼¥Þ¡¼¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
271 #else
272                                 msg_print("Warning! Could not place streamer!");
273 #endif
274
275                         }
276                         return;
277                 }
278
279
280                 /* Advance the streamer */
281                 y += ddy[dir];
282                 x += ddx[dir];
283
284                 /* Quit before leaving the dungeon */
285                 if (!in_bounds(y, x)) break;
286         }
287 }
288
289
290 /*
291  * Put trees near a hole in the dungeon roof  (rubble on ground + up stairway)
292  * This happens in real world lava tubes.
293  */
294 void place_trees(int x, int y)
295 {
296         int i, j;
297         cave_type *c_ptr;
298
299         /* place trees/ rubble in ovalish distribution */
300         for (i = x - 3; i < x + 4; i++)
301         {
302                 for (j = y - 3; j < y + 4; j++)
303                 {
304                         if (!in_bounds(j, i)) continue;
305                         c_ptr = &cave[j][i];
306
307                         if (c_ptr->info & CAVE_ICKY) continue;
308                         if (c_ptr->info & CAVE_TRAP) continue;
309                         if (c_ptr->o_idx) continue;
310
311                         /* Want square to be in the circle and accessable. */
312                         if ((distance(j, i, y, x) < 4) && !cave_perma_grid(c_ptr))
313                         {
314                                 /*
315                                  * Clear previous contents, add feature
316                                  * The border mainly gets trees, while the center gets rubble
317                                  */
318                                 if ((distance(j, i, y, x) > 1) || (randint(100) < 25))
319                                 {
320                                         if (randint(100) < 75)
321                                                 cave[j][i].feat = FEAT_TREES;
322                                 }
323                                 else
324                                 {
325                                         cave[j][i].feat = FEAT_RUBBLE;
326                                 }
327
328                                 /* Light area since is open above */
329                                 cave[j][i].info |= (CAVE_GLOW | CAVE_ROOM);
330                         }
331                 }
332         }
333
334         /* No up stairs in ironman mode */
335         if (!ironman_downward && (randint(3) == 1))
336         {
337                 /* up stair */
338                 cave[y][x].feat = FEAT_LESS;
339         }
340 }
341
342
343 /*
344  * Build a destroyed level
345  */
346 void destroy_level(void)
347 {
348         int y1, x1, y, x, k, t, n;
349
350         cave_type *c_ptr;
351
352         /* Note destroyed levels */
353 #ifdef JP
354 if (cheat_room) msg_print("Ç˲õ¤µ¤ì¤¿³¬");
355 #else
356         if (cheat_room) msg_print("Destroyed Level");
357 #endif
358
359
360         /* Drop a few epi-centers (usually about two) */
361         for (n = 0; n < randint(5); n++)
362         {
363                 /* Pick an epi-center */
364                 x1 = rand_range(5, cur_wid - 1 - 5);
365                 y1 = rand_range(5, cur_hgt - 1 - 5);
366
367                 /* Big area of affect */
368                 for (y = (y1 - 15); y <= (y1 + 15); y++)
369                 {
370                         for (x = (x1 - 15); x <= (x1 + 15); x++)
371                         {
372                                 /* Skip illegal grids */
373                                 if (!in_bounds(y, x)) continue;
374
375                                 /* Extract the distance */
376                                 k = distance(y1, x1, y, x);
377
378                                 /* Stay in the circle of death */
379                                 if (k >= 16) continue;
380
381                                 /* Delete the monster (if any) */
382                                 delete_monster(y, x);
383
384                                 /* Access the grid */
385                                 c_ptr = &cave[y][x];
386
387                                 /* Destroy valid grids */
388                                 if (cave_valid_grid(c_ptr))
389                                 {
390                                         /* Delete objects */
391                                         delete_object(y, x);
392
393                                         /* Wall (or floor) type */
394                                         t = rand_int(200);
395
396                                         /* Granite */
397                                         if (t < 20)
398                                         {
399                                                 /* Create granite wall */
400                                                 place_extra_grid(c_ptr);
401                                         }
402
403                                         /* Quartz */
404                                         else if (t < 70)
405                                         {
406                                                 /* Create quartz vein */
407                                                 c_ptr->feat = FEAT_QUARTZ;
408                                         }
409
410                                         /* Magma */
411                                         else if (t < 100)
412                                         {
413                                                 /* Create magma vein */
414                                                 c_ptr->feat = FEAT_MAGMA;
415                                         }
416
417                                         /* Floor */
418                                         else
419                                         {
420                                                 /* Create floor */
421                                                 place_floor_grid(c_ptr);
422                                         }
423
424                                         /* No longer part of a room or vault */
425                                         c_ptr->info &= ~(CAVE_ROOM | CAVE_ICKY);
426
427                                         /* No longer illuminated or known */
428                                         c_ptr->info &= ~(CAVE_MARK | CAVE_GLOW);
429                                 }
430                         }
431                 }
432         }
433 }