OSDN Git Service

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