OSDN Git Service

Merge pull request #2163 from Hourier/Remove-Unnecessary-Braces
[hengbandforosx/hengbandosx.git] / src / room / rooms-builder.cpp
1 /*!
2  * @brief ダンジョンフロアの部屋生成処理 / make rooms. Used by generate.c when creating dungeons.
3  * @date 2014/01/06
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  * This software may be copied and distributed for educational, research,\n
7  * and not for profit purposes provided that this copyright and statement\n
8  * are included in all such copies.  Other copyrights may also apply.\n
9  * 2014 Deskull rearranged comment for Doxygen. \n
10  * @details
11  * Room building routines.\n
12  *\n
13  * Room types:\n
14  *   1 -- normal\n
15  *   2 -- overlapping\n
16  *   3 -- cross shaped\n
17  *   4 -- large room with features\n
18  *   5 -- monster nests\n
19  *   6 -- monster pits\n
20  *   7 -- simple vaults\n
21  *   8 -- greater vaults\n
22  *   9 -- fractal caves\n
23  *  10 -- random vaults\n
24  *  11 -- circular rooms\n
25  *  12 -- crypts\n
26  *  13 -- trapped monster pits\n
27  *  14 -- trapped room\n
28  *  15 -- glass room\n
29  *  16 -- underground arcade\n
30  *\n
31  * Some functions are used to determine if the given monster\n
32  * is appropriate for inclusion in a monster nest or monster pit or\n
33  * the given type.\n
34  *\n
35  * None of the pits/nests are allowed to include "unique" monsters.\n
36  */
37
38 #include "room/rooms-builder.h"
39 #include "dungeon/dungeon-flag-types.h"
40 #include "dungeon/dungeon.h"
41 #include "floor/cave.h"
42 #include "grid/door.h"
43 #include "grid/feature.h"
44 #include "grid/grid.h"
45 #include "room/cave-filler.h"
46 #include "room/door-definition.h"
47 #include "room/lake-types.h"
48 #include "system/floor-type-definition.h"
49 #include "system/grid-type-definition.h"
50 #include "system/player-type-definition.h"
51 #include "view/display-messages.h"
52
53 /*!
54  * @brief 1マスだけの部屋を作成し、上下左右いずれか一つに隠しドアを配置する。
55  * @param player_ptr プレイヤーへの参照ポインタ
56  * @param y0 配置したい中心のY座標
57  * @param x0 配置したい中心のX座標
58  * @details
59  * This funtion makes a very small room centred at (x0, y0)
60  * This is used in crypts, and random elemental vaults.
61  *
62  * Note - this should be used only on allocated regions
63  * within another room.
64  */
65 void build_small_room(PlayerType *player_ptr, POSITION x0, POSITION y0)
66 {
67     for (POSITION y = y0 - 1; y <= y0 + 1; y++) {
68         place_bold(player_ptr, y, x0 - 1, GB_INNER);
69         place_bold(player_ptr, y, x0 + 1, GB_INNER);
70     }
71
72     for (POSITION x = x0 - 1; x <= x0 + 1; x++) {
73         place_bold(player_ptr, y0 - 1, x, GB_INNER);
74         place_bold(player_ptr, y0 + 1, x, GB_INNER);
75     }
76
77     switch (randint0(4)) {
78     case 0:
79         place_secret_door(player_ptr, y0, x0 - 1, DOOR_DEFAULT);
80         break;
81     case 1:
82         place_secret_door(player_ptr, y0, x0 + 1, DOOR_DEFAULT);
83         break;
84     case 2:
85         place_secret_door(player_ptr, y0 - 1, x0, DOOR_DEFAULT);
86         break;
87     case 3:
88         place_secret_door(player_ptr, y0 + 1, x0, DOOR_DEFAULT);
89         break;
90     }
91
92     player_ptr->current_floor_ptr->grid_array[y0][x0].mimic = 0;
93     place_bold(player_ptr, y0, x0, GB_FLOOR);
94 }
95
96 /*
97  * Builds a cave system in the center of the dungeon.
98  */
99 void build_cavern(PlayerType *player_ptr)
100 {
101     bool light = false;
102     bool done = false;
103     auto *floor_ptr = player_ptr->current_floor_ptr;
104     if ((floor_ptr->dun_level <= randint1(50)) && d_info[floor_ptr->dungeon_idx].flags.has_not(DungeonFeatureType::DARKNESS))
105         light = true;
106
107     POSITION xsize = floor_ptr->width - 1;
108     POSITION ysize = floor_ptr->height - 1;
109     POSITION x0 = xsize / 2;
110     POSITION y0 = ysize / 2;
111     xsize = x0 * 2;
112     ysize = y0 * 2;
113
114     while (!done) {
115         int grd = randint1(4) + 4;
116         int roug = randint1(8) * randint1(4);
117         int cutoff = xsize / 2;
118         generate_hmap(floor_ptr, y0 + 1, x0 + 1, xsize, ysize, grd, roug, cutoff);
119         done = generate_fracave(player_ptr, y0 + 1, x0 + 1, xsize, ysize, cutoff, light, false);
120     }
121 }
122
123 /*
124  * makes a lake/collapsed floor in the center of the dungeon
125  */
126 void build_lake(PlayerType *player_ptr, int type)
127 {
128     if ((type < LAKE_T_LAVA) || (type > LAKE_T_FIRE_VAULT)) {
129         msg_format("Invalid lake type (%d)", type);
130         return;
131     }
132
133     auto *floor_ptr = player_ptr->current_floor_ptr;
134     int xsize = floor_ptr->width - 1;
135     int ysize = floor_ptr->height - 1;
136     int x0 = xsize / 2;
137     int y0 = ysize / 2;
138     xsize = x0 * 2;
139     ysize = y0 * 2;
140     bool done = false;
141     while (!done) {
142         int grd = randint1(3) + 4;
143         int roug = randint1(8) * randint1(4);
144         int c3 = 3 * xsize / 4;
145         int c1 = randint0(c3 / 2) + randint0(c3 / 2) - 5;
146         int c2 = (c1 + c3) / 2;
147         generate_hmap(floor_ptr, y0 + 1, x0 + 1, xsize, ysize, grd, roug, c3);
148         done = generate_lake(player_ptr, y0 + 1, x0 + 1, xsize, ysize, c1, c2, c3, type);
149     }
150 }
151
152 /*
153  * Overlay a rectangular room given its bounds
154  * This routine is used by build_room_vault
155  * The area inside the walls is not touched:
156  * only granite is removed- normal walls stay
157  */
158 void build_room(PlayerType *player_ptr, POSITION x1, POSITION x2, POSITION y1, POSITION y2)
159 {
160     int temp;
161     if ((x1 == x2) || (y1 == y2))
162         return;
163
164     if (x1 > x2) {
165         temp = x1;
166         x1 = x2;
167         x2 = temp;
168     }
169
170     if (y1 > y2) {
171         temp = y1;
172         y1 = y2;
173         y2 = temp;
174     }
175
176     POSITION xsize = x2 - x1;
177     POSITION ysize = y2 - y1;
178     auto *floor_ptr = player_ptr->current_floor_ptr;
179     for (int i = 0; i <= xsize; i++) {
180         place_bold(player_ptr, y1, x1 + i, GB_OUTER_NOPERM);
181         floor_ptr->grid_array[y1][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
182         place_bold(player_ptr, y2, x1 + i, GB_OUTER_NOPERM);
183         floor_ptr->grid_array[y2][x1 + i].info |= (CAVE_ROOM | CAVE_ICKY);
184     }
185
186     for (int i = 1; i < ysize; i++) {
187         place_bold(player_ptr, y1 + i, x1, GB_OUTER_NOPERM);
188         floor_ptr->grid_array[y1 + i][x1].info |= (CAVE_ROOM | CAVE_ICKY);
189         place_bold(player_ptr, y1 + i, x2, GB_OUTER_NOPERM);
190         floor_ptr->grid_array[y1 + i][x2].info |= (CAVE_ROOM | CAVE_ICKY);
191     }
192
193     for (POSITION x = 1; x < xsize; x++) {
194         for (POSITION y = 1; y < ysize; y++) {
195             auto *g_ptr = &floor_ptr->grid_array[y1 + y][x1 + x];
196             if (g_ptr->is_extra()) {
197                 place_bold(player_ptr, y1 + y, x1 + x, GB_FLOOR);
198                 g_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
199             } else {
200                 g_ptr->info |= (CAVE_ROOM | CAVE_ICKY);
201             }
202         }
203     }
204 }
205
206 /*
207  * Build a town/ castle by using a recursive algorithm.
208  * Basically divide each region in a probalistic way to create
209  * smaller regions.  When the regions get too small stop.
210  *
211  * The power variable is a measure of how well defended a region is.
212  * This alters the possible choices.
213  */
214 void build_recursive_room(PlayerType *player_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, int power)
215 {
216     POSITION xsize = x2 - x1;
217     POSITION ysize = y2 - y1;
218
219     int choice;
220     if ((power < 3) && (xsize > 12) && (ysize > 12)) {
221         choice = 1;
222     } else {
223         if (power < 10) {
224             if ((randint1(10) > 2) && (xsize < 8) && (ysize < 8)) {
225                 choice = 4;
226             } else {
227                 choice = randint1(2) + 1;
228             }
229         } else {
230             choice = randint1(3) + 1;
231         }
232     }
233
234     switch (choice) {
235     case 1: {
236         /* Outer walls */
237         int x;
238         int y;
239         for (x = x1; x <= x2; x++) {
240             place_bold(player_ptr, y1, x, GB_OUTER);
241             place_bold(player_ptr, y2, x, GB_OUTER);
242         }
243
244         for (y = y1 + 1; y < y2; y++) {
245             place_bold(player_ptr, y, x1, GB_OUTER);
246             place_bold(player_ptr, y, x2, GB_OUTER);
247         }
248
249         if (one_in_(2)) {
250             y = randint1(ysize) + y1;
251             place_bold(player_ptr, y, x1, GB_FLOOR);
252             place_bold(player_ptr, y, x2, GB_FLOOR);
253         } else {
254             x = randint1(xsize) + x1;
255             place_bold(player_ptr, y1, x, GB_FLOOR);
256             place_bold(player_ptr, y2, x, GB_FLOOR);
257         }
258
259         int t1 = randint1(ysize / 3) + y1;
260         int t2 = y2 - randint1(ysize / 3);
261         int t3 = randint1(xsize / 3) + x1;
262         int t4 = x2 - randint1(xsize / 3);
263
264         /* Do outside areas */
265         build_recursive_room(player_ptr, x1 + 1, y1 + 1, x2 - 1, t1, power + 1);
266         build_recursive_room(player_ptr, x1 + 1, t2, x2 - 1, y2, power + 1);
267         build_recursive_room(player_ptr, x1 + 1, t1 + 1, t3, t2 - 1, power + 3);
268         build_recursive_room(player_ptr, t4, t1 + 1, x2 - 1, t2 - 1, power + 3);
269
270         x1 = t3;
271         x2 = t4;
272         y1 = t1;
273         y2 = t2;
274         xsize = x2 - x1;
275         ysize = y2 - y1;
276         power += 2;
277     }
278         /* Fall through */
279     case 4: {
280         /* Try to build a room */
281         if ((xsize < 3) || (ysize < 3)) {
282             for (int y = y1; y < y2; y++) {
283                 for (int x = x1; x < x2; x++) {
284                     place_bold(player_ptr, y, x, GB_INNER);
285                 }
286             }
287
288             return;
289         }
290
291         for (int x = x1 + 1; x <= x2 - 1; x++) {
292             place_bold(player_ptr, y1 + 1, x, GB_INNER);
293             place_bold(player_ptr, y2 - 1, x, GB_INNER);
294         }
295
296         for (int y = y1 + 1; y <= y2 - 1; y++) {
297             place_bold(player_ptr, y, x1 + 1, GB_INNER);
298             place_bold(player_ptr, y, x2 - 1, GB_INNER);
299         }
300
301         int y = randint1(ysize - 3) + y1 + 1;
302         if (one_in_(2)) {
303             /* left */
304             place_bold(player_ptr, y, x1 + 1, GB_FLOOR);
305         } else {
306             /* right */
307             place_bold(player_ptr, y, x2 - 1, GB_FLOOR);
308         }
309
310         build_recursive_room(player_ptr, x1 + 2, y1 + 2, x2 - 2, y2 - 2, power + 3);
311         break;
312     }
313     case 2: {
314         /* Try and divide vertically */
315         if (xsize < 3) {
316             for (int y = y1; y < y2; y++) {
317                 for (int x = x1; x < x2; x++) {
318                     place_bold(player_ptr, y, x, GB_INNER);
319                 }
320             }
321             return;
322         }
323
324         int t1 = randint1(xsize - 2) + x1 + 1;
325         build_recursive_room(player_ptr, x1, y1, t1, y2, power - 2);
326         build_recursive_room(player_ptr, t1 + 1, y1, x2, y2, power - 2);
327         break;
328     }
329     case 3: {
330         /* Try and divide horizontally */
331         if (ysize < 3) {
332             for (int y = y1; y < y2; y++)
333                 for (int x = x1; x < x2; x++)
334                     place_bold(player_ptr, y, x, GB_INNER);
335
336             return;
337         }
338
339         int t1 = randint1(ysize - 2) + y1 + 1;
340         build_recursive_room(player_ptr, x1, y1, x2, t1, power - 2);
341         build_recursive_room(player_ptr, x1, t1 + 1, x2, y2, power - 2);
342         break;
343     }
344     }
345 }
346
347 /*
348  * Add outer wall to a floored region
349  * Note: no range checking is done so must be inside dungeon
350  * This routine also stomps on doors
351  */
352 void add_outer_wall(PlayerType *player_ptr, POSITION x, POSITION y, int light, POSITION x1, POSITION y1, POSITION x2, POSITION y2)
353 {
354     auto *floor_ptr = player_ptr->current_floor_ptr;
355     if (!in_bounds(floor_ptr, y, x))
356         return;
357
358     grid_type *g_ptr;
359     g_ptr = &floor_ptr->grid_array[y][x];
360     if (g_ptr->is_room())
361         return;
362
363     g_ptr->info |= CAVE_ROOM;
364     feature_type *f_ptr;
365     f_ptr = &f_info[g_ptr->feat];
366     if (g_ptr->is_floor()) {
367         for (int i = -1; i <= 1; i++) {
368             for (int j = -1; j <= 1; j++) {
369                 if ((x + i >= x1) && (x + i <= x2) && (y + j >= y1) && (y + j <= y2)) {
370                     add_outer_wall(player_ptr, x + i, y + j, light, x1, y1, x2, y2);
371                     if (light)
372                         g_ptr->info |= CAVE_GLOW;
373                 }
374             }
375         }
376
377         return;
378     }
379
380     if (g_ptr->is_extra()) {
381         place_bold(player_ptr, y, x, GB_OUTER);
382         if (light)
383             g_ptr->info |= CAVE_GLOW;
384
385         return;
386     }
387
388     if (permanent_wall(f_ptr)) {
389         if (light)
390             g_ptr->info |= CAVE_GLOW;
391     }
392 }
393
394 /*
395  * Hacked distance formula - gives the 'wrong' answer.
396  * Used to build crypts
397  */
398 POSITION dist2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, POSITION h1, POSITION h2, POSITION h3, POSITION h4)
399 {
400     POSITION dx = abs(x2 - x1);
401     POSITION dy = abs(y2 - y1);
402     if (dx >= 2 * dy)
403         return dx + (dy * h1) / h2;
404
405     if (dy >= 2 * dx)
406         return dy + (dx * h1) / h2;
407
408     return ((dx + dy) * 128) / 181 + (dx * dx / (dy * h3) + dy * dy / (dx * h3)) * h4;
409 }