2 * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
9 #include "monster/monster-damage.h"
10 #include "avatar/avatar-changer.h"
11 #include "core/player-redraw-types.h"
12 #include "core/speed-table.h"
13 #include "core/stuff-handler.h"
14 #include "game-option/birth-options.h"
15 #include "game-option/play-record-options.h"
16 #include "io/files-util.h"
17 #include "io/report.h"
18 #include "io/write-diary.h"
19 #include "main/sound-definitions-table.h"
20 #include "main/sound-of-music.h"
21 #include "mind/mind-ninja.h"
22 #include "monster-floor/monster-death.h"
23 #include "monster-floor/monster-remover.h"
24 #include "monster-floor/monster-summon.h"
25 #include "monster-floor/place-monster-types.h"
26 #include "monster-race/monster-race-hook.h"
27 #include "monster-race/monster-race.h"
28 #include "monster-race/race-flags1.h"
29 #include "monster-race/race-flags2.h"
30 #include "monster-race/race-flags3.h"
31 #include "monster-race/race-flags7.h"
32 #include "monster-race/race-flags8.h"
33 #include "monster/monster-describer.h"
34 #include "monster/monster-description-types.h"
35 #include "monster/monster-info.h"
36 #include "monster/monster-status-setter.h"
37 #include "monster/monster-status.h"
38 #include "object-enchant/object-curse.h"
39 #include "player/player-status.h"
40 #include "player/special-defense-types.h"
41 #include "spell-kind/spells-random.h"
42 #include "status/experience.h"
43 #include "system/floor-type-definition.h"
44 #include "system/monster-race-definition.h"
45 #include "system/monster-type-definition.h"
46 #include "system/player-type-definition.h"
47 #include "util/bit-flags-calculator.h"
48 #include "view/display-messages.h"
49 #include "world/world.h"
53 * @param player_ptr プレイヤーへの参照ポインタ
54 * @param m_idx ダメージを与えたモンスターのID
56 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
57 * @param attribute 与えたダメージの種類 (単一属性)
58 * @param note モンスターが倒された際の特別なメッセージ述語
60 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *fear, AttributeType attribute)
61 : player_ptr(player_ptr)
66 this->attribute_flags.clear();
67 this->attribute_flags.set((AttributeType)attribute);
72 * @param player_ptr プレイヤーへの参照ポインタ
73 * @param m_idx ダメージを与えたモンスターのID
75 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
76 * @param attribute_flags 与えたダメージの種類 (複数属性)
77 * @param note モンスターが倒された際の特別なメッセージ述語
79 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *fear, AttributeFlags attribute_flags)
80 : player_ptr(player_ptr)
84 , attribute_flags(attribute_flags)
89 * @brief モンスターのHPをダメージに応じて減算する /
90 * @return モンスターが生きていればfalse、死んだらtrue
92 bool MonsterDamageProcessor::mon_take_hit(concptr note)
94 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
95 auto exp_mon = *m_ptr;
97 auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
99 this->get_exp_from_mon(&exp_mon, exp_dam);
100 if (this->genocide_chaos_patron()) {
104 m_ptr->hp -= this->dam;
105 m_ptr->dealt_damage += this->dam;
106 if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
107 m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
111 msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
114 if (this->process_dead_exp_virtue(note, &exp_mon)) {
118 this->add_monster_fear();
122 bool MonsterDamageProcessor::genocide_chaos_patron()
124 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
125 if (!monster_is_valid(m_ptr)) {
130 (void)set_monster_csleep(this->player_ptr, this->m_idx, 0);
131 set_superstealth(this->player_ptr, false);
133 return this->m_idx == 0;
136 bool MonsterDamageProcessor::process_dead_exp_virtue(concptr note, monster_type *exp_mon)
138 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
139 auto *r_ptr = real_r_ptr(m_ptr);
140 if (m_ptr->hp >= 0) {
144 this->death_special_flag_monster();
145 if (r_ptr->r_akills < MAX_SHORT) {
149 this->increase_kill_numbers();
150 GAME_TEXT m_name[MAX_NLEN];
151 monster_desc(this->player_ptr, m_name, m_ptr, MD_TRUE_NAME);
152 this->death_amberites(m_name);
153 this->dying_scream(m_name);
154 AvatarChanger ac(player_ptr, m_ptr);
156 if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) && record_destroy_uniq) {
158 sprintf(note_buf, "%s%s", r_ptr->name.c_str(), m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) ? _("(クローン)", "(Clone)") : "");
159 exe_write_diary(this->player_ptr, DIARY_UNIQUE, 0, note_buf);
163 this->show_kill_message(note, m_name);
164 this->show_bounty_message(m_name);
165 monster_death(this->player_ptr, this->m_idx, true, this->attribute_flags);
166 this->summon_special_unique();
167 this->get_exp_from_mon(exp_mon, exp_mon->max_maxhp * 2);
173 * @brief たぬき、カメレオン、ナズグル、ユニークの死亡時処理
174 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
176 void MonsterDamageProcessor::death_special_flag_monster()
178 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
179 auto r_idx = m_ptr->r_idx;
180 auto *r_ptr = &r_info[r_idx];
181 if (any_bits(r_info[r_idx].flags7, RF7_TANUKI)) {
182 r_ptr = &r_info[r_idx];
183 m_ptr->ap_r_idx = r_idx;
184 if (r_ptr->r_sights < MAX_SHORT) {
189 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
190 r_ptr = real_r_ptr(m_ptr);
191 r_idx = real_r_idx(m_ptr);
192 if (r_ptr->r_sights < MAX_SHORT) {
197 if (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
201 if (any_bits(r_ptr->flags7, RF7_NAZGUL)) {
206 if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
210 this->death_unique_monster((monster_race_type)r_idx);
215 * @param r_idx 死亡したユニークの種族番号
217 void MonsterDamageProcessor::death_unique_monster(monster_race_type r_idx)
219 r_info[r_idx].max_num = 0;
220 std::vector<monster_race_type> combined_unique_vec;
221 if (!check_combined_unique(r_idx, &combined_unique_vec)) {
225 combined_uniques uniques;
226 const int one_unit = 3;
227 for (auto i = 0U; i < combined_unique_vec.size(); i += one_unit) {
228 auto unique = std::make_tuple(combined_unique_vec[i], combined_unique_vec[i + 1], combined_unique_vec[i + 2]);
229 uniques.push_back(unique);
232 this->death_combined_uniques(r_idx, &uniques);
236 * @brief 死亡したモンスターが分裂/合体を行う特殊ユニークか否かの判定処理
237 * @param r_idx 死亡したモンスターの種族番号
238 * @param united_uniques 分裂/合体を行う特殊ユニーク
239 * @details 合体後、合体前1、合体前2 の順にpush_backすること
241 bool MonsterDamageProcessor::check_combined_unique(const monster_race_type r_idx, std::vector<monster_race_type> *combined_unique_vec)
243 combined_unique_vec->push_back(MON_BANORLUPART);
244 combined_unique_vec->push_back(MON_BANOR);
245 combined_unique_vec->push_back(MON_LUPART);
247 for (const auto &unique : *combined_unique_vec) {
248 if (r_idx == unique) {
257 * @brief 分裂/合体を行う特殊ユニークの死亡処理
258 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
259 * @uniques 分裂/合体を行う特殊ユニークのリスト
261 void MonsterDamageProcessor::death_combined_uniques(const monster_race_type r_idx, combined_uniques *combined_uniques)
263 for (const auto &unique : *combined_uniques) {
264 auto united = (monster_race_type)0;
265 auto split1 = (monster_race_type)0;
266 auto split2 = (monster_race_type)0;
267 std::tie(united, split1, split2) = unique;
268 if ((r_idx == split1) || (r_idx == split2)) {
269 r_info[united].max_num = 0;
270 r_info[united].r_pkills++;
271 r_info[united].r_akills++;
272 if (r_info[united].r_tkills < MAX_SHORT) {
273 r_info[united].r_tkills++;
279 if (r_idx != united) {
283 r_info[split1].max_num = 0;
284 r_info[split1].r_pkills++;
285 r_info[split1].r_akills++;
286 if (r_info[split1].r_tkills < MAX_SHORT) {
287 r_info[split1].r_tkills++;
290 r_info[split2].max_num = 0;
291 r_info[split2].r_pkills++;
292 r_info[split2].r_akills++;
293 if (r_info[split2].r_tkills < MAX_SHORT) {
294 r_info[split2].r_tkills++;
299 void MonsterDamageProcessor::increase_kill_numbers()
301 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
302 auto *r_ptr = real_r_ptr(m_ptr);
303 if (((m_ptr->ml == 0) || this->player_ptr->hallucinated) && r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
307 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (r_info[MON_KAGE].r_pkills < MAX_SHORT)) {
308 r_info[MON_KAGE].r_pkills++;
309 } else if (r_ptr->r_pkills < MAX_SHORT) {
313 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (r_info[MON_KAGE].r_tkills < MAX_SHORT)) {
314 r_info[MON_KAGE].r_tkills++;
315 } else if (r_ptr->r_tkills < MAX_SHORT) {
319 monster_race_track(this->player_ptr, m_ptr->ap_r_idx);
322 void MonsterDamageProcessor::death_amberites(GAME_TEXT *m_name)
324 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
325 auto *r_ptr = real_r_ptr(m_ptr);
326 if (r_ptr->kind_flags.has_not(MonsterKindType::AMBERITE) || one_in_(2)) {
330 auto curses = 1 + randint1(3);
331 auto stop_ty = false;
333 msg_format(_("%^sは恐ろしい血の呪いをあなたにかけた!", "%^s puts a terrible blood curse on you!"), m_name);
334 curse_equipment(this->player_ptr, 100, 50);
336 stop_ty = activate_ty_curse(this->player_ptr, stop_ty, &count);
340 void MonsterDamageProcessor::dying_scream(GAME_TEXT *m_name)
342 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
343 auto *r_ptr = real_r_ptr(m_ptr);
344 if (none_bits(r_ptr->flags2, RF2_CAN_SPEAK)) {
349 if (!get_rnd_line(_("mondeath_j.txt", "mondeath.txt"), m_ptr->r_idx, line_got)) {
350 msg_format("%^s %s", m_name, line_got);
354 if (m_ptr->r_idx == MON_SERPENT) {
355 screen_dump = make_screen_dump(this->player_ptr);
360 void MonsterDamageProcessor::show_kill_message(concptr note, GAME_TEXT *m_name)
362 auto *floor_ptr = this->player_ptr->current_floor_ptr;
363 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
364 auto *r_ptr = real_r_ptr(m_ptr);
365 if (note != nullptr) {
366 msg_format("%^s%s", m_name, note);
371 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have killed %s.")
372 : _("%sを殺した。", "You have killed %s.");
373 msg_format(mes, m_name);
377 if (monster_living(m_ptr->r_idx)) {
378 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have slain %s.")
379 : _("%sを殺した。", "You have slain %s.");
380 msg_format(mes, m_name);
384 auto explode = false;
385 for (auto i = 0; i < 4; i++) {
386 if (r_ptr->blow[i].method == RaceBlowMethodType::EXPLODE) {
392 msg_format(_("%sは爆発して粉々になった。", "%^s explodes into tiny shreds."), m_name);
396 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have destroyed %s.")
397 : _("%sを殺した。", "You have destroyed %s.");
398 msg_format(mes, m_name);
401 void MonsterDamageProcessor::show_bounty_message(GAME_TEXT *m_name)
403 auto *floor_ptr = this->player_ptr->current_floor_ptr;
404 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
405 auto *r_ptr = real_r_ptr(m_ptr);
406 if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE) || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) || vanilla_town) {
410 for (auto i = 0; i < MAX_BOUNTY; i++) {
411 if ((w_ptr->bounty_r_idx[i] == m_ptr->r_idx) && m_ptr->mflag2.has_not(MonsterConstantFlagType::CHAMELEON)) {
412 msg_format(_("%sの首には賞金がかかっている。", "There is a price on %s's head."), m_name);
419 * @brief モンスターに与えたダメージを元に経験値を加算する /
420 * Calculate experience point to be get
421 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
424 * Even the 64 bit operation is not big enough to avoid overflaw
425 * unless we carefully choose orders of ENERGY_MULTIPLICATION and ENERGY_DIVISION.
426 * Get the coefficient first, and multiply (potentially huge) base
427 * experience point of a monster later.
430 void MonsterDamageProcessor::get_exp_from_mon(monster_type *m_ptr, HIT_POINT exp_dam)
432 auto *r_ptr = &r_info[m_ptr->r_idx];
433 if (!monster_is_valid(m_ptr) || is_pet(m_ptr) || this->player_ptr->phase_out) {
438 * - Ratio of monster's level to player's level effects
439 * - Varying speed effects
440 * - Get a fraction in proportion of damage point
442 auto new_exp = r_ptr->level * SPEED_TO_ENERGY(m_ptr->mspeed) * exp_dam;
443 auto new_exp_frac = 0U;
445 auto div_l = (uint)((this->player_ptr->max_plv + 2) * SPEED_TO_ENERGY(r_ptr->speed));
447 /* Use (average maxhp * 2) as a denominator */
448 auto compensation = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? r_ptr->hside * 2 : r_ptr->hside + 1;
449 s64b_mul(&div_h, &div_l, 0, r_ptr->hdice * (ironman_nightmare ? 2 : 1) * compensation);
451 /* Special penalty in the wilderness */
452 if (!this->player_ptr->current_floor_ptr->dun_level && (none_bits(r_ptr->flags8, RF8_WILD_ONLY) || r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE))) {
453 s64b_mul(&div_h, &div_l, 0, 5);
456 /* Do ENERGY_DIVISION first to prevent overflaw */
457 s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
459 /* Special penalty for mutiply-monster */
460 if (any_bits(r_ptr->flags2, RF2_MULTIPLY) || (m_ptr->r_idx == MON_DAWN)) {
461 int monnum_penarty = r_ptr->r_akills / 400;
462 if (monnum_penarty > 8) {
466 while (monnum_penarty--) {
468 s64b_rshift(&new_exp, &new_exp_frac, 2);
472 /* Special penalty for rest_and_shoot exp scum */
473 if ((m_ptr->dealt_damage > m_ptr->max_maxhp) && (m_ptr->hp >= 0)) {
474 int over_damage = m_ptr->dealt_damage / m_ptr->max_maxhp;
475 if (over_damage > 32) {
479 while (over_damage--) {
481 s64b_mul(&new_exp, &new_exp_frac, 0, 9);
482 s64b_div(&new_exp, &new_exp_frac, 0, 10);
486 s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
487 gain_exp_64(this->player_ptr, new_exp, new_exp_frac);
490 void MonsterDamageProcessor::set_redraw()
492 if (this->player_ptr->health_who == this->m_idx) {
493 this->player_ptr->redraw |= PR_HEALTH;
496 if (this->player_ptr->riding == this->m_idx) {
497 this->player_ptr->redraw |= PR_UHEALTH;
502 * @brief 特定ユニークを倒した時に更にユニークを特殊召喚する処理
503 * @param m_ptr ダメージを与えた特定ユニークの構造体参照ポインタ
505 void MonsterDamageProcessor::summon_special_unique()
507 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
508 bool is_special_summon = m_ptr->r_idx == MON_IKETA;
509 is_special_summon |= m_ptr->r_idx == MON_DOPPIO;
510 if (!is_special_summon || this->player_ptr->current_floor_ptr->inside_arena || this->player_ptr->phase_out) {
511 delete_monster_idx(this->player_ptr, this->m_idx);
515 auto dummy_y = m_ptr->fy;
516 auto dummy_x = m_ptr->fx;
517 auto mode = (BIT_FLAGS)0;
519 mode |= PM_FORCE_PET;
522 MONRACE_IDX new_unique_idx;
524 switch (m_ptr->r_idx) {
526 new_unique_idx = MON_BIKETAL;
527 mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha! *I* am Biketal!");
530 new_unique_idx = MON_DIAVOLO;
531 mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」", "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
533 default: // バグでなければ入らない.
539 delete_monster_idx(this->player_ptr, this->m_idx);
540 if (summon_named_creature(this->player_ptr, 0, dummy_y, dummy_x, new_unique_idx, mode)) {
545 void MonsterDamageProcessor::add_monster_fear()
547 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
548 if (monster_fear_remaining(m_ptr) && (this->dam > 0)) {
549 auto fear_remining = monster_fear_remaining(m_ptr) - randint1(this->dam);
550 if (set_monster_monfear(this->player_ptr, this->m_idx, fear_remining)) {
555 auto *r_ptr = &r_info[m_ptr->r_idx];
556 if (monster_fear_remaining(m_ptr) || any_bits(r_ptr->flags3, RF3_NO_FEAR)) {
560 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
561 if ((randint1(10) < percentage) && ((this->dam < m_ptr->hp) || (randint0(100) >= 80))) {
566 auto fear_condition = (this->dam >= m_ptr->hp) && (percentage > 7);
567 auto fear_value = randint1(10) + (fear_condition ? 20 : (11 - percentage) * 5);
568 (void)set_monster_monfear(this->player_ptr, this->m_idx, fear_value);