OSDN Git Service

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