OSDN Git Service

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