OSDN Git Service

[Refactor] #1172 Changed remaining FALSE to false
[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/player-type-definition.h"
12 #include "util/bit-flags-calculator.h"
13 #include "wizard/wizard-messages.h"
14
15 static ugbldg_type *ugbldg;
16
17 /*
18  * Precalculate buildings' location of underground arcade
19  */
20 static bool precalc_ugarcade(int town_hgt, int town_wid, int n)
21 {
22     POSITION i, y, x, center_y, center_x;
23     int tmp, attempt = 10000;
24     POSITION max_bldg_hgt = 3 * town_hgt / MAX_TOWN_HGT;
25     POSITION max_bldg_wid = 5 * town_wid / MAX_TOWN_WID;
26     ugbldg_type *cur_ugbldg;
27     bool **ugarcade_used, abort;
28     C_MAKE(ugarcade_used, town_hgt, bool *);
29     C_MAKE(*ugarcade_used, town_hgt * town_wid, bool);
30     for (y = 1; y < town_hgt; y++)
31         ugarcade_used[y] = *ugarcade_used + y * town_wid;
32
33     for (i = 0; i < n; i++) {
34         cur_ugbldg = &ugbldg[i];
35         (void)WIPE(cur_ugbldg, ugbldg_type);
36         do {
37             center_y = rand_range(2, town_hgt - 3);
38             center_x = rand_range(2, town_wid - 3);
39             tmp = center_y - randint1(max_bldg_hgt);
40             cur_ugbldg->y0 = MAX(tmp, 1);
41             tmp = center_x - randint1(max_bldg_wid);
42             cur_ugbldg->x0 = MAX(tmp, 1);
43             tmp = center_y + randint1(max_bldg_hgt);
44             cur_ugbldg->y1 = MIN(tmp, town_hgt - 2);
45             tmp = center_x + randint1(max_bldg_wid);
46             cur_ugbldg->x1 = MIN(tmp, town_wid - 2);
47             for (abort = false, y = cur_ugbldg->y0; (y <= cur_ugbldg->y1) && !abort; y++) {
48                 for (x = cur_ugbldg->x0; x <= cur_ugbldg->x1; x++) {
49                     if (ugarcade_used[y][x]) {
50                         abort = true;
51                         break;
52                     }
53                 }
54             }
55
56             attempt--;
57         } while (abort && attempt);
58
59         if (!attempt)
60             break;
61
62         for (y = cur_ugbldg->y0 - 1; y <= cur_ugbldg->y1 + 1; y++) {
63             for (x = cur_ugbldg->x0 - 1; x <= cur_ugbldg->x1 + 1; x++) {
64                 ugarcade_used[y][x] = true;
65             }
66         }
67     }
68
69     C_KILL(*ugarcade_used, town_hgt * town_wid, bool);
70     C_KILL(ugarcade_used, town_hgt, bool *);
71     return i == n;
72 }
73
74 /* Create a new floor room with optional light */
75 static void generate_room_floor(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, int light)
76 {
77     grid_type *g_ptr;
78     for (POSITION y = y1; y <= y2; y++) {
79         for (POSITION x = x1; x <= x2; x++) {
80             g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
81             place_grid(player_ptr, g_ptr, GB_FLOOR);
82             g_ptr->info |= (CAVE_ROOM);
83             if (light)
84                 g_ptr->info |= (CAVE_GLOW);
85         }
86     }
87 }
88
89 static void generate_fill_perm_bold(player_type *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
90 {
91     for (POSITION y = y1; y <= y2; y++)
92         for (POSITION x = x1; x <= x2; x++)
93             place_bold(player_ptr, y, x, GB_INNER_PERM);
94 }
95
96 /*!
97  * @brief タイプ16の部屋…地下都市生成のサブルーチン / Actually create buildings
98  * @param player_ptr プレーヤーへの参照ポインタ
99  * @param ltcy 生成基準Y座標
100  * @param ltcx 生成基準X座標
101  * @param stotes[] 生成する店舗のリスト
102  * @param n 生成する店舗の数
103  * @note
104  * Note: ltcy and ltcx indicate "left top corner".
105  */
106 static void build_stores(player_type *player_ptr, POSITION ltcy, POSITION ltcx, int stores[], int n)
107 {
108     int i;
109     POSITION y, x;
110     FEAT_IDX j;
111     ugbldg_type *cur_ugbldg;
112
113     for (i = 0; i < n; i++) {
114         cur_ugbldg = &ugbldg[i];
115         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);
116     }
117
118     for (i = 0; i < n; i++) {
119         cur_ugbldg = &ugbldg[i];
120         generate_fill_perm_bold(player_ptr, ltcy + cur_ugbldg->y0, ltcx + cur_ugbldg->x0, ltcy + cur_ugbldg->y1, ltcx + cur_ugbldg->x1);
121
122         /* Pick a door direction (S,N,E,W) */
123         switch (randint0(4)) {
124             /* Bottom side */
125         case 0:
126             y = cur_ugbldg->y1;
127             x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
128             break;
129
130             /* Top side */
131         case 1:
132             y = cur_ugbldg->y0;
133             x = rand_range(cur_ugbldg->x0, cur_ugbldg->x1);
134             break;
135
136             /* Right side */
137         case 2:
138             y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
139             x = cur_ugbldg->x1;
140             break;
141
142             /* Left side */
143         default:
144             y = rand_range(cur_ugbldg->y0, cur_ugbldg->y1);
145             x = cur_ugbldg->x0;
146             break;
147         }
148
149         for (j = 0; j < max_f_idx; j++) {
150             if (has_flag(f_info[j].flags, FF_STORE)) {
151                 if (f_info[j].subtype == stores[i])
152                     break;
153             }
154         }
155
156         if (j < max_f_idx) {
157             cave_set_feat(player_ptr, ltcy + y, ltcx + x, j);
158             store_init(NO_TOWN, stores[i]);
159         }
160     }
161 }
162
163 /*!
164  * @brief タイプ16の部屋…地下都市の生成 / Type 16 -- Underground Arcade
165  * @details
166  * Town logic flow for generation of new town\n
167  * Originally from Vanilla 3.0.3\n
168  *\n
169  * We start with a fully wiped grids of normal floors.\n
170  *\n
171  * Note that town_gen_hack() plays games with the R.N.G.\n
172  *\n
173  * This function does NOT do anything about the owners of the stores,\n
174  * nor the contents thereof.  It only handles the physical layout.\n
175  */
176 bool build_type16(player_type *player_ptr, dun_data_type *dd_ptr)
177 {
178     int stores[] = {
179         STORE_GENERAL,
180         STORE_ARMOURY,
181         STORE_WEAPON,
182         STORE_TEMPLE,
183         STORE_ALCHEMIST,
184         STORE_MAGIC,
185         STORE_BLACK,
186         STORE_BOOK,
187     };
188     int n = sizeof stores / sizeof(int);
189     POSITION i, y, x, y1, x1, yval, xval;
190     int town_hgt = rand_range(MIN_TOWN_HGT, MAX_TOWN_HGT);
191     int town_wid = rand_range(MIN_TOWN_WID, MAX_TOWN_WID);
192     bool prevent_bm = false;
193     floor_type *floor_ptr = player_ptr->current_floor_ptr;
194     for (y = 0; (y < floor_ptr->height) && !prevent_bm; y++) {
195         for (x = 0; x < floor_ptr->width; x++) {
196             if (floor_ptr->grid_array[y][x].feat == FF_STORE) {
197                 prevent_bm = (f_info[floor_ptr->grid_array[y][x].feat].subtype == STORE_BLACK);
198                 break;
199             }
200         }
201     }
202
203     for (i = 0; i < n; i++)
204         if ((stores[i] == STORE_BLACK) && prevent_bm)
205             stores[i] = stores[--n];
206
207     if (!n)
208         return false;
209
210     C_MAKE(ugbldg, n, ugbldg_type);
211     if (!precalc_ugarcade(town_hgt, town_wid, n)) {
212         C_KILL(ugbldg, n, ugbldg_type);
213         return false;
214     }
215
216     if (!find_space(player_ptr, dd_ptr, &yval, &xval, town_hgt + 4, town_wid + 4)) {
217         C_KILL(ugbldg, n, ugbldg_type);
218         return false;
219     }
220
221     y1 = yval - (town_hgt / 2);
222     x1 = xval - (town_wid / 2);
223     generate_room_floor(player_ptr, y1 + town_hgt / 3, x1 + town_wid / 3, y1 + town_hgt * 2 / 3, x1 + town_wid * 2 / 3, false);
224     build_stores(player_ptr, y1, x1, stores, n);
225     msg_print_wizard(player_ptr, CHEAT_DUNGEON, _("地下街を生成しました", "Underground arcade was generated."));
226     C_KILL(ugbldg, n, ugbldg_type);
227     return true;
228 }