1 #include "monster/monster-status.h"
2 #include "autopick/autopick-pref-processor.h"
3 #include "core/speed-table.h"
4 #include "floor/cave.h"
5 #include "floor/geometry.h"
6 #include "game-option/birth-options.h"
7 #include "game-option/text-display-options.h"
9 #include "monster-race/monster-kind-mask.h"
10 #include "monster-race/monster-race.h"
11 #include "monster-race/race-flags-resistance.h"
12 #include "monster-race/race-flags1.h"
13 #include "monster-race/race-flags2.h"
14 #include "monster-race/race-flags3.h"
15 #include "monster/monster-describer.h"
16 #include "monster/monster-info.h"
17 #include "monster/monster-list.h"
18 #include "monster/monster-status-setter.h" //!< @todo 相互依存. 後で何とかする.
19 #include "monster/monster-update.h"
20 #include "system/floor-type-definition.h"
21 #include "system/monster-entity.h"
22 #include "system/monster-race-info.h"
23 #include "system/player-type-definition.h"
24 #include "system/redrawing-flags-updater.h"
25 #include "timed-effect/player-blindness.h"
26 #include "timed-effect/player-hallucination.h"
27 #include "timed-effect/timed-effects.h"
28 #include "util/bit-flags-calculator.h"
29 #include "view/display-messages.h"
30 #include "world/world.h"
34 #include "monster/monster-description-types.h"
37 static uint32_t csleep_noise;
40 * @brief モンスターIDからPOWERFULフラグの有無を取得する /
41 * @param floor_ptr 現在フロアへの参照ポインタ
42 * @param m_idx モンスターID
43 * @return POWERFULフラグがあればTRUE、なければFALSEを返す。
45 bool monster_is_powerful(FloorType *floor_ptr, MONSTER_IDX m_idx)
47 auto *m_ptr = &floor_ptr->m_list[m_idx];
48 auto *r_ptr = &monraces_info[m_ptr->r_idx];
49 return any_bits(r_ptr->flags2, RF2_POWERFUL);
53 * @brief モンスターIDからモンスターのレベルを取得する(ただし最低1を保証する) /
54 * @param m_idx モンスターID
57 DEPTH monster_level_idx(FloorType *floor_ptr, MONSTER_IDX m_idx)
59 auto *m_ptr = &floor_ptr->m_list[m_idx];
60 auto *r_ptr = &monraces_info[m_ptr->r_idx];
61 return (r_ptr->level >= 1) ? r_ptr->level : 1;
65 * @brief モンスターに与えたダメージの修正処理 /
66 * Modify the physical damage done to the monster.
67 * @param player_ptr プレイヤーへの参照ポインタ
68 * @param m_ptr ダメージを受けるモンスターの構造体参照ポインタ
70 * @param is_psy_spear 攻撃手段が光の剣ならばTRUE
71 * @return 修正を行った結果のダメージ量
72 * @details RES_ALL持ちはAC軽減後のダメージを1/100に補正する. 光の剣は無敵を無効化する. 一定確率で無敵は貫通できる.
74 int mon_damage_mod(PlayerType *player_ptr, MonsterEntity *m_ptr, int dam, bool is_psy_spear)
76 auto *r_ptr = &monraces_info[m_ptr->r_idx];
77 if (r_ptr->resistance_flags.has(MonsterResistanceType::RESIST_ALL) && dam > 0) {
79 if ((dam == 0) && one_in_(3)) {
84 if (!m_ptr->is_invulnerable()) {
89 if (!player_ptr->effects()->blindness()->is_blind() && is_seen(player_ptr, m_ptr)) {
90 msg_print(_("バリアを切り裂いた!", "The barrier is penetrated!"));
96 return one_in_(PENETRATE_INVULNERABILITY) ? dam : 0;
100 * @brief モンスターの時限ステータスを取得する
101 * @param floor_ptr 現在フロアへの参照ポインタ
102 * @return m_idx モンスターの参照ID
103 * @return mproc_type モンスターの時限ステータスID
106 int get_mproc_idx(FloorType *floor_ptr, MONSTER_IDX m_idx, int mproc_type)
108 const auto &cur_mproc_list = floor_ptr->mproc_list[mproc_type];
109 for (int i = floor_ptr->mproc_max[mproc_type] - 1; i >= 0; i--) {
110 if (cur_mproc_list[i] == m_idx) {
119 * @brief モンスターの時限ステータスリストを追加する
120 * @param floor_ptr 現在フロアへの参照ポインタ
121 * @return m_idx モンスターの参照ID
122 * @return mproc_type 追加したいモンスターの時限ステータスID
124 void mproc_add(FloorType *floor_ptr, MONSTER_IDX m_idx, int mproc_type)
126 if (floor_ptr->mproc_max[mproc_type] < w_ptr->max_m_idx) {
127 floor_ptr->mproc_list[mproc_type][floor_ptr->mproc_max[mproc_type]++] = (int16_t)m_idx;
132 * @brief モンスターの時限ステータスリストを初期化する / Initialize monster process
133 * @param floor_ptr 現在フロアへの参照ポインタ
135 void mproc_init(FloorType *floor_ptr)
137 /* Reset "player_ptr->current_floor_ptr->mproc_max[]" */
138 for (int i = 0; i < MAX_MTIMED; i++) {
139 floor_ptr->mproc_max[i] = 0;
142 /* Process the monsters (backwards) */
143 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--) {
144 auto *m_ptr = &floor_ptr->m_list[i];
146 /* Ignore "dead" monsters */
147 if (!m_ptr->is_valid()) {
151 for (int cmi = 0; cmi < MAX_MTIMED; cmi++) {
152 if (m_ptr->mtimed[cmi]) {
153 mproc_add(floor_ptr, i, cmi);
160 * @brief モンスターの各種状態値を時間経過により更新するサブルーチン
161 * @param floor_ptr 現在フロアへの参照ポインタ
162 * @param m_idx モンスター参照ID
163 * @param mtimed_idx 更新するモンスターの時限ステータスID
165 static void process_monsters_mtimed_aux(PlayerType *player_ptr, MONSTER_IDX m_idx, int mtimed_idx)
167 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
168 switch (mtimed_idx) {
169 case MTIMED_CSLEEP: {
170 auto *r_ptr = &monraces_info[m_ptr->r_idx];
171 auto is_wakeup = false;
172 if (m_ptr->cdis < MAX_MONSTER_SENSING) {
173 /* Handle "sensing radius" */
174 if (m_ptr->cdis <= (m_ptr->is_pet() ? ((r_ptr->aaf > MAX_PLAYER_SIGHT) ? MAX_PLAYER_SIGHT : r_ptr->aaf) : r_ptr->aaf)) {
178 /* Handle "sight" and "aggravation" */
179 else if ((m_ptr->cdis <= MAX_PLAYER_SIGHT) && (player_has_los_bold(player_ptr, m_ptr->fy, m_ptr->fx))) {
188 auto notice = (uint32_t)randint0(1024);
190 /* Nightmare monsters are more alert */
191 if (ironman_nightmare) {
195 /* Hack -- See if monster "notices" player */
196 if ((notice * notice * notice) > csleep_noise) {
200 /* Hack -- amount of "waking" */
201 /* Wake up faster near the player */
202 auto d = (m_ptr->cdis < MAX_MONSTER_SENSING / 2) ? (MAX_MONSTER_SENSING / m_ptr->cdis) : 1;
204 /* Hack -- amount of "waking" is affected by speed of player */
205 d = (d * speed_to_energy(player_ptr->pspeed)) / 10;
210 /* Monster wakes up "a little bit" */
213 if (!set_monster_csleep(player_ptr, m_idx, m_ptr->get_remaining_sleep() - d)) {
214 /* Notice the "not waking up" */
215 if (is_original_ap_and_seen(player_ptr, m_ptr)) {
216 /* Hack -- Count the ignores */
217 if (r_ptr->r_ignore < MAX_UCHAR) {
225 /* Notice the "waking up" */
227 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
228 msg_format(_("%s^が目を覚ました。", "%s^ wakes up."), m_name.data());
231 if (is_original_ap_and_seen(player_ptr, m_ptr)) {
232 /* Hack -- Count the wakings */
233 if (r_ptr->r_wake < MAX_UCHAR) {
242 /* Reduce by one, note if expires */
243 if (set_monster_fast(player_ptr, m_idx, m_ptr->get_remaining_acceleration() - 1)) {
244 if (is_seen(player_ptr, m_ptr)) {
245 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
246 msg_format(_("%s^はもう加速されていない。", "%s^ is no longer fast."), m_name.data());
253 /* Reduce by one, note if expires */
254 if (set_monster_slow(player_ptr, m_idx, m_ptr->get_remaining_deceleration() - 1)) {
255 if (is_seen(player_ptr, m_ptr)) {
256 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
257 msg_format(_("%s^はもう減速されていない。", "%s^ is no longer slow."), m_name.data());
263 case MTIMED_STUNNED: {
264 int rlev = monraces_info[m_ptr->r_idx].level;
266 /* Recover from stun */
267 if (set_monster_stunned(player_ptr, m_idx, (randint0(10000) <= rlev * rlev) ? 0 : (m_ptr->get_remaining_stun() - 1))) {
268 /* Message if visible */
269 if (is_seen(player_ptr, m_ptr)) {
270 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
271 msg_format(_("%s^は朦朧状態から立ち直った。", "%s^ is no longer stunned."), m_name.data());
278 case MTIMED_CONFUSED: {
279 /* Reduce the confusion */
280 if (!set_monster_confused(player_ptr, m_idx, m_ptr->get_remaining_confusion() - randint1(monraces_info[m_ptr->r_idx].level / 20 + 1))) {
284 /* Message if visible */
285 if (is_seen(player_ptr, m_ptr)) {
286 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
287 msg_format(_("%s^は混乱から立ち直った。", "%s^ is no longer confused."), m_name.data());
293 case MTIMED_MONFEAR: {
294 /* Reduce the fear */
295 if (!set_monster_monfear(player_ptr, m_idx, m_ptr->get_remaining_fear() - randint1(monraces_info[m_ptr->r_idx].level / 20 + 1))) {
300 if (is_seen(player_ptr, m_ptr)) {
301 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
304 /* Acquire the monster possessive */
305 const auto m_poss = monster_desc(player_ptr, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
308 msg_format("%s^は勇気を取り戻した。", m_name.data());
310 msg_format("%s^ recovers %s courage.", m_name.data(), m_poss.data());
317 case MTIMED_INVULNER: {
318 /* Reduce by one, note if expires */
319 if (!set_monster_invulner(player_ptr, m_idx, m_ptr->get_remaining_invulnerability() - 1, true)) {
323 if (is_seen(player_ptr, m_ptr)) {
324 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
325 msg_format(_("%s^はもう無敵でない。", "%s^ is no longer invulnerable."), m_name.data());
334 * @brief 全モンスターの各種状態値を時間経過により更新するメインルーチン
335 * @param mtimed_idx 更新するモンスターの時限ステータスID
336 * @param player_ptr プレイヤーへの参照ポインタ
338 * Process the counters of monsters (once per 10 game turns)\n
339 * These functions are to process monsters' counters same as player's.
341 void process_monsters_mtimed(PlayerType *player_ptr, int mtimed_idx)
343 auto *floor_ptr = player_ptr->current_floor_ptr;
344 const auto &cur_mproc_list = floor_ptr->mproc_list[mtimed_idx];
346 /* Hack -- calculate the "player noise" */
347 if (mtimed_idx == MTIMED_CSLEEP) {
348 csleep_noise = (1U << (30 - player_ptr->skill_stl));
351 /* Process the monsters (backwards) */
352 for (auto i = floor_ptr->mproc_max[mtimed_idx] - 1; i >= 0; i--) {
353 process_monsters_mtimed_aux(player_ptr, cur_mproc_list[i], mtimed_idx);
358 * @brief モンスターへの魔力消去処理
359 * @param player_ptr プレイヤーへの参照ポインタ
360 * @param m_idx 魔力消去を受けるモンスターの参照ID
362 void dispel_monster_status(PlayerType *player_ptr, MONSTER_IDX m_idx)
364 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
365 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
366 if (set_monster_invulner(player_ptr, m_idx, 0, true)) {
368 msg_format(_("%sはもう無敵ではない。", "%s^ is no longer invulnerable."), m_name.data());
372 if (set_monster_fast(player_ptr, m_idx, 0)) {
374 msg_format(_("%sはもう加速されていない。", "%s^ is no longer fast."), m_name.data());
378 if (set_monster_slow(player_ptr, m_idx, 0)) {
380 msg_format(_("%sはもう減速されていない。", "%s^ is no longer slow."), m_name.data());
386 * @brief モンスターの経験値取得処理
387 * @param player_ptr プレイヤーへの参照ポインタ
388 * @param m_idx 経験値を得るモンスターの参照ID
389 * @param s_idx 撃破されたモンスター種族の参照ID
391 void monster_gain_exp(PlayerType *player_ptr, MONSTER_IDX m_idx, MonsterRaceId s_idx)
393 if (m_idx <= 0 || !MonsterRace(s_idx).is_valid()) {
397 auto *floor_ptr = player_ptr->current_floor_ptr;
398 auto *m_ptr = &floor_ptr->m_list[m_idx];
400 if (!m_ptr->is_valid()) {
404 auto *r_ptr = &monraces_info[m_ptr->r_idx];
405 auto *s_ptr = &monraces_info[s_idx];
407 if (player_ptr->phase_out || (r_ptr->next_exp == 0)) {
411 auto new_exp = s_ptr->mexp * s_ptr->level / (r_ptr->level + 2);
412 if (m_idx == player_ptr->riding) {
413 new_exp = (new_exp + 1) / 2;
416 if (!floor_ptr->dun_level) {
420 m_ptr->exp += new_exp;
421 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
425 auto &rfu = RedrawingFlagsUpdater::get_instance();
426 if (m_ptr->exp < r_ptr->next_exp) {
427 if (m_idx == player_ptr->riding) {
428 rfu.set_flag(StatusRedrawingFlag::BONUS);
434 auto old_hp = m_ptr->hp;
435 auto old_maxhp = m_ptr->max_maxhp;
436 auto old_r_idx = m_ptr->r_idx;
437 auto old_sub_align = m_ptr->sub_align;
439 /* Hack -- Reduce the racial counter of previous monster */
440 m_ptr->get_real_r_ref().cur_num--;
442 const auto m_name = monster_desc(player_ptr, m_ptr, 0);
443 m_ptr->r_idx = r_ptr->next_r_idx;
445 /* Count the monsters on the level */
446 m_ptr->get_real_r_ref().cur_num++;
448 m_ptr->ap_r_idx = m_ptr->r_idx;
449 r_ptr = &monraces_info[m_ptr->r_idx];
451 m_ptr->max_maxhp = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? maxroll(r_ptr->hdice, r_ptr->hside) : damroll(r_ptr->hdice, r_ptr->hside);
452 if (ironman_nightmare) {
453 auto hp = m_ptr->max_maxhp * 2;
454 m_ptr->max_maxhp = std::min(MONSTER_MAXHP, hp);
457 m_ptr->maxhp = m_ptr->max_maxhp;
458 m_ptr->hp = old_hp * m_ptr->maxhp / old_maxhp;
460 /* dealt damage is 0 at initial*/
461 m_ptr->dealt_damage = 0;
463 /* Extract the monster base speed */
464 m_ptr->mspeed = get_mspeed(floor_ptr, r_ptr);
466 /* Sub-alignment of a monster */
467 if (!m_ptr->is_pet() && r_ptr->kind_flags.has_none_of(alignment_mask)) {
468 m_ptr->sub_align = old_sub_align;
470 m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
471 if (r_ptr->kind_flags.has(MonsterKindType::EVIL)) {
472 m_ptr->sub_align |= SUB_ALIGN_EVIL;
475 if (r_ptr->kind_flags.has(MonsterKindType::GOOD)) {
476 m_ptr->sub_align |= SUB_ALIGN_GOOD;
481 if (m_ptr->is_pet() || m_ptr->ml) {
482 auto is_hallucinated = player_ptr->effects()->hallucination()->is_hallucinated();
483 if (!ignore_unview || player_can_see_bold(player_ptr, m_ptr->fy, m_ptr->fx)) {
484 if (is_hallucinated) {
485 MonsterRaceInfo *hallucinated_race = nullptr;
487 auto r_idx = MonsterRace::pick_one_at_random();
488 hallucinated_race = &monraces_info[r_idx];
489 } while (hallucinated_race->name.empty() || hallucinated_race->kind_flags.has(MonsterKindType::UNIQUE));
490 auto mes_evolution = _("%sは%sに進化した。", "%s^ evolved into %s.");
491 auto mes_degeneration = _("%sは%sに退化した。", "%s^ degenerated into %s.");
492 auto mes = randint0(2) == 0 ? mes_evolution : mes_degeneration;
493 msg_format(mes, m_name.data(), hallucinated_race->name.data());
495 msg_format(_("%sは%sに進化した。", "%s^ evolved into %s."), m_name.data(), r_ptr->name.data());
499 if (!is_hallucinated) {
500 monraces_info[old_r_idx].r_can_evolve = true;
503 /* Now you feel very close to this pet. */
504 m_ptr->parent_m_idx = 0;
507 update_monster(player_ptr, m_idx, false);
508 lite_spot(player_ptr, m_ptr->fy, m_ptr->fx);
510 if (m_idx == player_ptr->riding) {
511 rfu.set_flag(StatusRedrawingFlag::BONUS);