OSDN Git Service

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