OSDN Git Service

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