OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / monster-floor / monster-generator.cpp
1 /*!
2  * todo 後で再分割する
3  * @brief モンスター生成処理
4  * @date 2020/06/10
5  * @author Hourier
6  */
7
8 #include "monster-floor/monster-generator.h"
9 #include "effect/effect-characteristics.h"
10 #include "floor/cave.h"
11 #include "floor/floor-util.h"
12 #include "floor/geometry.h"
13 #include "game-option/cheat-options.h"
14 #include "game-option/cheat-types.h"
15 #include "monster-floor/one-monster-placer.h"
16 #include "monster-floor/place-monster-types.h"
17 #include "monster-race/monster-race-hook.h"
18 #include "monster-race/monster-race.h"
19 #include "monster-race/race-flags1.h"
20 #include "monster-race/race-flags7.h"
21 #include "monster-race/race-flags8.h"
22 #include "monster-race/race-indice-types.h"
23 #include "monster/monster-flag-types.h"
24 #include "monster/monster-info.h"
25 #include "monster/monster-list.h"
26 #include "monster/monster-util.h"
27 #include "monster/smart-learn-types.h"
28 #include "mspell/summon-checker.h"
29 #include "spell/summon-types.h"
30 #include "system/dungeon-info.h"
31 #include "system/floor-type-definition.h"
32 #include "system/grid-type-definition.h"
33 #include "system/monster-entity.h"
34 #include "system/monster-race-info.h"
35 #include "system/player-type-definition.h"
36 #include "target/projection-path-calculator.h"
37 #include "util/string-processor.h"
38 #include "view/display-messages.h"
39 #include "wizard/wizard-messages.h"
40 #include <optional>
41
42 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
43
44 /*!
45  * @var place_monster_idx
46  * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
47  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
48  */
49 static MonsterRaceId place_monster_idx = MonsterRace::empty_id();
50
51 /*!
52  * @var place_monster_m_idx
53  * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
54  * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
55  */
56 static MONSTER_IDX place_monster_m_idx = 0;
57
58 /*!
59  * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
60  * @param player_ptr プレイヤーへの参照ポインタ
61  * @param r_idx 生成モンスター種族
62  * @param yp 結果生成位置y座標
63  * @param xp 結果生成位置x座標
64  * @param y 中心生成位置y座標
65  * @param x 中心生成位置x座標
66  * @param max_dist 生成位置の最大半径
67  * @return 成功したらtrue
68  *
69  */
70 bool mon_scatter(PlayerType *player_ptr, MonsterRaceId r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
71 {
72     int place_x[MON_SCAT_MAXD]{};
73     int place_y[MON_SCAT_MAXD]{};
74     int num[MON_SCAT_MAXD]{};
75
76     if (max_dist >= MON_SCAT_MAXD) {
77         return false;
78     }
79
80     int i;
81     for (i = 0; i < MON_SCAT_MAXD; i++) {
82         num[i] = 0;
83     }
84
85     auto *floor_ptr = player_ptr->current_floor_ptr;
86     for (POSITION nx = x - max_dist; nx <= x + max_dist; nx++) {
87         for (POSITION ny = y - max_dist; ny <= y + max_dist; ny++) {
88             if (!in_bounds(floor_ptr, ny, nx)) {
89                 continue;
90             }
91             if (!projectable(player_ptr, y, x, ny, nx)) {
92                 continue;
93             }
94             if (MonsterRace(r_idx).is_valid()) {
95                 auto *r_ptr = &monraces_info[r_idx];
96                 if (!monster_can_enter(player_ptr, ny, nx, r_ptr, 0)) {
97                     continue;
98                 }
99             } else {
100                 if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
101                     continue;
102                 }
103                 if (pattern_tile(floor_ptr, ny, nx)) {
104                     continue;
105                 }
106             }
107
108             i = distance(y, x, ny, nx);
109             if (i > max_dist) {
110                 continue;
111             }
112
113             num[i]++;
114             if (one_in_(num[i])) {
115                 place_x[i] = nx;
116                 place_y[i] = ny;
117             }
118         }
119     }
120
121     i = 0;
122     while (i < MON_SCAT_MAXD && 0 == num[i]) {
123         i++;
124     }
125     if (i >= MON_SCAT_MAXD) {
126         return false;
127     }
128
129     *xp = place_x[i];
130     *yp = place_y[i];
131
132     return true;
133 }
134
135 /*!
136  * @brief モンスターを増殖生成する / Let the given monster attempt to reproduce.
137  * @param player_ptr プレイヤーへの参照ポインタ
138  * @param m_idx 増殖するモンスター情報ID
139  * @param clone クローン・モンスター処理ならばtrue
140  * @param mode 生成オプション
141  * @return 生成できたらtrueを返す
142  * @details
143  * Note that "reproduction" REQUIRES empty space.
144  */
145 bool multiply_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
146 {
147     auto *floor_ptr = player_ptr->current_floor_ptr;
148     auto *m_ptr = &floor_ptr->m_list[m_idx];
149     POSITION y, x;
150     if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1)) {
151         return false;
152     }
153
154     if (m_ptr->mflag2.has(MonsterConstantFlagType::NOPET)) {
155         mode |= PM_NO_PET;
156     }
157
158     if (!place_specific_monster(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY))) {
159         return false;
160     }
161
162     if (clone || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
163         floor_ptr->m_list[hack_m_idx_ii].mflag2.set({ MonsterConstantFlagType::CLONED, MonsterConstantFlagType::NOPET });
164     }
165
166     return true;
167 }
168
169 /*!
170  * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
171  * @param who 召喚主のモンスター情報ID
172  * @param y 中心生成位置y座標
173  * @param x 中心生成位置x座標
174  * @param r_idx 生成モンスター種族
175  * @param mode 生成オプション
176  * @return 成功したらtrue
177  */
178 static bool place_monster_group(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
179 {
180     auto *r_ptr = &monraces_info[r_idx];
181     auto total = randint1(10);
182
183     auto *floor_ptr = player_ptr->current_floor_ptr;
184     auto extra = 0;
185     if (r_ptr->level > floor_ptr->dun_level) {
186         extra = r_ptr->level - floor_ptr->dun_level;
187         extra = 0 - randint1(extra);
188     } else if (r_ptr->level < floor_ptr->dun_level) {
189         extra = floor_ptr->dun_level - r_ptr->level;
190         extra = randint1(extra);
191     }
192
193     if (extra > 9) {
194         extra = 9;
195     }
196
197     total += extra;
198
199     if (total < 1) {
200         total = 1;
201     }
202
203     constexpr auto max_monsters_count = 32;
204     if (total > max_monsters_count) {
205         total = max_monsters_count;
206     }
207
208     auto hack_n = 1;
209     POSITION hack_x[max_monsters_count]{};
210     hack_x[0] = x;
211     POSITION hack_y[max_monsters_count]{};
212     hack_y[0] = y;
213
214     for (auto n = 0; (n < hack_n) && (hack_n < total); n++) {
215         POSITION hx = hack_x[n];
216         POSITION hy = hack_y[n];
217         for (int i = 0; (i < 8) && (hack_n < total); i++) {
218             POSITION mx, my;
219             scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
220             if (!is_cave_empty_bold2(player_ptr, my, mx)) {
221                 continue;
222             }
223
224             if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
225                 hack_y[hack_n] = my;
226                 hack_x[hack_n] = mx;
227                 hack_n++;
228             }
229         }
230     }
231
232     return true;
233 }
234
235 /*!
236  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
237  * @param r_idx チェックするモンスター種族のID
238  * @return 護衛にできるならばtrue
239  */
240 static bool place_monster_can_escort(PlayerType *player_ptr, MonsterRaceId r_idx)
241 {
242     auto *r_ptr = &monraces_info[place_monster_idx];
243     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[place_monster_m_idx];
244     MonsterRaceInfo *z_ptr = &monraces_info[r_idx];
245
246     if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx)) {
247         return false;
248     }
249
250     if (z_ptr->d_char != r_ptr->d_char) {
251         return false;
252     }
253
254     if (z_ptr->level > r_ptr->level) {
255         return false;
256     }
257
258     if (z_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
259         return false;
260     }
261
262     if (place_monster_idx == r_idx) {
263         return false;
264     }
265
266     if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr)) {
267         return false;
268     }
269
270     if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
271         if (monster_has_hostile_align(player_ptr, nullptr, 1, -1, z_ptr)) {
272             return false;
273         }
274     }
275
276     if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON)) {
277         return false;
278     }
279
280     return true;
281 }
282
283 /*!
284  * @brief 特定モンスターを生成する
285  * @param player_ptr プレイヤーへの参照ポインタ
286  * @param who 召喚主のモンスター情報ID
287  * @param y 生成地点y座標
288  * @param x 生成地点x座標
289  * @param r_idx 生成するモンスターの種族ID
290  * @param mode 生成オプション
291  * @return 生成に成功したらtrue
292  * @details 護衛も一緒に生成する
293  */
294 bool place_specific_monster(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
295 {
296     auto *r_ptr = &monraces_info[r_idx];
297
298     if (!(mode & PM_NO_KAGE) && one_in_(333)) {
299         mode |= PM_KAGE;
300     }
301
302     if (!place_monster_one(player_ptr, who, y, x, r_idx, mode)) {
303         return false;
304     }
305     if (!(mode & PM_ALLOW_GROUP)) {
306         return true;
307     }
308
309     place_monster_m_idx = hack_m_idx_ii;
310
311     /* Reinforcement */
312     for (auto [reinforce_r_idx, dd, ds] : r_ptr->reinforces) {
313         if (!MonsterRace(reinforce_r_idx).is_valid()) {
314             continue;
315         }
316         auto n = damroll(dd, ds);
317         for (int j = 0; j < n; j++) {
318             POSITION nx, ny, d;
319             const POSITION scatter_min = 7;
320             const POSITION scatter_max = 40;
321             for (d = scatter_min; d <= scatter_max; d++) {
322                 scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
323                 if (place_monster_one(player_ptr, place_monster_m_idx, ny, nx, reinforce_r_idx, mode)) {
324                     break;
325                 }
326             }
327             if (d > scatter_max) {
328                 msg_format_wizard(player_ptr, CHEAT_MONSTER, _("護衛の指定生成に失敗しました。", "Failed fixed escorts."));
329             }
330         }
331     }
332
333     if (r_ptr->flags1 & (RF1_FRIENDS)) {
334         (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
335     }
336
337     if (!(r_ptr->flags1 & (RF1_ESCORT))) {
338         return true;
339     }
340
341     place_monster_idx = r_idx;
342     for (int i = 0; i < 32; i++) {
343         POSITION nx, ny, d = 3;
344         MonsterRaceId z;
345         scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
346         if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
347             continue;
348         }
349
350         get_mon_num_prep(player_ptr, place_monster_can_escort, get_monster_hook2(player_ptr, ny, nx));
351         z = get_mon_num(player_ptr, 0, r_ptr->level, 0);
352         if (!MonsterRace(z).is_valid()) {
353             break;
354         }
355
356         (void)place_monster_one(player_ptr, place_monster_m_idx, ny, nx, z, mode);
357         if ((monraces_info[z].flags1 & RF1_FRIENDS) || (r_ptr->flags1 & RF1_ESCORTS)) {
358             (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
359         }
360     }
361
362     return true;
363 }
364
365 /*!
366  * @brief フロア相当のモンスターを1体生成する
367  * @param player_ptr プレイヤーへの参照ポインタ
368  * @param y 生成地点y座標
369  * @param x 生成地点x座標
370  * @param mode 生成オプション
371  * @return 生成に成功したらtrue
372  */
373 bool place_random_monster(PlayerType *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
374 {
375     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
376     MonsterRaceId r_idx;
377     do {
378         r_idx = get_mon_num(player_ptr, 0, player_ptr->current_floor_ptr->monster_level, 0);
379     } while ((mode & PM_NO_QUEST) && (monraces_info[r_idx].flags8 & RF8_NO_QUEST));
380
381     if (!MonsterRace(r_idx).is_valid()) {
382         return false;
383     }
384
385     if ((one_in_(5) || (player_ptr->current_floor_ptr->dun_level == 0)) && monraces_info[r_idx].kind_flags.has_not(MonsterKindType::UNIQUE) && angband_strchr("hkoptuyAHLOPTUVY", monraces_info[r_idx].d_char)) {
386         mode |= PM_JURAL;
387     }
388
389     return place_specific_monster(player_ptr, 0, y, x, r_idx, mode);
390 }
391
392 static std::optional<MonsterRaceId> select_horde_leader_r_idx(PlayerType *player_ptr)
393 {
394     const auto *floor_ptr = player_ptr->current_floor_ptr;
395
396     for (auto attempts = 1000; attempts > 0; --attempts) {
397         auto r_idx = get_mon_num(player_ptr, 0, floor_ptr->monster_level, 0);
398         if (!MonsterRace(r_idx).is_valid()) {
399             return std::nullopt;
400         }
401
402         if (monraces_info[r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
403             continue;
404         }
405
406         if (r_idx == MonsterRaceId::HAGURE) {
407             continue;
408         }
409
410         return r_idx;
411     }
412
413     return std::nullopt;
414 }
415
416 /*!
417  * @brief 指定地点に1種類のモンスター種族による群れを生成する
418  * @param player_ptr プレイヤーへの参照ポインタ
419  * @param y 生成地点y座標
420  * @param x 生成地点x座標
421  * @return 生成に成功したらtrue
422  */
423 bool alloc_horde(PlayerType *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
424 {
425     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
426
427     auto r_idx = select_horde_leader_r_idx(player_ptr);
428     if (!r_idx.has_value()) {
429         return false;
430     }
431
432     for (auto attempts = 1000;; --attempts) {
433         if (attempts <= 0) {
434             return false;
435         }
436
437         if (place_specific_monster(player_ptr, 0, y, x, r_idx.value(), 0L)) {
438             break;
439         }
440     }
441
442     auto *floor_ptr = player_ptr->current_floor_ptr;
443     MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
444
445     POSITION cy = y;
446     POSITION cx = x;
447     for (auto attempts = randint1(10) + 5; attempts > 0; attempts--) {
448         scatter(player_ptr, &cy, &cx, y, x, 5, PROJECT_NONE);
449         (void)(*summon_specific)(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
450         y = cy;
451         x = cx;
452     }
453
454     if (!cheat_hear) {
455         return true;
456     }
457
458     auto *r_ptr = &monraces_info[r_idx.value()];
459     if (floor_ptr->m_list[m_idx].mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
460         r_ptr = &monraces_info[floor_ptr->m_list[m_idx].r_idx];
461     }
462
463     msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
464
465     return true;
466 }
467
468 /*!
469  * @brief ダンジョンの主生成を試みる / Put the Guardian
470  * @param player_ptr プレイヤーへの参照ポインタ
471  * @param def_val 現在の主の生成状態
472  * @return 生成に成功したらtrue
473  */
474 bool alloc_guardian(PlayerType *player_ptr, bool def_val)
475 {
476     auto *floor_ptr = player_ptr->current_floor_ptr;
477     const auto &dungeon = floor_ptr->get_dungeon_definition();
478     MonsterRaceId guardian = dungeon.final_guardian;
479     bool is_guardian_applicable = MonsterRace(guardian).is_valid();
480     is_guardian_applicable &= dungeon.maxdepth == floor_ptr->dun_level;
481     is_guardian_applicable &= monraces_info[guardian].cur_num < monraces_info[guardian].max_num;
482     if (!is_guardian_applicable) {
483         return def_val;
484     }
485
486     int try_count = 4000;
487     while (try_count) {
488         POSITION oy = randint1(floor_ptr->height - 4) + 2;
489         POSITION ox = randint1(floor_ptr->width - 4) + 2;
490         if (!is_cave_empty_bold2(player_ptr, oy, ox)) {
491             try_count++;
492             continue;
493         }
494
495         if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &monraces_info[guardian], 0)) {
496             try_count++;
497             continue;
498         }
499
500         if (place_specific_monster(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) {
501             return true;
502         }
503
504         try_count--;
505     }
506
507     return false;
508 }
509
510 /*!
511  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
512  * @param dis プレイヤーから離れるべき最小距離
513  * @param mode 生成オプション
514  * @param summon_specific 特定モンスター種別を生成するための関数ポインタ
515  * @param max_dis プレイヤーから離れるべき最大距離 (デバッグ用)
516  * @return 生成に成功したらtrue
517  */
518 bool alloc_monster(PlayerType *player_ptr, int min_dis, BIT_FLAGS mode, summon_specific_pf summon_specific, int max_dis)
519 {
520     if (alloc_guardian(player_ptr, false)) {
521         return true;
522     }
523
524     auto *floor_ptr = player_ptr->current_floor_ptr;
525     auto y = 0;
526     auto x = 0;
527     auto attempts_left = 10000;
528     while (attempts_left--) {
529         y = randint0(floor_ptr->height);
530         x = randint0(floor_ptr->width);
531
532         if (floor_ptr->dun_level) {
533             if (!is_cave_empty_bold2(player_ptr, y, x)) {
534                 continue;
535             }
536         } else {
537             if (!is_cave_empty_bold(player_ptr, y, x)) {
538                 continue;
539             }
540         }
541
542         const auto dist = distance(y, x, player_ptr->y, player_ptr->x);
543         if ((min_dis < dist) && (dist <= max_dis)) {
544             break;
545         }
546     }
547
548     if (!attempts_left) {
549         if (cheat_xtra || cheat_hear) {
550             msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
551         }
552
553         return false;
554     }
555
556     if (randint1(5000) <= floor_ptr->dun_level) {
557         if (alloc_horde(player_ptr, y, x, summon_specific)) {
558             return true;
559         }
560     } else {
561         if (place_random_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) {
562             return true;
563         }
564     }
565
566     return false;
567 }