2 * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
7 #include "monster/monster-damage.h"
8 #include "avatar/avatar-changer.h"
9 #include "core/speed-table.h"
10 #include "core/stuff-handler.h"
11 #include "game-option/birth-options.h"
12 #include "game-option/play-record-options.h"
13 #include "io/files-util.h"
14 #include "io/report.h"
15 #include "io/write-diary.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "mind/mind-ninja.h"
19 #include "monster-floor/monster-death.h"
20 #include "monster-floor/monster-remover.h"
21 #include "monster-floor/monster-summon.h"
22 #include "monster-floor/place-monster-types.h"
23 #include "monster-race/monster-race-hook.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-flags1.h"
26 #include "monster-race/race-flags2.h"
27 #include "monster-race/race-flags3.h"
28 #include "monster-race/race-flags7.h"
29 #include "monster-race/race-flags8.h"
30 #include "monster/monster-describer.h"
31 #include "monster/monster-description-types.h"
32 #include "monster/monster-info.h"
33 #include "monster/monster-status-setter.h"
34 #include "monster/monster-status.h"
35 #include "object-enchant/object-curse.h"
36 #include "player/player-status.h"
37 #include "player/special-defense-types.h"
38 #include "spell-kind/spells-random.h"
39 #include "status/experience.h"
40 #include "system/floor-type-definition.h"
41 #include "system/monster-entity.h"
42 #include "system/monster-race-info.h"
43 #include "system/player-type-definition.h"
44 #include "system/redrawing-flags-updater.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"
56 * @param player_ptr プレイヤーへの参照ポインタ
57 * @param m_idx ダメージを与えたモンスターのID
59 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
60 * @param attribute 与えたダメージの種類 (単一属性)
61 * @param note モンスターが倒された際の特別なメッセージ述語
63 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeType attribute)
64 : player_ptr(player_ptr)
69 this->attribute_flags.clear();
70 this->attribute_flags.set((AttributeType)attribute);
75 * @param player_ptr プレイヤーへの参照ポインタ
76 * @param m_idx ダメージを与えたモンスターのID
78 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
79 * @param attribute_flags 与えたダメージの種類 (複数属性)
80 * @param note モンスターが倒された際の特別なメッセージ述語
82 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeFlags attribute_flags)
83 : player_ptr(player_ptr)
87 , attribute_flags(attribute_flags)
92 * @brief モンスターのHPをダメージに応じて減算する /
93 * @return モンスターが生きていればfalse、死んだらtrue
95 bool MonsterDamageProcessor::mon_take_hit(std::string_view note)
97 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
98 auto exp_mon = *m_ptr;
100 auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
102 this->get_exp_from_mon(&exp_mon, exp_dam);
103 if (this->genocide_chaos_patron()) {
107 m_ptr->hp -= this->dam;
108 m_ptr->dealt_damage += this->dam;
109 if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
110 m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
114 msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
117 if (this->process_dead_exp_virtue(note, &exp_mon)) {
121 this->add_monster_fear();
125 bool MonsterDamageProcessor::genocide_chaos_patron()
127 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
128 if (!m_ptr->is_valid()) {
133 (void)set_monster_csleep(this->player_ptr, this->m_idx, 0);
134 set_superstealth(this->player_ptr, false);
136 return this->m_idx == 0;
139 bool MonsterDamageProcessor::process_dead_exp_virtue(std::string_view note, MonsterEntity *exp_mon)
141 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
142 auto &r_ref = m_ptr->get_real_monrace();
143 if (m_ptr->hp >= 0) {
147 this->death_special_flag_monster();
148 if (r_ref.r_akills < MAX_SHORT) {
152 this->increase_kill_numbers();
153 const auto m_name = monster_desc(this->player_ptr, m_ptr, MD_TRUE_NAME);
154 this->death_amberites(m_name);
155 this->dying_scream(m_name);
156 AvatarChanger ac(player_ptr, m_ptr);
158 if (r_ref.kind_flags.has(MonsterKindType::UNIQUE) && record_destroy_uniq) {
159 std::stringstream ss;
160 ss << r_ref.name << (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) ? _("(クローン)", "(Clone)") : "");
161 exe_write_diary(this->player_ptr, DiaryKind::UNIQUE, 0, ss.str());
165 this->show_kill_message(note, m_name);
166 this->show_bounty_message(m_name);
167 monster_death(this->player_ptr, this->m_idx, true, this->attribute_flags);
168 this->summon_special_unique();
169 this->get_exp_from_mon(exp_mon, exp_mon->max_maxhp * 2);
175 * @brief たぬき、カメレオン、ナズグル、ユニークの死亡時処理
176 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
178 void MonsterDamageProcessor::death_special_flag_monster()
180 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
181 auto r_idx = m_ptr->r_idx;
182 auto *r_ptr = &monraces_info[r_idx];
183 if (any_bits(monraces_info[r_idx].flags7, RF7_TANUKI)) {
184 r_ptr = &monraces_info[r_idx];
185 m_ptr->ap_r_idx = r_idx;
186 if (r_ptr->r_sights < MAX_SHORT) {
191 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
192 auto &real_r_ref = m_ptr->get_real_monrace();
193 r_idx = m_ptr->get_real_monrace_id();
194 if (real_r_ref.r_sights < MAX_SHORT) {
195 real_r_ref.r_sights++;
199 if (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
203 if (r_ptr->population_flags.has(MonsterPopulationType::NAZGUL)) {
208 if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
212 this->death_unique_monster(r_idx);
217 * @param r_idx 死亡したユニークの種族番号
219 void MonsterDamageProcessor::death_unique_monster(MonsterRaceId r_idx)
221 monraces_info[r_idx].max_num = 0;
222 auto &monraces = MonraceList::get_instance();
223 if (monraces.can_unify_separate(r_idx)) {
224 monraces.kill_unified_unique(r_idx);
228 void MonsterDamageProcessor::increase_kill_numbers()
230 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
231 auto &r_ref = m_ptr->get_real_monrace();
232 auto is_hallucinated = this->player_ptr->effects()->hallucination()->is_hallucinated();
233 if (((m_ptr->ml == 0) || is_hallucinated) && r_ref.kind_flags.has_not(MonsterKindType::UNIQUE)) {
237 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (monraces_info[MonsterRaceId::KAGE].r_pkills < MAX_SHORT)) {
238 monraces_info[MonsterRaceId::KAGE].r_pkills++;
239 } else if (r_ref.r_pkills < MAX_SHORT) {
243 if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (monraces_info[MonsterRaceId::KAGE].r_tkills < MAX_SHORT)) {
244 monraces_info[MonsterRaceId::KAGE].r_tkills++;
245 } else if (r_ref.r_tkills < MAX_SHORT) {
249 monster_race_track(this->player_ptr, m_ptr->ap_r_idx);
252 void MonsterDamageProcessor::death_amberites(std::string_view m_name)
254 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
255 const auto &r_ref = m_ptr->get_real_monrace();
256 if (r_ref.kind_flags.has_not(MonsterKindType::AMBERITE) || one_in_(2)) {
260 auto curses = 1 + randint1(3);
261 auto stop_ty = false;
263 msg_format(_("%s^は恐ろしい血の呪いをあなたにかけた!", "%s^ puts a terrible blood curse on you!"), m_name.data());
264 curse_equipment(this->player_ptr, 100, 50);
266 stop_ty = activate_ty_curse(this->player_ptr, stop_ty, &count);
270 void MonsterDamageProcessor::dying_scream(std::string_view m_name)
272 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
273 const auto &r_ref = m_ptr->get_real_monrace();
274 if (r_ref.speak_flags.has_none_of({ MonsterSpeakType::SPEAK_ALL, MonsterSpeakType::SPEAK_DEATH })) {
278 const auto death_mes = get_random_line(_("mondeath_j.txt", "mondeath.txt"), enum2i(m_ptr->r_idx));
279 if (death_mes.has_value()) {
280 msg_format("%s^ %s", m_name.data(), death_mes->data());
284 if (m_ptr->r_idx == MonsterRaceId::SERPENT) {
285 screen_dump = make_screen_dump(this->player_ptr);
290 void MonsterDamageProcessor::show_kill_message(std::string_view note, std::string_view m_name)
292 auto *floor_ptr = this->player_ptr->current_floor_ptr;
293 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
295 msg_format("%s^%s", m_name.data(), note.data());
300 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have killed %s.")
301 : _("%sを殺した。", "You have killed %s.");
302 msg_format(mes, m_name.data());
306 const auto is_explodable = m_ptr->is_explodable();
307 const auto died_mes = m_ptr->get_died_message();
308 if (m_ptr->has_living_flag()) {
310 this->show_explosion_message(died_mes, m_name);
314 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを葬り去った。", "Because it's time, you have slain %s.")
315 : _("%sを葬り去った。", "You have slain %s.");
316 msg_format(mes, m_name.data());
321 this->show_explosion_message(died_mes, m_name);
325 auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを倒した。", "Because it's time, you have destroyed %s.")
326 : _("%sを倒した。", "You have destroyed %s.");
327 msg_format(mes, m_name.data());
330 void MonsterDamageProcessor::show_explosion_message(std::string_view died_mes, std::string_view m_name)
332 std::stringstream ss;
333 ss << _(m_name, format("%s^", m_name.data()));
339 void MonsterDamageProcessor::show_bounty_message(std::string_view m_name)
341 auto *floor_ptr = this->player_ptr->current_floor_ptr;
342 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
343 const auto &r_ref = m_ptr->get_real_monrace();
344 if (r_ref.kind_flags.has_not(MonsterKindType::UNIQUE) || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) || vanilla_town) {
348 if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
352 if (MonsterRace(m_ptr->r_idx).is_bounty(true)) {
353 msg_format(_("%sの首には賞金がかかっている。", "There is a price on %s's head."), m_name.data());
358 * @brief モンスターに与えたダメージを元に経験値を加算する /
359 * Calculate experience point to be get
360 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
363 * Even the 64 bit operation is not big enough to avoid overflaw
364 * unless we carefully choose orders of ENERGY_MULTIPLICATION and ENERGY_DIVISION.
365 * Get the coefficient first, and multiply (potentially huge) base
366 * experience point of a monster later.
369 void MonsterDamageProcessor::get_exp_from_mon(MonsterEntity *m_ptr, int exp_dam)
371 auto *r_ptr = &m_ptr->get_monrace();
372 if (!m_ptr->is_valid() || m_ptr->is_pet() || this->player_ptr->phase_out) {
377 * - Ratio of monster's level to player's level effects
378 * - Varying speed effects
379 * - Get a fraction in proportion of damage point
381 auto new_exp = r_ptr->level * speed_to_energy(m_ptr->mspeed) * exp_dam;
382 auto new_exp_frac = 0U;
384 auto div_l = (uint)((this->player_ptr->max_plv + 2) * speed_to_energy(r_ptr->speed));
386 /* Use (average maxhp * 2) as a denominator */
387 auto compensation = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? r_ptr->hside * 2 : r_ptr->hside + 1;
388 s64b_mul(&div_h, &div_l, 0, r_ptr->hdice * (ironman_nightmare ? 2 : 1) * compensation);
390 /* Special penalty in the wilderness */
391 if (!this->player_ptr->current_floor_ptr->is_in_dungeon()) {
392 auto is_dungeon_monster = r_ptr->wilderness_flags.has_not(MonsterWildernessType::WILD_ONLY);
393 is_dungeon_monster |= r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE);
394 if (is_dungeon_monster) {
395 s64b_mul(&div_h, &div_l, 0, 5);
399 /* Do ENERGY_DIVISION first to prevent overflaw */
400 s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
402 /* Special penalty for mutiply-monster */
403 if (any_bits(r_ptr->flags2, RF2_MULTIPLY) || (m_ptr->r_idx == MonsterRaceId::DAWN)) {
404 int monnum_penarty = r_ptr->r_akills / 400;
405 if (monnum_penarty > 8) {
409 while (monnum_penarty--) {
411 s64b_rshift(&new_exp, &new_exp_frac, 2);
415 /* Special penalty for rest_and_shoot exp scum */
416 if ((m_ptr->dealt_damage > m_ptr->max_maxhp) && (m_ptr->hp >= 0)) {
417 int over_damage = m_ptr->dealt_damage / m_ptr->max_maxhp;
418 if (over_damage > 32) {
422 while (over_damage--) {
424 s64b_mul(&new_exp, &new_exp_frac, 0, 9);
425 s64b_div(&new_exp, &new_exp_frac, 0, 10);
429 s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
430 gain_exp_64(this->player_ptr, new_exp, new_exp_frac);
433 void MonsterDamageProcessor::set_redraw()
435 auto &rfu = RedrawingFlagsUpdater::get_instance();
436 if (this->player_ptr->health_who == this->m_idx) {
437 rfu.set_flag(MainWindowRedrawingFlag::HEALTH);
440 if (this->player_ptr->riding == this->m_idx) {
441 rfu.set_flag(MainWindowRedrawingFlag::UHEALTH);
446 * @brief 特定ユニークを倒した時に更にユニークを特殊召喚する処理
447 * @param m_ptr ダメージを与えた特定ユニークの構造体参照ポインタ
449 void MonsterDamageProcessor::summon_special_unique()
451 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
452 bool is_special_summon = m_ptr->r_idx == MonsterRaceId::IKETA;
453 is_special_summon |= m_ptr->r_idx == MonsterRaceId::DOPPIO;
454 if (!is_special_summon || this->player_ptr->current_floor_ptr->inside_arena || this->player_ptr->phase_out) {
455 delete_monster_idx(this->player_ptr, this->m_idx);
459 auto dummy_y = m_ptr->fy;
460 auto dummy_x = m_ptr->fx;
461 auto mode = (BIT_FLAGS)0;
462 if (m_ptr->is_pet()) {
463 mode |= PM_FORCE_PET;
466 MonsterRaceId new_unique_idx;
468 switch (m_ptr->r_idx) {
469 case MonsterRaceId::IKETA:
470 new_unique_idx = MonsterRaceId::BIKETAL;
471 mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha! *I* am Biketal!");
473 case MonsterRaceId::DOPPIO:
474 new_unique_idx = MonsterRaceId::DIAVOLO;
475 mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」", "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
477 default: // バグでなければ入らない.
478 new_unique_idx = MonsterRace::empty_id();
483 delete_monster_idx(this->player_ptr, this->m_idx);
484 if (summon_named_creature(this->player_ptr, 0, dummy_y, dummy_x, new_unique_idx, mode)) {
489 void MonsterDamageProcessor::add_monster_fear()
491 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
492 if (m_ptr->is_fearful() && (this->dam > 0)) {
493 auto fear_remining = m_ptr->get_remaining_fear() - randint1(this->dam);
494 if (set_monster_monfear(this->player_ptr, this->m_idx, fear_remining)) {
499 auto *r_ptr = &m_ptr->get_monrace();
500 if (m_ptr->is_fearful() || any_bits(r_ptr->flags3, RF3_NO_FEAR)) {
504 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
505 if ((randint1(10) < percentage) && ((this->dam < m_ptr->hp) || (randint0(100) >= 80))) {
510 auto fear_condition = (this->dam >= m_ptr->hp) && (percentage > 7);
511 auto fear_value = randint1(10) + (fear_condition ? 20 : (11 - percentage) * 5);
512 (void)set_monster_monfear(this->player_ptr, this->m_idx, fear_value);