OSDN Git Service

[Refactor] #38997 place_floor_bold() を削除し、place_grid() に統合 / Removed place_floor_bold...
[hengband/hengband.git] / src / rooms.c
1 /*!
2  * @file rooms.c
3  * @brief ダンジョンフロアの部屋生成処理 / make rooms. Used by generate.c when creating dungeons.
4  * @date 2014/01/06
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  * @details
12  * Room building routines.\n
13  *\n
14  * Room types:\n
15  *   1 -- normal\n
16  *   2 -- overlapping\n
17  *   3 -- cross shaped\n
18  *   4 -- large room with features\n
19  *   5 -- monster nests\n
20  *   6 -- monster pits\n
21  *   7 -- simple vaults\n
22  *   8 -- greater vaults\n
23  *   9 -- fractal caves\n
24  *  10 -- random vaults\n
25  *  11 -- circular rooms\n
26  *  12 -- crypts\n
27  *  13 -- trapped monster pits\n
28  *  14 -- trapped room\n
29  *  15 -- glass room\n
30  *  16 -- underground arcade\n
31  *\n
32  * Some functions are used to determine if the given monster\n
33  * is appropriate for inclusion in a monster nest or monster pit or\n
34  * the given type.\n
35  *\n
36  * None of the pits/nests are allowed to include "unique" monsters.\n
37  */
38
39 #include "angband.h"
40 #include "util.h"
41
42 #include "feature.h"
43 #include "floor.h"
44 #include "floor-generate.h"
45 #include "dungeon.h"
46 #include "grid.h"
47 #include "rooms.h"
48
49 #include "rooms-city.h"
50 #include "rooms-fractal.h"
51 #include "rooms-normal.h"
52 #include "rooms-pitnest.h"
53 #include "rooms-special.h"
54 #include "rooms-trap.h"
55 #include "rooms-vault.h"
56
57 #include "trap.h"
58
59 #include "monster.h"
60
61
62  /*!
63   * 各部屋タイプの生成比定義
64   *[from SAngband (originally from OAngband)]\n
65   *\n
66   * Table of values that control how many times each type of room will\n
67   * appear.  Each type of room has its own row, and each column\n
68   * corresponds to dungeon levels 0, 10, 20, and so on.  The final\n
69   * value is the minimum depth the room can appear at.  -LM-\n
70   *\n
71   * Level 101 and below use the values for level 100.\n
72   *\n
73   * Rooms with lots of monsters or loot may not be generated if the\n
74   * object or monster lists are already nearly full.  Rooms will not\n
75   * appear above their minimum depth.  Tiny levels will not have space\n
76   * for all the rooms you ask for.\n
77   */
78
79 static room_info_type room_info_normal[ROOM_T_MAX] =
80 {
81         /* Depth */
82         /*  0  10  20  30  40  50  60  70  80  90 100  min limit */
83         {{999,900,800,700,600,500,400,300,200,100,  0},  0}, /*NORMAL   */
84         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  1}, /*OVERLAP  */
85         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*CROSS    */
86         {{  1, 10, 20, 30, 40, 50, 60, 70, 80, 90,100},  3}, /*INNER_F  */
87         {{  0,  1,  1,  1,  2,  3,  5,  6,  8, 10, 13}, 10}, /*NEST     */
88         {{  0,  1,  1,  2,  3,  4,  6,  8, 10, 13, 16}, 10}, /*PIT      */
89         {{  0,  1,  1,  1,  2,  2,  3,  5,  6,  8, 10}, 10}, /*LESSER_V */
90         {{  0,  0,  1,  1,  1,  2,  2,  2,  3,  3,  4}, 20}, /*GREATER_V*/
91         {{  0,100,200,300,400,500,600,700,800,900,999}, 10}, /*FRACAVE  */
92         {{  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2}, 10}, /*RANDOM_V */
93         {{  0,  4,  8, 12, 16, 20, 24, 28, 32, 36, 40},  3}, /*OVAL     */
94         {{  1,  6, 12, 18, 24, 30, 36, 42, 48, 54, 60}, 10}, /*CRYPT    */
95         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP_PIT */
96         {{  0,  0,  1,  1,  1,  2,  3,  4,  5,  6,  8}, 20}, /*TRAP     */
97         {{  0,  0,  0,  0,  1,  1,  1,  2,  2,  2,  2}, 40}, /*GLASS    */
98         {{  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  3},  1}, /*ARCADE   */
99         {{  1,  8, 16, 24, 32, 40, 48, 56, 64, 72, 80},  1}, /*FIX      */
100 };
101
102 /*! 部屋の生成処理順 / Build rooms in descending order of difficulty. */
103 static byte room_build_order[ROOM_T_MAX] = {
104         ROOM_T_GREATER_VAULT,
105         ROOM_T_ARCADE,
106         ROOM_T_RANDOM_VAULT,
107         ROOM_T_LESSER_VAULT,
108         ROOM_T_TRAP_PIT,
109         ROOM_T_PIT,
110         ROOM_T_NEST,
111         ROOM_T_TRAP,
112         ROOM_T_GLASS,
113         ROOM_T_INNER_FEAT,
114         ROOM_T_FIXED,
115         ROOM_T_OVAL,
116         ROOM_T_CRYPT,
117         ROOM_T_OVERLAP,
118         ROOM_T_CROSS,
119         ROOM_T_FRACAVE,
120         ROOM_T_NORMAL,
121 };
122
123 /*!
124  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
125  * @param player_ptr プレーヤーへの参照ポインタ
126  * @param y0 配置したい中心のY座標
127  * @param x0 配置したい中心のX座標
128  * @details
129  * This funtion makes a very small room centred at (x0, y0)
130  * This is used in crypts, and random elemental vaults.
131  *
132  * Note - this should be used only on allocated regions
133  * within another room.
134  */
135 void build_small_room(player_type *player_ptr, POSITION x0, POSITION y0)
136 {
137         POSITION x, y;
138
139         floor_type *floor_ptr = player_ptr->current_floor_ptr;
140         for (y = y0 - 1; y <= y0 + 1; y++)
141         {
142                 place_inner_bold(player_ptr, y, x0 - 1);
143                 place_inner_bold(player_ptr, y, x0 + 1);
144         }
145
146         for (x = x0 - 1; x <= x0 + 1; x++)
147         {
148                 place_inner_bold(player_ptr, y0 - 1, x);
149                 place_inner_bold(player_ptr, y0 + 1, x);
150         }
151
152         /* Place a secret door on one side */
153         switch (randint0(4))
154         {
155         case 0: place_secret_door(player_ptr, y0, x0 - 1, DOOR_DEFAULT); break;
156         case 1: place_secret_door(player_ptr, y0, x0 + 1, DOOR_DEFAULT); break;
157         case 2: place_secret_door(player_ptr, y0 - 1, x0, DOOR_DEFAULT); break;
158         case 3: place_secret_door(player_ptr, y0 + 1, x0, DOOR_DEFAULT); break;
159         }
160
161         /* Clear mimic type */
162         floor_ptr->grid_array[y0][x0].mimic = 0;
163
164         /* Add inner open space */
165         place_bold(player_ptr, y0, x0, 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_hack)*y;
442                 dun->cent[dun->cent_n].x = (byte_hack)*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_outer_bold(player_ptr, y, x);
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_hack)j;
903                                         tmp_pos.x[flow_tail] = (byte_hack)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_extra_bold(player_ptr, y0 + y - yhsize, x0 + x - xhsize);
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_outer_bold(player_ptr, y0 + 0 - yhsize, x0 + i - xhsize);
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_outer_bold(player_ptr, y0 + 0 - yhsize, x0 + i - xhsize);
996                 }
997                 else
998                 {
999                         /* set to be normal granite */
1000                         place_extra_bold(player_ptr, y0 + 0 - yhsize, x0 + i - xhsize);
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_outer_bold(player_ptr, y0 + ysize - yhsize, x0 + i - xhsize);
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_outer_bold(player_ptr, y0 + ysize - yhsize, x0 + i - xhsize);
1011                 }
1012                 else
1013                 {
1014                         /* set to be normal granite */
1015                         place_extra_bold(player_ptr, y0 + ysize - yhsize, x0 + i - xhsize);
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_outer_bold(player_ptr, y0 + i - yhsize, x0 + 0 - xhsize);
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_outer_bold(player_ptr, y0 + i - yhsize, x0 + 0 - xhsize);
1034                 }
1035                 else
1036                 {
1037                         /* outside room */
1038                         place_extra_bold(player_ptr, y0 + i - yhsize, x0 + 0 - xhsize);
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_outer_bold(player_ptr, y0 + i - yhsize, x0 + xsize - xhsize);
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_outer_bold(player_ptr, y0 + i - yhsize, x0 + xsize - xhsize);
1048                 }
1049                 else
1050                 {
1051                         /* outside room */
1052                         place_extra_bold(player_ptr, y0 + i - yhsize, x0 + xsize - xhsize);
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_extra_bold(player_ptr, y0 + y - yhsize, x0 + x - xhsize);
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_extra_bold(player_ptr, y0 + y - yhsize, x0 + x - xhsize);
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 #ifdef ALLOW_CAVERNS_AND_LAKES
1117 /*
1118  * Builds a cave system in the center of the dungeon.
1119  */
1120 void build_cavern(player_type *player_ptr)
1121 {
1122         int grd, roug, cutoff;
1123         POSITION xsize, ysize, x0, y0;
1124         bool done, light;
1125
1126         light = done = FALSE;
1127         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1128         if ((floor_ptr->dun_level <= randint1(50)) && !(d_info[floor_ptr->dungeon_idx].flags1 & DF1_DARKNESS)) light = TRUE;
1129
1130         /* Make a cave the size of the dungeon */
1131         xsize = floor_ptr->width - 1;
1132         ysize = floor_ptr->height - 1;
1133         x0 = xsize / 2;
1134         y0 = ysize / 2;
1135
1136         /* Paranoia: make size even */
1137         xsize = x0 * 2;
1138         ysize = y0 * 2;
1139
1140         while (!done)
1141         {
1142                 /* testing values for these parameters: feel free to adjust */
1143                 grd = randint1(4) + 4;
1144
1145                 /* want average of about 16 */
1146                 roug = randint1(8) * randint1(4);
1147
1148                 /* about size/2 */
1149                 cutoff = xsize / 2;
1150
1151                 /* make it */
1152                 generate_hmap(floor_ptr, y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
1153
1154                 /* Convert to normal format+ clean up */
1155                 done = generate_fracave(player_ptr, y0 + 1, x0 + 1, xsize, ysize, cutoff, light, FALSE);
1156         }
1157 }
1158
1159
1160 bool generate_lake(player_type *player_ptr, POSITION y0, POSITION x0, POSITION xsize, POSITION ysize, int c1, int c2, int c3, int type)
1161 {
1162         POSITION x, y, xhsize, yhsize;
1163         int i;
1164         FEAT_IDX feat1, feat2, feat3;
1165
1166         /* offsets to middle from corner */
1167         xhsize = xsize / 2;
1168         yhsize = ysize / 2;
1169
1170         /* Get features based on type */
1171         switch (type)
1172         {
1173         case LAKE_T_LAVA: /* Lava */
1174                 feat1 = feat_deep_lava;
1175                 feat2 = feat_shallow_lava;
1176                 feat3 = feat_ground_type[randint0(100)];
1177                 break;
1178         case LAKE_T_WATER: /* Water */
1179                 feat1 = feat_deep_water;
1180                 feat2 = feat_shallow_water;
1181                 feat3 = feat_ground_type[randint0(100)];
1182                 break;
1183         case LAKE_T_CAVE: /* Collapsed floor_ptr->grid_array */
1184                 feat1 = feat_ground_type[randint0(100)];
1185                 feat2 = feat_ground_type[randint0(100)];
1186                 feat3 = feat_rubble;
1187                 break;
1188         case LAKE_T_EARTH_VAULT: /* Earth vault */
1189                 feat1 = feat_rubble;
1190                 feat2 = feat_ground_type[randint0(100)];
1191                 feat3 = feat_rubble;
1192                 break;
1193         case LAKE_T_AIR_VAULT: /* Air vault */
1194                 feat1 = feat_grass;
1195                 feat2 = feat_tree;
1196                 feat3 = feat_grass;
1197                 break;
1198         case LAKE_T_WATER_VAULT: /* Water vault */
1199                 feat1 = feat_shallow_water;
1200                 feat2 = feat_deep_water;
1201                 feat3 = feat_shallow_water;
1202                 break;
1203         case LAKE_T_FIRE_VAULT: /* Fire Vault */
1204                 feat1 = feat_shallow_lava;
1205                 feat2 = feat_deep_lava;
1206                 feat3 = feat_shallow_lava;
1207                 break;
1208         default: return FALSE;
1209         }
1210
1211         /*
1212          * select region connected to center of floor_ptr->grid_array system
1213          * this gets rid of alot of isolated one-sqaures that
1214          * can make teleport traps instadeaths...
1215          */
1216
1217          /* cutoffs */
1218         fill_data.c1 = c1;
1219         fill_data.c2 = c2;
1220         fill_data.c3 = c3;
1221
1222         /* features to fill with */
1223         fill_data.feat1 = feat1;
1224         fill_data.feat2 = feat2;
1225         fill_data.feat3 = feat3;
1226
1227         fill_data.info1 = 0;
1228         fill_data.info2 = 0;
1229         fill_data.info3 = 0;
1230
1231         /* number of filled squares */
1232         fill_data.amount = 0;
1233
1234         /* select region connected to center of floor_ptr->grid_array system
1235         * this gets rid of alot of isolated one-sqaures that
1236         * can make teleport traps instadeaths... */
1237         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1238         cave_fill(player_ptr, (byte)y0, (byte)x0);
1239
1240         /* if tally too small, try again */
1241         if (fill_data.amount < 10)
1242         {
1243                 /* too small -clear area and try again later */
1244                 for (x = 0; x <= xsize; ++x)
1245                 {
1246                         for (y = 0; y <= ysize; ++y)
1247                         {
1248                                 place_bold(player_ptr, y0 + y - yhsize, x0 + x - xhsize, floor);
1249                                 floor_ptr->grid_array[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY);
1250                         }
1251                 }
1252                 return FALSE;
1253         }
1254
1255         /* Do boundarys- set to normal granite */
1256         for (i = 0; i <= xsize; ++i)
1257         {
1258                 place_extra_bold(player_ptr, y0 + 0 - yhsize, x0 + i - xhsize);
1259                 place_extra_bold(player_ptr, y0 + ysize - yhsize, x0 + i - xhsize);
1260
1261                 /* clear the icky flag-don't need it any more */
1262                 floor_ptr->grid_array[y0 + 0 - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1263                 floor_ptr->grid_array[y0 + ysize - yhsize][x0 + i - xhsize].info &= ~(CAVE_ICKY);
1264         }
1265
1266         /* Do the left and right boundaries minus the corners (done above) */
1267
1268         for (i = 1; i < ysize; ++i)
1269         {
1270                 place_extra_bold(player_ptr, y0 + i - yhsize, x0 + 0 - xhsize);
1271                 place_extra_bold(player_ptr, y0 + i - yhsize, x0 + xsize - xhsize);
1272
1273                 /* clear icky flag -done with it */
1274                 floor_ptr->grid_array[y0 + i - yhsize][x0 + 0 - xhsize].info &= ~(CAVE_ICKY);
1275                 floor_ptr->grid_array[y0 + i - yhsize][x0 + xsize - xhsize].info &= ~(CAVE_ICKY);
1276         }
1277
1278
1279         /* Do the rest: convert back to the normal format */
1280         for (x = 1; x < xsize; ++x)
1281         {
1282                 for (y = 1; y < ysize; ++y)
1283                 {
1284                         /* Fill unconnected regions with granite */
1285                         if ((!(floor_ptr->grid_array[y0 + y - yhsize][x0 + x - xhsize].info & CAVE_ICKY)) ||
1286                                 is_outer_bold(floor_ptr, y0 + y - yhsize, x0 + x - xhsize))
1287                                 place_extra_bold(player_ptr, y0 + y - yhsize, x0 + x - xhsize);
1288
1289                         /* turn off icky flag (no longer needed.) */
1290                         floor_ptr->grid_array[y0 + y - yhsize][x0 + x - xhsize].info &= ~(CAVE_ICKY | CAVE_ROOM);
1291
1292                         /* Light lava */
1293                         if (cave_have_flag_bold(floor_ptr, y0 + y - yhsize, x0 + x - xhsize, FF_LAVA))
1294                         {
1295                                 if (!(d_info[floor_ptr->dungeon_idx].flags1 & DF1_DARKNESS)) floor_ptr->grid_array[y0 + y - yhsize][x0 + x - xhsize].info |= CAVE_GLOW;
1296                         }
1297                 }
1298         }
1299
1300         return TRUE;
1301 }
1302
1303
1304 /*
1305  * makes a lake/collapsed floor in the center of the dungeon
1306  */
1307 void build_lake(player_type *player_ptr, int type)
1308 {
1309         int grd, roug, xsize, ysize, x0, y0;
1310         bool done = FALSE;
1311         int c1, c2, c3;
1312
1313         /* paranoia - exit if lake type out of range. */
1314         if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT))
1315         {
1316                 msg_format("Invalid lake type (%d)", type);
1317                 return;
1318         }
1319
1320         /* Make the size of the dungeon */
1321         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1322         xsize = floor_ptr->width - 1;
1323         ysize = floor_ptr->height - 1;
1324         x0 = xsize / 2;
1325         y0 = ysize / 2;
1326
1327         /* Paranoia: make size even */
1328         xsize = x0 * 2;
1329         ysize = y0 * 2;
1330
1331         while (!done)
1332         {
1333                 /* testing values for these parameters: feel free to adjust */
1334                 grd = randint1(3) + 4;
1335
1336                 /* want average of about 16 */
1337                 roug = randint1(8) * randint1(4);
1338
1339                 /* Make up size of various componants */
1340                 /* Floor */
1341                 c3 = 3 * xsize / 4;
1342
1343                 /* Deep water/lava */
1344                 c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
1345
1346                 /* Shallow boundary */
1347                 c2 = (c1 + c3) / 2;
1348
1349                 /* make it */
1350                 generate_hmap(floor_ptr, y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
1351
1352                 /* Convert to normal format+ clean up */
1353                 done = generate_lake(player_ptr, y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
1354         }
1355 }
1356 #endif /* ALLOW_CAVERNS_AND_LAKES */
1357
1358
1359
1360 /*
1361  * Routine that fills the empty areas of a room with treasure and monsters.
1362  */
1363 void fill_treasure(player_type *player_ptr, POSITION x1, POSITION x2, POSITION y1, POSITION y2, int difficulty)
1364 {
1365         POSITION x, y, cx, cy, size;
1366         s32b value;
1367
1368         /* center of room:*/
1369         cx = (x1 + x2) / 2;
1370         cy = (y1 + y2) / 2;
1371
1372         /* Rough measure of size of vault= sum of lengths of sides */
1373         size = abs(x2 - x1) + abs(y2 - y1);
1374
1375         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1376         for (x = x1; x <= x2; x++)
1377         {
1378                 for (y = y1; y <= y2; y++)
1379                 {
1380                         /* Thing added based on distance to center of vault
1381                          * Difficulty is 1-easy to 10-hard */
1382                         value = ((((s32b)(distance(cx, cy, x, y))) * 100) / size) + randint1(10) - difficulty;
1383
1384                         /* hack- empty square part of the time */
1385                         if ((randint1(100) - difficulty * 3) > 50) value = 20;
1386
1387                         /* if floor, shallow water and lava */
1388                         if (is_floor_bold(floor_ptr, y, x) ||
1389                                 (cave_have_flag_bold(floor_ptr, y, x, FF_PLACE) && cave_have_flag_bold(floor_ptr, y, x, FF_DROP)))
1390                         {
1391                                 /* The smaller 'value' is, the better the stuff */
1392                                 if (value < 0)
1393                                 {
1394                                         /* Meanest monster + treasure */
1395                                         floor_ptr->monster_level = floor_ptr->base_level + 40;
1396                                         place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1397                                         floor_ptr->monster_level = floor_ptr->base_level;
1398                                         floor_ptr->object_level = floor_ptr->base_level + 20;
1399                                         place_object(player_ptr, y, x, AM_GOOD);
1400                                         floor_ptr->object_level = floor_ptr->base_level;
1401                                 }
1402                                 else if (value < 5)
1403                                 {
1404                                         /* Mean monster +treasure */
1405                                         floor_ptr->monster_level = floor_ptr->base_level + 20;
1406                                         place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1407                                         floor_ptr->monster_level = floor_ptr->base_level;
1408                                         floor_ptr->object_level = floor_ptr->base_level + 10;
1409                                         place_object(player_ptr, y, x, AM_GOOD);
1410                                         floor_ptr->object_level = floor_ptr->base_level;
1411                                 }
1412                                 else if (value < 10)
1413                                 {
1414                                         floor_ptr->monster_level = floor_ptr->base_level + 9;
1415                                         place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1416                                         floor_ptr->monster_level = floor_ptr->base_level;
1417                                 }
1418                                 else if (value < 17)
1419                                 {
1420                                         /* Intentional Blank space */
1421
1422                                         /*
1423                                          * (Want some of the vault to be empty
1424                                          * so have room for group monsters.
1425                                          * This is used in the hack above to lower
1426                                          * the density of stuff in the vault.)
1427                                          */
1428                                 }
1429                                 else if (value < 23)
1430                                 {
1431                                         /* Object or trap */
1432                                         if (randint0(100) < 25)
1433                                         {
1434                                                 place_object(player_ptr, y, x, 0L);
1435                                         }
1436                                         else
1437                                         {
1438                                                 place_trap(player_ptr, y, x);
1439                                         }
1440                                 }
1441                                 else if (value < 30)
1442                                 {
1443                                         /* Monster and trap */
1444                                         floor_ptr->monster_level = floor_ptr->base_level + 5;
1445                                         place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1446                                         floor_ptr->monster_level = floor_ptr->base_level;
1447                                         place_trap(player_ptr, y, x);
1448                                 }
1449                                 else if (value < 40)
1450                                 {
1451                                         /* Monster or object */
1452                                         if (randint0(100) < 50)
1453                                         {
1454                                                 floor_ptr->monster_level = floor_ptr->base_level + 3;
1455                                                 place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1456                                                 floor_ptr->monster_level = floor_ptr->base_level;
1457                                         }
1458                                         if (randint0(100) < 50)
1459                                         {
1460                                                 floor_ptr->object_level = floor_ptr->base_level + 7;
1461                                                 place_object(player_ptr, y, x, 0L);
1462                                                 floor_ptr->object_level = floor_ptr->base_level;
1463                                         }
1464                                 }
1465                                 else if (value < 50)
1466                                 {
1467                                         /* Trap */
1468                                         place_trap(player_ptr, y, x);
1469                                 }
1470                                 else
1471                                 {
1472                                         /* Various Stuff */
1473
1474                                         /* 20% monster, 40% trap, 20% object, 20% blank space */
1475                                         if (randint0(100) < 20)
1476                                         {
1477                                                 place_monster(player_ptr, y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
1478                                         }
1479                                         else if (randint0(100) < 50)
1480                                         {
1481                                                 place_trap(player_ptr, y, x);
1482                                         }
1483                                         else if (randint0(100) < 50)
1484                                         {
1485                                                 place_object(player_ptr, y, x, 0L);
1486                                         }
1487                                 }
1488
1489                         }
1490                 }
1491         }
1492 }
1493
1494
1495 /*
1496  * Overlay a rectangular room given its bounds
1497  * This routine is used by build_room_vault
1498  * The area inside the walls is not touched:
1499  * only granite is removed- normal walls stay
1500  */
1501 void build_room(player_type *player_ptr, POSITION x1, POSITION x2, POSITION y1, POSITION y2)
1502 {
1503         POSITION x, y, xsize, ysize;
1504         int i, temp;
1505
1506         /* Check if rectangle has no width */
1507         if ((x1 == x2) || (y1 == y2)) return;
1508
1509         if (x1 > x2)
1510         {
1511                 /* Swap boundaries if in wrong order */
1512                 temp = x1;
1513                 x1 = x2;
1514                 x2 = temp;
1515         }
1516
1517         if (y1 > y2)
1518         {
1519                 /* Swap boundaries if in wrong order */
1520                 temp = y1;
1521                 y1 = y2;
1522                 y2 = temp;
1523         }
1524
1525         /* get total widths */
1526         xsize = x2 - x1;
1527         ysize = y2 - y1;
1528
1529
1530         /* Top and bottom boundaries */
1531         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1532         for (i = 0; i <= xsize; i++)
1533         {
1534                 place_outer_noperm_bold(player_ptr, y1, x1 + i);
1535                 floor_ptr->grid_array[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1536                 place_outer_noperm_bold(player_ptr, y2, x1 + i);
1537                 floor_ptr->grid_array[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
1538         }
1539
1540         /* Left and right boundaries */
1541         for (i = 1; i < ysize; i++)
1542         {
1543                 place_outer_noperm_bold(player_ptr, y1 + i, x1);
1544                 floor_ptr->grid_array[y1 + i][x1].info |= (CAVE_ROOM | CAVE_ICKY);
1545                 place_outer_noperm_bold(player_ptr, y1 + i, x2);
1546                 floor_ptr->grid_array[y1 + i][x2].info |= (CAVE_ROOM | CAVE_ICKY);
1547         }
1548
1549         /* Middle */
1550         for (x = 1; x < xsize; x++)
1551         {
1552                 for (y = 1; y < ysize; y++)
1553                 {
1554                         if (is_extra_bold(floor_ptr, y1 + y, x1 + x))
1555                         {
1556                                 /* clear the untouched region */
1557                                 place_bold(player_ptr, y1 + y, x1 + x, floor);
1558                                 floor_ptr->grid_array[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1559                         }
1560                         else
1561                         {
1562                                 /* make it a room- but don't touch */
1563                                 floor_ptr->grid_array[y1 + y][x1 + x].info |= (CAVE_ROOM | CAVE_ICKY);
1564                         }
1565                 }
1566         }
1567 }
1568
1569
1570
1571 /*
1572  * maze vault -- rectangular labyrinthine rooms
1573  *
1574  * maze vault uses two routines:
1575  *    r_visit - a recursive routine that builds the labyrinth
1576  *    build_maze_vault - a driver routine that calls r_visit and adds
1577  *                   monsters, traps and treasure
1578  *
1579  * The labyrinth is built by creating a spanning tree of a graph.
1580  * The graph vertices are at
1581  *    (x, y) = (2j + x1, 2k + y1)   j = 0,...,m-1    k = 0,...,n-1
1582  * and the edges are the vertical and horizontal nearest neighbors.
1583  *
1584  * The spanning tree is created by performing a suitably randomized
1585  * depth-first traversal of the graph. The only adjustable parameter
1586  * is the randint0(3) below; it governs the relative density of
1587  * twists and turns in the labyrinth: smaller number, more twists.
1588  */
1589 void r_visit(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, int node, DIRECTION dir, int *visited)
1590 {
1591         int i, j, m, n, temp, x, y, adj[4];
1592
1593         /* dimensions of vertex array */
1594         m = (x2 - x1) / 2 + 1;
1595         n = (y2 - y1) / 2 + 1;
1596
1597         /* mark node visited and set it to a floor */
1598         visited[node] = 1;
1599         x = 2 * (node % m) + x1;
1600         y = 2 * (node / m) + y1;
1601         place_bold(player_ptr, y, x, floor);
1602
1603         /* setup order of adjacent node visits */
1604         if (one_in_(3))
1605         {
1606                 /* pick a random ordering */
1607                 for (i = 0; i < 4; i++)
1608                         adj[i] = i;
1609                 for (i = 0; i < 4; i++)
1610                 {
1611                         j = randint0(4);
1612                         temp = adj[i];
1613                         adj[i] = adj[j];
1614                         adj[j] = temp;
1615                 }
1616                 dir = adj[0];
1617         }
1618         else
1619         {
1620                 /* pick a random ordering with dir first */
1621                 adj[0] = dir;
1622                 for (i = 1; i < 4; i++)
1623                         adj[i] = i;
1624                 for (i = 1; i < 4; i++)
1625                 {
1626                         j = 1 + randint0(3);
1627                         temp = adj[i];
1628                         adj[i] = adj[j];
1629                         adj[j] = temp;
1630                 }
1631         }
1632
1633         for (i = 0; i < 4; i++)
1634         {
1635                 switch (adj[i])
1636                 {
1637                 case 0:
1638                         /* (0,+) - check for bottom boundary */
1639                         if ((node / m < n - 1) && (visited[node + m] == 0))
1640                         {
1641                                 place_bold(player_ptr, y + 1, x, floor);
1642                                 r_visit(player_ptr, y1, x1, y2, x2, node + m, dir, visited);
1643                         }
1644                         break;
1645                 case 1:
1646                         /* (0,-) - check for top boundary */
1647                         if ((node / m > 0) && (visited[node - m] == 0))
1648                         {
1649                                 place_bold(player_ptr, y - 1, x, floor);
1650                                 r_visit(player_ptr, y1, x1, y2, x2, node - m, dir, visited);
1651                         }
1652                         break;
1653                 case 2:
1654                         /* (+,0) - check for right boundary */
1655                         if ((node % m < m - 1) && (visited[node + 1] == 0))
1656                         {
1657                                 place_bold(player_ptr, y, x + 1, floor);
1658                                 r_visit(player_ptr, y1, x1, y2, x2, node + 1, dir, visited);
1659                         }
1660                         break;
1661                 case 3:
1662                         /* (-,0) - check for left boundary */
1663                         if ((node % m > 0) && (visited[node - 1] == 0))
1664                         {
1665                                 place_bold(player_ptr, y, x - 1, floor);
1666                                 r_visit(player_ptr, y1, x1, y2, x2, node - 1, dir, visited);
1667                         }
1668                 } /* end switch */
1669         }
1670 }
1671
1672
1673 void build_maze_vault(player_type *player_ptr, POSITION x0, POSITION y0, POSITION xsize, POSITION ysize, bool is_vault)
1674 {
1675         POSITION y, x, dy, dx;
1676         POSITION y1, x1, y2, x2;
1677         int m, n, num_vertices, *visited;
1678         bool light;
1679         grid_type *g_ptr;
1680
1681         msg_print_wizard(CHEAT_DUNGEON, _("迷路ランダムVaultを生成しました。", "Maze Vault."));
1682
1683         /* Choose lite or dark */
1684         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1685         light = ((floor_ptr->dun_level <= randint1(25)) && is_vault && !(d_info[floor_ptr->dungeon_idx].flags1 & DF1_DARKNESS));
1686
1687         /* Pick a random room size - randomized by calling routine */
1688         dy = ysize / 2 - 1;
1689         dx = xsize / 2 - 1;
1690
1691         y1 = y0 - dy;
1692         x1 = x0 - dx;
1693         y2 = y0 + dy;
1694         x2 = x0 + dx;
1695
1696         /* generate the room */
1697         for (y = y1 - 1; y <= y2 + 1; y++)
1698         {
1699                 for (x = x1 - 1; x <= x2 + 1; x++)
1700                 {
1701                         g_ptr = &floor_ptr->grid_array[y][x];
1702                         g_ptr->info |= CAVE_ROOM;
1703                         if (is_vault) g_ptr->info |= CAVE_ICKY;
1704                         if ((x == x1 - 1) || (x == x2 + 1) || (y == y1 - 1) || (y == y2 + 1))
1705                         {
1706                                 place_grid(player_ptr, g_ptr, outer);
1707                         }
1708                         else if (!is_vault)
1709                         {
1710                                 place_grid(player_ptr, g_ptr, extra);
1711                         }
1712                         else
1713                         {
1714                                 place_grid(player_ptr, g_ptr, inner);
1715                         }
1716                         if (light) g_ptr->info |= (CAVE_GLOW);
1717                 }
1718         }
1719
1720         /* dimensions of vertex array */
1721         m = dx + 1;
1722         n = dy + 1;
1723         num_vertices = m * n;
1724
1725         /* initialize array of visited vertices */
1726         C_MAKE(visited, num_vertices, int);
1727
1728         /* traverse the graph to create a spaning tree, pick a random root */
1729         r_visit(player_ptr, y1, x1, y2, x2, randint0(num_vertices), 0, visited);
1730
1731         /* Fill with monsters and treasure, low difficulty */
1732         if (is_vault) fill_treasure(player_ptr, x1, x2, y1, y2, randint1(5));
1733
1734         C_KILL(visited, num_vertices, int);
1735 }
1736
1737
1738 /* Build a town/ castle by using a recursive algorithm.
1739  * Basically divide each region in a probalistic way to create
1740  * smaller regions.  When the regions get too small stop.
1741  *
1742  * The power variable is a measure of how well defended a region is.
1743  * This alters the possible choices.
1744  */
1745 void build_recursive_room(player_type *player_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, int power)
1746 {
1747         POSITION xsize, ysize;
1748         POSITION x, y;
1749         int choice;
1750
1751         /* Temp variables */
1752         int t1, t2, t3, t4;
1753
1754         xsize = x2 - x1;
1755         ysize = y2 - y1;
1756
1757         if ((power < 3) && (xsize > 12) && (ysize > 12))
1758         {
1759                 /* Need outside wall +keep */
1760                 choice = 1;
1761         }
1762         else
1763         {
1764                 if (power < 10)
1765                 {
1766                         /* Make rooms + subdivide */
1767                         if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8))
1768                         {
1769                                 choice = 4;
1770                         }
1771                         else
1772                         {
1773                                 choice = randint1(2) + 1;
1774                         }
1775                 }
1776                 else
1777                 {
1778                         /* Mostly subdivide */
1779                         choice = randint1(3) + 1;
1780                 }
1781         }
1782
1783         /* Based on the choice made above, do something */
1784         switch (choice)
1785         {
1786         case 1:
1787         {
1788                 /* Outer walls */
1789
1790                 /* top and bottom */
1791                 for (x = x1; x <= x2; x++)
1792                 {
1793                         place_outer_bold(player_ptr, y1, x);
1794                         place_outer_bold(player_ptr, y2, x);
1795                 }
1796
1797                 /* left and right */
1798                 for (y = y1 + 1; y < y2; y++)
1799                 {
1800                         place_outer_bold(player_ptr, y, x1);
1801                         place_outer_bold(player_ptr, y, x2);
1802                 }
1803
1804                 /* Make a couple of entrances */
1805                 if (one_in_(2))
1806                 {
1807                         /* left and right */
1808                         y = randint1(ysize) + y1;
1809                         place_bold(player_ptr, y, x1, floor);
1810                         place_bold(player_ptr, y, x2, floor);
1811                 }
1812                 else
1813                 {
1814                         /* top and bottom */
1815                         x = randint1(xsize) + x1;
1816                         place_bold(player_ptr, y1, x, floor);
1817                         place_bold(player_ptr, y2, x, floor);
1818                 }
1819
1820                 /* Select size of keep */
1821                 t1 = randint1(ysize / 3) + y1;
1822                 t2 = y2 - randint1(ysize / 3);
1823                 t3 = randint1(xsize / 3) + x1;
1824                 t4 = x2 - randint1(xsize / 3);
1825
1826                 /* Do outside areas */
1827
1828                 /* Above and below keep */
1829                 build_recursive_room(player_ptr, x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
1830                 build_recursive_room(player_ptr, x1 + 1, t2, x2 - 1, y2, power + 1);
1831
1832                 /* Left and right of keep */
1833                 build_recursive_room(player_ptr, x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
1834                 build_recursive_room(player_ptr, t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
1835
1836                 /* Make the keep itself: */
1837                 x1 = t3;
1838                 x2 = t4;
1839                 y1 = t1;
1840                 y2 = t2;
1841                 xsize = x2 - x1;
1842                 ysize = y2 - y1;
1843                 power += 2;
1844
1845                 /* Fall through */
1846         }
1847         case 4:
1848         {
1849                 /* Try to build a room */
1850                 if ((xsize < 3) || (ysize < 3))
1851                 {
1852                         for (y = y1; y < y2; y++)
1853                         {
1854                                 for (x = x1; x < x2; x++)
1855                                 {
1856                                         place_inner_bold(player_ptr, y, x);
1857                                 }
1858                         }
1859
1860                         /* Too small */
1861                         return;
1862                 }
1863
1864                 /* Make outside walls */
1865                 /* top and bottom */
1866                 for (x = x1 + 1; x <= x2 - 1; x++)
1867                 {
1868                         place_inner_bold(player_ptr, y1 + 1, x);
1869                         place_inner_bold(player_ptr, y2 - 1, x);
1870                 }
1871
1872                 /* left and right */
1873                 for (y = y1 + 1; y <= y2 - 1; y++)
1874                 {
1875                         place_inner_bold(player_ptr, y, x1 + 1);
1876                         place_inner_bold(player_ptr, y, x2 - 1);
1877                 }
1878
1879                 /* Make a door */
1880                 y = randint1(ysize - 3) + y1 + 1;
1881
1882                 if (one_in_(2))
1883                 {
1884                         /* left */
1885                         place_bold(player_ptr, y, x1 + 1, floor);
1886                 }
1887                 else
1888                 {
1889                         /* right */
1890                         place_bold(player_ptr, y, x2 - 1, floor);
1891                 }
1892
1893                 /* Build the room */
1894                 build_recursive_room(player_ptr, x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
1895                 break;
1896         }
1897         case 2:
1898         {
1899                 /* Try and divide vertically */
1900                 if (xsize < 3)
1901                 {
1902                         /* Too small */
1903                         for (y = y1; y < y2; y++)
1904                         {
1905                                 for (x = x1; x < x2; x++)
1906                                 {
1907                                         place_inner_bold(player_ptr, y, x);
1908                                 }
1909                         }
1910                         return;
1911                 }
1912
1913                 t1 = randint1(xsize - 2) + x1 + 1;
1914                 build_recursive_room(player_ptr, x1, y1, t1, y2, power - 2);
1915                 build_recursive_room(player_ptr, t1 + 1, y1, x2, y2, power - 2);
1916                 break;
1917         }
1918         case 3:
1919         {
1920                 /* Try and divide horizontally */
1921                 if (ysize < 3)
1922                 {
1923                         /* Too small */
1924                         for (y = y1; y < y2; y++)
1925                         {
1926                                 for (x = x1; x < x2; x++)
1927                                 {
1928                                         place_inner_bold(player_ptr, y, x);
1929                                 }
1930                         }
1931                         return;
1932                 }
1933
1934                 t1 = randint1(ysize - 2) + y1 + 1;
1935                 build_recursive_room(player_ptr, x1, y1, x2, t1, power - 2);
1936                 build_recursive_room(player_ptr, x1, t1 + 1, x2, y2, power - 2);
1937                 break;
1938         }
1939         }
1940 }
1941
1942
1943 /*
1944  * Add outer wall to a floored region
1945  * Note: no range checking is done so must be inside dungeon
1946  * This routine also stomps on doors
1947  */
1948 void add_outer_wall(player_type *player_ptr, POSITION x, POSITION y, int light, POSITION x1, POSITION y1, POSITION x2, POSITION y2)
1949 {
1950         grid_type *g_ptr;
1951         feature_type *f_ptr;
1952         int i, j;
1953
1954         floor_type *floor_ptr = player_ptr->current_floor_ptr;
1955         if (!in_bounds(floor_ptr, y, x)) return;
1956
1957         g_ptr = &floor_ptr->grid_array[y][x];
1958
1959         /* hack- check to see if square has been visited before
1960         * if so, then exit (use room flag to do this) */
1961         if (g_ptr->info & CAVE_ROOM) return;
1962
1963         /* set room flag */
1964         g_ptr->info |= CAVE_ROOM;
1965
1966         f_ptr = &f_info[g_ptr->feat];
1967
1968         if (is_floor_bold(floor_ptr, y, x))
1969         {
1970                 for (i = -1; i <= 1; i++)
1971                 {
1972                         for (j = -1; j <= 1; j++)
1973                         {
1974                                 if ((x + i >= x1) && (x + i <= x2) && (y + j >= y1) && (y + j <= y2))
1975                                 {
1976                                         add_outer_wall(player_ptr, x + i, y + j, light, x1, y1, x2, y2);
1977                                         if (light) g_ptr->info |= CAVE_GLOW;
1978                                 }
1979                         }
1980                 }
1981         }
1982         else if (is_extra_bold(floor_ptr, y, x))
1983         {
1984                 /* Set bounding walls */
1985                 place_outer_bold(player_ptr, y, x);
1986                 if (light) g_ptr->info |= CAVE_GLOW;
1987         }
1988         else if (permanent_wall(f_ptr))
1989         {
1990                 /* Set bounding walls */
1991                 if (light) g_ptr->info |= CAVE_GLOW;
1992         }
1993 }
1994
1995
1996 /*
1997  * Hacked distance formula - gives the 'wrong' answer.
1998  * Used to build crypts
1999  */
2000 POSITION dist2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, POSITION h1, POSITION h2, POSITION h3, POSITION h4)
2001 {
2002         POSITION dx, dy;
2003         dx = abs(x2 - x1);
2004         dy = abs(y2 - y1);
2005
2006         /* Basically this works by taking the normal pythagorean formula
2007          * and using an expansion to express this in a way without the
2008          * square root.  This approximate formula is then perturbed to give
2009          * the distorted results.  (I found this by making a mistake when I was
2010          * trying to fix the circular rooms.)
2011          */
2012
2013          /* h1-h4 are constants that describe the metric */
2014         if (dx >= 2 * dy) return (dx + (dy * h1) / h2);
2015         if (dy >= 2 * dx) return (dy + (dx * h1) / h2);
2016         return (((dx + dy) * 128) / 181 +
2017                 (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4);
2018         /* 128/181 is approx. 1/sqrt(2) */
2019 }
2020
2021
2022
2023
2024 /* Create a new floor room with optional light */
2025 void generate_room_floor(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, int light)
2026 {
2027         POSITION y, x;
2028
2029         grid_type *g_ptr;
2030
2031         for (y = y1; y <= y2; y++)
2032         {
2033                 for (x = x1; x <= x2; x++)
2034                 {
2035                         /* Point to grid */
2036                         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
2037                         place_grid(player_ptr, g_ptr, floor);
2038                         g_ptr->info |= (CAVE_ROOM);
2039                         if (light) g_ptr->info |= (CAVE_GLOW);
2040                 }
2041         }
2042 }
2043
2044 void generate_fill_perm_bold(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
2045 {
2046         POSITION y, x;
2047
2048         for (y = y1; y <= y2; y++)
2049         {
2050                 for (x = x1; x <= x2; x++)
2051                 {
2052                         /* Point to grid */
2053                         place_inner_perm_bold(player_ptr, y, x);
2054                 }
2055         }
2056 }
2057
2058
2059 /*!
2060  * @brief 与えられた部屋型IDに応じて部屋の生成処理分岐を行い結果を返す / Attempt to build a room of the given type at the given block
2061  * @param player_ptr プレーヤーへの参照ポインタ
2062  * @param type 部屋型ID
2063  * @note that we restrict the number of "crowded" rooms to reduce the chance of overflowing the monster list during level creation.
2064  * @return 部屋の生成に成功した場合 TRUE を返す。
2065  */
2066 static bool room_build(player_type *player_ptr, EFFECT_ID typ)
2067 {
2068         switch (typ)
2069         {
2070                 /* Build an appropriate room */
2071         case ROOM_T_NORMAL:        return build_type1(player_ptr);
2072         case ROOM_T_OVERLAP:       return build_type2(player_ptr);
2073         case ROOM_T_CROSS:         return build_type3(player_ptr);
2074         case ROOM_T_INNER_FEAT:    return build_type4(player_ptr);
2075         case ROOM_T_NEST:          return build_type5(player_ptr);
2076         case ROOM_T_PIT:           return build_type6(player_ptr);
2077         case ROOM_T_LESSER_VAULT:  return build_type7(player_ptr);
2078         case ROOM_T_GREATER_VAULT: return build_type8(player_ptr);
2079         case ROOM_T_FRACAVE:       return build_type9(player_ptr);
2080         case ROOM_T_RANDOM_VAULT:  return build_type10(player_ptr);
2081         case ROOM_T_OVAL:          return build_type11(player_ptr);
2082         case ROOM_T_CRYPT:         return build_type12(player_ptr);
2083         case ROOM_T_TRAP_PIT:      return build_type13(player_ptr);
2084         case ROOM_T_TRAP:          return build_type14(player_ptr);
2085         case ROOM_T_GLASS:         return build_type15(player_ptr);
2086         case ROOM_T_ARCADE:        return build_type16(player_ptr);
2087         case ROOM_T_FIXED:         return build_type17(player_ptr);
2088         }
2089         return FALSE;
2090 }
2091
2092 /*!
2093  * @brief 指定した部屋の生成確率を別の部屋に加算し、指定した部屋の生成率を0にする
2094  * @param dst 確率を移す先の部屋種ID
2095  * @param src 確率を与える元の部屋種ID
2096  */
2097 #define MOVE_PLIST(dst, src) (prob_list[dst] += prob_list[src], prob_list[src] = 0) 
2098
2099  /*!
2100   * @brief 部屋生成処理のメインルーチン(Sangbandを経由してOangbandからの実装を引用) / Generate rooms in dungeon.  Build bigger rooms at first. [from SAngband (originally from OAngband)]
2101   * @param player_ptr プレーヤーへの参照ポインタ
2102   * @return 部屋生成に成功した場合 TRUE を返す。
2103   */
2104 bool generate_rooms(player_type *player_ptr)
2105 {
2106         floor_type *floor_ptr = player_ptr->current_floor_ptr;
2107         int i;
2108         bool remain;
2109         int crowded = 0;
2110         int total_prob;
2111         int prob_list[ROOM_T_MAX];
2112         int rooms_built = 0;
2113         int area_size = 100 * (floor_ptr->height*floor_ptr->width) / (MAX_HGT*MAX_WID);
2114         int level_index = MIN(10, div_round(floor_ptr->dun_level, 10));
2115
2116         /* Number of each type of room on this level */
2117         s16b room_num[ROOM_T_MAX];
2118
2119         /* Limit number of rooms */
2120         int dun_rooms = DUN_ROOMS_MAX * area_size / 100;
2121
2122         /* Assume normal floor_ptr->grid_array */
2123         room_info_type *room_info_ptr = room_info_normal;
2124
2125         /*
2126          * Initialize probability list.
2127          */
2128         for (i = 0; i < ROOM_T_MAX; i++)
2129         {
2130                 /* No rooms allowed above their minimum depth. */
2131                 if (floor_ptr->dun_level < room_info_ptr[i].min_level)
2132                 {
2133                         prob_list[i] = 0;
2134                 }
2135                 else
2136                 {
2137                         prob_list[i] = room_info_ptr[i].prob[level_index];
2138                 }
2139         }
2140
2141         /*
2142          * XXX -- Various dungeon types and options.
2143          */
2144
2145          /*! @details ダンジョンにBEGINNER、CHAMELEON、SMALLESTいずれのフラグもなく、
2146           * かつ「常に通常でない部屋を生成する」フラグがONならば、
2147           * GRATER_VAULTのみを生成対象とする。 / Ironman sees only Greater Vaults */
2148         if (ironman_rooms && !((d_info[floor_ptr->dungeon_idx].flags1 & (DF1_BEGINNER | DF1_CHAMELEON | DF1_SMALLEST))))
2149         {
2150                 for (i = 0; i < ROOM_T_MAX; i++)
2151                 {
2152                         if (i == ROOM_T_GREATER_VAULT) prob_list[i] = 1;
2153                         else prob_list[i] = 0;
2154                 }
2155         }
2156
2157         /*! @details ダンジョンにNO_VAULTフラグがあるならば、LESSER_VAULT / GREATER_VAULT/ RANDOM_VAULTを除外 / Forbidden vaults */
2158         else if (d_info[floor_ptr->dungeon_idx].flags1 & DF1_NO_VAULT)
2159         {
2160                 prob_list[ROOM_T_LESSER_VAULT] = 0;
2161                 prob_list[ROOM_T_GREATER_VAULT] = 0;
2162                 prob_list[ROOM_T_RANDOM_VAULT] = 0;
2163         }
2164
2165         /*! @details ダンジョンにBEGINNERフラグがあるならば、FIXED_ROOMを除外 / Forbidden vaults */
2166         if (d_info[floor_ptr->dungeon_idx].flags1 & DF1_BEGINNER)
2167         {
2168                 prob_list[ROOM_T_FIXED] = 0;
2169         }
2170
2171         /*! @details ダンジョンにNO_CAVEフラグがある場合、FRACAVEの生成枠がNORMALに与えられる。CRIPT、OVALの生成枠がINNER_Fに与えられる。/ NO_CAVE dungeon (Castle)*/
2172         if (d_info[floor_ptr->dungeon_idx].flags1 & DF1_NO_CAVE)
2173         {
2174                 MOVE_PLIST(ROOM_T_NORMAL, ROOM_T_FRACAVE);
2175                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_CRYPT);
2176                 MOVE_PLIST(ROOM_T_INNER_FEAT, ROOM_T_OVAL);
2177         }
2178
2179         /*! @details ダンジョンにCAVEフラグがある場合、NORMALの生成枠がFRACAVEに与えられる。/ CAVE dungeon (Orc floor_ptr->grid_array etc.) */
2180         else if (d_info[floor_ptr->dungeon_idx].flags1 & DF1_CAVE)
2181         {
2182                 MOVE_PLIST(ROOM_T_FRACAVE, ROOM_T_NORMAL);
2183         }
2184
2185         /*! @details ダンジョンの基本地形が最初から渓谷かアリーナ型の場合 FRACAVE は生成から除外。 /  No caves when a (random) cavern exists: they look bad */
2186         else if (dun->cavern || dun->empty_level)
2187         {
2188                 prob_list[ROOM_T_FRACAVE] = 0;
2189         }
2190
2191         /*! @details ダンジョンに最初からGLASS_ROOMフラグがある場合、GLASS を生成から除外。/ Forbidden glass rooms */
2192         if (!(d_info[floor_ptr->dungeon_idx].flags1 & DF1_GLASS_ROOM))
2193         {
2194                 prob_list[ROOM_T_GLASS] = 0;
2195         }
2196
2197         /*! @details ARCADEは同フラグがダンジョンにないと生成されない。 / Forbidden glass rooms */
2198         if (!(d_info[floor_ptr->dungeon_idx].flags1 & DF1_ARCADE))
2199         {
2200                 prob_list[ROOM_T_ARCADE] = 0;
2201         }
2202
2203         /*
2204          * Initialize number of rooms,
2205          * And calcurate total probability.
2206          */
2207         for (total_prob = 0, i = 0; i < ROOM_T_MAX; i++)
2208         {
2209                 room_num[i] = 0;
2210                 total_prob += prob_list[i];
2211         }
2212
2213         /*
2214          * Prepare the number of rooms, of all types, we should build
2215          * on this level.
2216          */
2217         for (i = dun_rooms; i > 0; i--)
2218         {
2219                 int room_type;
2220                 int rand = randint0(total_prob);
2221
2222                 /* Get room_type randomly */
2223                 for (room_type = 0; room_type < ROOM_T_MAX; room_type++)
2224                 {
2225                         if (rand < prob_list[room_type]) break;
2226                         else rand -= prob_list[room_type];
2227                 }
2228                 if (room_type >= ROOM_T_MAX) room_type = ROOM_T_NORMAL;
2229
2230                 /* Increase the number of rooms of that type we should build. */
2231                 room_num[room_type]++;
2232
2233                 switch (room_type)
2234                 {
2235                 case ROOM_T_NEST:
2236                 case ROOM_T_PIT:
2237                 case ROOM_T_LESSER_VAULT:
2238                 case ROOM_T_TRAP_PIT:
2239                 case ROOM_T_GLASS:
2240                 case ROOM_T_ARCADE:
2241
2242                         /* Large room */
2243                         i -= 2;
2244                         break;
2245
2246                 case ROOM_T_GREATER_VAULT:
2247                 case ROOM_T_RANDOM_VAULT:
2248
2249                         /* Largest room */
2250                         i -= 3;
2251                         break;
2252                 }
2253         }
2254
2255         /*
2256          * Build each type of room one by one until we cannot build any more.
2257          * [from SAngband (originally from OAngband)]
2258          */
2259         while (TRUE)
2260         {
2261                 /* Assume no remaining rooms */
2262                 remain = FALSE;
2263
2264                 for (i = 0; i < ROOM_T_MAX; i++)
2265                 {
2266                         /* What type of room are we building now? */
2267                         int room_type = room_build_order[i];
2268
2269                         /* Go next if none available */
2270                         if (!room_num[room_type]) continue;
2271
2272                         /* Use up one unit */
2273                         room_num[room_type]--;
2274
2275                         /* Build the room. */
2276                         if (room_build(player_ptr, room_type))
2277                         {
2278                                 /* Increase the room built count. */
2279                                 rooms_built++;
2280
2281                                 /* Mark as there was some remaining rooms */
2282                                 remain = TRUE;
2283
2284                                 switch (room_type)
2285                                 {
2286                                 case ROOM_T_PIT:
2287                                 case ROOM_T_NEST:
2288                                 case ROOM_T_TRAP_PIT:
2289
2290                                         /* Avoid too many monsters */
2291                                         if (++crowded >= 2)
2292                                         {
2293                                                 room_num[ROOM_T_PIT] = 0;
2294                                                 room_num[ROOM_T_NEST] = 0;
2295                                                 room_num[ROOM_T_TRAP_PIT] = 0;
2296                                         }
2297                                         break;
2298
2299                                 case ROOM_T_ARCADE:
2300
2301                                         /* Avoid double-town */
2302                                         room_num[ROOM_T_ARCADE] = 0;
2303                                         break;
2304                                 }
2305                         }
2306                 }
2307
2308                 /* End loop if no room remain */
2309                 if (!remain) break;
2310         }
2311
2312         /*! @details 部屋生成数が2未満の場合生成失敗を返す */
2313         if (rooms_built < 2)
2314         {
2315                 msg_format_wizard(CHEAT_DUNGEON, _("部屋数が2未満でした。生成を再試行します。", "Number of rooms was under 2. Retry."), rooms_built);
2316                 return FALSE;
2317         }
2318
2319         msg_format_wizard(CHEAT_DUNGEON, _("このダンジョンの部屋数は %d です。", "Number of Rooms: %d"), rooms_built);
2320
2321         return TRUE;
2322 }