OSDN Git Service

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