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"
42 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
45 * @var place_monster_idx
46 * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
47 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
49 static MonsterRaceId place_monster_idx = MonsterRace::empty_id();
52 * @var place_monster_m_idx
53 * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
54 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
56 static MONSTER_IDX place_monster_m_idx = 0;
59 * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
60 * @param player_ptr プレイヤーへの参照ポインタ
61 * @param r_idx 生成モンスター種族
66 * @param max_dist 生成位置の最大半径
70 bool mon_scatter(PlayerType *player_ptr, MonsterRaceId r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
72 int place_x[MON_SCAT_MAXD]{};
73 int place_y[MON_SCAT_MAXD]{};
74 int num[MON_SCAT_MAXD]{};
76 if (max_dist >= MON_SCAT_MAXD) {
81 for (i = 0; i < MON_SCAT_MAXD; i++) {
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)) {
91 if (!projectable(player_ptr, y, x, ny, nx)) {
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)) {
100 if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
103 if (pattern_tile(floor_ptr, ny, nx)) {
108 i = distance(y, x, ny, nx);
114 if (one_in_(num[i])) {
122 while (i < MON_SCAT_MAXD && 0 == num[i]) {
125 if (i >= MON_SCAT_MAXD) {
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を返す
143 * Note that "reproduction" REQUIRES empty space.
145 bool multiply_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
147 auto *floor_ptr = player_ptr->current_floor_ptr;
148 auto *m_ptr = &floor_ptr->m_list[m_idx];
150 if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1)) {
154 if (m_ptr->mflag2.has(MonsterConstantFlagType::NOPET)) {
158 if (!place_specific_monster(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY))) {
162 if (clone || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
163 floor_ptr->m_list[hack_m_idx_ii].mflag2.set({ MonsterConstantFlagType::CLONED, MonsterConstantFlagType::NOPET });
170 * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
171 * @param who 召喚主のモンスター情報ID
174 * @param r_idx 生成モンスター種族
175 * @param mode 生成オプション
178 static bool place_monster_group(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
180 auto *r_ptr = &monraces_info[r_idx];
181 auto total = randint1(10);
183 auto *floor_ptr = player_ptr->current_floor_ptr;
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);
203 constexpr auto max_monsters_count = 32;
204 if (total > max_monsters_count) {
205 total = max_monsters_count;
209 POSITION hack_x[max_monsters_count]{};
211 POSITION hack_y[max_monsters_count]{};
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++) {
219 scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
220 if (!is_cave_empty_bold2(player_ptr, my, mx)) {
224 if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
236 * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
237 * @param r_idx チェックするモンスター種族のID
238 * @return 護衛にできるならばtrue
240 static bool place_monster_can_escort(PlayerType *player_ptr, MonsterRaceId r_idx)
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];
246 if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx)) {
250 if (z_ptr->d_char != r_ptr->d_char) {
254 if (z_ptr->level > r_ptr->level) {
258 if (z_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
262 if (place_monster_idx == r_idx) {
266 if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr)) {
270 if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
271 if (monster_has_hostile_align(player_ptr, nullptr, 1, -1, z_ptr)) {
276 if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON)) {
284 * @brief 特定モンスターを生成する
285 * @param player_ptr プレイヤーへの参照ポインタ
286 * @param who 召喚主のモンスター情報ID
289 * @param r_idx 生成するモンスターの種族ID
290 * @param mode 生成オプション
291 * @return 生成に成功したらtrue
292 * @details 護衛も一緒に生成する
294 bool place_specific_monster(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
296 auto *r_ptr = &monraces_info[r_idx];
298 if (!(mode & PM_NO_KAGE) && one_in_(333)) {
302 if (!place_monster_one(player_ptr, who, y, x, r_idx, mode)) {
305 if (!(mode & PM_ALLOW_GROUP)) {
309 place_monster_m_idx = hack_m_idx_ii;
312 for (auto [reinforce_r_idx, dd, ds] : r_ptr->reinforces) {
313 if (!MonsterRace(reinforce_r_idx).is_valid()) {
316 auto n = damroll(dd, ds);
317 for (int j = 0; j < n; j++) {
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)) {
327 if (d > scatter_max) {
328 msg_format_wizard(player_ptr, CHEAT_MONSTER, _("護衛の指定生成に失敗しました。", "Failed fixed escorts."));
333 if (r_ptr->flags1 & (RF1_FRIENDS)) {
334 (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
337 if (!(r_ptr->flags1 & (RF1_ESCORT))) {
341 place_monster_idx = r_idx;
342 for (int i = 0; i < 32; i++) {
343 POSITION nx, ny, d = 3;
345 scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
346 if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
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()) {
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);
366 * @brief フロア相当のモンスターを1体生成する
367 * @param player_ptr プレイヤーへの参照ポインタ
370 * @param mode 生成オプション
371 * @return 生成に成功したらtrue
373 bool place_random_monster(PlayerType *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
375 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
378 r_idx = get_mon_num(player_ptr, 0, player_ptr->current_floor_ptr->monster_level, PM_NONE);
379 } while ((mode & PM_NO_QUEST) && (monraces_info[r_idx].flags8 & RF8_NO_QUEST));
381 if (!MonsterRace(r_idx).is_valid()) {
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)) {
389 return place_specific_monster(player_ptr, 0, y, x, r_idx, mode);
392 static std::optional<MonsterRaceId> select_horde_leader_r_idx(PlayerType *player_ptr)
394 const auto *floor_ptr = player_ptr->current_floor_ptr;
396 for (auto attempts = 1000; attempts > 0; --attempts) {
397 auto r_idx = get_mon_num(player_ptr, 0, floor_ptr->monster_level, PM_NONE);
398 if (!MonsterRace(r_idx).is_valid()) {
402 if (monraces_info[r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
406 if (r_idx == MonsterRaceId::HAGURE) {
417 * @brief 指定地点に1種類のモンスター種族による群れを生成する
418 * @param player_ptr プレイヤーへの参照ポインタ
421 * @return 生成に成功したらtrue
423 bool alloc_horde(PlayerType *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
425 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
427 auto r_idx = select_horde_leader_r_idx(player_ptr);
428 if (!r_idx.has_value()) {
432 for (auto attempts = 1000;; --attempts) {
437 if (place_specific_monster(player_ptr, 0, y, x, r_idx.value(), 0L)) {
442 auto *floor_ptr = player_ptr->current_floor_ptr;
443 MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
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);
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];
463 msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
469 * @brief ダンジョンの主生成を試みる / Put the Guardian
470 * @param player_ptr プレイヤーへの参照ポインタ
471 * @param def_val 現在の主の生成状態
472 * @return 生成に成功したらtrue
474 bool alloc_guardian(PlayerType *player_ptr, bool def_val)
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) {
486 int try_count = 4000;
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)) {
495 if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &monraces_info[guardian], 0)) {
500 if (place_specific_monster(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) {
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
518 bool alloc_monster(PlayerType *player_ptr, int min_dis, BIT_FLAGS mode, summon_specific_pf summon_specific, int max_dis)
520 if (alloc_guardian(player_ptr, false)) {
524 auto *floor_ptr = player_ptr->current_floor_ptr;
527 auto attempts_left = 10000;
528 while (attempts_left--) {
529 y = randint0(floor_ptr->height);
530 x = randint0(floor_ptr->width);
532 if (floor_ptr->dun_level) {
533 if (!is_cave_empty_bold2(player_ptr, y, x)) {
537 if (!is_cave_empty_bold(player_ptr, y, x)) {
542 const auto dist = distance(y, x, player_ptr->y, player_ptr->x);
543 if ((min_dis < dist) && (dist <= max_dis)) {
548 if (!attempts_left) {
549 if (cheat_xtra || cheat_hear) {
550 msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
556 if (randint1(5000) <= floor_ptr->dun_level) {
557 if (alloc_horde(player_ptr, y, x, summon_specific)) {
561 if (place_random_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) {