OSDN Git Service

04f4839e0344d5564b0b0c4d540d13b4f867060c
[hengband/hengband.git] / src / rooms.c
1 /*!
2  * @file rooms.c
3  * @brief ダンジョンフロアの部屋生成処理 / make rooms. Used by generate.c when creating dungeons.
4  * @date 2014/01/06
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  * @details
12  * Room building routines.\n
13  *\n
14  * Room types:\n
15  *   1 -- normal\n
16  *   2 -- overlapping\n
17  *   3 -- cross shaped\n
18  *   4 -- large room with features\n
19  *   5 -- monster nests\n
20  *   6 -- monster pits\n
21  *   7 -- simple vaults\n
22  *   8 -- greater vaults\n
23  *   9 -- fractal caves\n
24  *  10 -- random vaults\n
25  *  11 -- circular rooms\n
26  *  12 -- crypts\n
27  *  13 -- trapped monster pits\n
28  *  14 -- trapped room\n
29  *  15 -- glass room\n
30  *  16 -- underground arcade\n
31  *\n
32  * Some functions are used to determine if the given monster\n
33  * is appropriate for inclusion in a monster nest or monster pit or\n
34  * the given type.\n
35  *\n
36  * None of the pits/nests are allowed to include "unique" monsters.\n
37  */
38
39 #include "angband.h"
40 #include "generate.h"
41 #include "grid.h"
42 #include "rooms.h"
43
44 #include "rooms-normal.h"
45 #include "rooms-pitnest.h"
46 #include "rooms-vault.h"
47
48
49 /*!
50  * 各部屋タイプの生成比定義
51  *[from SAngband (originally from OAngband)]\n
52  *\n
53  * Table of values that control how many times each type of room will\n
54  * appear.  Each type of room has its own row, and each column\n
55  * corresponds to dungeon levels 0, 10, 20, and so on.  The final\n
56  * value is the minimum depth the room can appear at.  -LM-\n
57  *\n
58  * Level 101 and below use the values for level 100.\n
59  *\n
60  * Rooms with lots of monsters or loot may not be generated if the\n
61  * object or monster lists are already nearly full.  Rooms will not\n
62  * appear above their minimum depth.  Tiny levels will not have space\n
63  * for all the rooms you ask for.\n
64  */
65 static room_info_type room_info_normal[ROOM_T_MAX] =
66 {
67         /* Depth */
68         /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
69
70         {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
71         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
72         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
73         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*INNER_F  */
74         {{  0,  1,  1,  1,  2,  3,  5,  6,  8, 10, 13}, 10}, /*NEST     */
75         {{  0,  1,  1,  2,  3,  4,  6,  8, 10, 13, 16}, 10}, /*PIT      */
76         {{  0,  1,  1,  1,  2,  2,  3,  5,  6,  8, 10}, 10}, /*LESSER_V */
77         {{  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  4}, 20}, /*GREATER_V*/
78         {{  0,100,200,300,400,500,600,700,800,900,999}, 10}, /*FRACAVE  */
79         {{  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2}, 10}, /*RANDOM_V */
80         {{  0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40},  3}, /*OVAL     */
81         {{  1,  6, 12, 18, 24, 30, 36, 42, 48, 54, 60}, 10}, /*CRYPT    */
82         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP_PIT */
83         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
84         {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
85         {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
86 };
87
88
89 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
90 static byte room_build_order[ROOM_T_MAX] = {
91         ROOM_T_GREATER_VAULT,
92         ROOM_T_ARCADE,
93         ROOM_T_RANDOM_VAULT,
94         ROOM_T_LESSER_VAULT,
95         ROOM_T_TRAP_PIT,
96         ROOM_T_PIT,
97         ROOM_T_NEST,
98         ROOM_T_TRAP,
99         ROOM_T_GLASS,
100         ROOM_T_INNER_FEAT,
101         ROOM_T_OVAL,
102         ROOM_T_CRYPT,
103         ROOM_T_OVERLAP,
104         ROOM_T_CROSS,
105         ROOM_T_FRACAVE,
106         ROOM_T_NORMAL,
107 };
108
109 /*!
110  * @brief 鍵のかかったドアを配置する
111  * @param y 配置したいフロアのY座標
112  * @param x 配置したいフロアのX座標
113  * @return なし
114  */
115 void place_locked_door(int y, int x)
116 {
117         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
118         {
119                 place_floor_bold(y, x);
120         }
121         else
122         {
123                 set_cave_feat(y, x, feat_locked_door_random((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
124                 cave[y][x].info &= ~(CAVE_FLOOR);
125                 delete_monster(y, x);
126         }
127 }
128
129 /*!
130  * @brief 隠しドアを配置する
131  * @param y 配置したいフロアのY座標
132  * @param x 配置したいフロアのX座標
133  * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
134  * @return なし
135  */
136 void place_secret_door(int y, int x, int type)
137 {
138         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
139         {
140                 place_floor_bold(y, x);
141         }
142         else
143         {
144                 cave_type *c_ptr = &cave[y][x];
145
146                 if (type == DOOR_DEFAULT)
147                 {
148                         type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
149                                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
150                                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
151                 }
152
153                 /* Create secret door */
154                 place_closed_door(y, x, type);
155
156                 if (type != DOOR_CURTAIN)
157                 {
158                         /* Hide by inner wall because this is used in rooms only */
159                         c_ptr->mimic = feat_wall_inner;
160
161                         /* Floor type terrain cannot hide a door */
162                         if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
163                         {
164                                 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
165                                 {
166                                         c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
167                                 }
168                                 c_ptr->mimic = 0;
169                         }
170                 }
171
172                 c_ptr->info &= ~(CAVE_FLOOR);
173                 delete_monster(y, x);
174         }
175 }
176
177 /*!
178  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
179  * @param y0 配置したい中心のY座標
180  * @param x0 配置したい中心のX座標
181  * @details
182  * This funtion makes a very small room centred at (x0, y0)
183  * This is used in crypts, and random elemental vaults.
184  *
185  * Note - this should be used only on allocated regions
186  * within another room.
187  */
188 void build_small_room(int x0, int y0)
189 {
190         int x, y;
191
192         for (y = y0 - 1; y <= y0 + 1; y++)
193         {
194                 place_inner_bold(y, x0 - 1);
195                 place_inner_bold(y, x0 + 1);
196         }
197
198         for (x = x0 - 1; x <= x0 + 1; x++)
199         {
200                 place_inner_bold(y0 - 1, x);
201                 place_inner_bold(y0 + 1, x);
202         }
203
204         /* Place a secret door on one side */
205         switch (randint0(4))
206         {
207                 case 0: place_secret_door(y0, x0 - 1, DOOR_DEFAULT); break;
208                 case 1: place_secret_door(y0, x0 + 1, DOOR_DEFAULT); break;
209                 case 2: place_secret_door(y0 - 1, x0, DOOR_DEFAULT); break;
210                 case 3: place_secret_door(y0 + 1, x0, DOOR_DEFAULT); break;
211         }
212
213         /* Clear mimic type */
214         cave[y0][x0].mimic = 0;
215
216         /* Add inner open space */
217         place_floor_bold(y0, x0);
218 }
219
220 /*!
221  * @brief
222  * 指定範囲に通路が通っていることを確認した上で床で埋める
223  * This function tunnels around a room if it will cut off part of a cave system.
224  * @param x1 範囲の左端
225  * @param y1 範囲の上端
226  * @param x2 範囲の右端
227  * @param y2 範囲の下端
228  * @return なし
229  */
230 static void check_room_boundary(int x1, int y1, int x2, int y2)
231 {
232         int count, x, y;
233         bool old_is_floor, new_is_floor;
234
235
236         /* Initialize */
237         count = 0;
238
239         old_is_floor = get_is_floor(x1 - 1, y1);
240
241         /*
242          * Count the number of floor-wall boundaries around the room
243          * Note: diagonal squares are ignored since the player can move diagonally
244          * to bypass these if needed.
245          */
246
247         /* Above the top boundary */
248         for (x = x1; x <= x2; x++)
249         {
250                 new_is_floor = get_is_floor(x, y1 - 1);
251
252                 /* Increment counter if they are different */
253                 if (new_is_floor != old_is_floor) count++;
254
255                 old_is_floor = new_is_floor;
256         }
257
258         /* Right boundary */
259         for (y = y1; y <= y2; y++)
260         {
261                 new_is_floor = get_is_floor(x2 + 1, y);
262
263                 /* increment counter if they are different */
264                 if (new_is_floor != old_is_floor) count++;
265
266                 old_is_floor = new_is_floor;
267         }
268
269         /* Bottom boundary */
270         for (x = x2; x >= x1; x--)
271         {
272                 new_is_floor = get_is_floor(x, y2 + 1);
273
274                 /* increment counter if they are different */
275                 if (new_is_floor != old_is_floor) count++;
276
277                 old_is_floor = new_is_floor;
278         }
279
280         /* Left boundary */
281         for (y = y2; y >= y1; y--)
282         {
283                 new_is_floor = get_is_floor(x1 - 1, y);
284
285                 /* increment counter if they are different */
286                 if (new_is_floor != old_is_floor) count++;
287
288                 old_is_floor = new_is_floor;
289         }
290
291         /* If all the same, or only one connection exit. */
292         if (count <= 2) return;
293
294
295         /* Tunnel around the room so to prevent problems with caves */
296         for (y = y1; y <= y2; y++)
297         {
298                 for (x = x1; x <= x2; x++)
299                 {
300                         set_floor(x, y);
301                 }
302         }
303 }
304
305
306 /*!
307  * @brief
308  * find_space()の予備処理として部屋の生成が可能かを判定する /
309  * Helper function for find_space(). Is this a good location?
310  * @param blocks_high 範囲の高さ
311  * @param blocks_wide 範囲の幅
312  * @param block_y 範囲の上端
313  * @param block_x 範囲の左端
314  * @return なし
315  */
316 static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int block_x)
317 {
318         int by1, bx1, by2, bx2, by, bx;
319
320         /* Itty-bitty rooms must shift about within their rectangle */
321         if (blocks_wide < 3)
322         {
323                 if ((blocks_wide == 2) && (block_x % 3) == 2)
324                         return FALSE;
325         }
326
327         /* Rooms with width divisible by 3 must be fitted to a rectangle. */
328         else if ((blocks_wide % 3) == 0)
329         {
330                 /* Must be aligned to the left edge of a 11x33 rectangle. */
331                 if ((block_x % 3) != 0)
332                         return FALSE;
333         }
334
335         /*
336          * Big rooms that do not have a width divisible by 3 must be
337          * aligned towards the edge of the dungeon closest to them.
338          */
339         else
340         {
341                 /* Shift towards left edge of dungeon. */
342                 if (block_x + (blocks_wide / 2) <= dun->col_rooms / 2)
343                 {
344                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
345                                 return FALSE;
346                         if ((block_x % 3) == 1)
347                                 return FALSE;
348                 }
349
350                 /* Shift toward right edge of dungeon. */
351                 else
352                 {
353                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
354                                 return FALSE;
355                         if ((block_x % 3) == 1)
356                                 return FALSE;
357                 }
358         }
359
360         /* Extract blocks */
361         by1 = block_y + 0;
362         bx1 = block_x + 0;
363         by2 = block_y + blocks_high;
364         bx2 = block_x + blocks_wide;
365
366         /* Never run off the screen */
367         if ((by1 < 0) || (by2 > dun->row_rooms)) return FALSE;
368         if ((bx1 < 0) || (bx2 > dun->col_rooms)) return FALSE;
369         
370         /* Verify available space */
371         for (by = by1; by < by2; by++)
372         {
373                 for (bx = bx1; bx < bx2; bx++)
374                 {
375                         if (dun->room_map[by][bx])
376                         {
377                                 return FALSE;
378                         }
379                 }
380         }
381
382         /* This location is okay */
383         return TRUE;
384 }
385
386
387 /*!
388  * @brief 部屋生成が可能なスペースを確保する / Find a good spot for the next room.  -LM-
389  * @param y 部屋の生成が可能な中心Y座標を返す参照ポインタ
390  * @param x 部屋の生成が可能な中心X座標を返す参照ポインタ
391  * @param height 確保したい領域の高さ
392  * @param width 確保したい領域の幅
393  * @return 所定の範囲が確保できた場合TRUEを返す
394  * @details
395  * Find and allocate a free space in the dungeon large enough to hold\n
396  * the room calling this function.\n
397  *\n
398  * We allocate space in 11x11 blocks, but want to make sure that rooms\n
399  * align neatly on the standard screen.  Therefore, we make them use\n
400  * blocks in few 11x33 rectangles as possible.\n
401  *\n
402  * Be careful to include the edges of the room in height and width!\n
403  *\n
404  * Return TRUE and values for the center of the room if all went well.\n
405  * Otherwise, return FALSE.\n
406  */
407 bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
408 {
409         int candidates, pick;
410         int by, bx, by1, bx1, by2, bx2;
411         int block_y = 0, block_x = 0;
412
413
414         /* Find out how many blocks we need. */
415         int blocks_high = 1 + ((height - 1) / BLOCK_HGT);
416         int blocks_wide = 1 + ((width - 1) / BLOCK_WID);
417
418         /* There are no way to allocate such huge space */
419         if (dun->row_rooms < blocks_high) return FALSE;
420         if (dun->col_rooms < blocks_wide) return FALSE;
421
422         /* Initiallize */
423         candidates = 0;
424
425         /* Count the number of valid places */
426         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
427         {
428                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
429                 {
430                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
431                         {
432                                 /* Find a valid place */
433                                 candidates++;
434                         }
435                 }
436         }
437
438         /* No place! */
439         if (!candidates)
440         {
441                 return FALSE;
442         }
443
444         /* Normal dungeon */
445         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
446         {
447                 /* Choose a random one */
448                 pick = randint1(candidates);
449         }
450
451         /* NO_CAVE dungeon (Castle) */
452         else
453         {
454                 /* Always choose the center one */
455                 pick = candidates/2 + 1;
456         }
457
458         /* Pick up the choosen location */
459         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
460         {
461                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
462                 {
463                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
464                         {
465                                 pick--;
466
467                                 /* This one is picked? */
468                                 if (!pick) break;
469                         }
470                 }
471
472                 if (!pick) break;
473         }
474
475         /* Extract blocks */
476         by1 = block_y + 0;
477         bx1 = block_x + 0;
478         by2 = block_y + blocks_high;
479         bx2 = block_x + blocks_wide;
480
481         /*
482          * It is *extremely* important that the following calculation
483          * be *exactly* correct to prevent memory errors
484          */
485
486         /* Acquire the location of the room */
487         (*y) = ((by1 + by2) * BLOCK_HGT) / 2;
488         (*x) = ((bx1 + bx2) * BLOCK_WID) / 2;
489
490         /* Save the room location */
491         if (dun->cent_n < CENT_MAX)
492         {
493                 dun->cent[dun->cent_n].y = (byte_hack)*y;
494                 dun->cent[dun->cent_n].x = (byte_hack)*x;
495                 dun->cent_n++;
496         }
497
498         /* Reserve some blocks. */
499         for (by = by1; by < by2; by++)
500         {
501                 for (bx = bx1; bx < bx2; bx++)
502                 {
503                         dun->room_map[by][bx] = TRUE;
504                 }
505         }
506
507
508         /*
509          * Hack- See if room will cut off a cavern.
510          *
511          * If so, fix by tunneling outside the room in such a
512          * way as to connect the caves.
513          */
514         check_room_boundary(*x - width / 2 - 1, *y - height / 2 - 1, *x + (width - 1) / 2 + 1, *y + (height - 1) / 2 + 1);
515
516
517         /* Success. */
518         return TRUE;
519 }
520
521
522
523
524
525
526
527 /*
528  * Structure to hold all "fill" data
529  */
530
531 typedef struct fill_data_type fill_data_type;
532
533 struct fill_data_type
534 {
535         /* area size */
536         int xmin;
537         int ymin;
538         int xmax;
539         int ymax;
540
541         /* cutoffs */
542         int c1;
543         int c2;
544         int c3;
545
546         /* features to fill with */
547         int feat1;
548         int feat2;
549         int feat3;
550
551         int info1;
552         int info2;
553         int info3;
554
555         /* number of filled squares */
556         int amount;
557 };
558
559 static fill_data_type fill_data;
560
561
562 /* Store routine for the fractal cave generator */
563 /* this routine probably should be an inline function or a macro. */
564 static void store_height(int x, int y, int val)
565 {
566         /* if on boundary set val > cutoff so walls are not as square */
567         if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
568              (x == fill_data.xmax) || (y == fill_data.ymax)) &&
569             (val <= fill_data.c1)) val = fill_data.c1 + 1;
570
571         /* store the value in height-map format */
572         cave[y][x].feat = (s16b)val;
573
574         return;
575 }
576
577
578 /*
579 * Explanation of the plasma fractal algorithm:
580 *
581 * A grid of points is created with the properties of a 'height-map'
582 * This is done by making the corners of the grid have a random value.
583 * The grid is then subdivided into one with twice the resolution.
584 * The new points midway between two 'known' points can be calculated
585 * by taking the average value of the 'known' ones and randomly adding
586 * or subtracting an amount proportional to the distance between those
587 * points.  The final 'middle' points of the grid are then calculated
588 * by averaging all four of the originally 'known' corner points.  An
589 * random amount is added or subtracted from this to get a value of the
590 * height at that point.  The scaling factor here is adjusted to the
591 * slightly larger distance diagonally as compared to orthogonally.
592 *
593 * This is then repeated recursively to fill an entire 'height-map'
594 * A rectangular map is done the same way, except there are different
595 * scaling factors along the x and y directions.
596 *
597 * A hack to change the amount of correlation between points is done using
598 * the grd variable.  If the current step size is greater than grd then
599 * the point will be random, otherwise it will be calculated by the
600 * above algorithm.  This makes a maximum distance at which two points on
601 * the height map can affect each other.
602 *
603 * How fractal caves are made:
604 *
605 * When the map is complete, a cut-off value is used to create a cave.
606 * Heights below this value are "floor", and heights above are "wall".
607 * This also can be used to create lakes, by adding more height levels
608 * representing shallow and deep water/ lava etc.
609 *
610 * The grd variable affects the width of passages.
611 * The roug variable affects the roughness of those passages
612 *
613 * The tricky part is making sure the created cave is connected.  This
614 * is done by 'filling' from the inside and only keeping the 'filled'
615 * floor.  Walls bounding the 'filled' floor are also kept.  Everything
616 * else is converted to the normal _extra_.
617  */
618
619
620 /*
621  *  Note that this uses the cave.feat array in a very hackish way
622  *  the values are first set to zero, and then each array location
623  *  is used as a "heightmap"
624  *  The heightmap then needs to be converted back into the "feat" format.
625  *
626  *  grd=level at which fractal turns on.  smaller gives more mazelike caves
627  *  roug=roughness level.  16=normal.  higher values make things more convoluted
628  *    small values are good for smooth walls.
629  *  size=length of the side of the square cave system.
630  */
631 static void generate_hmap(int y0, int x0, int xsiz, int ysiz, int grd, int roug, int cutoff)
632 {
633         int xhsize, yhsize, xsize, ysize, maxsize;
634
635         /*
636          * fixed point variables- these are stored as 256 x normal value
637          * this gives 8 binary places of fractional part + 8 places of normal part
638          */
639
640         u16b xstep, xhstep, ystep, yhstep;
641         u16b xstep2, xhstep2, ystep2, yhstep2;
642         u16b i, j, ii, jj, diagsize, xxsize, yysize;
643         
644         /* Cache for speed */
645         u16b xm, xp, ym, yp;
646
647         /* redefine size so can change the value if out of range */
648         xsize = xsiz;
649         ysize = ysiz;
650
651         /* Paranoia about size of the system of caves */
652         if (xsize > 254) xsize = 254;
653         if (xsize < 4) xsize = 4;
654         if (ysize > 254) ysize = 254;
655         if (ysize < 4) ysize = 4;
656
657         /* get offsets to middle of array */
658         xhsize = xsize / 2;
659         yhsize = ysize / 2;
660
661         /* fix rounding problem */
662         xsize = xhsize * 2;
663         ysize = yhsize * 2;
664
665         /* get limits of region */
666         fill_data.xmin = x0 - xhsize;
667         fill_data.ymin = y0 - yhsize;
668         fill_data.xmax = x0 + xhsize;
669         fill_data.ymax = y0 + yhsize;
670
671         /* Store cutoff in global for quick access */
672         fill_data.c1 = cutoff;
673
674         /*
675         * Scale factor for middle points:
676         * About sqrt(2) * 256 - correct for a square lattice
677         * approximately correct for everything else.
678          */
679         diagsize = 362;
680
681         /* maximum of xsize and ysize */
682         maxsize = (xsize > ysize) ? xsize : ysize;
683
684         /* Clear the section */
685         for (i = 0; i <= xsize; i++)
686         {
687                 for (j = 0; j <= ysize; j++)
688                 {
689                         /* -1 is a flag for "not done yet" */
690                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
691                         /* Clear icky flag because may be redoing the cave */
692                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
693                 }
694         }
695
696         /* Boundaries are walls */
697         cave[fill_data.ymin][fill_data.xmin].feat = (s16b)maxsize;
698         cave[fill_data.ymax][fill_data.xmin].feat = (s16b)maxsize;
699         cave[fill_data.ymin][fill_data.xmax].feat = (s16b)maxsize;
700         cave[fill_data.ymax][fill_data.xmax].feat = (s16b)maxsize;
701
702         /* Set the middle square to be an open area. */
703         cave[y0][x0].feat = 0;
704
705         /* Initialize the step sizes */
706         xstep = xhstep = xsize * 256;
707         ystep = yhstep = ysize * 256;
708         xxsize = xsize * 256;
709         yysize = ysize * 256;
710
711         /*
712          * Fill in the rectangle with fractal height data -
713          * like the 'plasma fractal' in fractint.
714          */
715         while ((xhstep > 256) || (yhstep > 256))
716         {
717                 /* Halve the step sizes */
718                 xstep = xhstep;
719                 xhstep /= 2;
720                 ystep = yhstep;
721                 yhstep /= 2;
722
723                 /* cache well used values */
724                 xstep2 = xstep / 256;
725                 ystep2 = ystep / 256;
726
727                 xhstep2 = xhstep / 256;
728                 yhstep2 = yhstep / 256;
729
730                 /* middle top to bottom. */
731                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
732                 {
733                         for (j = 0; j <= yysize; j += ystep)
734                         {
735                                 /* cache often used values */
736                                 ii = i / 256 + fill_data.xmin;
737                                 jj = j / 256 + fill_data.ymin;
738
739                                 /* Test square */
740                                 if (cave[jj][ii].feat == -1)
741                                 {
742                                         if (xhstep2 > grd)
743                                         {
744                                                 /* If greater than 'grid' level then is random */
745                                                 store_height(ii, jj, randint1(maxsize));
746                                         }
747                                         else
748                                         {
749                                                 /* Average of left and right points +random bit */
750                                                 store_height(ii, jj,
751                                                         (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
752                                                          + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
753                                                          + (randint1(xstep2) - xhstep2) * roug / 16);
754                                         }
755                                 }
756                         }
757                 }
758
759
760                 /* middle left to right. */
761                 for (j = yhstep; j <= yysize - yhstep; j += ystep)
762                 {
763                         for (i = 0; i <= xxsize; i += xstep)
764                         {
765                                 /* cache often used values */
766                                 ii = i / 256 + fill_data.xmin;
767                                 jj = j / 256 + fill_data.ymin;
768
769                                 /* Test square */
770                                 if (cave[jj][ii].feat == -1)
771                                 {
772                                         if (xhstep2 > grd)
773                                         {
774                                                 /* If greater than 'grid' level then is random */
775                                                 store_height(ii, jj, randint1(maxsize));
776                                         }
777                                         else
778                                         {
779                                                 /* Average of up and down points +random bit */
780                                                 store_height(ii, jj,
781                                                         (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
782                                                         + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
783                                                         + (randint1(ystep2) - yhstep2) * roug / 16);
784                                         }
785                                 }
786                         }
787                 }
788
789                 /* center. */
790                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
791                 {
792                         for (j = yhstep; j <= yysize - yhstep; j += ystep)
793                         {
794                                 /* cache often used values */
795                                 ii = i / 256 + fill_data.xmin;
796                                 jj = j / 256 + fill_data.ymin;
797
798                                 /* Test square */
799                                 if (cave[jj][ii].feat == -1)
800                                 {
801                                         if (xhstep2 > grd)
802                                         {
803                                                 /* If greater than 'grid' level then is random */
804                                                 store_height(ii, jj, randint1(maxsize));
805                                         }
806                                         else
807                                         {
808                                                 /* Cache reused values. */
809                                                 xm = fill_data.xmin + (i - xhstep) / 256;
810                                                 xp = fill_data.xmin + (i + xhstep) / 256;
811                                                 ym = fill_data.ymin + (j - yhstep) / 256;
812                                                 yp = fill_data.ymin + (j + yhstep) / 256;
813
814                                                 /* 
815                                                  * Average over all four corners + scale by diagsize to
816                                                  * reduce the effect of the square grid on the shape of the fractal
817                                                  */
818                                                 store_height(ii, jj,
819                                                         (cave[ym][xm].feat + cave[yp][xm].feat
820                                                         + cave[ym][xp].feat + cave[yp][xp].feat) / 4
821                                                         + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
822                                         }
823                                 }
824                         }
825                 }
826         }
827 }
828
829
830 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)
831 {
832         /*
833          * function used to convert from height-map back to the
834          *  normal angband cave format
835          */
836         if (cave[y][x].info & CAVE_ICKY)
837         {
838                 /* already done */
839                 return FALSE;
840         }
841         else
842         {
843                 /* Show that have looked at this square */
844                 cave[y][x].info|= (CAVE_ICKY);
845
846                 /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
847                 if (cave[y][x].feat <= c1)
848                 {
849                         /* 25% of the time use the other tile : it looks better this way */
850                         if (randint1(100) < 75)
851                         {
852                                 cave[y][x].feat = (s16b)feat1;
853                                 cave[y][x].info &= ~(CAVE_MASK);
854                                 cave[y][x].info |= info1;
855                                 return TRUE;
856                         }
857                         else
858                         {
859                                 cave[y][x].feat = (s16b)feat2;
860                                 cave[y][x].info &= ~(CAVE_MASK);
861                                 cave[y][x].info |= info2;
862                                 return TRUE;
863                         }
864                 }
865                 else if (cave[y][x].feat <= c2)
866                 {
867                         /* 25% of the time use the other tile : it looks better this way */
868                         if (randint1(100) < 75)
869                         {
870                                 cave[y][x].feat = (s16b)feat2;
871                                 cave[y][x].info &= ~(CAVE_MASK);
872                                 cave[y][x].info |= info2;
873                                 return TRUE;
874                         }
875                         else
876                         {
877                                 cave[y][x].feat = (s16b)feat1;
878                                 cave[y][x].info &= ~(CAVE_MASK);
879                                 cave[y][x].info |= info1;
880                                 return TRUE;
881                         }
882                 }
883                 else if (cave[y][x].feat <= c3)
884                 {
885                         cave[y][x].feat = (s16b)feat3;
886                         cave[y][x].info &= ~(CAVE_MASK);
887                         cave[y][x].info |= info3;
888                         return TRUE;
889                 }
890                 /* if greater than cutoff then is a wall */
891                 else
892                 {
893                         place_outer_bold(y, x);
894                         return FALSE;
895                 }
896         }
897 }
898
899
900
901
902 /*
903  * Quick and nasty fill routine used to find the connected region
904  * of floor in the middle of the cave
905  */
906 static void cave_fill(POSITION y, POSITION x)
907 {
908         int i, j, d;
909         int ty, tx;
910
911         int flow_tail = 1;
912         int flow_head = 0;
913
914
915         /*** Start Grid ***/
916
917         /* Enqueue that entry */
918         temp_y[0] = y;
919         temp_x[0] = x;
920
921
922         /* Now process the queue */
923         while (flow_head != flow_tail)
924         {
925                 /* Extract the next entry */
926                 ty = temp_y[flow_head];
927                 tx = temp_x[flow_head];
928
929                 /* Forget that entry */
930                 if (++flow_head == TEMP_MAX) flow_head = 0;
931
932                 /* Add the "children" */
933                 for (d = 0; d < 8; d++)
934                 {
935                         int old_head = flow_tail;
936
937                         /* Child location */
938                         j = ty + ddy_ddd[d];
939                         i = tx + ddx_ddd[d];
940
941                         /* Paranoia Don't leave the cave */
942                         if (!in_bounds(j, i))
943                         {
944                                 /* affect boundary */
945                                 cave[j][i].info |= CAVE_ICKY;
946 /*                              return; */
947                         }
948
949                         /* If within bounds */
950                         else if ((i > fill_data.xmin) && (i < fill_data.xmax)
951                                 && (j > fill_data.ymin) && (j < fill_data.ymax))
952                         {
953                                 /* If not a wall or floor done before */
954                                 if (hack_isnt_wall(j, i,
955                                         fill_data.c1, fill_data.c2, fill_data.c3,
956                                         fill_data.feat1, fill_data.feat2, fill_data.feat3,
957                                         fill_data.info1, fill_data.info2, fill_data.info3))
958                                 {
959                                         /* Enqueue that entry */
960                                         temp_y[flow_tail] = (byte_hack)j;
961                                         temp_x[flow_tail] = (byte_hack)i;
962
963                                         /* Advance the queue */
964                                         if (++flow_tail == TEMP_MAX) flow_tail = 0;
965
966                                         /* Hack -- Overflow by forgetting new entry */
967                                         if (flow_tail == flow_head)
968                                         {
969                                                 flow_tail = old_head;
970                                         }
971                                         else
972                                         {
973                                                 /* keep tally of size of cave system */
974                                                 (fill_data.amount)++;
975                                         }
976                                 }
977                         }
978                         else
979                         {
980                                 /* affect boundary */
981                                 cave[j][i].info |= CAVE_ICKY;
982                         }
983                 }
984         }
985 }
986
987
988 static bool generate_fracave(int y0, int x0, int xsize, int ysize, int cutoff, bool light, bool room)
989 {
990         int x, y, i, xhsize, yhsize;
991
992         /* offsets to middle from corner */
993         xhsize = xsize / 2;
994         yhsize = ysize / 2;
995
996
997         /*
998          * select region connected to center of cave system
999          * this gets rid of alot of isolated one-sqaures that
1000          * can make teleport traps instadeaths...
1001          */
1002
1003         /* cutoffs */
1004         fill_data.c1 = cutoff;
1005         fill_data.c2 = 0;
1006         fill_data.c3 = 0;
1007
1008         /* features to fill with */
1009         fill_data.feat1 = floor_type[randint0(100)];
1010         fill_data.feat2 = floor_type[randint0(100)];
1011         fill_data.feat3 = floor_type[randint0(100)];
1012
1013         fill_data.info1 = CAVE_FLOOR;
1014         fill_data.info2 = CAVE_FLOOR;
1015         fill_data.info3 = CAVE_FLOOR;
1016
1017         /* number of filled squares */
1018         fill_data.amount = 0;
1019
1020         cave_fill((byte)y0, (byte)x0);
1021
1022         /* if tally too small, try again */
1023         if (fill_data.amount < 10)
1024         {
1025                 /* too small - clear area and try again later */
1026                 for (x = 0; x <= xsize; ++x)
1027                 {
1028                         for (y = 0; y <= ysize; ++y)
1029                         {
1030                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1031                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1032                         }
1033                 }
1034                 return FALSE;
1035         }
1036
1037         /*
1038          * Do boundarys-check to see if they are next to a filled region
1039          * If not then they are set to normal granite
1040          * If so then they are marked as room walls.
1041          */
1042         for (i = 0; i <= xsize; ++i)
1043         {
1044                 /* top boundary */
1045                 if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
1046                 {
1047                         /* Next to a 'filled' region? - set to be room walls */
1048                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1049                         if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
1050                         cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
1051                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1052                 }
1053                 else
1054                 {
1055                         /* set to be normal granite */
1056                         place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1057                 }
1058
1059                 /* bottom boundary */
1060                 if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
1061                 {
1062                         /* Next to a 'filled' region? - set to be room walls */
1063                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1064                         if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
1065                         cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
1066                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1067                 }
1068                 else
1069                 {
1070                         /* set to be normal granite */
1071                         place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1072                 }
1073
1074                 /* clear the icky flag-don't need it any more */
1075                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1076                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1077         }
1078
1079         /* Do the left and right boundaries minus the corners (done above) */
1080         for (i = 1; i < ysize; ++i)
1081         {
1082                 /* left boundary */
1083                 if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
1084                 {
1085                         /* room boundary */
1086                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1087                         if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
1088                         cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
1089                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1090                 }
1091                 else
1092                 {
1093                         /* outside room */
1094                         place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1095                 }
1096                 /* right boundary */
1097                 if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
1098                 {
1099                         /* room boundary */
1100                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1101                         if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
1102                         cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
1103                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1104                 }
1105                 else
1106                 {
1107                         /* outside room */
1108                         place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1109                 }
1110
1111                 /* clear icky flag -done with it */
1112                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1113                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1114         }
1115
1116
1117         /* Do the rest: convert back to the normal format */
1118         for (x = 1; x < xsize; ++x)
1119         {
1120                 for (y = 1; y < ysize; ++y)
1121                 {
1122                         if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1123                             (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1124                         {
1125                                 /* Clear the icky flag in the filled region */
1126                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
1127
1128                                 /* Set appropriate flags */
1129                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1130                                 if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1131                         }
1132                         else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1133                                  (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1134                         {
1135                                 /* Walls */
1136                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1137                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1138                                 if (room)
1139                                 {
1140                                         cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1141                                 }
1142                                 else
1143                                 {
1144
1145                                         place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1146                                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
1147                                 }
1148                         }
1149                         else
1150                         {
1151                                 /* Clear the unconnected regions */
1152                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1153                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1154                         }
1155                 }
1156         }
1157
1158         /*
1159          * XXX XXX XXX There is a slight problem when tunnels pierce the caves:
1160          * Extra doors appear inside the system.  (Its not very noticeable though.)
1161          * This can be removed by "filling" from the outside in.  This allows a separation
1162          * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
1163          * The extra effort for what seems to be only a minor thing (even non-existant if you
1164          * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
1165          * worth it.
1166          */
1167
1168         return TRUE;
1169 }
1170
1171
1172 /*!
1173  * @brief タイプ9の部屋…フラクタルカーブによる洞窟生成 / Type 9 -- Driver routine to create fractal cave system
1174  * @return なし
1175  */
1176 static bool build_type9(void)
1177 {
1178         int grd, roug, cutoff;
1179         POSITION xsize, ysize, y0, x0;
1180
1181         bool done, light, room;
1182
1183         /* get size: note 'Evenness'*/
1184         xsize = randint1(22) * 2 + 6;
1185         ysize = randint1(15) * 2 + 6;
1186
1187         /* Find and reserve some space in the dungeon.  Get center of room. */
1188         if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
1189         {
1190                 /* Limit to the minimum room size, and retry */
1191                 xsize = 8;
1192                 ysize = 8;
1193
1194                 /* Find and reserve some space in the dungeon.  Get center of room. */
1195                 if (!find_space(&y0, &x0, ysize + 1, xsize + 1))
1196                 {
1197                         /*
1198                          * Still no space?!
1199                          * Try normal room
1200                          */
1201                         return build_type1();
1202                 }
1203         }
1204
1205         light = done = FALSE;
1206         room = TRUE;
1207
1208         if ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
1209
1210         while (!done)
1211         {
1212                 /* Note: size must be even or there are rounding problems
1213                 * This causes the tunnels not to connect properly to the room */
1214
1215                 /* testing values for these parameters feel free to adjust */
1216                 grd = 1 << (randint0(4));
1217
1218                 /* want average of about 16 */
1219                 roug = randint1(8) * randint1(4);
1220
1221                 /* about size/2 */
1222                 cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
1223                          randint1(xsize / 4) + randint1(ysize / 4);
1224
1225                 /* make it */
1226                 generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
1227
1228                 /* Convert to normal format + clean up */
1229                 done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
1230         }
1231
1232         return TRUE;
1233 }
1234
1235 #ifdef ALLOW_CAVERNS_AND_LAKES
1236 /*
1237  * Builds a cave system in the center of the dungeon.
1238  */
1239 void build_cavern(void)
1240 {
1241         int grd, roug, cutoff, xsize, ysize, x0, y0;
1242         bool done, light;
1243
1244         light = done = FALSE;
1245         if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
1246
1247         /* Make a cave the size of the dungeon */
1248         xsize = cur_wid - 1;
1249         ysize = cur_hgt - 1;
1250         x0 = xsize / 2;
1251         y0 = ysize / 2;
1252
1253         /* Paranoia: make size even */
1254         xsize = x0 * 2;
1255         ysize = y0 * 2;
1256
1257         while (!done)
1258         {
1259                 /* testing values for these parameters: feel free to adjust */
1260                 grd = randint1(4) + 4;
1261
1262                 /* want average of about 16 */
1263                 roug = randint1(8) * randint1(4);
1264
1265                 /* about size/2 */
1266                 cutoff = xsize / 2;
1267
1268                  /* make it */
1269                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
1270
1271                 /* Convert to normal format+ clean up */
1272                 done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
1273         }
1274 }
1275
1276 static bool generate_lake(int y0, int x0, int xsize, int ysize, int c1, int c2, int c3, int type)
1277 {
1278         int x, y, i, xhsize, yhsize;
1279         int feat1, feat2, feat3;
1280
1281         /* offsets to middle from corner */
1282         xhsize = xsize / 2;
1283         yhsize = ysize / 2;
1284
1285         /* Get features based on type */
1286         switch (type)
1287         {
1288         case LAKE_T_LAVA: /* Lava */
1289                 feat1 = feat_deep_lava;
1290                 feat2 = feat_shallow_lava;
1291                 feat3 = floor_type[randint0(100)];
1292                 break;
1293         case LAKE_T_WATER: /* Water */
1294                 feat1 = feat_deep_water;
1295                 feat2 = feat_shallow_water;
1296                 feat3 = floor_type[randint0(100)];
1297                 break;
1298         case LAKE_T_CAVE: /* Collapsed cave */
1299                 feat1 = floor_type[randint0(100)];
1300                 feat2 = floor_type[randint0(100)];
1301                 feat3 = feat_rubble;
1302                 break;
1303         case LAKE_T_EARTH_VAULT: /* Earth vault */
1304                 feat1 = feat_rubble;
1305                 feat2 = floor_type[randint0(100)];
1306                 feat3 = feat_rubble;
1307                 break;
1308         case LAKE_T_AIR_VAULT: /* Air vault */
1309                 feat1 = feat_grass;
1310                 feat2 = feat_tree;
1311                 feat3 = feat_grass;
1312                 break;
1313         case LAKE_T_WATER_VAULT: /* Water vault */
1314                 feat1 = feat_shallow_water;
1315                 feat2 = feat_deep_water;
1316                 feat3 = feat_shallow_water;
1317                 break;
1318         case LAKE_T_FIRE_VAULT: /* Fire Vault */
1319                 feat1 = feat_shallow_lava;
1320                 feat2 = feat_deep_lava;
1321                 feat3 = feat_shallow_lava;
1322                 break;
1323
1324         /* Paranoia */
1325         default: return FALSE;
1326         }
1327
1328         /*
1329          * select region connected to center of cave system
1330          * this gets rid of alot of isolated one-sqaures that
1331          * can make teleport traps instadeaths...
1332          */
1333
1334         /* cutoffs */
1335         fill_data.c1 = c1;
1336         fill_data.c2 = c2;
1337         fill_data.c3 = c3;
1338
1339         /* features to fill with */
1340         fill_data.feat1 = feat1;
1341         fill_data.feat2 = feat2;
1342         fill_data.feat3 = feat3;
1343
1344         fill_data.info1 = 0;
1345         fill_data.info2 = 0;
1346         fill_data.info3 = 0;
1347
1348         /* number of filled squares */
1349         fill_data.amount = 0;
1350
1351         /* select region connected to center of cave system
1352         * this gets rid of alot of isolated one-sqaures that
1353         * can make teleport traps instadeaths... */
1354         cave_fill((byte)y0, (byte)x0);
1355
1356         /* if tally too small, try again */
1357         if (fill_data.amount < 10)
1358         {
1359                 /* too small -clear area and try again later */
1360                 for (x = 0; x <= xsize; ++x)
1361                 {
1362                         for (y = 0; y <= ysize; ++y)
1363                         {
1364                                 place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
1365                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1366                         }
1367                 }
1368                 return FALSE;
1369         }
1370
1371         /* Do boundarys- set to normal granite */
1372         for (i = 0; i <= xsize; ++i)
1373         {
1374                 place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1375                 place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1376
1377                 /* clear the icky flag-don't need it any more */
1378                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1379                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1380         }
1381
1382         /* Do the left and right boundaries minus the corners (done above) */
1383
1384         for (i = 1; i < ysize; ++i)
1385         {
1386                 place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1387                 place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1388
1389                 /* clear icky flag -done with it */
1390                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1391                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1392         }
1393
1394
1395         /* Do the rest: convert back to the normal format */
1396         for (x = 1; x < xsize; ++x)
1397         {
1398                 for (y = 1; y < ysize; ++y)
1399                 {
1400                         /* Fill unconnected regions with granite */
1401                         if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
1402                                 is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
1403                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1404
1405                         /* turn off icky flag (no longer needed.) */
1406                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1407
1408                         /* Light lava */
1409                         if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
1410                         {
1411                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
1412                         }
1413                 }
1414         }
1415
1416         return TRUE;
1417 }
1418
1419
1420 /*
1421  * makes a lake/collapsed cave system in the center of the dungeon
1422  */
1423 void build_lake(int type)
1424 {
1425         int grd, roug, xsize, ysize, x0, y0;
1426         bool done = FALSE;
1427         int c1, c2, c3;
1428
1429         /* paranoia - exit if lake type out of range. */
1430         if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
1431         {
1432                 msg_format("Invalid lake type (%d)", type);
1433                 return;
1434         }
1435
1436         /* Make the size of the dungeon */
1437         xsize = cur_wid - 1;
1438         ysize = cur_hgt - 1;
1439         x0 = xsize / 2;
1440         y0 = ysize / 2;
1441
1442         /* Paranoia: make size even */
1443         xsize = x0 * 2;
1444         ysize = y0 * 2;
1445
1446         while (!done)
1447         {
1448                 /* testing values for these parameters: feel free to adjust */
1449                 grd = randint1(3) + 4;
1450
1451                 /* want average of about 16 */
1452                 roug = randint1(8) * randint1(4);
1453
1454                 /* Make up size of various componants */
1455                 /* Floor */
1456                 c3 = 3 * xsize / 4;
1457
1458                 /* Deep water/lava */
1459                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
1460
1461                 /* Shallow boundary */
1462                 c2 = (c1 + c3) / 2;
1463
1464                 /* make it */
1465                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
1466
1467                 /* Convert to normal format+ clean up */
1468                 done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
1469         }
1470 }
1471 #endif /* ALLOW_CAVERNS_AND_LAKES */
1472
1473
1474 /*
1475  * Routine used by the random vault creators to add a door to a location
1476  * Note that range checking has to be done in the calling routine.
1477  *
1478  * The doors must be INSIDE the allocated region.
1479  */
1480 static void add_door(int x, int y)
1481 {
1482         /* Need to have a wall in the center square */
1483         if (!is_outer_bold(y, x)) return;
1484
1485         /* look at:
1486          *  x#x
1487          *  .#.
1488          *  x#x
1489          *
1490          *  where x=don't care
1491          *  .=floor, #=wall
1492          */
1493
1494         if (is_floor_bold(y-1,x) && is_floor_bold(y+1,x) &&
1495             (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
1496         {
1497                 /* secret door */
1498                 place_secret_door(y, x, DOOR_DEFAULT);
1499
1500                 /* set boundarys so don't get wide doors */
1501                 place_solid_bold(y, x - 1);
1502                 place_solid_bold(y, x + 1);
1503         }
1504
1505
1506         /* look at:
1507          *  x#x
1508          *  .#.
1509          *  x#x
1510          *
1511          *  where x = don't care
1512          *  .=floor, #=wall
1513          */
1514         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
1515             is_floor_bold(y,x-1) && is_floor_bold(y,x+1))
1516         {
1517                 /* secret door */
1518                 place_secret_door(y, x, DOOR_DEFAULT);
1519
1520                 /* set boundarys so don't get wide doors */
1521                 place_solid_bold(y - 1, x);
1522                 place_solid_bold(y + 1, x);
1523         }
1524 }
1525
1526
1527 /*
1528  * Routine that fills the empty areas of a room with treasure and monsters.
1529  */
1530 static void fill_treasure(int x1, int x2, int y1, int y2, int difficulty)
1531 {
1532         int x, y, cx, cy, size;
1533         s32b value;
1534
1535         /* center of room:*/
1536         cx = (x1 + x2) / 2;
1537         cy = (y1 + y2) / 2;
1538
1539         /* Rough measure of size of vault= sum of lengths of sides */
1540         size = abs(x2 - x1) + abs(y2 - y1);
1541
1542         for (x = x1; x <= x2; x++)
1543         {
1544                 for (y = y1; y <= y2; y++)
1545                 {
1546                         /* Thing added based on distance to center of vault
1547                          * Difficulty is 1-easy to 10-hard */
1548                         value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
1549
1550                         /* hack- empty square part of the time */
1551                         if ((randint1(100) - difficulty * 3) > 50) value = 20;
1552
1553                          /* if floor, shallow water and lava */
1554                         if (is_floor_bold(y, x) ||
1555                             (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
1556                         {
1557                                 /* The smaller 'value' is, the better the stuff */
1558                                 if (value < 0)
1559                                 {
1560                                         /* Meanest monster + treasure */
1561                                         monster_level = base_level + 40;
1562                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1563                                         monster_level = base_level;
1564                                         object_level = base_level + 20;
1565                                         place_object(y, x, AM_GOOD);
1566                                         object_level = base_level;
1567                                 }
1568                                 else if (value < 5)
1569                                 {
1570                                         /* Mean monster +treasure */
1571                                         monster_level = base_level + 20;
1572                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1573                                         monster_level = base_level;
1574                                         object_level = base_level + 10;
1575                                         place_object(y, x, AM_GOOD);
1576                                         object_level = base_level;
1577                                 }
1578                                 else if (value < 10)
1579                                 {
1580                                         /* Monster */
1581                                         monster_level = base_level + 9;
1582                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1583                                         monster_level = base_level;
1584                                 }
1585                                 else if (value < 17)
1586                                 {
1587                                         /* Intentional Blank space */
1588
1589                                         /*
1590                                          * (Want some of the vault to be empty
1591                                          * so have room for group monsters.
1592                                          * This is used in the hack above to lower
1593                                          * the density of stuff in the vault.)
1594                                          */
1595                                 }
1596                                 else if (value < 23)
1597                                 {
1598                                         /* Object or trap */
1599                                         if (randint0(100) < 25)
1600                                         {
1601                                                 place_object(y, x, 0L);
1602                                         }
1603                                         else
1604                                         {
1605                                                 place_trap(y, x);
1606                                         }
1607                                 }
1608                                 else if (value < 30)
1609                                 {
1610                                         /* Monster and trap */
1611                                         monster_level = base_level + 5;
1612                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1613                                         monster_level = base_level;
1614                                         place_trap(y, x);
1615                                 }
1616                                 else if (value < 40)
1617                                 {
1618                                         /* Monster or object */
1619                                         if (randint0(100) < 50)
1620                                         {
1621                                                 monster_level = base_level + 3;
1622                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1623                                                 monster_level = base_level;
1624                                         }
1625                                         if (randint0(100) < 50)
1626                                         {
1627                                                 object_level = base_level + 7;
1628                                                 place_object(y, x, 0L);
1629                                                 object_level = base_level;
1630                                         }
1631                                 }
1632                                 else if (value < 50)
1633                                 {
1634                                         /* Trap */
1635                                         place_trap(y, x);
1636                                 }
1637                                 else
1638                                 {
1639                                         /* Various Stuff */
1640
1641                                         /* 20% monster, 40% trap, 20% object, 20% blank space */
1642                                         if (randint0(100) < 20)
1643                                         {
1644                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1645                                         }
1646                                         else if (randint0(100) < 50)
1647                                         {
1648                                                 place_trap(y, x);
1649                                         }
1650                                         else if (randint0(100) < 50)
1651                                         {
1652                                                 place_object(y, x, 0L);
1653                                         }
1654                                 }
1655
1656                         }
1657                 }
1658         }
1659 }
1660
1661
1662 /*
1663  * This function creates a random vault that looks like a collection of bubbles.
1664  * It works by getting a set of coordinates that represent the center of each
1665  * bubble.  The entire room is made by seeing which bubble center is closest. If
1666  * two centers are equidistant then the square is a wall, otherwise it is a floor.
1667  * The only exception is for squares really near a center, these are always floor.
1668  * (It looks better than without this check.)
1669  *
1670  * Note: If two centers are on the same point then this algorithm will create a
1671  *       blank bubble filled with walls. - This is prevented from happening.
1672  */
1673 static void build_bubble_vault(int x0, int y0, int xsize, int ysize)
1674 {
1675         #define BUBBLENUM 10            /* number of bubbles */
1676
1677         /* array of center points of bubbles */
1678         coord center[BUBBLENUM];
1679
1680         int i, j, x, y;
1681         u16b min1, min2, temp;
1682         bool done;
1683
1684         /* Offset from center to top left hand corner */
1685         int xhsize = xsize / 2;
1686         int yhsize = ysize / 2;
1687
1688         msg_print_wizard(CHEAT_DUNGEON, _("泡型ランダムVaultを生成しました。", "Room Vault."));
1689
1690         /* Allocate center of bubbles */
1691         center[0].x = (byte)randint1(xsize - 3) + 1;
1692         center[0].y = (byte)randint1(ysize - 3) + 1;
1693
1694         for (i = 1; i < BUBBLENUM; i++)
1695         {
1696                 done = FALSE;
1697
1698                 /* get center and check to see if it is unique */
1699                 while (!done)
1700                 {
1701                         done = TRUE;
1702
1703                         x = randint1(xsize - 3) + 1;
1704                         y = randint1(ysize - 3) + 1;
1705
1706                         for (j = 0; j < i; j++)
1707                         {
1708                                 /* rough test to see if there is an overlap */
1709                                 if ((x == center[j].x) && (y == center[j].y)) done = FALSE;
1710                         }
1711                 }
1712
1713                 center[i].x = (byte_hack)x;
1714                 center[i].y = (byte_hack)y;
1715         }
1716
1717
1718         /* Top and bottom boundaries */
1719         for (i = 0; i < xsize; i++)
1720         {
1721                 int side_x = x0 - xhsize + i;
1722
1723                 place_outer_noperm_bold(y0 - yhsize + 0, side_x);
1724                 cave[y0 - yhsize + 0][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
1725                 place_outer_noperm_bold(y0 - yhsize + ysize - 1, side_x);
1726                 cave[y0 - yhsize + ysize - 1][side_x].info |= (CAVE_ROOM | CAVE_ICKY);
1727         }
1728
1729         /* Left and right boundaries */
1730         for (i = 1; i < ysize - 1; i++)
1731         {
1732                 int side_y = y0 - yhsize + i;
1733
1734                 place_outer_noperm_bold(side_y, x0 - xhsize + 0);
1735                 cave[side_y][x0 - xhsize + 0].info |= (CAVE_ROOM | CAVE_ICKY);
1736                 place_outer_noperm_bold(side_y, x0 - xhsize + xsize - 1);
1737                 cave[side_y][x0 - xhsize + xsize - 1].info |= (CAVE_ROOM | CAVE_ICKY);
1738         }
1739
1740         /* Fill in middle with bubbles */
1741         for (x = 1; x < xsize - 1; x++)
1742         {
1743                 for (y = 1; y < ysize - 1; y++)
1744                 {
1745                         /* Get distances to two closest centers */
1746
1747                         /* initialize */
1748                         min1 = (u16b)distance(x, y, center[0].x, center[0].y);
1749                         min2 = (u16b)distance(x, y, center[1].x, center[1].y);
1750
1751                         if (min1 > min2)
1752                         {
1753                                 /* swap if in wrong order */
1754                                 temp = min1;
1755                                 min1 = min2;
1756                                 min2 = temp;
1757                         }
1758
1759                         /* Scan the rest */
1760                         for (i = 2; i < BUBBLENUM; i++)
1761                         {
1762                                 temp = (u16b)distance(x, y, center[i].x, center[i].y);
1763
1764                                 if (temp < min1)
1765                                 {
1766                                         /* smallest */
1767                                         min2 = min1;
1768                                         min1 = temp;
1769                                 }
1770                                 else if (temp < min2)
1771                                 {
1772                                         /* second smallest */
1773                                         min2 = temp;
1774                                 }
1775                         }
1776                         if (((min2 - min1) <= 2) && (!(min1 < 3)))
1777                         {
1778                                 /* Boundary at midpoint+ not at inner region of bubble */
1779                                 place_outer_noperm_bold(y0 - yhsize + y, x0 - xhsize + x);
1780                         }
1781                         else
1782                         {
1783                                 /* middle of a bubble */
1784                                 place_floor_bold(y0 - yhsize + y, x0 - xhsize + x);
1785                         }
1786
1787                         /* clean up rest of flags */
1788                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= (CAVE_ROOM | CAVE_ICKY);
1789                 }
1790         }
1791
1792         /* Try to add some random doors */
1793         for (i = 0; i < 500; i++)
1794         {
1795                 x = randint1(xsize - 3) - xhsize + x0 + 1;
1796                 y = randint1(ysize - 3) - yhsize + y0 + 1;
1797                 add_door(x, y);
1798         }
1799
1800         /* Fill with monsters and treasure, low difficulty */
1801         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5));
1802 }
1803
1804
1805 /*
1806  * Overlay a rectangular room given its bounds
1807  * This routine is used by build_room_vault
1808  * The area inside the walls is not touched:
1809  * only granite is removed- normal walls stay
1810  */
1811 static void build_room(int x1, int x2, int y1, int y2)
1812 {
1813         int x, y, i, xsize, ysize, temp;
1814
1815         /* Check if rectangle has no width */
1816         if ((x1 == x2) || (y1 == y2)) return;
1817
1818         /* initialize */
1819         if (x1 > x2)
1820         {
1821                 /* Swap boundaries if in wrong order */
1822                 temp = x1;
1823                 x1 = x2;
1824                 x2 = temp;
1825         }
1826
1827         if (y1 > y2)
1828         {
1829                 /* Swap boundaries if in wrong order */
1830                 temp = y1;
1831                 y1 = y2;
1832                 y2 = temp;
1833         }
1834
1835         /* get total widths */
1836         xsize = x2 - x1;
1837         ysize = y2 - y1;
1838
1839
1840         /* Top and bottom boundaries */
1841         for (i = 0; i <= xsize; i++)
1842         {
1843                 place_outer_noperm_bold(y1, x1 + i);
1844                 cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1845                 place_outer_noperm_bold(y2, x1 + i);
1846                 cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1847         }
1848
1849         /* Left and right boundaries */
1850         for (i = 1; i < ysize; i++)
1851         {
1852                 place_outer_noperm_bold(y1 + i, x1);
1853                 cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
1854                 place_outer_noperm_bold(y1 + i, x2);
1855                 cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
1856         }
1857
1858         /* Middle */
1859         for (x = 1; x < xsize; x++)
1860         {
1861                 for (y = 1; y < ysize; y++)
1862                 {
1863                         if (is_extra_bold(y1+y, x1+x))
1864                         {
1865                                 /* clear the untouched region */
1866                                 place_floor_bold(y1 + y, x1 + x);
1867                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1868                         }
1869                         else
1870                         {
1871                                 /* make it a room- but don't touch */
1872                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1873                         }
1874                 }
1875         }
1876 }
1877
1878
1879 /* Create a random vault that looks like a collection of overlapping rooms */
1880
1881 static void build_room_vault(int x0, int y0, int xsize, int ysize)
1882 {
1883         int i, x1, x2, y1, y2, xhsize, yhsize;
1884
1885         /* get offset from center */
1886         xhsize = xsize / 2;
1887         yhsize = ysize / 2;
1888
1889         msg_print_wizard(CHEAT_DUNGEON, _("部屋型ランダムVaultを生成しました。", "Room Vault."));
1890
1891         /* fill area so don't get problems with arena levels */
1892         for (x1 = 0; x1 < xsize; x1++)
1893         {
1894                 int x = x0 - xhsize + x1;
1895
1896                 for (y1 = 0; y1 < ysize; y1++)
1897                 {
1898                         int y = y0 - yhsize + y1;
1899
1900                         place_extra_bold(y, x);
1901                         cave[y][x].info &= (~CAVE_ICKY);
1902                 }
1903         }
1904
1905         /* add ten random rooms */
1906         for (i = 0; i < 10; i++)
1907         {
1908                 x1 = randint1(xhsize) * 2 + x0 - xhsize;
1909                 x2 = randint1(xhsize) * 2 + x0 - xhsize;
1910                 y1 = randint1(yhsize) * 2 + y0 - yhsize;
1911                 y2 = randint1(yhsize) * 2 + y0 - yhsize;
1912                 build_room(x1, x2, y1, y2);
1913         }
1914
1915         /* Add some random doors */
1916         for (i = 0; i < 500; i++)
1917         {
1918                 x1 = randint1(xsize - 3) - xhsize + x0 + 1;
1919                 y1 = randint1(ysize - 3) - yhsize + y0 + 1;
1920                 add_door(x1, y1);
1921         }
1922
1923         /* Fill with monsters and treasure, high difficulty */
1924         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 2, y0 - yhsize + 1, y0 - yhsize + ysize - 2, randint1(5) + 5);
1925 }
1926
1927
1928 /* Create a random vault out of a fractal cave */
1929 static void build_cave_vault(int x0, int y0, int xsiz, int ysiz)
1930 {
1931         int grd, roug, cutoff, xhsize, yhsize, xsize, ysize, x, y;
1932         bool done, light, room;
1933
1934         /* round to make sizes even */
1935         xhsize = xsiz / 2;
1936         yhsize = ysiz / 2;
1937         xsize = xhsize * 2;
1938         ysize = yhsize * 2;
1939
1940         msg_print_wizard(CHEAT_DUNGEON, _("洞穴ランダムVaultを生成しました。", "Cave Vault."));
1941
1942         light = done = FALSE;
1943         room = TRUE;
1944
1945         while (!done)
1946         {
1947                 /* testing values for these parameters feel free to adjust */
1948                 grd = 1 << randint0(4);
1949
1950                 /* want average of about 16 */
1951                 roug = randint1(8) * randint1(4);
1952
1953                 /* about size/2 */
1954                 cutoff = randint1(xsize / 4) + randint1(ysize / 4) +
1955                          randint1(xsize / 4) + randint1(ysize / 4);
1956
1957                 /* make it */
1958                 generate_hmap(y0, x0, xsize, ysize, grd, roug, cutoff);
1959
1960                 /* Convert to normal format+ clean up */
1961                 done = generate_fracave(y0, x0, xsize, ysize, cutoff, light, room);
1962         }
1963
1964         /* Set icky flag because is a vault */
1965         for (x = 0; x <= xsize; x++)
1966         {
1967                 for (y = 0; y <= ysize; y++)
1968                 {
1969                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
1970                 }
1971         }
1972
1973         /* Fill with monsters and treasure, low difficulty */
1974         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1, y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
1975 }
1976
1977 /*
1978  * maze vault -- rectangular labyrinthine rooms
1979  *
1980  * maze vault uses two routines:
1981  *    r_visit - a recursive routine that builds the labyrinth
1982  *    build_maze_vault - a driver routine that calls r_visit and adds
1983  *                   monsters, traps and treasure
1984  *
1985  * The labyrinth is built by creating a spanning tree of a graph.
1986  * The graph vertices are at
1987  *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
1988  * and the edges are the vertical and horizontal nearest neighbors.
1989  *
1990  * The spanning tree is created by performing a suitably randomized
1991  * depth-first traversal of the graph. The only adjustable parameter
1992  * is the randint0(3) below; it governs the relative density of
1993  * twists and turns in the labyrinth: smaller number, more twists.
1994  */
1995 static void r_visit(int y1, int x1, int y2, int x2,
1996                     int node, int dir, int *visited)
1997 {
1998         int i, j, m, n, temp, x, y, adj[4];
1999
2000         /* dimensions of vertex array */
2001         m = (x2 - x1) / 2 + 1;
2002         n = (y2 - y1) / 2 + 1;
2003
2004         /* mark node visited and set it to a floor */
2005         visited[node] = 1;
2006         x = 2 * (node % m) + x1;
2007         y = 2 * (node / m) + y1;
2008         place_floor_bold(y, x);
2009
2010         /* setup order of adjacent node visits */
2011         if (one_in_(3))
2012         {
2013                 /* pick a random ordering */
2014                 for (i = 0; i < 4; i++)
2015                         adj[i] = i;
2016                 for (i = 0; i < 4; i++)
2017                 {
2018                         j = randint0(4);
2019                         temp = adj[i];
2020                         adj[i] = adj[j];
2021                         adj[j] = temp;
2022                 }
2023                 dir = adj[0];
2024         }
2025         else
2026         {
2027                 /* pick a random ordering with dir first */
2028                 adj[0] = dir;
2029                 for (i = 1; i < 4; i++)
2030                         adj[i] = i;
2031                 for (i = 1; i < 4; i++)
2032                 {
2033                         j = 1 + randint0(3);
2034                         temp = adj[i];
2035                         adj[i] = adj[j];
2036                         adj[j] = temp;
2037                 }
2038         }
2039
2040         for (i = 0; i < 4; i++)
2041         {
2042                 switch (adj[i])
2043                 {
2044                         case 0:
2045                                 /* (0,+) - check for bottom boundary */
2046                                 if ((node / m < n - 1) && (visited[node + m] == 0))
2047                                 {
2048                                         place_floor_bold(y + 1, x);
2049                                         r_visit(y1, x1, y2, x2, node + m, dir, visited);
2050                                 }
2051                                 break;
2052                         case 1:
2053                                 /* (0,-) - check for top boundary */
2054                                 if ((node / m > 0) && (visited[node - m] == 0))
2055                                 {
2056                                         place_floor_bold(y - 1, x);
2057                                         r_visit(y1, x1, y2, x2, node - m, dir, visited);
2058                                 }
2059                                 break;
2060                         case 2:
2061                                 /* (+,0) - check for right boundary */
2062                                 if ((node % m < m - 1) && (visited[node + 1] == 0))
2063                                 {
2064                                         place_floor_bold(y, x + 1);
2065                                         r_visit(y1, x1, y2, x2, node + 1, dir, visited);
2066                                 }
2067                                 break;
2068                         case 3:
2069                                 /* (-,0) - check for left boundary */
2070                                 if ((node % m > 0) && (visited[node - 1] == 0))
2071                                 {
2072                                         place_floor_bold(y, x - 1);
2073                                         r_visit(y1, x1, y2, x2, node - 1, dir, visited);
2074                                 }
2075                 } /* end switch */
2076         }
2077 }
2078
2079
2080 void build_maze_vault(int x0, int y0, int xsize, int ysize, bool is_vault)
2081 {
2082         int y, x, dy, dx;
2083         int y1, x1, y2, x2;
2084         int m, n, num_vertices, *visited;
2085         bool light;
2086         cave_type *c_ptr;
2087
2088         msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
2089
2090         /* Choose lite or dark */
2091         light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
2092
2093         /* Pick a random room size - randomized by calling routine */
2094         dy = ysize / 2 - 1;
2095         dx = xsize / 2 - 1;
2096
2097         y1 = y0 - dy;
2098         x1 = x0 - dx;
2099         y2 = y0 + dy;
2100         x2 = x0 + dx;
2101
2102         /* generate the room */
2103         for (y = y1 - 1; y <= y2 + 1; y++)
2104         {
2105                 for (x = x1 - 1; x <= x2 + 1; x++)
2106                 {
2107                         c_ptr = &cave[y][x];
2108                         c_ptr->info |= CAVE_ROOM;
2109                         if (is_vault) c_ptr->info |= CAVE_ICKY;
2110                         if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
2111                         {
2112                                 place_outer_grid(c_ptr);
2113                         }
2114                         else if (!is_vault)
2115                         {
2116                                 place_extra_grid(c_ptr);
2117                         }
2118                         else
2119                         {
2120                                 place_inner_grid(c_ptr);
2121                         }
2122                         if (light) c_ptr->info |= (CAVE_GLOW);
2123                 }
2124         }
2125
2126         /* dimensions of vertex array */
2127         m = dx + 1;
2128         n = dy + 1;
2129         num_vertices = m * n;
2130
2131         /* initialize array of visited vertices */
2132         C_MAKE(visited, num_vertices, int);
2133
2134         /* traverse the graph to create a spaning tree, pick a random root */
2135         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
2136
2137         /* Fill with monsters and treasure, low difficulty */
2138         if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
2139
2140         C_KILL(visited, num_vertices, int);
2141 }
2142
2143
2144 /* Build a "mini" checkerboard vault
2145  *
2146  * This is done by making a permanent wall maze and setting
2147  * the diagonal sqaures of the checker board to be granite.
2148  * The vault has two entrances on opposite sides to guarantee
2149  * a way to get in even if the vault abuts a side of the dungeon.
2150  */
2151 static void build_mini_c_vault(int x0, int y0, int xsize, int ysize)
2152 {
2153         int dy, dx;
2154         int y1, x1, y2, x2, y, x, total;
2155         int m, n, num_vertices;
2156         int *visited;
2157
2158         msg_print_wizard(CHEAT_DUNGEON, _("小型チェッカーランダムVaultを生成しました。", "Mini Checker Board Vault."));
2159
2160         /* Pick a random room size */
2161         dy = ysize / 2 - 1;
2162         dx = xsize / 2 - 1;
2163
2164         y1 = y0 - dy;
2165         x1 = x0 - dx;
2166         y2 = y0 + dy;
2167         x2 = x0 + dx;
2168
2169
2170         /* generate the room */
2171         for (x = x1 - 2; x <= x2 + 2; x++)
2172         {
2173                 if (!in_bounds(y1-2,x)) break;
2174
2175                 cave[y1-2][x].info |= (CAVE_ROOM | CAVE_ICKY);
2176
2177                 place_outer_noperm_bold(y1-2, x);
2178         }
2179
2180         for (x = x1 - 2; x <= x2 + 2; x++)
2181         {
2182                 if (!in_bounds(y2+2,x)) break;
2183
2184                 cave[y2+2][x].info |= (CAVE_ROOM | CAVE_ICKY);
2185
2186                 place_outer_noperm_bold(y2+2, x);
2187         }
2188
2189         for (y = y1 - 2; y <= y2 + 2; y++)
2190         {
2191                 if (!in_bounds(y,x1-2)) break;
2192
2193                 cave[y][x1-2].info |= (CAVE_ROOM | CAVE_ICKY);
2194
2195                 place_outer_noperm_bold(y, x1-2);
2196         }
2197
2198         for (y = y1 - 2; y <= y2 + 2; y++)
2199         {
2200                 if (!in_bounds(y,x2+2)) break;
2201
2202                 cave[y][x2+2].info |= (CAVE_ROOM | CAVE_ICKY);
2203
2204                 place_outer_noperm_bold(y, x2+2);
2205         }
2206
2207         for (y = y1 - 1; y <= y2 + 1; y++)
2208         {
2209                 for (x = x1 - 1; x <= x2 + 1; x++)
2210                 {
2211                         cave_type *c_ptr = &cave[y][x];
2212
2213                         c_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
2214
2215                         /* Permanent walls */
2216                         place_inner_perm_grid(c_ptr);
2217                 }
2218         }
2219
2220
2221         /* dimensions of vertex array */
2222         m = dx + 1;
2223         n = dy + 1;
2224         num_vertices = m * n;
2225
2226         /* initialize array of visited vertices */
2227         C_MAKE(visited, num_vertices, int);
2228
2229         /* traverse the graph to create a spannng tree, pick a random root */
2230         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
2231
2232         /* Make it look like a checker board vault */
2233         for (x = x1; x <= x2; x++)
2234         {
2235                 for (y = y1; y <= y2; y++)
2236                 {
2237                         total = x - x1 + y - y1;
2238                         /* If total is odd- and is a floor then make a wall */
2239                         if ((total % 2 == 1) && is_floor_bold(y, x))
2240                         {
2241                                 place_inner_bold(y, x);
2242                         }
2243                 }
2244         }
2245
2246         /* Make a couple of entrances */
2247         if (one_in_(2))
2248         {
2249                 /* left and right */
2250                 y = randint1(dy) + dy / 2;
2251                 place_inner_bold(y1 + y, x1 - 1);
2252                 place_inner_bold(y1 + y, x2 + 1);
2253         }
2254         else
2255         {
2256                 /* top and bottom */
2257                 x = randint1(dx) + dx / 2;
2258                 place_inner_bold(y1 - 1, x1 + x);
2259                 place_inner_bold(y2 + 1, x1 + x);
2260         }
2261
2262         /* Fill with monsters and treasure, highest difficulty */
2263         fill_treasure(x1, x2, y1, y2, 10);
2264
2265         C_KILL(visited, num_vertices, int);
2266 }
2267
2268
2269 /* Build a town/ castle by using a recursive algorithm.
2270  * Basically divide each region in a probalistic way to create
2271  * smaller regions.  When the regions get too small stop.
2272  *
2273  * The power variable is a measure of how well defended a region is.
2274  * This alters the possible choices.
2275  */
2276 static void build_recursive_room(int x1, int y1, int x2, int y2, int power)
2277 {
2278         int xsize, ysize;
2279         int x, y;
2280         int choice;
2281
2282         /* Temp variables */
2283         int t1, t2, t3, t4;
2284
2285         xsize = x2 - x1;
2286         ysize = y2 - y1;
2287
2288         if ((power < 3) && (xsize > 12) && (ysize > 12))
2289         {
2290                 /* Need outside wall +keep */
2291                 choice = 1;
2292         }
2293         else
2294         {
2295                 if (power < 10)
2296                 {
2297                         /* Make rooms + subdivide */
2298                         if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
2299                         {
2300                                 choice = 4;
2301                         }
2302                         else
2303                         {
2304                                 choice = randint1(2) + 1;
2305                         }
2306                 }
2307                 else
2308                 {
2309                         /* Mostly subdivide */
2310                         choice = randint1(3) + 1;
2311                 }
2312         }
2313
2314         /* Based on the choice made above, do something */
2315
2316         switch (choice)
2317         {
2318                 case 1:
2319                 {
2320                         /* Outer walls */
2321
2322                         /* top and bottom */
2323                         for (x = x1; x <= x2; x++)
2324                         {
2325                                 place_outer_bold(y1, x);
2326                                 place_outer_bold(y2, x);
2327                         }
2328
2329                         /* left and right */
2330                         for (y = y1 + 1; y < y2; y++)
2331                         {
2332                                 place_outer_bold(y, x1);
2333                                 place_outer_bold(y, x2);
2334                         }
2335
2336                         /* Make a couple of entrances */
2337                         if (one_in_(2))
2338                         {
2339                                 /* left and right */
2340                                 y = randint1(ysize) + y1;
2341                                 place_floor_bold(y, x1);
2342                                 place_floor_bold(y, x2);
2343                         }
2344                         else
2345                         {
2346                                 /* top and bottom */
2347                                 x = randint1(xsize) + x1;
2348                                 place_floor_bold(y1, x);
2349                                 place_floor_bold(y2, x);
2350                         }
2351
2352                         /* Select size of keep */
2353                         t1 = randint1(ysize / 3) + y1;
2354                         t2 = y2 - randint1(ysize / 3);
2355                         t3 = randint1(xsize / 3) + x1;
2356                         t4 = x2 - randint1(xsize / 3);
2357
2358                         /* Do outside areas */
2359
2360                         /* Above and below keep */
2361                         build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
2362                         build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
2363
2364                         /* Left and right of keep */
2365                         build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
2366                         build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
2367
2368                         /* Make the keep itself: */
2369                         x1 = t3;
2370                         x2 = t4;
2371                         y1 = t1;
2372                         y2 = t2;
2373                         xsize = x2 - x1;
2374                         ysize = y2 - y1;
2375                         power += 2;
2376
2377                         /* Fall through */
2378                 }
2379                 case 4:
2380                 {
2381                         /* Try to build a room */
2382                         if ((xsize < 3) || (ysize < 3))
2383                         {
2384                                 for (y = y1; y < y2; y++)
2385                                 {
2386                                         for (x = x1; x < x2; x++)
2387                                         {
2388                                                 place_inner_bold(y, x);
2389                                         }
2390                                 }
2391
2392                                 /* Too small */
2393                                 return;
2394                         }
2395
2396                         /* Make outside walls */
2397                         /* top and bottom */
2398                         for (x = x1 + 1; x <= x2 - 1; x++)
2399                         {
2400                                 place_inner_bold(y1 + 1, x);
2401                                 place_inner_bold(y2 - 1, x);
2402                         }
2403
2404                         /* left and right */
2405                         for (y = y1 + 1; y <= y2 - 1; y++)
2406                         {
2407                                 place_inner_bold(y, x1 + 1);
2408                                 place_inner_bold(y, x2 - 1);
2409                         }
2410
2411                         /* Make a door */
2412                         y = randint1(ysize - 3) + y1 + 1;
2413
2414                         if (one_in_(2))
2415                         {
2416                                 /* left */
2417                                 place_floor_bold(y, x1 + 1);
2418                         }
2419                         else
2420                         {
2421                                 /* right */
2422                                 place_floor_bold(y, x2 - 1);
2423                         }
2424
2425                         /* Build the room */
2426                         build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
2427                         break;
2428                 }
2429                 case 2:
2430                 {
2431                         /* Try and divide vertically */
2432                         if (xsize < 3)
2433                         {
2434                                 /* Too small */
2435                                 for (y = y1; y < y2; y++)
2436                                 {
2437                                         for (x = x1; x < x2; x++)
2438                                         {
2439                                                 place_inner_bold(y, x);
2440                                         }
2441                                 }
2442                                 return;
2443                         }
2444
2445                         t1 = randint1(xsize - 2) + x1 + 1;
2446                         build_recursive_room(x1, y1, t1, y2, power - 2);
2447                         build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
2448                         break;
2449                 }
2450                 case 3:
2451                 {
2452                         /* Try and divide horizontally */
2453                         if (ysize < 3)
2454                         {
2455                                 /* Too small */
2456                                 for (y = y1; y < y2; y++)
2457                                 {
2458                                         for (x = x1; x < x2; x++)
2459                                         {
2460                                                 place_inner_bold(y, x);
2461                                         }
2462                                 }
2463                                 return;
2464                         }
2465
2466                         t1 = randint1(ysize - 2) + y1 + 1;
2467                         build_recursive_room(x1, y1, x2, t1, power - 2);
2468                         build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
2469                         break;
2470                 }
2471         }
2472 }
2473
2474
2475 /* Build a castle */
2476
2477 /* Driver routine: clear the region and call the recursive
2478 * room routine.
2479 *
2480 *This makes a vault that looks like a castle/ city in the dungeon.
2481 */
2482 static void build_castle_vault(int x0, int y0, int xsize, int ysize)
2483 {
2484         int dy, dx;
2485         int y1, x1, y2, x2;
2486         int y, x;
2487
2488         /* Pick a random room size */
2489         dy = ysize / 2 - 1;
2490         dx = xsize / 2 - 1;
2491
2492         y1 = y0 - dy;
2493         x1 = x0 - dx;
2494         y2 = y0 + dy;
2495         x2 = x0 + dx;
2496
2497         msg_print_wizard(CHEAT_DUNGEON, _("城型ランダムVaultを生成しました。", "Castle Vault"));
2498
2499         /* generate the room */
2500         for (y = y1 - 1; y <= y2 + 1; y++)
2501         {
2502                 for (x = x1 - 1; x <= x2 + 1; x++)
2503                 {
2504                         cave[y][x].info |= (CAVE_ROOM | CAVE_ICKY);
2505                         /* Make everything a floor */
2506                         place_floor_bold(y, x);
2507                 }
2508         }
2509
2510         /* Make the castle */
2511         build_recursive_room(x1, y1, x2, y2, randint1(5));
2512
2513         /* Fill with monsters and treasure, low difficulty */
2514         fill_treasure(x1, x2, y1, y2, randint1(3));
2515 }
2516
2517
2518 /*
2519  * Add outer wall to a floored region
2520  * Note: no range checking is done so must be inside dungeon
2521  * This routine also stomps on doors
2522  */
2523 void add_outer_wall(int x, int y, int light, int x1, int y1, int x2, int y2)
2524 {
2525         cave_type *c_ptr;
2526         feature_type *f_ptr;
2527         int i, j;
2528
2529         if (!in_bounds(y, x)) return;
2530
2531         c_ptr = &cave[y][x];
2532
2533         /* hack- check to see if square has been visited before
2534         * if so, then exit (use room flag to do this) */
2535         if (c_ptr->info & CAVE_ROOM) return;
2536
2537         /* set room flag */
2538         c_ptr->info |= CAVE_ROOM;
2539
2540         f_ptr = &f_info[c_ptr->feat];
2541
2542         if (is_floor_bold(y, x))
2543         {
2544                 for (i = -1; i <= 1; i++)
2545                 {
2546                         for (j = -1; j <= 1; j++)
2547                         {
2548                                 if ((x + i >= x1) && (x + i <= x2) &&
2549                                          (y + j >= y1) && (y + j <= y2))
2550                                 {
2551                                         add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
2552                                         if (light) c_ptr->info |= CAVE_GLOW;
2553                                 }
2554                         }
2555                 }
2556         }
2557         else if (is_extra_bold(y, x))
2558         {
2559                 /* Set bounding walls */
2560                 place_outer_bold(y, x);
2561                 if (light) c_ptr->info |= CAVE_GLOW;
2562         }
2563         else if (permanent_wall(f_ptr))
2564         {
2565                 /* Set bounding walls */
2566                 if (light) c_ptr->info |= CAVE_GLOW;
2567         }
2568 }
2569
2570
2571 /*
2572  * Hacked distance formula - gives the 'wrong' answer.
2573  * Used to build crypts
2574  */
2575 int dist2(int x1, int y1, int x2, int y2, int h1, int h2, int h3, int h4)
2576 {
2577         int dx, dy;
2578         dx = abs(x2 - x1);
2579         dy = abs(y2 - y1);
2580
2581         /* Basically this works by taking the normal pythagorean formula
2582          * and using an expansion to express this in a way without the
2583          * square root.  This approximate formula is then perturbed to give
2584          * the distorted results.  (I found this by making a mistake when I was
2585          * trying to fix the circular rooms.)
2586          */
2587
2588         /* h1-h4 are constants that describe the metric */
2589         if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
2590         if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
2591         return (((dx + dy) * 128) / 181 +
2592                 (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
2593         /* 128/181 is approx. 1/sqrt(2) */
2594 }
2595
2596
2597 /*
2598  * Build target vault.
2599  * This is made by two concentric "crypts" with perpendicular
2600  * walls creating the cross-hairs.
2601  */
2602 static void build_target_vault(int x0, int y0, int xsize, int ysize)
2603 {
2604         int rad, x, y;
2605
2606         /* Make a random metric */
2607         int h1, h2, h3, h4;
2608         h1 = randint1(32) - 16;
2609         h2 = randint1(16);
2610         h3 = randint1(32);
2611         h4 = randint1(32) - 16;
2612
2613         msg_print_wizard(CHEAT_DUNGEON, _("対称形ランダムVaultを生成しました。", "Elemental Vault"));
2614
2615         /* work out outer radius */
2616         if (xsize > ysize)
2617         {
2618                 rad = ysize / 2;
2619         }
2620         else
2621         {
2622                 rad = xsize / 2;
2623         }
2624
2625         /* Make floor */
2626         for (x = x0 - rad; x <= x0 + rad; x++)
2627         {
2628                 for (y = y0 - rad; y <= y0 + rad; y++)
2629                 {
2630                         /* clear room flag */
2631                         cave[y][x].info &= ~(CAVE_ROOM);
2632
2633                         /* Vault - so is "icky" */
2634                         cave[y][x].info |= CAVE_ICKY;
2635
2636                         if (dist2(y0, x0, y, x, h1, h2, h3, h4) <= rad - 1)
2637                         {
2638                                 /* inside- so is floor */
2639                                 place_floor_bold(y, x);
2640                         }
2641                         else
2642                         {
2643                                 /* make granite outside so arena works */
2644                                 place_extra_bold(y, x);
2645                         }
2646
2647                         /* proper boundary for arena */
2648                         if (((y + rad) == y0) || ((y - rad) == y0) ||
2649                             ((x + rad) == x0) || ((x - rad) == x0))
2650                         {
2651                                 place_extra_bold(y, x);
2652                         }
2653                 }
2654         }
2655
2656         /* Find visible outer walls and set to be FEAT_OUTER */
2657         add_outer_wall(x0, y0, FALSE, x0 - rad - 1, y0 - rad - 1,
2658                        x0 + rad + 1, y0 + rad + 1);
2659
2660         /* Add inner wall */
2661         for (x = x0 - rad / 2; x <= x0 + rad / 2; x++)
2662         {
2663                 for (y = y0 - rad / 2; y <= y0 + rad / 2; y++)
2664                 {
2665                         if (dist2(y0, x0, y, x, h1, h2, h3, h4) == rad / 2)
2666                         {
2667                                 /* Make an internal wall */
2668                                 place_inner_bold(y, x);
2669                         }
2670                 }
2671         }
2672
2673         /* Add perpendicular walls */
2674         for (x = x0 - rad; x <= x0 + rad; x++)
2675         {
2676                 place_inner_bold(y0, x);
2677         }
2678
2679         for (y = y0 - rad; y <= y0 + rad; y++)
2680         {
2681                 place_inner_bold(y, x0);
2682         }
2683
2684         /* Make inner vault */
2685         for (y = y0 - 1; y <= y0 + 1; y++)
2686         {
2687                 place_inner_bold(y, x0 - 1);
2688                 place_inner_bold(y, x0 + 1);
2689         }
2690         for (x = x0 - 1; x <= x0 + 1; x++)
2691         {
2692                 place_inner_bold(y0 - 1, x);
2693                 place_inner_bold(y0 + 1, x);
2694         }
2695
2696         place_floor_bold(y0, x0);
2697
2698
2699         /* Add doors to vault */
2700         /* get two distances so can place doors relative to centre */
2701         x = (rad - 2) / 4 + 1;
2702         y = rad / 2 + x;
2703
2704         add_door(x0 + x, y0);
2705         add_door(x0 + y, y0);
2706         add_door(x0 - x, y0);
2707         add_door(x0 - y, y0);
2708         add_door(x0, y0 + x);
2709         add_door(x0, y0 + y);
2710         add_door(x0, y0 - x);
2711         add_door(x0, y0 - y);
2712
2713         /* Fill with stuff - medium difficulty */
2714         fill_treasure(x0 - rad, x0 + rad, y0 - rad, y0 + rad, randint1(3) + 3);
2715 }
2716
2717
2718 #ifdef ALLOW_CAVERNS_AND_LAKES
2719 /*
2720  * This routine uses a modified version of the lake code to make a
2721  * distribution of some terrain type over the vault.  This type
2722  * depends on the dungeon depth.
2723  *
2724  * Miniture rooms are then scattered across the vault.
2725  */
2726 static void build_elemental_vault(int x0, int y0, int xsiz, int ysiz)
2727 {
2728         int grd, roug;
2729         int c1, c2, c3;
2730         bool done = FALSE;
2731         int xsize, ysize, xhsize, yhsize, x, y, i;
2732         int type;
2733
2734         msg_print_wizard(CHEAT_DUNGEON, _("精霊界ランダムVaultを生成しました。", "Elemental Vault"));
2735
2736         /* round to make sizes even */
2737         xhsize = xsiz / 2;
2738         yhsize = ysiz / 2;
2739         xsize = xhsize * 2;
2740         ysize = yhsize * 2;
2741
2742         if (dun_level < 25)
2743         {
2744                 /* Earth vault  (Rubble) */
2745                 type = LAKE_T_EARTH_VAULT;
2746         }
2747         else if (dun_level < 50)
2748         {
2749                 /* Air vault (Trees) */
2750                 type = LAKE_T_AIR_VAULT;
2751         }
2752         else if (dun_level < 75)
2753         {
2754                 /* Water vault (shallow water) */
2755                 type = LAKE_T_WATER_VAULT;
2756         }
2757         else
2758         {
2759                 /* Fire vault (shallow lava) */
2760                 type = LAKE_T_FIRE_VAULT;
2761         }
2762
2763         while (!done)
2764         {
2765                 /* testing values for these parameters: feel free to adjust */
2766                 grd = 1 << (randint0(3));
2767
2768                 /* want average of about 16 */
2769                 roug = randint1(8) * randint1(4);
2770
2771                 /* Make up size of various componants */
2772                 /* Floor */
2773                 c3 = 2 * xsize / 3;
2774
2775                 /* Deep water/lava */
2776                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
2777
2778                 /* Shallow boundary */
2779                 c2 = (c1 + c3) / 2;
2780
2781                 /* make it */
2782                 generate_hmap(y0, x0, xsize, ysize, grd, roug, c3);
2783
2784                 /* Convert to normal format+ clean up */
2785                 done = generate_lake(y0, x0, xsize, ysize, c1, c2, c3, type);
2786         }
2787
2788         /* Set icky flag because is a vault */
2789         for (x = 0; x <= xsize; x++)
2790         {
2791                 for (y = 0; y <= ysize; y++)
2792                 {
2793                         cave[y0 - yhsize + y][x0 - xhsize + x].info |= CAVE_ICKY;
2794                 }
2795         }
2796
2797         /* make a few rooms in the vault */
2798         for (i = 1; i <= (xsize * ysize) / 50; i++)
2799         {
2800                 build_small_room(x0 + randint0(xsize - 4) - xsize / 2 + 2,
2801                                  y0 + randint0(ysize - 4) - ysize / 2 + 2);
2802         }
2803
2804         /* Fill with monsters and treasure, low difficulty */
2805         fill_treasure(x0 - xhsize + 1, x0 - xhsize + xsize - 1,
2806                       y0 - yhsize + 1, y0 - yhsize + ysize - 1, randint1(5));
2807 }
2808 #endif /* ALLOW_CAVERNS_AND_LAKES */
2809
2810
2811 /*!
2812  * @brief タイプ10の部屋…ランダム生成vault / Type 10 -- Random vaults
2813  * @return なし
2814  */
2815 static bool build_type10(void)
2816 {
2817         POSITION y0, x0, xsize, ysize, vtype;
2818
2819         /* Get size */
2820         /* big enough to look good, small enough to be fairly common. */
2821         xsize = randint1(22) + 22;
2822         ysize = randint1(11) + 11;
2823
2824         /* Find and reserve some space in the dungeon.  Get center of room. */
2825         if (!find_space(&y0, &x0, ysize + 1, xsize + 1)) return FALSE;
2826
2827         /* Select type of vault */
2828 #ifdef ALLOW_CAVERNS_AND_LAKES
2829         do
2830         {
2831                 vtype = randint1(15);
2832         }
2833         while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
2834                 ((vtype == 1) || (vtype == 3) || (vtype == 8) || (vtype == 9) || (vtype == 11)));
2835 #else /* ALLOW_CAVERNS_AND_LAKES */
2836         do
2837         {
2838                 vtype = randint1(7);
2839         }
2840         while ((d_info[dungeon_type].flags1 & DF1_NO_CAVE) &&
2841                 ((vtype == 1) || (vtype == 3)));
2842 #endif /* ALLOW_CAVERNS_AND_LAKES */
2843
2844         switch (vtype)
2845         {
2846                 /* Build an appropriate room */
2847                 case 1: case  9: build_bubble_vault(x0, y0, xsize, ysize); break;
2848                 case 2: case 10: build_room_vault(x0, y0, xsize, ysize); break;
2849                 case 3: case 11: build_cave_vault(x0, y0, xsize, ysize); break;
2850                 case 4: case 12: build_maze_vault(x0, y0, xsize, ysize, TRUE); break;
2851                 case 5: case 13: build_mini_c_vault(x0, y0, xsize, ysize); break;
2852                 case 6: case 14: build_castle_vault(x0, y0, xsize, ysize); break;
2853                 case 7: case 15: build_target_vault(x0, y0, xsize, ysize); break;
2854 #ifdef ALLOW_CAVERNS_AND_LAKES
2855                 case 8: build_elemental_vault(x0, y0, xsize, ysize); break;
2856 #endif /* ALLOW_CAVERNS_AND_LAKES */
2857                 /* I know how to add a few more... give me some time. */
2858
2859                 /* Paranoia */
2860                 default: return FALSE;
2861         }
2862
2863         return TRUE;
2864 }
2865
2866
2867
2868 /*!
2869  * @brief タイプ14の部屋…特殊トラップ部屋の生成 / Type 14 -- trapped rooms
2870  * @return なし
2871  * @details
2872  * A special trap is placed at center of the room
2873  */
2874 static bool build_type14(void)
2875 {
2876         POSITION y, x, y2, x2, yval, xval;
2877         POSITION y1, x1, xsize, ysize;
2878
2879         bool light;
2880
2881         cave_type *c_ptr;
2882         s16b trap;
2883
2884         /* Pick a room size */
2885         y1 = randint1(4);
2886         x1 = randint1(11);
2887         y2 = randint1(3);
2888         x2 = randint1(11);
2889
2890         xsize = x1 + x2 + 1;
2891         ysize = y1 + y2 + 1;
2892
2893         /* Find and reserve some space in the dungeon.  Get center of room. */
2894         if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
2895
2896         /* Choose lite or dark */
2897         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
2898
2899
2900         /* Get corner values */
2901         y1 = yval - ysize / 2;
2902         x1 = xval - xsize / 2;
2903         y2 = yval + (ysize - 1) / 2;
2904         x2 = xval + (xsize - 1) / 2;
2905
2906
2907         /* Place a full floor under the room */
2908         for (y = y1 - 1; y <= y2 + 1; y++)
2909         {
2910                 for (x = x1 - 1; x <= x2 + 1; x++)
2911                 {
2912                         c_ptr = &cave[y][x];
2913                         place_floor_grid(c_ptr);
2914                         c_ptr->info |= (CAVE_ROOM);
2915                         if (light) c_ptr->info |= (CAVE_GLOW);
2916                 }
2917         }
2918
2919         /* Walls around the room */
2920         for (y = y1 - 1; y <= y2 + 1; y++)
2921         {
2922                 c_ptr = &cave[y][x1 - 1];
2923                 place_outer_grid(c_ptr);
2924                 c_ptr = &cave[y][x2 + 1];
2925                 place_outer_grid(c_ptr);
2926         }
2927         for (x = x1 - 1; x <= x2 + 1; x++)
2928         {
2929                 c_ptr = &cave[y1 - 1][x];
2930                 place_outer_grid(c_ptr);
2931                 c_ptr = &cave[y2 + 1][x];
2932                 place_outer_grid(c_ptr);
2933         }
2934
2935         if (dun_level < 30 + randint1(30))
2936                 trap = feat_trap_piranha;
2937         else
2938                 trap = feat_trap_armageddon;
2939
2940         /* Place a special trap */
2941         c_ptr = &cave[rand_spread(yval, ysize/4)][rand_spread(xval, xsize/4)];
2942         c_ptr->mimic = c_ptr->feat;
2943         c_ptr->feat = trap;
2944
2945         msg_format_wizard(CHEAT_DUNGEON, _("%sの部屋が生成されました。", "Room of %s was generated."), f_name + f_info[trap].name);
2946
2947         return TRUE;
2948 }
2949
2950
2951 /*
2952  * Helper function for "glass room"
2953  */
2954 static bool vault_aux_lite(MONRACE_IDX r_idx)
2955 {
2956         monster_race *r_ptr = &r_info[r_idx];
2957
2958         /* Validate the monster */
2959         if (!vault_monster_okay(r_idx)) return FALSE;
2960
2961         /* Require lite attack */
2962         if (!(r_ptr->flags4 & RF4_BR_LITE) && !(r_ptr->a_ability_flags1 & RF5_BA_LITE)) return FALSE;
2963
2964         /* No wall passing monsters */
2965         if (r_ptr->flags2 & (RF2_PASS_WALL | RF2_KILL_WALL)) return FALSE;
2966
2967         /* No disintegrating monsters */
2968         if (r_ptr->flags4 & RF4_BR_DISI) return FALSE;
2969
2970         return TRUE;
2971 }
2972
2973 /*
2974  * Helper function for "glass room"
2975  */
2976 static bool vault_aux_shards(MONRACE_IDX r_idx)
2977 {
2978         monster_race *r_ptr = &r_info[r_idx];
2979
2980         /* Validate the monster */
2981         if (!vault_monster_okay(r_idx)) return FALSE;
2982
2983         /* Require shards breath attack */
2984         if (!(r_ptr->flags4 & RF4_BR_SHAR)) return FALSE;
2985
2986         return TRUE;
2987 }
2988
2989 /*
2990  * Hack -- determine if a template is potion
2991  */
2992 static bool kind_is_potion(KIND_OBJECT_IDX k_idx)
2993 {
2994         return k_info[k_idx].tval == TV_POTION;
2995 }
2996
2997 /*!
2998  * @brief タイプ15の部屋…ガラス部屋の生成 / Type 15 -- glass rooms
2999  * @return なし
3000  */
3001 static bool build_type15(void)
3002 {
3003         POSITION y, x, y2, x2, yval, xval;
3004         POSITION y1, x1, xsize, ysize;
3005         bool light;
3006
3007         cave_type *c_ptr;
3008
3009         /* Pick a room size */
3010         xsize = rand_range(9, 13);
3011         ysize = rand_range(9, 13);
3012
3013         /* Find and reserve some space in the dungeon.  Get center of room. */
3014         if (!find_space(&yval, &xval, ysize + 2, xsize + 2)) return FALSE;
3015
3016         /* Choose lite or dark */
3017         light = ((dun_level <= randint1(25)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
3018
3019         /* Get corner values */
3020         y1 = yval - ysize / 2;
3021         x1 = xval - xsize / 2;
3022         y2 = yval + (ysize - 1) / 2;
3023         x2 = xval + (xsize - 1) / 2;
3024
3025         /* Place a full floor under the room */
3026         for (y = y1 - 1; y <= y2 + 1; y++)
3027         {
3028                 for (x = x1 - 1; x <= x2 + 1; x++)
3029                 {
3030                         c_ptr = &cave[y][x];
3031                         place_floor_grid(c_ptr);
3032                         c_ptr->feat = feat_glass_floor;
3033                         c_ptr->info |= (CAVE_ROOM);
3034                         if (light) c_ptr->info |= (CAVE_GLOW);
3035                 }
3036         }
3037
3038         /* Walls around the room */
3039         for (y = y1 - 1; y <= y2 + 1; y++)
3040         {
3041                 c_ptr = &cave[y][x1 - 1];
3042                 place_outer_grid(c_ptr);
3043                 c_ptr->feat = feat_glass_wall;
3044                 c_ptr = &cave[y][x2 + 1];
3045                 place_outer_grid(c_ptr);
3046                 c_ptr->feat = feat_glass_wall;
3047         }
3048         for (x = x1 - 1; x <= x2 + 1; x++)
3049         {
3050                 c_ptr = &cave[y1 - 1][x];
3051                 place_outer_grid(c_ptr);
3052                 c_ptr->feat = feat_glass_wall;
3053                 c_ptr = &cave[y2 + 1][x];
3054                 place_outer_grid(c_ptr);
3055                 c_ptr->feat = feat_glass_wall;
3056         }
3057
3058         switch (randint1(3))
3059         {
3060         case 1: /* 4 lite breathers + potion */
3061                 {
3062                         int dir1, dir2;
3063
3064                         /* Prepare allocation table */
3065                         get_mon_num_prep(vault_aux_lite, NULL);
3066
3067                         /* Place fixed lite berathers */
3068                         for (dir1 = 4; dir1 < 8; dir1++)
3069                         {
3070                                 MONRACE_IDX r_idx = get_mon_num(dun_level);
3071
3072                                 y = yval + 2 * ddy_ddd[dir1];
3073                                 x = xval + 2 * ddx_ddd[dir1];
3074                                 if (r_idx) place_monster_aux(0, y, x, r_idx, PM_ALLOW_SLEEP);
3075
3076                                 /* Walls around the breather */
3077                                 for (dir2 = 0; dir2 < 8; dir2++)
3078                                 {
3079                                         c_ptr = &cave[y + ddy_ddd[dir2]][x + ddx_ddd[dir2]];
3080                                         place_inner_grid(c_ptr);
3081                                         c_ptr->feat = feat_glass_wall;
3082                                 }
3083                         }
3084
3085                         /* Walls around the potion */
3086                         for (dir1 = 0; dir1 < 4; dir1++)
3087                         {
3088                                 y = yval + 2 * ddy_ddd[dir1];
3089                                 x = xval + 2 * ddx_ddd[dir1];
3090                                 c_ptr = &cave[y][x];
3091                                 place_inner_perm_grid(c_ptr);
3092                                 c_ptr->feat = feat_permanent_glass_wall;
3093                                 cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]].info |= (CAVE_ICKY);
3094                         }
3095
3096                         /* Glass door */
3097                         dir1 = randint0(4);
3098                         y = yval + 2 * ddy_ddd[dir1];
3099                         x = xval + 2 * ddx_ddd[dir1];
3100                         place_secret_door(y, x, DOOR_GLASS_DOOR);
3101                         c_ptr = &cave[y][x];
3102                         if (is_closed_door(c_ptr->feat)) c_ptr->mimic = feat_glass_wall;
3103
3104                         /* Place a potion */
3105                         get_obj_num_hook = kind_is_potion;
3106                         place_object(yval, xval, AM_NO_FIXED_ART);
3107                         cave[yval][xval].info |= (CAVE_ICKY);
3108                 }
3109                 break;
3110
3111         case 2: /* 1 lite breather + random object */
3112                 {
3113                         MONRACE_IDX r_idx;
3114                         DIRECTION dir1;
3115
3116                         /* Pillars */
3117                         c_ptr = &cave[y1 + 1][x1 + 1];
3118                         place_inner_grid(c_ptr);
3119                         c_ptr->feat = feat_glass_wall;
3120
3121                         c_ptr = &cave[y1 + 1][x2 - 1];
3122                         place_inner_grid(c_ptr);
3123                         c_ptr->feat = feat_glass_wall;
3124
3125                         c_ptr = &cave[y2 - 1][x1 + 1];
3126                         place_inner_grid(c_ptr);
3127                         c_ptr->feat = feat_glass_wall;
3128
3129                         c_ptr = &cave[y2 - 1][x2 - 1];
3130                         place_inner_grid(c_ptr);
3131                         c_ptr->feat = feat_glass_wall;
3132
3133                         /* Prepare allocation table */
3134                         get_mon_num_prep(vault_aux_lite, NULL);
3135
3136                         r_idx = get_mon_num(dun_level);
3137                         if (r_idx) place_monster_aux(0, yval, xval, r_idx, 0L);
3138
3139                         /* Walls around the breather */
3140                         for (dir1 = 0; dir1 < 8; dir1++)
3141                         {
3142                                 c_ptr = &cave[yval + ddy_ddd[dir1]][xval + ddx_ddd[dir1]];
3143                                 place_inner_grid(c_ptr);
3144                                 c_ptr->feat = feat_glass_wall;
3145                         }
3146
3147                         /* Curtains around the breather */
3148                         for (y = yval - 1; y <= yval + 1; y++)
3149                         {
3150                                 place_closed_door(y, xval - 2, DOOR_CURTAIN);
3151                                 place_closed_door(y, xval + 2, DOOR_CURTAIN);
3152                         }
3153                         for (x = xval - 1; x <= xval + 1; x++)
3154                         {
3155                                 place_closed_door(yval - 2, x, DOOR_CURTAIN);
3156                                 place_closed_door(yval + 2, x, DOOR_CURTAIN);
3157                         }
3158
3159                         /* Place an object */
3160                         place_object(yval, xval, AM_NO_FIXED_ART);
3161                         cave[yval][xval].info |= (CAVE_ICKY);
3162                 }
3163                 break;
3164
3165         case 3: /* 4 shards breathers + 2 potions */
3166                 {
3167                         int dir1;
3168
3169                         /* Walls around the potion */
3170                         for (y = yval - 2; y <= yval + 2; y++)
3171                         {
3172                                 c_ptr = &cave[y][xval - 3];
3173                                 place_inner_grid(c_ptr);
3174                                 c_ptr->feat = feat_glass_wall;
3175                                 c_ptr = &cave[y][xval + 3];
3176                                 place_inner_grid(c_ptr);
3177                                 c_ptr->feat = feat_glass_wall;
3178                         }
3179                         for (x = xval - 2; x <= xval + 2; x++)
3180                         {
3181                                 c_ptr = &cave[yval - 3][x];
3182                                 place_inner_grid(c_ptr);
3183                                 c_ptr->feat = feat_glass_wall;
3184                                 c_ptr = &cave[yval + 3][x];
3185                                 place_inner_grid(c_ptr);
3186                                 c_ptr->feat = feat_glass_wall;
3187                         }
3188                         for (dir1 = 4; dir1 < 8; dir1++)
3189                         {
3190                                 c_ptr = &cave[yval + 2 * ddy_ddd[dir1]][xval + 2 * ddx_ddd[dir1]];
3191                                 place_inner_grid(c_ptr);
3192                                 c_ptr->feat = feat_glass_wall;
3193                         }
3194
3195                         /* Prepare allocation table */
3196                         get_mon_num_prep(vault_aux_shards, NULL);
3197
3198                         /* Place shard berathers */
3199                         for (dir1 = 4; dir1 < 8; dir1++)
3200                         {
3201                                 MONRACE_IDX r_idx = get_mon_num(dun_level);
3202
3203                                 y = yval + ddy_ddd[dir1];
3204                                 x = xval + ddx_ddd[dir1];
3205                                 if (r_idx) place_monster_aux(0, y, x, r_idx, 0L);
3206                         }
3207
3208                         /* Place two potions */
3209                         if (one_in_(2))
3210                         {
3211                                 get_obj_num_hook = kind_is_potion;
3212                                 place_object(yval, xval - 1, AM_NO_FIXED_ART);
3213                                 get_obj_num_hook = kind_is_potion;
3214                                 place_object(yval, xval + 1, AM_NO_FIXED_ART);
3215                         }
3216                         else
3217                         {
3218                                 get_obj_num_hook = kind_is_potion;
3219                                 place_object(yval - 1, xval, AM_NO_FIXED_ART);
3220                                 get_obj_num_hook = kind_is_potion;
3221                                 place_object(yval + 1, xval, AM_NO_FIXED_ART);
3222                         }
3223
3224                         for (y = yval - 2; y <= yval + 2; y++)
3225                                 for (x = xval - 2; x <= xval + 2; x++)
3226                                         cave[y][x].info |= (CAVE_ICKY);
3227
3228                 }
3229                 break;
3230         }
3231
3232         msg_print_wizard(CHEAT_DUNGEON, _("ガラスの部屋が生成されました。", "Glass room was generated."));
3233
3234         return TRUE;
3235 }
3236
3237
3238 /* Create a new floor room with optional light */
3239 void generate_room_floor(int y1, int x1, int y2, int x2, int light)
3240 {
3241         int y, x;
3242         
3243         cave_type *c_ptr;
3244
3245         for (y = y1; y <= y2; y++)
3246         {
3247                 for (x = x1; x <= x2; x++)
3248                 {
3249                         /* Point to grid */
3250                         c_ptr = &cave[y][x];
3251                         place_floor_grid(c_ptr);
3252                         c_ptr->info |= (CAVE_ROOM);
3253                         if (light) c_ptr->info |= (CAVE_GLOW);
3254                 }
3255         }
3256 }
3257
3258 void generate_fill_perm_bold(int y1, int x1, int y2, int x2)
3259 {
3260         int y, x;
3261
3262         for (y = y1; y <= y2; y++)
3263         {
3264                 for (x = x1; x <= x2; x++)
3265                 {
3266                         /* Point to grid */
3267                         place_inner_perm_bold(y, x);
3268                 }
3269         }
3270 }
3271
3272 /* Minimum & maximum town size */
3273 #define MIN_TOWN_WID ((MAX_WID / 3) / 2)
3274 #define MIN_TOWN_HGT ((MAX_HGT / 3) / 2)
3275 #define MAX_TOWN_WID ((MAX_WID / 3) * 2 / 3)
3276 #define MAX_TOWN_HGT ((MAX_HGT / 3) * 2 / 3)
3277
3278 /* Struct for build underground buildings */
3279 typedef struct
3280 {
3281         int y0, x0; /* North-west corner (relative) */
3282         int y1, x1; /* South-east corner (relative) */
3283 }
3284 ugbldg_type;
3285
3286 ugbldg_type *ugbldg;
3287
3288 /*
3289  * Precalculate buildings' location of underground arcade
3290  */
3291 static bool precalc_ugarcade(int town_hgt, int town_wid, int n)
3292 {
3293         int i, y, x, center_y, center_x, tmp, attempt = 10000;
3294         int max_bldg_hgt = 3 * town_hgt / MAX_TOWN_HGT;
3295         int max_bldg_wid = 5 * town_wid / MAX_TOWN_WID;
3296         ugbldg_type *cur_ugbldg;
3297         bool **ugarcade_used, abort;
3298
3299         /* Allocate "ugarcade_used" array (2-dimension) */
3300         C_MAKE(ugarcade_used, town_hgt, bool *);
3301         C_MAKE(*ugarcade_used, town_hgt * town_wid, bool);
3302         for (y = 1; y < town_hgt; y++) ugarcade_used[y] = *ugarcade_used + y * town_wid;
3303
3304         /* Calculate building locations */
3305         for (i = 0; i < n; i++)
3306         {
3307                 cur_ugbldg = &ugbldg[i];
3308                 (void)WIPE(cur_ugbldg, ugbldg_type);
3309
3310                 do
3311                 {
3312                         /* Find the "center" of the store */
3313                         center_y = rand_range(2, town_hgt - 3);
3314                         center_x = rand_range(2, town_wid - 3);
3315
3316                         /* Determine the store boundaries */
3317                         tmp = center_y - randint1(max_bldg_hgt);
3318                         cur_ugbldg->y0 = MAX(tmp, 1);
3319                         tmp = center_x - randint1(max_bldg_wid);
3320                         cur_ugbldg->x0 = MAX(tmp, 1);
3321                         tmp = center_y + randint1(max_bldg_hgt);
3322                         cur_ugbldg->y1 = MIN(tmp, town_hgt - 2);
3323                         tmp = center_x + randint1(max_bldg_wid);
3324                         cur_ugbldg->x1 = MIN(tmp, town_wid - 2);
3325
3326                         /* Scan this building's area */
3327                         for (abort = FALSE, y = cur_ugbldg->y0; (y <= cur_ugbldg->y1) && !abort; y++)
3328                         {
3329                                 for (x = cur_ugbldg->x0; x <= cur_ugbldg->x1; x++)
3330                                 {
3331                                         if (ugarcade_used[y][x])
3332                                         {
3333                                                 abort = TRUE;
3334                                                 break;
3335                                         }
3336                                 }
3337                         }
3338
3339                         attempt--;
3340                 }
3341                 while (abort && attempt); /* Accept this building if no overlapping */
3342
3343                 /* Failed to generate underground arcade */
3344                 if (!attempt) break;
3345
3346                 /*
3347                  * Mark to ugarcade_used[][] as "used"
3348                  * Note: Building-adjacent grids are included for preventing
3349                  * connected bulidings.
3350                  */
3351                 for (y = cur_ugbldg->y0 - 1; y <= cur_ugbldg->y1 + 1; y++)
3352                 {
3353                         for (x = cur_ugbldg->x0 - 1; x <= cur_ugbldg->x1 + 1; x++)
3354                         {
3355                                 ugarcade_used[y][x] = TRUE;
3356                         }
3357                 }
3358         }
3359
3360         /* Free "ugarcade_used" array (2-dimension) */
3361         C_KILL(*ugarcade_used, town_hgt * town_wid, bool);
3362         C_KILL(ugarcade_used, town_hgt, bool *);
3363
3364         /* If i < n, generation is not allowed */
3365         return i == n;
3366 }
3367
3368 /*!
3369  * @brief タイプ16の部屋…地下都市生成のサブルーチン / Actually create buildings
3370  * @return なし
3371  * @param ltcy 生成基準Y座標
3372  * @param ltcx 生成基準X座標
3373  * @param stotes[] 生成する店舗のリスト
3374  * @param n 生成する店舗の数
3375  * @note
3376  * Note: ltcy and ltcx indicate "left top corner".
3377  */
3378 static void build_stores(int ltcy, int ltcx, int stores[], int n)
3379 {
3380         int i, y, x;
3381         IDX j;
3382         ugbldg_type *cur_ugbldg;
3383
3384         for (i = 0; i < n; i++)
3385         {
3386                 cur_ugbldg = &ugbldg[i];
3387
3388                 /* Generate new room */
3389                 generate_room_floor(
3390                         ltcy + cur_ugbldg->y0 - 2, ltcx + cur_ugbldg->x0 - 2,
3391                         ltcy + cur_ugbldg->y1 + 2, ltcx + cur_ugbldg->x1 + 2,
3392                         FALSE);
3393         }
3394
3395         for (i = 0; i < n; i++)
3396         {
3397                 cur_ugbldg = &ugbldg[i];
3398
3399                 /* Build an invulnerable rectangular building */
3400                 generate_fill_perm_bold(
3401                         ltcy + cur_ugbldg->y0, ltcx + cur_ugbldg->x0,
3402                         ltcy + cur_ugbldg->y1, ltcx + cur_ugbldg->x1);
3403
3404                 /* Pick a door direction (S,N,E,W) */
3405                 switch (randint0(4))
3406                 {
3407                 /* Bottom side */
3408                 case 0:
3409                         y = cur_ugbldg->y1;
3410                         x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
3411                         break;
3412
3413                 /* Top side */
3414                 case 1:
3415                         y = cur_ugbldg->y0;
3416                         x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
3417                         break;
3418
3419                 /* Right side */
3420                 case 2:
3421                         y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
3422                         x = cur_ugbldg->x1;
3423                         break;
3424
3425                 /* Left side */
3426                 default:
3427                         y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
3428                         x = cur_ugbldg->x0;
3429                         break;
3430                 }
3431
3432                 for (j = 0; j < max_f_idx; j++)
3433                 {
3434                         if (have_flag(f_info[j].flags, FF_STORE))
3435                         {
3436                                 if (f_info[j].subtype == stores[i]) break;
3437                         }
3438                 }
3439
3440                 /* Clear previous contents, add a store door */
3441                 if (j < max_f_idx)
3442                 {
3443                         cave_set_feat(ltcy + y, ltcx + x, j);
3444
3445                         /* Init store */
3446                         store_init(NO_TOWN, stores[i]);
3447                 }
3448         }
3449 }
3450
3451
3452 /*!
3453  * @brief タイプ16の部屋…地下都市の生成 / Type 16 -- Underground Arcade
3454  * @return なし
3455  * @details
3456  * Town logic flow for generation of new town\n
3457  * Originally from Vanilla 3.0.3\n
3458  *\n
3459  * We start with a fully wiped cave of normal floors.\n
3460  *\n
3461  * Note that town_gen_hack() plays games with the R.N.G.\n
3462  *\n
3463  * This function does NOT do anything about the owners of the stores,\n
3464  * nor the contents thereof.  It only handles the physical layout.\n
3465  */
3466 static bool build_type16(void)
3467 {
3468         int stores[] =
3469         {
3470                 STORE_GENERAL, STORE_ARMOURY, STORE_WEAPON, STORE_TEMPLE,
3471                 STORE_ALCHEMIST, STORE_MAGIC, STORE_BLACK, STORE_BOOK,
3472         };
3473         int n = sizeof stores / sizeof (int);
3474         POSITION i, y, x, y1, x1, yval, xval;
3475         int town_hgt = rand_range(MIN_TOWN_HGT, MAX_TOWN_HGT);
3476         int town_wid = rand_range(MIN_TOWN_WID, MAX_TOWN_WID);
3477         bool prevent_bm = FALSE;
3478
3479         /* Hack -- If already exist black market, prevent building */
3480         for (y = 0; (y < cur_hgt) && !prevent_bm; y++)
3481         {
3482                 for (x = 0; x < cur_wid; x++)
3483                 {
3484                         if (cave[y][x].feat == FF_STORE)
3485                         {
3486                                 prevent_bm = (f_info[cave[y][x].feat].subtype == STORE_BLACK);
3487                                 break;
3488                         }
3489                 }
3490         }
3491         for (i = 0; i < n; i++)
3492         {
3493                 if ((stores[i] == STORE_BLACK) && prevent_bm) stores[i] = stores[--n];
3494         }
3495         if (!n) return FALSE;
3496
3497         /* Allocate buildings array */
3498         C_MAKE(ugbldg, n, ugbldg_type);
3499
3500         /* If cannot build stores, abort */
3501         if (!precalc_ugarcade(town_hgt, town_wid, n))
3502         {
3503                 /* Free buildings array */
3504                 C_KILL(ugbldg, n, ugbldg_type);
3505                 return FALSE;
3506         }
3507
3508         /* Find and reserve some space in the dungeon.  Get center of room. */
3509         if (!find_space(&yval, &xval, town_hgt + 4, town_wid + 4))
3510         {
3511                 /* Free buildings array */
3512                 C_KILL(ugbldg, n, ugbldg_type);
3513                 return FALSE;
3514         }
3515
3516         /* Get top left corner */
3517         y1 = yval - (town_hgt / 2);
3518         x1 = xval - (town_wid / 2);
3519
3520         /* Generate new room */
3521         generate_room_floor(
3522                 y1 + town_hgt / 3, x1 + town_wid / 3,
3523                 y1 + town_hgt * 2 / 3, x1 + town_wid * 2 / 3, FALSE);
3524
3525         /* Build stores */
3526         build_stores(y1, x1, stores, n);
3527
3528         msg_print_wizard(CHEAT_DUNGEON, _("地下街を生成しました", "Underground arcade was generated."));
3529
3530         /* Free buildings array */
3531         C_KILL(ugbldg, n, ugbldg_type);
3532
3533         return TRUE;
3534 }
3535
3536
3537 /*!
3538  * @brief 与えられた部屋型IDに応じて部屋の生成処理分岐を行い結果を返す / Attempt to build a room of the given type at the given block
3539  * @param type 部屋型ID
3540  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
3541  * @return 部屋の精製に成功した場合 TRUE を返す。
3542  */
3543 static bool room_build(int typ)
3544 {
3545         /* Build a room */
3546         switch (typ)
3547         {
3548         /* Build an appropriate room */
3549         case ROOM_T_NORMAL:        return build_type1();
3550         case ROOM_T_OVERLAP:       return build_type2();
3551         case ROOM_T_CROSS:         return build_type3();
3552         case ROOM_T_INNER_FEAT:    return build_type4();
3553         case ROOM_T_NEST:          return build_type5();
3554         case ROOM_T_PIT:           return build_type6();
3555         case ROOM_T_LESSER_VAULT:  return build_type7();
3556         case ROOM_T_GREATER_VAULT: return build_type8();
3557         case ROOM_T_FRACAVE:       return build_type9();
3558         case ROOM_T_RANDOM_VAULT:  return build_type10();
3559         case ROOM_T_OVAL:          return build_type11();
3560         case ROOM_T_CRYPT:         return build_type12();
3561         case ROOM_T_TRAP_PIT:      return build_type13();
3562         case ROOM_T_TRAP:          return build_type14();
3563         case ROOM_T_GLASS:         return build_type15();
3564         case ROOM_T_ARCADE:        return build_type16();
3565         }
3566
3567         /* Paranoia */
3568         return FALSE;
3569 }
3570
3571 /*!
3572  * @brief 指定した部屋の生成確率を別の部屋に加算し、指定した部屋の生成率を0にする
3573  * @param dst 確率を移す先の部屋種ID
3574  * @param src 確率を与える元の部屋種ID
3575  */
3576 #define MOVE_PLIST(dst, src) (prob_list[dst] += prob_list[src], prob_list[src] = 0) 
3577
3578 /*!
3579  * @brief 部屋生成処理のメインルーチン(Sangbandを経由してOangbandからの実装を引用) / Generate rooms in dungeon.  Build bigger rooms at first. [from SAngband (originally from OAngband)]
3580  * @return 部屋生成に成功した場合 TRUE を返す。
3581  */
3582 bool generate_rooms(void)
3583 {
3584         int i;
3585         bool remain;
3586         int crowded = 0;
3587         int total_prob;
3588         int prob_list[ROOM_T_MAX];
3589         int rooms_built = 0;
3590         int area_size = 100 * (cur_hgt*cur_wid) / (MAX_HGT*MAX_WID);
3591         int level_index = MIN(10, div_round(dun_level, 10));
3592
3593         /* Number of each type of room on this level */
3594         s16b room_num[ROOM_T_MAX];
3595
3596         /* Limit number of rooms */
3597         int dun_rooms = DUN_ROOMS_MAX * area_size / 100;
3598
3599         /* Assume normal cave */
3600         room_info_type *room_info_ptr = room_info_normal;
3601
3602         /*
3603          * Initialize probability list.
3604          */
3605         for (i = 0; i < ROOM_T_MAX; i++)
3606         {
3607                 /* No rooms allowed above their minimum depth. */
3608                 if (dun_level < room_info_ptr[i].min_level)
3609                 {
3610                         prob_list[i] = 0;
3611                 }
3612                 else
3613                 {
3614                         prob_list[i] = room_info_ptr[i].prob[level_index];
3615                 }
3616         }
3617
3618         /*
3619          * XXX -- Various dungeon types and options.
3620          */
3621
3622         /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、かつ「常に通常でない部屋を生成する」フラグがONならば、GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
3623         if (ironman_rooms && !((d_info[dungeon_type].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
3624         {
3625                 for (i = 0; i < ROOM_T_MAX; i++)
3626                 {
3627                         if (i == ROOM_T_GREATER_VAULT) prob_list[i] = 1;
3628                         else prob_list[i] = 0;
3629                 }
3630         }
3631
3632         /*! @details ダンジョンにNO_VAULTフラグがあるならば、LESSER_VAULT / GREATER_VAULT/ RANDOM_VAULTを除外 / Forbidden vaults */
3633         else if (d_info[dungeon_type].flags1 & DF1_NO_VAULT)
3634         {
3635                 prob_list[ROOM_T_LESSER_VAULT] = 0;
3636                 prob_list[ROOM_T_GREATER_VAULT] = 0;
3637                 prob_list[ROOM_T_RANDOM_VAULT] = 0;
3638         }
3639
3640         /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
3641         if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
3642         {
3643                 MOVE_PLIST(ROOM_T_NORMAL, ROOM_T_FRACAVE);
3644                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_CRYPT);
3645                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_OVAL);
3646         }
3647
3648         /*! @details ダンジョンにCAVEフラグがある場合、NORMALの生成枠がFRACAVEに与えられる。/ CAVE dungeon (Orc cave etc.) */
3649         else if (d_info[dungeon_type].flags1 & DF1_CAVE)
3650         {
3651                 MOVE_PLIST(ROOM_T_FRACAVE, ROOM_T_NORMAL);
3652         }
3653
3654         /*! @details ダンジョンの基本地形が最初から渓谷かアリーナ型の場合 FRACAVE は生成から除外。 /  No caves when a (random) cavern exists: they look bad */
3655         else if (dun->cavern || dun->empty_level)
3656         {
3657                 prob_list[ROOM_T_FRACAVE] = 0;
3658         }
3659
3660         /*! @details ダンジョンに最初からGLASS_ROOMフラグがある場合、GLASS を生成から除外。/ Forbidden glass rooms */
3661         if (!(d_info[dungeon_type].flags1 & DF1_GLASS_ROOM))
3662         {
3663                 prob_list[ROOM_T_GLASS] = 0;
3664         }
3665
3666         /*! @details ARCADEは同フラグがダンジョンにないと生成されない。 / Forbidden glass rooms */
3667         if (!(d_info[dungeon_type].flags1 & DF1_ARCADE))
3668         {
3669                 prob_list[ROOM_T_ARCADE] = 0;
3670         }
3671
3672         /*
3673          * Initialize number of rooms,
3674          * And calcurate total probability.
3675          */
3676         for (total_prob = 0, i = 0; i < ROOM_T_MAX; i++)
3677         {
3678                 room_num[i] = 0;
3679                 total_prob += prob_list[i];
3680         }
3681
3682         /*
3683          * Prepare the number of rooms, of all types, we should build
3684          * on this level.
3685          */
3686         for (i = dun_rooms; i > 0; i--)
3687         {
3688                 int room_type;
3689                 int rand = randint0(total_prob);
3690
3691                 /* Get room_type randomly */
3692                 for (room_type = 0; room_type < ROOM_T_MAX; room_type++)
3693                 {
3694                         if (rand < prob_list[room_type]) break;
3695                         else rand -= prob_list[room_type];
3696                 }
3697
3698                 /* Paranoia */
3699                 if (room_type >= ROOM_T_MAX) room_type = ROOM_T_NORMAL;
3700
3701                 /* Increase the number of rooms of that type we should build. */
3702                 room_num[room_type]++;
3703
3704                 switch (room_type)
3705                 {
3706                 case ROOM_T_NEST:
3707                 case ROOM_T_PIT:
3708                 case ROOM_T_LESSER_VAULT:
3709                 case ROOM_T_TRAP_PIT:
3710                 case ROOM_T_GLASS:
3711                 case ROOM_T_ARCADE:
3712
3713                         /* Large room */
3714                         i -= 2;
3715                         break;
3716
3717                 case ROOM_T_GREATER_VAULT:
3718                 case ROOM_T_RANDOM_VAULT:
3719
3720                         /* Largest room */
3721                         i -= 3;
3722                         break;
3723                 }
3724         }
3725
3726         /*
3727          * Build each type of room one by one until we cannot build any more.
3728          * [from SAngband (originally from OAngband)]
3729          */
3730         while (TRUE)
3731         {
3732                 /* Assume no remaining rooms */
3733                 remain = FALSE;
3734
3735                 for (i = 0; i < ROOM_T_MAX; i++)
3736                 {
3737                         /* What type of room are we building now? */
3738                         int room_type = room_build_order[i];
3739
3740                         /* Go next if none available */
3741                         if (!room_num[room_type]) continue;
3742
3743                         /* Use up one unit */
3744                         room_num[room_type]--;
3745
3746                         /* Build the room. */
3747                         if (room_build(room_type))
3748                         {
3749                                 /* Increase the room built count. */
3750                                 rooms_built++;
3751
3752                                 /* Mark as there was some remaining rooms */
3753                                 remain = TRUE;
3754
3755                                 switch (room_type)
3756                                 {
3757                                 case ROOM_T_PIT:
3758                                 case ROOM_T_NEST:
3759                                 case ROOM_T_TRAP_PIT:
3760
3761                                         /* Avoid too many monsters */
3762                                         if (++crowded >= 2)
3763                                         {
3764                                                 room_num[ROOM_T_PIT] = 0;
3765                                                 room_num[ROOM_T_NEST] = 0;
3766                                                 room_num[ROOM_T_TRAP_PIT] = 0;
3767                                         }
3768                                         break;
3769
3770                                 case ROOM_T_ARCADE:
3771
3772                                         /* Avoid double-town */
3773                                         room_num[ROOM_T_ARCADE] = 0;
3774                                         break;
3775                                 }
3776                         }
3777                 }
3778
3779                 /* End loop if no room remain */
3780                 if (!remain) break;
3781         }
3782
3783         /*! @details 部屋生成数が2未満の場合生成失敗を返す */
3784         if (rooms_built < 2)
3785         {
3786                 msg_format_wizard(CHEAT_DUNGEON, _("部屋数が2未満でした。生成を再試行します。", "Number of rooms was under 2. Retry."), rooms_built);
3787                 return FALSE;
3788         }
3789
3790         msg_format_wizard(CHEAT_DUNGEON, _("このダンジョンの部屋数は %d です。", "Number of Rooms: %d"), rooms_built);
3791
3792         return TRUE;
3793 }