OSDN Git Service

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