OSDN Git Service

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