OSDN Git Service

[Refactor] #37353 コメント整理。 / Refactor comments.
[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-city.h"
45 #include "rooms-fractal.h"
46 #include "rooms-normal.h"
47 #include "rooms-pitnest.h"
48 #include "rooms-special.h"
49 #include "rooms-trap.h"
50 #include "rooms-vault.h"
51
52 #include "trap.h"
53
54
55 /*!
56  * 各部屋タイプの生成比定義
57  *[from SAngband (originally from OAngband)]\n
58  *\n
59  * Table of values that control how many times each type of room will\n
60  * appear.  Each type of room has its own row, and each column\n
61  * corresponds to dungeon levels 0, 10, 20, and so on.  The final\n
62  * value is the minimum depth the room can appear at.  -LM-\n
63  *\n
64  * Level 101 and below use the values for level 100.\n
65  *\n
66  * Rooms with lots of monsters or loot may not be generated if the\n
67  * object or monster lists are already nearly full.  Rooms will not\n
68  * appear above their minimum depth.  Tiny levels will not have space\n
69  * for all the rooms you ask for.\n
70  */
71
72 #if 0
73
74 static room_info_type room_info_normal[ROOM_T_MAX] =
75 {
76         /* Depth */
77         /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
78
79         {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
80         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
81         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
82         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*INNER_F  */
83         {{  0,  1,  1,  1,  2,  3,  5,  6,  8, 10, 13}, 10}, /*NEST     */
84         {{  0,  1,  1,  2,  3,  4,  6,  8, 10, 13, 16}, 10}, /*PIT      */
85         {{  0,  1,  1,  1,  2,  2,  3,  5,  6,  8, 10}, 10}, /*LESSER_V */
86         {{  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  4}, 20}, /*GREATER_V*/
87         {{  0,100,200,300,400,500,600,700,800,900,999}, 10}, /*FRACAVE  */
88         {{  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2}, 10}, /*RANDOM_V */
89         {{  0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40},  3}, /*OVAL     */
90         {{  1,  6, 12, 18, 24, 30, 36, 42, 48, 54, 60}, 10}, /*CRYPT    */
91         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP_PIT */
92         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
93         {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
94         {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
95         {{ 20, 40, 60, 80,100,100,100,100,100,100,100},  1}, /*FIX   */
96 };
97
98 #endif
99
100 #if 1
101 static room_info_type room_info_normal[ROOM_T_MAX] =
102 {
103 /*デバッグ用配列*/
104 { {999, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0}, 0}, /*NORMAL   */
105 { {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  1 }, /*OVERLAP  */
106 { {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  3 }, /*CROSS    */
107 { {  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,  0},  3 }, /*INNER_F  */
108 { {  0,  1,  1,  1,  2,  3,  5,  6,  8, 10,  0}, 10 }, /*NEST     */
109 { {  0,  1,  1,  2,  3,  4,  6,  8, 10, 13,  0}, 10 }, /*PIT      */
110 { {  0,  1,  1,  1,  2,  2,  3,  5,  6,  8,  0}, 10 }, /*LESSER_V */
111 { {  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  0}, 20 }, /*GREATER_V*/
112 { {  0,100,200,300,400,500,600,700,800,900,  0}, 10 }, /*FRACAVE  */
113 { {  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  0}, 10 }, /*RANDOM_V */
114 { {  0,  4,  8, 12, 16, 20, 24, 28, 32, 36,  0},  3 }, /*OVAL     */
115 { {  1,  6, 12, 18, 24, 30, 36, 42, 48, 54,  0}, 10 }, /*CRYPT    */
116 { {  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  0}, 20 }, /*TRAP_PIT */
117 { {  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  0}, 20 }, /*TRAP     */
118 { {  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  0}, 40 }, /*GLASS    */
119 { {  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  0},  1 }, /*ARCADE   */
120 { { 20, 40, 60, 80,100,100,100,100,100,100,100},  1 }, /*FIX   */
121 };
122
123 #endif
124
125
126
127 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
128 static byte room_build_order[ROOM_T_MAX] = {
129         ROOM_T_GREATER_VAULT,
130         ROOM_T_ARCADE,
131         ROOM_T_RANDOM_VAULT,
132         ROOM_T_LESSER_VAULT,
133         ROOM_T_TRAP_PIT,
134         ROOM_T_PIT,
135         ROOM_T_NEST,
136         ROOM_T_TRAP,
137         ROOM_T_GLASS,
138         ROOM_T_INNER_FEAT,
139         ROOM_T_OVAL,
140         ROOM_T_CRYPT,
141         ROOM_T_OVERLAP,
142         ROOM_T_CROSS,
143         ROOM_T_FRACAVE,
144         ROOM_T_FIXED,
145         ROOM_T_NORMAL,
146 };
147
148 /*!
149  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
150  * @param y0 配置したい中心のY座標
151  * @param x0 配置したい中心のX座標
152  * @details
153  * This funtion makes a very small room centred at (x0, y0)
154  * This is used in crypts, and random elemental vaults.
155  *
156  * Note - this should be used only on allocated regions
157  * within another room.
158  */
159 void build_small_room(POSITION x0, POSITION y0)
160 {
161         POSITION x, y;
162
163         for (y = y0 - 1; y <= y0 + 1; y++)
164         {
165                 place_inner_bold(y, x0 - 1);
166                 place_inner_bold(y, x0 + 1);
167         }
168
169         for (x = x0 - 1; x <= x0 + 1; x++)
170         {
171                 place_inner_bold(y0 - 1, x);
172                 place_inner_bold(y0 + 1, x);
173         }
174
175         /* Place a secret door on one side */
176         switch (randint0(4))
177         {
178                 case 0: place_secret_door(y0, x0 - 1, DOOR_DEFAULT); break;
179                 case 1: place_secret_door(y0, x0 + 1, DOOR_DEFAULT); break;
180                 case 2: place_secret_door(y0 - 1, x0, DOOR_DEFAULT); break;
181                 case 3: place_secret_door(y0 + 1, x0, DOOR_DEFAULT); break;
182         }
183
184         /* Clear mimic type */
185         cave[y0][x0].mimic = 0;
186
187         /* Add inner open space */
188         place_floor_bold(y0, x0);
189 }
190
191 /*!
192  * @brief
193  * 指定範囲に通路が通っていることを確認した上で床で埋める
194  * This function tunnels around a room if it will cut off part of a cave system.
195  * @param x1 範囲の左端
196  * @param y1 範囲の上端
197  * @param x2 範囲の右端
198  * @param y2 範囲の下端
199  * @return なし
200  */
201 static void check_room_boundary(POSITION x1, POSITION y1, POSITION x2, POSITION y2)
202 {
203         int count;
204         POSITION x, y;
205         bool old_is_floor, new_is_floor;
206         count = 0;
207
208         old_is_floor = get_is_floor(x1 - 1, y1);
209
210         /*
211          * Count the number of floor-wall boundaries around the room
212          * Note: diagonal squares are ignored since the player can move diagonally
213          * to bypass these if needed.
214          */
215
216         /* Above the top boundary */
217         for (x = x1; x <= x2; x++)
218         {
219                 new_is_floor = get_is_floor(x, y1 - 1);
220
221                 /* Increment counter if they are different */
222                 if (new_is_floor != old_is_floor) count++;
223
224                 old_is_floor = new_is_floor;
225         }
226
227         /* Right boundary */
228         for (y = y1; y <= y2; y++)
229         {
230                 new_is_floor = get_is_floor(x2 + 1, y);
231
232                 /* increment counter if they are different */
233                 if (new_is_floor != old_is_floor) count++;
234
235                 old_is_floor = new_is_floor;
236         }
237
238         /* Bottom boundary */
239         for (x = x2; x >= x1; x--)
240         {
241                 new_is_floor = get_is_floor(x, y2 + 1);
242
243                 /* increment counter if they are different */
244                 if (new_is_floor != old_is_floor) count++;
245
246                 old_is_floor = new_is_floor;
247         }
248
249         /* Left boundary */
250         for (y = y2; y >= y1; y--)
251         {
252                 new_is_floor = get_is_floor(x1 - 1, y);
253
254                 /* increment counter if they are different */
255                 if (new_is_floor != old_is_floor) count++;
256
257                 old_is_floor = new_is_floor;
258         }
259
260         /* If all the same, or only one connection exit. */
261         if (count <= 2) return;
262
263
264         /* Tunnel around the room so to prevent problems with caves */
265         for (y = y1; y <= y2; y++)
266         {
267                 for (x = x1; x <= x2; x++)
268                 {
269                         set_floor(x, y);
270                 }
271         }
272 }
273
274
275 /*!
276  * @brief
277  * find_space()の予備処理として部屋の生成が可能かを判定する /
278  * Helper function for find_space(). Is this a good location?
279  * @param blocks_high 範囲の高さ
280  * @param blocks_wide 範囲の幅
281  * @param block_y 範囲の上端
282  * @param block_x 範囲の左端
283  * @return なし
284  */
285 static bool find_space_aux(int blocks_high, int blocks_wide, int block_y, int block_x)
286 {
287         int by1, bx1, by2, bx2, by, bx;
288
289         /* Itty-bitty rooms must shift about within their rectangle */
290         if (blocks_wide < 3)
291         {
292                 if ((blocks_wide == 2) && (block_x % 3) == 2)
293                         return FALSE;
294         }
295
296         /* Rooms with width divisible by 3 must be fitted to a rectangle. */
297         else if ((blocks_wide % 3) == 0)
298         {
299                 /* Must be aligned to the left edge of a 11x33 rectangle. */
300                 if ((block_x % 3) != 0)
301                         return FALSE;
302         }
303
304         /*
305          * Big rooms that do not have a width divisible by 3 must be
306          * aligned towards the edge of the dungeon closest to them.
307          */
308         else
309         {
310                 /* Shift towards left edge of dungeon. */
311                 if (block_x + (blocks_wide / 2) <= dun->col_rooms / 2)
312                 {
313                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
314                                 return FALSE;
315                         if ((block_x % 3) == 1)
316                                 return FALSE;
317                 }
318
319                 /* Shift toward right edge of dungeon. */
320                 else
321                 {
322                         if (((block_x % 3) == 2) && ((blocks_wide % 3) == 2))
323                                 return FALSE;
324                         if ((block_x % 3) == 1)
325                                 return FALSE;
326                 }
327         }
328
329         /* Extract blocks */
330         by1 = block_y + 0;
331         bx1 = block_x + 0;
332         by2 = block_y + blocks_high;
333         bx2 = block_x + blocks_wide;
334
335         /* Never run off the screen */
336         if ((by1 < 0) || (by2 > dun->row_rooms)) return FALSE;
337         if ((bx1 < 0) || (bx2 > dun->col_rooms)) return FALSE;
338         
339         /* Verify available space */
340         for (by = by1; by < by2; by++)
341         {
342                 for (bx = bx1; bx < bx2; bx++)
343                 {
344                         if (dun->room_map[by][bx])
345                         {
346                                 return FALSE;
347                         }
348                 }
349         }
350
351         /* This location is okay */
352         return TRUE;
353 }
354
355
356 /*!
357  * @brief 部屋生成が可能なスペースを確保する / Find a good spot for the next room.  -LM-
358  * @param y 部屋の生成が可能な中心Y座標を返す参照ポインタ
359  * @param x 部屋の生成が可能な中心X座標を返す参照ポインタ
360  * @param height 確保したい領域の高さ
361  * @param width 確保したい領域の幅
362  * @return 所定の範囲が確保できた場合TRUEを返す
363  * @details
364  * Find and allocate a free space in the dungeon large enough to hold\n
365  * the room calling this function.\n
366  *\n
367  * We allocate space in 11x11 blocks, but want to make sure that rooms\n
368  * align neatly on the standard screen.  Therefore, we make them use\n
369  * blocks in few 11x33 rectangles as possible.\n
370  *\n
371  * Be careful to include the edges of the room in height and width!\n
372  *\n
373  * Return TRUE and values for the center of the room if all went well.\n
374  * Otherwise, return FALSE.\n
375  */
376 bool find_space(POSITION *y, POSITION *x, POSITION height, POSITION width)
377 {
378         int candidates, pick;
379         int by, bx, by1, bx1, by2, bx2;
380         int block_y = 0, block_x = 0;
381
382
383         /* Find out how many blocks we need. */
384         int blocks_high = 1 + ((height - 1) / BLOCK_HGT);
385         int blocks_wide = 1 + ((width - 1) / BLOCK_WID);
386
387         /* There are no way to allocate such huge space */
388         if (dun->row_rooms < blocks_high) return FALSE;
389         if (dun->col_rooms < blocks_wide) return FALSE;
390
391         /* Initiallize */
392         candidates = 0;
393
394         /* Count the number of valid places */
395         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
396         {
397                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
398                 {
399                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
400                         {
401                                 /* Find a valid place */
402                                 candidates++;
403                         }
404                 }
405         }
406
407         /* No place! */
408         if (!candidates)
409         {
410                 return FALSE;
411         }
412
413         /* Normal dungeon */
414         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
415         {
416                 /* Choose a random one */
417                 pick = randint1(candidates);
418         }
419
420         /* NO_CAVE dungeon (Castle) */
421         else
422         {
423                 /* Always choose the center one */
424                 pick = candidates/2 + 1;
425         }
426
427         /* Pick up the choosen location */
428         for (block_y = dun->row_rooms - blocks_high; block_y >= 0; block_y--)
429         {
430                 for (block_x = dun->col_rooms - blocks_wide; block_x >= 0; block_x--)
431                 {
432                         if (find_space_aux(blocks_high, blocks_wide, block_y, block_x))
433                         {
434                                 pick--;
435
436                                 /* This one is picked? */
437                                 if (!pick) break;
438                         }
439                 }
440
441                 if (!pick) break;
442         }
443
444         /* Extract blocks */
445         by1 = block_y + 0;
446         bx1 = block_x + 0;
447         by2 = block_y + blocks_high;
448         bx2 = block_x + blocks_wide;
449
450         /*
451          * It is *extremely* important that the following calculation
452          * be *exactly* correct to prevent memory errors
453          */
454
455         /* Acquire the location of the room */
456         (*y) = ((by1 + by2) * BLOCK_HGT) / 2;
457         (*x) = ((bx1 + bx2) * BLOCK_WID) / 2;
458
459         /* Save the room location */
460         if (dun->cent_n < CENT_MAX)
461         {
462                 dun->cent[dun->cent_n].y = (byte_hack)*y;
463                 dun->cent[dun->cent_n].x = (byte_hack)*x;
464                 dun->cent_n++;
465         }
466
467         /* Reserve some blocks. */
468         for (by = by1; by < by2; by++)
469         {
470                 for (bx = bx1; bx < bx2; bx++)
471                 {
472                         dun->room_map[by][bx] = TRUE;
473                 }
474         }
475
476
477         /*
478          * Hack- See if room will cut off a cavern.
479          *
480          * If so, fix by tunneling outside the room in such a
481          * way as to connect the caves.
482          */
483         check_room_boundary(*x - width / 2 - 1, *y - height / 2 - 1, *x + (width - 1) / 2 + 1, *y + (height - 1) / 2 + 1);
484
485
486         /* Success. */
487         return TRUE;
488 }
489
490
491
492
493 /*
494  * Structure to hold all "fill" data
495  */
496
497 typedef struct fill_data_type fill_data_type;
498
499 struct fill_data_type
500 {
501         /* area size */
502         POSITION xmin;
503         POSITION ymin;
504         POSITION xmax;
505         POSITION ymax;
506
507         /* cutoffs */
508         int c1;
509         int c2;
510         int c3;
511
512         /* features to fill with */
513         FEAT_IDX feat1;
514         FEAT_IDX feat2;
515         FEAT_IDX feat3;
516
517         int info1;
518         int info2;
519         int info3;
520
521         /* number of filled squares */
522         int amount;
523 };
524
525 static fill_data_type fill_data;
526
527
528 /* Store routine for the fractal cave generator */
529 /* this routine probably should be an inline function or a macro. */
530 static void store_height(POSITION x, POSITION y, int val)
531 {
532         /* if on boundary set val > cutoff so walls are not as square */
533         if (((x == fill_data.xmin) || (y == fill_data.ymin) ||
534              (x == fill_data.xmax) || (y == fill_data.ymax)) &&
535             (val <= fill_data.c1)) val = fill_data.c1 + 1;
536
537         /* store the value in height-map format */
538         cave[y][x].feat = (s16b)val;
539
540         return;
541 }
542
543
544 /*
545 * Explanation of the plasma fractal algorithm:
546 *
547 * A grid of points is created with the properties of a 'height-map'
548 * This is done by making the corners of the grid have a random value.
549 * The grid is then subdivided into one with twice the resolution.
550 * The new points midway between two 'known' points can be calculated
551 * by taking the average value of the 'known' ones and randomly adding
552 * or subtracting an amount proportional to the distance between those
553 * points.  The final 'middle' points of the grid are then calculated
554 * by averaging all four of the originally 'known' corner points.  An
555 * random amount is added or subtracted from this to get a value of the
556 * height at that point.  The scaling factor here is adjusted to the
557 * slightly larger distance diagonally as compared to orthogonally.
558 *
559 * This is then repeated recursively to fill an entire 'height-map'
560 * A rectangular map is done the same way, except there are different
561 * scaling factors along the x and y directions.
562 *
563 * A hack to change the amount of correlation between points is done using
564 * the grd variable.  If the current step size is greater than grd then
565 * the point will be random, otherwise it will be calculated by the
566 * above algorithm.  This makes a maximum distance at which two points on
567 * the height map can affect each other.
568 *
569 * How fractal caves are made:
570 *
571 * When the map is complete, a cut-off value is used to create a cave.
572 * Heights below this value are "floor", and heights above are "wall".
573 * This also can be used to create lakes, by adding more height levels
574 * representing shallow and deep water/ lava etc.
575 *
576 * The grd variable affects the width of passages.
577 * The roug variable affects the roughness of those passages
578 *
579 * The tricky part is making sure the created cave is connected.  This
580 * is done by 'filling' from the inside and only keeping the 'filled'
581 * floor.  Walls bounding the 'filled' floor are also kept.  Everything
582 * else is converted to the normal _extra_.
583  */
584
585
586 /*
587  *  Note that this uses the cave.feat array in a very hackish way
588  *  the values are first set to zero, and then each array location
589  *  is used as a "heightmap"
590  *  The heightmap then needs to be converted back into the "feat" format.
591  *
592  *  grd=level at which fractal turns on.  smaller gives more mazelike caves
593  *  roug=roughness level.  16=normal.  higher values make things more convoluted
594  *    small values are good for smooth walls.
595  *  size=length of the side of the square cave system.
596  */
597 void generate_hmap(POSITION y0, POSITION x0, POSITION xsiz, POSITION ysiz, int grd, int roug, int cutoff)
598 {
599         POSITION xhsize, yhsize, xsize, ysize, maxsize;
600
601         /*
602          * fixed point variables- these are stored as 256 x normal value
603          * this gives 8 binary places of fractional part + 8 places of normal part
604          */
605
606         u16b xstep, xhstep, ystep, yhstep;
607         u16b xstep2, xhstep2, ystep2, yhstep2;
608         u16b i, j, ii, jj, diagsize, xxsize, yysize;
609         
610         /* Cache for speed */
611         u16b xm, xp, ym, yp;
612
613         /* redefine size so can change the value if out of range */
614         xsize = xsiz;
615         ysize = ysiz;
616
617         /* Paranoia about size of the system of caves */
618         if (xsize > 254) xsize = 254;
619         if (xsize < 4) xsize = 4;
620         if (ysize > 254) ysize = 254;
621         if (ysize < 4) ysize = 4;
622
623         /* get offsets to middle of array */
624         xhsize = xsize / 2;
625         yhsize = ysize / 2;
626
627         /* fix rounding problem */
628         xsize = xhsize * 2;
629         ysize = yhsize * 2;
630
631         /* get limits of region */
632         fill_data.xmin = x0 - xhsize;
633         fill_data.ymin = y0 - yhsize;
634         fill_data.xmax = x0 + xhsize;
635         fill_data.ymax = y0 + yhsize;
636
637         /* Store cutoff in global for quick access */
638         fill_data.c1 = cutoff;
639
640         /*
641         * Scale factor for middle points:
642         * About sqrt(2) * 256 - correct for a square lattice
643         * approximately correct for everything else.
644          */
645         diagsize = 362;
646
647         /* maximum of xsize and ysize */
648         maxsize = (xsize > ysize) ? xsize : ysize;
649
650         /* Clear the section */
651         for (i = 0; i <= xsize; i++)
652         {
653                 for (j = 0; j <= ysize; j++)
654                 {
655                         /* -1 is a flag for "not done yet" */
656                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].feat = -1;
657                         /* Clear icky flag because may be redoing the cave */
658                         cave[(int)(fill_data.ymin + j)][(int)(fill_data.xmin + i)].info &= ~(CAVE_ICKY);
659                 }
660         }
661
662         /* Boundaries are walls */
663         cave[fill_data.ymin][fill_data.xmin].feat = (s16b)maxsize;
664         cave[fill_data.ymax][fill_data.xmin].feat = (s16b)maxsize;
665         cave[fill_data.ymin][fill_data.xmax].feat = (s16b)maxsize;
666         cave[fill_data.ymax][fill_data.xmax].feat = (s16b)maxsize;
667
668         /* Set the middle square to be an open area. */
669         cave[y0][x0].feat = 0;
670
671         /* Initialize the step sizes */
672         xstep = xhstep = xsize * 256;
673         ystep = yhstep = ysize * 256;
674         xxsize = xsize * 256;
675         yysize = ysize * 256;
676
677         /*
678          * Fill in the rectangle with fractal height data -
679          * like the 'plasma fractal' in fractint.
680          */
681         while ((xhstep > 256) || (yhstep > 256))
682         {
683                 /* Halve the step sizes */
684                 xstep = xhstep;
685                 xhstep /= 2;
686                 ystep = yhstep;
687                 yhstep /= 2;
688
689                 /* cache well used values */
690                 xstep2 = xstep / 256;
691                 ystep2 = ystep / 256;
692
693                 xhstep2 = xhstep / 256;
694                 yhstep2 = yhstep / 256;
695
696                 /* middle top to bottom. */
697                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
698                 {
699                         for (j = 0; j <= yysize; j += ystep)
700                         {
701                                 /* cache often used values */
702                                 ii = i / 256 + fill_data.xmin;
703                                 jj = j / 256 + fill_data.ymin;
704
705                                 /* Test square */
706                                 if (cave[jj][ii].feat == -1)
707                                 {
708                                         if (xhstep2 > grd)
709                                         {
710                                                 /* If greater than 'grid' level then is random */
711                                                 store_height(ii, jj, randint1(maxsize));
712                                         }
713                                         else
714                                         {
715                                                 /* Average of left and right points +random bit */
716                                                 store_height(ii, jj,
717                                                         (cave[jj][fill_data.xmin + (i - xhstep) / 256].feat
718                                                          + cave[jj][fill_data.xmin + (i + xhstep) / 256].feat) / 2
719                                                          + (randint1(xstep2) - xhstep2) * roug / 16);
720                                         }
721                                 }
722                         }
723                 }
724
725
726                 /* middle left to right. */
727                 for (j = yhstep; j <= yysize - yhstep; j += ystep)
728                 {
729                         for (i = 0; i <= xxsize; i += xstep)
730                         {
731                                 /* cache often used values */
732                                 ii = i / 256 + fill_data.xmin;
733                                 jj = j / 256 + fill_data.ymin;
734
735                                 /* Test square */
736                                 if (cave[jj][ii].feat == -1)
737                                 {
738                                         if (xhstep2 > grd)
739                                         {
740                                                 /* If greater than 'grid' level then is random */
741                                                 store_height(ii, jj, randint1(maxsize));
742                                         }
743                                         else
744                                         {
745                                                 /* Average of up and down points +random bit */
746                                                 store_height(ii, jj,
747                                                         (cave[fill_data.ymin + (j - yhstep) / 256][ii].feat
748                                                         + cave[fill_data.ymin + (j + yhstep) / 256][ii].feat) / 2
749                                                         + (randint1(ystep2) - yhstep2) * roug / 16);
750                                         }
751                                 }
752                         }
753                 }
754
755                 /* center. */
756                 for (i = xhstep; i <= xxsize - xhstep; i += xstep)
757                 {
758                         for (j = yhstep; j <= yysize - yhstep; j += ystep)
759                         {
760                                 /* cache often used values */
761                                 ii = i / 256 + fill_data.xmin;
762                                 jj = j / 256 + fill_data.ymin;
763
764                                 /* Test square */
765                                 if (cave[jj][ii].feat == -1)
766                                 {
767                                         if (xhstep2 > grd)
768                                         {
769                                                 /* If greater than 'grid' level then is random */
770                                                 store_height(ii, jj, randint1(maxsize));
771                                         }
772                                         else
773                                         {
774                                                 /* Cache reused values. */
775                                                 xm = fill_data.xmin + (i - xhstep) / 256;
776                                                 xp = fill_data.xmin + (i + xhstep) / 256;
777                                                 ym = fill_data.ymin + (j - yhstep) / 256;
778                                                 yp = fill_data.ymin + (j + yhstep) / 256;
779
780                                                 /* 
781                                                  * Average over all four corners + scale by diagsize to
782                                                  * reduce the effect of the square grid on the shape of the fractal
783                                                  */
784                                                 store_height(ii, jj,
785                                                         (cave[ym][xm].feat + cave[yp][xm].feat
786                                                         + cave[ym][xp].feat + cave[yp][xp].feat) / 4
787                                                         + (randint1(xstep2) - xhstep2) * (diagsize / 16) / 256 * roug);
788                                         }
789                                 }
790                         }
791                 }
792         }
793 }
794
795
796 static bool hack_isnt_wall(POSITION y, POSITION x, int c1, int c2, int c3, int feat1, int feat2, int feat3, int info1, int info2, int info3)
797 {
798         /*
799          * function used to convert from height-map back to the
800          *  normal angband cave format
801          */
802         if (cave[y][x].info & CAVE_ICKY)
803         {
804                 /* already done */
805                 return FALSE;
806         }
807         else
808         {
809                 /* Show that have looked at this square */
810                 cave[y][x].info|= (CAVE_ICKY);
811
812                 /* Use cutoffs c1-c3 to allocate regions of floor /water/ lava etc. */
813                 if (cave[y][x].feat <= c1)
814                 {
815                         /* 25% of the time use the other tile : it looks better this way */
816                         if (randint1(100) < 75)
817                         {
818                                 cave[y][x].feat = (s16b)feat1;
819                                 cave[y][x].info &= ~(CAVE_MASK);
820                                 cave[y][x].info |= info1;
821                                 return TRUE;
822                         }
823                         else
824                         {
825                                 cave[y][x].feat = (s16b)feat2;
826                                 cave[y][x].info &= ~(CAVE_MASK);
827                                 cave[y][x].info |= info2;
828                                 return TRUE;
829                         }
830                 }
831                 else if (cave[y][x].feat <= c2)
832                 {
833                         /* 25% of the time use the other tile : it looks better this way */
834                         if (randint1(100) < 75)
835                         {
836                                 cave[y][x].feat = (s16b)feat2;
837                                 cave[y][x].info &= ~(CAVE_MASK);
838                                 cave[y][x].info |= info2;
839                                 return TRUE;
840                         }
841                         else
842                         {
843                                 cave[y][x].feat = (s16b)feat1;
844                                 cave[y][x].info &= ~(CAVE_MASK);
845                                 cave[y][x].info |= info1;
846                                 return TRUE;
847                         }
848                 }
849                 else if (cave[y][x].feat <= c3)
850                 {
851                         cave[y][x].feat = (s16b)feat3;
852                         cave[y][x].info &= ~(CAVE_MASK);
853                         cave[y][x].info |= info3;
854                         return TRUE;
855                 }
856                 /* if greater than cutoff then is a wall */
857                 else
858                 {
859                         place_outer_bold(y, x);
860                         return FALSE;
861                 }
862         }
863 }
864
865
866
867
868 /*
869  * Quick and nasty fill routine used to find the connected region
870  * of floor in the middle of the cave
871  */
872 static void cave_fill(POSITION y, POSITION x)
873 {
874         int i, j, d;
875         int ty, tx;
876
877         int flow_tail = 1;
878         int flow_head = 0;
879
880
881         /*** Start Grid ***/
882
883         /* Enqueue that entry */
884         temp_y[0] = y;
885         temp_x[0] = x;
886
887
888         /* Now process the queue */
889         while (flow_head != flow_tail)
890         {
891                 /* Extract the next entry */
892                 ty = temp_y[flow_head];
893                 tx = temp_x[flow_head];
894
895                 /* Forget that entry */
896                 if (++flow_head == TEMP_MAX) flow_head = 0;
897
898                 /* Add the "children" */
899                 for (d = 0; d < 8; d++)
900                 {
901                         int old_head = flow_tail;
902
903                         /* Child location */
904                         j = ty + ddy_ddd[d];
905                         i = tx + ddx_ddd[d];
906
907                         /* Paranoia Don't leave the cave */
908                         if (!in_bounds(j, i))
909                         {
910                                 /* affect boundary */
911                                 cave[j][i].info |= CAVE_ICKY;
912 /*                              return; */
913                         }
914
915                         /* If within bounds */
916                         else if ((i > fill_data.xmin) && (i < fill_data.xmax)
917                                 && (j > fill_data.ymin) && (j < fill_data.ymax))
918                         {
919                                 /* If not a wall or floor done before */
920                                 if (hack_isnt_wall(j, i,
921                                         fill_data.c1, fill_data.c2, fill_data.c3,
922                                         fill_data.feat1, fill_data.feat2, fill_data.feat3,
923                                         fill_data.info1, fill_data.info2, fill_data.info3))
924                                 {
925                                         /* Enqueue that entry */
926                                         temp_y[flow_tail] = (byte_hack)j;
927                                         temp_x[flow_tail] = (byte_hack)i;
928
929                                         /* Advance the queue */
930                                         if (++flow_tail == TEMP_MAX) flow_tail = 0;
931
932                                         /* Hack -- Overflow by forgetting new entry */
933                                         if (flow_tail == flow_head)
934                                         {
935                                                 flow_tail = old_head;
936                                         }
937                                         else
938                                         {
939                                                 /* keep tally of size of cave system */
940                                                 (fill_data.amount)++;
941                                         }
942                                 }
943                         }
944                         else
945                         {
946                                 /* affect boundary */
947                                 cave[j][i].info |= CAVE_ICKY;
948                         }
949                 }
950         }
951 }
952
953
954 bool generate_fracave(POSITION y0, POSITION x0, POSITION xsize, POSITION ysize, int cutoff, bool light, bool room)
955 {
956         POSITION x, y, xhsize, yhsize;
957         int i;
958
959         /* offsets to middle from corner */
960         xhsize = xsize / 2;
961         yhsize = ysize / 2;
962
963
964         /*
965          * select region connected to center of cave system
966          * this gets rid of alot of isolated one-sqaures that
967          * can make teleport traps instadeaths...
968          */
969
970         /* cutoffs */
971         fill_data.c1 = cutoff;
972         fill_data.c2 = 0;
973         fill_data.c3 = 0;
974
975         /* features to fill with */
976         fill_data.feat1 = floor_type[randint0(100)];
977         fill_data.feat2 = floor_type[randint0(100)];
978         fill_data.feat3 = floor_type[randint0(100)];
979
980         fill_data.info1 = CAVE_FLOOR;
981         fill_data.info2 = CAVE_FLOOR;
982         fill_data.info3 = CAVE_FLOOR;
983
984         /* number of filled squares */
985         fill_data.amount = 0;
986
987         cave_fill((byte)y0, (byte)x0);
988
989         /* if tally too small, try again */
990         if (fill_data.amount < 10)
991         {
992                 /* too small - clear area and try again later */
993                 for (x = 0; x <= xsize; ++x)
994                 {
995                         for (y = 0; y <= ysize; ++y)
996                         {
997                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
998                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
999                         }
1000                 }
1001                 return FALSE;
1002         }
1003
1004         /*
1005          * Do boundarys-check to see if they are next to a filled region
1006          * If not then they are set to normal granite
1007          * If so then they are marked as room walls.
1008          */
1009         for (i = 0; i <= xsize; ++i)
1010         {
1011                 /* top boundary */
1012                 if ((cave[0 + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
1013                 {
1014                         /* Next to a 'filled' region? - set to be room walls */
1015                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1016                         if (light) cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_GLOW);
1017                         cave[y0 + 0 - yhsize][x0 + i - xhsize].info |= (CAVE_ROOM);
1018                         place_outer_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1019                 }
1020                 else
1021                 {
1022                         /* set to be normal granite */
1023                         place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1024                 }
1025
1026                 /* bottom boundary */
1027                 if ((cave[ysize + y0 - yhsize][i + x0 - xhsize].info & CAVE_ICKY) && (room))
1028                 {
1029                         /* Next to a 'filled' region? - set to be room walls */
1030                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1031                         if (light) cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_GLOW);
1032                         cave[y0 + ysize - yhsize][x0 + i - xhsize].info|=(CAVE_ROOM);
1033                         place_outer_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1034                 }
1035                 else
1036                 {
1037                         /* set to be normal granite */
1038                         place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1039                 }
1040
1041                 /* clear the icky flag-don't need it any more */
1042                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1043                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1044         }
1045
1046         /* Do the left and right boundaries minus the corners (done above) */
1047         for (i = 1; i < ysize; ++i)
1048         {
1049                 /* left boundary */
1050                 if ((cave[i + y0 - yhsize][0 + x0 - xhsize].info & CAVE_ICKY) && room)
1051                 {
1052                         /* room boundary */
1053                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1054                         if (light) cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_GLOW);
1055                         cave[y0 + i - yhsize][x0 + 0 - xhsize].info |= (CAVE_ROOM);
1056                         place_outer_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1057                 }
1058                 else
1059                 {
1060                         /* outside room */
1061                         place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1062                 }
1063                 /* right boundary */
1064                 if ((cave[i + y0 - yhsize][xsize + x0 - xhsize].info & CAVE_ICKY) && room)
1065                 {
1066                         /* room boundary */
1067                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1068                         if (light) cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_GLOW);
1069                         cave[y0 + i - yhsize][x0 + xsize - xhsize].info |= (CAVE_ROOM);
1070                         place_outer_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1071                 }
1072                 else
1073                 {
1074                         /* outside room */
1075                         place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1076                 }
1077
1078                 /* clear icky flag -done with it */
1079                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1080                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1081         }
1082
1083
1084         /* Do the rest: convert back to the normal format */
1085         for (x = 1; x < xsize; ++x)
1086         {
1087                 for (y = 1; y < ysize; ++y)
1088                 {
1089                         if (is_floor_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1090                             (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1091                         {
1092                                 /* Clear the icky flag in the filled region */
1093                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~CAVE_ICKY;
1094
1095                                 /* Set appropriate flags */
1096                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1097                                 if (room) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1098                         }
1099                         else if (is_outer_bold(y0 + y - yhsize, x0 + x - xhsize) &&
1100                                  (cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY))
1101                         {
1102                                 /* Walls */
1103                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1104                                 if (light) cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_GLOW);
1105                                 if (room)
1106                                 {
1107                                         cave[y0 + y - yhsize][x0 + x - xhsize].info |= (CAVE_ROOM);
1108                                 }
1109                                 else
1110                                 {
1111
1112                                         place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1113                                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ROOM);
1114                                 }
1115                         }
1116                         else
1117                         {
1118                                 /* Clear the unconnected regions */
1119                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1120                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1121                         }
1122                 }
1123         }
1124
1125         /*
1126          * There is a slight problem when tunnels pierce the caves:
1127          * Extra doors appear inside the system.  (Its not very noticeable though.)
1128          * This can be removed by "filling" from the outside in.  This allows a separation
1129          * from _outer_ with _inner_.  (Internal walls are  _outer_ instead.)
1130          * The extra effort for what seems to be only a minor thing (even non-existant if you
1131          * think of the caves not as normal rooms, but as holes in the dungeon), doesn't seem
1132          * worth it.
1133          */
1134
1135         return TRUE;
1136 }
1137
1138
1139 #ifdef ALLOW_CAVERNS_AND_LAKES
1140 /*
1141  * Builds a cave system in the center of the dungeon.
1142  */
1143 void build_cavern(void)
1144 {
1145         int grd, roug, cutoff, xsize, ysize, x0, y0;
1146         bool done, light;
1147
1148         light = done = FALSE;
1149         if ((dun_level <= randint1(50)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS)) light = TRUE;
1150
1151         /* Make a cave the size of the dungeon */
1152         xsize = cur_wid - 1;
1153         ysize = cur_hgt - 1;
1154         x0 = xsize / 2;
1155         y0 = ysize / 2;
1156
1157         /* Paranoia: make size even */
1158         xsize = x0 * 2;
1159         ysize = y0 * 2;
1160
1161         while (!done)
1162         {
1163                 /* testing values for these parameters: feel free to adjust */
1164                 grd = randint1(4) + 4;
1165
1166                 /* want average of about 16 */
1167                 roug = randint1(8) * randint1(4);
1168
1169                 /* about size/2 */
1170                 cutoff = xsize / 2;
1171
1172                  /* make it */
1173                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
1174
1175                 /* Convert to normal format+ clean up */
1176                 done = generate_fracave(y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
1177         }
1178 }
1179
1180 bool generate_lake(POSITION y0, POSITION x0, POSITION xsize, POSITION ysize, int c1, int c2, int c3, int type)
1181 {
1182         POSITION x, y, xhsize, yhsize;
1183         int i;
1184         FEAT_IDX feat1, feat2, feat3;
1185
1186         /* offsets to middle from corner */
1187         xhsize = xsize / 2;
1188         yhsize = ysize / 2;
1189
1190         /* Get features based on type */
1191         switch (type)
1192         {
1193         case LAKE_T_LAVA: /* Lava */
1194                 feat1 = feat_deep_lava;
1195                 feat2 = feat_shallow_lava;
1196                 feat3 = floor_type[randint0(100)];
1197                 break;
1198         case LAKE_T_WATER: /* Water */
1199                 feat1 = feat_deep_water;
1200                 feat2 = feat_shallow_water;
1201                 feat3 = floor_type[randint0(100)];
1202                 break;
1203         case LAKE_T_CAVE: /* Collapsed cave */
1204                 feat1 = floor_type[randint0(100)];
1205                 feat2 = floor_type[randint0(100)];
1206                 feat3 = feat_rubble;
1207                 break;
1208         case LAKE_T_EARTH_VAULT: /* Earth vault */
1209                 feat1 = feat_rubble;
1210                 feat2 = floor_type[randint0(100)];
1211                 feat3 = feat_rubble;
1212                 break;
1213         case LAKE_T_AIR_VAULT: /* Air vault */
1214                 feat1 = feat_grass;
1215                 feat2 = feat_tree;
1216                 feat3 = feat_grass;
1217                 break;
1218         case LAKE_T_WATER_VAULT: /* Water vault */
1219                 feat1 = feat_shallow_water;
1220                 feat2 = feat_deep_water;
1221                 feat3 = feat_shallow_water;
1222                 break;
1223         case LAKE_T_FIRE_VAULT: /* Fire Vault */
1224                 feat1 = feat_shallow_lava;
1225                 feat2 = feat_deep_lava;
1226                 feat3 = feat_shallow_lava;
1227                 break;
1228
1229         /* Paranoia */
1230         default: return FALSE;
1231         }
1232
1233         /*
1234          * select region connected to center of cave system
1235          * this gets rid of alot of isolated one-sqaures that
1236          * can make teleport traps instadeaths...
1237          */
1238
1239         /* cutoffs */
1240         fill_data.c1 = c1;
1241         fill_data.c2 = c2;
1242         fill_data.c3 = c3;
1243
1244         /* features to fill with */
1245         fill_data.feat1 = feat1;
1246         fill_data.feat2 = feat2;
1247         fill_data.feat3 = feat3;
1248
1249         fill_data.info1 = 0;
1250         fill_data.info2 = 0;
1251         fill_data.info3 = 0;
1252
1253         /* number of filled squares */
1254         fill_data.amount = 0;
1255
1256         /* select region connected to center of cave system
1257         * this gets rid of alot of isolated one-sqaures that
1258         * can make teleport traps instadeaths... */
1259         cave_fill((byte)y0, (byte)x0);
1260
1261         /* if tally too small, try again */
1262         if (fill_data.amount < 10)
1263         {
1264                 /* too small -clear area and try again later */
1265                 for (x = 0; x <= xsize; ++x)
1266                 {
1267                         for (y = 0; y <= ysize; ++y)
1268                         {
1269                                 place_floor_bold(y0 + y - yhsize, x0 + x - xhsize);
1270                                 cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1271                         }
1272                 }
1273                 return FALSE;
1274         }
1275
1276         /* Do boundarys- set to normal granite */
1277         for (i = 0; i <= xsize; ++i)
1278         {
1279                 place_extra_bold(y0 + 0 - yhsize, x0 + i - xhsize);
1280                 place_extra_bold(y0 + ysize - yhsize, x0 + i - xhsize);
1281
1282                 /* clear the icky flag-don't need it any more */
1283                 cave[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1284                 cave[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1285         }
1286
1287         /* Do the left and right boundaries minus the corners (done above) */
1288
1289         for (i = 1; i < ysize; ++i)
1290         {
1291                 place_extra_bold(y0 + i - yhsize, x0 + 0 - xhsize);
1292                 place_extra_bold(y0 + i - yhsize, x0 + xsize - xhsize);
1293
1294                 /* clear icky flag -done with it */
1295                 cave[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1296                 cave[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1297         }
1298
1299
1300         /* Do the rest: convert back to the normal format */
1301         for (x = 1; x < xsize; ++x)
1302         {
1303                 for (y = 1; y < ysize; ++y)
1304                 {
1305                         /* Fill unconnected regions with granite */
1306                         if ((!(cave[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
1307                                 is_outer_bold(y0 + y - yhsize, x0 + x - xhsize))
1308                                 place_extra_bold(y0 + y - yhsize, x0 + x - xhsize);
1309
1310                         /* turn off icky flag (no longer needed.) */
1311                         cave[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1312
1313                         /* Light lava */
1314                         if (cave_have_flag_bold(y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
1315                         {
1316                                 if (!(d_info[dungeon_type].flags1 & DF1_DARKNESS)) cave[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
1317                         }
1318                 }
1319         }
1320
1321         return TRUE;
1322 }
1323
1324
1325 /*
1326  * makes a lake/collapsed cave system in the center of the dungeon
1327  */
1328 void build_lake(int type)
1329 {
1330         int grd, roug, xsize, ysize, x0, y0;
1331         bool done = FALSE;
1332         int c1, c2, c3;
1333
1334         /* paranoia - exit if lake type out of range. */
1335         if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
1336         {
1337                 msg_format("Invalid lake type (%d)", type);
1338                 return;
1339         }
1340
1341         /* Make the size of the dungeon */
1342         xsize = cur_wid - 1;
1343         ysize = cur_hgt - 1;
1344         x0 = xsize / 2;
1345         y0 = ysize / 2;
1346
1347         /* Paranoia: make size even */
1348         xsize = x0 * 2;
1349         ysize = y0 * 2;
1350
1351         while (!done)
1352         {
1353                 /* testing values for these parameters: feel free to adjust */
1354                 grd = randint1(3) + 4;
1355
1356                 /* want average of about 16 */
1357                 roug = randint1(8) * randint1(4);
1358
1359                 /* Make up size of various componants */
1360                 /* Floor */
1361                 c3 = 3 * xsize / 4;
1362
1363                 /* Deep water/lava */
1364                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
1365
1366                 /* Shallow boundary */
1367                 c2 = (c1 + c3) / 2;
1368
1369                 /* make it */
1370                 generate_hmap(y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
1371
1372                 /* Convert to normal format+ clean up */
1373                 done = generate_lake(y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
1374         }
1375 }
1376 #endif /* ALLOW_CAVERNS_AND_LAKES */
1377
1378
1379
1380 /*
1381  * Routine that fills the empty areas of a room with treasure and monsters.
1382  */
1383 void fill_treasure(POSITION x1, POSITION x2, POSITION y1, POSITION y2, int difficulty)
1384 {
1385         POSITION x, y, cx, cy, size;
1386         s32b value;
1387
1388         /* center of room:*/
1389         cx = (x1 + x2) / 2;
1390         cy = (y1 + y2) / 2;
1391
1392         /* Rough measure of size of vault= sum of lengths of sides */
1393         size = abs(x2 - x1) + abs(y2 - y1);
1394
1395         for (x = x1; x <= x2; x++)
1396         {
1397                 for (y = y1; y <= y2; y++)
1398                 {
1399                         /* Thing added based on distance to center of vault
1400                          * Difficulty is 1-easy to 10-hard */
1401                         value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
1402
1403                         /* hack- empty square part of the time */
1404                         if ((randint1(100) - difficulty * 3) > 50) value = 20;
1405
1406                          /* if floor, shallow water and lava */
1407                         if (is_floor_bold(y, x) ||
1408                             (cave_have_flag_bold(y, x, FF_PLACE) && cave_have_flag_bold(y, x, FF_DROP)))
1409                         {
1410                                 /* The smaller 'value' is, the better the stuff */
1411                                 if (value < 0)
1412                                 {
1413                                         /* Meanest monster + treasure */
1414                                         monster_level = base_level + 40;
1415                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1416                                         monster_level = base_level;
1417                                         object_level = base_level + 20;
1418                                         place_object(y, x, AM_GOOD);
1419                                         object_level = base_level;
1420                                 }
1421                                 else if (value < 5)
1422                                 {
1423                                         /* Mean monster +treasure */
1424                                         monster_level = base_level + 20;
1425                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1426                                         monster_level = base_level;
1427                                         object_level = base_level + 10;
1428                                         place_object(y, x, AM_GOOD);
1429                                         object_level = base_level;
1430                                 }
1431                                 else if (value < 10)
1432                                 {
1433                                         /* Monster */
1434                                         monster_level = base_level + 9;
1435                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1436                                         monster_level = base_level;
1437                                 }
1438                                 else if (value < 17)
1439                                 {
1440                                         /* Intentional Blank space */
1441
1442                                         /*
1443                                          * (Want some of the vault to be empty
1444                                          * so have room for group monsters.
1445                                          * This is used in the hack above to lower
1446                                          * the density of stuff in the vault.)
1447                                          */
1448                                 }
1449                                 else if (value < 23)
1450                                 {
1451                                         /* Object or trap */
1452                                         if (randint0(100) < 25)
1453                                         {
1454                                                 place_object(y, x, 0L);
1455                                         }
1456                                         else
1457                                         {
1458                                                 place_trap(y, x);
1459                                         }
1460                                 }
1461                                 else if (value < 30)
1462                                 {
1463                                         /* Monster and trap */
1464                                         monster_level = base_level + 5;
1465                                         place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1466                                         monster_level = base_level;
1467                                         place_trap(y, x);
1468                                 }
1469                                 else if (value < 40)
1470                                 {
1471                                         /* Monster or object */
1472                                         if (randint0(100) < 50)
1473                                         {
1474                                                 monster_level = base_level + 3;
1475                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1476                                                 monster_level = base_level;
1477                                         }
1478                                         if (randint0(100) < 50)
1479                                         {
1480                                                 object_level = base_level + 7;
1481                                                 place_object(y, x, 0L);
1482                                                 object_level = base_level;
1483                                         }
1484                                 }
1485                                 else if (value < 50)
1486                                 {
1487                                         /* Trap */
1488                                         place_trap(y, x);
1489                                 }
1490                                 else
1491                                 {
1492                                         /* Various Stuff */
1493
1494                                         /* 20% monster, 40% trap, 20% object, 20% blank space */
1495                                         if (randint0(100) < 20)
1496                                         {
1497                                                 place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1498                                         }
1499                                         else if (randint0(100) < 50)
1500                                         {
1501                                                 place_trap(y, x);
1502                                         }
1503                                         else if (randint0(100) < 50)
1504                                         {
1505                                                 place_object(y, x, 0L);
1506                                         }
1507                                 }
1508
1509                         }
1510                 }
1511         }
1512 }
1513
1514
1515
1516 /*
1517  * Overlay a rectangular room given its bounds
1518  * This routine is used by build_room_vault
1519  * The area inside the walls is not touched:
1520  * only granite is removed- normal walls stay
1521  */
1522 void build_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2)
1523 {
1524         POSITION x, y, xsize, ysize;
1525         int i, temp;
1526
1527         /* Check if rectangle has no width */
1528         if ((x1 == x2) || (y1 == y2)) return;
1529
1530         if (x1 > x2)
1531         {
1532                 /* Swap boundaries if in wrong order */
1533                 temp = x1;
1534                 x1 = x2;
1535                 x2 = temp;
1536         }
1537
1538         if (y1 > y2)
1539         {
1540                 /* Swap boundaries if in wrong order */
1541                 temp = y1;
1542                 y1 = y2;
1543                 y2 = temp;
1544         }
1545
1546         /* get total widths */
1547         xsize = x2 - x1;
1548         ysize = y2 - y1;
1549
1550
1551         /* Top and bottom boundaries */
1552         for (i = 0; i <= xsize; i++)
1553         {
1554                 place_outer_noperm_bold(y1, x1 + i);
1555                 cave[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1556                 place_outer_noperm_bold(y2, x1 + i);
1557                 cave[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1558         }
1559
1560         /* Left and right boundaries */
1561         for (i = 1; i < ysize; i++)
1562         {
1563                 place_outer_noperm_bold(y1 + i, x1);
1564                 cave[y1 + i][x1].info|=(CAVE_ROOM | CAVE_ICKY);
1565                 place_outer_noperm_bold(y1 + i, x2);
1566                 cave[y1 + i][x2].info|=(CAVE_ROOM | CAVE_ICKY);
1567         }
1568
1569         /* Middle */
1570         for (x = 1; x < xsize; x++)
1571         {
1572                 for (y = 1; y < ysize; y++)
1573                 {
1574                         if (is_extra_bold(y1+y, x1+x))
1575                         {
1576                                 /* clear the untouched region */
1577                                 place_floor_bold(y1 + y, x1 + x);
1578                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1579                         }
1580                         else
1581                         {
1582                                 /* make it a room- but don't touch */
1583                                 cave[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1584                         }
1585                 }
1586         }
1587 }
1588
1589
1590
1591 /*
1592  * maze vault -- rectangular labyrinthine rooms
1593  *
1594  * maze vault uses two routines:
1595  *    r_visit - a recursive routine that builds the labyrinth
1596  *    build_maze_vault - a driver routine that calls r_visit and adds
1597  *                   monsters, traps and treasure
1598  *
1599  * The labyrinth is built by creating a spanning tree of a graph.
1600  * The graph vertices are at
1601  *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
1602  * and the edges are the vertical and horizontal nearest neighbors.
1603  *
1604  * The spanning tree is created by performing a suitably randomized
1605  * depth-first traversal of the graph. The only adjustable parameter
1606  * is the randint0(3) below; it governs the relative density of
1607  * twists and turns in the labyrinth: smaller number, more twists.
1608  */
1609 void r_visit(POSITION y1, POSITION x1, POSITION y2, POSITION x2, int node, DIRECTION dir, int *visited)
1610 {
1611         int i, j, m, n, temp, x, y, adj[4];
1612
1613         /* dimensions of vertex array */
1614         m = (x2 - x1) / 2 + 1;
1615         n = (y2 - y1) / 2 + 1;
1616
1617         /* mark node visited and set it to a floor */
1618         visited[node] = 1;
1619         x = 2 * (node % m) + x1;
1620         y = 2 * (node / m) + y1;
1621         place_floor_bold(y, x);
1622
1623         /* setup order of adjacent node visits */
1624         if (one_in_(3))
1625         {
1626                 /* pick a random ordering */
1627                 for (i = 0; i < 4; i++)
1628                         adj[i] = i;
1629                 for (i = 0; i < 4; i++)
1630                 {
1631                         j = randint0(4);
1632                         temp = adj[i];
1633                         adj[i] = adj[j];
1634                         adj[j] = temp;
1635                 }
1636                 dir = adj[0];
1637         }
1638         else
1639         {
1640                 /* pick a random ordering with dir first */
1641                 adj[0] = dir;
1642                 for (i = 1; i < 4; i++)
1643                         adj[i] = i;
1644                 for (i = 1; i < 4; i++)
1645                 {
1646                         j = 1 + randint0(3);
1647                         temp = adj[i];
1648                         adj[i] = adj[j];
1649                         adj[j] = temp;
1650                 }
1651         }
1652
1653         for (i = 0; i < 4; i++)
1654         {
1655                 switch (adj[i])
1656                 {
1657                         case 0:
1658                                 /* (0,+) - check for bottom boundary */
1659                                 if ((node / m < n - 1) && (visited[node + m] == 0))
1660                                 {
1661                                         place_floor_bold(y + 1, x);
1662                                         r_visit(y1, x1, y2, x2, node + m, dir, visited);
1663                                 }
1664                                 break;
1665                         case 1:
1666                                 /* (0,-) - check for top boundary */
1667                                 if ((node / m > 0) && (visited[node - m] == 0))
1668                                 {
1669                                         place_floor_bold(y - 1, x);
1670                                         r_visit(y1, x1, y2, x2, node - m, dir, visited);
1671                                 }
1672                                 break;
1673                         case 2:
1674                                 /* (+,0) - check for right boundary */
1675                                 if ((node % m < m - 1) && (visited[node + 1] == 0))
1676                                 {
1677                                         place_floor_bold(y, x + 1);
1678                                         r_visit(y1, x1, y2, x2, node + 1, dir, visited);
1679                                 }
1680                                 break;
1681                         case 3:
1682                                 /* (-,0) - check for left boundary */
1683                                 if ((node % m > 0) && (visited[node - 1] == 0))
1684                                 {
1685                                         place_floor_bold(y, x - 1);
1686                                         r_visit(y1, x1, y2, x2, node - 1, dir, visited);
1687                                 }
1688                 } /* end switch */
1689         }
1690 }
1691
1692
1693 void build_maze_vault(POSITION x0, POSITION y0, POSITION xsize, POSITION ysize, bool is_vault)
1694 {
1695         POSITION y, x, dy, dx;
1696         POSITION y1, x1, y2, x2;
1697         int m, n, num_vertices, *visited;
1698         bool light;
1699         cave_type *c_ptr;
1700
1701         msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
1702
1703         /* Choose lite or dark */
1704         light = ((dun_level <= randint1(25)) && is_vault && !(d_info[dungeon_type].flags1 & DF1_DARKNESS));
1705
1706         /* Pick a random room size - randomized by calling routine */
1707         dy = ysize / 2 - 1;
1708         dx = xsize / 2 - 1;
1709
1710         y1 = y0 - dy;
1711         x1 = x0 - dx;
1712         y2 = y0 + dy;
1713         x2 = x0 + dx;
1714
1715         /* generate the room */
1716         for (y = y1 - 1; y <= y2 + 1; y++)
1717         {
1718                 for (x = x1 - 1; x <= x2 + 1; x++)
1719                 {
1720                         c_ptr = &cave[y][x];
1721                         c_ptr->info |= CAVE_ROOM;
1722                         if (is_vault) c_ptr->info |= CAVE_ICKY;
1723                         if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
1724                         {
1725                                 place_outer_grid(c_ptr);
1726                         }
1727                         else if (!is_vault)
1728                         {
1729                                 place_extra_grid(c_ptr);
1730                         }
1731                         else
1732                         {
1733                                 place_inner_grid(c_ptr);
1734                         }
1735                         if (light) c_ptr->info |= (CAVE_GLOW);
1736                 }
1737         }
1738
1739         /* dimensions of vertex array */
1740         m = dx + 1;
1741         n = dy + 1;
1742         num_vertices = m * n;
1743
1744         /* initialize array of visited vertices */
1745         C_MAKE(visited, num_vertices, int);
1746
1747         /* traverse the graph to create a spaning tree, pick a random root */
1748         r_visit(y1, x1, y2, x2, randint0(num_vertices), 0, visited);
1749
1750         /* Fill with monsters and treasure, low difficulty */
1751         if (is_vault) fill_treasure(x1, x2, y1, y2, randint1(5));
1752
1753         C_KILL(visited, num_vertices, int);
1754 }
1755
1756
1757 /* Build a town/ castle by using a recursive algorithm.
1758  * Basically divide each region in a probalistic way to create
1759  * smaller regions.  When the regions get too small stop.
1760  *
1761  * The power variable is a measure of how well defended a region is.
1762  * This alters the possible choices.
1763  */
1764 void build_recursive_room(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int power)
1765 {
1766         POSITION xsize, ysize;
1767         POSITION x, y;
1768         int choice;
1769
1770         /* Temp variables */
1771         int t1, t2, t3, t4;
1772
1773         xsize = x2 - x1;
1774         ysize = y2 - y1;
1775
1776         if ((power < 3) && (xsize > 12) && (ysize > 12))
1777         {
1778                 /* Need outside wall +keep */
1779                 choice = 1;
1780         }
1781         else
1782         {
1783                 if (power < 10)
1784                 {
1785                         /* Make rooms + subdivide */
1786                         if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
1787                         {
1788                                 choice = 4;
1789                         }
1790                         else
1791                         {
1792                                 choice = randint1(2) + 1;
1793                         }
1794                 }
1795                 else
1796                 {
1797                         /* Mostly subdivide */
1798                         choice = randint1(3) + 1;
1799                 }
1800         }
1801
1802         /* Based on the choice made above, do something */
1803
1804         switch (choice)
1805         {
1806                 case 1:
1807                 {
1808                         /* Outer walls */
1809
1810                         /* top and bottom */
1811                         for (x = x1; x <= x2; x++)
1812                         {
1813                                 place_outer_bold(y1, x);
1814                                 place_outer_bold(y2, x);
1815                         }
1816
1817                         /* left and right */
1818                         for (y = y1 + 1; y < y2; y++)
1819                         {
1820                                 place_outer_bold(y, x1);
1821                                 place_outer_bold(y, x2);
1822                         }
1823
1824                         /* Make a couple of entrances */
1825                         if (one_in_(2))
1826                         {
1827                                 /* left and right */
1828                                 y = randint1(ysize) + y1;
1829                                 place_floor_bold(y, x1);
1830                                 place_floor_bold(y, x2);
1831                         }
1832                         else
1833                         {
1834                                 /* top and bottom */
1835                                 x = randint1(xsize) + x1;
1836                                 place_floor_bold(y1, x);
1837                                 place_floor_bold(y2, x);
1838                         }
1839
1840                         /* Select size of keep */
1841                         t1 = randint1(ysize / 3) + y1;
1842                         t2 = y2 - randint1(ysize / 3);
1843                         t3 = randint1(xsize / 3) + x1;
1844                         t4 = x2 - randint1(xsize / 3);
1845
1846                         /* Do outside areas */
1847
1848                         /* Above and below keep */
1849                         build_recursive_room(x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
1850                         build_recursive_room(x1 + 1, t2, x2 - 1, y2, power + 1);
1851
1852                         /* Left and right of keep */
1853                         build_recursive_room(x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
1854                         build_recursive_room(t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
1855
1856                         /* Make the keep itself: */
1857                         x1 = t3;
1858                         x2 = t4;
1859                         y1 = t1;
1860                         y2 = t2;
1861                         xsize = x2 - x1;
1862                         ysize = y2 - y1;
1863                         power += 2;
1864
1865                         /* Fall through */
1866                 }
1867                 case 4:
1868                 {
1869                         /* Try to build a room */
1870                         if ((xsize < 3) || (ysize < 3))
1871                         {
1872                                 for (y = y1; y < y2; y++)
1873                                 {
1874                                         for (x = x1; x < x2; x++)
1875                                         {
1876                                                 place_inner_bold(y, x);
1877                                         }
1878                                 }
1879
1880                                 /* Too small */
1881                                 return;
1882                         }
1883
1884                         /* Make outside walls */
1885                         /* top and bottom */
1886                         for (x = x1 + 1; x <= x2 - 1; x++)
1887                         {
1888                                 place_inner_bold(y1 + 1, x);
1889                                 place_inner_bold(y2 - 1, x);
1890                         }
1891
1892                         /* left and right */
1893                         for (y = y1 + 1; y <= y2 - 1; y++)
1894                         {
1895                                 place_inner_bold(y, x1 + 1);
1896                                 place_inner_bold(y, x2 - 1);
1897                         }
1898
1899                         /* Make a door */
1900                         y = randint1(ysize - 3) + y1 + 1;
1901
1902                         if (one_in_(2))
1903                         {
1904                                 /* left */
1905                                 place_floor_bold(y, x1 + 1);
1906                         }
1907                         else
1908                         {
1909                                 /* right */
1910                                 place_floor_bold(y, x2 - 1);
1911                         }
1912
1913                         /* Build the room */
1914                         build_recursive_room(x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
1915                         break;
1916                 }
1917                 case 2:
1918                 {
1919                         /* Try and divide vertically */
1920                         if (xsize < 3)
1921                         {
1922                                 /* Too small */
1923                                 for (y = y1; y < y2; y++)
1924                                 {
1925                                         for (x = x1; x < x2; x++)
1926                                         {
1927                                                 place_inner_bold(y, x);
1928                                         }
1929                                 }
1930                                 return;
1931                         }
1932
1933                         t1 = randint1(xsize - 2) + x1 + 1;
1934                         build_recursive_room(x1, y1, t1, y2, power - 2);
1935                         build_recursive_room(t1 + 1, y1, x2, y2, power - 2);
1936                         break;
1937                 }
1938                 case 3:
1939                 {
1940                         /* Try and divide horizontally */
1941                         if (ysize < 3)
1942                         {
1943                                 /* Too small */
1944                                 for (y = y1; y < y2; y++)
1945                                 {
1946                                         for (x = x1; x < x2; x++)
1947                                         {
1948                                                 place_inner_bold(y, x);
1949                                         }
1950                                 }
1951                                 return;
1952                         }
1953
1954                         t1 = randint1(ysize - 2) + y1 + 1;
1955                         build_recursive_room(x1, y1, x2, t1, power - 2);
1956                         build_recursive_room(x1, t1 + 1, x2, y2, power - 2);
1957                         break;
1958                 }
1959         }
1960 }
1961
1962
1963 /*
1964  * Add outer wall to a floored region
1965  * Note: no range checking is done so must be inside dungeon
1966  * This routine also stomps on doors
1967  */
1968 void add_outer_wall(POSITION x, POSITION y, int light, POSITION x1, POSITION y1, POSITION x2, POSITION y2)
1969 {
1970         cave_type *c_ptr;
1971         feature_type *f_ptr;
1972         int i, j;
1973
1974         if (!in_bounds(y, x)) return;
1975
1976         c_ptr = &cave[y][x];
1977
1978         /* hack- check to see if square has been visited before
1979         * if so, then exit (use room flag to do this) */
1980         if (c_ptr->info & CAVE_ROOM) return;
1981
1982         /* set room flag */
1983         c_ptr->info |= CAVE_ROOM;
1984
1985         f_ptr = &f_info[c_ptr->feat];
1986
1987         if (is_floor_bold(y, x))
1988         {
1989                 for (i = -1; i <= 1; i++)
1990                 {
1991                         for (j = -1; j <= 1; j++)
1992                         {
1993                                 if ((x + i >= x1) && (x + i <= x2) &&
1994                                          (y + j >= y1) && (y + j <= y2))
1995                                 {
1996                                         add_outer_wall(x + i, y + j, light, x1, y1, x2, y2);
1997                                         if (light) c_ptr->info |= CAVE_GLOW;
1998                                 }
1999                         }
2000                 }
2001         }
2002         else if (is_extra_bold(y, x))
2003         {
2004                 /* Set bounding walls */
2005                 place_outer_bold(y, x);
2006                 if (light) c_ptr->info |= CAVE_GLOW;
2007         }
2008         else if (permanent_wall(f_ptr))
2009         {
2010                 /* Set bounding walls */
2011                 if (light) c_ptr->info |= CAVE_GLOW;
2012         }
2013 }
2014
2015
2016 /*
2017  * Hacked distance formula - gives the 'wrong' answer.
2018  * Used to build crypts
2019  */
2020 POSITION dist2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, POSITION h1, POSITION h2, POSITION h3, POSITION h4)
2021 {
2022         POSITION dx, dy;
2023         dx = abs(x2 - x1);
2024         dy = abs(y2 - y1);
2025
2026         /* Basically this works by taking the normal pythagorean formula
2027          * and using an expansion to express this in a way without the
2028          * square root.  This approximate formula is then perturbed to give
2029          * the distorted results.  (I found this by making a mistake when I was
2030          * trying to fix the circular rooms.)
2031          */
2032
2033         /* h1-h4 are constants that describe the metric */
2034         if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
2035         if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
2036         return (((dx + dy) * 128) / 181 +
2037                 (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
2038         /* 128/181 is approx. 1/sqrt(2) */
2039 }
2040
2041
2042
2043
2044 /* Create a new floor room with optional light */
2045 void generate_room_floor(POSITION y1, POSITION x1, POSITION y2, POSITION x2, int light)
2046 {
2047         POSITION y, x;
2048         
2049         cave_type *c_ptr;
2050
2051         for (y = y1; y <= y2; y++)
2052         {
2053                 for (x = x1; x <= x2; x++)
2054                 {
2055                         /* Point to grid */
2056                         c_ptr = &cave[y][x];
2057                         place_floor_grid(c_ptr);
2058                         c_ptr->info |= (CAVE_ROOM);
2059                         if (light) c_ptr->info |= (CAVE_GLOW);
2060                 }
2061         }
2062 }
2063
2064 void generate_fill_perm_bold(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
2065 {
2066         POSITION y, x;
2067
2068         for (y = y1; y <= y2; y++)
2069         {
2070                 for (x = x1; x <= x2; x++)
2071                 {
2072                         /* Point to grid */
2073                         place_inner_perm_bold(y, x);
2074                 }
2075         }
2076 }
2077
2078
2079 /*!
2080  * @brief 与えられた部屋型IDに応じて部屋の生成処理分岐を行い結果を返す / Attempt to build a room of the given type at the given block
2081  * @param type 部屋型ID
2082  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
2083  * @return 部屋の精製に成功した場合 TRUE を返す。
2084  */
2085 static bool room_build(EFFECT_ID typ)
2086 {
2087         /* Build a room */
2088         switch (typ)
2089         {
2090         /* Build an appropriate room */
2091         case ROOM_T_NORMAL:        return build_type1();
2092         case ROOM_T_OVERLAP:       return build_type2();
2093         case ROOM_T_CROSS:         return build_type3();
2094         case ROOM_T_INNER_FEAT:    return build_type4();
2095         case ROOM_T_NEST:          return build_type5();
2096         case ROOM_T_PIT:           return build_type6();
2097         case ROOM_T_LESSER_VAULT:  return build_type7();
2098         case ROOM_T_GREATER_VAULT: return build_type8();
2099         case ROOM_T_FRACAVE:       return build_type9();
2100         case ROOM_T_RANDOM_VAULT:  return build_type10();
2101         case ROOM_T_OVAL:          return build_type11();
2102         case ROOM_T_CRYPT:         return build_type12();
2103         case ROOM_T_TRAP_PIT:      return build_type13();
2104         case ROOM_T_TRAP:          return build_type14();
2105         case ROOM_T_GLASS:         return build_type15();
2106         case ROOM_T_ARCADE:        return build_type16();
2107         case ROOM_T_FIXED:         return build_type17();
2108         }
2109
2110         /* Paranoia */
2111         return FALSE;
2112 }
2113
2114 /*!
2115  * @brief 指定した部屋の生成確率を別の部屋に加算し、指定した部屋の生成率を0にする
2116  * @param dst 確率を移す先の部屋種ID
2117  * @param src 確率を与える元の部屋種ID
2118  */
2119 #define MOVE_PLIST(dst, src) (prob_list[dst] += prob_list[src], prob_list[src] = 0) 
2120
2121 /*!
2122  * @brief 部屋生成処理のメインルーチン(Sangbandを経由してOangbandからの実装を引用) / Generate rooms in dungeon.  Build bigger rooms at first. [from SAngband (originally from OAngband)]
2123  * @return 部屋生成に成功した場合 TRUE を返す。
2124  */
2125 bool generate_rooms(void)
2126 {
2127         int i;
2128         bool remain;
2129         int crowded = 0;
2130         int total_prob;
2131         int prob_list[ROOM_T_MAX];
2132         int rooms_built = 0;
2133         int area_size = 100 * (cur_hgt*cur_wid) / (MAX_HGT*MAX_WID);
2134         int level_index = MIN(10, div_round(dun_level, 10));
2135
2136         /* Number of each type of room on this level */
2137         s16b room_num[ROOM_T_MAX];
2138
2139         /* Limit number of rooms */
2140         int dun_rooms = DUN_ROOMS_MAX * area_size / 100;
2141
2142         /* Assume normal cave */
2143         room_info_type *room_info_ptr = room_info_normal;
2144
2145         /*
2146          * Initialize probability list.
2147          */
2148         for (i = 0; i < ROOM_T_MAX; i++)
2149         {
2150                 /* No rooms allowed above their minimum depth. */
2151                 if (dun_level < room_info_ptr[i].min_level)
2152                 {
2153                         prob_list[i] = 0;
2154                 }
2155                 else
2156                 {
2157                         prob_list[i] = room_info_ptr[i].prob[level_index];
2158                 }
2159         }
2160
2161         /*
2162          * XXX -- Various dungeon types and options.
2163          */
2164
2165         /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、かつ「常に通常でない部屋を生成する」フラグがONならば、GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
2166         if (ironman_rooms && !((d_info[dungeon_type].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
2167         {
2168                 for (i = 0; i < ROOM_T_MAX; i++)
2169                 {
2170                         if (i == ROOM_T_GREATER_VAULT) prob_list[i] = 1;
2171                         else prob_list[i] = 0;
2172                 }
2173         }
2174
2175         /*! @details ダンジョンにNO_VAULTフラグがあるならば、LESSER_VAULT / GREATER_VAULT/ RANDOM_VAULTを除外 / Forbidden vaults */
2176         else if (d_info[dungeon_type].flags1 & DF1_NO_VAULT)
2177         {
2178                 prob_list[ROOM_T_LESSER_VAULT] = 0;
2179                 prob_list[ROOM_T_GREATER_VAULT] = 0;
2180                 prob_list[ROOM_T_RANDOM_VAULT] = 0;
2181         }
2182
2183         /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
2184         if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
2185         {
2186                 MOVE_PLIST(ROOM_T_NORMAL, ROOM_T_FRACAVE);
2187                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_CRYPT);
2188                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_OVAL);
2189         }
2190
2191         /*! @details ダンジョンにCAVEフラグがある場合、NORMALの生成枠がFRACAVEに与えられる。/ CAVE dungeon (Orc cave etc.) */
2192         else if (d_info[dungeon_type].flags1 & DF1_CAVE)
2193         {
2194                 MOVE_PLIST(ROOM_T_FRACAVE, ROOM_T_NORMAL);
2195         }
2196
2197         /*! @details ダンジョンの基本地形が最初から渓谷かアリーナ型の場合 FRACAVE は生成から除外。 /  No caves when a (random) cavern exists: they look bad */
2198         else if (dun->cavern || dun->empty_level)
2199         {
2200                 prob_list[ROOM_T_FRACAVE] = 0;
2201         }
2202
2203         /*! @details ダンジョンに最初からGLASS_ROOMフラグがある場合、GLASS を生成から除外。/ Forbidden glass rooms */
2204         if (!(d_info[dungeon_type].flags1 & DF1_GLASS_ROOM))
2205         {
2206                 prob_list[ROOM_T_GLASS] = 0;
2207         }
2208
2209         /*! @details ARCADEは同フラグがダンジョンにないと生成されない。 / Forbidden glass rooms */
2210         if (!(d_info[dungeon_type].flags1 & DF1_ARCADE))
2211         {
2212                 prob_list[ROOM_T_ARCADE] = 0;
2213         }
2214
2215         /*
2216          * Initialize number of rooms,
2217          * And calcurate total probability.
2218          */
2219         for (total_prob = 0, i = 0; i < ROOM_T_MAX; i++)
2220         {
2221                 room_num[i] = 0;
2222                 total_prob += prob_list[i];
2223         }
2224
2225         /*
2226          * Prepare the number of rooms, of all types, we should build
2227          * on this level.
2228          */
2229         for (i = dun_rooms; i > 0; i--)
2230         {
2231                 int room_type;
2232                 int rand = randint0(total_prob);
2233
2234                 /* Get room_type randomly */
2235                 for (room_type = 0; room_type < ROOM_T_MAX; room_type++)
2236                 {
2237                         if (rand < prob_list[room_type]) break;
2238                         else rand -= prob_list[room_type];
2239                 }
2240
2241                 /* Paranoia */
2242                 if (room_type >= ROOM_T_MAX) room_type = ROOM_T_NORMAL;
2243
2244                 /* Increase the number of rooms of that type we should build. */
2245                 room_num[room_type]++;
2246
2247                 switch (room_type)
2248                 {
2249                 case ROOM_T_NEST:
2250                 case ROOM_T_PIT:
2251                 case ROOM_T_LESSER_VAULT:
2252                 case ROOM_T_TRAP_PIT:
2253                 case ROOM_T_GLASS:
2254                 case ROOM_T_ARCADE:
2255
2256                         /* Large room */
2257                         i -= 2;
2258                         break;
2259
2260                 case ROOM_T_GREATER_VAULT:
2261                 case ROOM_T_RANDOM_VAULT:
2262
2263                         /* Largest room */
2264                         i -= 3;
2265                         break;
2266                 }
2267         }
2268
2269         /*
2270          * Build each type of room one by one until we cannot build any more.
2271          * [from SAngband (originally from OAngband)]
2272          */
2273         while (TRUE)
2274         {
2275                 /* Assume no remaining rooms */
2276                 remain = FALSE;
2277
2278                 for (i = 0; i < ROOM_T_MAX; i++)
2279                 {
2280                         /* What type of room are we building now? */
2281                         int room_type = room_build_order[i];
2282
2283                         /* Go next if none available */
2284                         if (!room_num[room_type]) continue;
2285
2286                         /* Use up one unit */
2287                         room_num[room_type]--;
2288
2289                         /* Build the room. */
2290                         if (room_build(room_type))
2291                         {
2292                                 /* Increase the room built count. */
2293                                 rooms_built++;
2294
2295                                 /* Mark as there was some remaining rooms */
2296                                 remain = TRUE;
2297
2298                                 switch (room_type)
2299                                 {
2300                                 case ROOM_T_PIT:
2301                                 case ROOM_T_NEST:
2302                                 case ROOM_T_TRAP_PIT:
2303
2304                                         /* Avoid too many monsters */
2305                                         if (++crowded >= 2)
2306                                         {
2307                                                 room_num[ROOM_T_PIT] = 0;
2308                                                 room_num[ROOM_T_NEST] = 0;
2309                                                 room_num[ROOM_T_TRAP_PIT] = 0;
2310                                         }
2311                                         break;
2312
2313                                 case ROOM_T_ARCADE:
2314
2315                                         /* Avoid double-town */
2316                                         room_num[ROOM_T_ARCADE] = 0;
2317                                         break;
2318                                 }
2319                         }
2320                 }
2321
2322                 /* End loop if no room remain */
2323                 if (!remain) break;
2324         }
2325
2326         /*! @details 部屋生成数が2未満の場合生成失敗を返す */
2327         if (rooms_built < 2)
2328         {
2329                 msg_format_wizard(CHEAT_DUNGEON, _("部屋数が2未満でした。生成を再試行します。", "Number of rooms was under 2. Retry."), rooms_built);
2330                 return FALSE;
2331         }
2332
2333         msg_format_wizard(CHEAT_DUNGEON, _("このダンジョンの部屋数は %d です。", "Number of Rooms: %d"), rooms_built);
2334
2335         return TRUE;
2336 }