OSDN Git Service

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