OSDN Git Service

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