OSDN Git Service

[Refactor] #40569 Separated floor-type-definition.h from floor.h
[hengband/hengband.git] / src / monster-floor / monster-generator.c
1 /*!
2  * todo 後で再分割する
3  * @brief モンスター生成処理
4  * @date 2020/06/10
5  * @author Hourier
6  */
7
8 #include "monster-floor/monster-generator.h"
9 #include "dungeon/dungeon.h"
10 #include "game-option/cheat-options.h"
11 #include "grid/grid.h"
12 #include "monster-floor/one-monster-placer.h"
13 #include "monster-floor/place-monster-types.h"
14 #include "monster-race/monster-race-hook.h"
15 #include "monster-race/monster-race.h"
16 #include "monster-race/race-flags1.h"
17 #include "monster-race/race-flags7.h"
18 #include "monster-race/race-indice-types.h"
19 #include "monster/monster-flag-types.h"
20 #include "monster/monster-info.h"
21 #include "monster/monster-list.h"
22 #include "monster/monster-util.h"
23 #include "monster/smart-learn-types.h"
24 #include "mspell/summon-checker.h"
25 #include "spell/spells-summon.h"
26 #include "system/floor-type-definition.h"
27 #include "util/string-processor.h"
28 #include "view/display-messages.h"
29
30 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
31
32 /*!
33  * @var place_monster_idx
34  * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
35  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
36  */
37 static MONSTER_IDX place_monster_idx = 0;
38
39 /*!
40  * @var place_monster_m_idx
41  * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
42  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
43  */
44 static MONSTER_IDX place_monster_m_idx = 0;
45
46 /*!
47  * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
48  * @param player_ptr プレーヤーへの参照ポインタ
49  * @param r_idx 生成モンスター種族
50  * @param yp 結果生成位置y座標
51  * @param xp 結果生成位置x座標
52  * @param y 中心生成位置y座標
53  * @param x 中心生成位置x座標
54  * @param max_dist 生成位置の最大半径
55  * @return 成功したらtrue
56  *
57  */
58 bool mon_scatter(player_type *player_ptr, MONRACE_IDX r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
59 {
60     POSITION place_x[MON_SCAT_MAXD];
61     POSITION place_y[MON_SCAT_MAXD];
62     int num[MON_SCAT_MAXD];
63
64     if (max_dist >= MON_SCAT_MAXD)
65         return FALSE;
66
67     int i;
68     for (i = 0; i < MON_SCAT_MAXD; i++)
69         num[i] = 0;
70
71     floor_type *floor_ptr = player_ptr->current_floor_ptr;
72     for (POSITION nx = x - max_dist; nx <= x + max_dist; nx++) {
73         for (POSITION ny = y - max_dist; ny <= y + max_dist; ny++) {
74             if (!in_bounds(floor_ptr, ny, nx))
75                 continue;
76             if (!projectable(player_ptr, y, x, ny, nx))
77                 continue;
78             if (r_idx > 0) {
79                 monster_race *r_ptr = &r_info[r_idx];
80                 if (!monster_can_enter(player_ptr, ny, nx, r_ptr, 0))
81                     continue;
82             } else {
83                 if (!is_cave_empty_bold2(player_ptr, ny, nx))
84                     continue;
85                 if (pattern_tile(floor_ptr, ny, nx))
86                     continue;
87             }
88
89             i = distance(y, x, ny, nx);
90             if (i > max_dist)
91                 continue;
92
93             num[i]++;
94             if (one_in_(num[i])) {
95                 place_x[i] = nx;
96                 place_y[i] = ny;
97             }
98         }
99     }
100
101     i = 0;
102     while (i < MON_SCAT_MAXD && 0 == num[i])
103         i++;
104     if (i >= MON_SCAT_MAXD)
105         return FALSE;
106
107     *xp = place_x[i];
108     *yp = place_y[i];
109
110     return TRUE;
111 }
112
113 /*!
114  * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
115  * @param player_ptr プレーヤーへの参照ポインタ
116  * @param m_idx 増殖するモンスター情報ID
117  * @param clone クローン・モンスター処理ならばtrue
118  * @param mode 生成オプション
119  * @return 生成できたらtrueを返す
120  * @details
121  * Note that "reproduction" REQUIRES empty space.
122  */
123 bool multiply_monster(player_type *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
124 {
125     floor_type *floor_ptr = player_ptr->current_floor_ptr;
126     monster_type *m_ptr = &floor_ptr->m_list[m_idx];
127     POSITION y, x;
128     if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1))
129         return FALSE;
130
131     if (m_ptr->mflag2 & MFLAG2_NOPET)
132         mode |= PM_NO_PET;
133
134     if (!place_monster_aux(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY)))
135         return FALSE;
136
137     if (clone || (m_ptr->smart & SM_CLONED)) {
138         floor_ptr->m_list[hack_m_idx_ii].smart |= SM_CLONED;
139         floor_ptr->m_list[hack_m_idx_ii].mflag2 |= MFLAG2_NOPET;
140     }
141
142     return TRUE;
143 }
144
145 /*!
146  * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
147  * @param who 召喚主のモンスター情報ID
148  * @param y 中心生成位置y座標
149  * @param x 中心生成位置x座標
150  * @param r_idx 生成モンスター種族
151  * @param mode 生成オプション
152  * @return 成功したらtrue
153  */
154 static bool place_monster_group(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
155 {
156     monster_race *r_ptr = &r_info[r_idx];
157     int total = randint1(10);
158
159     floor_type *floor_ptr = player_ptr->current_floor_ptr;
160     int extra = 0;
161     if (r_ptr->level > floor_ptr->dun_level) {
162         extra = r_ptr->level - floor_ptr->dun_level;
163         extra = 0 - randint1(extra);
164     } else if (r_ptr->level < floor_ptr->dun_level) {
165         extra = floor_ptr->dun_level - r_ptr->level;
166         extra = randint1(extra);
167     }
168
169     if (extra > 9)
170         extra = 9;
171
172     total += extra;
173
174     if (total < 1)
175         total = 1;
176     if (total > GROUP_MAX)
177         total = GROUP_MAX;
178
179     int hack_n = 1;
180     POSITION hack_x[GROUP_MAX];
181     hack_x[0] = x;
182     POSITION hack_y[GROUP_MAX];
183     hack_y[0] = y;
184
185     for (int n = 0; (n < hack_n) && (hack_n < total); n++) {
186         POSITION hx = hack_x[n];
187         POSITION hy = hack_y[n];
188         for (int i = 0; (i < 8) && (hack_n < total); i++) {
189             POSITION mx, my;
190             scatter(player_ptr, &my, &mx, hy, hx, 4, 0);
191             if (!is_cave_empty_bold2(player_ptr, my, mx))
192                 continue;
193
194             if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
195                 hack_y[hack_n] = my;
196                 hack_x[hack_n] = mx;
197                 hack_n++;
198             }
199         }
200     }
201
202     return TRUE;
203 }
204
205 /*!
206  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
207  * @param r_idx チェックするモンスター種族のID
208  * @return 護衛にできるならばtrue
209  */
210 static bool place_monster_can_escort(player_type *player_ptr, MONRACE_IDX r_idx)
211 {
212     monster_race *r_ptr = &r_info[place_monster_idx];
213     monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[place_monster_m_idx];
214     monster_race *z_ptr = &r_info[r_idx];
215
216     if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx))
217         return FALSE;
218     if (z_ptr->d_char != r_ptr->d_char)
219         return FALSE;
220     if (z_ptr->level > r_ptr->level)
221         return FALSE;
222     if (z_ptr->flags1 & RF1_UNIQUE)
223         return FALSE;
224     if (place_monster_idx == r_idx)
225         return FALSE;
226     if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr))
227         return FALSE;
228
229     if (r_ptr->flags7 & RF7_FRIENDLY) {
230         if (monster_has_hostile_align(player_ptr, NULL, 1, -1, z_ptr))
231             return FALSE;
232     }
233
234     if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON))
235         return FALSE;
236
237     return TRUE;
238 }
239
240 /*!
241  * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
242  * @param player_ptr プレーヤーへの参照ポインタ
243  * @param who 召喚主のモンスター情報ID
244  * @param y 生成地点y座標
245  * @param x 生成地点x座標
246  * @param r_idx 生成するモンスターの種族ID
247  * @param mode 生成オプション
248  * @return 生成に成功したらtrue
249  */
250 bool place_monster_aux(player_type *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MONRACE_IDX r_idx, BIT_FLAGS mode)
251 {
252     monster_race *r_ptr = &r_info[r_idx];
253
254     if (!(mode & PM_NO_KAGE) && one_in_(333))
255         mode |= PM_KAGE;
256
257     if (!place_monster_one(player_ptr, who, y, x, r_idx, mode))
258         return FALSE;
259     if (!(mode & PM_ALLOW_GROUP))
260         return TRUE;
261
262     place_monster_m_idx = hack_m_idx_ii;
263
264     /* Reinforcement */
265     for (int i = 0; i < 6; i++) {
266         if (!r_ptr->reinforce_id[i])
267             break;
268         int n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
269         for (int j = 0; j < n; j++) {
270             POSITION nx, ny, d = 7;
271             scatter(player_ptr, &ny, &nx, y, x, d, 0);
272             (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, r_ptr->reinforce_id[i], mode);
273         }
274     }
275
276     if (r_ptr->flags1 & (RF1_FRIENDS)) {
277         (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
278     }
279
280     if (!(r_ptr->flags1 & (RF1_ESCORT)))
281         return TRUE;
282
283     place_monster_idx = r_idx;
284     for (int i = 0; i < 32; i++) {
285         POSITION nx, ny, d = 3;
286         MONRACE_IDX z;
287         scatter(player_ptr, &ny, &nx, y, x, d, 0);
288         if (!is_cave_empty_bold2(player_ptr, ny, nx))
289             continue;
290
291         get_mon_num_prep(player_ptr, place_monster_can_escort, get_monster_hook2(player_ptr, ny, nx));
292         z = get_mon_num(player_ptr, r_ptr->level, 0);
293         if (!z)
294             break;
295
296         (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, z, mode);
297         if ((r_info[z].flags1 & RF1_FRIENDS) || (r_ptr->flags1 & RF1_ESCORTS)) {
298             (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
299         }
300     }
301
302     return TRUE;
303 }
304
305 /*!
306  * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
307  * @param player_ptr プレーヤーへの参照ポインタ
308  * @param y 生成地点y座標
309  * @param x 生成地点x座標
310  * @param mode 生成オプション
311  * @return 生成に成功したらtrue
312  */
313 bool place_monster(player_type *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
314 {
315     MONRACE_IDX r_idx;
316     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
317     r_idx = get_mon_num(player_ptr, player_ptr->current_floor_ptr->monster_level, 0);
318     if (!r_idx)
319         return FALSE;
320
321     if ((one_in_(5) || (player_ptr->current_floor_ptr->base_level == 0)) && !(r_info[r_idx].flags1 & RF1_UNIQUE)
322         && angband_strchr("hkoptuyAHLOPTUVY", r_info[r_idx].d_char)) {
323         mode |= PM_JURAL;
324     }
325
326     if (place_monster_aux(player_ptr, 0, y, x, r_idx, mode))
327         return TRUE;
328
329     return FALSE;
330 }
331
332 /*!
333  * @brief 指定地点に1種類のモンスター種族による群れを生成する
334  * @param player_ptr プレーヤーへの参照ポインタ
335  * @param y 生成地点y座標
336  * @param x 生成地点x座標
337  * @return 生成に成功したらtrue
338  */
339 bool alloc_horde(player_type *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
340 {
341     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
342
343     floor_type *floor_ptr = player_ptr->current_floor_ptr;
344     MONRACE_IDX r_idx = 0;
345     int attempts = 1000;
346     monster_race *r_ptr = NULL;
347     while (--attempts) {
348         r_idx = get_mon_num(player_ptr, floor_ptr->monster_level, 0);
349         if (!r_idx)
350             return FALSE;
351
352         r_ptr = &r_info[r_idx];
353         if (r_ptr->flags1 & RF1_UNIQUE)
354             continue;
355
356         if (r_idx == MON_HAGURE)
357             continue;
358         break;
359     }
360
361     if (attempts < 1)
362         return FALSE;
363
364     attempts = 1000;
365
366     while (--attempts) {
367         if (place_monster_aux(player_ptr, 0, y, x, r_idx, 0L))
368             break;
369     }
370
371     if (attempts < 1)
372         return FALSE;
373
374     MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
375     if (floor_ptr->m_list[m_idx].mflag2 & MFLAG2_CHAMELEON)
376         r_ptr = &r_info[floor_ptr->m_list[m_idx].r_idx];
377
378     POSITION cy = y;
379     POSITION cx = x;
380     for (attempts = randint1(10) + 5; attempts; attempts--) {
381         scatter(player_ptr, &cy, &cx, y, x, 5, 0);
382         (void)(*summon_specific)(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
383         y = cy;
384         x = cx;
385     }
386
387     if (cheat_hear)
388         msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
389     return TRUE;
390 }
391
392 /*!
393  * @brief ダンジョンの主生成を試みる / Put the Guardian
394  * @param player_ptr プレーヤーへの参照ポインタ
395  * @param def_val 現在の主の生成状態
396  * @return 生成に成功したらtrue
397  */
398 bool alloc_guardian(player_type *player_ptr, bool def_val)
399 {
400     MONRACE_IDX guardian = d_info[player_ptr->dungeon_idx].final_guardian;
401     floor_type *floor_ptr = player_ptr->current_floor_ptr;
402     bool is_guardian_applicable = guardian > 0;
403     is_guardian_applicable &= d_info[player_ptr->dungeon_idx].maxdepth == floor_ptr->dun_level;
404     is_guardian_applicable &= r_info[guardian].cur_num < r_info[guardian].max_num;
405     if (!is_guardian_applicable)
406         return def_val;
407
408     int try_count = 4000;
409     while (try_count) {
410         POSITION oy = randint1(floor_ptr->height - 4) + 2;
411         POSITION ox = randint1(floor_ptr->width - 4) + 2;
412         if (!is_cave_empty_bold2(player_ptr, oy, ox)) {
413             try_count++;
414             continue;
415         }
416
417         if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0)) {
418             try_count++;
419             continue;
420         }
421
422         if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET)))
423             return TRUE;
424
425         try_count--;
426     }
427
428     return FALSE;
429 }
430
431 /*!
432  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
433  * @param dis プレイヤーから離れるべき最低距離
434  * @param mode 生成オプション
435  * @return 生成に成功したらtrue
436  * @details
437  * Place the monster at least "dis" distance from the player.
438  * Use "slp" to choose the initial "sleep" status
439  * Use "floor_ptr->monster_level" for the monster level
440  */
441 bool alloc_monster(player_type *player_ptr, POSITION dis, BIT_FLAGS mode, summon_specific_pf summon_specific)
442 {
443     if (alloc_guardian(player_ptr, FALSE))
444         return TRUE;
445
446     floor_type *floor_ptr = player_ptr->current_floor_ptr;
447     POSITION y = 0, x = 0;
448     int attempts_left = 10000;
449     while (attempts_left--) {
450         y = randint0(floor_ptr->height);
451         x = randint0(floor_ptr->width);
452
453         if (floor_ptr->dun_level) {
454             if (!is_cave_empty_bold2(player_ptr, y, x))
455                 continue;
456         } else {
457             if (!is_cave_empty_bold(player_ptr, y, x))
458                 continue;
459         }
460
461         if (distance(y, x, player_ptr->y, player_ptr->x) > dis)
462             break;
463     }
464
465     if (!attempts_left) {
466         if (cheat_xtra || cheat_hear) {
467             msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
468         }
469
470         return FALSE;
471     }
472
473     if (randint1(5000) <= floor_ptr->dun_level) {
474         if (alloc_horde(player_ptr, y, x, summon_specific)) {
475             return TRUE;
476         }
477     } else {
478         if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP)))
479             return TRUE;
480     }
481
482     return FALSE;
483 }