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"
39 #define MON_SCAT_MAXD 10 /*!< mon_scatter()関数によるモンスター配置で許される中心からの最大距離 */
42 * @var place_monster_idx
43 * @brief 護衛対象となるモンスター種族IDを渡すグローバル変数 / Hack -- help pick an escort type
44 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_idxを除去し、関数引数化する
46 static MonsterRaceId place_monster_idx = MonsterRace::empty_id();
49 * @var place_monster_m_idx
50 * @brief 護衛対象となるモンスターIDを渡すグローバル変数 / Hack -- help pick an escort type
51 * @todo 関数ポインタの都合を配慮しながら、グローバル変数place_monster_m_idxを除去し、関数引数化する
53 static MONSTER_IDX place_monster_m_idx = 0;
56 * @brief モンスター1体を目標地点に可能な限り近い位置に生成する / improved version of scatter() for place monster
57 * @param player_ptr プレイヤーへの参照ポインタ
58 * @param r_idx 生成モンスター種族
63 * @param max_dist 生成位置の最大半径
67 bool mon_scatter(PlayerType *player_ptr, MonsterRaceId r_idx, POSITION *yp, POSITION *xp, POSITION y, POSITION x, POSITION max_dist)
69 int place_x[MON_SCAT_MAXD]{};
70 int place_y[MON_SCAT_MAXD]{};
71 int num[MON_SCAT_MAXD]{};
73 if (max_dist >= MON_SCAT_MAXD) {
78 for (i = 0; i < MON_SCAT_MAXD; i++) {
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)) {
88 if (!projectable(player_ptr, y, x, ny, nx)) {
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)) {
97 if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
100 if (pattern_tile(floor_ptr, ny, nx)) {
105 i = distance(y, x, ny, nx);
111 if (one_in_(num[i])) {
119 while (i < MON_SCAT_MAXD && 0 == num[i]) {
122 if (i >= MON_SCAT_MAXD) {
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を返す
140 * Note that "reproduction" REQUIRES empty space.
142 bool multiply_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, bool clone, BIT_FLAGS mode)
144 auto *floor_ptr = player_ptr->current_floor_ptr;
145 auto *m_ptr = &floor_ptr->m_list[m_idx];
147 if (!mon_scatter(player_ptr, m_ptr->r_idx, &y, &x, m_ptr->fy, m_ptr->fx, 1)) {
151 if (m_ptr->mflag2.has(MonsterConstantFlagType::NOPET)) {
155 if (!place_specific_monster(player_ptr, m_idx, y, x, m_ptr->r_idx, (mode | PM_NO_KAGE | PM_MULTIPLY))) {
159 if (clone || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
160 floor_ptr->m_list[hack_m_idx_ii].mflag2.set({ MonsterConstantFlagType::CLONED, MonsterConstantFlagType::NOPET });
167 * @brief モンスターを目標地点に集団生成する / Attempt to place a "group" of monsters around the given location
168 * @param who 召喚主のモンスター情報ID
171 * @param r_idx 生成モンスター種族
172 * @param mode 生成オプション
175 static bool place_monster_group(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
177 auto *r_ptr = &monraces_info[r_idx];
178 auto total = randint1(10);
180 auto *floor_ptr = player_ptr->current_floor_ptr;
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);
200 constexpr auto max_monsters_count = 32;
201 if (total > max_monsters_count) {
202 total = max_monsters_count;
206 POSITION hack_x[max_monsters_count]{};
208 POSITION hack_y[max_monsters_count]{};
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++) {
216 scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
217 if (!is_cave_empty_bold2(player_ptr, my, mx)) {
221 if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
233 * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
234 * @param r_idx チェックするモンスター種族のID
235 * @return 護衛にできるならばtrue
237 static bool place_monster_can_escort(PlayerType *player_ptr, MonsterRaceId r_idx)
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];
243 if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx)) {
247 if (z_ptr->d_char != r_ptr->d_char) {
251 if (z_ptr->level > r_ptr->level) {
255 if (z_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
259 if (place_monster_idx == r_idx) {
263 if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr)) {
267 if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
268 if (monster_has_hostile_align(player_ptr, nullptr, 1, -1, z_ptr)) {
273 if (r_ptr->misc_flags.has(MonsterMiscType::CHAMELEON) && z_ptr->misc_flags.has_not(MonsterMiscType::CHAMELEON)) {
281 * @brief 特定モンスターを生成する
282 * @param player_ptr プレイヤーへの参照ポインタ
283 * @param who 召喚主のモンスター情報ID
286 * @param r_idx 生成するモンスターの種族ID
287 * @param mode 生成オプション
288 * @return 生成に成功したらtrue
289 * @details 護衛も一緒に生成する
291 bool place_specific_monster(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
293 auto *r_ptr = &monraces_info[r_idx];
295 if (!(mode & PM_NO_KAGE) && one_in_(333)) {
299 if (!place_monster_one(player_ptr, who, y, x, r_idx, mode)) {
302 if (!(mode & PM_ALLOW_GROUP)) {
306 place_monster_m_idx = hack_m_idx_ii;
309 for (auto [reinforce_r_idx, dd, ds] : r_ptr->reinforces) {
310 if (!MonsterRace(reinforce_r_idx).is_valid()) {
313 auto n = damroll(dd, ds);
314 for (int j = 0; j < n; j++) {
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)) {
324 if (d > scatter_max) {
325 msg_format_wizard(player_ptr, CHEAT_MONSTER, _("護衛の指定生成に失敗しました。", "Failed fixed escorts."));
330 if (r_ptr->misc_flags.has(MonsterMiscType::HAS_FRIENDS)) {
331 (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
334 if (r_ptr->misc_flags.has_not(MonsterMiscType::ESCORT)) {
338 place_monster_idx = r_idx;
339 for (int i = 0; i < 32; i++) {
340 POSITION nx, ny, d = 3;
342 scatter(player_ptr, &ny, &nx, y, x, d, PROJECT_NONE);
343 if (!is_cave_empty_bold2(player_ptr, ny, nx)) {
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()) {
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);
362 * @brief フロア相当のモンスターを1体生成する
363 * @param player_ptr プレイヤーへの参照ポインタ
366 * @param mode 生成オプション
367 * @return 生成に成功したらtrue
369 bool place_random_monster(PlayerType *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
371 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
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()) {
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)) {
384 return place_specific_monster(player_ptr, 0, y, x, r_idx, mode);
387 static std::optional<MonsterRaceId> select_horde_leader_r_idx(PlayerType *player_ptr)
389 const auto *floor_ptr = player_ptr->current_floor_ptr;
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()) {
397 if (monraces_info[r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
401 if (r_idx == MonsterRaceId::HAGURE) {
412 * @brief 指定地点に1種類のモンスター種族による群れを生成する
413 * @param player_ptr プレイヤーへの参照ポインタ
416 * @return 生成に成功したらtrue
418 bool alloc_horde(PlayerType *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
420 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
422 const auto r_idx = select_horde_leader_r_idx(player_ptr);
427 for (auto attempts = 1000;; --attempts) {
432 if (place_specific_monster(player_ptr, 0, y, x, *r_idx, 0L)) {
437 auto *floor_ptr = player_ptr->current_floor_ptr;
438 MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
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);
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];
458 msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
464 * @brief ダンジョンの主生成を試みる / Put the Guardian
465 * @param player_ptr プレイヤーへの参照ポインタ
466 * @param def_val 現在の主の生成状態
467 * @return 生成に成功したらtrue
469 bool alloc_guardian(PlayerType *player_ptr, bool def_val)
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) {
481 int try_count = 4000;
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)) {
490 if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &monraces_info[guardian], 0)) {
495 if (place_specific_monster(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) {
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
513 bool alloc_monster(PlayerType *player_ptr, int min_dis, BIT_FLAGS mode, summon_specific_pf summon_specific, int max_dis)
515 if (alloc_guardian(player_ptr, false)) {
519 auto *floor_ptr = player_ptr->current_floor_ptr;
522 auto attempts_left = 10000;
523 while (attempts_left--) {
524 y = randint0(floor_ptr->height);
525 x = randint0(floor_ptr->width);
527 if (floor_ptr->dun_level) {
528 if (!is_cave_empty_bold2(player_ptr, y, x)) {
532 if (!is_cave_empty_bold(player_ptr, y, x)) {
537 const auto dist = distance(y, x, player_ptr->y, player_ptr->x);
538 if ((min_dis < dist) && (dist <= max_dis)) {
543 if (!attempts_left) {
544 if (cheat_xtra || cheat_hear) {
545 msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
551 if (randint1(5000) <= floor_ptr->dun_level) {
552 if (alloc_horde(player_ptr, y, x, summon_specific)) {
556 if (place_random_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) {