2 * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
7 #include "monster/monster-damage.h"
8 #include "avatar/avatar-changer.h"
9 #include "core/player-redraw-types.h"
10 #include "core/speed-table.h"
11 #include "core/stuff-handler.h"
12 #include "game-option/birth-options.h"
13 #include "game-option/play-record-options.h"
14 #include "io/files-util.h"
15 #include "io/report.h"
16 #include "io/write-diary.h"
17 #include "main/sound-definitions-table.h"
18 #include "main/sound-of-music.h"
19 #include "mind/mind-ninja.h"
20 #include "monster-floor/monster-death.h"
21 #include "monster-floor/monster-remover.h"
22 #include "monster-floor/monster-summon.h"
23 #include "monster-floor/place-monster-types.h"
24 #include "monster-race/monster-race-hook.h"
25 #include "monster-race/monster-race.h"
26 #include "monster-race/race-flags1.h"
27 #include "monster-race/race-flags2.h"
28 #include "monster-race/race-flags3.h"
29 #include "monster-race/race-flags7.h"
30 #include "monster-race/race-flags8.h"
31 #include "monster/monster-describer.h"
32 #include "monster/monster-description-types.h"
33 #include "monster/monster-info.h"
34 #include "monster/monster-status-setter.h"
35 #include "monster/monster-status.h"
36 #include "object-enchant/object-curse.h"
37 #include "player/player-status.h"
38 #include "player/special-defense-types.h"
39 #include "spell-kind/spells-random.h"
40 #include "status/experience.h"
41 #include "system/floor-type-definition.h"
42 #include "system/monster-entity.h"
43 #include "system/monster-race-info.h"
44 #include "system/player-type-definition.h"
45 #include "timed-effect/player-hallucination.h"
46 #include "timed-effect/timed-effects.h"
47 #include "util/bit-flags-calculator.h"
48 #include "view/display-messages.h"
49 #include "world/world.h"
54 * @param player_ptr プレイヤーへの参照ポインタ
55 * @param m_idx ダメージを与えたモンスターのID
57 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
58 * @param attribute 与えたダメージの種類 (単一属性)
59 * @param note モンスターが倒された際の特別なメッセージ述語
61 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeType attribute)
62 : player_ptr(player_ptr)
67 this->attribute_flags.clear();
68 this->attribute_flags.set((AttributeType)attribute);
73 * @param player_ptr プレイヤーへの参照ポインタ
74 * @param m_idx ダメージを与えたモンスターのID
76 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
77 * @param attribute_flags 与えたダメージの種類 (複数属性)
78 * @param note モンスターが倒された際の特別なメッセージ述語
80 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeFlags attribute_flags)
81 : player_ptr(player_ptr)
85 , attribute_flags(attribute_flags)
90 * @brief モンスターのHPをダメージに応じて減算する /
91 * @return モンスターが生きていればfalse、死んだらtrue
93 bool MonsterDamageProcessor::mon_take_hit(concptr note)
95 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
96 auto exp_mon = *m_ptr;
98 auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
100 this->get_exp_from_mon(&exp_mon, exp_dam);
101 if (this->genocide_chaos_patron()) {
105 m_ptr->hp -= this->dam;
106 m_ptr->dealt_damage += this->dam;
107 if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
108 m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
112 msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
115 if (this->process_dead_exp_virtue(note, &exp_mon)) {
119 this->add_monster_fear();
123 bool MonsterDamageProcessor::genocide_chaos_patron()
125 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
126 if (!m_ptr->is_valid()) {
131 (void)set_monster_csleep(this->player_ptr, this->m_idx, 0);
132 set_superstealth(this->player_ptr, false);
134 return this->m_idx == 0;
137 bool MonsterDamageProcessor::process_dead_exp_virtue(concptr note, MonsterEntity *exp_mon)
139 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
140 auto &r_ref = m_ptr->get_real_r_ref();
141 if (m_ptr->hp >= 0) {
145 this->death_special_flag_monster();
146 if (r_ref.r_akills < MAX_SHORT) {
150 this->increase_kill_numbers();
151 GAME_TEXT m_name[MAX_NLEN];
152 monster_desc(this->player_ptr, m_name, m_ptr, MD_TRUE_NAME);
153 this->death_amberites(m_name);
154 this->dying_scream(m_name);
155 AvatarChanger ac(player_ptr, m_ptr);
157 if (r_ref.kind_flags.has(MonsterKindType::UNIQUE) && record_destroy_uniq) {
159 sprintf(note_buf, "%s%s", r_ref.name.data(), m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) ? _("(クローン)", "(Clone)") : "");
160 exe_write_diary(this->player_ptr, DIARY_UNIQUE, 0, note_buf);
164 this->show_kill_message(note, m_name);
165 this->show_bounty_message(m_name);
166 monster_death(this->player_ptr, this->m_idx, true, this->attribute_flags);
167 this->summon_special_unique();
168 this->get_exp_from_mon(exp_mon, exp_mon->max_maxhp * 2);
174 * @brief たぬき、カメレオン、ナズグル、ユニークの死亡時処理
175 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
177 void MonsterDamageProcessor::death_special_flag_monster()
179 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
180 auto r_idx = m_ptr->r_idx;
181 auto *r_ptr = &monraces_info[r_idx];
182 if (any_bits(monraces_info[r_idx].flags7, RF7_TANUKI)) {
183 r_ptr = &monraces_info[r_idx];
184 m_ptr->ap_r_idx = r_idx;
185 if (r_ptr->r_sights < MAX_SHORT) {
190 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
191 auto &real_r_ref = m_ptr->get_real_r_ref();
192 r_idx = m_ptr->get_real_r_idx();
193 if (real_r_ref.r_sights < MAX_SHORT) {
194 real_r_ref.r_sights++;
198 if (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
202 if (r_ptr->population_flags.has(MonsterPopulationType::NAZGUL)) {
207 if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
211 this->death_unique_monster(r_idx);
216 * @param r_idx 死亡したユニークの種族番号
218 void MonsterDamageProcessor::death_unique_monster(MonsterRaceId r_idx)
220 monraces_info[r_idx].max_num = 0;
221 std::vector<MonsterRaceId> combined_unique_vec;
222 if (!check_combined_unique(r_idx, &combined_unique_vec)) {
226 combined_uniques uniques;
227 const int one_unit = 3;
228 for (auto i = 0U; i < combined_unique_vec.size(); i += one_unit) {
229 auto unique = std::make_tuple(combined_unique_vec[i], combined_unique_vec[i + 1], combined_unique_vec[i + 2]);
230 uniques.push_back(unique);
233 this->death_combined_uniques(r_idx, uniques);
237 * @brief 死亡したモンスターが分裂/合体を行う特殊ユニークか否かの判定処理
238 * @param r_idx 死亡したモンスターの種族番号
239 * @param united_uniques 分裂/合体を行う特殊ユニーク
240 * @details 合体後、合体前1、合体前2 の順にpush_backすること
242 bool MonsterDamageProcessor::check_combined_unique(const MonsterRaceId r_idx, std::vector<MonsterRaceId> *combined_unique_vec)
244 combined_unique_vec->push_back(MonsterRaceId::BANORLUPART);
245 combined_unique_vec->push_back(MonsterRaceId::BANOR);
246 combined_unique_vec->push_back(MonsterRaceId::LUPART);
248 for (const auto &unique : *combined_unique_vec) {
249 if (r_idx == unique) {
258 * @brief 分裂/合体を行う特殊ユニークの死亡処理
259 * @details 分裂/合体が A = B + C という図式の時、Aが死亡した場合BとCも死亡処理を行う。
260 * BもしくはCが死亡した場合、Aの死亡処理を行う。
261 * @param r_idx 実際に死亡したモンスターの種族ID
262 * @param combined_uniques 分裂/合体を行う特殊ユニークのリスト
264 void MonsterDamageProcessor::death_combined_uniques(const MonsterRaceId r_idx, const combined_uniques &combined_uniques)
266 auto death_r_idx = [](MonsterRaceId r_idx) {
267 auto &r_ref = monraces_info[r_idx];
271 if (r_ref.r_tkills < MAX_SHORT) {
276 for (auto [united, split1, split2] : combined_uniques) {
277 if ((r_idx == split1) || (r_idx == split2)) {
282 if (r_idx != united) {
291 void MonsterDamageProcessor::increase_kill_numbers()
293 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
294 auto &r_ref = m_ptr->get_real_r_ref();
295 auto is_hallucinated = this->player_ptr->effects()->hallucination()->is_hallucinated();
296 if (((m_ptr->ml == 0) || is_hallucinated) && r_ref.kind_flags.has_not(MonsterKindType::UNIQUE)) {
300 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (monraces_info[MonsterRaceId::KAGE].r_pkills < MAX_SHORT)) {
301 monraces_info[MonsterRaceId::KAGE].r_pkills++;
302 } else if (r_ref.r_pkills < MAX_SHORT) {
306 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (monraces_info[MonsterRaceId::KAGE].r_tkills < MAX_SHORT)) {
307 monraces_info[MonsterRaceId::KAGE].r_tkills++;
308 } else if (r_ref.r_tkills < MAX_SHORT) {
312 monster_race_track(this->player_ptr, m_ptr->ap_r_idx);
315 void MonsterDamageProcessor::death_amberites(GAME_TEXT *m_name)
317 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
318 const auto &r_ref = m_ptr->get_real_r_ref();
319 if (r_ref.kind_flags.has_not(MonsterKindType::AMBERITE) || one_in_(2)) {
323 auto curses = 1 + randint1(3);
324 auto stop_ty = false;
326 msg_format(_("%^sは恐ろしい血の呪いをあなたにかけた!", "%^s puts a terrible blood curse on you!"), m_name);
327 curse_equipment(this->player_ptr, 100, 50);
329 stop_ty = activate_ty_curse(this->player_ptr, stop_ty, &count);
333 void MonsterDamageProcessor::dying_scream(GAME_TEXT *m_name)
335 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
336 const auto &r_ref = m_ptr->get_real_r_ref();
337 if (r_ref.speak_flags.has_none_of({ MonsterSpeakType::SPEAK_ALL, MonsterSpeakType::SPEAK_DEATH })) {
342 if (!get_rnd_line(_("mondeath_j.txt", "mondeath.txt"), enum2i(m_ptr->r_idx), line_got)) {
343 msg_format("%^s %s", m_name, line_got);
347 if (m_ptr->r_idx == MonsterRaceId::SERPENT) {
348 screen_dump = make_screen_dump(this->player_ptr);
353 void MonsterDamageProcessor::show_kill_message(concptr note, GAME_TEXT *m_name)
355 auto *floor_ptr = this->player_ptr->current_floor_ptr;
356 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
357 const auto &r_ref = m_ptr->get_real_r_ref();
358 if (note != nullptr) {
359 msg_format("%^s%s", m_name, note);
364 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have killed %s.")
365 : _("%sを殺した。", "You have killed %s.");
366 msg_format(mes, m_name);
370 if (monster_living(m_ptr->r_idx)) {
371 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have slain %s.")
372 : _("%sを殺した。", "You have slain %s.");
373 msg_format(mes, m_name);
377 auto explode = false;
378 for (auto i = 0; i < 4; i++) {
379 if (r_ref.blow[i].method == RaceBlowMethodType::EXPLODE) {
385 msg_format(_("%sは爆発して粉々になった。", "%^s explodes into tiny shreds."), m_name);
389 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have destroyed %s.")
390 : _("%sを殺した。", "You have destroyed %s.");
391 msg_format(mes, m_name);
394 void MonsterDamageProcessor::show_bounty_message(GAME_TEXT *m_name)
396 auto *floor_ptr = this->player_ptr->current_floor_ptr;
397 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
398 const auto &r_ref = m_ptr->get_real_r_ref();
399 if (r_ref.kind_flags.has_not(MonsterKindType::UNIQUE) || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) || vanilla_town) {
403 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
407 if (MonsterRace(m_ptr->r_idx).is_bounty(true)) {
408 msg_format(_("%sの首には賞金がかかっている。", "There is a price on %s's head."), m_name);
413 * @brief モンスターに与えたダメージを元に経験値を加算する /
414 * Calculate experience point to be get
415 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
418 * Even the 64 bit operation is not big enough to avoid overflaw
419 * unless we carefully choose orders of ENERGY_MULTIPLICATION and ENERGY_DIVISION.
420 * Get the coefficient first, and multiply (potentially huge) base
421 * experience point of a monster later.
424 void MonsterDamageProcessor::get_exp_from_mon(MonsterEntity *m_ptr, int exp_dam)
426 auto *r_ptr = &monraces_info[m_ptr->r_idx];
427 if (!m_ptr->is_valid() || m_ptr->is_pet() || this->player_ptr->phase_out) {
432 * - Ratio of monster's level to player's level effects
433 * - Varying speed effects
434 * - Get a fraction in proportion of damage point
436 auto new_exp = r_ptr->level * speed_to_energy(m_ptr->mspeed) * exp_dam;
437 auto new_exp_frac = 0U;
439 auto div_l = (uint)((this->player_ptr->max_plv + 2) * speed_to_energy(r_ptr->speed));
441 /* Use (average maxhp * 2) as a denominator */
442 auto compensation = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? r_ptr->hside * 2 : r_ptr->hside + 1;
443 s64b_mul(&div_h, &div_l, 0, r_ptr->hdice * (ironman_nightmare ? 2 : 1) * compensation);
445 /* Special penalty in the wilderness */
446 if (!this->player_ptr->current_floor_ptr->dun_level && (r_ptr->wilderness_flags.has_not(MonsterWildernessType::WILD_ONLY) || r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE))) {
447 s64b_mul(&div_h, &div_l, 0, 5);
450 /* Do ENERGY_DIVISION first to prevent overflaw */
451 s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
453 /* Special penalty for mutiply-monster */
454 if (any_bits(r_ptr->flags2, RF2_MULTIPLY) || (m_ptr->r_idx == MonsterRaceId::DAWN)) {
455 int monnum_penarty = r_ptr->r_akills / 400;
456 if (monnum_penarty > 8) {
460 while (monnum_penarty--) {
462 s64b_rshift(&new_exp, &new_exp_frac, 2);
466 /* Special penalty for rest_and_shoot exp scum */
467 if ((m_ptr->dealt_damage > m_ptr->max_maxhp) && (m_ptr->hp >= 0)) {
468 int over_damage = m_ptr->dealt_damage / m_ptr->max_maxhp;
469 if (over_damage > 32) {
473 while (over_damage--) {
475 s64b_mul(&new_exp, &new_exp_frac, 0, 9);
476 s64b_div(&new_exp, &new_exp_frac, 0, 10);
480 s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
481 gain_exp_64(this->player_ptr, new_exp, new_exp_frac);
484 void MonsterDamageProcessor::set_redraw()
486 if (this->player_ptr->health_who == this->m_idx) {
487 this->player_ptr->redraw |= PR_HEALTH;
490 if (this->player_ptr->riding == this->m_idx) {
491 this->player_ptr->redraw |= PR_UHEALTH;
496 * @brief 特定ユニークを倒した時に更にユニークを特殊召喚する処理
497 * @param m_ptr ダメージを与えた特定ユニークの構造体参照ポインタ
499 void MonsterDamageProcessor::summon_special_unique()
501 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
502 bool is_special_summon = m_ptr->r_idx == MonsterRaceId::IKETA;
503 is_special_summon |= m_ptr->r_idx == MonsterRaceId::DOPPIO;
504 if (!is_special_summon || this->player_ptr->current_floor_ptr->inside_arena || this->player_ptr->phase_out) {
505 delete_monster_idx(this->player_ptr, this->m_idx);
509 auto dummy_y = m_ptr->fy;
510 auto dummy_x = m_ptr->fx;
511 auto mode = (BIT_FLAGS)0;
512 if (m_ptr->is_pet()) {
513 mode |= PM_FORCE_PET;
516 MonsterRaceId new_unique_idx;
518 switch (m_ptr->r_idx) {
519 case MonsterRaceId::IKETA:
520 new_unique_idx = MonsterRaceId::BIKETAL;
521 mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha! *I* am Biketal!");
523 case MonsterRaceId::DOPPIO:
524 new_unique_idx = MonsterRaceId::DIAVOLO;
525 mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」", "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
527 default: // バグでなければ入らない.
528 new_unique_idx = MonsterRace::empty_id();
533 delete_monster_idx(this->player_ptr, this->m_idx);
534 if (summon_named_creature(this->player_ptr, 0, dummy_y, dummy_x, new_unique_idx, mode)) {
539 void MonsterDamageProcessor::add_monster_fear()
541 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
542 if (m_ptr->is_fearful() && (this->dam > 0)) {
543 auto fear_remining = m_ptr->get_remaining_fear() - randint1(this->dam);
544 if (set_monster_monfear(this->player_ptr, this->m_idx, fear_remining)) {
549 auto *r_ptr = &monraces_info[m_ptr->r_idx];
550 if (m_ptr->is_fearful() || any_bits(r_ptr->flags3, RF3_NO_FEAR)) {
554 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
555 if ((randint1(10) < percentage) && ((this->dam < m_ptr->hp) || (randint0(100) >= 80))) {
560 auto fear_condition = (this->dam >= m_ptr->hp) && (percentage > 7);
561 auto fear_value = randint1(10) + (fear_condition ? 20 : (11 - percentage) * 5);
562 (void)set_monster_monfear(this->player_ptr, this->m_idx, fear_value);