OSDN Git Service

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