OSDN Git Service

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