OSDN Git Service

[Refactor] 店舗の種類を enum class StoreSaleType 型に変更しリファクタリング。
[hengbandforosx/hengbandosx.git] / src / room / rooms-city.cpp
1 #include "room/rooms-city.h"
2 #include "floor/floor-generator.h"
3 #include "floor/wild.h"
4 #include "game-option/cheat-types.h"
5 #include "grid/feature.h"
6 #include "grid/grid.h"
7 #include "room/space-finder.h"
8 #include "store/store-util.h"
9 #include "store/store.h"
10 #include "system/floor-type-definition.h"
11 #include "system/grid-type-definition.h"
12 #include "system/player-type-definition.h"
13 #include "util/bit-flags-calculator.h"
14 #include "wizard/wizard-messages.h"
15
16 #include <algorithm>
17
18 /*
19  * Precalculate buildings' location of underground arcade
20  */
21 static bool precalc_ugarcade(int town_hgt, int town_wid, int n, std::vector<ugbldg_type>& ugbldg)
22 {
23     POSITION i, y, x, center_y, center_x;
24     int tmp, attempt = 10000;
25     POSITION max_bldg_hgt = 3 * town_hgt / MAX_TOWN_HGT;
26     POSITION max_bldg_wid = 5 * town_wid / MAX_TOWN_WID;
27     ugbldg_type *cur_ugbldg;
28     std::vector<std::vector<bool>> ugarcade_used(town_hgt, std::vector<bool>(town_wid));
29     bool abort;
30
31     for (i = 0; i < n; i++) {
32         cur_ugbldg = &ugbldg[i];
33         *cur_ugbldg = {};
34         do {
35             center_y = rand_range(2, town_hgt - 3);
36             center_x = rand_range(2, town_wid - 3);
37             tmp = center_y - randint1(max_bldg_hgt);
38             cur_ugbldg->y0 = MAX(tmp, 1);
39             tmp = center_x - randint1(max_bldg_wid);
40             cur_ugbldg->x0 = MAX(tmp, 1);
41             tmp = center_y + randint1(max_bldg_hgt);
42             cur_ugbldg->y1 = MIN(tmp, town_hgt - 2);
43             tmp = center_x + randint1(max_bldg_wid);
44             cur_ugbldg->x1 = MIN(tmp, town_wid - 2);
45             for (abort = false, y = cur_ugbldg->y0; (y <= cur_ugbldg->y1) && !abort; y++) {
46                 for (x = cur_ugbldg->x0; x <= cur_ugbldg->x1; x++) {
47                     if (ugarcade_used[y][x]) {
48                         abort = true;
49                         break;
50                     }
51                 }
52             }
53
54             attempt--;
55         } while (abort && attempt);
56
57         if (!attempt)
58             break;
59
60         for (y = cur_ugbldg->y0 - 1; y <= cur_ugbldg->y1 + 1; y++) {
61             for (x = cur_ugbldg->x0 - 1; x <= cur_ugbldg->x1 + 1; x++) {
62                 ugarcade_used[y][x] = true;
63             }
64         }
65     }
66
67     return i == n;
68 }
69
70 /* Create a new floor room with optional light */
71 static void generate_room_floor(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, int light)
72 {
73     grid_type *g_ptr;
74     for (POSITION y = y1; y <= y2; y++) {
75         for (POSITION x = x1; x <= x2; x++) {
76             g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
77             place_grid(player_ptr, g_ptr, GB_FLOOR);
78             g_ptr->info |= (CAVE_ROOM);
79             if (light)
80                 g_ptr->info |= (CAVE_GLOW);
81         }
82     }
83 }
84
85 static void generate_fill_perm_bold(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
86 {
87     for (POSITION y = y1; y <= y2; y++)
88         for (POSITION x = x1; x <= x2; x++)
89             place_bold(player_ptr, y, x, GB_INNER_PERM);
90 }
91
92 /*!
93  * @brief タイプ16の部屋…地下都市生成のサブルーチン / Actually create buildings
94  * @param player_ptr プレイヤーへの参照ポインタ
95  * @param ltcy 生成基準Y座標
96  * @param ltcx 生成基準X座標
97  * @param stotes[] 生成する店舗のリスト
98  * @param n 生成する店舗の数
99  * @note
100  * Note: ltcy and ltcx indicate "left top corner".
101  */
102 static void build_stores(player_type *player_ptr, POSITION ltcy, POSITION ltcx, StoreSaleType stores[], int n, const std::vector<ugbldg_type>& ugbldg)
103 {
104     int i;
105     POSITION y, x;
106     const ugbldg_type *cur_ugbldg;
107
108     for (i = 0; i < n; i++) {
109         cur_ugbldg = &ugbldg[i];
110         generate_room_floor(player_ptr, ltcy + cur_ugbldg->y0 - 2, ltcx + cur_ugbldg->x0 - 2, ltcy + cur_ugbldg->y1 + 2, ltcx + cur_ugbldg->x1 + 2, false);
111     }
112
113     for (i = 0; i < n; i++) {
114         cur_ugbldg = &ugbldg[i];
115         generate_fill_perm_bold(player_ptr, ltcy + cur_ugbldg->y0, ltcx + cur_ugbldg->x0, ltcy + cur_ugbldg->y1, ltcx + cur_ugbldg->x1);
116
117         /* Pick a door direction (S,N,E,W) */
118         switch (randint0(4)) {
119             /* Bottom side */
120         case 0:
121             y = cur_ugbldg->y1;
122             x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
123             break;
124
125             /* Top side */
126         case 1:
127             y = cur_ugbldg->y0;
128             x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
129             break;
130
131             /* Right side */
132         case 2:
133             y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
134             x = cur_ugbldg->x1;
135             break;
136
137             /* Left side */
138         default:
139             y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
140             x = cur_ugbldg->x0;
141             break;
142         }
143
144         if (auto it = std::find_if(f_info.begin(), f_info.end(),
145                 [subtype = stores[i]](const feature_type &f_ref) {
146                     return f_ref.flags.has(FF::STORE) && (i2enum<StoreSaleType>(static_cast<int>(f_ref.subtype)) == subtype);
147                 });
148             it != f_info.end()) {
149             cave_set_feat(player_ptr, ltcy + y, ltcx + x, (*it).idx);
150             store_init(NO_TOWN, stores[i]);
151         }
152     }
153 }
154
155 /*!
156  * @brief タイプ16の部屋…地下都市の生成 / Type 16 -- Underground Arcade
157  * @details
158  * Town logic flow for generation of new town\n
159  * Originally from Vanilla 3.0.3\n
160  *\n
161  * We start with a fully wiped grids of normal floors.\n
162  *\n
163  * Note that town_gen_hack() plays games with the R.N.G.\n
164  *\n
165  * This function does NOT do anything about the owners of the stores,\n
166  * nor the contents thereof.  It only handles the physical layout.\n
167  */
168 bool build_type16(player_type *player_ptr, dun_data_type *dd_ptr)
169 {
170     StoreSaleType stores[] = {
171         StoreSaleType::GENERAL,
172         StoreSaleType::ARMOURY,
173         StoreSaleType::WEAPON,
174         StoreSaleType::TEMPLE,
175         StoreSaleType::ALCHEMIST,
176         StoreSaleType::MAGIC,
177         StoreSaleType::BLACK,
178         StoreSaleType::BOOK,
179     };
180     int n = sizeof stores / sizeof(int);
181     POSITION y1, x1, yval, xval;
182     int town_hgt = rand_range(MIN_TOWN_HGT, MAX_TOWN_HGT);
183     int town_wid = rand_range(MIN_TOWN_WID, MAX_TOWN_WID);
184
185     if (!n)
186         return false;
187
188     std::vector<ugbldg_type> ugbldg(n);
189     if (!precalc_ugarcade(town_hgt, town_wid, n, ugbldg)) {
190         return false;
191     }
192
193     if (!find_space(player_ptr, dd_ptr, &yval, &xval, town_hgt + 4, town_wid + 4)) {
194         return false;
195     }
196
197     y1 = yval - (town_hgt / 2);
198     x1 = xval - (town_wid / 2);
199     generate_room_floor(player_ptr, y1 + town_hgt / 3, x1 + town_wid / 3, y1 + town_hgt * 2 / 3, x1 + town_wid * 2 / 3, false);
200     build_stores(player_ptr, y1, x1, stores, n, ugbldg);
201     msg_print_wizard(player_ptr, CHEAT_DUNGEON, _("地下街を生成しました", "Underground arcade was generated."));
202     return true;
203 }