OSDN Git Service

[Refactor] #37353 rooms, grid, generate間のドア処理整理。 / Refactor between door process...
[hengband/hengband.git] / src / rooms.c
1 /*!
2  * @file rooms.c
3  * @brief ダンジョンフロアの部屋生成処理 / make rooms. Used by generate.c when creating dungeons.
4  * @date 2014/01/06
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  * @details
12  * Room building routines.\n
13  *\n
14  * Room types:\n
15  *   1 -- normal\n
16  *   2 -- overlapping\n
17  *   3 -- cross shaped\n
18  *   4 -- large room with features\n
19  *   5 -- monster nests\n
20  *   6 -- monster pits\n
21  *   7 -- simple vaults\n
22  *   8 -- greater vaults\n
23  *   9 -- fractal caves\n
24  *  10 -- random vaults\n
25  *  11 -- circular rooms\n
26  *  12 -- crypts\n
27  *  13 -- trapped monster pits\n
28  *  14 -- trapped room\n
29  *  15 -- glass room\n
30  *  16 -- underground arcade\n
31  *\n
32  * Some functions are used to determine if the given monster\n
33  * is appropriate for inclusion in a monster nest or monster pit or\n
34  * the given type.\n
35  *\n
36  * None of the pits/nests are allowed to include "unique" monsters.\n
37  */
38
39 #include "angband.h"
40 #include "generate.h"
41 #include "grid.h"
42 #include "rooms.h"
43
44 #include "rooms-city.h"
45 #include "rooms-fractal.h"
46 #include "rooms-normal.h"
47 #include "rooms-pitnest.h"
48 #include "rooms-special.h"
49 #include "rooms-trap.h"
50 #include "rooms-vault.h"
51
52
53 /*!
54  * 各部屋タイプの生成比定義
55  *[from SAngband (originally from OAngband)]\n
56  *\n
57  * Table of values that control how many times each type of room will\n
58  * appear.  Each type of room has its own row, and each column\n
59  * corresponds to dungeon levels 0, 10, 20, and so on.  The final\n
60  * value is the minimum depth the room can appear at.  -LM-\n
61  *\n
62  * Level 101 and below use the values for level 100.\n
63  *\n
64  * Rooms with lots of monsters or loot may not be generated if the\n
65  * object or monster lists are already nearly full.  Rooms will not\n
66  * appear above their minimum depth.  Tiny levels will not have space\n
67  * for all the rooms you ask for.\n
68  */
69 static room_info_type room_info_normal[ROOM_T_MAX] =
70 {
71         /* Depth */
72         /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
73
74         {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
75         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
76         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
77         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*INNER_F  */
78         {{  0,  1,  1,  1,  2,  3,  5,  6,  8, 10, 13}, 10}, /*NEST     */
79         {{  0,  1,  1,  2,  3,  4,  6,  8, 10, 13, 16}, 10}, /*PIT      */
80         {{  0,  1,  1,  1,  2,  2,  3,  5,  6,  8, 10}, 10}, /*LESSER_V */
81         {{  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  4}, 20}, /*GREATER_V*/
82         {{  0,100,200,300,400,500,600,700,800,900,999}, 10}, /*FRACAVE  */
83         {{  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2}, 10}, /*RANDOM_V */
84         {{  0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40},  3}, /*OVAL     */
85         {{  1,  6, 12, 18, 24, 30, 36, 42, 48, 54, 60}, 10}, /*CRYPT    */
86         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP_PIT */
87         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
88         {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
89         {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
90 };
91
92
93 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
94 static byte room_build_order[ROOM_T_MAX] = {
95         ROOM_T_GREATER_VAULT,
96         ROOM_T_ARCADE,
97         ROOM_T_RANDOM_VAULT,
98         ROOM_T_LESSER_VAULT,
99         ROOM_T_TRAP_PIT,
100         ROOM_T_PIT,
101         ROOM_T_NEST,
102         ROOM_T_TRAP,
103         ROOM_T_GLASS,
104         ROOM_T_INNER_FEAT,
105         ROOM_T_OVAL,
106         ROOM_T_CRYPT,
107         ROOM_T_OVERLAP,
108         ROOM_T_CROSS,
109         ROOM_T_FRACAVE,
110         ROOM_T_NORMAL,
111 };
112
113 /*!
114  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
115  * @param y0 配置したい中心のY座標
116  * @param x0 配置したい中心のX座標
117  * @details
118  * This funtion makes a very small room centred at (x0, y0)
119  * This is used in crypts, and random elemental vaults.
120  *
121  * Note - this should be used only on allocated regions
122  * within another room.
123  */
124 void build_small_room(int x0, int y0)
125 {
126         int x, y;
127
128         for (y = y0 - 1; y <= y0 + 1; y++)
129         {
130                 place_inner_bold(y, x0 - 1);
131                 place_inner_bold(y, x0 + 1);
132         }
133
134         for (x = x0 - 1; x <= x0 + 1; x++)
135         {
136                 place_inner_bold(y0 - 1, x);
137                 place_inner_bold(y0 + 1, x);
138         }
139
140         /* Place a secret door on one side */
141         switch (randint0(4))
142         {
143                 case 0: place_secret_door(y0, x0 - 1, DOOR_DEFAULT); break;
144                 case 1: place_secret_door(y0, x0 + 1, DOOR_DEFAULT); break;
145                 case 2: place_secret_door(y0 - 1, x0, DOOR_DEFAULT); break;
146                 case 3: place_secret_door(y0 + 1, x0, DOOR_DEFAULT); break;
147         }
148
149         /* Clear mimic type */
150         cave[y0][x0].mimic = 0;
151
152         /* Add inner open space */
153         place_floor_bold(y0, x0);
154 }
155
156 /*!
157  * @brief
158  * 指定範囲に通路が通っていることを確認した上で床で埋める
159  * This function tunnels around a room if it will cut off part of a cave system.
160  * @param x1 範囲の左端
161  * @param y1 範囲の上端
162  * @param x2 範囲の右端
163  * @param y2 範囲の下端
164  * @return なし
165  */
166 static void check_room_boundary(int x1, int y1, int x2, int y2)
167 {
168         int count, x, y;
169         bool old_is_floor, new_is_floor;
170
171
172         /* Initialize */
173         count = 0;
174
175         old_is_floor = get_is_floor(x1 - 1, y1);
176
177         /*
178          * Count the number of floor-wall boundaries around the room
179          * Note: diagonal squares are ignored since the player can move diagonally
180          * to bypass these if needed.
181          */
182
183         /* Above the top boundary */
184         for (x = x1; x <= x2; x++)
185         {
186                 new_is_floor = get_is_floor(x, y1 - 1);
187
188                 /* Increment counter if they are different */
189                 if (new_is_floor != old_is_floor) count++;
190
191                 old_is_floor = new_is_floor;
192         }
193
194         /* Right boundary */
195         for (y = y1; y <= y2; y++)
196         {
197                 new_is_floor = get_is_floor(x2 + 1, y);
198
199                 /* increment counter if they are different */
200                 if (new_is_floor != old_is_floor) count++;
201
202                 old_is_floor = new_is_floor;
203         }
204
205         /* Bottom boundary */
206         for (x = x2; x >= x1; x--)
207         {
208                 new_is_floor = get_is_floor(x, y2 + 1);
209
210                 /* increment counter if they are different */
211                 if (new_is_floor != old_is_floor) count++;
212
213                 old_is_floor = new_is_floor;
214         }
215
216         /* Left boundary */
217         for (y = y2; y >= y1; y--)
218         {
219                 new_is_floor = get_is_floor(x1 - 1, y);
220
221                 /* increment counter if they are different */
222                 if (new_is_floor != old_is_floor) count++;
223
224                 old_is_floor = new_is_floor;
225         }
226
227         /* If all the same, or only one connection exit. */
228         if (count <= 2) return;
229
230
231         /* Tunnel around the room so to prevent problems with caves */
232         for (y = y1; y <= y2; y++)
233         {
234                 for (x = x1; x <= x2; x++)
235                 {
236                         set_floor(x, y);
237                 }
238         }
239 }
240
241
242 /*!
243  * @brief
244  * find_space()の予備処理として部屋の生成が可能かを判定する /
245  * Helper function for find_space(). Is this a good location?
246  * @param blocks_high 範囲の高さ
247  * @param blocks_wide 範囲の幅
248  * @param block_y 範囲の上端
249  * @param block_x 範囲の左端
250  * @return なし
251  */
252 static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int block_x)
253 {
254         int by1, bx1, by2, bx2, by, bx;
255
256         /* Itty-bitty rooms must shift about within their rectangle */
257         if (blocks_wide < 3)
258         {
259                 if ((blocks_wide == 2) && (block_x % 3) == 2)
260                         return FALSE;
261         }
262
263         /* Rooms with width divisible by 3 must be fitted to a rectangle. */
264         else if ((blocks_wide % 3) == 0)
265         {
266                 /* Must be aligned to the left edge of a 11x33 rectangle. */
267                 if ((block_x % 3) != 0)
268                         return FALSE;
269         }
270
271         /*
272          * Big rooms that do not have a width divisible by 3 must be
273          * aligned towards the edge of the dungeon closest to them.
274          */
275         else
276         {
277                 /* Shift towards left edge of dungeon. */
278                 if (block_x + (blocks_wide / 2) <= dun->col_rooms / 2)
279                 {
280                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
281                                 return FALSE;
282                         if ((block_x % 3) == 1)
283                                 return FALSE;
284                 }
285
286                 /* Shift toward right edge of dungeon. */
287                 else
288                 {
289                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
290                                 return FALSE;
291                         if ((block_x % 3) == 1)
292                                 return FALSE;
293                 }
294         }
295
296         /* Extract blocks */
297         by1 = block_y + 0;
298         bx1 = block_x + 0;
299         by2 = block_y + blocks_high;
300         bx2 = block_x + blocks_wide;
301
302         /* Never run off the screen */
303         if ((by1 < 0) || (by2 > dun->row_rooms)) return FALSE;
304         if ((bx1 < 0) || (bx2 > dun->col_rooms)) return FALSE;
305         
306         /* Verify available space */
307         for (by = by1; by < by2; by++)
308         {
309                 for (bx = bx1; bx < bx2; bx++)
310                 {
311                         if (dun->room_map[by][bx])
312                         {
313                                 return FALSE;
314                         }
315                 }
316         }
317
318         /* This location is okay */
319         return TRUE;
320 }
321
322
323 /*!
324  * @brief 部屋生成が可能なスペースを確保する / Find a good spot for the next room.  -LM-
325  * @param y 部屋の生成が可能な中心Y座標を返す参照ポインタ
326  * @param x 部屋の生成が可能な中心X座標を返す参照ポインタ
327  * @param height 確保したい領域の高さ
328  * @param width 確保したい領域の幅
329  * @return 所定の範囲が確保できた場合TRUEを返す
330  * @details
331  * Find and allocate a free space in the dungeon large enough to hold\n
332  * the room calling this function.\n
333  *\n
334  * We allocate space in 11x11 blocks, but want to make sure that rooms\n
335  * align neatly on the standard screen.  Therefore, we make them use\n
336  * blocks in few 11x33 rectangles as possible.\n
337  *\n
338  * Be careful to include the edges of the room in height and width!\n
339  *\n
340  * Return TRUE and values for the center of the room if all went well.\n
341  * Otherwise, return FALSE.\n
342  */
343 bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
344 {
345         int candidates, pick;
346         int by, bx, by1, bx1, by2, bx2;
347         int block_y = 0, block_x = 0;
348
349
350         /* Find out how many blocks we need. */
351         int blocks_high = 1 + ((height - 1) / BLOCK_HGT);
352         int blocks_wide = 1 + ((width - 1) / BLOCK_WID);
353
354         /* There are no way to allocate such huge space */
355         if (dun->row_rooms < blocks_high) return FALSE;
356         if (dun->col_rooms < blocks_wide) return FALSE;
357
358         /* Initiallize */
359         candidates = 0;
360
361         /* Count the number of valid places */
362         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
363         {
364                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
365                 {
366                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
367                         {
368                                 /* Find a valid place */
369                                 candidates++;
370                         }
371                 }
372         }
373
374         /* No place! */
375         if (!candidates)
376         {
377                 return FALSE;
378         }
379
380         /* Normal dungeon */
381         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
382         {
383                 /* Choose a random one */
384                 pick = randint1(candidates);
385         }
386
387         /* NO_CAVE dungeon (Castle) */
388         else
389         {
390                 /* Always choose the center one */
391                 pick = candidates/2 + 1;
392         }
393
394         /* Pick up the choosen location */
395         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
396         {
397                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
398                 {
399                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
400                         {
401                                 pick--;
402
403                                 /* This one is picked? */
404                                 if (!pick) break;
405                         }
406                 }
407
408                 if (!pick) break;
409         }
410
411         /* Extract blocks */
412         by1 = block_y + 0;
413         bx1 = block_x + 0;
414         by2 = block_y + blocks_high;
415         bx2 = block_x + blocks_wide;
416
417         /*
418          * It is *extremely* important that the following calculation
419          * be *exactly* correct to prevent memory errors
420          */
421
422         /* Acquire the location of the room */
423         (*y) = ((by1 + by2) * BLOCK_HGT) / 2;
424         (*x) = ((bx1 + bx2) * BLOCK_WID) / 2;
425
426         /* Save the room location */
427         if (dun->cent_n < CENT_MAX)
428         {
429                 dun->cent[dun->cent_n].y = (byte_hack)*y;
430                 dun->cent[dun->cent_n].x = (byte_hack)*x;
431                 dun->cent_n++;
432         }
433
434         /* Reserve some blocks. */
435         for (by = by1; by < by2; by++)
436         {
437                 for (bx = bx1; bx < bx2; bx++)
438                 {
439                         dun->room_map[by][bx] = TRUE;
440                 }
441         }
442
443
444         /*
445          * Hack- See if room will cut off a cavern.
446          *
447          * If so, fix by tunneling outside the room in such a
448          * way as to connect the caves.
449          */
450         check_room_boundary(*x - width / 2 - 1, *y - height / 2 - 1, *x + (width - 1) / 2 + 1, *y + (height - 1) / 2 + 1);
451
452
453         /* Success. */
454         return TRUE;
455 }
456
457
458
459
460 /*
461  * Structure to hold all "fill" data
462  */
463
464 typedef struct fill_data_type fill_data_type;
465
466 struct fill_data_type
467 {
468         /* area size */
469         int xmin;
470         int ymin;
471         int xmax;
472         int ymax;
473
474         /* cutoffs */
475         int c1;
476         int c2;
477         int c3;
478
479         /* features to fill with */
480         int feat1;
481         int feat2;
482         int feat3;
483
484         int info1;
485         int info2;
486         int info3;
487
488         /* number of filled squares */
489         int amount;
490 };
491
492 static fill_data_type fill_data;
493
494
495 /* Store routine for the fractal cave generator */
496 /* this routine probably should be an inline function or a macro. */
497 static void store_height(int x, int y, int val)
498 {
499         /* if on boundary set val > cutoff so walls are not as square */
500         if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
501              (x == fill_data.xmax) || (y == fill_data.ymax)) &&
502             (val <= fill_data.c1)) val = fill_data.c1 + 1;
503
504         /* store the value in height-map format */
505         cave[y][x].feat = (s16b)val;
506
507         return;
508 }
509
510
511 /*
512 * Explanation of the plasma fractal algorithm:
513 *
514 * A grid of points is created with the properties of a 'height-map'
515 * This is done by making the corners of the grid have a random value.
516 * The grid is then subdivided into one with twice the resolution.
517 * The new points midway between two 'known' points can be calculated
518 * by taking the average value of the 'known' ones and randomly adding
519 * or subtracting an amount proportional to the distance between those
520 * points.  The final 'middle' points of the grid are then calculated
521 * by averaging all four of the originally 'known' corner points.  An
522 * random amount is added or subtracted from this to get a value of the
523 * height at that point.  The scaling factor here is adjusted to the
524 * slightly larger distance diagonally as compared to orthogonally.
525 *
526 * This is then repeated recursively to fill an entire 'height-map'
527 * A rectangular map is done the same way, except there are different
528 * scaling factors along the x and y directions.
529 *
530 * A hack to change the amount of correlation between points is done using
531 * the grd variable.  If the current step size is greater than grd then
532 * the point will be random, otherwise it will be calculated by the
533 * above algorithm.  This makes a maximum distance at which two points on
534 * the height map can affect each other.
535 *
536 * How fractal caves are made:
537 *
538 * When the map is complete, a cut-off value is used to create a cave.
539 * Heights below this value are "floor", and heights above are "wall".
540 * This also can be used to create lakes, by adding more height levels
541 * representing shallow and deep water/ lava etc.
542 *
543 * The grd variable affects the width of passages.
544 * The roug variable affects the roughness of those passages
545 *
546 * The tricky part is making sure the created cave is connected.  This
547 * is done by 'filling' from the inside and only keeping the 'filled'
548 * floor.  Walls bounding the 'filled' floor are also kept.  Everything
549 * else is converted to the normal _extra_.
550  */
551
552
553 /*
554  *  Note that this uses the cave.feat array in a very hackish way
555  *  the values are first set to zero, and then each array location
556  *  is used as a "heightmap"
557  *  The heightmap then needs to be converted back into the "feat" format.
558  *
559  *  grd=level at which fractal turns on.  smaller gives more mazelike caves
560  *  roug=roughness level.  16=normal.  higher values make things more convoluted
561  *    small values are good for smooth walls.
562  *  size=length of the side of the square cave system.
563  */
564 void generate_hmap(int y0, int x0, int xsiz, int ysiz, int grd, int roug, int cutoff)
565 {
566         int xhsize, yhsize, xsize, ysize, maxsize;
567
568         /*
569          * fixed point variables- these are stored as 256 x normal value
570          * this gives 8 binary places of fractional part + 8 places of normal part
571          */
572
573         u16b xstep, xhstep, ystep, yhstep;
574         u16b xstep2, xhstep2, ystep2, yhstep2;
575         u16b i, j, ii, jj, diagsize, xxsize, yysize;
576         
577         /* Cache for speed */
578         u16b xm, xp, ym, yp;
579
580         /* redefine size so can change the value if out of range */
581         xsize = xsiz;
582         ysize = ysiz;
583
584         /* Paranoia about size of the system of caves */
585         if (xsize > 254) xsize = 254;
586         if (xsize < 4) xsize = 4;
587         if (ysize > 254) ysize = 254;
588         if (ysize < 4) ysize = 4;
589
590         /* get offsets to middle of array */
591         xhsize = xsize / 2;
592         yhsize = ysize / 2;
593
594         /* fix rounding problem */
595         xsize = xhsize * 2;
596         ysize = yhsize * 2;
597
598         /* get limits of region */
599         fill_data.xmin = x0 - xhsize;
600         fill_data.ymin = y0 - yhsize;
601         fill_data.xmax = x0 + xhsize;
602         fill_data.ymax = y0 + yhsize;
603
604         /* Store cutoff in global for quick access */
605         fill_data.c1 = cutoff;
606
607         /*
608         * Scale factor for middle points:
609         * About sqrt(2) * 256 - correct for a square lattice
610         * approximately correct for everything else.
611          */
612         diagsize = 362;
613
614         /* maximum of xsize and ysize */
615         maxsize = (xsize > ysize) ? xsize : ysize;
616
617         /* Clear the section */
618         for (i = 0; i <= xsize; i++)
619         {
620                 for (j = 0; j <= ysize; j++)
621                 {
622                         /* -1 is a flag for "not done yet" */
623                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
624                         /* Clear icky flag because may be redoing the cave */
625                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
626                 }
627         }
628
629         /* Boundaries are walls */
630         cave[fill_data.ymin][fill_data.xmin].feat = (s16b)maxsize;
631         cave[fill_data.ymax][fill_data.xmin].feat = (s16b)maxsize;
632         cave[fill_data.ymin][fill_data.xmax].feat = (s16b)maxsize;
633         cave[fill_data.ymax][fill_data.xmax].feat = (s16b)maxsize;
634
635         /* Set the middle square to be an open area. */
636         cave[y0][x0].feat = 0;
637
638         /* Initialize the step sizes */
639         xstep = xhstep = xsize * 256;
640         ystep = yhstep = ysize * 256;
641         xxsize = xsize * 256;
642         yysize = ysize * 256;
643
644         /*
645          * Fill in the rectangle with fractal height data -
646          * like the 'plasma fractal' in fractint.
647          */
648         while ((xhstep > 256) || (yhstep > 256))
649         {
650                 /* Halve the step sizes */
651                 xstep = xhstep;
652                 xhstep /= 2;
653                 ystep = yhstep;
654                 yhstep /= 2;
655
656                 /* cache well used values */
657                 xstep2 = xstep / 256;
658                 ystep2 = ystep / 256;
659
660                 xhstep2 = xhstep / 256;
661                 yhstep2 = yhstep / 256;
662
663                 /* middle top to bottom. */
664                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
665                 {
666                         for (j = 0; j <= yysize; j += ystep)
667                         {
668                                 /* cache often used values */
669                                 ii = i / 256 + fill_data.xmin;
670                                 jj = j / 256 + fill_data.ymin;
671
672                                 /* Test square */
673                                 if (cave[jj][ii].feat == -1)
674                                 {
675                                         if (xhstep2 > grd)
676                                         {
677                                                 /* If greater than 'grid' level then is random */
678                                                 store_height(ii, jj, randint1(maxsize));
679                                         }
680                                         else
681                                         {
682                                                 /* Average of left and right points +random bit */
683                                                 store_height(ii, jj,
684                                                         (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
685                                                          + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
686                                                          + (randint1(xstep2) - xhstep2) * roug / 16);
687                                         }
688                                 }
689                         }
690                 }
691
692
693                 /* middle left to right. */
694                 for (j = yhstep; j <= yysize - yhstep; j += ystep)
695                 {
696                         for (i = 0; i <= xxsize; i += xstep)
697                         {
698                                 /* cache often used values */
699                                 ii = i / 256 + fill_data.xmin;
700                                 jj = j / 256 + fill_data.ymin;
701
702                                 /* Test square */
703                                 if (cave[jj][ii].feat == -1)
704                                 {
705                                         if (xhstep2 > grd)
706                                         {
707                                                 /* If greater than 'grid' level then is random */
708                                                 store_height(ii, jj, randint1(maxsize));
709                                         }
710                                         else
711                                         {
712                                                 /* Average of up and down points +random bit */
713                                                 store_height(ii, jj,
714                                                         (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
715                                                         + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
716                                                         + (randint1(ystep2) - yhstep2) * roug / 16);
717                                         }
718                                 }
719                         }
720                 }
721
722                 /* center. */
723                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
724                 {
725                         for (j = yhstep; j <= yysize - yhstep; j += ystep)
726                         {
727                                 /* cache often used values */
728                                 ii = i / 256 + fill_data.xmin;
729                                 jj = j / 256 + fill_data.ymin;
730
731                                 /* Test square */
732                                 if (cave[jj][ii].feat == -1)
733                                 {
734                                         if (xhstep2 > grd)
735                                         {
736                                                 /* If greater than 'grid' level then is random */
737                                                 store_height(ii, jj, randint1(maxsize));
738                                         }
739                                         else
740                                         {
741                                                 /* Cache reused values. */
742                                                 xm = fill_data.xmin + (i - xhstep) / 256;
743                                                 xp = fill_data.xmin + (i + xhstep) / 256;
744                                                 ym = fill_data.ymin + (j - yhstep) / 256;
745                                                 yp = fill_data.ymin + (j + yhstep) / 256;
746
747                                                 /* 
748                                                  * Average over all four corners + scale by diagsize to
749                                                  * reduce the effect of the square grid on the shape of the fractal
750                                                  */
751                                                 store_height(ii, jj,
752                                                         (cave[ym][xm].feat + cave[yp][xm].feat
753                                                         + cave[ym][xp].feat + cave[yp][xp].feat) / 4
754                                                         + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
755                                         }
756                                 }
757                         }
758                 }
759         }
760 }
761
762
763 static bool hack_isnt_wall(int y, int x, int c1, int c2, int c3, int feat1, int feat2, int feat3, int info1, int info2, int info3)
764 {
765         /*
766          * function used to convert from height-map back to the
767          *  normal angband cave format
768          */
769         if (cave[y][x].info & CAVE_ICKY)
770         {
771                 /* already done */
772                 return FALSE;
773         }
774         else
775         {
776                 /* Show that have looked at this square */
777                 cave[y][x].info|= (CAVE_ICKY);
778
779                 /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
780                 if (cave[y][x].feat <= c1)
781                 {
782                         /* 25% of the time use the other tile : it looks better this way */
783                         if (randint1(100) < 75)
784                         {
785                                 cave[y][x].feat = (s16b)feat1;
786                                 cave[y][x].info &= ~(CAVE_MASK);
787                                 cave[y][x].info |= info1;
788                                 return TRUE;
789                         }
790                         else
791                         {
792                                 cave[y][x].feat = (s16b)feat2;
793                                 cave[y][x].info &= ~(CAVE_MASK);
794                                 cave[y][x].info |= info2;
795                                 return TRUE;
796                         }
797                 }
798                 else if (cave[y][x].feat <= c2)
799                 {
800                         /* 25% of the time use the other tile : it looks better this way */
801                         if (randint1(100) < 75)
802                         {
803                                 cave[y][x].feat = (s16b)feat2;
804                                 cave[y][x].info &= ~(CAVE_MASK);
805                                 cave[y][x].info |= info2;
806                                 return TRUE;
807                         }
808                         else
809                         {
810                                 cave[y][x].feat = (s16b)feat1;
811                                 cave[y][x].info &= ~(CAVE_MASK);
812                                 cave[y][x].info |= info1;
813                                 return TRUE;
814                         }
815                 }
816                 else if (cave[y][x].feat <= c3)
817                 {
818                         cave[y][x].feat = (s16b)feat3;
819                         cave[y][x].info &= ~(CAVE_MASK);
820                         cave[y][x].info |= info3;
821                         return TRUE;
822                 }
823                 /* if greater than cutoff then is a wall */
824                 else
825                 {
826                         place_outer_bold(y, x);
827                         return FALSE;
828                 }
829         }
830 }
831
832
833
834
835 /*
836  * Quick and nasty fill routine used to find the connected region
837  * of floor in the middle of the cave
838  */
839 static void cave_fill(POSITION y, POSITION x)
840 {
841         int i, j, d;
842         int ty, tx;
843
844         int flow_tail = 1;
845         int flow_head = 0;
846
847
848         /*** Start Grid ***/
849
850         /* Enqueue that entry */
851         temp_y[0] = y;
852         temp_x[0] = x;
853
854
855         /* Now process the queue */
856         while (flow_head != flow_tail)
857         {
858                 /* Extract the next entry */
859                 ty = temp_y[flow_head];
860                 tx = temp_x[flow_head];
861
862                 /* Forget that entry */
863                 if (++flow_head == TEMP_MAX) flow_head = 0;
864
865                 /* Add the "children" */
866                 for (d = 0; d < 8; d++)
867                 {
868                         int old_head = flow_tail;
869
870                         /* Child location */
871                         j = ty + ddy_ddd[d];
872                         i = tx + ddx_ddd[d];
873
874                         /* Paranoia Don't leave the cave */
875                         if (!in_bounds(j, i))
876                         {
877                                 /* affect boundary */
878                                 cave[j][i].info |= CAVE_ICKY;
879 /*                              return; */
880                         }
881
882                         /* If within bounds */
883                         else if ((i > fill_data.xmin) && (i < fill_data.xmax)
884                                 && (j > fill_data.ymin) && (j < fill_data.ymax))
885                         {
886                                 /* If not a wall or floor done before */
887                                 if (hack_isnt_wall(j, i,
888                                         fill_data.c1, fill_data.c2, fill_data.c3,
889                                         fill_data.feat1, fill_data.feat2, fill_data.feat3,
890                                         fill_data.info1, fill_data.info2, fill_data.info3))
891                                 {
892                                         /* Enqueue that entry */
893                                         temp_y[flow_tail] = (byte_hack)j;
894                                         temp_x[flow_tail] = (byte_hack)i;
895
896                                         /* Advance the queue */
897                                         if (++flow_tail == TEMP_MAX) flow_tail = 0;
898
899                                         /* Hack -- Overflow by forgetting new entry */
900                                         if (flow_tail == flow_head)
901                                         {
902                                                 flow_tail = old_head;
903                                         }
904                                         else
905                                         {
906                                                 /* keep tally of size of cave system */
907                                                 (fill_data.amount)++;
908                                         }
909                                 }
910                         }
911                         else
912                         {
913                                 /* affect boundary */
914                                 cave[j][i].info |= CAVE_ICKY;
915                         }
916                 }
917         }
918 }
919
920
921 bool generate_fracave(int y0, int x0, int xsize, int ysize, int cutoff, bool light, bool room)
922 {
923         int x, y, i, xhsize, yhsize;
924
925         /* offsets to middle from corner */
926         xhsize = xsize / 2;
927         yhsize = ysize / 2;
928
929
930         /*
931          * select region connected to center of cave system
932          * this gets rid of alot of isolated one-sqaures that
933          * can make teleport traps instadeaths...
934          */
935
936         /* cutoffs */
937         fill_data.c1 = cutoff;
938         fill_data.c2 = 0;
939         fill_data.c3 = 0;
940
941         /* features to fill with */
942         fill_data.feat1 = floor_type[randint0(100)];
943         fill_data.feat2 = floor_type[randint0(100)];
944         fill_data.feat3 = floor_type[randint0(100)];
945
946         fill_data.info1 = CAVE_FLOOR;
947         fill_data.info2 = CAVE_FLOOR;
948         fill_data.info3 = CAVE_FLOOR;
949
950         /* number of filled squares */
951         fill_data.amount = 0;
952
953         cave_fill((byte)y0, (byte)x0);
954
955         /* if tally too small, try again */
956         if (fill_data.amount < 10)
957         {
958                 /* too small - clear area and try again later */
959                 for (x = 0; x <= xsize; ++x)
960                 {
961                         for (y = 0; y <= ysize; ++y)
962                         {
963                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
964                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
965                         }
966                 }
967                 return FALSE;
968         }
969
970         /*
971          * Do boundarys-check to see if they are next to a filled region
972          * If not then they are set to normal granite
973          * If so then they are marked as room walls.
974          */
975         for (i = 0; i <= xsize; ++i)
976         {
977                 /* top boundary */
978                 if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
979                 {
980                         /* Next to a 'filled' region? - set to be room walls */
981                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
982                         if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
983                         cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
984                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
985                 }
986                 else
987                 {
988                         /* set to be normal granite */
989                         place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
990                 }
991
992                 /* bottom boundary */
993                 if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
994                 {
995                         /* Next to a 'filled' region? - set to be room walls */
996                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
997                         if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
998                         cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
999                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1000                 }
1001                 else
1002                 {
1003                         /* set to be normal granite */
1004                         place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1005                 }
1006
1007                 /* clear the icky flag-don't need it any more */
1008                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1009                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1010         }
1011
1012         /* Do the left and right boundaries minus the corners (done above) */
1013         for (i = 1; i < ysize; ++i)
1014         {
1015                 /* left boundary */
1016                 if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
1017                 {
1018                         /* room boundary */
1019                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1020                         if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
1021                         cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
1022                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1023                 }
1024                 else
1025                 {
1026                         /* outside room */
1027                         place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1028                 }
1029                 /* right boundary */
1030                 if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
1031                 {
1032                         /* room boundary */
1033                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1034                         if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
1035                         cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
1036                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1037                 }
1038                 else
1039                 {
1040                         /* outside room */
1041                         place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1042                 }
1043
1044                 /* clear icky flag -done with it */
1045                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1046                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1047         }
1048
1049
1050         /* Do the rest: convert back to the normal format */
1051         for (x = 1; x < xsize; ++x)
1052         {
1053                 for (y = 1; y < ysize; ++y)
1054                 {
1055                         if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1056                             (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1057                         {
1058                                 /* Clear the icky flag in the filled region */
1059                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
1060
1061                                 /* Set appropriate flags */
1062                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1063                                 if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1064                         }
1065                         else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1066                                  (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1067                         {
1068                                 /* Walls */
1069                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1070                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1071                                 if (room)
1072                                 {
1073                                         cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1074                                 }
1075                                 else
1076                                 {
1077
1078                                         place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1079                                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
1080                                 }
1081                         }
1082                         else
1083                         {
1084                                 /* Clear the unconnected regions */
1085                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1086                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1087                         }
1088                 }
1089         }
1090
1091         /*
1092          * XXX XXX XXX There is a slight problem when tunnels pierce the caves:
1093          * Extra doors appear inside the system.  (Its not very noticeable though.)
1094          * This can be removed by "filling" from the outside in.  This allows a separation
1095          * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
1096          * The extra effort for what seems to be only a minor thing (even non-existant if you
1097          * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
1098          * worth it.
1099          */
1100
1101         return TRUE;
1102 }
1103
1104
1105 #ifdef ALLOW_CAVERNS_AND_LAKES
1106 /*
1107  * Builds a cave system in the center of the dungeon.
1108  */
1109 void build_cavern(void)
1110 {
1111         int grd, roug, cutoff, xsize, ysize, x0, y0;
1112         bool done, light;
1113
1114         light = done = FALSE;
1115         if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
1116
1117         /* Make a cave the size of the dungeon */
1118         xsize = cur_wid - 1;
1119         ysize = cur_hgt - 1;
1120         x0 = xsize / 2;
1121         y0 = ysize / 2;
1122
1123         /* Paranoia: make size even */
1124         xsize = x0 * 2;
1125         ysize = y0 * 2;
1126
1127         while (!done)
1128         {
1129                 /* testing values for these parameters: feel free to adjust */
1130                 grd = randint1(4) + 4;
1131
1132                 /* want average of about 16 */
1133                 roug = randint1(8) * randint1(4);
1134
1135                 /* about size/2 */
1136                 cutoff = xsize / 2;
1137
1138                  /* make it */
1139                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
1140
1141                 /* Convert to normal format+ clean up */
1142                 done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
1143         }
1144 }
1145
1146 bool generate_lake(int y0, int x0, int xsize, int ysize, int c1, int c2, int c3, int type)
1147 {
1148         int x, y, i, xhsize, yhsize;
1149         int feat1, feat2, feat3;
1150
1151         /* offsets to middle from corner */
1152         xhsize = xsize / 2;
1153         yhsize = ysize / 2;
1154
1155         /* Get features based on type */
1156         switch (type)
1157         {
1158         case LAKE_T_LAVA: /* Lava */
1159                 feat1 = feat_deep_lava;
1160                 feat2 = feat_shallow_lava;
1161                 feat3 = floor_type[randint0(100)];
1162                 break;
1163         case LAKE_T_WATER: /* Water */
1164                 feat1 = feat_deep_water;
1165                 feat2 = feat_shallow_water;
1166                 feat3 = floor_type[randint0(100)];
1167                 break;
1168         case LAKE_T_CAVE: /* Collapsed cave */
1169                 feat1 = floor_type[randint0(100)];
1170                 feat2 = floor_type[randint0(100)];
1171                 feat3 = feat_rubble;
1172                 break;
1173         case LAKE_T_EARTH_VAULT: /* Earth vault */
1174                 feat1 = feat_rubble;
1175                 feat2 = floor_type[randint0(100)];
1176                 feat3 = feat_rubble;
1177                 break;
1178         case LAKE_T_AIR_VAULT: /* Air vault */
1179                 feat1 = feat_grass;
1180                 feat2 = feat_tree;
1181                 feat3 = feat_grass;
1182                 break;
1183         case LAKE_T_WATER_VAULT: /* Water vault */
1184                 feat1 = feat_shallow_water;
1185                 feat2 = feat_deep_water;
1186                 feat3 = feat_shallow_water;
1187                 break;
1188         case LAKE_T_FIRE_VAULT: /* Fire Vault */
1189                 feat1 = feat_shallow_lava;
1190                 feat2 = feat_deep_lava;
1191                 feat3 = feat_shallow_lava;
1192                 break;
1193
1194         /* Paranoia */
1195         default: return FALSE;
1196         }
1197
1198         /*
1199          * select region connected to center of cave system
1200          * this gets rid of alot of isolated one-sqaures that
1201          * can make teleport traps instadeaths...
1202          */
1203
1204         /* cutoffs */
1205         fill_data.c1 = c1;
1206         fill_data.c2 = c2;
1207         fill_data.c3 = c3;
1208
1209         /* features to fill with */
1210         fill_data.feat1 = feat1;
1211         fill_data.feat2 = feat2;
1212         fill_data.feat3 = feat3;
1213
1214         fill_data.info1 = 0;
1215         fill_data.info2 = 0;
1216         fill_data.info3 = 0;
1217
1218         /* number of filled squares */
1219         fill_data.amount = 0;
1220
1221         /* select region connected to center of cave system
1222         * this gets rid of alot of isolated one-sqaures that
1223         * can make teleport traps instadeaths... */
1224         cave_fill((byte)y0, (byte)x0);
1225
1226         /* if tally too small, try again */
1227         if (fill_data.amount < 10)
1228         {
1229                 /* too small -clear area and try again later */
1230                 for (x = 0; x <= xsize; ++x)
1231                 {
1232                         for (y = 0; y <= ysize; ++y)
1233                         {
1234                                 place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
1235                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1236                         }
1237                 }
1238                 return FALSE;
1239         }
1240
1241         /* Do boundarys- set to normal granite */
1242         for (i = 0; i <= xsize; ++i)
1243         {
1244                 place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1245                 place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1246
1247                 /* clear the icky flag-don't need it any more */
1248                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1249                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1250         }
1251
1252         /* Do the left and right boundaries minus the corners (done above) */
1253
1254         for (i = 1; i < ysize; ++i)
1255         {
1256                 place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1257                 place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1258
1259                 /* clear icky flag -done with it */
1260                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1261                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1262         }
1263
1264
1265         /* Do the rest: convert back to the normal format */
1266         for (x = 1; x < xsize; ++x)
1267         {
1268                 for (y = 1; y < ysize; ++y)
1269                 {
1270                         /* Fill unconnected regions with granite */
1271                         if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
1272                                 is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
1273                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1274
1275                         /* turn off icky flag (no longer needed.) */
1276                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1277
1278                         /* Light lava */
1279                         if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
1280                         {
1281                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
1282                         }
1283                 }
1284         }
1285
1286         return TRUE;
1287 }
1288
1289
1290 /*
1291  * makes a lake/collapsed cave system in the center of the dungeon
1292  */
1293 void build_lake(int type)
1294 {
1295         int grd, roug, xsize, ysize, x0, y0;
1296         bool done = FALSE;
1297         int c1, c2, c3;
1298
1299         /* paranoia - exit if lake type out of range. */
1300         if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
1301         {
1302                 msg_format("Invalid lake type (%d)", type);
1303                 return;
1304         }
1305
1306         /* Make the size of the dungeon */
1307         xsize = cur_wid - 1;
1308         ysize = cur_hgt - 1;
1309         x0 = xsize / 2;
1310         y0 = ysize / 2;
1311
1312         /* Paranoia: make size even */
1313         xsize = x0 * 2;
1314         ysize = y0 * 2;
1315
1316         while (!done)
1317         {
1318                 /* testing values for these parameters: feel free to adjust */
1319                 grd = randint1(3) + 4;
1320
1321                 /* want average of about 16 */
1322                 roug = randint1(8) * randint1(4);
1323
1324                 /* Make up size of various componants */
1325                 /* Floor */
1326                 c3 = 3 * xsize / 4;
1327
1328                 /* Deep water/lava */
1329                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
1330
1331                 /* Shallow boundary */
1332                 c2 = (c1 + c3) / 2;
1333
1334                 /* make it */
1335                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
1336
1337                 /* Convert to normal format+ clean up */
1338                 done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
1339         }
1340 }
1341 #endif /* ALLOW_CAVERNS_AND_LAKES */
1342
1343
1344
1345 /*
1346  * Routine that fills the empty areas of a room with treasure and monsters.
1347  */
1348 void fill_treasure(int x1, int x2, int y1, int y2, int difficulty)
1349 {
1350         int x, y, cx, cy, size;
1351         s32b value;
1352
1353         /* center of room:*/
1354         cx = (x1 + x2) / 2;
1355         cy = (y1 + y2) / 2;
1356
1357         /* Rough measure of size of vault= sum of lengths of sides */
1358         size = abs(x2 - x1) + abs(y2 - y1);
1359
1360         for (x = x1; x <= x2; x++)
1361         {
1362                 for (y = y1; y <= y2; y++)
1363                 {
1364                         /* Thing added based on distance to center of vault
1365                          * Difficulty is 1-easy to 10-hard */
1366                         value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
1367
1368                         /* hack- empty square part of the time */
1369                         if ((randint1(100) - difficulty * 3) > 50) value = 20;
1370
1371                          /* if floor, shallow water and lava */
1372                         if (is_floor_bold(y, x) ||
1373                             (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
1374                         {
1375                                 /* The smaller 'value' is, the better the stuff */
1376                                 if (value < 0)
1377                                 {
1378                                         /* Meanest monster + treasure */
1379                                         monster_level = base_level + 40;
1380                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1381                                         monster_level = base_level;
1382                                         object_level = base_level + 20;
1383                                         place_object(y, x, AM_GOOD);
1384                                         object_level = base_level;
1385                                 }
1386                                 else if (value < 5)
1387                                 {
1388                                         /* Mean monster +treasure */
1389                                         monster_level = base_level + 20;
1390                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1391                                         monster_level = base_level;
1392                                         object_level = base_level + 10;
1393                                         place_object(y, x, AM_GOOD);
1394                                         object_level = base_level;
1395                                 }
1396                                 else if (value < 10)
1397                                 {
1398                                         /* Monster */
1399                                         monster_level = base_level + 9;
1400                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1401                                         monster_level = base_level;
1402                                 }
1403                                 else if (value < 17)
1404                                 {
1405                                         /* Intentional Blank space */
1406
1407                                         /*
1408                                          * (Want some of the vault to be empty
1409                                          * so have room for group monsters.
1410                                          * This is used in the hack above to lower
1411                                          * the density of stuff in the vault.)
1412                                          */
1413                                 }
1414                                 else if (value < 23)
1415                                 {
1416                                         /* Object or trap */
1417                                         if (randint0(100) < 25)
1418                                         {
1419                                                 place_object(y, x, 0L);
1420                                         }
1421                                         else
1422                                         {
1423                                                 place_trap(y, x);
1424                                         }
1425                                 }
1426                                 else if (value < 30)
1427                                 {
1428                                         /* Monster and trap */
1429                                         monster_level = base_level + 5;
1430                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1431                                         monster_level = base_level;
1432                                         place_trap(y, x);
1433                                 }
1434                                 else if (value < 40)
1435                                 {
1436                                         /* Monster or object */
1437                                         if (randint0(100) < 50)
1438                                         {
1439                                                 monster_level = base_level + 3;
1440                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1441                                                 monster_level = base_level;
1442                                         }
1443                                         if (randint0(100) < 50)
1444                                         {
1445                                                 object_level = base_level + 7;
1446                                                 place_object(y, x, 0L);
1447                                                 object_level = base_level;
1448                                         }
1449                                 }
1450                                 else if (value < 50)
1451                                 {
1452                                         /* Trap */
1453                                         place_trap(y, x);
1454                                 }
1455                                 else
1456                                 {
1457                                         /* Various Stuff */
1458
1459                                         /* 20% monster, 40% trap, 20% object, 20% blank space */
1460                                         if (randint0(100) < 20)
1461                                         {
1462                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1463                                         }
1464                                         else if (randint0(100) < 50)
1465                                         {
1466                                                 place_trap(y, x);
1467                                         }
1468                                         else if (randint0(100) < 50)
1469                                         {
1470                                                 place_object(y, x, 0L);
1471                                         }
1472                                 }
1473
1474                         }
1475                 }
1476         }
1477 }
1478
1479
1480
1481 /*
1482  * Overlay a rectangular room given its bounds
1483  * This routine is used by build_room_vault
1484  * The area inside the walls is not touched:
1485  * only granite is removed- normal walls stay
1486  */
1487 void build_room(int x1, int x2, int y1, int y2)
1488 {
1489         int x, y, i, xsize, ysize, temp;
1490
1491         /* Check if rectangle has no width */
1492         if ((x1 == x2) || (y1 == y2)) return;
1493
1494         /* initialize */
1495         if (x1 > x2)
1496         {
1497                 /* Swap boundaries if in wrong order */
1498                 temp = x1;
1499                 x1 = x2;
1500                 x2 = temp;
1501         }
1502
1503         if (y1 > y2)
1504         {
1505                 /* Swap boundaries if in wrong order */
1506                 temp = y1;
1507                 y1 = y2;
1508                 y2 = temp;
1509         }
1510
1511         /* get total widths */
1512         xsize = x2 - x1;
1513         ysize = y2 - y1;
1514
1515
1516         /* Top and bottom boundaries */
1517         for (i = 0; i <= xsize; i++)
1518         {
1519                 place_outer_noperm_bold(y1, x1 + i);
1520                 cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1521                 place_outer_noperm_bold(y2, x1 + i);
1522                 cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1523         }
1524
1525         /* Left and right boundaries */
1526         for (i = 1; i < ysize; i++)
1527         {
1528                 place_outer_noperm_bold(y1 + i, x1);
1529                 cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
1530                 place_outer_noperm_bold(y1 + i, x2);
1531                 cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
1532         }
1533
1534         /* Middle */
1535         for (x = 1; x < xsize; x++)
1536         {
1537                 for (y = 1; y < ysize; y++)
1538                 {
1539                         if (is_extra_bold(y1+y, x1+x))
1540                         {
1541                                 /* clear the untouched region */
1542                                 place_floor_bold(y1 + y, x1 + x);
1543                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1544                         }
1545                         else
1546                         {
1547                                 /* make it a room- but don't touch */
1548                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1549                         }
1550                 }
1551         }
1552 }
1553
1554
1555
1556 /*
1557  * maze vault -- rectangular labyrinthine rooms
1558  *
1559  * maze vault uses two routines:
1560  *    r_visit - a recursive routine that builds the labyrinth
1561  *    build_maze_vault - a driver routine that calls r_visit and adds
1562  *                   monsters, traps and treasure
1563  *
1564  * The labyrinth is built by creating a spanning tree of a graph.
1565  * The graph vertices are at
1566  *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
1567  * and the edges are the vertical and horizontal nearest neighbors.
1568  *
1569  * The spanning tree is created by performing a suitably randomized
1570  * depth-first traversal of the graph. The only adjustable parameter
1571  * is the randint0(3) below; it governs the relative density of
1572  * twists and turns in the labyrinth: smaller number, more twists.
1573  */
1574 void r_visit(int y1, int x1, int y2, int x2, int node, int dir, int *visited)
1575 {
1576         int i, j, m, n, temp, x, y, adj[4];
1577
1578         /* dimensions of vertex array */
1579         m = (x2 - x1) / 2 + 1;
1580         n = (y2 - y1) / 2 + 1;
1581
1582         /* mark node visited and set it to a floor */
1583         visited[node] = 1;
1584         x = 2 * (node % m) + x1;
1585         y = 2 * (node / m) + y1;
1586         place_floor_bold(y, x);
1587
1588         /* setup order of adjacent node visits */
1589         if (one_in_(3))
1590         {
1591                 /* pick a random ordering */
1592                 for (i = 0; i < 4; i++)
1593                         adj[i] = i;
1594                 for (i = 0; i < 4; i++)
1595                 {
1596                         j = randint0(4);
1597                         temp = adj[i];
1598                         adj[i] = adj[j];
1599                         adj[j] = temp;
1600                 }
1601                 dir = adj[0];
1602         }
1603         else
1604         {
1605                 /* pick a random ordering with dir first */
1606                 adj[0] = dir;
1607                 for (i = 1; i < 4; i++)
1608                         adj[i] = i;
1609                 for (i = 1; i < 4; i++)
1610                 {
1611                         j = 1 + randint0(3);
1612                         temp = adj[i];
1613                         adj[i] = adj[j];
1614                         adj[j] = temp;
1615                 }
1616         }
1617
1618         for (i = 0; i < 4; i++)
1619         {
1620                 switch (adj[i])
1621                 {
1622                         case 0:
1623                                 /* (0,+) - check for bottom boundary */
1624                                 if ((node / m < n - 1) && (visited[node + m] == 0))
1625                                 {
1626                                         place_floor_bold(y + 1, x);
1627                                         r_visit(y1, x1, y2, x2, node + m, dir, visited);
1628                                 }
1629                                 break;
1630                         case 1:
1631                                 /* (0,-) - check for top boundary */
1632                                 if ((node / m > 0) && (visited[node - m] == 0))
1633                                 {
1634                                         place_floor_bold(y - 1, x);
1635                                         r_visit(y1, x1, y2, x2, node - m, dir, visited);
1636                                 }
1637                                 break;
1638                         case 2:
1639                                 /* (+,0) - check for right boundary */
1640                                 if ((node % m < m - 1) && (visited[node + 1] == 0))
1641                                 {
1642                                         place_floor_bold(y, x + 1);
1643                                         r_visit(y1, x1, y2, x2, node + 1, dir, visited);
1644                                 }
1645                                 break;
1646                         case 3:
1647                                 /* (-,0) - check for left boundary */
1648                                 if ((node % m > 0) && (visited[node - 1] == 0))
1649                                 {
1650                                         place_floor_bold(y, x - 1);
1651                                         r_visit(y1, x1, y2, x2, node - 1, dir, visited);
1652                                 }
1653                 } /* end switch */
1654         }
1655 }
1656
1657
1658 void build_maze_vault(int x0, int y0, int xsize, int ysize, bool is_vault)
1659 {
1660         int y, x, dy, dx;
1661         int y1, x1, y2, x2;
1662         int m, n, num_vertices, *visited;
1663         bool light;
1664         cave_type *c_ptr;
1665
1666         msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
1667
1668         /* Choose lite or dark */
1669         light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
1670
1671         /* Pick a random room size - randomized by calling routine */
1672         dy = ysize / 2 - 1;
1673         dx = xsize / 2 - 1;
1674
1675         y1 = y0 - dy;
1676         x1 = x0 - dx;
1677         y2 = y0 + dy;
1678         x2 = x0 + dx;
1679
1680         /* generate the room */
1681         for (y = y1 - 1; y <= y2 + 1; y++)
1682         {
1683                 for (x = x1 - 1; x <= x2 + 1; x++)
1684                 {
1685                         c_ptr = &cave[y][x];
1686                         c_ptr->info |= CAVE_ROOM;
1687                         if (is_vault) c_ptr->info |= CAVE_ICKY;
1688                         if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
1689                         {
1690                                 place_outer_grid(c_ptr);
1691                         }
1692                         else if (!is_vault)
1693                         {
1694                                 place_extra_grid(c_ptr);
1695                         }
1696                         else
1697                         {
1698                                 place_inner_grid(c_ptr);
1699                         }
1700                         if (light) c_ptr->info |= (CAVE_GLOW);
1701                 }
1702         }
1703
1704         /* dimensions of vertex array */
1705         m = dx + 1;
1706         n = dy + 1;
1707         num_vertices = m * n;
1708
1709         /* initialize array of visited vertices */
1710         C_MAKE(visited, num_vertices, int);
1711
1712         /* traverse the graph to create a spaning tree, pick a random root */
1713         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
1714
1715         /* Fill with monsters and treasure, low difficulty */
1716         if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
1717
1718         C_KILL(visited, num_vertices, int);
1719 }
1720
1721
1722 /* Build a town/ castle by using a recursive algorithm.
1723  * Basically divide each region in a probalistic way to create
1724  * smaller regions.  When the regions get too small stop.
1725  *
1726  * The power variable is a measure of how well defended a region is.
1727  * This alters the possible choices.
1728  */
1729 void build_recursive_room(int x1, int y1, int x2, int y2, int power)
1730 {
1731         int xsize, ysize;
1732         int x, y;
1733         int choice;
1734
1735         /* Temp variables */
1736         int t1, t2, t3, t4;
1737
1738         xsize = x2 - x1;
1739         ysize = y2 - y1;
1740
1741         if ((power < 3) && (xsize > 12) && (ysize > 12))
1742         {
1743                 /* Need outside wall +keep */
1744                 choice = 1;
1745         }
1746         else
1747         {
1748                 if (power < 10)
1749                 {
1750                         /* Make rooms + subdivide */
1751                         if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
1752                         {
1753                                 choice = 4;
1754                         }
1755                         else
1756                         {
1757                                 choice = randint1(2) + 1;
1758                         }
1759                 }
1760                 else
1761                 {
1762                         /* Mostly subdivide */
1763                         choice = randint1(3) + 1;
1764                 }
1765         }
1766
1767         /* Based on the choice made above, do something */
1768
1769         switch (choice)
1770         {
1771                 case 1:
1772                 {
1773                         /* Outer walls */
1774
1775                         /* top and bottom */
1776                         for (x = x1; x <= x2; x++)
1777                         {
1778                                 place_outer_bold(y1, x);
1779                                 place_outer_bold(y2, x);
1780                         }
1781
1782                         /* left and right */
1783                         for (y = y1 + 1; y < y2; y++)
1784                         {
1785                                 place_outer_bold(y, x1);
1786                                 place_outer_bold(y, x2);
1787                         }
1788
1789                         /* Make a couple of entrances */
1790                         if (one_in_(2))
1791                         {
1792                                 /* left and right */
1793                                 y = randint1(ysize) + y1;
1794                                 place_floor_bold(y, x1);
1795                                 place_floor_bold(y, x2);
1796                         }
1797                         else
1798                         {
1799                                 /* top and bottom */
1800                                 x = randint1(xsize) + x1;
1801                                 place_floor_bold(y1, x);
1802                                 place_floor_bold(y2, x);
1803                         }
1804
1805                         /* Select size of keep */
1806                         t1 = randint1(ysize / 3) + y1;
1807                         t2 = y2 - randint1(ysize / 3);
1808                         t3 = randint1(xsize / 3) + x1;
1809                         t4 = x2 - randint1(xsize / 3);
1810
1811                         /* Do outside areas */
1812
1813                         /* Above and below keep */
1814                         build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
1815                         build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
1816
1817                         /* Left and right of keep */
1818                         build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
1819                         build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
1820
1821                         /* Make the keep itself: */
1822                         x1 = t3;
1823                         x2 = t4;
1824                         y1 = t1;
1825                         y2 = t2;
1826                         xsize = x2 - x1;
1827                         ysize = y2 - y1;
1828                         power += 2;
1829
1830                         /* Fall through */
1831                 }
1832                 case 4:
1833                 {
1834                         /* Try to build a room */
1835                         if ((xsize < 3) || (ysize < 3))
1836                         {
1837                                 for (y = y1; y < y2; y++)
1838                                 {
1839                                         for (x = x1; x < x2; x++)
1840                                         {
1841                                                 place_inner_bold(y, x);
1842                                         }
1843                                 }
1844
1845                                 /* Too small */
1846                                 return;
1847                         }
1848
1849                         /* Make outside walls */
1850                         /* top and bottom */
1851                         for (x = x1 + 1; x <= x2 - 1; x++)
1852                         {
1853                                 place_inner_bold(y1 + 1, x);
1854                                 place_inner_bold(y2 - 1, x);
1855                         }
1856
1857                         /* left and right */
1858                         for (y = y1 + 1; y <= y2 - 1; y++)
1859                         {
1860                                 place_inner_bold(y, x1 + 1);
1861                                 place_inner_bold(y, x2 - 1);
1862                         }
1863
1864                         /* Make a door */
1865                         y = randint1(ysize - 3) + y1 + 1;
1866
1867                         if (one_in_(2))
1868                         {
1869                                 /* left */
1870                                 place_floor_bold(y, x1 + 1);
1871                         }
1872                         else
1873                         {
1874                                 /* right */
1875                                 place_floor_bold(y, x2 - 1);
1876                         }
1877
1878                         /* Build the room */
1879                         build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
1880                         break;
1881                 }
1882                 case 2:
1883                 {
1884                         /* Try and divide vertically */
1885                         if (xsize < 3)
1886                         {
1887                                 /* Too small */
1888                                 for (y = y1; y < y2; y++)
1889                                 {
1890                                         for (x = x1; x < x2; x++)
1891                                         {
1892                                                 place_inner_bold(y, x);
1893                                         }
1894                                 }
1895                                 return;
1896                         }
1897
1898                         t1 = randint1(xsize - 2) + x1 + 1;
1899                         build_recursive_room(x1, y1, t1, y2, power - 2);
1900                         build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
1901                         break;
1902                 }
1903                 case 3:
1904                 {
1905                         /* Try and divide horizontally */
1906                         if (ysize < 3)
1907                         {
1908                                 /* Too small */
1909                                 for (y = y1; y < y2; y++)
1910                                 {
1911                                         for (x = x1; x < x2; x++)
1912                                         {
1913                                                 place_inner_bold(y, x);
1914                                         }
1915                                 }
1916                                 return;
1917                         }
1918
1919                         t1 = randint1(ysize - 2) + y1 + 1;
1920                         build_recursive_room(x1, y1, x2, t1, power - 2);
1921                         build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
1922                         break;
1923                 }
1924         }
1925 }
1926
1927
1928 /*
1929  * Add outer wall to a floored region
1930  * Note: no range checking is done so must be inside dungeon
1931  * This routine also stomps on doors
1932  */
1933 void add_outer_wall(int x, int y, int light, int x1, int y1, int x2, int y2)
1934 {
1935         cave_type *c_ptr;
1936         feature_type *f_ptr;
1937         int i, j;
1938
1939         if (!in_bounds(y, x)) return;
1940
1941         c_ptr = &cave[y][x];
1942
1943         /* hack- check to see if square has been visited before
1944         * if so, then exit (use room flag to do this) */
1945         if (c_ptr->info & CAVE_ROOM) return;
1946
1947         /* set room flag */
1948         c_ptr->info |= CAVE_ROOM;
1949
1950         f_ptr = &f_info[c_ptr->feat];
1951
1952         if (is_floor_bold(y, x))
1953         {
1954                 for (i = -1; i <= 1; i++)
1955                 {
1956                         for (j = -1; j <= 1; j++)
1957                         {
1958                                 if ((x + i >= x1) && (x + i <= x2) &&
1959                                          (y + j >= y1) && (y + j <= y2))
1960                                 {
1961                                         add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
1962                                         if (light) c_ptr->info |= CAVE_GLOW;
1963                                 }
1964                         }
1965                 }
1966         }
1967         else if (is_extra_bold(y, x))
1968         {
1969                 /* Set bounding walls */
1970                 place_outer_bold(y, x);
1971                 if (light) c_ptr->info |= CAVE_GLOW;
1972         }
1973         else if (permanent_wall(f_ptr))
1974         {
1975                 /* Set bounding walls */
1976                 if (light) c_ptr->info |= CAVE_GLOW;
1977         }
1978 }
1979
1980
1981 /*
1982  * Hacked distance formula - gives the 'wrong' answer.
1983  * Used to build crypts
1984  */
1985 int dist2(int x1, int y1, int x2, int y2, int h1, int h2, int h3, int h4)
1986 {
1987         int dx, dy;
1988         dx = abs(x2 - x1);
1989         dy = abs(y2 - y1);
1990
1991         /* Basically this works by taking the normal pythagorean formula
1992          * and using an expansion to express this in a way without the
1993          * square root.  This approximate formula is then perturbed to give
1994          * the distorted results.  (I found this by making a mistake when I was
1995          * trying to fix the circular rooms.)
1996          */
1997
1998         /* h1-h4 are constants that describe the metric */
1999         if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
2000         if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
2001         return (((dx + dy) * 128) / 181 +
2002                 (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
2003         /* 128/181 is approx. 1/sqrt(2) */
2004 }
2005
2006
2007
2008
2009 /* Create a new floor room with optional light */
2010 void generate_room_floor(int y1, int x1, int y2, int x2, int light)
2011 {
2012         int y, x;
2013         
2014         cave_type *c_ptr;
2015
2016         for (y = y1; y <= y2; y++)
2017         {
2018                 for (x = x1; x <= x2; x++)
2019                 {
2020                         /* Point to grid */
2021                         c_ptr = &cave[y][x];
2022                         place_floor_grid(c_ptr);
2023                         c_ptr->info |= (CAVE_ROOM);
2024                         if (light) c_ptr->info |= (CAVE_GLOW);
2025                 }
2026         }
2027 }
2028
2029 void generate_fill_perm_bold(int y1, int x1, int y2, int x2)
2030 {
2031         int y, x;
2032
2033         for (y = y1; y <= y2; y++)
2034         {
2035                 for (x = x1; x <= x2; x++)
2036                 {
2037                         /* Point to grid */
2038                         place_inner_perm_bold(y, x);
2039                 }
2040         }
2041 }
2042
2043
2044 /*!
2045  * @brief 与えられた部屋型IDに応じて部屋の生成処理分岐を行い結果を返す / Attempt to build a room of the given type at the given block
2046  * @param type 部屋型ID
2047  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
2048  * @return 部屋の精製に成功した場合 TRUE を返す。
2049  */
2050 static bool room_build(int typ)
2051 {
2052         /* Build a room */
2053         switch (typ)
2054         {
2055         /* Build an appropriate room */
2056         case ROOM_T_NORMAL:        return build_type1();
2057         case ROOM_T_OVERLAP:       return build_type2();
2058         case ROOM_T_CROSS:         return build_type3();
2059         case ROOM_T_INNER_FEAT:    return build_type4();
2060         case ROOM_T_NEST:          return build_type5();
2061         case ROOM_T_PIT:           return build_type6();
2062         case ROOM_T_LESSER_VAULT:  return build_type7();
2063         case ROOM_T_GREATER_VAULT: return build_type8();
2064         case ROOM_T_FRACAVE:       return build_type9();
2065         case ROOM_T_RANDOM_VAULT:  return build_type10();
2066         case ROOM_T_OVAL:          return build_type11();
2067         case ROOM_T_CRYPT:         return build_type12();
2068         case ROOM_T_TRAP_PIT:      return build_type13();
2069         case ROOM_T_TRAP:          return build_type14();
2070         case ROOM_T_GLASS:         return build_type15();
2071         case ROOM_T_ARCADE:        return build_type16();
2072         }
2073
2074         /* Paranoia */
2075         return FALSE;
2076 }
2077
2078 /*!
2079  * @brief 指定した部屋の生成確率を別の部屋に加算し、指定した部屋の生成率を0にする
2080  * @param dst 確率を移す先の部屋種ID
2081  * @param src 確率を与える元の部屋種ID
2082  */
2083 #define MOVE_PLIST(dst, src) (prob_list[dst] += prob_list[src], prob_list[src] = 0) 
2084
2085 /*!
2086  * @brief 部屋生成処理のメインルーチン(Sangbandを経由してOangbandからの実装を引用) / Generate rooms in dungeon.  Build bigger rooms at first. [from SAngband (originally from OAngband)]
2087  * @return 部屋生成に成功した場合 TRUE を返す。
2088  */
2089 bool generate_rooms(void)
2090 {
2091         int i;
2092         bool remain;
2093         int crowded = 0;
2094         int total_prob;
2095         int prob_list[ROOM_T_MAX];
2096         int rooms_built = 0;
2097         int area_size = 100 * (cur_hgt*cur_wid) / (MAX_HGT*MAX_WID);
2098         int level_index = MIN(10, div_round(dun_level, 10));
2099
2100         /* Number of each type of room on this level */
2101         s16b room_num[ROOM_T_MAX];
2102
2103         /* Limit number of rooms */
2104         int dun_rooms = DUN_ROOMS_MAX * area_size / 100;
2105
2106         /* Assume normal cave */
2107         room_info_type *room_info_ptr = room_info_normal;
2108
2109         /*
2110          * Initialize probability list.
2111          */
2112         for (i = 0; i < ROOM_T_MAX; i++)
2113         {
2114                 /* No rooms allowed above their minimum depth. */
2115                 if (dun_level < room_info_ptr[i].min_level)
2116                 {
2117                         prob_list[i] = 0;
2118                 }
2119                 else
2120                 {
2121                         prob_list[i] = room_info_ptr[i].prob[level_index];
2122                 }
2123         }
2124
2125         /*
2126          * XXX -- Various dungeon types and options.
2127          */
2128
2129         /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、かつ「常に通常でない部屋を生成する」フラグがONならば、GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
2130         if (ironman_rooms && !((d_info[dungeon_type].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
2131         {
2132                 for (i = 0; i < ROOM_T_MAX; i++)
2133                 {
2134                         if (i == ROOM_T_GREATER_VAULT) prob_list[i] = 1;
2135                         else prob_list[i] = 0;
2136                 }
2137         }
2138
2139         /*! @details ダンジョンにNO_VAULTフラグがあるならば、LESSER_VAULT / GREATER_VAULT/ RANDOM_VAULTを除外 / Forbidden vaults */
2140         else if (d_info[dungeon_type].flags1 & DF1_NO_VAULT)
2141         {
2142                 prob_list[ROOM_T_LESSER_VAULT] = 0;
2143                 prob_list[ROOM_T_GREATER_VAULT] = 0;
2144                 prob_list[ROOM_T_RANDOM_VAULT] = 0;
2145         }
2146
2147         /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
2148         if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
2149         {
2150                 MOVE_PLIST(ROOM_T_NORMAL, ROOM_T_FRACAVE);
2151                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_CRYPT);
2152                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_OVAL);
2153         }
2154
2155         /*! @details ダンジョンにCAVEフラグがある場合、NORMALの生成枠がFRACAVEに与えられる。/ CAVE dungeon (Orc cave etc.) */
2156         else if (d_info[dungeon_type].flags1 & DF1_CAVE)
2157         {
2158                 MOVE_PLIST(ROOM_T_FRACAVE, ROOM_T_NORMAL);
2159         }
2160
2161         /*! @details ダンジョンの基本地形が最初から渓谷かアリーナ型の場合 FRACAVE は生成から除外。 /  No caves when a (random) cavern exists: they look bad */
2162         else if (dun->cavern || dun->empty_level)
2163         {
2164                 prob_list[ROOM_T_FRACAVE] = 0;
2165         }
2166
2167         /*! @details ダンジョンに最初からGLASS_ROOMフラグがある場合、GLASS を生成から除外。/ Forbidden glass rooms */
2168         if (!(d_info[dungeon_type].flags1 & DF1_GLASS_ROOM))
2169         {
2170                 prob_list[ROOM_T_GLASS] = 0;
2171         }
2172
2173         /*! @details ARCADEは同フラグがダンジョンにないと生成されない。 / Forbidden glass rooms */
2174         if (!(d_info[dungeon_type].flags1 & DF1_ARCADE))
2175         {
2176                 prob_list[ROOM_T_ARCADE] = 0;
2177         }
2178
2179         /*
2180          * Initialize number of rooms,
2181          * And calcurate total probability.
2182          */
2183         for (total_prob = 0, i = 0; i < ROOM_T_MAX; i++)
2184         {
2185                 room_num[i] = 0;
2186                 total_prob += prob_list[i];
2187         }
2188
2189         /*
2190          * Prepare the number of rooms, of all types, we should build
2191          * on this level.
2192          */
2193         for (i = dun_rooms; i > 0; i--)
2194         {
2195                 int room_type;
2196                 int rand = randint0(total_prob);
2197
2198                 /* Get room_type randomly */
2199                 for (room_type = 0; room_type < ROOM_T_MAX; room_type++)
2200                 {
2201                         if (rand < prob_list[room_type]) break;
2202                         else rand -= prob_list[room_type];
2203                 }
2204
2205                 /* Paranoia */
2206                 if (room_type >= ROOM_T_MAX) room_type = ROOM_T_NORMAL;
2207
2208                 /* Increase the number of rooms of that type we should build. */
2209                 room_num[room_type]++;
2210
2211                 switch (room_type)
2212                 {
2213                 case ROOM_T_NEST:
2214                 case ROOM_T_PIT:
2215                 case ROOM_T_LESSER_VAULT:
2216                 case ROOM_T_TRAP_PIT:
2217                 case ROOM_T_GLASS:
2218                 case ROOM_T_ARCADE:
2219
2220                         /* Large room */
2221                         i -= 2;
2222                         break;
2223
2224                 case ROOM_T_GREATER_VAULT:
2225                 case ROOM_T_RANDOM_VAULT:
2226
2227                         /* Largest room */
2228                         i -= 3;
2229                         break;
2230                 }
2231         }
2232
2233         /*
2234          * Build each type of room one by one until we cannot build any more.
2235          * [from SAngband (originally from OAngband)]
2236          */
2237         while (TRUE)
2238         {
2239                 /* Assume no remaining rooms */
2240                 remain = FALSE;
2241
2242                 for (i = 0; i < ROOM_T_MAX; i++)
2243                 {
2244                         /* What type of room are we building now? */
2245                         int room_type = room_build_order[i];
2246
2247                         /* Go next if none available */
2248                         if (!room_num[room_type]) continue;
2249
2250                         /* Use up one unit */
2251                         room_num[room_type]--;
2252
2253                         /* Build the room. */
2254                         if (room_build(room_type))
2255                         {
2256                                 /* Increase the room built count. */
2257                                 rooms_built++;
2258
2259                                 /* Mark as there was some remaining rooms */
2260                                 remain = TRUE;
2261
2262                                 switch (room_type)
2263                                 {
2264                                 case ROOM_T_PIT:
2265                                 case ROOM_T_NEST:
2266                                 case ROOM_T_TRAP_PIT:
2267
2268                                         /* Avoid too many monsters */
2269                                         if (++crowded >= 2)
2270                                         {
2271                                                 room_num[ROOM_T_PIT] = 0;
2272                                                 room_num[ROOM_T_NEST] = 0;
2273                                                 room_num[ROOM_T_TRAP_PIT] = 0;
2274                                         }
2275                                         break;
2276
2277                                 case ROOM_T_ARCADE:
2278
2279                                         /* Avoid double-town */
2280                                         room_num[ROOM_T_ARCADE] = 0;
2281                                         break;
2282                                 }
2283                         }
2284                 }
2285
2286                 /* End loop if no room remain */
2287                 if (!remain) break;
2288         }
2289
2290         /*! @details 部屋生成数が2未満の場合生成失敗を返す */
2291         if (rooms_built < 2)
2292         {
2293                 msg_format_wizard(CHEAT_DUNGEON, _("部屋数が2未満でした。生成を再試行します。", "Number of rooms was under 2. Retry."), rooms_built);
2294                 return FALSE;
2295         }
2296
2297         msg_format_wizard(CHEAT_DUNGEON, _("このダンジョンの部屋数は %d です。", "Number of Rooms: %d"), rooms_built);
2298
2299         return TRUE;
2300 }