OSDN Git Service

be3f97627513ab9277cd58b55cf8b124eab61a9b
[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 "dungeon/dungeon.h"
10 #include "effect/effect-characteristics.h"
11 #include "floor/cave.h"
12 #include "floor/floor-util.h"
13 #include "floor/geometry.h"
14 #include "game-option/cheat-options.h"
15 #include "game-option/cheat-types.h"
16 #include "monster-floor/one-monster-placer.h"
17 #include "monster-floor/place-monster-types.h"
18 #include "monster-race/monster-race-hook.h"
19 #include "monster-race/monster-race.h"
20 #include "monster-race/race-flags1.h"
21 #include "monster-race/race-flags7.h"
22 #include "monster-race/race-flags8.h"
23 #include "monster-race/race-indice-types.h"
24 #include "monster/monster-flag-types.h"
25 #include "monster/monster-info.h"
26 #include "monster/monster-list.h"
27 #include "monster/monster-util.h"
28 #include "monster/smart-learn-types.h"
29 #include "mspell/summon-checker.h"
30 #include "spell/summon-types.h"
31 #include "system/floor-type-definition.h"
32 #include "system/grid-type-definition.h"
33 #include "system/monster-race-definition.h"
34 #include "system/monster-type-definition.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     POSITION place_x[MON_SCAT_MAXD];
73     POSITION 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 = &r_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_monster_aux(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 = &r_info[r_idx];
181     int total = randint1(10);
182
183     auto *floor_ptr = player_ptr->current_floor_ptr;
184     int 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     if (total > GROUP_MAX) {
203         total = GROUP_MAX;
204     }
205
206     int hack_n = 1;
207     POSITION hack_x[GROUP_MAX];
208     hack_x[0] = x;
209     POSITION hack_y[GROUP_MAX];
210     hack_y[0] = y;
211
212     for (int n = 0; (n < hack_n) && (hack_n < total); n++) {
213         POSITION hx = hack_x[n];
214         POSITION hy = hack_y[n];
215         for (int i = 0; (i < 8) && (hack_n < total); i++) {
216             POSITION mx, my;
217             scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
218             if (!is_cave_empty_bold2(player_ptr, my, mx)) {
219                 continue;
220             }
221
222             if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
223                 hack_y[hack_n] = my;
224                 hack_x[hack_n] = mx;
225                 hack_n++;
226             }
227         }
228     }
229
230     return true;
231 }
232
233 /*!
234  * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
235  * @param r_idx チェックするモンスター種族のID
236  * @return 護衛にできるならばtrue
237  */
238 static bool place_monster_can_escort(PlayerType *player_ptr, MonsterRaceId r_idx)
239 {
240     auto *r_ptr = &r_info[place_monster_idx];
241     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[place_monster_m_idx];
242     monster_race *z_ptr = &r_info[r_idx];
243
244     if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx)) {
245         return false;
246     }
247
248     if (z_ptr->d_char != r_ptr->d_char) {
249         return false;
250     }
251
252     if (z_ptr->level > r_ptr->level) {
253         return false;
254     }
255
256     if (z_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
257         return false;
258     }
259
260     if (place_monster_idx == r_idx) {
261         return false;
262     }
263
264     if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr)) {
265         return false;
266     }
267
268     if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
269         if (monster_has_hostile_align(player_ptr, nullptr, 1, -1, z_ptr)) {
270             return false;
271         }
272     }
273
274     if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON)) {
275         return false;
276     }
277
278     return true;
279 }
280
281 /*!
282  * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
283  * @param player_ptr プレイヤーへの参照ポインタ
284  * @param who 召喚主のモンスター情報ID
285  * @param y 生成地点y座標
286  * @param x 生成地点x座標
287  * @param r_idx 生成するモンスターの種族ID
288  * @param mode 生成オプション
289  * @return 生成に成功したらtrue
290  */
291 bool place_monster_aux(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
292 {
293     auto *r_ptr = &r_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 (int i = 0; i < 6; i++) {
310         if (!MonsterRace(r_ptr->reinforce_id[i]).is_valid()) {
311             break;
312         }
313         int n = damroll(r_ptr->reinforce_dd[i], r_ptr->reinforce_ds[i]);
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, r_ptr->reinforce_id[i], 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->flags1 & (RF1_FRIENDS)) {
331         (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
332     }
333
334     if (!(r_ptr->flags1 & (RF1_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 ((r_info[z].flags1 & RF1_FRIENDS) || (r_ptr->flags1 & RF1_ESCORTS)) {
355             (void)place_monster_group(player_ptr, place_monster_m_idx, ny, nx, z, mode);
356         }
357     }
358
359     return true;
360 }
361
362 /*!
363  * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
364  * @param player_ptr プレイヤーへの参照ポインタ
365  * @param y 生成地点y座標
366  * @param x 生成地点x座標
367  * @param mode 生成オプション
368  * @return 生成に成功したらtrue
369  */
370 bool place_monster(PlayerType *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
371 {
372     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
373     MonsterRaceId r_idx;
374     do {
375         r_idx = get_mon_num(player_ptr, 0, player_ptr->current_floor_ptr->monster_level, 0);
376     } while ((mode & PM_NO_QUEST) && (r_info[r_idx].flags8 & RF8_NO_QUEST));
377
378     if (!MonsterRace(r_idx).is_valid()) {
379         return false;
380     }
381
382     if ((one_in_(5) || (player_ptr->current_floor_ptr->dun_level == 0)) && r_info[r_idx].kind_flags.has_not(MonsterKindType::UNIQUE) && angband_strchr("hkoptuyAHLOPTUVY", r_info[r_idx].d_char)) {
383         mode |= PM_JURAL;
384     }
385
386     return place_monster_aux(player_ptr, 0, y, x, r_idx, mode);
387 }
388
389 static std::optional<MonsterRaceId> select_horde_leader_r_idx(PlayerType *player_ptr)
390 {
391     const auto *floor_ptr = player_ptr->current_floor_ptr;
392
393     for (auto attempts = 1000; attempts > 0; --attempts) {
394         auto r_idx = get_mon_num(player_ptr, 0, floor_ptr->monster_level, 0);
395         if (!MonsterRace(r_idx).is_valid()) {
396             return std::nullopt;
397         }
398
399         if (r_info[r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
400             continue;
401         }
402
403         if (r_idx == MonsterRaceId::HAGURE) {
404             continue;
405         }
406
407         return r_idx;
408     }
409
410     return std::nullopt;
411 }
412
413 /*!
414  * @brief 指定地点に1種類のモンスター種族による群れを生成する
415  * @param player_ptr プレイヤーへの参照ポインタ
416  * @param y 生成地点y座標
417  * @param x 生成地点x座標
418  * @return 生成に成功したらtrue
419  */
420 bool alloc_horde(PlayerType *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
421 {
422     get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
423
424     auto r_idx = select_horde_leader_r_idx(player_ptr);
425     if (!r_idx.has_value()) {
426         return false;
427     }
428
429     for (auto attempts = 1000;; --attempts) {
430         if (attempts <= 0) {
431             return false;
432         }
433
434         if (place_monster_aux(player_ptr, 0, y, x, r_idx.value(), 0L)) {
435             break;
436         }
437     }
438
439     auto *floor_ptr = player_ptr->current_floor_ptr;
440     MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
441
442     POSITION cy = y;
443     POSITION cx = x;
444     for (auto attempts = randint1(10) + 5; attempts > 0; attempts--) {
445         scatter(player_ptr, &cy, &cx, y, x, 5, PROJECT_NONE);
446         (void)(*summon_specific)(player_ptr, m_idx, cy, cx, floor_ptr->dun_level + 5, SUMMON_KIN, PM_ALLOW_GROUP);
447         y = cy;
448         x = cx;
449     }
450
451     if (!cheat_hear) {
452         return true;
453     }
454
455     auto *r_ptr = &r_info[r_idx.value()];
456     if (floor_ptr->m_list[m_idx].mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
457         r_ptr = &r_info[floor_ptr->m_list[m_idx].r_idx];
458     }
459
460     msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
461
462     return true;
463 }
464
465 /*!
466  * @brief ダンジョンの主生成を試みる / Put the Guardian
467  * @param player_ptr プレイヤーへの参照ポインタ
468  * @param def_val 現在の主の生成状態
469  * @return 生成に成功したらtrue
470  */
471 bool alloc_guardian(PlayerType *player_ptr, bool def_val)
472 {
473     MonsterRaceId guardian = d_info[player_ptr->dungeon_idx].final_guardian;
474     auto *floor_ptr = player_ptr->current_floor_ptr;
475     bool is_guardian_applicable = MonsterRace(guardian).is_valid();
476     is_guardian_applicable &= d_info[player_ptr->dungeon_idx].maxdepth == floor_ptr->dun_level;
477     is_guardian_applicable &= r_info[guardian].cur_num < r_info[guardian].max_num;
478     if (!is_guardian_applicable) {
479         return def_val;
480     }
481
482     int try_count = 4000;
483     while (try_count) {
484         POSITION oy = randint1(floor_ptr->height - 4) + 2;
485         POSITION ox = randint1(floor_ptr->width - 4) + 2;
486         if (!is_cave_empty_bold2(player_ptr, oy, ox)) {
487             try_count++;
488             continue;
489         }
490
491         if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0)) {
492             try_count++;
493             continue;
494         }
495
496         if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) {
497             return true;
498         }
499
500         try_count--;
501     }
502
503     return false;
504 }
505
506 /*!
507  * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
508  * @param dis プレイヤーから離れるべき最低距離
509  * @param mode 生成オプション
510  * @return 生成に成功したらtrue
511  * @details
512  * Place the monster at least "dis" distance from the player.
513  * Use "slp" to choose the initial "sleep" status
514  * Use "floor_ptr->monster_level" for the monster level
515  */
516 bool alloc_monster(PlayerType *player_ptr, POSITION dis, BIT_FLAGS mode, summon_specific_pf summon_specific)
517 {
518     if (alloc_guardian(player_ptr, false)) {
519         return true;
520     }
521
522     auto *floor_ptr = player_ptr->current_floor_ptr;
523     POSITION y = 0, x = 0;
524     int attempts_left = 10000;
525     while (attempts_left--) {
526         y = randint0(floor_ptr->height);
527         x = randint0(floor_ptr->width);
528
529         if (floor_ptr->dun_level) {
530             if (!is_cave_empty_bold2(player_ptr, y, x)) {
531                 continue;
532             }
533         } else {
534             if (!is_cave_empty_bold(player_ptr, y, x)) {
535                 continue;
536             }
537         }
538
539         if (distance(y, x, player_ptr->y, player_ptr->x) > dis) {
540             break;
541         }
542     }
543
544     if (!attempts_left) {
545         if (cheat_xtra || cheat_hear) {
546             msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
547         }
548
549         return false;
550     }
551
552     if (randint1(5000) <= floor_ptr->dun_level) {
553         if (alloc_horde(player_ptr, y, x, summon_specific)) {
554             return true;
555         }
556     } else {
557         if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) {
558             return true;
559         }
560     }
561
562     return false;
563 }