OSDN Git Service

#37287 (2.2.0.40) cmd1.c内のC4457警告に対応。 / Deal C4457 warning in cmd1.c.
[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
45 /*!
46  * 各部屋タイプの生成比定義
47  *[from SAngband (originally from OAngband)]\n
48  *\n
49  * Table of values that control how many times each type of room will\n
50  * appear.  Each type of room has its own row, and each column\n
51  * corresponds to dungeon levels 0, 10, 20, and so on.  The final\n
52  * value is the minimum depth the room can appear at.  -LM-\n
53  *\n
54  * Level 101 and below use the values for level 100.\n
55  *\n
56  * Rooms with lots of monsters or loot may not be generated if the\n
57  * object or monster lists are already nearly full.  Rooms will not\n
58  * appear above their minimum depth.  Tiny levels will not have space\n
59  * for all the rooms you ask for.\n
60  */
61 static room_info_type room_info_normal[ROOM_T_MAX] =
62 {
63         /* Depth */
64         /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
65
66         {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
67         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
68         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
69         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*INNER_F  */
70         {{  0,  1,  1,  1,  2,  3,  5,  6,  8, 10, 13}, 10}, /*NEST     */
71         {{  0,  1,  1,  2,  3,  4,  6,  8, 10, 13, 16}, 10}, /*PIT      */
72         {{  0,  1,  1,  1,  2,  2,  3,  5,  6,  8, 10}, 10}, /*LESSER_V */
73         {{  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  4}, 20}, /*GREATER_V*/
74         {{  0,100,200,300,400,500,600,700,800,900,999}, 10}, /*FRACAVE  */
75         {{  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2}, 10}, /*RANDOM_V */
76         {{  0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40},  3}, /*OVAL     */
77         {{  1,  6, 12, 18, 24, 30, 36, 42, 48, 54, 60}, 10}, /*CRYPT    */
78         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP_PIT */
79         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
80         {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
81         {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
82 };
83
84
85 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
86 static byte room_build_order[ROOM_T_MAX] = {
87         ROOM_T_GREATER_VAULT,
88         ROOM_T_ARCADE,
89         ROOM_T_RANDOM_VAULT,
90         ROOM_T_LESSER_VAULT,
91         ROOM_T_TRAP_PIT,
92         ROOM_T_PIT,
93         ROOM_T_NEST,
94         ROOM_T_TRAP,
95         ROOM_T_GLASS,
96         ROOM_T_INNER_FEAT,
97         ROOM_T_OVAL,
98         ROOM_T_CRYPT,
99         ROOM_T_OVERLAP,
100         ROOM_T_CROSS,
101         ROOM_T_FRACAVE,
102         ROOM_T_NORMAL,
103 };
104
105 /*!
106  * @brief 鍵のかかったドアを配置する
107  * @param y 配置したいフロアのY座標
108  * @param x 配置したいフロアのX座標
109  * @return なし
110  */
111 static void place_locked_door(int y, int x)
112 {
113         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
114         {
115                 place_floor_bold(y, x);
116         }
117         else
118         {
119                 set_cave_feat(y, x, feat_locked_door_random((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
120                 cave[y][x].info &= ~(CAVE_FLOOR);
121                 delete_monster(y, x);
122         }
123 }
124
125 /*!
126  * @brief 隠しドアを配置する
127  * @param y 配置したいフロアのY座標
128  * @param x 配置したいフロアのX座標
129  * @param type #DOOR_DEFAULT / #DOOR_DOOR / #DOOR_GLASS_DOOR / #DOOR_CURTAIN のいずれか
130  * @return なし
131  */
132 static void place_secret_door(int y, int x, int type)
133 {
134         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
135         {
136                 place_floor_bold(y, x);
137         }
138         else
139         {
140                 cave_type *c_ptr = &cave[y][x];
141
142                 if (type == DOOR_DEFAULT)
143                 {
144                         type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
145                                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
146                                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
147                 }
148
149                 /* Create secret door */
150                 place_closed_door(y, x, type);
151
152                 if (type != DOOR_CURTAIN)
153                 {
154                         /* Hide by inner wall because this is used in rooms only */
155                         c_ptr->mimic = feat_wall_inner;
156
157                         /* Floor type terrain cannot hide a door */
158                         if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
159                         {
160                                 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
161                                 {
162                                         c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
163                                 }
164                                 c_ptr->mimic = 0;
165                         }
166                 }
167
168                 c_ptr->info &= ~(CAVE_FLOOR);
169                 delete_monster(y, x);
170         }
171 }
172
173 /*!
174  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
175  * @param y0 配置したい中心のY座標
176  * @param x0 配置したい中心のX座標
177  * @details
178  * This funtion makes a very small room centred at (x0, y0)
179  * This is used in crypts, and random elemental vaults.
180  *
181  * Note - this should be used only on allocated regions
182  * within another room.
183  */
184 static void build_small_room(int x0, int y0)
185 {
186         int x, y;
187
188         for (y = y0 - 1; y <= y0 + 1; y++)
189         {
190                 place_inner_bold(y, x0 - 1);
191                 place_inner_bold(y, x0 + 1);
192         }
193
194         for (x = x0 - 1; x <= x0 + 1; x++)
195         {
196                 place_inner_bold(y0 - 1, x);
197                 place_inner_bold(y0 + 1, x);
198         }
199
200         /* Place a secret door on one side */
201         switch (randint0(4))
202         {
203                 case 0: place_secret_door(y0, x0 - 1, DOOR_DEFAULT); break;
204                 case 1: place_secret_door(y0, x0 + 1, DOOR_DEFAULT); break;
205                 case 2: place_secret_door(y0 - 1, x0, DOOR_DEFAULT); break;
206                 case 3: place_secret_door(y0 + 1, x0, DOOR_DEFAULT); break;
207         }
208
209         /* Clear mimic type */
210         cave[y0][x0].mimic = 0;
211
212         /* Add inner open space */
213         place_floor_bold(y0, x0);
214 }
215
216 /*!
217  * @brief
218  * 指定範囲に通路が通っていることを確認した上で床で埋める
219  * This function tunnels around a room if it will cut off part of a cave system.
220  * @param x1 範囲の左端
221  * @param y1 範囲の上端
222  * @param x2 範囲の右端
223  * @param y2 範囲の下端
224  * @return なし
225  */
226 static void check_room_boundary(int x1, int y1, int x2, int y2)
227 {
228         int count, x, y;
229         bool old_is_floor, new_is_floor;
230
231
232         /* Initialize */
233         count = 0;
234
235         old_is_floor = get_is_floor(x1 - 1, y1);
236
237         /*
238          * Count the number of floor-wall boundaries around the room
239          * Note: diagonal squares are ignored since the player can move diagonally
240          * to bypass these if needed.
241          */
242
243         /* Above the top boundary */
244         for (x = x1; x <= x2; x++)
245         {
246                 new_is_floor = get_is_floor(x, y1 - 1);
247
248                 /* Increment counter if they are different */
249                 if (new_is_floor != old_is_floor) count++;
250
251                 old_is_floor = new_is_floor;
252         }
253
254         /* Right boundary */
255         for (y = y1; y <= y2; y++)
256         {
257                 new_is_floor = get_is_floor(x2 + 1, y);
258
259                 /* increment counter if they are different */
260                 if (new_is_floor != old_is_floor) count++;
261
262                 old_is_floor = new_is_floor;
263         }
264
265         /* Bottom boundary */
266         for (x = x2; x >= x1; x--)
267         {
268                 new_is_floor = get_is_floor(x, y2 + 1);
269
270                 /* increment counter if they are different */
271                 if (new_is_floor != old_is_floor) count++;
272
273                 old_is_floor = new_is_floor;
274         }
275
276         /* Left boundary */
277         for (y = y2; y >= y1; y--)
278         {
279                 new_is_floor = get_is_floor(x1 - 1, y);
280
281                 /* increment counter if they are different */
282                 if (new_is_floor != old_is_floor) count++;
283
284                 old_is_floor = new_is_floor;
285         }
286
287         /* If all the same, or only one connection exit. */
288         if (count <= 2) return;
289
290
291         /* Tunnel around the room so to prevent problems with caves */
292         for (y = y1; y <= y2; y++)
293         {
294                 for (x = x1; x <= x2; x++)
295                 {
296                         set_floor(x, y);
297                 }
298         }
299 }
300
301
302 /*!
303  * @brief
304  * find_space()の予備処理として部屋の生成が可能かを判定する /
305  * Helper function for find_space(). Is this a good location?
306  * @param blocks_high 範囲の高さ
307  * @param blocks_wide 範囲の幅
308  * @param block_y 範囲の上端
309  * @param block_x 範囲の左端
310  * @return なし
311  */
312 static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int block_x)
313 {
314         int by1, bx1, by2, bx2, by, bx;
315
316         /* Itty-bitty rooms must shift about within their rectangle */
317         if (blocks_wide < 3)
318         {
319                 if ((blocks_wide == 2) && (block_x % 3) == 2)
320                         return FALSE;
321         }
322
323         /* Rooms with width divisible by 3 must be fitted to a rectangle. */
324         else if ((blocks_wide % 3) == 0)
325         {
326                 /* Must be aligned to the left edge of a 11x33 rectangle. */
327                 if ((block_x % 3) != 0)
328                         return FALSE;
329         }
330
331         /*
332          * Big rooms that do not have a width divisible by 3 must be
333          * aligned towards the edge of the dungeon closest to them.
334          */
335         else
336         {
337                 /* Shift towards left edge of dungeon. */
338                 if (block_x + (blocks_wide / 2) <= dun->col_rooms / 2)
339                 {
340                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
341                                 return FALSE;
342                         if ((block_x % 3) == 1)
343                                 return FALSE;
344                 }
345
346                 /* Shift toward right edge of dungeon. */
347                 else
348                 {
349                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
350                                 return FALSE;
351                         if ((block_x % 3) == 1)
352                                 return FALSE;
353                 }
354         }
355
356         /* Extract blocks */
357         by1 = block_y + 0;
358         bx1 = block_x + 0;
359         by2 = block_y + blocks_high;
360         bx2 = block_x + blocks_wide;
361
362         /* Never run off the screen */
363         if ((by1 < 0) || (by2 > dun->row_rooms)) return FALSE;
364         if ((bx1 < 0) || (bx2 > dun->col_rooms)) return FALSE;
365         
366         /* Verify available space */
367         for (by = by1; by < by2; by++)
368         {
369                 for (bx = bx1; bx < bx2; bx++)
370                 {
371                         if (dun->room_map[by][bx])
372                         {
373                                 return FALSE;
374                         }
375                 }
376         }
377
378         /* This location is okay */
379         return TRUE;
380 }
381
382
383 /*!
384  * @brief 部屋生成が可能なスペースを確保する / Find a good spot for the next room.  -LM-
385  * @param y 部屋の生成が可能な中心Y座標を返す参照ポインタ
386  * @param x 部屋の生成が可能な中心X座標を返す参照ポインタ
387  * @param height 確保したい領域の高さ
388  * @param width 確保したい領域の幅
389  * @return 所定の範囲が確保できた場合TRUEを返す
390  * @details
391  * Find and allocate a free space in the dungeon large enough to hold\n
392  * the room calling this function.\n
393  *\n
394  * We allocate space in 11x11 blocks, but want to make sure that rooms\n
395  * align neatly on the standard screen.  Therefore, we make them use\n
396  * blocks in few 11x33 rectangles as possible.\n
397  *\n
398  * Be careful to include the edges of the room in height and width!\n
399  *\n
400  * Return TRUE and values for the center of the room if all went well.\n
401  * Otherwise, return FALSE.\n
402  */
403 static bool find_space(int *y, int *x, int height, int width)
404 {
405         int candidates, pick;
406         int by, bx, by1, bx1, by2, bx2;
407         int block_y = 0, block_x = 0;
408
409
410         /* Find out how many blocks we need. */
411         int blocks_high = 1 + ((height - 1) / BLOCK_HGT);
412         int blocks_wide = 1 + ((width - 1) / BLOCK_WID);
413
414         /* There are no way to allocate such huge space */
415         if (dun->row_rooms < blocks_high) return FALSE;
416         if (dun->col_rooms < blocks_wide) return FALSE;
417
418         /* Initiallize */
419         candidates = 0;
420
421         /* Count the number of valid places */
422         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
423         {
424                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
425                 {
426                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
427                         {
428                                 /* Find a valid place */
429                                 candidates++;
430                         }
431                 }
432         }
433
434         /* No place! */
435         if (!candidates)
436         {
437                 return FALSE;
438         }
439
440         /* Normal dungeon */
441         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
442         {
443                 /* Choose a random one */
444                 pick = randint1(candidates);
445         }
446
447         /* NO_CAVE dungeon (Castle) */
448         else
449         {
450                 /* Always choose the center one */
451                 pick = candidates/2 + 1;
452         }
453
454         /* Pick up the choosen location */
455         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
456         {
457                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
458                 {
459                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
460                         {
461                                 pick--;
462
463                                 /* This one is picked? */
464                                 if (!pick) break;
465                         }
466                 }
467
468                 if (!pick) break;
469         }
470
471         /* Extract blocks */
472         by1 = block_y + 0;
473         bx1 = block_x + 0;
474         by2 = block_y + blocks_high;
475         bx2 = block_x + blocks_wide;
476
477         /*
478          * It is *extremely* important that the following calculation
479          * be *exactly* correct to prevent memory errors
480          */
481
482         /* Acquire the location of the room */
483         (*y) = ((by1 + by2) * BLOCK_HGT) / 2;
484         (*x) = ((bx1 + bx2) * BLOCK_WID) / 2;
485
486         /* Save the room location */
487         if (dun->cent_n < CENT_MAX)
488         {
489                 dun->cent[dun->cent_n].y = *y;
490                 dun->cent[dun->cent_n].x = *x;
491                 dun->cent_n++;
492         }
493
494         /* Reserve some blocks. */
495         for (by = by1; by < by2; by++)
496         {
497                 for (bx = bx1; bx < bx2; bx++)
498                 {
499                         dun->room_map[by][bx] = TRUE;
500                 }
501         }
502
503
504         /*
505          * Hack- See if room will cut off a cavern.
506          *
507          * If so, fix by tunneling outside the room in such a
508          * way as to connect the caves.
509          */
510         check_room_boundary(*x - width / 2 - 1, *y - height / 2 - 1, *x + (width - 1) / 2 + 1, *y + (height - 1) / 2 + 1);
511
512
513         /* Success. */
514         return TRUE;
515 }
516
517
518 /*!
519  * @brief タイプ1の部屋…通常可変長方形の部屋を生成する / Type 1 -- normal rectangular rooms
520  * @return なし
521  */
522 static bool build_type1(void)
523 {
524         int y, x, y2, x2, yval, xval;
525         int y1, x1, xsize, ysize;
526
527         bool light;
528
529         cave_type *c_ptr;
530
531         bool curtain = (d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
532                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 48 : 512);
533
534         /* Pick a room size */
535         y1 = randint1(4);
536         x1 = randint1(11);
537         y2 = randint1(3);
538         x2 = randint1(11);
539
540         xsize = x1 + x2 + 1;
541         ysize = y1 + y2 + 1;
542
543         /* Find and reserve some space in the dungeon.  Get center of room. */
544         if (!find_space(&yval, &xval, ysize + 2, xsize + 2))
545         {
546                 /* Limit to the minimum room size, and retry */
547                 y1 = 1;
548                 x1 = 1;
549                 y2 = 1;
550                 x2 = 1;
551
552                 xsize = x1 + x2 + 1;
553                 ysize = y1 + y2 + 1;
554
555                 /* Find and reserve some space in the dungeon.  Get center of room. */
556                 if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
557         }
558
559         /* Choose lite or dark */
560         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
561
562
563         /* Get corner values */
564         y1 = yval - ysize / 2;
565         x1 = xval - xsize / 2;
566         y2 = yval + (ysize - 1) / 2;
567         x2 = xval + (xsize - 1) / 2;
568
569
570         /* Place a full floor under the room */
571         for (y = y1 - 1; y <= y2 + 1; y++)
572         {
573                 for (x = x1 - 1; x <= x2 + 1; x++)
574                 {
575                         c_ptr = &cave[y][x];
576                         place_floor_grid(c_ptr);
577                         c_ptr->info |= (CAVE_ROOM);
578                         if (light) c_ptr->info |= (CAVE_GLOW);
579                 }
580         }
581
582         /* Walls around the room */
583         for (y = y1 - 1; y <= y2 + 1; y++)
584         {
585                 c_ptr = &cave[y][x1 - 1];
586                 place_outer_grid(c_ptr);
587                 c_ptr = &cave[y][x2 + 1];
588                 place_outer_grid(c_ptr);
589         }
590         for (x = x1 - 1; x <= x2 + 1; x++)
591         {
592                 c_ptr = &cave[y1 - 1][x];
593                 place_outer_grid(c_ptr);
594                 c_ptr = &cave[y2 + 1][x];
595                 place_outer_grid(c_ptr);
596         }
597
598
599         /* Hack -- Occasional curtained room */
600         if (curtain && (y2 - y1 > 2) && (x2 - x1 > 2))
601         {
602                 for (y = y1; y <= y2; y++)
603                 {
604                         c_ptr = &cave[y][x1];
605                         c_ptr->feat = feat_door[DOOR_CURTAIN].closed;
606                         c_ptr->info &= ~(CAVE_MASK);
607                         c_ptr = &cave[y][x2];
608                         c_ptr->feat = feat_door[DOOR_CURTAIN].closed;
609                         c_ptr->info &= ~(CAVE_MASK);
610                 }
611                 for (x = x1; x <= x2; x++)
612                 {
613                         c_ptr = &cave[y1][x];
614                         c_ptr->feat = feat_door[DOOR_CURTAIN].closed;
615                         c_ptr->info &= ~(CAVE_MASK);
616                         c_ptr = &cave[y2][x];
617                         c_ptr->feat = feat_door[DOOR_CURTAIN].closed;
618                         c_ptr->info &= ~(CAVE_MASK);
619                 }
620         }
621
622
623         /* Hack -- Occasional pillar room */
624         if (one_in_(20))
625         {
626                 for (y = y1; y <= y2; y += 2)
627                 {
628                         for (x = x1; x <= x2; x += 2)
629                         {
630                                 c_ptr = &cave[y][x];
631                                 place_inner_grid(c_ptr);
632                         }
633                 }
634         }
635
636         /* Hack -- Occasional room with four pillars */
637         else if (one_in_(20))
638         {
639                 if ((y1 + 4 < y2) && (x1 + 4 < x2))
640                 {
641                         c_ptr = &cave[y1 + 1][x1 + 1];
642                         place_inner_grid(c_ptr);
643
644                         c_ptr = &cave[y1 + 1][x2 - 1];
645                         place_inner_grid(c_ptr);
646
647                         c_ptr = &cave[y2 - 1][x1 + 1];
648                         place_inner_grid(c_ptr);
649
650                         c_ptr = &cave[y2 - 1][x2 - 1];
651                         place_inner_grid(c_ptr);
652                 }
653         }
654
655         /* Hack -- Occasional ragged-edge room */
656         else if (one_in_(50))
657         {
658                 for (y = y1 + 2; y <= y2 - 2; y += 2)
659                 {
660                         c_ptr = &cave[y][x1];
661                         place_inner_grid(c_ptr);
662                         c_ptr = &cave[y][x2];
663                         place_inner_grid(c_ptr);
664                 }
665                 for (x = x1 + 2; x <= x2 - 2; x += 2)
666                 {
667                         c_ptr = &cave[y1][x];
668                         place_inner_grid(c_ptr);
669                         c_ptr = &cave[y2][x];
670                         place_inner_grid(c_ptr);
671                 }
672         }
673         /* Hack -- Occasional divided room */
674         else if (one_in_(50))
675         {
676                 bool curtain2 = (d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
677                         one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 2 : 128);
678
679                 if (randint1(100) < 50)
680                 {
681                         /* Horizontal wall */
682                         for (x = x1; x <= x2; x++)
683                         {
684                                 place_inner_bold(yval, x);
685                                 if (curtain2) cave[yval][x].feat = feat_door[DOOR_CURTAIN].closed;
686                         }
687
688                         /* Prevent edge of wall from being tunneled */
689                         place_solid_bold(yval, x1 - 1);
690                         place_solid_bold(yval, x2 + 1);
691                 }
692                 else
693                 {
694                         /* Vertical wall */
695                         for (y = y1; y <= y2; y++)
696                         {
697                                 place_inner_bold(y, xval);
698                                 if (curtain2) cave[y][xval].feat = feat_door[DOOR_CURTAIN].closed;
699                         }
700
701                         /* Prevent edge of wall from being tunneled */
702                         place_solid_bold(y1 - 1, xval);
703                         place_solid_bold(y2 + 1, xval);
704                 }
705
706                 place_random_door(yval, xval, TRUE);
707                 if (curtain2) cave[yval][xval].feat = feat_door[DOOR_CURTAIN].closed;
708         }
709
710         return TRUE;
711 }
712
713 /*!
714  * @brief タイプ2の部屋…二重長方形の部屋を生成する / Type 2 -- Overlapping rectangular rooms
715  * @return なし
716  */
717 static bool build_type2(void)
718 {
719         int                     y, x, xval, yval;
720         int                     y1a, x1a, y2a, x2a;
721         int                     y1b, x1b, y2b, x2b;
722         bool            light;
723         cave_type   *c_ptr;
724
725         /* Find and reserve some space in the dungeon.  Get center of room. */
726         if (!find_space(&yval, &xval, 11, 25)) return FALSE;
727
728         /* Choose lite or dark */
729         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
730
731         /* Determine extents of the first room */
732         y1a = yval - randint1(4);
733         y2a = yval + randint1(3);
734         x1a = xval - randint1(11);
735         x2a = xval + randint1(10);
736
737         /* Determine extents of the second room */
738         y1b = yval - randint1(3);
739         y2b = yval + randint1(4);
740         x1b = xval - randint1(10);
741         x2b = xval + randint1(11);
742
743
744         /* Place a full floor for room "a" */
745         for (y = y1a - 1; y <= y2a + 1; y++)
746         {
747                 for (x = x1a - 1; x <= x2a + 1; x++)
748                 {
749                         c_ptr = &cave[y][x];
750                         place_floor_grid(c_ptr);
751                         c_ptr->info |= (CAVE_ROOM);
752                         if (light) c_ptr->info |= (CAVE_GLOW);
753                 }
754         }
755
756         /* Place a full floor for room "b" */
757         for (y = y1b - 1; y <= y2b + 1; y++)
758         {
759                 for (x = x1b - 1; x <= x2b + 1; x++)
760                 {
761                         c_ptr = &cave[y][x];
762                         place_floor_grid(c_ptr);
763                         c_ptr->info |= (CAVE_ROOM);
764                         if (light) c_ptr->info |= (CAVE_GLOW);
765                 }
766         }
767
768
769         /* Place the walls around room "a" */
770         for (y = y1a - 1; y <= y2a + 1; y++)
771         {
772                 c_ptr = &cave[y][x1a - 1];
773                 place_outer_grid(c_ptr);
774                 c_ptr = &cave[y][x2a + 1];
775                 place_outer_grid(c_ptr);
776         }
777         for (x = x1a - 1; x <= x2a + 1; x++)
778         {
779                 c_ptr = &cave[y1a - 1][x];
780                 place_outer_grid(c_ptr);
781                 c_ptr = &cave[y2a + 1][x];
782                 place_outer_grid(c_ptr);
783         }
784
785         /* Place the walls around room "b" */
786         for (y = y1b - 1; y <= y2b + 1; y++)
787         {
788                 c_ptr = &cave[y][x1b - 1];
789                 place_outer_grid(c_ptr);
790                 c_ptr = &cave[y][x2b + 1];
791                 place_outer_grid(c_ptr);
792         }
793         for (x = x1b - 1; x <= x2b + 1; x++)
794         {
795                 c_ptr = &cave[y1b - 1][x];
796                 place_outer_grid(c_ptr);
797                 c_ptr = &cave[y2b + 1][x];
798                 place_outer_grid(c_ptr);
799         }
800
801
802
803         /* Replace the floor for room "a" */
804         for (y = y1a; y <= y2a; y++)
805         {
806                 for (x = x1a; x <= x2a; x++)
807                 {
808                         c_ptr = &cave[y][x];
809                         place_floor_grid(c_ptr);
810                 }
811         }
812
813         /* Replace the floor for room "b" */
814         for (y = y1b; y <= y2b; y++)
815         {
816                 for (x = x1b; x <= x2b; x++)
817                 {
818                         c_ptr = &cave[y][x];
819                         place_floor_grid(c_ptr);
820                 }
821         }
822
823         return TRUE;
824 }
825
826
827
828 /*!
829  * @brief タイプ2の部屋…十字型の部屋を生成する / Type 3 -- Cross shaped rooms
830  * @return なし
831  * @details
832  * Builds a room at a row, column coordinate\n
833  *\n
834  * Room "a" runs north/south, and Room "b" runs east/east\n
835  * So the "central pillar" runs from x1a, y1b to x2a, y2b.\n
836  *\n
837  * Note that currently, the "center" is always 3x3, but I think that\n
838  * the code below will work (with "bounds checking") for 5x5, or even\n
839  * for unsymetric values like 4x3 or 5x3 or 3x4 or 3x5, or even larger.\n
840  */
841 static bool build_type3(void)
842 {
843         int                     y, x, dy, dx, wy, wx;
844         int                     y1a, x1a, y2a, x2a;
845         int                     y1b, x1b, y2b, x2b;
846         int                     yval, xval;
847         bool            light;
848         cave_type   *c_ptr;
849
850
851         /* Find and reserve some space in the dungeon.  Get center of room. */
852         if (!find_space(&yval, &xval, 11, 25)) return FALSE;
853
854
855         /* Choose lite or dark */
856         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
857
858         /* For now, always 3x3 */
859         wx = wy = 1;
860
861         /* Pick max vertical size (at most 4) */
862         dy = rand_range(3, 4);
863
864         /* Pick max horizontal size (at most 15) */
865         dx = rand_range(3, 11);
866
867
868         /* Determine extents of the north/south room */
869         y1a = yval - dy;
870         y2a = yval + dy;
871         x1a = xval - wx;
872         x2a = xval + wx;
873
874         /* Determine extents of the east/west room */
875         y1b = yval - wy;
876         y2b = yval + wy;
877         x1b = xval - dx;
878         x2b = xval + dx;
879
880
881         /* Place a full floor for room "a" */
882         for (y = y1a - 1; y <= y2a + 1; y++)
883         {
884                 for (x = x1a - 1; x <= x2a + 1; x++)
885                 {
886                         c_ptr = &cave[y][x];
887                         place_floor_grid(c_ptr);
888                         c_ptr->info |= (CAVE_ROOM);
889                         if (light) c_ptr->info |= (CAVE_GLOW);
890                 }
891         }
892
893         /* Place a full floor for room "b" */
894         for (y = y1b - 1; y <= y2b + 1; y++)
895         {
896                 for (x = x1b - 1; x <= x2b + 1; x++)
897                 {
898                         c_ptr = &cave[y][x];
899                         place_floor_grid(c_ptr);
900                         c_ptr->info |= (CAVE_ROOM);
901                         if (light) c_ptr->info |= (CAVE_GLOW);
902                 }
903         }
904
905
906         /* Place the walls around room "a" */
907         for (y = y1a - 1; y <= y2a + 1; y++)
908         {
909                 c_ptr = &cave[y][x1a - 1];
910                 place_outer_grid(c_ptr);
911                 c_ptr = &cave[y][x2a + 1];
912                 place_outer_grid(c_ptr);
913         }
914         for (x = x1a - 1; x <= x2a + 1; x++)
915         {
916                 c_ptr = &cave[y1a - 1][x];
917                 place_outer_grid(c_ptr);
918                 c_ptr = &cave[y2a + 1][x];
919                 place_outer_grid(c_ptr);
920         }
921
922         /* Place the walls around room "b" */
923         for (y = y1b - 1; y <= y2b + 1; y++)
924         {
925                 c_ptr = &cave[y][x1b - 1];
926                 place_outer_grid(c_ptr);
927                 c_ptr = &cave[y][x2b + 1];
928                 place_outer_grid(c_ptr);
929         }
930         for (x = x1b - 1; x <= x2b + 1; x++)
931         {
932                 c_ptr = &cave[y1b - 1][x];
933                 place_outer_grid(c_ptr);
934                 c_ptr = &cave[y2b + 1][x];
935                 place_outer_grid(c_ptr);
936         }
937
938
939         /* Replace the floor for room "a" */
940         for (y = y1a; y <= y2a; y++)
941         {
942                 for (x = x1a; x <= x2a; x++)
943                 {
944                         c_ptr = &cave[y][x];
945                         place_floor_grid(c_ptr);
946                 }
947         }
948
949         /* Replace the floor for room "b" */
950         for (y = y1b; y <= y2b; y++)
951         {
952                 for (x = x1b; x <= x2b; x++)
953                 {
954                         c_ptr = &cave[y][x];
955                         place_floor_grid(c_ptr);
956                 }
957         }
958
959
960
961         /* Special features (3/4) */
962         switch (randint0(4))
963         {
964                 /* Large solid middle pillar */
965                 case 1:
966                 {
967                         for (y = y1b; y <= y2b; y++)
968                         {
969                                 for (x = x1a; x <= x2a; x++)
970                                 {
971                                         c_ptr = &cave[y][x];
972                                         place_inner_grid(c_ptr);
973                                 }
974                         }
975                         break;
976                 }
977
978                 /* Inner treasure vault */
979                 case 2:
980                 {
981                         /* Build the vault */
982                         for (y = y1b; y <= y2b; y++)
983                         {
984                                 c_ptr = &cave[y][x1a];
985                                 place_inner_grid(c_ptr);
986                                 c_ptr = &cave[y][x2a];
987                                 place_inner_grid(c_ptr);
988                         }
989                         for (x = x1a; x <= x2a; x++)
990                         {
991                                 c_ptr = &cave[y1b][x];
992                                 place_inner_grid(c_ptr);
993                                 c_ptr = &cave[y2b][x];
994                                 place_inner_grid(c_ptr);
995                         }
996
997                         /* Place a secret door on the inner room */
998                         switch (randint0(4))
999                         {
1000                                 case 0: place_secret_door(y1b, xval, DOOR_DEFAULT); break;
1001                                 case 1: place_secret_door(y2b, xval, DOOR_DEFAULT); break;
1002                                 case 2: place_secret_door(yval, x1a, DOOR_DEFAULT); break;
1003                                 case 3: place_secret_door(yval, x2a, DOOR_DEFAULT); break;
1004                         }
1005
1006                         /* Place a treasure in the vault */
1007                         place_object(yval, xval, 0L);
1008
1009                         /* Let's guard the treasure well */
1010                         vault_monsters(yval, xval, randint0(2) + 3);
1011
1012                         /* Traps naturally */
1013                         vault_traps(yval, xval, 4, 4, randint0(3) + 2);
1014
1015                         break;
1016                 }
1017
1018                 /* Something else */
1019                 case 3:
1020                 {
1021                         /* Occasionally pinch the center shut */
1022                         if (one_in_(3))
1023                         {
1024                                 /* Pinch the east/west sides */
1025                                 for (y = y1b; y <= y2b; y++)
1026                                 {
1027                                         if (y == yval) continue;
1028                                         c_ptr = &cave[y][x1a - 1];
1029                                         place_inner_grid(c_ptr);
1030                                         c_ptr = &cave[y][x2a + 1];
1031                                         place_inner_grid(c_ptr);
1032                                 }
1033
1034                                 /* Pinch the north/south sides */
1035                                 for (x = x1a; x <= x2a; x++)
1036                                 {
1037                                         if (x == xval) continue;
1038                                         c_ptr = &cave[y1b - 1][x];
1039                                         place_inner_grid(c_ptr);
1040                                         c_ptr = &cave[y2b + 1][x];
1041                                         place_inner_grid(c_ptr);
1042                                 }
1043
1044                                 /* Sometimes shut using secret doors */
1045                                 if (one_in_(3))
1046                                 {
1047                                         int door_type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
1048                                                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
1049                                                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
1050
1051                                         place_secret_door(yval, x1a - 1, door_type);
1052                                         place_secret_door(yval, x2a + 1, door_type);
1053                                         place_secret_door(y1b - 1, xval, door_type);
1054                                         place_secret_door(y2b + 1, xval, door_type);
1055                                 }
1056                         }
1057
1058                         /* Occasionally put a "plus" in the center */
1059                         else if (one_in_(3))
1060                         {
1061                                 c_ptr = &cave[yval][xval];
1062                                 place_inner_grid(c_ptr);
1063                                 c_ptr = &cave[y1b][xval];
1064                                 place_inner_grid(c_ptr);
1065                                 c_ptr = &cave[y2b][xval];
1066                                 place_inner_grid(c_ptr);
1067                                 c_ptr = &cave[yval][x1a];
1068                                 place_inner_grid(c_ptr);
1069                                 c_ptr = &cave[yval][x2a];
1070                                 place_inner_grid(c_ptr);
1071                         }
1072
1073                         /* Occasionally put a pillar in the center */
1074                         else if (one_in_(3))
1075                         {
1076                                 c_ptr = &cave[yval][xval];
1077                                 place_inner_grid(c_ptr);
1078                         }
1079
1080                         break;
1081                 }
1082         }
1083
1084         return TRUE;
1085 }
1086
1087
1088 /*!
1089  * @brief タイプ4の部屋…固定サイズの二重構造部屋を生成する / Type 4 -- Large room with inner features
1090  * @return なし
1091  * @details
1092  * Possible sub-types:\n
1093  *      1 - Just an inner room with one door\n
1094  *      2 - An inner room within an inner room\n
1095  *      3 - An inner room with pillar(s)\n
1096  *      4 - Inner room has a maze\n
1097  *      5 - A set of four inner rooms\n
1098  */
1099 static bool build_type4(void)
1100 {
1101         int         y, x, y1, x1;
1102         int         y2, x2, tmp, yval, xval;
1103         bool        light;
1104         cave_type   *c_ptr;
1105
1106
1107         /* Find and reserve some space in the dungeon.  Get center of room. */
1108         if (!find_space(&yval, &xval, 11, 25)) return FALSE;
1109
1110         /* Choose lite or dark */
1111         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
1112
1113         /* Large room */
1114         y1 = yval - 4;
1115         y2 = yval + 4;
1116         x1 = xval - 11;
1117         x2 = xval + 11;
1118
1119         /* Place a full floor under the room */
1120         for (y = y1 - 1; y <= y2 + 1; y++)
1121         {
1122                 for (x = x1 - 1; x <= x2 + 1; x++)
1123                 {
1124                         c_ptr = &cave[y][x];
1125                         place_floor_grid(c_ptr);
1126                         c_ptr->info |= (CAVE_ROOM);
1127                         if (light) c_ptr->info |= (CAVE_GLOW);
1128                 }
1129         }
1130
1131         /* Outer Walls */
1132         for (y = y1 - 1; y <= y2 + 1; y++)
1133         {
1134                 c_ptr = &cave[y][x1 - 1];
1135                 place_outer_grid(c_ptr);
1136                 c_ptr = &cave[y][x2 + 1];
1137                 place_outer_grid(c_ptr);
1138         }
1139         for (x = x1 - 1; x <= x2 + 1; x++)
1140         {
1141                 c_ptr = &cave[y1 - 1][x];
1142                 place_outer_grid(c_ptr);
1143                 c_ptr = &cave[y2 + 1][x];
1144                 place_outer_grid(c_ptr);
1145         }
1146
1147
1148         /* The inner room */
1149         y1 = y1 + 2;
1150         y2 = y2 - 2;
1151         x1 = x1 + 2;
1152         x2 = x2 - 2;
1153
1154         /* The inner walls */
1155         for (y = y1 - 1; y <= y2 + 1; y++)
1156         {
1157                 c_ptr = &cave[y][x1 - 1];
1158                 place_inner_grid(c_ptr);
1159                 c_ptr = &cave[y][x2 + 1];
1160                 place_inner_grid(c_ptr);
1161         }
1162         for (x = x1 - 1; x <= x2 + 1; x++)
1163         {
1164                 c_ptr = &cave[y1 - 1][x];
1165                 place_inner_grid(c_ptr);
1166                 c_ptr = &cave[y2 + 1][x];
1167                 place_inner_grid(c_ptr);
1168         }
1169
1170
1171         /* Inner room variations */
1172         switch (randint1(5))
1173         {
1174                 /* Just an inner room with a monster */
1175                 case 1:
1176                 {
1177                         /* Place a secret door */
1178                         switch (randint1(4))
1179                         {
1180                                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
1181                                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
1182                                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
1183                                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
1184                         }
1185
1186                         /* Place a monster in the room */
1187                         vault_monsters(yval, xval, 1);
1188
1189                         break;
1190                 }
1191
1192                 /* Treasure Vault (with a door) */
1193                 case 2:
1194                 {
1195                         /* Place a secret door */
1196                         switch (randint1(4))
1197                         {
1198                                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
1199                                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
1200                                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
1201                                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
1202                         }
1203
1204                         /* Place another inner room */
1205                         for (y = yval - 1; y <= yval + 1; y++)
1206                         {
1207                                 for (x = xval -  1; x <= xval + 1; x++)
1208                                 {
1209                                         if ((x == xval) && (y == yval)) continue;
1210                                         c_ptr = &cave[y][x];
1211                                         place_inner_grid(c_ptr);
1212                                 }
1213                         }
1214
1215                         /* Place a locked door on the inner room */
1216                         switch (randint1(4))
1217                         {
1218                                 case 1: place_locked_door(yval - 1, xval); break;
1219                                 case 2: place_locked_door(yval + 1, xval); break;
1220                                 case 3: place_locked_door(yval, xval - 1); break;
1221                                 case 4: place_locked_door(yval, xval + 1); break;
1222                         }
1223
1224                         /* Monsters to guard the "treasure" */
1225                         vault_monsters(yval, xval, randint1(3) + 2);
1226
1227                         /* Object (80%) */
1228                         if (randint0(100) < 80)
1229                         {
1230                                 place_object(yval, xval, 0L);
1231                         }
1232
1233                         /* Stairs (20%) */
1234                         else
1235                         {
1236                                 place_random_stairs(yval, xval);
1237                         }
1238
1239                         /* Traps to protect the treasure */
1240                         vault_traps(yval, xval, 4, 10, 2 + randint1(3));
1241
1242                         break;
1243                 }
1244
1245                 /* Inner pillar(s). */
1246                 case 3:
1247                 {
1248                         /* Place a secret door */
1249                         switch (randint1(4))
1250                         {
1251                                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
1252                                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
1253                                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
1254                                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
1255                         }
1256
1257                         /* Large Inner Pillar */
1258                         for (y = yval - 1; y <= yval + 1; y++)
1259                         {
1260                                 for (x = xval - 1; x <= xval + 1; x++)
1261                                 {
1262                                 c_ptr = &cave[y][x];
1263                                 place_inner_grid(c_ptr);
1264                                 }
1265                         }
1266
1267                         /* Occasionally, two more Large Inner Pillars */
1268                         if (one_in_(2))
1269                         {
1270                                 tmp = randint1(2);
1271                                 for (y = yval - 1; y <= yval + 1; y++)
1272                                 {
1273                                         for (x = xval - 5 - tmp; x <= xval - 3 - tmp; x++)
1274                                         {
1275                                         c_ptr = &cave[y][x];
1276                                         place_inner_grid(c_ptr);
1277                                         }
1278                                         for (x = xval + 3 + tmp; x <= xval + 5 + tmp; x++)
1279                                         {
1280                                         c_ptr = &cave[y][x];
1281                                         place_inner_grid(c_ptr);
1282                                         }
1283                                 }
1284                         }
1285
1286                         /* Occasionally, some Inner rooms */
1287                         if (one_in_(3))
1288                         {
1289                                 int door_type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
1290                                         one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
1291                                         ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
1292
1293                                 /* Long horizontal walls */
1294                                 for (x = xval - 5; x <= xval + 5; x++)
1295                                 {
1296                                 c_ptr = &cave[yval - 1][x];
1297                                 place_inner_grid(c_ptr);
1298                                 c_ptr = &cave[yval + 1][x];
1299                                 place_inner_grid(c_ptr);
1300                                 }
1301
1302                                 /* Close off the left/right edges */
1303                                 c_ptr = &cave[yval][xval - 5];
1304                                 place_inner_grid(c_ptr);
1305                                 c_ptr = &cave[yval][xval + 5];
1306                                 place_inner_grid(c_ptr);
1307
1308                                 /* Secret doors (random top/bottom) */
1309                                 place_secret_door(yval - 3 + (randint1(2) * 2), xval - 3, door_type);
1310                                 place_secret_door(yval - 3 + (randint1(2) * 2), xval + 3, door_type);
1311
1312                                 /* Monsters */
1313                                 vault_monsters(yval, xval - 2, randint1(2));
1314                                 vault_monsters(yval, xval + 2, randint1(2));
1315
1316                                 /* Objects */
1317                                 if (one_in_(3)) place_object(yval, xval - 2, 0L);
1318                                 if (one_in_(3)) place_object(yval, xval + 2, 0L);
1319                         }
1320
1321                         break;
1322                 }
1323
1324                 /* Maze inside. */
1325                 case 4:
1326                 {
1327                         /* Place a secret door */
1328                         switch (randint1(4))
1329                         {
1330                                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
1331                                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
1332                                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
1333                                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
1334                         }
1335
1336                         /* Maze (really a checkerboard) */
1337                         for (y = y1; y <= y2; y++)
1338                         {
1339                                 for (x = x1; x <= x2; x++)
1340                                 {
1341                                         if (0x1 & (x + y))
1342                                         {
1343                                                 c_ptr = &cave[y][x];
1344                                                 place_inner_grid(c_ptr);
1345                                         }
1346                                 }
1347                         }
1348
1349                         /* Monsters just love mazes. */
1350                         vault_monsters(yval, xval - 5, randint1(3));
1351                         vault_monsters(yval, xval + 5, randint1(3));
1352
1353                         /* Traps make them entertaining. */
1354                         vault_traps(yval, xval - 3, 2, 8, randint1(3));
1355                         vault_traps(yval, xval + 3, 2, 8, randint1(3));
1356
1357                         /* Mazes should have some treasure too. */
1358                         vault_objects(yval, xval, 3);
1359
1360                         break;
1361                 }
1362
1363                 /* Four small rooms. */
1364                 case 5:
1365                 {
1366                         int door_type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
1367                                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
1368                                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
1369
1370                         /* Inner "cross" */
1371                         for (y = y1; y <= y2; y++)
1372                         {
1373                                 c_ptr = &cave[y][xval];
1374                                 place_inner_grid(c_ptr);
1375                         }
1376                         for (x = x1; x <= x2; x++)
1377                         {
1378                                 c_ptr = &cave[yval][x];
1379                                 place_inner_grid(c_ptr);
1380                         }
1381
1382                         /* Doors into the rooms */
1383                         if (randint0(100) < 50)
1384                         {
1385                                 int i = randint1(10);
1386                                 place_secret_door(y1 - 1, xval - i, door_type);
1387                                 place_secret_door(y1 - 1, xval + i, door_type);
1388                                 place_secret_door(y2 + 1, xval - i, door_type);
1389                                 place_secret_door(y2 + 1, xval + i, door_type);
1390                         }
1391                         else
1392                         {
1393                                 int i = randint1(3);
1394                                 place_secret_door(yval + i, x1 - 1, door_type);
1395                                 place_secret_door(yval - i, x1 - 1, door_type);
1396                                 place_secret_door(yval + i, x2 + 1, door_type);
1397                                 place_secret_door(yval - i, x2 + 1, door_type);
1398                         }
1399
1400                         /* Treasure, centered at the center of the cross */
1401                         vault_objects(yval, xval, 2 + randint1(2));
1402
1403                         /* Gotta have some monsters. */
1404                         vault_monsters(yval + 1, xval - 4, randint1(4));
1405                         vault_monsters(yval + 1, xval + 4, randint1(4));
1406                         vault_monsters(yval - 1, xval - 4, randint1(4));
1407                         vault_monsters(yval - 1, xval + 4, randint1(4));
1408
1409                         break;
1410                 }
1411         }
1412
1413         return TRUE;
1414 }
1415
1416
1417
1418 /*!
1419  * vaultに配置可能なモンスターの条件を指定するマクロ / Monster validation macro
1420  *
1421  * Line 1 -- forbid town monsters
1422  * Line 2 -- forbid uniques
1423  * Line 3 -- forbid aquatic monsters
1424  */
1425 #define vault_monster_okay(I) \
1426         (mon_hook_dungeon(I) && \
1427          !(r_info[I].flags1 & RF1_UNIQUE) && \
1428          !(r_info[I].flags7 & RF7_UNIQUE2) && \
1429          !(r_info[I].flagsr & RFR_RES_ALL) && \
1430          !(r_info[I].flags7 & RF7_AQUATIC))
1431
1432
1433 /*! 通常pit生成時のモンスターの構成条件ID / Race index for "monster pit (clone)" */
1434 static int vault_aux_race;
1435
1436 /*! 単一シンボルpit生成時の指定シンボル / Race index for "monster pit (symbol clone)" */
1437 static char vault_aux_char;
1438
1439 /*! ブレス属性に基づくドラゴンpit生成時条件マスク / Breath mask for "monster pit (dragon)" */
1440 static u32b vault_aux_dragon_mask4;
1441
1442
1443 /*!
1444  * @brief モンスターがVault生成の最低必要条件を満たしているかを返す /
1445  * Helper monster selection function
1446  * @param r_idx 確認したいモンスター種族ID
1447  * @return Vault生成の最低必要条件を満たしているならTRUEを返す。
1448  */
1449 static bool vault_aux_simple(int r_idx)
1450 {
1451         /* Okay */
1452         return (vault_monster_okay(r_idx));
1453 }
1454
1455
1456 /*!
1457  * @brief モンスターがゼリーnestの生成必要条件を満たしているかを返す /
1458  * Helper function for "monster nest (jelly)"
1459  * @param r_idx 確認したいモンスター種族ID
1460  * @return 生成必要条件を満たしているならTRUEを返す。
1461  */
1462 static bool vault_aux_jelly(int r_idx)
1463 {
1464         monster_race *r_ptr = &r_info[r_idx];
1465
1466         /* Validate the monster */
1467         if (!vault_monster_okay(r_idx)) return (FALSE);
1468
1469         if ((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW)) return (FALSE);
1470
1471         /* Also decline evil jellies (like death molds and shoggoths) */
1472         if (r_ptr->flags3 & (RF3_EVIL)) return (FALSE);
1473
1474         /* Require icky thing, jelly, mold, or mushroom */
1475         if (!my_strchr("ijm,", r_ptr->d_char)) return (FALSE);
1476
1477         /* Okay */
1478         return (TRUE);
1479 }
1480
1481 /*!
1482  * @brief モンスターが動物nestの生成必要条件を満たしているかを返す /
1483  * Helper function for "monster nest (animal)"
1484  * @param r_idx 確認したいモンスター種族ID
1485  * @return 生成必要条件を満たしているならTRUEを返す。
1486  */
1487 static bool vault_aux_animal(int r_idx)
1488 {
1489         monster_race *r_ptr = &r_info[r_idx];
1490
1491         /* Validate the monster */
1492         if (!vault_monster_okay(r_idx)) return (FALSE);
1493
1494         /* Require "animal" flag */
1495         if (!(r_ptr->flags3 & (RF3_ANIMAL))) return (FALSE);
1496
1497         /* Okay */
1498         return (TRUE);
1499 }
1500
1501
1502 /*!
1503  * @brief モンスターがアンデッドnestの生成必要条件を満たしているかを返す /
1504  * Helper function for "monster nest (undead)"
1505  * @param r_idx 確認したいモンスター種族ID
1506  * @return 生成必要条件を満たしているならTRUEを返す。
1507  */
1508 static bool vault_aux_undead(int r_idx)
1509 {
1510         monster_race *r_ptr = &r_info[r_idx];
1511
1512         /* Validate the monster */
1513         if (!vault_monster_okay(r_idx)) return (FALSE);
1514
1515         /* Require Undead */
1516         if (!(r_ptr->flags3 & (RF3_UNDEAD))) return (FALSE);
1517
1518         /* Okay */
1519         return (TRUE);
1520 }
1521
1522 /*!
1523  * @brief モンスターが聖堂nestの生成必要条件を満たしているかを返す /
1524  * Helper function for "monster nest (chapel)"
1525  * @param r_idx 確認したいモンスター種族ID
1526  * @return 生成必要条件を満たしているならTRUEを返す。
1527  */
1528 static bool vault_aux_chapel_g(int r_idx)
1529 {
1530         static int chapel_list[] = {
1531                 MON_NOV_PRIEST, MON_NOV_PALADIN, MON_NOV_PRIEST_G, MON_NOV_PALADIN_G, 
1532                 MON_PRIEST, MON_JADE_MONK, MON_IVORY_MONK, MON_ULTRA_PALADIN, 
1533                 MON_EBONY_MONK, MON_W_KNIGHT, MON_KNI_TEMPLAR, MON_PALADIN,
1534                 MON_TOPAZ_MONK, 0};
1535
1536         int i;
1537
1538         monster_race *r_ptr = &r_info[r_idx];
1539
1540         /* Validate the monster */
1541         if (!vault_monster_okay(r_idx)) return (FALSE);
1542
1543         if (r_ptr->flags3 & (RF3_EVIL)) return (FALSE);
1544         if ((r_idx == MON_A_GOLD) || (r_idx == MON_A_SILVER)) return (FALSE);
1545
1546         /* Require "priest" or Angel */
1547
1548         if (r_ptr->d_char == 'A') return TRUE;
1549
1550         for (i = 0; chapel_list[i]; i++)
1551                 if (r_idx == chapel_list[i]) return TRUE;
1552
1553         return FALSE;
1554 }
1555
1556 /*!
1557  * @brief モンスターが犬小屋nestの生成必要条件を満たしているかを返す /
1558  * Helper function for "monster nest (kennel)"
1559  * @param r_idx 確認したいモンスター種族ID
1560  * @return 生成必要条件を満たしているならTRUEを返す。
1561  */
1562 static bool vault_aux_kennel(int r_idx)
1563 {
1564         monster_race *r_ptr = &r_info[r_idx];
1565
1566         /* Validate the monster */
1567         if (!vault_monster_okay(r_idx)) return (FALSE);
1568
1569         /* Require a Zephyr Hound or a dog */
1570         if (!my_strchr("CZ", r_ptr->d_char)) return (FALSE);
1571   
1572         /* Okay */
1573         return (TRUE);
1574 }
1575
1576 /*!
1577  * @brief モンスターがミミックnestの生成必要条件を満たしているかを返す /
1578  * Helper function for "monster nest (mimic)"
1579  * @param r_idx 確認したいモンスター種族ID
1580  * @return 生成必要条件を満たしているならTRUEを返す。
1581  */
1582 static bool vault_aux_mimic(int r_idx)
1583 {
1584         monster_race *r_ptr = &r_info[r_idx];
1585
1586         /* Validate the monster */
1587         if (!vault_monster_okay(r_idx)) return (FALSE);
1588
1589         /* Require mimic */
1590         if (!my_strchr("!$&(/=?[\\|", r_ptr->d_char)) return (FALSE);
1591
1592         /* Okay */
1593         return (TRUE);
1594 }
1595
1596 /*!
1597  * @brief モンスターが単一クローンnestの生成必要条件を満たしているかを返す /
1598  * Helper function for "monster nest (clone)"
1599  * @param r_idx 確認したいモンスター種族ID
1600  * @return 生成必要条件を満たしているならTRUEを返す。
1601  */
1602 static bool vault_aux_clone(int r_idx)
1603 {
1604         /* Validate the monster */
1605         if (!vault_monster_okay(r_idx)) return (FALSE);
1606
1607         return (r_idx == vault_aux_race);
1608 }
1609
1610
1611 /*!
1612  * @brief モンスターが邪悪属性シンボルクローンnestの生成必要条件を満たしているかを返す /
1613  * Helper function for "monster nest (symbol clone)"
1614  * @param r_idx 確認したいモンスター種族ID
1615  * @return 生成必要条件を満たしているならTRUEを返す。
1616  */
1617 static bool vault_aux_symbol_e(int r_idx)
1618 {
1619         monster_race *r_ptr = &r_info[r_idx];
1620
1621         /* Validate the monster */
1622         if (!vault_monster_okay(r_idx)) return (FALSE);
1623
1624         if ((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW)) return (FALSE);
1625
1626         if (r_ptr->flags3 & (RF3_GOOD)) return (FALSE);
1627
1628         /* Decline incorrect symbol */
1629         if (r_ptr->d_char != vault_aux_char) return (FALSE);
1630
1631         /* Okay */
1632         return (TRUE);
1633 }
1634
1635
1636 /*!
1637  * @brief モンスターが善良属性シンボルクローンnestの生成必要条件を満たしているかを返す /
1638  * Helper function for "monster nest (symbol clone)"
1639  * @param r_idx 確認したいモンスター種族ID
1640  * @return 生成必要条件を満たしているならTRUEを返す。
1641  */
1642 static bool vault_aux_symbol_g(int r_idx)
1643 {
1644         monster_race *r_ptr = &r_info[r_idx];
1645
1646         /* Validate the monster */
1647         if (!vault_monster_okay(r_idx)) return (FALSE);
1648
1649         if ((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW)) return (FALSE);
1650
1651         if (r_ptr->flags3 & (RF3_EVIL)) return (FALSE);
1652
1653         /* Decline incorrect symbol */
1654         if (r_ptr->d_char != vault_aux_char) return (FALSE);
1655
1656         /* Okay */
1657         return (TRUE);
1658 }
1659
1660
1661 /*!
1662  * @brief モンスターがオークpitの生成必要条件を満たしているかを返す /
1663  * Helper function for "monster pit (orc)"
1664  * @param r_idx 確認したいモンスター種族ID
1665  * @return 生成必要条件を満たしているならTRUEを返す。
1666  */
1667 static bool vault_aux_orc(int r_idx)
1668 {
1669         monster_race *r_ptr = &r_info[r_idx];
1670
1671         /* Validate the monster */
1672         if (!vault_monster_okay(r_idx)) return (FALSE);
1673
1674         /* Require orc */
1675         if (!(r_ptr->flags3 & RF3_ORC)) return (FALSE);
1676
1677         /* Decline undead */
1678         if (r_ptr->flags3 & RF3_UNDEAD) return (FALSE);
1679
1680         /* Okay */
1681         return (TRUE);
1682 }
1683
1684
1685 /*!
1686  * @brief モンスターがトロルpitの生成必要条件を満たしているかを返す /
1687  * Helper function for "monster pit (troll)"
1688  * @param r_idx 確認したいモンスター種族ID
1689  * @return 生成必要条件を満たしているならTRUEを返す。
1690  */
1691 static bool vault_aux_troll(int r_idx)
1692 {
1693         monster_race *r_ptr = &r_info[r_idx];
1694
1695         /* Validate the monster */
1696         if (!vault_monster_okay(r_idx)) return (FALSE);
1697
1698         /* Require troll */
1699         if (!(r_ptr->flags3 & RF3_TROLL)) return (FALSE);
1700
1701         /* Decline undead */
1702         if (r_ptr->flags3 & RF3_UNDEAD) return (FALSE);
1703
1704         /* Okay */
1705         return (TRUE);
1706 }
1707
1708
1709 /*!
1710  * @brief モンスターが巨人pitの生成必要条件を満たしているかを返す /
1711  * Helper function for "monster pit (giant)"
1712  * @param r_idx 確認したいモンスター種族ID
1713  * @return 生成必要条件を満たしているならTRUEを返す。
1714  */
1715 static bool vault_aux_giant(int r_idx)
1716 {
1717         monster_race *r_ptr = &r_info[r_idx];
1718
1719         /* Validate the monster */
1720         if (!vault_monster_okay(r_idx)) return (FALSE);
1721
1722         /* Require giant */
1723         if (!(r_ptr->flags3 & RF3_GIANT)) return (FALSE);
1724
1725         if (r_ptr->flags3 & RF3_GOOD) return (FALSE);
1726
1727         /* Decline undead */
1728         if (r_ptr->flags3 & RF3_UNDEAD) return (FALSE);
1729
1730         /* Okay */
1731         return (TRUE);
1732 }
1733
1734
1735 /*!
1736  * @brief モンスターがドラゴンpitの生成必要条件を満たしているかを返す /
1737  * Helper function for "monster pit (dragon)"
1738  * @param r_idx 確認したいモンスター種族ID
1739  * @return 生成必要条件を満たしているならTRUEを返す。
1740  */
1741 static bool vault_aux_dragon(int r_idx)
1742 {
1743         monster_race *r_ptr = &r_info[r_idx];
1744
1745         /* Validate the monster */
1746         if (!vault_monster_okay(r_idx)) return (FALSE);
1747
1748         /* Require dragon */
1749         if (!(r_ptr->flags3 & RF3_DRAGON)) return (FALSE);
1750
1751         /* Hack -- Require correct "breath attack" */
1752         if (r_ptr->flags4 != vault_aux_dragon_mask4) return (FALSE);
1753
1754         /* Decline undead */
1755         if (r_ptr->flags3 & RF3_UNDEAD) return (FALSE);
1756
1757         /* Okay */
1758         return (TRUE);
1759 }
1760
1761
1762 /*!
1763  * @brief モンスターが悪魔pitの生成必要条件を満たしているかを返す /
1764  * Helper function for "monster pit (demon)"
1765  * @param r_idx 確認したいモンスター種族ID
1766  * @return 生成必要条件を満たしているならTRUEを返す。
1767  */
1768 static bool vault_aux_demon(int r_idx)
1769 {
1770         monster_race *r_ptr = &r_info[r_idx];
1771
1772         /* Validate the monster */
1773         if (!vault_monster_okay(r_idx)) return (FALSE);
1774
1775         if ((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW)) return (FALSE);
1776
1777         /* Require demon */
1778         if (!(r_ptr->flags3 & RF3_DEMON)) return (FALSE);
1779
1780         /* Okay */
1781         return (TRUE);
1782 }
1783
1784
1785 /*!
1786  * @brief モンスターが狂気pitの生成必要条件を満たしているかを返す /
1787  * Helper function for "monster pit (lovecraftian)"
1788  * @param r_idx 確認したいモンスター種族ID
1789  * @return 生成必要条件を満たしているならTRUEを返す。
1790  */
1791 static bool vault_aux_cthulhu(int r_idx)
1792 {
1793         monster_race *r_ptr = &r_info[r_idx];
1794
1795         /* Validate the monster */
1796         if (!vault_monster_okay(r_idx)) return (FALSE);
1797
1798         if ((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW)) return (FALSE);
1799
1800         /* Require eldritch horror */
1801         if (!(r_ptr->flags2 & (RF2_ELDRITCH_HORROR))) return (FALSE);
1802
1803         /* Okay */
1804         return (TRUE);
1805 }
1806
1807
1808 /*!
1809  * @brief pit/nestの基準となる単種モンスターを決める /
1810  * @return なし
1811  */
1812 static void vault_prep_clone(void)
1813 {
1814         /* Apply the monster restriction */
1815         get_mon_num_prep(vault_aux_simple, NULL);
1816
1817         /* Pick a race to clone */
1818         vault_aux_race = get_mon_num(dun_level + 10);
1819
1820         /* Remove the monster restriction */
1821         get_mon_num_prep(NULL, NULL);
1822 }
1823
1824
1825 /*!
1826  * @brief pit/nestの基準となるモンスターシンボルを決める /
1827  * @return なし
1828  */
1829 static void vault_prep_symbol(void)
1830 {
1831         int r_idx;
1832
1833         /* Apply the monster restriction */
1834         get_mon_num_prep(vault_aux_simple, NULL);
1835
1836         /* Pick a race to clone */
1837         r_idx = get_mon_num(dun_level + 10);
1838
1839         /* Remove the monster restriction */
1840         get_mon_num_prep(NULL, NULL);
1841
1842         /* Extract the symbol */
1843         vault_aux_char = r_info[r_idx].d_char;
1844 }
1845
1846 /*!
1847  * @brief pit/nestの基準となるドラゴンの種類を決める /
1848  * @return なし
1849  */
1850 static void vault_prep_dragon(void)
1851 {
1852         /* Pick dragon type */
1853         switch (randint0(6))
1854         {
1855                 /* Black */
1856                 case 0:
1857                 {
1858                         /* Restrict dragon breath type */
1859                         vault_aux_dragon_mask4 = RF4_BR_ACID;
1860
1861                         /* Done */
1862                         break;
1863                 }
1864
1865                 /* Blue */
1866                 case 1:
1867                 {
1868                         /* Restrict dragon breath type */
1869                         vault_aux_dragon_mask4 = RF4_BR_ELEC;
1870
1871                         /* Done */
1872                         break;
1873                 }
1874
1875                 /* Red */
1876                 case 2:
1877                 {
1878                         /* Restrict dragon breath type */
1879                         vault_aux_dragon_mask4 = RF4_BR_FIRE;
1880
1881                         /* Done */
1882                         break;
1883                 }
1884
1885                 /* White */
1886                 case 3:
1887                 {
1888                         /* Restrict dragon breath type */
1889                         vault_aux_dragon_mask4 = RF4_BR_COLD;
1890
1891                         /* Done */
1892                         break;
1893                 }
1894
1895                 /* Green */
1896                 case 4:
1897                 {
1898                         /* Restrict dragon breath type */
1899                         vault_aux_dragon_mask4 = RF4_BR_POIS;
1900
1901                         /* Done */
1902                         break;
1903                 }
1904
1905                 /* Multi-hued */
1906                 default:
1907                 {
1908                         /* Restrict dragon breath type */
1909                         vault_aux_dragon_mask4 = (RF4_BR_ACID | RF4_BR_ELEC |
1910                                                                                           RF4_BR_FIRE | RF4_BR_COLD |
1911                                                                                           RF4_BR_POIS);
1912
1913                         /* Done */
1914                         break;
1915                 }
1916         }
1917 }
1918
1919
1920 /*!
1921  * @brief モンスターがダークエルフpitの生成必要条件を満たしているかを返す /
1922  * Helper function for "monster pit (dark elf)"
1923  * @param r_idx 確認したいモンスター種族ID
1924  * @return 生成必要条件を満たしているならTRUEを返す。
1925  */
1926 static bool vault_aux_dark_elf(int r_idx)
1927 {
1928         int i;
1929         static int dark_elf_list[] =
1930         {
1931                 MON_D_ELF, MON_D_ELF_MAGE, MON_D_ELF_WARRIOR, MON_D_ELF_PRIEST,
1932                 MON_D_ELF_LORD, MON_D_ELF_WARLOCK, MON_D_ELF_DRUID, MON_NIGHTBLADE,
1933                 MON_D_ELF_SORC, MON_D_ELF_SHADE, 0,
1934         };
1935
1936         /* Validate the monster */
1937         if (!vault_monster_okay(r_idx)) return FALSE;
1938
1939         /* Require dark elves */
1940         for (i = 0; dark_elf_list[i]; i++)
1941                 if (r_idx == dark_elf_list[i]) return TRUE;
1942
1943         /* Assume not */
1944         return FALSE;
1945 }
1946
1947 /*! pit/nest型情報のtypedef */
1948 typedef struct vault_aux_type vault_aux_type;
1949
1950 /*! pit/nest型情報の構造体定義 */
1951 struct vault_aux_type
1952 {
1953         cptr name;
1954         bool (*hook_func)(int r_idx);
1955         void (*prep_func)(void);
1956         int level;
1957         int chance;
1958 };
1959
1960 /*!
1961  * @brief ダンジョン毎に指定されたピット配列を基準にランダムなpit/nestタイプを決める
1962  * @param l_ptr 選択されたpit/nest情報を返す参照ポインタ
1963  * @param allow_flag_mask 生成が許されるpit/nestのビット配列
1964  * @return 選択されたpit/nestのID、選択失敗した場合-1を返す。
1965  */
1966 static int pick_vault_type(vault_aux_type *l_ptr, s16b allow_flag_mask)
1967 {
1968         int tmp, total, count;
1969
1970         vault_aux_type *n_ptr;
1971
1972         /* Calculate the total possibilities */
1973         for (n_ptr = l_ptr, total = 0, count = 0; TRUE; n_ptr++, count++)
1974         {
1975                 /* Note end */
1976                 if (!n_ptr->name) break;
1977
1978                 /* Ignore excessive depth */
1979                 if (n_ptr->level > dun_level) continue;
1980
1981                 /* Not matched with pit/nest flag */
1982                 if (!(allow_flag_mask & (1L << count))) continue;
1983
1984                 /* Count this possibility */
1985                 total += n_ptr->chance * MAX_DEPTH / (MIN(dun_level, MAX_DEPTH - 1) - n_ptr->level + 5);
1986         }
1987
1988         /* Pick a random type */
1989         tmp = randint0(total);
1990
1991         /* Find this type */
1992         for (n_ptr = l_ptr, total = 0, count = 0; TRUE; n_ptr++, count++)
1993         {
1994                 /* Note end */
1995                 if (!n_ptr->name) break;
1996
1997                 /* Ignore excessive depth */
1998                 if (n_ptr->level > dun_level) continue;
1999
2000                 /* Not matched with pit/nest flag */
2001                 if (!(allow_flag_mask & (1L << count))) continue;
2002
2003                 /* Count this possibility */
2004                 total += n_ptr->chance * MAX_DEPTH / (MIN(dun_level, MAX_DEPTH - 1) - n_ptr->level + 5);
2005
2006                 /* Found the type */
2007                 if (tmp < total) break;
2008         }
2009
2010         return n_ptr->name ? count : -1;
2011 }
2012
2013 /*!nest情報テーブル*/
2014 static vault_aux_type nest_types[] =
2015 {
2016 #ifdef JP
2017         {"クローン",     vault_aux_clone,    vault_prep_clone,   5, 3},
2018         {"ゼリー",       vault_aux_jelly,    NULL,               5, 6},
2019         {"シンボル(善)", vault_aux_symbol_g, vault_prep_symbol, 25, 2},
2020         {"シンボル(悪)", vault_aux_symbol_e, vault_prep_symbol, 25, 2},
2021         {"ミミック",     vault_aux_mimic,    NULL,              30, 4},
2022         {"狂気",         vault_aux_cthulhu,  NULL,              70, 2},
2023         {"犬小屋",       vault_aux_kennel,   NULL,              45, 4},
2024         {"動物園",       vault_aux_animal,   NULL,              35, 5},
2025         {"教会",         vault_aux_chapel_g, NULL,              75, 4},
2026         {"アンデッド",   vault_aux_undead,   NULL,              75, 5},
2027         {NULL,           NULL,               NULL,               0, 0},
2028 #else
2029         {"clone",        vault_aux_clone,    vault_prep_clone,   5, 3},
2030         {"jelly",        vault_aux_jelly,    NULL,               5, 6},
2031         {"symbol good",  vault_aux_symbol_g, vault_prep_symbol, 25, 2},
2032         {"symbol evil",  vault_aux_symbol_e, vault_prep_symbol, 25, 2},
2033         {"mimic",        vault_aux_mimic,    NULL,              30, 4},
2034         {"lovecraftian", vault_aux_cthulhu,  NULL,              70, 2},
2035         {"kennel",       vault_aux_kennel,   NULL,              45, 4},
2036         {"animal",       vault_aux_animal,   NULL,              35, 5},
2037         {"chapel",       vault_aux_chapel_g, NULL,              75, 4},
2038         {"undead",       vault_aux_undead,   NULL,              75, 5},
2039         {NULL,           NULL,               NULL,               0, 0},
2040 #endif
2041 };
2042
2043 /*!pit情報テーブル*/
2044 static vault_aux_type pit_types[] =
2045 {
2046 #ifdef JP
2047         {"オーク",       vault_aux_orc,      NULL,               5, 6},
2048         {"トロル",       vault_aux_troll,    NULL,              20, 6},
2049         {"ジャイアント", vault_aux_giant,    NULL,              50, 6},
2050         {"狂気",         vault_aux_cthulhu,  NULL,              80, 2},
2051         {"シンボル(善)", vault_aux_symbol_g, vault_prep_symbol, 70, 1},
2052         {"シンボル(悪)", vault_aux_symbol_e, vault_prep_symbol, 70, 1},
2053         {"教会",         vault_aux_chapel_g, NULL,              65, 2},
2054         {"ドラゴン",     vault_aux_dragon,   vault_prep_dragon, 70, 6},
2055         {"デーモン",     vault_aux_demon,    NULL,              80, 6},
2056         {"ダークエルフ", vault_aux_dark_elf, NULL,              45, 4},
2057         {NULL,           NULL,               NULL,               0, 0},
2058 #else
2059         {"orc",          vault_aux_orc,      NULL,               5, 6},
2060         {"troll",        vault_aux_troll,    NULL,              20, 6},
2061         {"giant",        vault_aux_giant,    NULL,              50, 6},
2062         {"lovecraftian", vault_aux_cthulhu,  NULL,              80, 2},
2063         {"symbol good",  vault_aux_symbol_g, vault_prep_symbol, 70, 1},
2064         {"symbol evil",  vault_aux_symbol_e, vault_prep_symbol, 70, 1},
2065         {"chapel",       vault_aux_chapel_g, NULL,              65, 2},
2066         {"dragon",       vault_aux_dragon,   vault_prep_dragon, 70, 6},
2067         {"demon",        vault_aux_demon,    NULL,              80, 6},
2068         {"dark elf",     vault_aux_dark_elf, NULL,              45, 4},
2069         {NULL,           NULL,               NULL,               0, 0},
2070 #endif
2071 };
2072
2073
2074 /*! nestのID定義 /  Nest types code */
2075 #define NEST_TYPE_CLONE        0
2076 #define NEST_TYPE_JELLY        1
2077 #define NEST_TYPE_SYMBOL_GOOD  2
2078 #define NEST_TYPE_SYMBOL_EVIL  3
2079 #define NEST_TYPE_MIMIC        4
2080 #define NEST_TYPE_LOVECRAFTIAN 5
2081 #define NEST_TYPE_KENNEL       6
2082 #define NEST_TYPE_ANIMAL       7
2083 #define NEST_TYPE_CHAPEL       8
2084 #define NEST_TYPE_UNDEAD       9
2085
2086 /*! pitのID定義 / Pit types code */
2087 #define PIT_TYPE_ORC           0
2088 #define PIT_TYPE_TROLL         1
2089 #define PIT_TYPE_GIANT         2
2090 #define PIT_TYPE_LOVECRAFTIAN  3
2091 #define PIT_TYPE_SYMBOL_GOOD   4
2092 #define PIT_TYPE_SYMBOL_EVIL   5
2093 #define PIT_TYPE_CHAPEL        6
2094 #define PIT_TYPE_DRAGON        7
2095 #define PIT_TYPE_DEMON         8
2096 #define PIT_TYPE_DARK_ELF      9
2097
2098
2099 /*!
2100  * @brief デバッグ時に生成されたpit/nestの型を出力する処理
2101  * @param type pit/nestの型ID
2102  * @param nest TRUEならばnest、FALSEならばpit
2103  * @return デバッグ表示文字列の参照ポインタ
2104  * @details
2105  * Hack -- Get the string describing subtype of pit/nest
2106  * Determined in prepare function (some pit/nest only)
2107  */
2108 static cptr pit_subtype_string(int type, bool nest)
2109 {
2110         static char inner_buf[256] = "";
2111
2112         inner_buf[0] = '\0'; /* Init string */
2113
2114         if (nest) /* Nests */
2115         {
2116                 switch (type)
2117                 {
2118                 case NEST_TYPE_CLONE:
2119                         sprintf(inner_buf, "(%s)", r_name + r_info[vault_aux_race].name);
2120                         break;
2121                 case NEST_TYPE_SYMBOL_GOOD:
2122                 case NEST_TYPE_SYMBOL_EVIL:
2123                         sprintf(inner_buf, "(%c)", vault_aux_char);
2124                         break;
2125                 }
2126         }
2127         else /* Pits */
2128         {
2129                 switch (type)
2130                 {
2131                 case PIT_TYPE_SYMBOL_GOOD:
2132                 case PIT_TYPE_SYMBOL_EVIL:
2133                         sprintf(inner_buf, "(%c)", vault_aux_char);
2134                         break;
2135                 case PIT_TYPE_DRAGON:
2136                         switch (vault_aux_dragon_mask4)
2137                         {
2138 #ifdef JP
2139                         case RF4_BR_ACID: strcpy(inner_buf, "(酸)");   break;
2140                         case RF4_BR_ELEC: strcpy(inner_buf, "(稲妻)"); break;
2141                         case RF4_BR_FIRE: strcpy(inner_buf, "(火炎)"); break;
2142                         case RF4_BR_COLD: strcpy(inner_buf, "(冷気)"); break;
2143                         case RF4_BR_POIS: strcpy(inner_buf, "(毒)");   break;
2144                         case (RF4_BR_ACID | RF4_BR_ELEC | RF4_BR_FIRE | RF4_BR_COLD | RF4_BR_POIS):
2145                                 strcpy(inner_buf, "(万色)"); break;
2146                         default: strcpy(inner_buf, "(未定義)"); break;
2147 #else
2148                         case RF4_BR_ACID: strcpy(inner_buf, "(acid)");      break;
2149                         case RF4_BR_ELEC: strcpy(inner_buf, "(lightning)"); break;
2150                         case RF4_BR_FIRE: strcpy(inner_buf, "(fire)");      break;
2151                         case RF4_BR_COLD: strcpy(inner_buf, "(frost)");     break;
2152                         case RF4_BR_POIS: strcpy(inner_buf, "(poison)");    break;
2153                         case (RF4_BR_ACID | RF4_BR_ELEC | RF4_BR_FIRE | RF4_BR_COLD | RF4_BR_POIS):
2154                                 strcpy(inner_buf, "(multi-hued)"); break;
2155                         default: strcpy(inner_buf, "(undefined)"); break;
2156 #endif
2157                         }
2158                         break;
2159                 }
2160         }
2161
2162         return inner_buf;
2163 }
2164
2165
2166 /*! デバッグ時にnestのモンスター情報を確認するための構造体 / A struct for nest monster information with cheat_hear */
2167 typedef struct
2168 {
2169         s16b r_idx;
2170         bool used;
2171 }
2172 nest_mon_info_type;
2173
2174
2175 /*
2176  *! @brief nestのモンスターリストをソートするための関数 /
2177  *  Comp function for sorting nest monster information
2178  *  @param u ソート処理対象配列ポインタ
2179  *  @param v 未使用
2180  *  @param a 比較対象参照ID1
2181  *  @param b 比較対象参照ID2
2182  */
2183 static bool ang_sort_comp_nest_mon_info(vptr u, vptr v, int a, int b)
2184 {
2185         nest_mon_info_type *nest_mon_info = (nest_mon_info_type *)u;
2186         int w1 = nest_mon_info[a].r_idx;
2187         int w2 = nest_mon_info[b].r_idx;
2188         monster_race *r1_ptr = &r_info[w1];
2189         monster_race *r2_ptr = &r_info[w2];
2190         int z1, z2;
2191
2192         /* Unused */
2193         (void)v;
2194
2195         /* Extract used info */
2196         z1 = nest_mon_info[a].used;
2197         z2 = nest_mon_info[b].used;
2198
2199         /* Compare used status */
2200         if (z1 < z2) return FALSE;
2201         if (z1 > z2) return TRUE;
2202
2203         /* Compare levels */
2204         if (r1_ptr->level < r2_ptr->level) return TRUE;
2205         if (r1_ptr->level > r2_ptr->level) return FALSE;
2206
2207         /* Compare experience */
2208         if (r1_ptr->mexp < r2_ptr->mexp) return TRUE;
2209         if (r1_ptr->mexp > r2_ptr->mexp) return FALSE;
2210
2211         /* Compare indexes */
2212         return w1 <= w2;
2213 }
2214
2215 /*!
2216  * @brief nestのモンスターリストをスワップするための関数 /
2217  * Swap function for sorting nest monster information
2218  * @param u スワップ処理対象配列ポインタ
2219  * @param v 未使用
2220  * @param a スワップ対象参照ID1
2221  * @param b スワップ対象参照ID2
2222  */
2223 static void ang_sort_swap_nest_mon_info(vptr u, vptr v, int a, int b)
2224 {
2225         nest_mon_info_type *nest_mon_info = (nest_mon_info_type *)u;
2226         nest_mon_info_type holder;
2227
2228         /* Unused */
2229         (void)v;
2230
2231         /* Swap */
2232         holder = nest_mon_info[a];
2233         nest_mon_info[a] = nest_mon_info[b];
2234         nest_mon_info[b] = holder;
2235 }
2236
2237
2238 #define NUM_NEST_MON_TYPE 64 /*!<nestの種別数 */
2239
2240
2241 /*!
2242  * @brief タイプ5の部屋…nestを生成する / Type 5 -- Monster nests
2243  * @return なし
2244  * @details
2245  * A monster nest is a "big" room, with an "inner" room, containing\n
2246  * a "collection" of monsters of a given type strewn about the room.\n
2247  *\n
2248  * The monsters are chosen from a set of 64 randomly selected monster\n
2249  * races, to allow the nest creation to fail instead of having "holes".\n
2250  *\n
2251  * Note the use of the "get_mon_num_prep()" function, and the special\n
2252  * "get_mon_num_hook()" restriction function, to prepare the "monster\n
2253  * allocation table" in such a way as to optimize the selection of\n
2254  * "appropriate" non-unique monsters for the nest.\n
2255  *\n
2256  * Note that the "get_mon_num()" function may (rarely) fail, in which\n
2257  * case the nest will be empty.\n
2258  *\n
2259  * Note that "monster nests" will never contain "unique" monsters.\n
2260  */
2261 static bool build_type5(void)
2262 {
2263         int y, x, y1, x1, y2, x2, xval, yval;
2264         int i;
2265         nest_mon_info_type nest_mon_info[NUM_NEST_MON_TYPE];
2266
2267         monster_type align;
2268
2269         cave_type *c_ptr;
2270
2271         int cur_nest_type = pick_vault_type(nest_types, d_info[dungeon_type].nest);
2272         vault_aux_type *n_ptr;
2273
2274         /* No type available */
2275         if (cur_nest_type < 0) return FALSE;
2276
2277         n_ptr = &nest_types[cur_nest_type];
2278
2279         /* Process a preparation function if necessary */
2280         if (n_ptr->prep_func) (*(n_ptr->prep_func))();
2281
2282         /* Prepare allocation table */
2283         get_mon_num_prep(n_ptr->hook_func, NULL);
2284
2285         align.sub_align = SUB_ALIGN_NEUTRAL;
2286
2287         /* Pick some monster types */
2288         for (i = 0; i < NUM_NEST_MON_TYPE; i++)
2289         {
2290                 int r_idx = 0, attempts = 100;
2291                 monster_race *r_ptr = NULL;
2292
2293                 while (attempts--)
2294                 {
2295                         /* Get a (hard) monster type */
2296                         r_idx = get_mon_num(dun_level + 11);
2297                         r_ptr = &r_info[r_idx];
2298
2299                         /* Decline incorrect alignment */
2300                         if (monster_has_hostile_align(&align, 0, 0, r_ptr)) continue;
2301
2302                         /* Accept this monster */
2303                         break;
2304                 }
2305
2306                 /* Notice failure */
2307                 if (!r_idx || !attempts) return FALSE;
2308
2309                 /* Note the alignment */
2310                 if (r_ptr->flags3 & RF3_EVIL) align.sub_align |= SUB_ALIGN_EVIL;
2311                 if (r_ptr->flags3 & RF3_GOOD) align.sub_align |= SUB_ALIGN_GOOD;
2312
2313                 nest_mon_info[i].r_idx = r_idx;
2314                 nest_mon_info[i].used = FALSE;
2315         }
2316
2317         /* Find and reserve some space in the dungeon.  Get center of room. */
2318         if (!find_space(&yval, &xval, 11, 25)) return FALSE;
2319
2320         /* Large room */
2321         y1 = yval - 4;
2322         y2 = yval + 4;
2323         x1 = xval - 11;
2324         x2 = xval + 11;
2325
2326         /* Place the floor area */
2327         for (y = y1 - 1; y <= y2 + 1; y++)
2328         {
2329                 for (x = x1 - 1; x <= x2 + 1; x++)
2330                 {
2331                         c_ptr = &cave[y][x];
2332                         place_floor_grid(c_ptr);
2333                         c_ptr->info |= (CAVE_ROOM);
2334                 }
2335         }
2336
2337         /* Place the outer walls */
2338         for (y = y1 - 1; y <= y2 + 1; y++)
2339         {
2340                 c_ptr = &cave[y][x1 - 1];
2341                 place_outer_grid(c_ptr);
2342                 c_ptr = &cave[y][x2 + 1];
2343                 place_outer_grid(c_ptr);
2344         }
2345         for (x = x1 - 1; x <= x2 + 1; x++)
2346         {
2347                 c_ptr = &cave[y1 - 1][x];
2348                 place_outer_grid(c_ptr);
2349                 c_ptr = &cave[y2 + 1][x];
2350                 place_outer_grid(c_ptr);
2351         }
2352
2353
2354         /* Advance to the center room */
2355         y1 = y1 + 2;
2356         y2 = y2 - 2;
2357         x1 = x1 + 2;
2358         x2 = x2 - 2;
2359
2360         /* The inner walls */
2361         for (y = y1 - 1; y <= y2 + 1; y++)
2362         {
2363                 c_ptr = &cave[y][x1 - 1];
2364                 place_inner_grid(c_ptr);
2365                 c_ptr = &cave[y][x2 + 1];
2366                 place_inner_grid(c_ptr);
2367         }
2368
2369         for (x = x1 - 1; x <= x2 + 1; x++)
2370         {
2371                 c_ptr = &cave[y1 - 1][x];
2372                 place_inner_grid(c_ptr);
2373                 c_ptr = &cave[y2 + 1][x];
2374                 place_inner_grid(c_ptr);
2375         }
2376         for (y = y1; y <= y2; y++)
2377         {
2378                 for (x = x1; x <= x2; x++)
2379                 {
2380                         add_cave_info(y, x, CAVE_ICKY);
2381                 }
2382         }
2383
2384         /* Place a secret door */
2385         switch (randint1(4))
2386         {
2387                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
2388                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
2389                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
2390                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
2391         }
2392
2393         /* Describe */
2394         if (cheat_room)
2395         {
2396                 /* Room type */
2397                 msg_format(_("モンスター部屋(nest)(%s%s)", "Monster nest (%s%s)"), n_ptr->name, pit_subtype_string(cur_nest_type, TRUE));
2398         }
2399
2400         /* Place some monsters */
2401         for (y = yval - 2; y <= yval + 2; y++)
2402         {
2403                 for (x = xval - 9; x <= xval + 9; x++)
2404                 {
2405                         int r_idx;
2406
2407                         i = randint0(NUM_NEST_MON_TYPE);
2408                         r_idx = nest_mon_info[i].r_idx;
2409
2410                         /* Place that "random" monster (no groups) */
2411                         (void)place_monster_aux(0, y, x, r_idx, 0L);
2412
2413                         nest_mon_info[i].used = TRUE;
2414                 }
2415         }
2416
2417         if (cheat_room && cheat_hear)
2418         {
2419                 ang_sort_comp = ang_sort_comp_nest_mon_info;
2420                 ang_sort_swap = ang_sort_swap_nest_mon_info;
2421                 ang_sort(nest_mon_info, NULL, NUM_NEST_MON_TYPE);
2422
2423                 /* Dump the entries (prevent multi-printing) */
2424                 for (i = 0; i < NUM_NEST_MON_TYPE; i++)
2425                 {
2426                         if (!nest_mon_info[i].used) break;
2427                         for (; i < NUM_NEST_MON_TYPE - 1; i++)
2428                         {
2429                                 if (nest_mon_info[i].r_idx != nest_mon_info[i + 1].r_idx) break;
2430                                 if (!nest_mon_info[i + 1].used) break;
2431                         }
2432                         msg_print(r_name + r_info[nest_mon_info[i].r_idx].name);
2433                 }
2434         }
2435
2436         return TRUE;
2437 }
2438
2439
2440 /*!
2441  * @brief タイプ6の部屋…pitを生成する / Type 6 -- Monster pits
2442  * @return なし
2443  * @details
2444  * A monster pit is a "big" room, with an "inner" room, containing\n
2445  * a "collection" of monsters of a given type organized in the room.\n
2446  *\n
2447  * The inside room in a monster pit appears as shown below, where the\n
2448  * actual monsters in each location depend on the type of the pit\n
2449  *\n
2450  *   XXXXXXXXXXXXXXXXXXXXX\n
2451  *   X0000000000000000000X\n
2452  *   X0112233455543322110X\n
2453  *   X0112233467643322110X\n
2454  *   X0112233455543322110X\n
2455  *   X0000000000000000000X\n
2456  *   XXXXXXXXXXXXXXXXXXXXX\n
2457  *\n
2458  * Note that the monsters in the pit are now chosen by using "get_mon_num()"\n
2459  * to request 16 "appropriate" monsters, sorting them by level, and using\n
2460  * the "even" entries in this sorted list for the contents of the pit.\n
2461  *\n
2462  * Hack -- all of the "dragons" in a "dragon" pit must be the same "color",\n
2463  * which is handled by requiring a specific "breath" attack for all of the\n
2464  * dragons.  This may include "multi-hued" breath.  Note that "wyrms" may\n
2465  * be present in many of the dragon pits, if they have the proper breath.\n
2466  *\n
2467  * Note the use of the "get_mon_num_prep()" function, and the special\n
2468  * "get_mon_num_hook()" restriction function, to prepare the "monster\n
2469  * allocation table" in such a way as to optimize the selection of\n
2470  * "appropriate" non-unique monsters for the pit.\n
2471  *\n
2472  * Note that the "get_mon_num()" function may (rarely) fail, in which case\n
2473  * the pit will be empty.\n
2474  *\n
2475  * Note that "monster pits" will never contain "unique" monsters.\n
2476  */
2477 static bool build_type6(void)
2478 {
2479         int y, x, y1, x1, y2, x2, xval, yval;
2480         int i, j;
2481
2482         int what[16];
2483
2484         monster_type align;
2485
2486         cave_type *c_ptr;
2487
2488         int cur_pit_type = pick_vault_type(pit_types, d_info[dungeon_type].pit);
2489         vault_aux_type *n_ptr;
2490
2491         /* No type available */
2492         if (cur_pit_type < 0) return FALSE;
2493
2494         n_ptr = &pit_types[cur_pit_type];
2495
2496         /* Process a preparation function if necessary */
2497         if (n_ptr->prep_func) (*(n_ptr->prep_func))();
2498
2499         /* Prepare allocation table */
2500         get_mon_num_prep(n_ptr->hook_func, NULL);
2501
2502         align.sub_align = SUB_ALIGN_NEUTRAL;
2503
2504         /* Pick some monster types */
2505         for (i = 0; i < 16; i++)
2506         {
2507                 int r_idx = 0, attempts = 100;
2508                 monster_race *r_ptr = NULL;
2509
2510                 while (attempts--)
2511                 {
2512                         /* Get a (hard) monster type */
2513                         r_idx = get_mon_num(dun_level + 11);
2514                         r_ptr = &r_info[r_idx];
2515
2516                         /* Decline incorrect alignment */
2517                         if (monster_has_hostile_align(&align, 0, 0, r_ptr)) continue;
2518
2519                         /* Accept this monster */
2520                         break;
2521                 }
2522
2523                 /* Notice failure */
2524                 if (!r_idx || !attempts) return FALSE;
2525
2526                 /* Note the alignment */
2527                 if (r_ptr->flags3 & RF3_EVIL) align.sub_align |= SUB_ALIGN_EVIL;
2528                 if (r_ptr->flags3 & RF3_GOOD) align.sub_align |= SUB_ALIGN_GOOD;
2529
2530                 what[i] = r_idx;
2531         }
2532
2533         /* Find and reserve some space in the dungeon.  Get center of room. */
2534         if (!find_space(&yval, &xval, 11, 25)) return FALSE;
2535
2536         /* Large room */
2537         y1 = yval - 4;
2538         y2 = yval + 4;
2539         x1 = xval - 11;
2540         x2 = xval + 11;
2541
2542         /* Place the floor area */
2543         for (y = y1 - 1; y <= y2 + 1; y++)
2544         {
2545                 for (x = x1 - 1; x <= x2 + 1; x++)
2546                 {
2547                         c_ptr = &cave[y][x];
2548                         place_floor_grid(c_ptr);
2549                         c_ptr->info |= (CAVE_ROOM);
2550                 }
2551         }
2552
2553         /* Place the outer walls */
2554         for (y = y1 - 1; y <= y2 + 1; y++)
2555         {
2556                 c_ptr = &cave[y][x1 - 1];
2557                 place_outer_grid(c_ptr);
2558                 c_ptr = &cave[y][x2 + 1];
2559                 place_outer_grid(c_ptr);
2560         }
2561         for (x = x1 - 1; x <= x2 + 1; x++)
2562         {
2563                 c_ptr = &cave[y1 - 1][x];
2564                 place_outer_grid(c_ptr);
2565                 c_ptr = &cave[y2 + 1][x];
2566                 place_outer_grid(c_ptr);
2567         }
2568
2569         /* Advance to the center room */
2570         y1 = y1 + 2;
2571         y2 = y2 - 2;
2572         x1 = x1 + 2;
2573         x2 = x2 - 2;
2574
2575         /* The inner walls */
2576         for (y = y1 - 1; y <= y2 + 1; y++)
2577         {
2578                 c_ptr = &cave[y][x1 - 1];
2579                 place_inner_grid(c_ptr);
2580                 c_ptr = &cave[y][x2 + 1];
2581                 place_inner_grid(c_ptr);
2582         }
2583         for (x = x1 - 1; x <= x2 + 1; x++)
2584         {
2585                 c_ptr = &cave[y1 - 1][x];
2586                 place_inner_grid(c_ptr);
2587                 c_ptr = &cave[y2 + 1][x];
2588                 place_inner_grid(c_ptr);
2589         }
2590         for (y = y1; y <= y2; y++)
2591         {
2592                 for (x = x1; x <= x2; x++)
2593                 {
2594                         add_cave_info(y, x, CAVE_ICKY);
2595                 }
2596         }
2597
2598         /* Place a secret door */
2599         switch (randint1(4))
2600         {
2601                 case 1: place_secret_door(y1 - 1, xval, DOOR_DEFAULT); break;
2602                 case 2: place_secret_door(y2 + 1, xval, DOOR_DEFAULT); break;
2603                 case 3: place_secret_door(yval, x1 - 1, DOOR_DEFAULT); break;
2604                 case 4: place_secret_door(yval, x2 + 1, DOOR_DEFAULT); break;
2605         }
2606
2607         /* Sort the entries */
2608         for (i = 0; i < 16 - 1; i++)
2609         {
2610                 /* Sort the entries */
2611                 for (j = 0; j < 16 - 1; j++)
2612                 {
2613                         int i1 = j;
2614                         int i2 = j + 1;
2615
2616                         int p1 = r_info[what[i1]].level;
2617                         int p2 = r_info[what[i2]].level;
2618
2619                         /* Bubble */
2620                         if (p1 > p2)
2621                         {
2622                                 int tmp = what[i1];
2623                                 what[i1] = what[i2];
2624                                 what[i2] = tmp;
2625                         }
2626                 }
2627         }
2628
2629         /* Message */
2630         if (cheat_room)
2631         {
2632                 /* Room type */
2633                 msg_format(_("モンスター部屋(pit)(%s%s)", "Monster pit (%s%s)"), n_ptr->name, pit_subtype_string(cur_pit_type, FALSE));
2634         }
2635
2636         /* Select the entries */
2637         for (i = 0; i < 8; i++)
2638         {
2639                 /* Every other entry */
2640                 what[i] = what[i * 2];
2641
2642                 if (cheat_hear)
2643                 {
2644                         /* Message */
2645                         msg_print(r_name + r_info[what[i]].name);
2646                 }
2647         }
2648
2649         /* Top and bottom rows */
2650         for (x = xval - 9; x <= xval + 9; x++)
2651         {
2652                 place_monster_aux(0, yval - 2, x, what[0], PM_NO_KAGE);
2653                 place_monster_aux(0, yval + 2, x, what[0], PM_NO_KAGE);
2654         }
2655
2656         /* Middle columns */
2657         for (y = yval - 1; y <= yval + 1; y++)
2658         {
2659                 place_monster_aux(0, y, xval - 9, what[0], PM_NO_KAGE);
2660                 place_monster_aux(0, y, xval + 9, what[0], PM_NO_KAGE);
2661
2662                 place_monster_aux(0, y, xval - 8, what[1], PM_NO_KAGE);
2663                 place_monster_aux(0, y, xval + 8, what[1], PM_NO_KAGE);
2664
2665                 place_monster_aux(0, y, xval - 7, what[1], PM_NO_KAGE);
2666                 place_monster_aux(0, y, xval + 7, what[1], PM_NO_KAGE);
2667
2668                 place_monster_aux(0, y, xval - 6, what[2], PM_NO_KAGE);
2669                 place_monster_aux(0, y, xval + 6, what[2], PM_NO_KAGE);
2670
2671                 place_monster_aux(0, y, xval - 5, what[2], PM_NO_KAGE);
2672                 place_monster_aux(0, y, xval + 5, what[2], PM_NO_KAGE);
2673
2674                 place_monster_aux(0, y, xval - 4, what[3], PM_NO_KAGE);
2675                 place_monster_aux(0, y, xval + 4, what[3], PM_NO_KAGE);
2676
2677                 place_monster_aux(0, y, xval - 3, what[3], PM_NO_KAGE);
2678                 place_monster_aux(0, y, xval + 3, what[3], PM_NO_KAGE);
2679
2680                 place_monster_aux(0, y, xval - 2, what[4], PM_NO_KAGE);
2681                 place_monster_aux(0, y, xval + 2, what[4], PM_NO_KAGE);
2682         }
2683
2684         /* Above/Below the center monster */
2685         for (x = xval - 1; x <= xval + 1; x++)
2686         {
2687                 place_monster_aux(0, yval + 1, x, what[5], PM_NO_KAGE);
2688                 place_monster_aux(0, yval - 1, x, what[5], PM_NO_KAGE);
2689         }
2690
2691         /* Next to the center monster */
2692         place_monster_aux(0, yval, xval + 1, what[6], PM_NO_KAGE);
2693         place_monster_aux(0, yval, xval - 1, what[6], PM_NO_KAGE);
2694
2695         /* Center monster */
2696         place_monster_aux(0, yval, xval, what[7], PM_NO_KAGE);
2697
2698         return TRUE;
2699 }
2700
2701
2702 /*!
2703  * @brief Vault地形を回転、上下左右反転するための座標変換を返す / coordinate translation code
2704  * @param x 変換したい点のX座標参照ポインタ
2705  * @param y 変換したい点のY座標参照ポインタ
2706  * @param xoffset Vault生成時の基準X座標
2707  * @param yoffset Vault生成時の基準Y座標
2708  * @param transno 処理ID
2709  * @return なし
2710  */
2711 static void coord_trans(int *x, int *y, int xoffset, int yoffset, int transno)
2712 {
2713         int i;
2714         int temp;
2715
2716         /*
2717          * transno specifies what transformation is required. (0-7)
2718          * The lower two bits indicate by how much the vault is rotated,
2719          * and the upper bit indicates a reflection.
2720          * This is done by using rotation matrices... however since
2721          * these are mostly zeros for rotations by 90 degrees this can
2722          * be expressed simply in terms of swapping and inverting the
2723          * x and y coordinates.
2724          */
2725         for (i = 0; i < transno % 4; i++)
2726         {
2727                 /* rotate by 90 degrees */
2728                 temp = *x;
2729                 *x = -(*y);
2730                 *y = temp;
2731         }
2732
2733         if (transno / 4)
2734         {
2735                 /* Reflect depending on status of 3rd bit. */
2736                 *x = -(*x);
2737         }
2738
2739         /* Add offsets so vault stays in the first quadrant */
2740         *x += xoffset;
2741         *y += yoffset;
2742 }
2743
2744 /*!
2745  * @brief Vaultをフロアに配置する / Hack -- fill in "vault" rooms
2746  * @param yval 生成基準Y座標
2747  * @param xval 生成基準X座標
2748  * @param ymax VaultのYサイズ
2749  * @param xmax VaultのXサイズ
2750  * @param data Vaultのデータ文字列
2751  * @param xoffset 変換基準X座標
2752  * @param yoffset 変換基準Y座標
2753  * @param transno 変換ID
2754  * @return なし
2755  */
2756 static void build_vault(int yval, int xval, int ymax, int xmax, cptr data,
2757                 int xoffset, int yoffset, int transno)
2758 {
2759         int dx, dy, x, y, i, j;
2760
2761         cptr t;
2762
2763         cave_type *c_ptr;
2764
2765
2766         /* Place dungeon features and objects */
2767         for (t = data, dy = 0; dy < ymax; dy++)
2768         {
2769                 for (dx = 0; dx < xmax; dx++, t++)
2770                 {
2771                         /* prevent loop counter from being overwritten */
2772                         i = dx;
2773                         j = dy;
2774
2775                         /* Flip / rotate */
2776                         coord_trans(&i, &j, xoffset, yoffset, transno);
2777
2778                         /* Extract the location */
2779                         if (transno % 2 == 0)
2780                         {
2781                                 /* no swap of x/y */
2782                                 x = xval - (xmax / 2) + i;
2783                                 y = yval - (ymax / 2) + j;
2784                         }
2785                         else
2786                         {
2787                                 /* swap of x/y */
2788                                 x = xval - (ymax / 2) + i;
2789                                 y = yval - (xmax / 2) + j;
2790                         }
2791
2792                         /* Hack -- skip "non-grids" */
2793                         if (*t == ' ') continue;
2794
2795                         /* Access the grid */
2796                         c_ptr = &cave[y][x];
2797
2798                         /* Lay down a floor */
2799                         place_floor_grid(c_ptr);
2800
2801                         /* Remove any mimic */
2802                         c_ptr->mimic = 0;
2803
2804                         /* Part of a vault */
2805                         c_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
2806
2807                         /* Analyze the grid */
2808                         switch (*t)
2809                         {
2810                                 /* Granite wall (outer) */
2811                         case '%':
2812                                 place_outer_noperm_grid(c_ptr);
2813                                 break;
2814
2815                                 /* Granite wall (inner) */
2816                         case '#':
2817                                 place_inner_grid(c_ptr);
2818                                 break;
2819
2820                                 /* Glass wall (inner) */
2821                         case '$':
2822                                 place_inner_grid(c_ptr);
2823                                 c_ptr->feat = feat_glass_wall;
2824                                 break;
2825
2826                                 /* Permanent wall (inner) */
2827                         case 'X':
2828                                 place_inner_perm_grid(c_ptr);
2829                                 break;
2830
2831                                 /* Permanent glass wall (inner) */
2832                         case 'Y':
2833                                 place_inner_perm_grid(c_ptr);
2834                                 c_ptr->feat = feat_permanent_glass_wall;
2835                                 break;
2836
2837                                 /* Treasure/trap */
2838                         case '*':
2839                                 if (randint0(100) < 75)
2840                                 {
2841                                         place_object(y, x, 0L);
2842                                 }
2843                                 else
2844                                 {
2845                                         place_trap(y, x);
2846                                 }
2847                                 break;
2848
2849                                 /* Secret doors */
2850                         case '+':
2851                                 place_secret_door(y, x, DOOR_DEFAULT);
2852                                 break;
2853
2854                                 /* Secret glass doors */
2855                         case '-':
2856                                 place_secret_door(y, x, DOOR_GLASS_DOOR);
2857                                 if (is_closed_door(c_ptr->feat)) c_ptr->mimic = feat_glass_wall;
2858                                 break;
2859
2860                                 /* Curtains */
2861                         case '\'':
2862                                 place_secret_door(y, x, DOOR_CURTAIN);
2863                                 break;
2864
2865                                 /* Trap */
2866                         case '^':
2867                                 place_trap(y, x);
2868                                 break;
2869
2870                                 /* Black market in a dungeon */
2871                         case 'S':
2872                                 set_cave_feat(y, x, feat_black_market);
2873                                 store_init(NO_TOWN, STORE_BLACK);
2874                                 break;
2875
2876                                 /* The Pattern */
2877                         case 'p':
2878                                 set_cave_feat(y, x, feat_pattern_start);
2879                                 break;
2880
2881                         case 'a':
2882                                 set_cave_feat(y, x, feat_pattern_1);
2883                                 break;
2884
2885                         case 'b':
2886                                 set_cave_feat(y, x, feat_pattern_2);
2887                                 break;
2888
2889                         case 'c':
2890                                 set_cave_feat(y, x, feat_pattern_3);
2891                                 break;
2892
2893                         case 'd':
2894                                 set_cave_feat(y, x, feat_pattern_4);
2895                                 break;
2896
2897                         case 'P':
2898                                 set_cave_feat(y, x, feat_pattern_end);
2899                                 break;
2900
2901                         case 'B':
2902                                 set_cave_feat(y, x, feat_pattern_exit);
2903                                 break;
2904
2905                         case 'A':
2906                                 /* Reward for Pattern walk */
2907                                 object_level = base_level + 12;
2908                                 place_object(y, x, AM_GOOD | AM_GREAT);
2909                                 object_level = base_level;
2910                                 break;
2911                         }
2912                 }
2913         }
2914
2915
2916         /* Place dungeon monsters and objects */
2917         for (t = data, dy = 0; dy < ymax; dy++)
2918         {
2919                 for (dx = 0; dx < xmax; dx++, t++)
2920                 {
2921                         /* prevent loop counter from being overwritten */
2922                         i = dx;
2923                         j = dy;
2924
2925                         /* Flip / rotate */
2926                         coord_trans(&i, &j, xoffset, yoffset, transno);
2927
2928                         /* Extract the location */
2929                         if (transno % 2 == 0)
2930                         {
2931                                 /* no swap of x/y */
2932                                 x = xval - (xmax / 2) + i;
2933                                 y = yval - (ymax / 2) + j;
2934                         }
2935                         else
2936                         {
2937                                 /* swap of x/y */
2938                                 x = xval - (ymax / 2) + i;
2939                                 y = yval - (xmax / 2) + j;
2940                         }
2941
2942                         /* Hack -- skip "non-grids" */
2943                         if (*t == ' ') continue;
2944
2945                         /* Analyze the symbol */
2946                         switch (*t)
2947                         {
2948                                 /* Monster */
2949                                 case '&':
2950                                 {
2951                                         monster_level = base_level + 5;
2952                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
2953                                         monster_level = base_level;
2954                                         break;
2955                                 }
2956
2957                                 /* Meaner monster */
2958                                 case '@':
2959                                 {
2960                                         monster_level = base_level + 11;
2961                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
2962                                         monster_level = base_level;
2963                                         break;
2964                                 }
2965
2966                                 /* Meaner monster, plus treasure */
2967                                 case '9':
2968                                 {
2969                                         monster_level = base_level + 9;
2970                                         place_monster(y, x, PM_ALLOW_SLEEP);
2971                                         monster_level = base_level;
2972                                         object_level = base_level + 7;
2973                                         place_object(y, x, AM_GOOD);
2974                                         object_level = base_level;
2975                                         break;
2976                                 }
2977
2978                                 /* Nasty monster and treasure */
2979                                 case '8':
2980                                 {
2981                                         monster_level = base_level + 40;
2982                                         place_monster(y, x, PM_ALLOW_SLEEP);
2983                                         monster_level = base_level;
2984                                         object_level = base_level + 20;
2985                                         place_object(y, x, AM_GOOD | AM_GREAT);
2986                                         object_level = base_level;
2987                                         break;
2988                                 }
2989
2990                                 /* Monster and/or object */
2991                                 case ',':
2992                                 {
2993                                         if (randint0(100) < 50)
2994                                         {
2995                                                 monster_level = base_level + 3;
2996                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
2997                                                 monster_level = base_level;
2998                                         }
2999                                         if (randint0(100) < 50)
3000                                         {
3001                                                 object_level = base_level + 7;
3002                                                 place_object(y, x, 0L);
3003                                                 object_level = base_level;
3004                                         }
3005                                         break;
3006                                 }
3007
3008                         }
3009                 }
3010         }
3011 }
3012
3013
3014 /*!
3015  * @brief タイプ7の部屋…v_info.txtより小型vaultを生成する / Type 7 -- simple vaults (see "v_info.txt")
3016  * @return なし
3017  */
3018 static bool build_type7(void)
3019 {
3020         vault_type *v_ptr = NULL;
3021         int dummy;
3022         int x, y;
3023         int xval, yval;
3024         int xoffset, yoffset;
3025         int transno;
3026
3027         /* Pick a lesser vault */
3028         for (dummy = 0; dummy < SAFE_MAX_ATTEMPTS; dummy++)
3029         {
3030                 /* Access a random vault record */
3031                 v_ptr = &v_info[randint0(max_v_idx)];
3032
3033                 /* Accept the first lesser vault */
3034                 if (v_ptr->typ == 7) break;
3035         }
3036
3037         /* No lesser vault found */
3038         if (dummy >= SAFE_MAX_ATTEMPTS)
3039         {
3040                 if (cheat_room)
3041                 {
3042                         msg_print(_("警告!小さな地下室を配置できません!", "Warning! Could not place lesser vault!"));
3043                 }
3044                 return FALSE;
3045         }
3046
3047         /* pick type of transformation (0-7) */
3048         transno = randint0(8);
3049
3050         /* calculate offsets */
3051         x = v_ptr->wid;
3052         y = v_ptr->hgt;
3053
3054         /* Some huge vault cannot be ratated to fit in the dungeon */
3055         if (x+2 > cur_hgt-2)
3056         {
3057                 /* Forbid 90 or 270 degree ratation */
3058                 transno &= ~1;
3059         }
3060
3061         coord_trans(&x, &y, 0, 0, transno);
3062
3063         if (x < 0)
3064         {
3065                 xoffset = -x - 1;
3066         }
3067         else
3068         {
3069                 xoffset = 0;
3070         }
3071
3072         if (y < 0)
3073         {
3074                 yoffset = -y - 1;
3075         }
3076         else
3077         {
3078                 yoffset = 0;
3079         }
3080
3081         /* Find and reserve some space in the dungeon.  Get center of room. */
3082         if (!find_space(&yval, &xval, abs(y), abs(x))) return FALSE;
3083
3084 #ifdef FORCE_V_IDX
3085         v_ptr = &v_info[2];
3086 #endif
3087
3088         /* Message */
3089         if (cheat_room) msg_format(_("小さな地下室(%s)", "Lesser vault (%s)"), v_name + v_ptr->name);
3090
3091         /* Hack -- Build the vault */
3092         build_vault(yval, xval, v_ptr->hgt, v_ptr->wid,
3093                     v_text + v_ptr->text, xoffset, yoffset, transno);
3094
3095         return TRUE;
3096 }
3097
3098 /*!
3099  * @brief タイプ8の部屋…v_info.txtより大型vaultを生成する / Type 8 -- greater vaults (see "v_info.txt")
3100  * @return なし
3101  */
3102 static bool build_type8(void)
3103 {
3104         vault_type *v_ptr;
3105         int dummy;
3106         int xval, yval;
3107         int x, y;
3108         int transno;
3109         int xoffset, yoffset;
3110
3111         /* Pick a greater vault */
3112         for (dummy = 0; dummy < SAFE_MAX_ATTEMPTS; dummy++)
3113         {
3114                 /* Access a random vault record */
3115                 v_ptr = &v_info[randint0(max_v_idx)];
3116
3117                 /* Accept the first greater vault */
3118                 if (v_ptr->typ == 8) break;
3119         }
3120
3121         /* No greater vault found */
3122         if (dummy >= SAFE_MAX_ATTEMPTS)
3123         {
3124                 if (cheat_room)
3125                 {
3126                         msg_print(_("警告!巨大な地下室を配置できません!", "Warning! Could not place greater vault!"));
3127                 }
3128                 return FALSE;
3129         }
3130
3131         /* pick type of transformation (0-7) */
3132         transno = randint0(8);
3133
3134         /* calculate offsets */
3135         x = v_ptr->wid;
3136         y = v_ptr->hgt;
3137
3138         /* Some huge vault cannot be ratated to fit in the dungeon */
3139         if (x+2 > cur_hgt-2)
3140         {
3141                 /* Forbid 90 or 270 degree ratation */
3142                 transno &= ~1;
3143         }
3144
3145         coord_trans(&x, &y, 0, 0, transno);
3146
3147         if (x < 0)
3148         {
3149                 xoffset = - x - 1;
3150         }
3151         else
3152         {
3153                 xoffset = 0;
3154         }
3155
3156         if (y < 0)
3157         {
3158                 yoffset = - y - 1;
3159         }
3160         else
3161         {
3162                 yoffset = 0;
3163         }
3164
3165         /*
3166          * Try to allocate space for room.  If fails, exit
3167          *
3168          * Hack -- Prepare a bit larger space (+2, +2) to 
3169          * prevent generation of vaults with no-entrance.
3170          */
3171         /* Find and reserve some space in the dungeon.  Get center of room. */
3172         if (!find_space(&yval, &xval, abs(y) + 2, abs(x) + 2)) return FALSE;
3173
3174 #ifdef FORCE_V_IDX
3175         v_ptr = &v_info[76 + randint1(3)];
3176 #endif
3177
3178         /* Message */
3179         if (cheat_room) msg_format(_("巨大な地下室(%s)", "Greater vault (%s)"), v_name + v_ptr->name);
3180
3181         /* Hack -- Build the vault */
3182         build_vault(yval, xval, v_ptr->hgt, v_ptr->wid,
3183                     v_text + v_ptr->text, xoffset, yoffset, transno);
3184
3185         return TRUE;
3186 }
3187
3188 /*
3189  * Structure to hold all "fill" data
3190  */
3191
3192 typedef struct fill_data_type fill_data_type;
3193
3194 struct fill_data_type
3195 {
3196         /* area size */
3197         int xmin;
3198         int ymin;
3199         int xmax;
3200         int ymax;
3201
3202         /* cutoffs */
3203         int c1;
3204         int c2;
3205         int c3;
3206
3207         /* features to fill with */
3208         int feat1;
3209         int feat2;
3210         int feat3;
3211
3212         int info1;
3213         int info2;
3214         int info3;
3215
3216         /* number of filled squares */
3217         int amount;
3218 };
3219
3220 static fill_data_type fill_data;
3221
3222
3223 /* Store routine for the fractal cave generator */
3224 /* this routine probably should be an inline function or a macro. */
3225 static void store_height(int x, int y, int val)
3226 {
3227         /* if on boundary set val > cutoff so walls are not as square */
3228         if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
3229              (x == fill_data.xmax) || (y == fill_data.ymax)) &&
3230             (val <= fill_data.c1)) val = fill_data.c1 + 1;
3231
3232         /* store the value in height-map format */
3233         cave[y][x].feat = val;
3234
3235         return;
3236 }
3237
3238
3239 /*
3240 * Explanation of the plasma fractal algorithm:
3241 *
3242 * A grid of points is created with the properties of a 'height-map'
3243 * This is done by making the corners of the grid have a random value.
3244 * The grid is then subdivided into one with twice the resolution.
3245 * The new points midway between two 'known' points can be calculated
3246 * by taking the average value of the 'known' ones and randomly adding
3247 * or subtracting an amount proportional to the distance between those
3248 * points.  The final 'middle' points of the grid are then calculated
3249 * by averaging all four of the originally 'known' corner points.  An
3250 * random amount is added or subtracted from this to get a value of the
3251 * height at that point.  The scaling factor here is adjusted to the
3252 * slightly larger distance diagonally as compared to orthogonally.
3253 *
3254 * This is then repeated recursively to fill an entire 'height-map'
3255 * A rectangular map is done the same way, except there are different
3256 * scaling factors along the x and y directions.
3257 *
3258 * A hack to change the amount of correlation between points is done using
3259 * the grd variable.  If the current step size is greater than grd then
3260 * the point will be random, otherwise it will be calculated by the
3261 * above algorithm.  This makes a maximum distance at which two points on
3262 * the height map can affect each other.
3263 *
3264 * How fractal caves are made:
3265 *
3266 * When the map is complete, a cut-off value is used to create a cave.
3267 * Heights below this value are "floor", and heights above are "wall".
3268 * This also can be used to create lakes, by adding more height levels
3269 * representing shallow and deep water/ lava etc.
3270 *
3271 * The grd variable affects the width of passages.
3272 * The roug variable affects the roughness of those passages
3273 *
3274 * The tricky part is making sure the created cave is connected.  This
3275 * is done by 'filling' from the inside and only keeping the 'filled'
3276 * floor.  Walls bounding the 'filled' floor are also kept.  Everything
3277 * else is converted to the normal _extra_.
3278  */
3279
3280
3281 /*
3282  *  Note that this uses the cave.feat array in a very hackish way
3283  *  the values are first set to zero, and then each array location
3284  *  is used as a "heightmap"
3285  *  The heightmap then needs to be converted back into the "feat" format.
3286  *
3287  *  grd=level at which fractal turns on.  smaller gives more mazelike caves
3288  *  roug=roughness level.  16=normal.  higher values make things more convoluted
3289  *    small values are good for smooth walls.
3290  *  size=length of the side of the square cave system.
3291  */
3292 static void generate_hmap(int y0, int x0, int xsiz, int ysiz, int grd, int roug, int cutoff)
3293 {
3294         int xhsize, yhsize, xsize, ysize, maxsize;
3295
3296         /*
3297          * fixed point variables- these are stored as 256 x normal value
3298          * this gives 8 binary places of fractional part + 8 places of normal part
3299          */
3300
3301         u16b xstep, xhstep, ystep, yhstep;
3302         u16b xstep2, xhstep2, ystep2, yhstep2;
3303         u16b i, j, ii, jj, diagsize, xxsize, yysize;
3304         
3305         /* Cache for speed */
3306         u16b xm, xp, ym, yp;
3307
3308         /* redefine size so can change the value if out of range */
3309         xsize = xsiz;
3310         ysize = ysiz;
3311
3312         /* Paranoia about size of the system of caves */
3313         if (xsize > 254) xsize = 254;
3314         if (xsize < 4) xsize = 4;
3315         if (ysize > 254) ysize = 254;
3316         if (ysize < 4) ysize = 4;
3317
3318         /* get offsets to middle of array */
3319         xhsize = xsize / 2;
3320         yhsize = ysize / 2;
3321
3322         /* fix rounding problem */
3323         xsize = xhsize * 2;
3324         ysize = yhsize * 2;
3325
3326         /* get limits of region */
3327         fill_data.xmin = x0 - xhsize;
3328         fill_data.ymin = y0 - yhsize;
3329         fill_data.xmax = x0 + xhsize;
3330         fill_data.ymax = y0 + yhsize;
3331
3332         /* Store cutoff in global for quick access */
3333         fill_data.c1 = cutoff;
3334
3335         /*
3336         * Scale factor for middle points:
3337         * About sqrt(2) * 256 - correct for a square lattice
3338         * approximately correct for everything else.
3339          */
3340         diagsize = 362;
3341
3342         /* maximum of xsize and ysize */
3343         maxsize = (xsize > ysize) ? xsize : ysize;
3344
3345         /* Clear the section */
3346         for (i = 0; i <= xsize; i++)
3347         {
3348                 for (j = 0; j <= ysize; j++)
3349                 {
3350                         /* -1 is a flag for "not done yet" */
3351                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
3352                         /* Clear icky flag because may be redoing the cave */
3353                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
3354                 }
3355         }
3356
3357         /* Boundaries are walls */
3358         cave[fill_data.ymin][fill_data.xmin].feat = maxsize;
3359         cave[fill_data.ymax][fill_data.xmin].feat = maxsize;
3360         cave[fill_data.ymin][fill_data.xmax].feat = maxsize;
3361         cave[fill_data.ymax][fill_data.xmax].feat = maxsize;
3362
3363         /* Set the middle square to be an open area. */
3364         cave[y0][x0].feat = 0;
3365
3366         /* Initialize the step sizes */
3367         xstep = xhstep = xsize * 256;
3368         ystep = yhstep = ysize * 256;
3369         xxsize = xsize * 256;
3370         yysize = ysize * 256;
3371
3372         /*
3373          * Fill in the rectangle with fractal height data -
3374          * like the 'plasma fractal' in fractint.
3375          */
3376         while ((xhstep > 256) || (yhstep > 256))
3377         {
3378                 /* Halve the step sizes */
3379                 xstep = xhstep;
3380                 xhstep /= 2;
3381                 ystep = yhstep;
3382                 yhstep /= 2;
3383
3384                 /* cache well used values */
3385                 xstep2 = xstep / 256;
3386                 ystep2 = ystep / 256;
3387
3388                 xhstep2 = xhstep / 256;
3389                 yhstep2 = yhstep / 256;
3390
3391                 /* middle top to bottom. */
3392                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
3393                 {
3394                         for (j = 0; j <= yysize; j += ystep)
3395                         {
3396                                 /* cache often used values */
3397                                 ii = i / 256 + fill_data.xmin;
3398                                 jj = j / 256 + fill_data.ymin;
3399
3400                                 /* Test square */
3401                                 if (cave[jj][ii].feat == -1)
3402                                 {
3403                                         if (xhstep2 > grd)
3404                                         {
3405                                                 /* If greater than 'grid' level then is random */
3406                                                 store_height(ii, jj, randint1(maxsize));
3407                                         }
3408                                         else
3409                                         {
3410                                                 /* Average of left and right points +random bit */
3411                                                 store_height(ii, jj,
3412                                                         (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
3413                                                          + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
3414                                                          + (randint1(xstep2) - xhstep2) * roug / 16);
3415                                         }
3416                                 }
3417                         }
3418                 }
3419
3420
3421                 /* middle left to right. */
3422                 for (j = yhstep; j <= yysize - yhstep; j += ystep)
3423                 {
3424                         for (i = 0; i <= xxsize; i += xstep)
3425                         {
3426                                 /* cache often used values */
3427                                 ii = i / 256 + fill_data.xmin;
3428                                 jj = j / 256 + fill_data.ymin;
3429
3430                                 /* Test square */
3431                                 if (cave[jj][ii].feat == -1)
3432                                 {
3433                                         if (xhstep2 > grd)
3434                                         {
3435                                                 /* If greater than 'grid' level then is random */
3436                                                 store_height(ii, jj, randint1(maxsize));
3437                                         }
3438                                         else
3439                                         {
3440                                                 /* Average of up and down points +random bit */
3441                                                 store_height(ii, jj,
3442                                                         (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
3443                                                         + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
3444                                                         + (randint1(ystep2) - yhstep2) * roug / 16);
3445                                         }
3446                                 }
3447                         }
3448                 }
3449
3450                 /* center. */
3451                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
3452                 {
3453                         for (j = yhstep; j <= yysize - yhstep; j += ystep)
3454                         {
3455                                 /* cache often used values */
3456                                 ii = i / 256 + fill_data.xmin;
3457                                 jj = j / 256 + fill_data.ymin;
3458
3459                                 /* Test square */
3460                                 if (cave[jj][ii].feat == -1)
3461                                 {
3462                                         if (xhstep2 > grd)
3463                                         {
3464                                                 /* If greater than 'grid' level then is random */
3465                                                 store_height(ii, jj, randint1(maxsize));
3466                                         }
3467                                         else
3468                                         {
3469                                                 /* Cache reused values. */
3470                                                 xm = fill_data.xmin + (i - xhstep) / 256;
3471                                                 xp = fill_data.xmin + (i + xhstep) / 256;
3472                                                 ym = fill_data.ymin + (j - yhstep) / 256;
3473                                                 yp = fill_data.ymin + (j + yhstep) / 256;
3474
3475                                                 /* 
3476                                                  * Average over all four corners + scale by diagsize to
3477                                                  * reduce the effect of the square grid on the shape of the fractal
3478                                                  */
3479                                                 store_height(ii, jj,
3480                                                         (cave[ym][xm].feat + cave[yp][xm].feat
3481                                                         + cave[ym][xp].feat + cave[yp][xp].feat) / 4
3482                                                         + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
3483                                         }
3484                                 }
3485                         }
3486                 }
3487         }
3488 }
3489
3490
3491 static bool hack_isnt_wall(int y, int x, int c1, int c2, int c3, int feat1, int feat2, int feat3, int info1, int info2, int info3)
3492 {
3493         /*
3494          * function used to convert from height-map back to the
3495          *  normal angband cave format
3496          */
3497         if (cave[y][x].info & CAVE_ICKY)
3498         {
3499                 /* already done */
3500                 return FALSE;
3501         }
3502         else
3503         {
3504                 /* Show that have looked at this square */
3505                 cave[y][x].info|= (CAVE_ICKY);
3506
3507                 /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
3508                 if (cave[y][x].feat <= c1)
3509                 {
3510                         /* 25% of the time use the other tile : it looks better this way */
3511                         if (randint1(100) < 75)
3512                         {
3513                                 cave[y][x].feat = feat1;
3514                                 cave[y][x].info &= ~(CAVE_MASK);
3515                                 cave[y][x].info |= info1;
3516                                 return TRUE;
3517                         }
3518                         else
3519                         {
3520                                 cave[y][x].feat = feat2;
3521                                 cave[y][x].info &= ~(CAVE_MASK);
3522                                 cave[y][x].info |= info2;
3523                                 return TRUE;
3524                         }
3525                 }
3526                 else if (cave[y][x].feat <= c2)
3527                 {
3528                         /* 25% of the time use the other tile : it looks better this way */
3529                         if (randint1(100) < 75)
3530                         {
3531                                 cave[y][x].feat = feat2;
3532                                 cave[y][x].info &= ~(CAVE_MASK);
3533                                 cave[y][x].info |= info2;
3534                                 return TRUE;
3535                         }
3536                         else
3537                         {
3538                                 cave[y][x].feat = feat1;
3539                                 cave[y][x].info &= ~(CAVE_MASK);
3540                                 cave[y][x].info |= info1;
3541                                 return TRUE;
3542                         }
3543                 }
3544                 else if (cave[y][x].feat <= c3)
3545                 {
3546                         cave[y][x].feat = feat3;
3547                         cave[y][x].info &= ~(CAVE_MASK);
3548                         cave[y][x].info |= info3;
3549                         return TRUE;
3550                 }
3551                 /* if greater than cutoff then is a wall */
3552                 else
3553                 {
3554                         place_outer_bold(y, x);
3555                         return FALSE;
3556                 }
3557         }
3558 }
3559
3560
3561
3562
3563 /*
3564  * Quick and nasty fill routine used to find the connected region
3565  * of floor in the middle of the cave
3566  */
3567 static void cave_fill(byte y, byte x)
3568 {
3569         int i, j, d;
3570         int ty, tx;
3571
3572         int flow_tail = 1;
3573         int flow_head = 0;
3574
3575
3576         /*** Start Grid ***/
3577
3578         /* Enqueue that entry */
3579         temp_y[0] = y;
3580         temp_x[0] = x;
3581
3582
3583         /* Now process the queue */
3584         while (flow_head != flow_tail)
3585         {
3586                 /* Extract the next entry */
3587                 ty = temp_y[flow_head];
3588                 tx = temp_x[flow_head];
3589
3590                 /* Forget that entry */
3591                 if (++flow_head == TEMP_MAX) flow_head = 0;
3592
3593                 /* Add the "children" */
3594                 for (d = 0; d < 8; d++)
3595                 {
3596                         int old_head = flow_tail;
3597
3598                         /* Child location */
3599                         j = ty + ddy_ddd[d];
3600                         i = tx + ddx_ddd[d];
3601
3602                         /* Paranoia Don't leave the cave */
3603                         if (!in_bounds(j, i))
3604                         {
3605                                 /* affect boundary */
3606                                 cave[j][i].info |= CAVE_ICKY;
3607 /*                              return; */
3608                         }
3609
3610                         /* If within bounds */
3611                         else if ((i > fill_data.xmin) && (i < fill_data.xmax)
3612                                 && (j > fill_data.ymin) && (j < fill_data.ymax))
3613                         {
3614                                 /* If not a wall or floor done before */
3615                                 if (hack_isnt_wall(j, i,
3616                                         fill_data.c1, fill_data.c2, fill_data.c3,
3617                                         fill_data.feat1, fill_data.feat2, fill_data.feat3,
3618                                         fill_data.info1, fill_data.info2, fill_data.info3))
3619                                 {
3620                                         /* Enqueue that entry */
3621                                         temp_y[flow_tail] = j;
3622                                         temp_x[flow_tail] = i;
3623
3624                                         /* Advance the queue */
3625                                         if (++flow_tail == TEMP_MAX) flow_tail = 0;
3626
3627                                         /* Hack -- Overflow by forgetting new entry */
3628                                         if (flow_tail == flow_head)
3629                                         {
3630                                                 flow_tail = old_head;
3631                                         }
3632                                         else
3633                                         {
3634                                                 /* keep tally of size of cave system */
3635                                                 (fill_data.amount)++;
3636                                         }
3637                                 }
3638                         }
3639                         else
3640                         {
3641                                 /* affect boundary */
3642                                 cave[j][i].info |= CAVE_ICKY;
3643                         }
3644                 }
3645         }
3646 }
3647
3648
3649 static bool generate_fracave(int y0, int x0, int xsize, int ysize, int cutoff, bool light, bool room)
3650 {
3651         int x, y, i, xhsize, yhsize;
3652
3653         /* offsets to middle from corner */
3654         xhsize = xsize / 2;
3655         yhsize = ysize / 2;
3656
3657
3658         /*
3659          * select region connected to center of cave system
3660          * this gets rid of alot of isolated one-sqaures that
3661          * can make teleport traps instadeaths...
3662          */
3663
3664         /* cutoffs */
3665         fill_data.c1 = cutoff;
3666         fill_data.c2 = 0;
3667         fill_data.c3 = 0;
3668
3669         /* features to fill with */
3670         fill_data.feat1 = floor_type[randint0(100)];
3671         fill_data.feat2 = floor_type[randint0(100)];
3672         fill_data.feat3 = floor_type[randint0(100)];
3673
3674         fill_data.info1 = CAVE_FLOOR;
3675         fill_data.info2 = CAVE_FLOOR;
3676         fill_data.info3 = CAVE_FLOOR;
3677
3678         /* number of filled squares */
3679         fill_data.amount = 0;
3680
3681         cave_fill((byte)y0, (byte)x0);
3682
3683         /* if tally too small, try again */
3684         if (fill_data.amount < 10)
3685         {
3686                 /* too small - clear area and try again later */
3687                 for (x = 0; x <= xsize; ++x)
3688                 {
3689                         for (y = 0; y <= ysize; ++y)
3690                         {
3691                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
3692                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
3693                         }
3694                 }
3695                 return FALSE;
3696         }
3697
3698         /*
3699          * Do boundarys-check to see if they are next to a filled region
3700          * If not then they are set to normal granite
3701          * If so then they are marked as room walls.
3702          */
3703         for (i = 0; i <= xsize; ++i)
3704         {
3705                 /* top boundary */
3706                 if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
3707                 {
3708                         /* Next to a 'filled' region? - set to be room walls */
3709                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
3710                         if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
3711                         cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
3712                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
3713                 }
3714                 else
3715                 {
3716                         /* set to be normal granite */
3717                         place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
3718                 }
3719
3720                 /* bottom boundary */
3721                 if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
3722                 {
3723                         /* Next to a 'filled' region? - set to be room walls */
3724                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
3725                         if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
3726                         cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
3727                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
3728                 }
3729                 else
3730                 {
3731                         /* set to be normal granite */
3732                         place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
3733                 }
3734
3735                 /* clear the icky flag-don't need it any more */
3736                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
3737                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
3738         }
3739
3740         /* Do the left and right boundaries minus the corners (done above) */
3741         for (i = 1; i < ysize; ++i)
3742         {
3743                 /* left boundary */
3744                 if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
3745                 {
3746                         /* room boundary */
3747                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
3748                         if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
3749                         cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
3750                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
3751                 }
3752                 else
3753                 {
3754                         /* outside room */
3755                         place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
3756                 }
3757                 /* right boundary */
3758                 if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
3759                 {
3760                         /* room boundary */
3761                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
3762                         if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
3763                         cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
3764                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
3765                 }
3766                 else
3767                 {
3768                         /* outside room */
3769                         place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
3770                 }
3771
3772                 /* clear icky flag -done with it */
3773                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
3774                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
3775         }
3776
3777
3778         /* Do the rest: convert back to the normal format */
3779         for (x = 1; x < xsize; ++x)
3780         {
3781                 for (y = 1; y < ysize; ++y)
3782                 {
3783                         if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
3784                             (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
3785                         {
3786                                 /* Clear the icky flag in the filled region */
3787                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
3788
3789                                 /* Set appropriate flags */
3790                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
3791                                 if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
3792                         }
3793                         else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
3794                                  (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
3795                         {
3796                                 /* Walls */
3797                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
3798                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
3799                                 if (room)
3800                                 {
3801                                         cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
3802                                 }
3803                                 else
3804                                 {
3805
3806                                         place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
3807                                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
3808                                 }
3809                         }
3810                         else
3811                         {
3812                                 /* Clear the unconnected regions */
3813                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
3814                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
3815                         }
3816                 }
3817         }
3818
3819         /*
3820          * XXX XXX XXX There is a slight problem when tunnels pierce the caves:
3821          * Extra doors appear inside the system.  (Its not very noticeable though.)
3822          * This can be removed by "filling" from the outside in.  This allows a separation
3823          * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
3824          * The extra effort for what seems to be only a minor thing (even non-existant if you
3825          * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
3826          * worth it.
3827          */
3828
3829         return TRUE;
3830 }
3831
3832
3833 /*!
3834  * @brief タイプ9の部屋…フラクタルカーブによる洞窟生成 / Type 9 -- Driver routine to create fractal cave system
3835  * @return なし
3836  */
3837 static bool build_type9(void)
3838 {
3839         int grd, roug, cutoff, xsize, ysize, y0, x0;
3840
3841         bool done, light, room;
3842
3843         /* get size: note 'Evenness'*/
3844         xsize = randint1(22) * 2 + 6;
3845         ysize = randint1(15) * 2 + 6;
3846
3847         /* Find and reserve some space in the dungeon.  Get center of room. */
3848         if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
3849         {
3850                 /* Limit to the minimum room size, and retry */
3851                 xsize = 8;
3852                 ysize = 8;
3853
3854                 /* Find and reserve some space in the dungeon.  Get center of room. */
3855                 if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
3856                 {
3857                         /*
3858                          * Still no space?!
3859                          * Try normal room
3860                          */
3861                         return build_type1();
3862                 }
3863         }
3864
3865         light = done = FALSE;
3866         room = TRUE;
3867
3868         if ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
3869
3870         while (!done)
3871         {
3872                 /* Note: size must be even or there are rounding problems
3873                 * This causes the tunnels not to connect properly to the room */
3874
3875                 /* testing values for these parameters feel free to adjust */
3876                 grd = 1 << (randint0(4));
3877
3878                 /* want average of about 16 */
3879                 roug = randint1(8) * randint1(4);
3880
3881                 /* about size/2 */
3882                 cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
3883                          randint1(xsize / 4) + randint1(ysize / 4);
3884
3885                 /* make it */
3886                 generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
3887
3888                 /* Convert to normal format + clean up */
3889                 done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
3890         }
3891
3892         return TRUE;
3893 }
3894
3895 #ifdef ALLOW_CAVERNS_AND_LAKES
3896 /*
3897  * Builds a cave system in the center of the dungeon.
3898  */
3899 void build_cavern(void)
3900 {
3901         int grd, roug, cutoff, xsize, ysize, x0, y0;
3902         bool done, light;
3903
3904         light = done = FALSE;
3905         if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
3906
3907         /* Make a cave the size of the dungeon */
3908         xsize = cur_wid - 1;
3909         ysize = cur_hgt - 1;
3910         x0 = xsize / 2;
3911         y0 = ysize / 2;
3912
3913         /* Paranoia: make size even */
3914         xsize = x0 * 2;
3915         ysize = y0 * 2;
3916
3917         while (!done)
3918         {
3919                 /* testing values for these parameters: feel free to adjust */
3920                 grd = randint1(4) + 4;
3921
3922                 /* want average of about 16 */
3923                 roug = randint1(8) * randint1(4);
3924
3925                 /* about size/2 */
3926                 cutoff = xsize / 2;
3927
3928                  /* make it */
3929                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
3930
3931                 /* Convert to normal format+ clean up */
3932                 done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
3933         }
3934 }
3935
3936 static bool generate_lake(int y0, int x0, int xsize, int ysize, int c1, int c2, int c3, int type)
3937 {
3938         int x, y, i, xhsize, yhsize;
3939         int feat1, feat2, feat3;
3940
3941         /* offsets to middle from corner */
3942         xhsize = xsize / 2;
3943         yhsize = ysize / 2;
3944
3945         /* Get features based on type */
3946         switch (type)
3947         {
3948         case LAKE_T_LAVA: /* Lava */
3949                 feat1 = feat_deep_lava;
3950                 feat2 = feat_shallow_lava;
3951                 feat3 = floor_type[randint0(100)];
3952                 break;
3953         case LAKE_T_WATER: /* Water */
3954                 feat1 = feat_deep_water;
3955                 feat2 = feat_shallow_water;
3956                 feat3 = floor_type[randint0(100)];
3957                 break;
3958         case LAKE_T_CAVE: /* Collapsed cave */
3959                 feat1 = floor_type[randint0(100)];
3960                 feat2 = floor_type[randint0(100)];
3961                 feat3 = feat_rubble;
3962                 break;
3963         case LAKE_T_EARTH_VAULT: /* Earth vault */
3964                 feat1 = feat_rubble;
3965                 feat2 = floor_type[randint0(100)];
3966                 feat3 = feat_rubble;
3967                 break;
3968         case LAKE_T_AIR_VAULT: /* Air vault */
3969                 feat1 = feat_grass;
3970                 feat2 = feat_tree;
3971                 feat3 = feat_grass;
3972                 break;
3973         case LAKE_T_WATER_VAULT: /* Water vault */
3974                 feat1 = feat_shallow_water;
3975                 feat2 = feat_deep_water;
3976                 feat3 = feat_shallow_water;
3977                 break;
3978         case LAKE_T_FIRE_VAULT: /* Fire Vault */
3979                 feat1 = feat_shallow_lava;
3980                 feat2 = feat_deep_lava;
3981                 feat3 = feat_shallow_lava;
3982                 break;
3983
3984         /* Paranoia */
3985         default: return FALSE;
3986         }
3987
3988         /*
3989          * select region connected to center of cave system
3990          * this gets rid of alot of isolated one-sqaures that
3991          * can make teleport traps instadeaths...
3992          */
3993
3994         /* cutoffs */
3995         fill_data.c1 = c1;
3996         fill_data.c2 = c2;
3997         fill_data.c3 = c3;
3998
3999         /* features to fill with */
4000         fill_data.feat1 = feat1;
4001         fill_data.feat2 = feat2;
4002         fill_data.feat3 = feat3;
4003
4004         fill_data.info1 = 0;
4005         fill_data.info2 = 0;
4006         fill_data.info3 = 0;
4007
4008         /* number of filled squares */
4009         fill_data.amount = 0;
4010
4011         /* select region connected to center of cave system
4012         * this gets rid of alot of isolated one-sqaures that
4013         * can make teleport traps instadeaths... */
4014         cave_fill((byte)y0, (byte)x0);
4015
4016         /* if tally too small, try again */
4017         if (fill_data.amount < 10)
4018         {
4019                 /* too small -clear area and try again later */
4020                 for (x = 0; x <= xsize; ++x)
4021                 {
4022                         for (y = 0; y <= ysize; ++y)
4023                         {
4024                                 place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
4025                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
4026                         }
4027                 }
4028                 return FALSE;
4029         }
4030
4031         /* Do boundarys- set to normal granite */
4032         for (i = 0; i <= xsize; ++i)
4033         {
4034                 place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
4035                 place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
4036
4037                 /* clear the icky flag-don't need it any more */
4038                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
4039                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
4040         }
4041
4042         /* Do the left and right boundaries minus the corners (done above) */
4043
4044         for (i = 1; i < ysize; ++i)
4045         {
4046                 place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
4047                 place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
4048
4049                 /* clear icky flag -done with it */
4050                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
4051                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
4052         }
4053
4054
4055         /* Do the rest: convert back to the normal format */
4056         for (x = 1; x < xsize; ++x)
4057         {
4058                 for (y = 1; y < ysize; ++y)
4059                 {
4060                         /* Fill unconnected regions with granite */
4061                         if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
4062                                 is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
4063                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
4064
4065                         /* turn off icky flag (no longer needed.) */
4066                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
4067
4068                         /* Light lava */
4069                         if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
4070                         {
4071                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
4072                         }
4073                 }
4074         }
4075
4076         return TRUE;
4077 }
4078
4079
4080 /*
4081  * makes a lake/collapsed cave system in the center of the dungeon
4082  */
4083 void build_lake(int type)
4084 {
4085         int grd, roug, xsize, ysize, x0, y0;
4086         bool done = FALSE;
4087         int c1, c2, c3;
4088
4089         /* paranoia - exit if lake type out of range. */
4090         if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
4091         {
4092                 msg_format("Invalid lake type (%d)", type);
4093                 return;
4094         }
4095
4096         /* Make the size of the dungeon */
4097         xsize = cur_wid - 1;
4098         ysize = cur_hgt - 1;
4099         x0 = xsize / 2;
4100         y0 = ysize / 2;
4101
4102         /* Paranoia: make size even */
4103         xsize = x0 * 2;
4104         ysize = y0 * 2;
4105
4106         while (!done)
4107         {
4108                 /* testing values for these parameters: feel free to adjust */
4109                 grd = randint1(3) + 4;
4110
4111                 /* want average of about 16 */
4112                 roug = randint1(8) * randint1(4);
4113
4114                 /* Make up size of various componants */
4115                 /* Floor */
4116                 c3 = 3 * xsize / 4;
4117
4118                 /* Deep water/lava */
4119                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
4120
4121                 /* Shallow boundary */
4122                 c2 = (c1 + c3) / 2;
4123
4124                 /* make it */
4125                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
4126
4127                 /* Convert to normal format+ clean up */
4128                 done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
4129         }
4130 }
4131 #endif /* ALLOW_CAVERNS_AND_LAKES */
4132
4133
4134 /*
4135  * Routine used by the random vault creators to add a door to a location
4136  * Note that range checking has to be done in the calling routine.
4137  *
4138  * The doors must be INSIDE the allocated region.
4139  */
4140 static void add_door(int x, int y)
4141 {
4142         /* Need to have a wall in the center square */
4143         if (!is_outer_bold(y, x)) return;
4144
4145         /* look at:
4146          *  x#x
4147          *  .#.
4148          *  x#x
4149          *
4150          *  where x=don't care
4151          *  .=floor, #=wall
4152          */
4153
4154         if (is_floor_bold(y-1,x) && is_floor_bold(y+1,x) &&
4155             (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
4156         {
4157                 /* secret door */
4158                 place_secret_door(y, x, DOOR_DEFAULT);
4159
4160                 /* set boundarys so don't get wide doors */
4161                 place_solid_bold(y, x - 1);
4162                 place_solid_bold(y, x + 1);
4163         }
4164
4165
4166         /* look at:
4167          *  x#x
4168          *  .#.
4169          *  x#x
4170          *
4171          *  where x = don't care
4172          *  .=floor, #=wall
4173          */
4174         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
4175             is_floor_bold(y,x-1) && is_floor_bold(y,x+1))
4176         {
4177                 /* secret door */
4178                 place_secret_door(y, x, DOOR_DEFAULT);
4179
4180                 /* set boundarys so don't get wide doors */
4181                 place_solid_bold(y - 1, x);
4182                 place_solid_bold(y + 1, x);
4183         }
4184 }
4185
4186
4187 /*
4188  * Routine that fills the empty areas of a room with treasure and monsters.
4189  */
4190 static void fill_treasure(int x1, int x2, int y1, int y2, int difficulty)
4191 {
4192         int x, y, cx, cy, size;
4193         s32b value;
4194
4195         /* center of room:*/
4196         cx = (x1 + x2) / 2;
4197         cy = (y1 + y2) / 2;
4198
4199         /* Rough measure of size of vault= sum of lengths of sides */
4200         size = abs(x2 - x1) + abs(y2 - y1);
4201
4202         for (x = x1; x <= x2; x++)
4203         {
4204                 for (y = y1; y <= y2; y++)
4205                 {
4206                         /* Thing added based on distance to center of vault
4207                          * Difficulty is 1-easy to 10-hard */
4208                         value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
4209
4210                         /* hack- empty square part of the time */
4211                         if ((randint1(100) - difficulty * 3) > 50) value = 20;
4212
4213                          /* if floor, shallow water and lava */
4214                         if (is_floor_bold(y, x) ||
4215                             (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
4216                         {
4217                                 /* The smaller 'value' is, the better the stuff */
4218                                 if (value < 0)
4219                                 {
4220                                         /* Meanest monster + treasure */
4221                                         monster_level = base_level + 40;
4222                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4223                                         monster_level = base_level;
4224                                         object_level = base_level + 20;
4225                                         place_object(y, x, AM_GOOD);
4226                                         object_level = base_level;
4227                                 }
4228                                 else if (value < 5)
4229                                 {
4230                                         /* Mean monster +treasure */
4231                                         monster_level = base_level + 20;
4232                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4233                                         monster_level = base_level;
4234                                         object_level = base_level + 10;
4235                                         place_object(y, x, AM_GOOD);
4236                                         object_level = base_level;
4237                                 }
4238                                 else if (value < 10)
4239                                 {
4240                                         /* Monster */
4241                                         monster_level = base_level + 9;
4242                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4243                                         monster_level = base_level;
4244                                 }
4245                                 else if (value < 17)
4246                                 {
4247                                         /* Intentional Blank space */
4248
4249                                         /*
4250                                          * (Want some of the vault to be empty
4251                                          * so have room for group monsters.
4252                                          * This is used in the hack above to lower
4253                                          * the density of stuff in the vault.)
4254                                          */
4255                                 }
4256                                 else if (value < 23)
4257                                 {
4258                                         /* Object or trap */
4259                                         if (randint0(100) < 25)
4260                                         {
4261                                                 place_object(y, x, 0L);
4262                                         }
4263                                         else
4264                                         {
4265                                                 place_trap(y, x);
4266                                         }
4267                                 }
4268                                 else if (value < 30)
4269                                 {
4270                                         /* Monster and trap */
4271                                         monster_level = base_level + 5;
4272                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4273                                         monster_level = base_level;
4274                                         place_trap(y, x);
4275                                 }
4276                                 else if (value < 40)
4277                                 {
4278                                         /* Monster or object */
4279                                         if (randint0(100) < 50)
4280                                         {
4281                                                 monster_level = base_level + 3;
4282                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4283                                                 monster_level = base_level;
4284                                         }
4285                                         if (randint0(100) < 50)
4286                                         {
4287                                                 object_level = base_level + 7;
4288                                                 place_object(y, x, 0L);
4289                                                 object_level = base_level;
4290                                         }
4291                                 }
4292                                 else if (value < 50)
4293                                 {
4294                                         /* Trap */
4295                                         place_trap(y, x);
4296                                 }
4297                                 else
4298                                 {
4299                                         /* Various Stuff */
4300
4301                                         /* 20% monster, 40% trap, 20% object, 20% blank space */
4302                                         if (randint0(100) < 20)
4303                                         {
4304                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
4305                                         }
4306                                         else if (randint0(100) < 50)
4307                                         {
4308                                                 place_trap(y, x);
4309                                         }
4310                                         else if (randint0(100) < 50)
4311                                         {
4312                                                 place_object(y, x, 0L);
4313                                         }
4314                                 }
4315
4316                         }
4317                 }
4318         }
4319 }
4320
4321
4322 /*
4323  * This function creates a random vault that looks like a collection of bubbles.
4324  * It works by getting a set of coordinates that represent the center of each
4325  * bubble.  The entire room is made by seeing which bubble center is closest. If
4326  * two centers are equidistant then the square is a wall, otherwise it is a floor.
4327  * The only exception is for squares really near a center, these are always floor.
4328  * (It looks better than without this check.)
4329  *
4330  * Note: If two centers are on the same point then this algorithm will create a
4331  *       blank bubble filled with walls. - This is prevented from happening.
4332  */
4333 static void build_bubble_vault(int x0, int y0, int xsize, int ysize)
4334 {
4335         #define BUBBLENUM 10            /* number of bubbles */
4336
4337         /* array of center points of bubbles */
4338         coord center[BUBBLENUM];
4339
4340         int i, j, x, y;
4341         u16b min1, min2, temp;
4342         bool done;
4343
4344         /* Offset from center to top left hand corner */
4345         int xhsize = xsize / 2;
4346         int yhsize = ysize / 2;
4347
4348
4349         if (cheat_room) msg_print("Bubble Vault");
4350
4351         /* Allocate center of bubbles */
4352         center[0].x = (byte)randint1(xsize - 3) + 1;
4353         center[0].y = (byte)randint1(ysize - 3) + 1;
4354
4355         for (i = 1; i < BUBBLENUM; i++)
4356         {
4357                 done = FALSE;
4358
4359                 /* get center and check to see if it is unique */
4360                 while (!done)
4361                 {
4362                         done = TRUE;
4363
4364                         x = randint1(xsize - 3) + 1;
4365                         y = randint1(ysize - 3) + 1;
4366
4367                         for (j = 0; j < i; j++)
4368                         {
4369                                 /* rough test to see if there is an overlap */
4370                                 if ((x == center[j].x) && (y == center[j].y)) done = FALSE;
4371                         }
4372                 }
4373
4374                 center[i].x = x;
4375                 center[i].y = y;
4376         }
4377
4378
4379         /* Top and bottom boundaries */
4380         for (i = 0; i < xsize; i++)
4381         {
4382                 int side_x = x0 - xhsize + i;
4383
4384                 place_outer_noperm_bold(y0 - yhsize + 0, side_x);
4385                 cave[y0 - yhsize + 0][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
4386                 place_outer_noperm_bold(y0 - yhsize + ysize - 1, side_x);
4387                 cave[y0 - yhsize + ysize - 1][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
4388         }
4389
4390         /* Left and right boundaries */
4391         for (i = 1; i < ysize - 1; i++)
4392         {
4393                 int side_y = y0 - yhsize + i;
4394
4395                 place_outer_noperm_bold(side_y, x0 - xhsize + 0);
4396                 cave[side_y][x0 - xhsize + 0].info |= (CAVE_ROOM | CAVE_ICKY);
4397                 place_outer_noperm_bold(side_y, x0 - xhsize + xsize - 1);
4398                 cave[side_y][x0 - xhsize + xsize - 1].info |= (CAVE_ROOM | CAVE_ICKY);
4399         }
4400
4401         /* Fill in middle with bubbles */
4402         for (x = 1; x < xsize - 1; x++)
4403         {
4404                 for (y = 1; y < ysize - 1; y++)
4405                 {
4406                         /* Get distances to two closest centers */
4407
4408                         /* initialize */
4409                         min1 = distance(x, y, center[0].x, center[0].y);
4410                         min2 = distance(x, y, center[1].x, center[1].y);
4411
4412                         if (min1 > min2)
4413                         {
4414                                 /* swap if in wrong order */
4415                                 temp = min1;
4416                                 min1 = min2;
4417                                 min2 = temp;
4418                         }
4419
4420                         /* Scan the rest */
4421                         for (i = 2; i < BUBBLENUM; i++)
4422                         {
4423                                 temp = distance(x, y, center[i].x, center[i].y);
4424
4425                                 if (temp < min1)
4426                                 {
4427                                         /* smallest */
4428                                         min2 = min1;
4429                                         min1 = temp;
4430                                 }
4431                                 else if (temp < min2)
4432                                 {
4433                                         /* second smallest */
4434                                         min2 = temp;
4435                                 }
4436                         }
4437                         if (((min2 - min1) <= 2) && (!(min1 < 3)))
4438                         {
4439                                 /* Boundary at midpoint+ not at inner region of bubble */
4440                                 place_outer_noperm_bold(y0 - yhsize + y, x0 - xhsize + x);
4441                         }
4442                         else
4443                         {
4444                                 /* middle of a bubble */
4445                                 place_floor_bold(y0 - yhsize + y, x0 - xhsize + x);
4446                         }
4447
4448                         /* clean up rest of flags */
4449                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= (CAVE_ROOM | CAVE_ICKY);
4450                 }
4451         }
4452
4453         /* Try to add some random doors */
4454         for (i = 0; i < 500; i++)
4455         {
4456                 x = randint1(xsize - 3) - xhsize + x0 + 1;
4457                 y = randint1(ysize - 3) - yhsize + y0 + 1;
4458                 add_door(x, y);
4459         }
4460
4461         /* Fill with monsters and treasure, low difficulty */
4462         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5));
4463 }
4464
4465
4466 /*
4467  * Overlay a rectangular room given its bounds
4468  * This routine is used by build_room_vault
4469  * The area inside the walls is not touched:
4470  * only granite is removed- normal walls stay
4471  */
4472 static void build_room(int x1, int x2, int y1, int y2)
4473 {
4474         int x, y, i, xsize, ysize, temp;
4475
4476         /* Check if rectangle has no width */
4477         if ((x1 == x2) || (y1 == y2)) return;
4478
4479         /* initialize */
4480         if (x1 > x2)
4481         {
4482                 /* Swap boundaries if in wrong order */
4483                 temp = x1;
4484                 x1 = x2;
4485                 x2 = temp;
4486         }
4487
4488         if (y1 > y2)
4489         {
4490                 /* Swap boundaries if in wrong order */
4491                 temp = y1;
4492                 y1 = y2;
4493                 y2 = temp;
4494         }
4495
4496         /* get total widths */
4497         xsize = x2 - x1;
4498         ysize = y2 - y1;
4499
4500
4501         /* Top and bottom boundaries */
4502         for (i = 0; i <= xsize; i++)
4503         {
4504                 place_outer_noperm_bold(y1, x1 + i);
4505                 cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
4506                 place_outer_noperm_bold(y2, x1 + i);
4507                 cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
4508         }
4509
4510         /* Left and right boundaries */
4511         for (i = 1; i < ysize; i++)
4512         {
4513                 place_outer_noperm_bold(y1 + i, x1);
4514                 cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
4515                 place_outer_noperm_bold(y1 + i, x2);
4516                 cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
4517         }
4518
4519         /* Middle */
4520         for (x = 1; x < xsize; x++)
4521         {
4522                 for (y = 1; y < ysize; y++)
4523                 {
4524                         if (is_extra_bold(y1+y, x1+x))
4525                         {
4526                                 /* clear the untouched region */
4527                                 place_floor_bold(y1 + y, x1 + x);
4528                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
4529                         }
4530                         else
4531                         {
4532                                 /* make it a room- but don't touch */
4533                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
4534                         }
4535                 }
4536         }
4537 }
4538
4539
4540 /* Create a random vault that looks like a collection of overlapping rooms */
4541
4542 static void build_room_vault(int x0, int y0, int xsize, int ysize)
4543 {
4544         int i, x1, x2, y1, y2, xhsize, yhsize;
4545
4546         /* get offset from center */
4547         xhsize = xsize / 2;
4548         yhsize = ysize / 2;
4549
4550         if (cheat_room) msg_print("Room Vault");
4551
4552         /* fill area so don't get problems with arena levels */
4553         for (x1 = 0; x1 < xsize; x1++)
4554         {
4555                 int x = x0 - xhsize + x1;
4556
4557                 for (y1 = 0; y1 < ysize; y1++)
4558                 {
4559                         int y = y0 - yhsize + y1;
4560
4561                         place_extra_bold(y, x);
4562                         cave[y][x].info &= (~CAVE_ICKY);
4563                 }
4564         }
4565
4566         /* add ten random rooms */
4567         for (i = 0; i < 10; i++)
4568         {
4569                 x1 = randint1(xhsize) * 2 + x0 - xhsize;
4570                 x2 = randint1(xhsize) * 2 + x0 - xhsize;
4571                 y1 = randint1(yhsize) * 2 + y0 - yhsize;
4572                 y2 = randint1(yhsize) * 2 + y0 - yhsize;
4573                 build_room(x1, x2, y1, y2);
4574         }
4575
4576         /* Add some random doors */
4577         for (i = 0; i < 500; i++)
4578         {
4579                 x1 = randint1(xsize - 3) - xhsize + x0 + 1;
4580                 y1 = randint1(ysize - 3) - yhsize + y0 + 1;
4581                 add_door(x1, y1);
4582         }
4583
4584         /* Fill with monsters and treasure, high difficulty */
4585         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5) + 5);
4586 }
4587
4588
4589 /* Create a random vault out of a fractal cave */
4590 static void build_cave_vault(int x0, int y0, int xsiz, int ysiz)
4591 {
4592         int grd, roug, cutoff, xhsize, yhsize, xsize, ysize, x, y;
4593         bool done, light, room;
4594
4595         /* round to make sizes even */
4596         xhsize = xsiz / 2;
4597         yhsize = ysiz / 2;
4598         xsize = xhsize * 2;
4599         ysize = yhsize * 2;
4600
4601         if (cheat_room) msg_print("Cave Vault");
4602
4603         light = done = FALSE;
4604         room = TRUE;
4605
4606         while (!done)
4607         {
4608                 /* testing values for these parameters feel free to adjust */
4609                 grd = 1 << randint0(4);
4610
4611                 /* want average of about 16 */
4612                 roug = randint1(8) * randint1(4);
4613
4614                 /* about size/2 */
4615                 cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
4616                          randint1(xsize / 4) + randint1(ysize / 4);
4617
4618                 /* make it */
4619                 generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
4620
4621                 /* Convert to normal format+ clean up */
4622                 done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
4623         }
4624
4625         /* Set icky flag because is a vault */
4626         for (x = 0; x <= xsize; x++)
4627         {
4628                 for (y = 0; y <= ysize; y++)
4629                 {
4630                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
4631                 }
4632         }
4633
4634         /* Fill with monsters and treasure, low difficulty */
4635         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1, y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
4636 }
4637
4638 /*
4639  * maze vault -- rectangular labyrinthine rooms
4640  *
4641  * maze vault uses two routines:
4642  *    r_visit - a recursive routine that builds the labyrinth
4643  *    build_maze_vault - a driver routine that calls r_visit and adds
4644  *                   monsters, traps and treasure
4645  *
4646  * The labyrinth is built by creating a spanning tree of a graph.
4647  * The graph vertices are at
4648  *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
4649  * and the edges are the vertical and horizontal nearest neighbors.
4650  *
4651  * The spanning tree is created by performing a suitably randomized
4652  * depth-first traversal of the graph. The only adjustable parameter
4653  * is the randint0(3) below; it governs the relative density of
4654  * twists and turns in the labyrinth: smaller number, more twists.
4655  */
4656 static void r_visit(int y1, int x1, int y2, int x2,
4657                     int node, int dir, int *visited)
4658 {
4659         int i, j, m, n, temp, x, y, adj[4];
4660
4661         /* dimensions of vertex array */
4662         m = (x2 - x1) / 2 + 1;
4663         n = (y2 - y1) / 2 + 1;
4664
4665         /* mark node visited and set it to a floor */
4666         visited[node] = 1;
4667         x = 2 * (node % m) + x1;
4668         y = 2 * (node / m) + y1;
4669         place_floor_bold(y, x);
4670
4671         /* setup order of adjacent node visits */
4672         if (one_in_(3))
4673         {
4674                 /* pick a random ordering */
4675                 for (i = 0; i < 4; i++)
4676                         adj[i] = i;
4677                 for (i = 0; i < 4; i++)
4678                 {
4679                         j = randint0(4);
4680                         temp = adj[i];
4681                         adj[i] = adj[j];
4682                         adj[j] = temp;
4683                 }
4684                 dir = adj[0];
4685         }
4686         else
4687         {
4688                 /* pick a random ordering with dir first */
4689                 adj[0] = dir;
4690                 for (i = 1; i < 4; i++)
4691                         adj[i] = i;
4692                 for (i = 1; i < 4; i++)
4693                 {
4694                         j = 1 + randint0(3);
4695                         temp = adj[i];
4696                         adj[i] = adj[j];
4697                         adj[j] = temp;
4698                 }
4699         }
4700
4701         for (i = 0; i < 4; i++)
4702         {
4703                 switch (adj[i])
4704                 {
4705                         case 0:
4706                                 /* (0,+) - check for bottom boundary */
4707                                 if ((node / m < n - 1) && (visited[node + m] == 0))
4708                                 {
4709                                         place_floor_bold(y + 1, x);
4710                                         r_visit(y1, x1, y2, x2, node + m, dir, visited);
4711                                 }
4712                                 break;
4713                         case 1:
4714                                 /* (0,-) - check for top boundary */
4715                                 if ((node / m > 0) && (visited[node - m] == 0))
4716                                 {
4717                                         place_floor_bold(y - 1, x);
4718                                         r_visit(y1, x1, y2, x2, node - m, dir, visited);
4719                                 }
4720                                 break;
4721                         case 2:
4722                                 /* (+,0) - check for right boundary */
4723                                 if ((node % m < m - 1) && (visited[node + 1] == 0))
4724                                 {
4725                                         place_floor_bold(y, x + 1);
4726                                         r_visit(y1, x1, y2, x2, node + 1, dir, visited);
4727                                 }
4728                                 break;
4729                         case 3:
4730                                 /* (-,0) - check for left boundary */
4731                                 if ((node % m > 0) && (visited[node - 1] == 0))
4732                                 {
4733                                         place_floor_bold(y, x - 1);
4734                                         r_visit(y1, x1, y2, x2, node - 1, dir, visited);
4735                                 }
4736                 } /* end switch */
4737         }
4738 }
4739
4740
4741 void build_maze_vault(int x0, int y0, int xsize, int ysize, bool is_vault)
4742 {
4743         int y, x, dy, dx;
4744         int y1, x1, y2, x2;
4745         int m, n, num_vertices, *visited;
4746         bool light;
4747         cave_type *c_ptr;
4748
4749
4750         if (cheat_room && is_vault) msg_print("Maze Vault");
4751
4752         /* Choose lite or dark */
4753         light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
4754
4755         /* Pick a random room size - randomized by calling routine */
4756         dy = ysize / 2 - 1;
4757         dx = xsize / 2 - 1;
4758
4759         y1 = y0 - dy;
4760         x1 = x0 - dx;
4761         y2 = y0 + dy;
4762         x2 = x0 + dx;
4763
4764         /* generate the room */
4765         for (y = y1 - 1; y <= y2 + 1; y++)
4766         {
4767                 for (x = x1 - 1; x <= x2 + 1; x++)
4768                 {
4769                         c_ptr = &cave[y][x];
4770                         c_ptr->info |= CAVE_ROOM;
4771                         if (is_vault) c_ptr->info |= CAVE_ICKY;
4772                         if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
4773                         {
4774                                 place_outer_grid(c_ptr);
4775                         }
4776                         else if (!is_vault)
4777                         {
4778                                 place_extra_grid(c_ptr);
4779                         }
4780                         else
4781                         {
4782                                 place_inner_grid(c_ptr);
4783                         }
4784                         if (light) c_ptr->info |= (CAVE_GLOW);
4785                 }
4786         }
4787
4788         /* dimensions of vertex array */
4789         m = dx + 1;
4790         n = dy + 1;
4791         num_vertices = m * n;
4792
4793         /* initialize array of visited vertices */
4794         C_MAKE(visited, num_vertices, int);
4795
4796         /* traverse the graph to create a spaning tree, pick a random root */
4797         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
4798
4799         /* Fill with monsters and treasure, low difficulty */
4800         if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
4801
4802         C_KILL(visited, num_vertices, int);
4803 }
4804
4805
4806 /* Build a "mini" checkerboard vault
4807  *
4808  * This is done by making a permanent wall maze and setting
4809  * the diagonal sqaures of the checker board to be granite.
4810  * The vault has two entrances on opposite sides to guarantee
4811  * a way to get in even if the vault abuts a side of the dungeon.
4812  */
4813 static void build_mini_c_vault(int x0, int y0, int xsize, int ysize)
4814 {
4815         int dy, dx;
4816         int y1, x1, y2, x2, y, x, total;
4817         int m, n, num_vertices;
4818         int *visited;
4819
4820         if (cheat_room) msg_print("Mini Checker Board Vault");
4821
4822         /* Pick a random room size */
4823         dy = ysize / 2 - 1;
4824         dx = xsize / 2 - 1;
4825
4826         y1 = y0 - dy;
4827         x1 = x0 - dx;
4828         y2 = y0 + dy;
4829         x2 = x0 + dx;
4830
4831
4832         /* generate the room */
4833         for (x = x1 - 2; x <= x2 + 2; x++)
4834         {
4835                 if (!in_bounds(y1-2,x)) break;
4836
4837                 cave[y1-2][x].info |= (CAVE_ROOM | CAVE_ICKY);
4838
4839                 place_outer_noperm_bold(y1-2, x);
4840         }
4841
4842         for (x = x1 - 2; x <= x2 + 2; x++)
4843         {
4844                 if (!in_bounds(y2+2,x)) break;
4845
4846                 cave[y2+2][x].info |= (CAVE_ROOM | CAVE_ICKY);
4847
4848                 place_outer_noperm_bold(y2+2, x);
4849         }
4850
4851         for (y = y1 - 2; y <= y2 + 2; y++)
4852         {
4853                 if (!in_bounds(y,x1-2)) break;
4854
4855                 cave[y][x1-2].info |= (CAVE_ROOM | CAVE_ICKY);
4856
4857                 place_outer_noperm_bold(y, x1-2);
4858         }
4859
4860         for (y = y1 - 2; y <= y2 + 2; y++)
4861         {
4862                 if (!in_bounds(y,x2+2)) break;
4863
4864                 cave[y][x2+2].info |= (CAVE_ROOM | CAVE_ICKY);
4865
4866                 place_outer_noperm_bold(y, x2+2);
4867         }
4868
4869         for (y = y1 - 1; y <= y2 + 1; y++)
4870         {
4871                 for (x = x1 - 1; x <= x2 + 1; x++)
4872                 {
4873                         cave_type *c_ptr = &cave[y][x];
4874
4875                         c_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
4876
4877                         /* Permanent walls */
4878                         place_inner_perm_grid(c_ptr);
4879                 }
4880         }
4881
4882
4883         /* dimensions of vertex array */
4884         m = dx + 1;
4885         n = dy + 1;
4886         num_vertices = m * n;
4887
4888         /* initialize array of visited vertices */
4889         C_MAKE(visited, num_vertices, int);
4890
4891         /* traverse the graph to create a spannng tree, pick a random root */
4892         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
4893
4894         /* Make it look like a checker board vault */
4895         for (x = x1; x <= x2; x++)
4896         {
4897                 for (y = y1; y <= y2; y++)
4898                 {
4899                         total = x - x1 + y - y1;
4900                         /* If total is odd- and is a floor then make a wall */
4901                         if ((total % 2 == 1) && is_floor_bold(y, x))
4902                         {
4903                                 place_inner_bold(y, x);
4904                         }
4905                 }
4906         }
4907
4908         /* Make a couple of entrances */
4909         if (one_in_(2))
4910         {
4911                 /* left and right */
4912                 y = randint1(dy) + dy / 2;
4913                 place_inner_bold(y1 + y, x1 - 1);
4914                 place_inner_bold(y1 + y, x2 + 1);
4915         }
4916         else
4917         {
4918                 /* top and bottom */
4919                 x = randint1(dx) + dx / 2;
4920                 place_inner_bold(y1 - 1, x1 + x);
4921                 place_inner_bold(y2 + 1, x1 + x);
4922         }
4923
4924         /* Fill with monsters and treasure, highest difficulty */
4925         fill_treasure(x1, x2, y1, y2, 10);
4926
4927         C_KILL(visited, num_vertices, int);
4928 }
4929
4930
4931 /* Build a town/ castle by using a recursive algorithm.
4932  * Basically divide each region in a probalistic way to create
4933  * smaller regions.  When the regions get too small stop.
4934  *
4935  * The power variable is a measure of how well defended a region is.
4936  * This alters the possible choices.
4937  */
4938 static void build_recursive_room(int x1, int y1, int x2, int y2, int power)
4939 {
4940         int xsize, ysize;
4941         int x, y;
4942         int choice;
4943
4944         /* Temp variables */
4945         int t1, t2, t3, t4;
4946
4947         xsize = x2 - x1;
4948         ysize = y2 - y1;
4949
4950         if ((power < 3) && (xsize > 12) && (ysize > 12))
4951         {
4952                 /* Need outside wall +keep */
4953                 choice = 1;
4954         }
4955         else
4956         {
4957                 if (power < 10)
4958                 {
4959                         /* Make rooms + subdivide */
4960                         if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
4961                         {
4962                                 choice = 4;
4963                         }
4964                         else
4965                         {
4966                                 choice = randint1(2) + 1;
4967                         }
4968                 }
4969                 else
4970                 {
4971                         /* Mostly subdivide */
4972                         choice = randint1(3) + 1;
4973                 }
4974         }
4975
4976         /* Based on the choice made above, do something */
4977
4978         switch (choice)
4979         {
4980                 case 1:
4981                 {
4982                         /* Outer walls */
4983
4984                         /* top and bottom */
4985                         for (x = x1; x <= x2; x++)
4986                         {
4987                                 place_outer_bold(y1, x);
4988                                 place_outer_bold(y2, x);
4989                         }
4990
4991                         /* left and right */
4992                         for (y = y1 + 1; y < y2; y++)
4993                         {
4994                                 place_outer_bold(y, x1);
4995                                 place_outer_bold(y, x2);
4996                         }
4997
4998                         /* Make a couple of entrances */
4999                         if (one_in_(2))
5000                         {
5001                                 /* left and right */
5002                                 y = randint1(ysize) + y1;
5003                                 place_floor_bold(y, x1);
5004                                 place_floor_bold(y, x2);
5005                         }
5006                         else
5007                         {
5008                                 /* top and bottom */
5009                                 x = randint1(xsize) + x1;
5010                                 place_floor_bold(y1, x);
5011                                 place_floor_bold(y2, x);
5012                         }
5013
5014                         /* Select size of keep */
5015                         t1 = randint1(ysize / 3) + y1;
5016                         t2 = y2 - randint1(ysize / 3);
5017                         t3 = randint1(xsize / 3) + x1;
5018                         t4 = x2 - randint1(xsize / 3);
5019
5020                         /* Do outside areas */
5021
5022                         /* Above and below keep */
5023                         build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
5024                         build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
5025
5026                         /* Left and right of keep */
5027                         build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
5028                         build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
5029
5030                         /* Make the keep itself: */
5031                         x1 = t3;
5032                         x2 = t4;
5033                         y1 = t1;
5034                         y2 = t2;
5035                         xsize = x2 - x1;
5036                         ysize = y2 - y1;
5037                         power += 2;
5038
5039                         /* Fall through */
5040                 }
5041                 case 4:
5042                 {
5043                         /* Try to build a room */
5044                         if ((xsize < 3) || (ysize < 3))
5045                         {
5046                                 for (y = y1; y < y2; y++)
5047                                 {
5048                                         for (x = x1; x < x2; x++)
5049                                         {
5050                                                 place_inner_bold(y, x);
5051                                         }
5052                                 }
5053
5054                                 /* Too small */
5055                                 return;
5056                         }
5057
5058                         /* Make outside walls */
5059                         /* top and bottom */
5060                         for (x = x1 + 1; x <= x2 - 1; x++)
5061                         {
5062                                 place_inner_bold(y1 + 1, x);
5063                                 place_inner_bold(y2 - 1, x);
5064                         }
5065
5066                         /* left and right */
5067                         for (y = y1 + 1; y <= y2 - 1; y++)
5068                         {
5069                                 place_inner_bold(y, x1 + 1);
5070                                 place_inner_bold(y, x2 - 1);
5071                         }
5072
5073                         /* Make a door */
5074                         y = randint1(ysize - 3) + y1 + 1;
5075
5076                         if (one_in_(2))
5077                         {
5078                                 /* left */
5079                                 place_floor_bold(y, x1 + 1);
5080                         }
5081                         else
5082                         {
5083                                 /* right */
5084                                 place_floor_bold(y, x2 - 1);
5085                         }
5086
5087                         /* Build the room */
5088                         build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
5089                         break;
5090                 }
5091                 case 2:
5092                 {
5093                         /* Try and divide vertically */
5094                         if (xsize < 3)
5095                         {
5096                                 /* Too small */
5097                                 for (y = y1; y < y2; y++)
5098                                 {
5099                                         for (x = x1; x < x2; x++)
5100                                         {
5101                                                 place_inner_bold(y, x);
5102                                         }
5103                                 }
5104                                 return;
5105                         }
5106
5107                         t1 = randint1(xsize - 2) + x1 + 1;
5108                         build_recursive_room(x1, y1, t1, y2, power - 2);
5109                         build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
5110                         break;
5111                 }
5112                 case 3:
5113                 {
5114                         /* Try and divide horizontally */
5115                         if (ysize < 3)
5116                         {
5117                                 /* Too small */
5118                                 for (y = y1; y < y2; y++)
5119                                 {
5120                                         for (x = x1; x < x2; x++)
5121                                         {
5122                                                 place_inner_bold(y, x);
5123                                         }
5124                                 }
5125                                 return;
5126                         }
5127
5128                         t1 = randint1(ysize - 2) + y1 + 1;
5129                         build_recursive_room(x1, y1, x2, t1, power - 2);
5130                         build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
5131                         break;
5132                 }
5133         }
5134 }
5135
5136
5137 /* Build a castle */
5138
5139 /* Driver routine: clear the region and call the recursive
5140 * room routine.
5141 *
5142 *This makes a vault that looks like a castle/ city in the dungeon.
5143 */
5144 static void build_castle_vault(int x0, int y0, int xsize, int ysize)
5145 {
5146         int dy, dx;
5147         int y1, x1, y2, x2;
5148         int y, x;
5149
5150         /* Pick a random room size */
5151         dy = ysize / 2 - 1;
5152         dx = xsize / 2 - 1;
5153
5154         y1 = y0 - dy;
5155         x1 = x0 - dx;
5156         y2 = y0 + dy;
5157         x2 = x0 + dx;
5158
5159         if (cheat_room) msg_print("Castle Vault");
5160
5161         /* generate the room */
5162         for (y = y1 - 1; y <= y2 + 1; y++)
5163         {
5164                 for (x = x1 - 1; x <= x2 + 1; x++)
5165                 {
5166                         cave[y][x].info |= (CAVE_ROOM | CAVE_ICKY);
5167                         /* Make everything a floor */
5168                         place_floor_bold(y, x);
5169                 }
5170         }
5171
5172         /* Make the castle */
5173         build_recursive_room(x1, y1, x2, y2, randint1(5));
5174
5175         /* Fill with monsters and treasure, low difficulty */
5176         fill_treasure(x1, x2, y1, y2, randint1(3));
5177 }
5178
5179
5180 /*
5181  * Add outer wall to a floored region
5182  * Note: no range checking is done so must be inside dungeon
5183  * This routine also stomps on doors
5184  */
5185 static void add_outer_wall(int x, int y, int light, int x1, int y1, int x2, int y2)
5186 {
5187         cave_type *c_ptr;
5188         feature_type *f_ptr;
5189         int i, j;
5190
5191         if (!in_bounds(y, x)) return;
5192
5193         c_ptr = &cave[y][x];
5194
5195         /* hack- check to see if square has been visited before
5196         * if so, then exit (use room flag to do this) */
5197         if (c_ptr->info & CAVE_ROOM) return;
5198
5199         /* set room flag */
5200         c_ptr->info |= CAVE_ROOM;
5201
5202         f_ptr = &f_info[c_ptr->feat];
5203
5204         if (is_floor_bold(y, x))
5205         {
5206                 for (i = -1; i <= 1; i++)
5207                 {
5208                         for (j = -1; j <= 1; j++)
5209                         {
5210                                 if ((x + i >= x1) && (x + i <= x2) &&
5211                                          (y + j >= y1) && (y + j <= y2))
5212                                 {
5213                                         add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
5214                                         if (light) c_ptr->info |= CAVE_GLOW;
5215                                 }
5216                         }
5217                 }
5218         }
5219         else if (is_extra_bold(y, x))
5220         {
5221                 /* Set bounding walls */
5222                 place_outer_bold(y, x);
5223                 if (light) c_ptr->info |= CAVE_GLOW;
5224         }
5225         else if (permanent_wall(f_ptr))
5226         {
5227                 /* Set bounding walls */
5228                 if (light) c_ptr->info |= CAVE_GLOW;
5229         }
5230 }
5231
5232
5233 /*
5234  * Hacked distance formula - gives the 'wrong' answer.
5235  * Used to build crypts
5236  */
5237 static int dist2(int x1, int y1, int x2, int y2,
5238                  int h1, int h2, int h3, int h4)
5239 {
5240         int dx, dy;
5241         dx = abs(x2 - x1);
5242         dy = abs(y2 - y1);
5243
5244         /* Basically this works by taking the normal pythagorean formula
5245          * and using an expansion to express this in a way without the
5246          * square root.  This approximate formula is then perturbed to give
5247          * the distorted results.  (I found this by making a mistake when I was
5248          * trying to fix the circular rooms.)
5249          */
5250
5251         /* h1-h4 are constants that describe the metric */
5252         if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
5253         if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
5254         return (((dx + dy) * 128) / 181 +
5255                 (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
5256         /* 128/181 is approx. 1/sqrt(2) */
5257 }
5258
5259
5260 /*
5261  * Build target vault.
5262  * This is made by two concentric "crypts" with perpendicular
5263  * walls creating the cross-hairs.
5264  */
5265 static void build_target_vault(int x0, int y0, int xsize, int ysize)
5266 {
5267         int rad, x, y;
5268
5269         /* Make a random metric */
5270         int h1, h2, h3, h4;
5271         h1 = randint1(32) - 16;
5272         h2 = randint1(16);
5273         h3 = randint1(32);
5274         h4 = randint1(32) - 16;
5275
5276         if (cheat_room) msg_print("Target Vault");
5277
5278         /* work out outer radius */
5279         if (xsize > ysize)
5280         {
5281                 rad = ysize / 2;
5282         }
5283         else
5284         {
5285                 rad = xsize / 2;
5286         }
5287
5288         /* Make floor */
5289         for (x = x0 - rad; x <= x0 + rad; x++)
5290         {
5291                 for (y = y0 - rad; y <= y0 + rad; y++)
5292                 {
5293                         /* clear room flag */
5294                         cave[y][x].info &= ~(CAVE_ROOM);
5295
5296                         /* Vault - so is "icky" */
5297                         cave[y][x].info |= CAVE_ICKY;
5298
5299                         if (dist2(y0, x0, y, x, h1, h2, h3, h4) <= rad - 1)
5300                         {
5301                                 /* inside- so is floor */
5302                                 place_floor_bold(y, x);
5303                         }
5304                         else
5305                         {
5306                                 /* make granite outside so arena works */
5307                                 place_extra_bold(y, x);
5308                         }
5309
5310                         /* proper boundary for arena */
5311                         if (((y + rad) == y0) || ((y - rad) == y0) ||
5312                             ((x + rad) == x0) || ((x - rad) == x0))
5313                         {
5314                                 place_extra_bold(y, x);
5315                         }
5316                 }
5317         }
5318
5319         /* Find visible outer walls and set to be FEAT_OUTER */
5320         add_outer_wall(x0, y0, FALSE, x0 - rad - 1, y0 - rad - 1,
5321                        x0 + rad + 1, y0 + rad + 1);
5322
5323         /* Add inner wall */
5324         for (x = x0 - rad / 2; x <= x0 + rad / 2; x++)
5325         {
5326                 for (y = y0 - rad / 2; y <= y0 + rad / 2; y++)
5327                 {
5328                         if (dist2(y0, x0, y, x, h1, h2, h3, h4) == rad / 2)
5329                         {
5330                                 /* Make an internal wall */
5331                                 place_inner_bold(y, x);
5332                         }
5333                 }
5334         }
5335
5336         /* Add perpendicular walls */
5337         for (x = x0 - rad; x <= x0 + rad; x++)
5338         {
5339                 place_inner_bold(y0, x);
5340         }
5341
5342         for (y = y0 - rad; y <= y0 + rad; y++)
5343         {
5344                 place_inner_bold(y, x0);
5345         }
5346
5347         /* Make inner vault */
5348         for (y = y0 - 1; y <= y0 + 1; y++)
5349         {
5350                 place_inner_bold(y, x0 - 1);
5351                 place_inner_bold(y, x0 + 1);
5352         }
5353         for (x = x0 - 1; x <= x0 + 1; x++)
5354         {
5355                 place_inner_bold(y0 - 1, x);
5356                 place_inner_bold(y0 + 1, x);
5357         }
5358
5359         place_floor_bold(y0, x0);
5360
5361
5362         /* Add doors to vault */
5363         /* get two distances so can place doors relative to centre */
5364         x = (rad - 2) / 4 + 1;
5365         y = rad / 2 + x;
5366
5367         add_door(x0 + x, y0);
5368         add_door(x0 + y, y0);
5369         add_door(x0 - x, y0);
5370         add_door(x0 - y, y0);
5371         add_door(x0, y0 + x);
5372         add_door(x0, y0 + y);
5373         add_door(x0, y0 - x);
5374         add_door(x0, y0 - y);
5375
5376         /* Fill with stuff - medium difficulty */
5377         fill_treasure(x0 - rad, x0 + rad, y0 - rad, y0 + rad, randint1(3) + 3);
5378 }
5379
5380
5381 #ifdef ALLOW_CAVERNS_AND_LAKES
5382 /*
5383  * This routine uses a modified version of the lake code to make a
5384  * distribution of some terrain type over the vault.  This type
5385  * depends on the dungeon depth.
5386  *
5387  * Miniture rooms are then scattered across the vault.
5388  */
5389 static void build_elemental_vault(int x0, int y0, int xsiz, int ysiz)
5390 {
5391         int grd, roug;
5392         int c1, c2, c3;
5393         bool done = FALSE;
5394         int xsize, ysize, xhsize, yhsize, x, y, i;
5395         int type;
5396
5397
5398         if (cheat_room) msg_print("Elemental Vault");
5399
5400         /* round to make sizes even */
5401         xhsize = xsiz / 2;
5402         yhsize = ysiz / 2;
5403         xsize = xhsize * 2;
5404         ysize = yhsize * 2;
5405
5406         if (dun_level < 25)
5407         {
5408                 /* Earth vault  (Rubble) */
5409                 type = LAKE_T_EARTH_VAULT;
5410         }
5411         else if (dun_level < 50)
5412         {
5413                 /* Air vault (Trees) */
5414                 type = LAKE_T_AIR_VAULT;
5415         }
5416         else if (dun_level < 75)
5417         {
5418                 /* Water vault (shallow water) */
5419                 type = LAKE_T_WATER_VAULT;
5420         }
5421         else
5422         {
5423                 /* Fire vault (shallow lava) */
5424                 type = LAKE_T_FIRE_VAULT;
5425         }
5426
5427         while (!done)
5428         {
5429                 /* testing values for these parameters: feel free to adjust */
5430                 grd = 1 << (randint0(3));
5431
5432                 /* want average of about 16 */
5433                 roug = randint1(8) * randint1(4);
5434
5435                 /* Make up size of various componants */
5436                 /* Floor */
5437                 c3 = 2 * xsize / 3;
5438
5439                 /* Deep water/lava */
5440                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
5441
5442                 /* Shallow boundary */
5443                 c2 = (c1 + c3) / 2;
5444
5445                 /* make it */
5446                 generate_hmap(y0, x0, xsize, ysize, grd, roug, c3);
5447
5448                 /* Convert to normal format+ clean up */
5449                 done = generate_lake(y0, x0, xsize, ysize, c1, c2, c3, type);
5450         }
5451
5452         /* Set icky flag because is a vault */
5453         for (x = 0; x <= xsize; x++)
5454         {
5455                 for (y = 0; y <= ysize; y++)
5456                 {
5457                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
5458                 }
5459         }
5460
5461         /* make a few rooms in the vault */
5462         for (i = 1; i <= (xsize * ysize) / 50; i++)
5463         {
5464                 build_small_room(x0 + randint0(xsize - 4) - xsize / 2 + 2,
5465                                  y0 + randint0(ysize - 4) - ysize / 2 + 2);
5466         }
5467
5468         /* Fill with monsters and treasure, low difficulty */
5469         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1,
5470                       y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
5471 }
5472 #endif /* ALLOW_CAVERNS_AND_LAKES */
5473
5474
5475 /*!
5476  * @brief タイプ10の部屋…ランダム生成vault / Type 10 -- Random vaults
5477  * @return なし
5478  */
5479 static bool build_type10(void)
5480 {
5481         int y0, x0, xsize, ysize, vtype;
5482
5483         /* Get size */
5484         /* big enough to look good, small enough to be fairly common. */
5485         xsize = randint1(22) + 22;
5486         ysize = randint1(11) + 11;
5487
5488         /* Find and reserve some space in the dungeon.  Get center of room. */
5489         if (!find_space(&y0, &x0, ysize + 1, xsize + 1)) return FALSE;
5490
5491         /* Select type of vault */
5492 #ifdef ALLOW_CAVERNS_AND_LAKES
5493         do
5494         {
5495                 vtype = randint1(15);
5496         }
5497         while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
5498                 ((vtype == 1) || (vtype == 3) || (vtype == 8) || (vtype == 9) || (vtype == 11)));
5499 #else /* ALLOW_CAVERNS_AND_LAKES */
5500         do
5501         {
5502                 vtype = randint1(7);
5503         }
5504         while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
5505                 ((vtype == 1) || (vtype == 3)));
5506 #endif /* ALLOW_CAVERNS_AND_LAKES */
5507
5508         switch (vtype)
5509         {
5510                 /* Build an appropriate room */
5511                 case 1: case  9: build_bubble_vault(x0, y0, xsize, ysize); break;
5512                 case 2: case 10: build_room_vault(x0, y0, xsize, ysize); break;
5513                 case 3: case 11: build_cave_vault(x0, y0, xsize, ysize); break;
5514                 case 4: case 12: build_maze_vault(x0, y0, xsize, ysize, TRUE); break;
5515                 case 5: case 13: build_mini_c_vault(x0, y0, xsize, ysize); break;
5516                 case 6: case 14: build_castle_vault(x0, y0, xsize, ysize); break;
5517                 case 7: case 15: build_target_vault(x0, y0, xsize, ysize); break;
5518 #ifdef ALLOW_CAVERNS_AND_LAKES
5519                 case 8: build_elemental_vault(x0, y0, xsize, ysize); break;
5520 #endif /* ALLOW_CAVERNS_AND_LAKES */
5521                 /* I know how to add a few more... give me some time. */
5522
5523                 /* Paranoia */
5524                 default: return FALSE;
5525         }
5526
5527         return TRUE;
5528 }
5529
5530
5531 /*!
5532  * @brief タイプ11の部屋…円形部屋の生成 / Type 11 -- Build an vertical oval room.
5533  * @return なし
5534  * @details
5535  * For every grid in the possible square, check the distance.\n
5536  * If it's less than the radius, make it a room square.\n
5537  *\n
5538  * When done fill from the inside to find the walls,\n
5539  */
5540 static bool build_type11(void)
5541 {
5542         int rad, x, y, x0, y0;
5543         int light = FALSE;
5544
5545         /* Occasional light */
5546         if ((randint1(dun_level) <= 15) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
5547
5548         rad = randint0(9);
5549
5550         /* Find and reserve some space in the dungeon.  Get center of room. */
5551         if (!find_space(&y0, &x0, rad * 2 + 1, rad * 2 + 1)) return FALSE;
5552
5553         /* Make circular floor */
5554         for (x = x0 - rad; x <= x0 + rad; x++)
5555         {
5556                 for (y = y0 - rad; y <= y0 + rad; y++)
5557                 {
5558                         if (distance(y0, x0, y, x) <= rad - 1)
5559                         {
5560                                 /* inside- so is floor */
5561                                 place_floor_bold(y, x);
5562                         }
5563                         else if (distance(y0, x0, y, x) <= rad + 1)
5564                         {
5565                                 /* make granite outside so arena works */
5566                                 place_extra_bold(y, x);
5567                         }
5568                 }
5569         }
5570
5571         /* Find visible outer walls and set to be FEAT_OUTER */
5572         add_outer_wall(x0, y0, light, x0 - rad, y0 - rad, x0 + rad, y0 + rad);
5573
5574         return TRUE;
5575 }
5576
5577
5578 /*!
5579  * @brief タイプ12の部屋…ドーム型部屋の生成 / Type 12 -- Build crypt room.
5580  * @return なし
5581  * @details
5582  * For every grid in the possible square, check the (fake) distance.\n
5583  * If it's less than the radius, make it a room square.\n
5584  *\n
5585  * When done fill from the inside to find the walls,\n
5586  */
5587 static bool build_type12(void)
5588 {
5589         int rad, x, y, x0, y0;
5590         int light = FALSE;
5591         bool emptyflag = TRUE;
5592
5593         /* Make a random metric */
5594         int h1, h2, h3, h4;
5595         h1 = randint1(32) - 16;
5596         h2 = randint1(16);
5597         h3 = randint1(32);
5598         h4 = randint1(32) - 16;
5599
5600         /* Occasional light */
5601         if ((randint1(dun_level) <= 5) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
5602
5603         rad = randint1(9);
5604
5605         /* Find and reserve some space in the dungeon.  Get center of room. */
5606         if (!find_space(&y0, &x0, rad * 2 + 3, rad * 2 + 3)) return FALSE;
5607
5608         /* Make floor */
5609         for (x = x0 - rad; x <= x0 + rad; x++)
5610         {
5611                 for (y = y0 - rad; y <= y0 + rad; y++)
5612                 {
5613                         /* clear room flag */
5614                         cave[y][x].info &= ~(CAVE_ROOM);
5615
5616                         if (dist2(y0, x0, y, x, h1, h2, h3, h4) <= rad - 1)
5617                         {
5618                                 /* inside - so is floor */
5619                                 place_floor_bold(y, x);
5620                         }
5621                         else if (distance(y0, x0, y, x) < 3)
5622                         {
5623                                 place_floor_bold(y, x);
5624                         }
5625                         else
5626                         {
5627                                 /* make granite outside so arena works */
5628                                 place_extra_bold(y, x);
5629                         }
5630
5631                         /* proper boundary for arena */
5632                         if (((y + rad) == y0) || ((y - rad) == y0) ||
5633                             ((x + rad) == x0) || ((x - rad) == x0))
5634                         {
5635                                 place_extra_bold(y, x);
5636                         }
5637                 }
5638         }
5639
5640         /* Find visible outer walls and set to be FEAT_OUTER */
5641         add_outer_wall(x0, y0, light, x0 - rad - 1, y0 - rad - 1,
5642                        x0 + rad + 1, y0 + rad + 1);
5643
5644         /* Check to see if there is room for an inner vault */
5645         for (x = x0 - 2; x <= x0 + 2; x++)
5646         {
5647                 for (y = y0 - 2; y <= y0 + 2; y++)
5648                 {
5649                         if (!is_floor_bold(y, x))
5650                         {
5651                                 /* Wall in the way */
5652                                 emptyflag = FALSE;
5653                         }
5654                 }
5655         }
5656
5657         if (emptyflag && one_in_(2))
5658         {
5659                 /* Build the vault */
5660                 build_small_room(x0, y0);
5661
5662                 /* Place a treasure in the vault */
5663                 place_object(y0, x0, 0L);
5664
5665                 /* Let's guard the treasure well */
5666                 vault_monsters(y0, x0, randint0(2) + 3);
5667
5668                 /* Traps naturally */
5669                 vault_traps(y0, x0, 4, 4, randint0(3) + 2);
5670         }
5671
5672         return TRUE;
5673 }
5674
5675
5676 /*
5677  * Helper function for "trapped monster pit"
5678  */
5679 static bool vault_aux_trapped_pit(int r_idx)
5680 {
5681         monster_race *r_ptr = &r_info[r_idx];
5682
5683         /* Validate the monster */
5684         if (!vault_monster_okay(r_idx)) return (FALSE);
5685
5686         /* No wall passing monster */
5687         if (r_ptr->flags2 & (RF2_PASS_WALL | RF2_KILL_WALL)) return (FALSE);
5688
5689         /* Okay */
5690         return (TRUE);
5691 }
5692
5693
5694 /*!
5695  * @brief タイプ13の部屋…トラップpitの生成 / Type 13 -- Trapped monster pits
5696  * @return なし
5697  * @details
5698  * A trapped monster pit is a "big" room with a straight corridor in\n
5699  * which wall opening traps are placed, and with two "inner" rooms\n
5700  * containing a "collection" of monsters of a given type organized in\n
5701  * the room.\n
5702  *\n
5703  * The trapped monster pit appears as shown below, where the actual\n
5704  * monsters in each location depend on the type of the pit\n
5705  *\n
5706  *  XXXXXXXXXXXXXXXXXXXXXXXXX\n
5707  *  X                       X\n
5708  *  XXXXXXXXXXXXXXXXXXXXXXX X\n
5709  *  XXXXX001123454321100XXX X\n
5710  *  XXX0012234567654322100X X\n
5711  *  XXXXXXXXXXXXXXXXXXXXXXX X\n
5712  *  X           ^           X\n
5713  *  X XXXXXXXXXXXXXXXXXXXXXXX\n
5714  *  X X0012234567654322100XXX\n
5715  *  X XXX001123454321100XXXXX\n
5716  *  X XXXXXXXXXXXXXXXXXXXXXXX\n
5717  *  X                       X\n
5718  *  XXXXXXXXXXXXXXXXXXXXXXXXX\n
5719  *\n
5720  * Note that the monsters in the pit are now chosen by using "get_mon_num()"\n
5721  * to request 16 "appropriate" monsters, sorting them by level, and using\n
5722  * the "even" entries in this sorted list for the contents of the pit.\n
5723  *\n
5724  * Hack -- all of the "dragons" in a "dragon" pit must be the same "color",\n
5725  * which is handled by requiring a specific "breath" attack for all of the\n
5726  * dragons.  This may include "multi-hued" breath.  Note that "wyrms" may\n
5727  * be present in many of the dragon pits, if they have the proper breath.\n
5728  *\n
5729  * Note the use of the "get_mon_num_prep()" function, and the special\n
5730  * "get_mon_num_hook()" restriction function, to prepare the "monster\n
5731  * allocation table" in such a way as to optimize the selection of\n
5732  * "appropriate" non-unique monsters for the pit.\n
5733  *\n
5734  * Note that the "get_mon_num()" function may (rarely) fail, in which case\n
5735  * the pit will be empty.\n
5736  *\n
5737  * Note that "monster pits" will never contain "unique" monsters.\n
5738  */
5739 static bool build_type13(void)
5740 {
5741         static int placing[][3] = {
5742                 {-2, -9, 0}, {-2, -8, 0}, {-3, -7, 0}, {-3, -6, 0},
5743                 {+2, -9, 0}, {+2, -8, 0}, {+3, -7, 0}, {+3, -6, 0},
5744                 {-2, +9, 0}, {-2, +8, 0}, {-3, +7, 0}, {-3, +6, 0},
5745                 {+2, +9, 0}, {+2, +8, 0}, {+3, +7, 0}, {+3, +6, 0},
5746                 {-2, -7, 1}, {-3, -5, 1}, {-3, -4, 1}, 
5747                 {+2, -7, 1}, {+3, -5, 1}, {+3, -4, 1}, 
5748                 {-2, +7, 1}, {-3, +5, 1}, {-3, +4, 1}, 
5749                 {+2, +7, 1}, {+3, +5, 1}, {+3, +4, 1},
5750                 {-2, -6, 2}, {-2, -5, 2}, {-3, -3, 2},
5751                 {+2, -6, 2}, {+2, -5, 2}, {+3, -3, 2},
5752                 {-2, +6, 2}, {-2, +5, 2}, {-3, +3, 2},
5753                 {+2, +6, 2}, {+2, +5, 2}, {+3, +3, 2},
5754                 {-2, -4, 3}, {-3, -2, 3},
5755                 {+2, -4, 3}, {+3, -2, 3},
5756                 {-2, +4, 3}, {-3, +2, 3},
5757                 {+2, +4, 3}, {+3, +2, 3},
5758                 {-2, -3, 4}, {-3, -1, 4},
5759                 {+2, -3, 4}, {+3, -1, 4},
5760                 {-2, +3, 4}, {-3, +1, 4},
5761                 {+2, +3, 4}, {+3, +1, 4},
5762                 {-2, -2, 5}, {-3, 0, 5}, {-2, +2, 5},
5763                 {+2, -2, 5}, {+3, 0, 5}, {+2, +2, 5},
5764                 {-2, -1, 6}, {-2, +1, 6},
5765                 {+2, -1, 6}, {+2, +1, 6},
5766                 {-2, 0, 7}, {+2, 0, 7},
5767                 {0, 0, -1}
5768         };
5769
5770         int y, x, y1, x1, y2, x2, xval, yval;
5771         int i, j;
5772
5773         int what[16];
5774
5775         monster_type align;
5776
5777         cave_type *c_ptr;
5778
5779         int cur_pit_type = pick_vault_type(pit_types, d_info[dungeon_type].pit);
5780         vault_aux_type *n_ptr;
5781
5782         /* Only in Angband */
5783         if (dungeon_type != DUNGEON_ANGBAND) return FALSE;
5784
5785         /* No type available */
5786         if (cur_pit_type < 0) return FALSE;
5787
5788         n_ptr = &pit_types[cur_pit_type];
5789
5790         /* Process a preparation function if necessary */
5791         if (n_ptr->prep_func) (*(n_ptr->prep_func))();
5792
5793         /* Prepare allocation table */
5794         get_mon_num_prep(n_ptr->hook_func, vault_aux_trapped_pit);
5795
5796         align.sub_align = SUB_ALIGN_NEUTRAL;
5797
5798         /* Pick some monster types */
5799         for (i = 0; i < 16; i++)
5800         {
5801                 int r_idx = 0, attempts = 100;
5802                 monster_race *r_ptr = NULL;
5803
5804                 while (attempts--)
5805                 {
5806                         /* Get a (hard) monster type */
5807                         r_idx = get_mon_num(dun_level + 0);
5808                         r_ptr = &r_info[r_idx];
5809
5810                         /* Decline incorrect alignment */
5811                         if (monster_has_hostile_align(&align, 0, 0, r_ptr)) continue;
5812
5813                         /* Accept this monster */
5814                         break;
5815                 }
5816
5817                 /* Notice failure */
5818                 if (!r_idx || !attempts) return FALSE;
5819
5820                 /* Note the alignment */
5821                 if (r_ptr->flags3 & RF3_EVIL) align.sub_align |= SUB_ALIGN_EVIL;
5822                 if (r_ptr->flags3 & RF3_GOOD) align.sub_align |= SUB_ALIGN_GOOD;
5823
5824                 what[i] = r_idx;
5825         }
5826
5827         /* Find and reserve some space in the dungeon.  Get center of room. */
5828         if (!find_space(&yval, &xval, 13, 25)) return FALSE;
5829
5830         /* Large room */
5831         y1 = yval - 5;
5832         y2 = yval + 5;
5833         x1 = xval - 11;
5834         x2 = xval + 11;
5835
5836         /* Fill with inner walls */
5837         for (y = y1 - 1; y <= y2 + 1; y++)
5838         {
5839                 for (x = x1 - 1; x <= x2 + 1; x++)
5840                 {
5841                         c_ptr = &cave[y][x];
5842                         place_inner_grid(c_ptr);
5843                         c_ptr->info |= (CAVE_ROOM);
5844                 }
5845         }
5846
5847         /* Place the floor area 1 */
5848         for (x = x1 + 3; x <= x2 - 3; x++)
5849         {
5850                 c_ptr = &cave[yval-2][x];
5851                 place_floor_grid(c_ptr);
5852                 add_cave_info(yval-2, x, CAVE_ICKY);
5853
5854                 c_ptr = &cave[yval+2][x];
5855                 place_floor_grid(c_ptr);
5856                 add_cave_info(yval+2, x, CAVE_ICKY);
5857         }
5858
5859         /* Place the floor area 2 */
5860         for (x = x1 + 5; x <= x2 - 5; x++)
5861         {
5862                 c_ptr = &cave[yval-3][x];
5863                 place_floor_grid(c_ptr);
5864                 add_cave_info(yval-3, x, CAVE_ICKY);
5865
5866                 c_ptr = &cave[yval+3][x];
5867                 place_floor_grid(c_ptr);
5868                 add_cave_info(yval+3, x, CAVE_ICKY);
5869         }
5870
5871         /* Corridor */
5872         for (x = x1; x <= x2; x++)
5873         {
5874                 c_ptr = &cave[yval][x];
5875                 place_floor_grid(c_ptr);
5876                 c_ptr = &cave[y1][x];
5877                 place_floor_grid(c_ptr);
5878                 c_ptr = &cave[y2][x];
5879                 place_floor_grid(c_ptr);
5880         }
5881
5882         /* Place the outer walls */
5883         for (y = y1 - 1; y <= y2 + 1; y++)
5884         {
5885                 c_ptr = &cave[y][x1 - 1];
5886                 place_outer_grid(c_ptr);
5887                 c_ptr = &cave[y][x2 + 1];
5888                 place_outer_grid(c_ptr);
5889         }
5890         for (x = x1 - 1; x <= x2 + 1; x++)
5891         {
5892                 c_ptr = &cave[y1 - 1][x];
5893                 place_outer_grid(c_ptr);
5894                 c_ptr = &cave[y2 + 1][x];
5895                 place_outer_grid(c_ptr);
5896         }
5897
5898         /* Random corridor */
5899         if (one_in_(2))
5900         {
5901                 for (y = y1; y <= yval; y++)
5902                 {
5903                         place_floor_bold(y, x2);
5904                         place_solid_bold(y, x1-1);
5905                 }
5906                 for (y = yval; y <= y2 + 1; y++)
5907                 {
5908                         place_floor_bold(y, x1);
5909                         place_solid_bold(y, x2+1);
5910                 }
5911         }
5912         else
5913         {
5914                 for (y = yval; y <= y2 + 1; y++)
5915                 {
5916                         place_floor_bold(y, x1);
5917                         place_solid_bold(y, x2+1);
5918                 }
5919                 for (y = y1; y <= yval; y++)
5920                 {
5921                         place_floor_bold(y, x2);
5922                         place_solid_bold(y, x1-1);
5923                 }
5924         }
5925
5926         /* Place the wall open trap */
5927         cave[yval][xval].mimic = cave[yval][xval].feat;
5928         cave[yval][xval].feat = feat_trap_open;
5929
5930         /* Sort the entries */
5931         for (i = 0; i < 16 - 1; i++)
5932         {
5933                 /* Sort the entries */
5934                 for (j = 0; j < 16 - 1; j++)
5935                 {
5936                         int i1 = j;
5937                         int i2 = j + 1;
5938
5939                         int p1 = r_info[what[i1]].level;
5940                         int p2 = r_info[what[i2]].level;
5941
5942                         /* Bubble */
5943                         if (p1 > p2)
5944                         {
5945                                 int tmp = what[i1];
5946                                 what[i1] = what[i2];
5947                                 what[i2] = tmp;
5948                         }
5949                 }
5950         }
5951
5952         /* Message */
5953         if (cheat_room)
5954         {
5955                 /* Room type */
5956                 msg_format(_("%s%sの罠ピット", "Trapped monster pit (%s%s)"), n_ptr->name, pit_subtype_string(cur_pit_type, FALSE));
5957         }
5958
5959         /* Select the entries */
5960         for (i = 0; i < 8; i++)
5961         {
5962                 /* Every other entry */
5963                 what[i] = what[i * 2];
5964
5965                 if (cheat_hear)
5966                 {
5967                         /* Message */
5968                         msg_print(r_name + r_info[what[i]].name);
5969                 }
5970         }
5971
5972         for (i = 0; placing[i][2] >= 0; i++)
5973         {
5974                 y = yval + placing[i][0];
5975                 x = xval + placing[i][1];
5976                 place_monster_aux(0, y, x, what[placing[i][2]], PM_NO_KAGE);
5977         }
5978
5979         return TRUE;
5980 }
5981
5982
5983 /*!
5984  * @brief タイプ14の部屋…特殊トラップ部屋の生成 / Type 14 -- trapped rooms
5985  * @return なし
5986  * @details
5987  * A special trap is placed at center of the room
5988  */
5989 static bool build_type14(void)
5990 {
5991         int y, x, y2, x2, yval, xval;
5992         int y1, x1, xsize, ysize;
5993
5994         bool light;
5995
5996         cave_type *c_ptr;
5997         s16b trap;
5998
5999         /* Pick a room size */
6000         y1 = randint1(4);
6001         x1 = randint1(11);
6002         y2 = randint1(3);
6003         x2 = randint1(11);
6004
6005         xsize = x1 + x2 + 1;
6006         ysize = y1 + y2 + 1;
6007
6008         /* Find and reserve some space in the dungeon.  Get center of room. */
6009         if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
6010
6011         /* Choose lite or dark */
6012         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
6013
6014
6015         /* Get corner values */
6016         y1 = yval - ysize / 2;
6017         x1 = xval - xsize / 2;
6018         y2 = yval + (ysize - 1) / 2;
6019         x2 = xval + (xsize - 1) / 2;
6020
6021
6022         /* Place a full floor under the room */
6023         for (y = y1 - 1; y <= y2 + 1; y++)
6024         {
6025                 for (x = x1 - 1; x <= x2 + 1; x++)
6026                 {
6027                         c_ptr = &cave[y][x];
6028                         place_floor_grid(c_ptr);
6029                         c_ptr->info |= (CAVE_ROOM);
6030                         if (light) c_ptr->info |= (CAVE_GLOW);
6031                 }
6032         }
6033
6034         /* Walls around the room */
6035         for (y = y1 - 1; y <= y2 + 1; y++)
6036         {
6037                 c_ptr = &cave[y][x1 - 1];
6038                 place_outer_grid(c_ptr);
6039                 c_ptr = &cave[y][x2 + 1];
6040                 place_outer_grid(c_ptr);
6041         }
6042         for (x = x1 - 1; x <= x2 + 1; x++)
6043         {
6044                 c_ptr = &cave[y1 - 1][x];
6045                 place_outer_grid(c_ptr);
6046                 c_ptr = &cave[y2 + 1][x];
6047                 place_outer_grid(c_ptr);
6048         }
6049
6050         if (dun_level < 30 + randint1(30))
6051                 trap = feat_trap_piranha;
6052         else
6053                 trap = feat_trap_armageddon;
6054
6055         /* Place a special trap */
6056         c_ptr = &cave[rand_spread(yval, ysize/4)][rand_spread(xval, xsize/4)];
6057         c_ptr->mimic = c_ptr->feat;
6058         c_ptr->feat = trap;
6059
6060         /* Message */
6061         if (cheat_room)
6062         {
6063                 msg_format(_("%sの部屋", "Room of %s"), f_name + f_info[trap].name);
6064         }
6065
6066         return TRUE;
6067 }
6068
6069
6070 /*
6071  * Helper function for "glass room"
6072  */
6073 static bool vault_aux_lite(int r_idx)
6074 {
6075         monster_race *r_ptr = &r_info[r_idx];
6076
6077         /* Validate the monster */
6078         if (!vault_monster_okay(r_idx)) return FALSE;
6079
6080         /* Require lite attack */
6081         if (!(r_ptr->flags4 & RF4_BR_LITE) && !(r_ptr->a_ability_flags1 & RF5_BA_LITE)) return FALSE;
6082
6083         /* No wall passing monsters */
6084         if (r_ptr->flags2 & (RF2_PASS_WALL | RF2_KILL_WALL)) return FALSE;
6085
6086         /* No disintegrating monsters */
6087         if (r_ptr->flags4 & RF4_BR_DISI) return FALSE;
6088
6089         return TRUE;
6090 }
6091
6092 /*
6093  * Helper function for "glass room"
6094  */
6095 static bool vault_aux_shards(int r_idx)
6096 {
6097         monster_race *r_ptr = &r_info[r_idx];
6098
6099         /* Validate the monster */
6100         if (!vault_monster_okay(r_idx)) return FALSE;
6101
6102         /* Require shards breath attack */
6103         if (!(r_ptr->flags4 & RF4_BR_SHAR)) return FALSE;
6104
6105         return TRUE;
6106 }
6107
6108 /*
6109  * Hack -- determine if a template is potion
6110  */
6111 static bool kind_is_potion(int k_idx)
6112 {
6113         return k_info[k_idx].tval == TV_POTION;
6114 }
6115
6116 /*!
6117  * @brief タイプ15の部屋…ガラス部屋の生成 / Type 15 -- glass rooms
6118  * @return なし
6119  */
6120 static bool build_type15(void)
6121 {
6122         int y, x, y2, x2, yval, xval;
6123         int y1, x1, xsize, ysize;
6124         bool light;
6125
6126         cave_type *c_ptr;
6127
6128         /* Pick a room size */
6129         xsize = rand_range(9, 13);
6130         ysize = rand_range(9, 13);
6131
6132         /* Find and reserve some space in the dungeon.  Get center of room. */
6133         if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
6134
6135         /* Choose lite or dark */
6136         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
6137
6138         /* Get corner values */
6139         y1 = yval - ysize / 2;
6140         x1 = xval - xsize / 2;
6141         y2 = yval + (ysize - 1) / 2;
6142         x2 = xval + (xsize - 1) / 2;
6143
6144         /* Place a full floor under the room */
6145         for (y = y1 - 1; y <= y2 + 1; y++)
6146         {
6147                 for (x = x1 - 1; x <= x2 + 1; x++)
6148                 {
6149                         c_ptr = &cave[y][x];
6150                         place_floor_grid(c_ptr);
6151                         c_ptr->feat = feat_glass_floor;
6152                         c_ptr->info |= (CAVE_ROOM);
6153                         if (light) c_ptr->info |= (CAVE_GLOW);
6154                 }
6155         }
6156
6157         /* Walls around the room */
6158         for (y = y1 - 1; y <= y2 + 1; y++)
6159         {
6160                 c_ptr = &cave[y][x1 - 1];
6161                 place_outer_grid(c_ptr);
6162                 c_ptr->feat = feat_glass_wall;
6163                 c_ptr = &cave[y][x2 + 1];
6164                 place_outer_grid(c_ptr);
6165                 c_ptr->feat = feat_glass_wall;
6166         }
6167         for (x = x1 - 1; x <= x2 + 1; x++)
6168         {
6169                 c_ptr = &cave[y1 - 1][x];
6170                 place_outer_grid(c_ptr);
6171                 c_ptr->feat = feat_glass_wall;
6172                 c_ptr = &cave[y2 + 1][x];
6173                 place_outer_grid(c_ptr);
6174                 c_ptr->feat = feat_glass_wall;
6175         }
6176
6177         switch (randint1(3))
6178         {
6179         case 1: /* 4 lite breathers + potion */
6180                 {
6181                         int dir1, dir2;
6182
6183                         /* Prepare allocation table */
6184                         get_mon_num_prep(vault_aux_lite, NULL);
6185
6186                         /* Place fixed lite berathers */
6187                         for (dir1 = 4; dir1 < 8; dir1++)
6188                         {
6189                                 int r_idx = get_mon_num(dun_level);
6190
6191                                 y = yval + 2 * ddy_ddd[dir1];
6192                                 x = xval + 2 * ddx_ddd[dir1];
6193                                 if (r_idx) place_monster_aux(0, y, x, r_idx, PM_ALLOW_SLEEP);
6194
6195                                 /* Walls around the breather */
6196                                 for (dir2 = 0; dir2 < 8; dir2++)
6197                                 {
6198                                         c_ptr = &cave[y + ddy_ddd[dir2]][x + ddx_ddd[dir2]];
6199                                         place_inner_grid(c_ptr);
6200                                         c_ptr->feat = feat_glass_wall;
6201                                 }
6202                         }
6203
6204                         /* Walls around the potion */
6205                         for (dir1 = 0; dir1 < 4; dir1++)
6206                         {
6207                                 y = yval + 2 * ddy_ddd[dir1];
6208                                 x = xval + 2 * ddx_ddd[dir1];
6209                                 c_ptr = &cave[y][x];
6210                                 place_inner_perm_grid(c_ptr);
6211                                 c_ptr->feat = feat_permanent_glass_wall;
6212                                 cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]].info |= (CAVE_ICKY);
6213                         }
6214
6215                         /* Glass door */
6216                         dir1 = randint0(4);
6217                         y = yval + 2 * ddy_ddd[dir1];
6218                         x = xval + 2 * ddx_ddd[dir1];
6219                         place_secret_door(y, x, DOOR_GLASS_DOOR);
6220                         c_ptr = &cave[y][x];
6221                         if (is_closed_door(c_ptr->feat)) c_ptr->mimic = feat_glass_wall;
6222
6223                         /* Place a potion */
6224                         get_obj_num_hook = kind_is_potion;
6225                         place_object(yval, xval, AM_NO_FIXED_ART);
6226                         cave[yval][xval].info |= (CAVE_ICKY);
6227                 }
6228                 break;
6229
6230         case 2: /* 1 lite breather + random object */
6231                 {
6232                         int r_idx, dir1;
6233
6234                         /* Pillars */
6235                         c_ptr = &cave[y1 + 1][x1 + 1];
6236                         place_inner_grid(c_ptr);
6237                         c_ptr->feat = feat_glass_wall;
6238
6239                         c_ptr = &cave[y1 + 1][x2 - 1];
6240                         place_inner_grid(c_ptr);
6241                         c_ptr->feat = feat_glass_wall;
6242
6243                         c_ptr = &cave[y2 - 1][x1 + 1];
6244                         place_inner_grid(c_ptr);
6245                         c_ptr->feat = feat_glass_wall;
6246
6247                         c_ptr = &cave[y2 - 1][x2 - 1];
6248                         place_inner_grid(c_ptr);
6249                         c_ptr->feat = feat_glass_wall;
6250
6251                         /* Prepare allocation table */
6252                         get_mon_num_prep(vault_aux_lite, NULL);
6253
6254                         r_idx = get_mon_num(dun_level);
6255                         if (r_idx) place_monster_aux(0, yval, xval, r_idx, 0L);
6256
6257                         /* Walls around the breather */
6258                         for (dir1 = 0; dir1 < 8; dir1++)
6259                         {
6260                                 c_ptr = &cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]];
6261                                 place_inner_grid(c_ptr);
6262                                 c_ptr->feat = feat_glass_wall;
6263                         }
6264
6265                         /* Curtains around the breather */
6266                         for (y = yval - 1; y <= yval + 1; y++)
6267                         {
6268                                 place_closed_door(y, xval - 2, DOOR_CURTAIN);
6269                                 place_closed_door(y, xval + 2, DOOR_CURTAIN);
6270                         }
6271                         for (x = xval - 1; x <= xval + 1; x++)
6272                         {
6273                                 place_closed_door(yval - 2, x, DOOR_CURTAIN);
6274                                 place_closed_door(yval + 2, x, DOOR_CURTAIN);
6275                         }
6276
6277                         /* Place an object */
6278                         place_object(yval, xval, AM_NO_FIXED_ART);
6279                         cave[yval][xval].info |= (CAVE_ICKY);
6280                 }
6281                 break;
6282
6283         case 3: /* 4 shards breathers + 2 potions */
6284                 {
6285                         int dir1;
6286
6287                         /* Walls around the potion */
6288                         for (y = yval - 2; y <= yval + 2; y++)
6289                         {
6290                                 c_ptr = &cave[y][xval - 3];
6291                                 place_inner_grid(c_ptr);
6292                                 c_ptr->feat = feat_glass_wall;
6293                                 c_ptr = &cave[y][xval + 3];
6294                                 place_inner_grid(c_ptr);
6295                                 c_ptr->feat = feat_glass_wall;
6296                         }
6297                         for (x = xval - 2; x <= xval + 2; x++)
6298                         {
6299                                 c_ptr = &cave[yval - 3][x];
6300                                 place_inner_grid(c_ptr);
6301                                 c_ptr->feat = feat_glass_wall;
6302                                 c_ptr = &cave[yval + 3][x];
6303                                 place_inner_grid(c_ptr);
6304                                 c_ptr->feat = feat_glass_wall;
6305                         }
6306                         for (dir1 = 4; dir1 < 8; dir1++)
6307                         {
6308                                 c_ptr = &cave[yval + 2 * ddy_ddd[dir1]][xval + 2 * ddx_ddd[dir1]];
6309                                 place_inner_grid(c_ptr);
6310                                 c_ptr->feat = feat_glass_wall;
6311                         }
6312
6313                         /* Prepare allocation table */
6314                         get_mon_num_prep(vault_aux_shards, NULL);
6315
6316                         /* Place shard berathers */
6317                         for (dir1 = 4; dir1 < 8; dir1++)
6318                         {
6319                                 int r_idx = get_mon_num(dun_level);
6320
6321                                 y = yval + ddy_ddd[dir1];
6322                                 x = xval + ddx_ddd[dir1];
6323                                 if (r_idx) place_monster_aux(0, y, x, r_idx, 0L);
6324                         }
6325
6326                         /* Place two potions */
6327                         if (one_in_(2))
6328                         {
6329                                 get_obj_num_hook = kind_is_potion;
6330                                 place_object(yval, xval - 1, AM_NO_FIXED_ART);
6331                                 get_obj_num_hook = kind_is_potion;
6332                                 place_object(yval, xval + 1, AM_NO_FIXED_ART);
6333                         }
6334                         else
6335                         {
6336                                 get_obj_num_hook = kind_is_potion;
6337                                 place_object(yval - 1, xval, AM_NO_FIXED_ART);
6338                                 get_obj_num_hook = kind_is_potion;
6339                                 place_object(yval + 1, xval, AM_NO_FIXED_ART);
6340                         }
6341
6342                         for (y = yval - 2; y <= yval + 2; y++)
6343                                 for (x = xval - 2; x <= xval + 2; x++)
6344                                         cave[y][x].info |= (CAVE_ICKY);
6345
6346                 }
6347                 break;
6348         }
6349
6350         /* Message */
6351         if (cheat_room)
6352         {
6353                 msg_print(_("ガラスの部屋", "Glass room"));
6354         }
6355
6356         return TRUE;
6357 }
6358
6359
6360 /* Create a new floor room with optional light */
6361 void generate_room_floor(int y1, int x1, int y2, int x2, int light)
6362 {
6363         int y, x;
6364         
6365         cave_type *c_ptr;
6366
6367         for (y = y1; y <= y2; y++)
6368         {
6369                 for (x = x1; x <= x2; x++)
6370                 {
6371                         /* Point to grid */
6372                         c_ptr = &cave[y][x];
6373                         place_floor_grid(c_ptr);
6374                         c_ptr->info |= (CAVE_ROOM);
6375                         if (light) c_ptr->info |= (CAVE_GLOW);
6376                 }
6377         }
6378 }
6379
6380 void generate_fill_perm_bold(int y1, int x1, int y2, int x2)
6381 {
6382         int y, x;
6383
6384         for (y = y1; y <= y2; y++)
6385         {
6386                 for (x = x1; x <= x2; x++)
6387                 {
6388                         /* Point to grid */
6389                         place_inner_perm_bold(y, x);
6390                 }
6391         }
6392 }
6393
6394 /* Minimum & maximum town size */
6395 #define MIN_TOWN_WID ((MAX_WID / 3) / 2)
6396 #define MIN_TOWN_HGT ((MAX_HGT / 3) / 2)
6397 #define MAX_TOWN_WID ((MAX_WID / 3) * 2 / 3)
6398 #define MAX_TOWN_HGT ((MAX_HGT / 3) * 2 / 3)
6399
6400 /* Struct for build underground buildings */
6401 typedef struct
6402 {
6403         int y0, x0; /* North-west corner (relative) */
6404         int y1, x1; /* South-east corner (relative) */
6405 }
6406 ugbldg_type;
6407
6408 ugbldg_type *ugbldg;
6409
6410 /*
6411  * Precalculate buildings' location of underground arcade
6412  */
6413 static bool precalc_ugarcade(int town_hgt, int town_wid, int n)
6414 {
6415         int i, y, x, center_y, center_x, tmp, attempt = 10000;
6416         int max_bldg_hgt = 3 * town_hgt / MAX_TOWN_HGT;
6417         int max_bldg_wid = 5 * town_wid / MAX_TOWN_WID;
6418         ugbldg_type *cur_ugbldg;
6419         bool **ugarcade_used, abort;
6420
6421         /* Allocate "ugarcade_used" array (2-dimension) */
6422         C_MAKE(ugarcade_used, town_hgt, bool *);
6423         C_MAKE(*ugarcade_used, town_hgt * town_wid, bool);
6424         for (y = 1; y < town_hgt; y++) ugarcade_used[y] = *ugarcade_used + y * town_wid;
6425
6426         /* Calculate building locations */
6427         for (i = 0; i < n; i++)
6428         {
6429                 cur_ugbldg = &ugbldg[i];
6430                 (void)WIPE(cur_ugbldg, ugbldg_type);
6431
6432                 do
6433                 {
6434                         /* Find the "center" of the store */
6435                         center_y = rand_range(2, town_hgt - 3);
6436                         center_x = rand_range(2, town_wid - 3);
6437
6438                         /* Determine the store boundaries */
6439                         tmp = center_y - randint1(max_bldg_hgt);
6440                         cur_ugbldg->y0 = MAX(tmp, 1);
6441                         tmp = center_x - randint1(max_bldg_wid);
6442                         cur_ugbldg->x0 = MAX(tmp, 1);
6443                         tmp = center_y + randint1(max_bldg_hgt);
6444                         cur_ugbldg->y1 = MIN(tmp, town_hgt - 2);
6445                         tmp = center_x + randint1(max_bldg_wid);
6446                         cur_ugbldg->x1 = MIN(tmp, town_wid - 2);
6447
6448                         /* Scan this building's area */
6449                         for (abort = FALSE, y = cur_ugbldg->y0; (y <= cur_ugbldg->y1) && !abort; y++)
6450                         {
6451                                 for (x = cur_ugbldg->x0; x <= cur_ugbldg->x1; x++)
6452                                 {
6453                                         if (ugarcade_used[y][x])
6454                                         {
6455                                                 abort = TRUE;
6456                                                 break;
6457                                         }
6458                                 }
6459                         }
6460
6461                         attempt--;
6462                 }
6463                 while (abort && attempt); /* Accept this building if no overlapping */
6464
6465                 /* Failed to generate underground arcade */
6466                 if (!attempt) break;
6467
6468                 /*
6469                  * Mark to ugarcade_used[][] as "used"
6470                  * Note: Building-adjacent grids are included for preventing
6471                  * connected bulidings.
6472                  */
6473                 for (y = cur_ugbldg->y0 - 1; y <= cur_ugbldg->y1 + 1; y++)
6474                 {
6475                         for (x = cur_ugbldg->x0 - 1; x <= cur_ugbldg->x1 + 1; x++)
6476                         {
6477                                 ugarcade_used[y][x] = TRUE;
6478                         }
6479                 }
6480         }
6481
6482         /* Free "ugarcade_used" array (2-dimension) */
6483         C_KILL(*ugarcade_used, town_hgt * town_wid, bool);
6484         C_KILL(ugarcade_used, town_hgt, bool *);
6485
6486         /* If i < n, generation is not allowed */
6487         return i == n;
6488 }
6489
6490 /*!
6491  * @brief タイプ16の部屋…地下都市生成のサブルーチン / Actually create buildings
6492  * @return なし
6493  * @param ltcy 生成基準Y座標
6494  * @param ltcx 生成基準X座標
6495  * @param stotes[] 生成する店舗のリスト
6496  * @param n 生成する店舗の数
6497  * @note
6498  * Note: ltcy and ltcx indicate "left top corner".
6499  */
6500 static void build_stores(int ltcy, int ltcx, int stores[], int n)
6501 {
6502         int i, j, y, x;
6503         ugbldg_type *cur_ugbldg;
6504
6505         for (i = 0; i < n; i++)
6506         {
6507                 cur_ugbldg = &ugbldg[i];
6508
6509                 /* Generate new room */
6510                 generate_room_floor(
6511                         ltcy + cur_ugbldg->y0 - 2, ltcx + cur_ugbldg->x0 - 2,
6512                         ltcy + cur_ugbldg->y1 + 2, ltcx + cur_ugbldg->x1 + 2,
6513                         FALSE);
6514         }
6515
6516         for (i = 0; i < n; i++)
6517         {
6518                 cur_ugbldg = &ugbldg[i];
6519
6520                 /* Build an invulnerable rectangular building */
6521                 generate_fill_perm_bold(
6522                         ltcy + cur_ugbldg->y0, ltcx + cur_ugbldg->x0,
6523                         ltcy + cur_ugbldg->y1, ltcx + cur_ugbldg->x1);
6524
6525                 /* Pick a door direction (S,N,E,W) */
6526                 switch (randint0(4))
6527                 {
6528                 /* Bottom side */
6529                 case 0:
6530                         y = cur_ugbldg->y1;
6531                         x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
6532                         break;
6533
6534                 /* Top side */
6535                 case 1:
6536                         y = cur_ugbldg->y0;
6537                         x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
6538                         break;
6539
6540                 /* Right side */
6541                 case 2:
6542                         y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
6543                         x = cur_ugbldg->x1;
6544                         break;
6545
6546                 /* Left side */
6547                 default:
6548                         y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
6549                         x = cur_ugbldg->x0;
6550                         break;
6551                 }
6552
6553                 for (j = 0; j < max_f_idx; j++)
6554                 {
6555                         if (have_flag(f_info[j].flags, FF_STORE))
6556                         {
6557                                 if (f_info[j].subtype == stores[i]) break;
6558                         }
6559                 }
6560
6561                 /* Clear previous contents, add a store door */
6562                 if (j < max_f_idx)
6563                 {
6564                         cave_set_feat(ltcy + y, ltcx + x, j);
6565
6566                         /* Init store */
6567                         store_init(NO_TOWN, stores[i]);
6568                 }
6569         }
6570 }
6571
6572
6573 /*!
6574  * @brief タイプ16の部屋…地下都市の生成 / Type 16 -- Underground Arcade
6575  * @return なし
6576  * @details
6577  * Town logic flow for generation of new town\n
6578  * Originally from Vanilla 3.0.3\n
6579  *\n
6580  * We start with a fully wiped cave of normal floors.\n
6581  *\n
6582  * Note that town_gen_hack() plays games with the R.N.G.\n
6583  *\n
6584  * This function does NOT do anything about the owners of the stores,\n
6585  * nor the contents thereof.  It only handles the physical layout.\n
6586  */
6587 static bool build_type16(void)
6588 {
6589         int stores[] =
6590         {
6591                 STORE_GENERAL, STORE_ARMOURY, STORE_WEAPON, STORE_TEMPLE,
6592                 STORE_ALCHEMIST, STORE_MAGIC, STORE_BLACK, STORE_BOOK,
6593         };
6594         int n = sizeof stores / sizeof (int);
6595         int i, y, x, y1, x1, yval, xval;
6596         int town_hgt = rand_range(MIN_TOWN_HGT, MAX_TOWN_HGT);
6597         int town_wid = rand_range(MIN_TOWN_WID, MAX_TOWN_WID);
6598         bool prevent_bm = FALSE;
6599
6600         /* Hack -- If already exist black market, prevent building */
6601         for (y = 0; (y < cur_hgt) && !prevent_bm; y++)
6602         {
6603                 for (x = 0; x < cur_wid; x++)
6604                 {
6605                         if (cave[y][x].feat == FF_STORE)
6606                         {
6607                                 prevent_bm = (f_info[cave[y][x].feat].subtype == STORE_BLACK);
6608                                 break;
6609                         }
6610                 }
6611         }
6612         for (i = 0; i < n; i++)
6613         {
6614                 if ((stores[i] == STORE_BLACK) && prevent_bm) stores[i] = stores[--n];
6615         }
6616         if (!n) return FALSE;
6617
6618         /* Allocate buildings array */
6619         C_MAKE(ugbldg, n, ugbldg_type);
6620
6621         /* If cannot build stores, abort */
6622         if (!precalc_ugarcade(town_hgt, town_wid, n))
6623         {
6624                 /* Free buildings array */
6625                 C_KILL(ugbldg, n, ugbldg_type);
6626                 return FALSE;
6627         }
6628
6629         /* Find and reserve some space in the dungeon.  Get center of room. */
6630         if (!find_space(&yval, &xval, town_hgt + 4, town_wid + 4))
6631         {
6632                 /* Free buildings array */
6633                 C_KILL(ugbldg, n, ugbldg_type);
6634                 return FALSE;
6635         }
6636
6637         /* Get top left corner */
6638         y1 = yval - (town_hgt / 2);
6639         x1 = xval - (town_wid / 2);
6640
6641         /* Generate new room */
6642         generate_room_floor(
6643                 y1 + town_hgt / 3, x1 + town_wid / 3,
6644                 y1 + town_hgt * 2 / 3, x1 + town_wid * 2 / 3, FALSE);
6645
6646         /* Build stores */
6647         build_stores(y1, x1, stores, n);
6648
6649         if (cheat_room) msg_print(_("地下街", "Underground Arcade"));
6650
6651         /* Free buildings array */
6652         C_KILL(ugbldg, n, ugbldg_type);
6653
6654         return TRUE;
6655 }
6656
6657
6658 /*!
6659  * @brief 与えられた部屋型IDに応じて部屋の生成処理分岐を行い結果を返す / Attempt to build a room of the given type at the given block
6660  * @param type 部屋型ID
6661  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
6662  * @return 部屋の精製に成功した場合 TRUE を返す。
6663  */
6664 static bool room_build(int typ)
6665 {
6666         /* Build a room */
6667         switch (typ)
6668         {
6669         /* Build an appropriate room */
6670         case ROOM_T_NORMAL:        return build_type1();
6671         case ROOM_T_OVERLAP:       return build_type2();
6672         case ROOM_T_CROSS:         return build_type3();
6673         case ROOM_T_INNER_FEAT:    return build_type4();
6674         case ROOM_T_NEST:          return build_type5();
6675         case ROOM_T_PIT:           return build_type6();
6676         case ROOM_T_LESSER_VAULT:  return build_type7();
6677         case ROOM_T_GREATER_VAULT: return build_type8();
6678         case ROOM_T_FRACAVE:       return build_type9();
6679         case ROOM_T_RANDOM_VAULT:  return build_type10();
6680         case ROOM_T_OVAL:          return build_type11();
6681         case ROOM_T_CRYPT:         return build_type12();
6682         case ROOM_T_TRAP_PIT:      return build_type13();
6683         case ROOM_T_TRAP:          return build_type14();
6684         case ROOM_T_GLASS:         return build_type15();
6685         case ROOM_T_ARCADE:        return build_type16();
6686         }
6687
6688         /* Paranoia */
6689         return FALSE;
6690 }
6691
6692 /*!
6693  * @brief 指定した部屋の生成確率を別の部屋に加算し、指定した部屋の生成率を0にする
6694  * @param dst 確率を移す先の部屋種ID
6695  * @param src 確率を与える元の部屋種ID
6696  */
6697 #define MOVE_PLIST(dst, src) (prob_list[dst] += prob_list[src], prob_list[src] = 0) 
6698
6699 /*!
6700  * @brief 部屋生成処理のメインルーチン(Sangbandを経由してOangbandからの実装を引用) / Generate rooms in dungeon.  Build bigger rooms at first. [from SAngband (originally from OAngband)]
6701  * @return 部屋生成に成功した場合 TRUE を返す。
6702  */
6703 bool generate_rooms(void)
6704 {
6705         int i;
6706         bool remain;
6707         int crowded = 0;
6708         int total_prob;
6709         int prob_list[ROOM_T_MAX];
6710         int rooms_built = 0;
6711         int area_size = 100 * (cur_hgt*cur_wid) / (MAX_HGT*MAX_WID);
6712         int level_index = MIN(10, div_round(dun_level, 10));
6713
6714         /* Number of each type of room on this level */
6715         s16b room_num[ROOM_T_MAX];
6716
6717         /* Limit number of rooms */
6718         int dun_rooms = DUN_ROOMS_MAX * area_size / 100;
6719
6720         /* Assume normal cave */
6721         room_info_type *room_info_ptr = room_info_normal;
6722
6723         /*
6724          * Initialize probability list.
6725          */
6726         for (i = 0; i < ROOM_T_MAX; i++)
6727         {
6728                 /* No rooms allowed above their minimum depth. */
6729                 if (dun_level < room_info_ptr[i].min_level)
6730                 {
6731                         prob_list[i] = 0;
6732                 }
6733                 else
6734                 {
6735                         prob_list[i] = room_info_ptr[i].prob[level_index];
6736                 }
6737         }
6738
6739         /*
6740          * XXX -- Various dungeon types and options.
6741          */
6742
6743         /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、かつ「常に通常でない部屋を生成する」フラグがONならば、GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
6744         if (ironman_rooms && !((d_info[dungeon_type].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
6745         {
6746                 for (i = 0; i < ROOM_T_MAX; i++)
6747                 {
6748                         if (i == ROOM_T_GREATER_VAULT) prob_list[i] = 1;
6749                         else prob_list[i] = 0;
6750                 }
6751         }
6752
6753         /*! @details ダンジョンにNO_VAULTフラグがあるならば、LESSER_VAULT / GREATER_VAULT/ RANDOM_VAULTを除外 / Forbidden vaults */
6754         else if (d_info[dungeon_type].flags1 & DF1_NO_VAULT)
6755         {
6756                 prob_list[ROOM_T_LESSER_VAULT] = 0;
6757                 prob_list[ROOM_T_GREATER_VAULT] = 0;
6758                 prob_list[ROOM_T_RANDOM_VAULT] = 0;
6759         }
6760
6761         /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
6762         if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
6763         {
6764                 MOVE_PLIST(ROOM_T_NORMAL, ROOM_T_FRACAVE);
6765                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_CRYPT);
6766                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_OVAL);
6767         }
6768
6769         /*! @details ダンジョンにCAVEフラグがある場合、NORMALの生成枠がFRACAVEに与えられる。/ CAVE dungeon (Orc cave etc.) */
6770         else if (d_info[dungeon_type].flags1 & DF1_CAVE)
6771         {
6772                 MOVE_PLIST(ROOM_T_FRACAVE, ROOM_T_NORMAL);
6773         }
6774
6775         /*! @details ダンジョンの基本地形が最初から渓谷かアリーナ型の場合 FRACAVE は生成から除外。 /  No caves when a (random) cavern exists: they look bad */
6776         else if (dun->cavern || dun->empty_level)
6777         {
6778                 prob_list[ROOM_T_FRACAVE] = 0;
6779         }
6780
6781         /*! @details ダンジョンに最初からGLASS_ROOMフラグがある場合、GLASS を生成から除外。/ Forbidden glass rooms */
6782         if (!(d_info[dungeon_type].flags1 & DF1_GLASS_ROOM))
6783         {
6784                 prob_list[ROOM_T_GLASS] = 0;
6785         }
6786
6787         /*! @details ARCADEは同フラグがダンジョンにないと生成されない。 / Forbidden glass rooms */
6788         if (!(d_info[dungeon_type].flags1 & DF1_ARCADE))
6789         {
6790                 prob_list[ROOM_T_ARCADE] = 0;
6791         }
6792
6793         /*
6794          * Initialize number of rooms,
6795          * And calcurate total probability.
6796          */
6797         for (total_prob = 0, i = 0; i < ROOM_T_MAX; i++)
6798         {
6799                 room_num[i] = 0;
6800                 total_prob += prob_list[i];
6801         }
6802
6803         /*
6804          * Prepare the number of rooms, of all types, we should build
6805          * on this level.
6806          */
6807         for (i = dun_rooms; i > 0; i--)
6808         {
6809                 int room_type;
6810                 int rand = randint0(total_prob);
6811
6812                 /* Get room_type randomly */
6813                 for (room_type = 0; room_type < ROOM_T_MAX; room_type++)
6814                 {
6815                         if (rand < prob_list[room_type]) break;
6816                         else rand -= prob_list[room_type];
6817                 }
6818
6819                 /* Paranoia */
6820                 if (room_type >= ROOM_T_MAX) room_type = ROOM_T_NORMAL;
6821
6822                 /* Increase the number of rooms of that type we should build. */
6823                 room_num[room_type]++;
6824
6825                 switch (room_type)
6826                 {
6827                 case ROOM_T_NEST:
6828                 case ROOM_T_PIT:
6829                 case ROOM_T_LESSER_VAULT:
6830                 case ROOM_T_TRAP_PIT:
6831                 case ROOM_T_GLASS:
6832                 case ROOM_T_ARCADE:
6833
6834                         /* Large room */
6835                         i -= 2;
6836                         break;
6837
6838                 case ROOM_T_GREATER_VAULT:
6839                 case ROOM_T_RANDOM_VAULT:
6840
6841                         /* Largest room */
6842                         i -= 3;
6843                         break;
6844                 }
6845         }
6846
6847         /*
6848          * Build each type of room one by one until we cannot build any more.
6849          * [from SAngband (originally from OAngband)]
6850          */
6851         while (TRUE)
6852         {
6853                 /* Assume no remaining rooms */
6854                 remain = FALSE;
6855
6856                 for (i = 0; i < ROOM_T_MAX; i++)
6857                 {
6858                         /* What type of room are we building now? */
6859                         int room_type = room_build_order[i];
6860
6861                         /* Go next if none available */
6862                         if (!room_num[room_type]) continue;
6863
6864                         /* Use up one unit */
6865                         room_num[room_type]--;
6866
6867                         /* Build the room. */
6868                         if (room_build(room_type))
6869                         {
6870                                 /* Increase the room built count. */
6871                                 rooms_built++;
6872
6873                                 /* Mark as there was some remaining rooms */
6874                                 remain = TRUE;
6875
6876                                 switch (room_type)
6877                                 {
6878                                 case ROOM_T_PIT:
6879                                 case ROOM_T_NEST:
6880                                 case ROOM_T_TRAP_PIT:
6881
6882                                         /* Avoid too many monsters */
6883                                         if (++crowded >= 2)
6884                                         {
6885                                                 room_num[ROOM_T_PIT] = 0;
6886                                                 room_num[ROOM_T_NEST] = 0;
6887                                                 room_num[ROOM_T_TRAP_PIT] = 0;
6888                                         }
6889                                         break;
6890
6891                                 case ROOM_T_ARCADE:
6892
6893                                         /* Avoid double-town */
6894                                         room_num[ROOM_T_ARCADE] = 0;
6895                                         break;
6896                                 }
6897                         }
6898                 }
6899
6900                 /* End loop if no room remain */
6901                 if (!remain) break;
6902         }
6903
6904         if (rooms_built < 2) return FALSE; /*! @details 部屋生成数が2未満の場合生成失敗を返す */
6905
6906         if (cheat_room)
6907         {
6908                 msg_format(_("部屋数: %d", "Number of Rooms: %d"), rooms_built);
6909         }
6910
6911         return TRUE;
6912 }