OSDN Git Service

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