OSDN Git Service

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