OSDN Git Service

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