OSDN Git Service

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