OSDN Git Service

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