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"
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 POSITION place_x[MON_SCAT_MAXD];
73 POSITION 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 = &r_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_monster_aux(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 = &r_info[r_idx];
181 int 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);
202 if (total > GROUP_MAX) {
207 POSITION hack_x[GROUP_MAX];
209 POSITION hack_y[GROUP_MAX];
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++) {
217 scatter(player_ptr, &my, &mx, hy, hx, 4, PROJECT_NONE);
218 if (!is_cave_empty_bold2(player_ptr, my, mx)) {
222 if (place_monster_one(player_ptr, who, my, mx, r_idx, mode)) {
234 * @brief モンスター種族が召喚主の護衛となれるかどうかをチェックする / Hack -- help pick an escort type
235 * @param r_idx チェックするモンスター種族のID
236 * @return 護衛にできるならばtrue
238 static bool place_monster_can_escort(PlayerType *player_ptr, MonsterRaceId r_idx)
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];
244 if (mon_hook_dungeon(player_ptr, place_monster_idx) != mon_hook_dungeon(player_ptr, r_idx)) {
248 if (z_ptr->d_char != r_ptr->d_char) {
252 if (z_ptr->level > r_ptr->level) {
256 if (z_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
260 if (place_monster_idx == r_idx) {
264 if (monster_has_hostile_align(player_ptr, m_ptr, 0, 0, z_ptr)) {
268 if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
269 if (monster_has_hostile_align(player_ptr, nullptr, 1, -1, z_ptr)) {
274 if ((r_ptr->flags7 & RF7_CHAMELEON) && !(z_ptr->flags7 & RF7_CHAMELEON)) {
282 * @brief 一般的なモンスター生成処理のサブルーチン / Attempt to place a monster of the given race at the given location
283 * @param player_ptr プレイヤーへの参照ポインタ
284 * @param who 召喚主のモンスター情報ID
287 * @param r_idx 生成するモンスターの種族ID
288 * @param mode 生成オプション
289 * @return 生成に成功したらtrue
291 bool place_monster_aux(PlayerType *player_ptr, MONSTER_IDX who, POSITION y, POSITION x, MonsterRaceId r_idx, BIT_FLAGS mode)
293 auto *r_ptr = &r_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->flags1 & (RF1_FRIENDS)) {
331 (void)place_monster_group(player_ptr, who, y, x, r_idx, mode);
334 if (!(r_ptr->flags1 & (RF1_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 ((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);
363 * @brief 一般的なモンスター生成処理のメインルーチン / Attempt to place a monster of the given race at the given location
364 * @param player_ptr プレイヤーへの参照ポインタ
367 * @param mode 生成オプション
368 * @return 生成に成功したらtrue
370 bool place_monster(PlayerType *player_ptr, POSITION y, POSITION x, BIT_FLAGS mode)
372 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
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));
378 if (!MonsterRace(r_idx).is_valid()) {
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)) {
386 return place_monster_aux(player_ptr, 0, y, x, r_idx, mode);
389 static std::optional<MonsterRaceId> select_horde_leader_r_idx(PlayerType *player_ptr)
391 const auto *floor_ptr = player_ptr->current_floor_ptr;
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()) {
399 if (r_info[r_idx].kind_flags.has(MonsterKindType::UNIQUE)) {
403 if (r_idx == MonsterRaceId::HAGURE) {
414 * @brief 指定地点に1種類のモンスター種族による群れを生成する
415 * @param player_ptr プレイヤーへの参照ポインタ
418 * @return 生成に成功したらtrue
420 bool alloc_horde(PlayerType *player_ptr, POSITION y, POSITION x, summon_specific_pf summon_specific)
422 get_mon_num_prep(player_ptr, get_monster_hook(player_ptr), get_monster_hook2(player_ptr, y, x));
424 auto r_idx = select_horde_leader_r_idx(player_ptr);
425 if (!r_idx.has_value()) {
429 for (auto attempts = 1000;; --attempts) {
434 if (place_monster_aux(player_ptr, 0, y, x, r_idx.value(), 0L)) {
439 auto *floor_ptr = player_ptr->current_floor_ptr;
440 MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
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);
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];
460 msg_format(_("モンスターの大群(%c)", "Monster horde (%c)."), r_ptr->d_char);
466 * @brief ダンジョンの主生成を試みる / Put the Guardian
467 * @param player_ptr プレイヤーへの参照ポインタ
468 * @param def_val 現在の主の生成状態
469 * @return 生成に成功したらtrue
471 bool alloc_guardian(PlayerType *player_ptr, bool def_val)
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) {
482 int try_count = 4000;
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)) {
491 if (!monster_can_cross_terrain(player_ptr, floor_ptr->grid_array[oy][ox].feat, &r_info[guardian], 0)) {
496 if (place_monster_aux(player_ptr, 0, oy, ox, guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) {
507 * @brief ダンジョンの初期配置モンスターを生成1回生成する / Attempt to allocate a random monster in the dungeon.
508 * @param dis プレイヤーから離れるべき最低距離
509 * @param mode 生成オプション
510 * @return 生成に成功したらtrue
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
516 bool alloc_monster(PlayerType *player_ptr, POSITION dis, BIT_FLAGS mode, summon_specific_pf summon_specific)
518 if (alloc_guardian(player_ptr, false)) {
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);
529 if (floor_ptr->dun_level) {
530 if (!is_cave_empty_bold2(player_ptr, y, x)) {
534 if (!is_cave_empty_bold(player_ptr, y, x)) {
539 if (distance(y, x, player_ptr->y, player_ptr->x) > dis) {
544 if (!attempts_left) {
545 if (cheat_xtra || cheat_hear) {
546 msg_print(_("警告!新たなモンスターを配置できません。小さい階ですか?", "Warning! Could not allocate a new monster. Small level?"));
552 if (randint1(5000) <= floor_ptr->dun_level) {
553 if (alloc_horde(player_ptr, y, x, summon_specific)) {
557 if (place_monster(player_ptr, y, x, (mode | PM_ALLOW_GROUP))) {