OSDN Git Service

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