OSDN Git Service

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