OSDN Git Service

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