2 * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
7 #include "monster/monster-damage.h"
8 #include "core/player-redraw-types.h"
9 #include "core/speed-table.h"
10 #include "core/stuff-handler.h"
11 #include "dungeon/dungeon.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-ability-mask.h"
27 #include "monster-race/race-flags1.h"
28 #include "monster-race/race-flags2.h"
29 #include "monster-race/race-flags3.h"
30 #include "monster-race/race-flags7.h"
31 #include "monster-race/race-flags8.h"
32 #include "monster-race/race-indice-types.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-info/avatar.h"
40 #include "player/player-status.h"
41 #include "player/special-defense-types.h"
42 #include "spell-kind/spells-random.h"
43 #include "status/experience.h"
44 #include "system/floor-type-definition.h"
45 #include "system/monster-race-definition.h"
46 #include "system/monster-type-definition.h"
47 #include "system/player-type-definition.h"
48 #include "util/bit-flags-calculator.h"
49 #include "view/display-messages.h"
50 #include "world/world.h"
54 * @param target_ptr プレーヤーへの参照ポインタ
55 * @param m_idx ダメージを与えたモンスターのID
57 * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
58 * @param note モンスターが倒された際の特別なメッセージ述語
60 MonsterDamageProcessor::MonsterDamageProcessor(player_type *target_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *fear)
61 : target_ptr(target_ptr)
69 * @brief モンスターに与えたダメージを元に経験値を加算する /
70 * Calculate experience point to be get
71 * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
74 * Even the 64 bit operation is not big enough to avoid overflaw
75 * unless we carefully choose orders of ENERGY_MULTIPLICATION and ENERGY_DIVISION.
76 * Get the coefficient first, and multiply (potentially huge) base
77 * experience point of a monster later.
80 void MonsterDamageProcessor::get_exp_from_mon(monster_type *m_ptr, HIT_POINT exp_dam)
82 auto *r_ptr = &r_info[m_ptr->r_idx];
83 if (!monster_is_valid(m_ptr) || is_pet(m_ptr) || this->target_ptr->phase_out) {
88 * - Ratio of monster's level to player's level effects
89 * - Varying speed effects
90 * - Get a fraction in proportion of damage point
92 auto new_exp = r_ptr->level * SPEED_TO_ENERGY(m_ptr->mspeed) * exp_dam;
93 auto new_exp_frac = 0U;
95 auto div_l = (uint)((this->target_ptr->max_plv + 2) * SPEED_TO_ENERGY(r_ptr->speed));
97 /* Use (average maxhp * 2) as a denominator */
98 auto compensation = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? r_ptr->hside * 2 : r_ptr->hside + 1;
99 s64b_mul(&div_h, &div_l, 0, r_ptr->hdice * (ironman_nightmare ? 2 : 1) * compensation);
101 /* Special penalty in the wilderness */
102 if (!this->target_ptr->current_floor_ptr->dun_level && (none_bits(r_ptr->flags8, RF8_WILD_ONLY) || none_bits(r_ptr->flags1, RF1_UNIQUE))) {
103 s64b_mul(&div_h, &div_l, 0, 5);
106 /* Do ENERGY_DIVISION first to prevent overflaw */
107 s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
109 /* Special penalty for mutiply-monster */
110 if (any_bits(r_ptr->flags2, RF2_MULTIPLY) || (m_ptr->r_idx == MON_DAWN)) {
111 int monnum_penarty = r_ptr->r_akills / 400;
112 if (monnum_penarty > 8) {
116 while (monnum_penarty--) {
118 s64b_rshift(&new_exp, &new_exp_frac, 2);
122 /* Special penalty for rest_and_shoot exp scum */
123 if ((m_ptr->dealt_damage > m_ptr->max_maxhp) && (m_ptr->hp >= 0)) {
124 int over_damage = m_ptr->dealt_damage / m_ptr->max_maxhp;
125 if (over_damage > 32) {
129 while (over_damage--) {
131 s64b_mul(&new_exp, &new_exp_frac, 0, 9);
132 s64b_div(&new_exp, &new_exp_frac, 0, 10);
136 s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
137 gain_exp_64(this->target_ptr, new_exp, new_exp_frac);
141 * @brief モンスターのHPをダメージに応じて減算する /
142 * @return モンスターが生きていればfalse、死んだらtrue
144 bool MonsterDamageProcessor::mon_take_hit(concptr note)
146 auto *m_ptr = &this->target_ptr->current_floor_ptr->m_list[this->m_idx];
147 auto *r_ptr = &r_info[m_ptr->r_idx];
149 /* Innocent until proven otherwise */
150 auto innocent = true;
154 monster_type exp_mon;
155 (void)COPY(&exp_mon, m_ptr, monster_type);
157 auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
159 this->get_exp_from_mon(&exp_mon, exp_dam);
161 /* Genocided by chaos patron */
162 if (!monster_is_valid(m_ptr)) {
166 /* Redraw (later) if needed */
167 if (this->target_ptr->health_who == this->m_idx) {
168 this->target_ptr->redraw |= PR_HEALTH;
171 if (this->target_ptr->riding == this->m_idx) {
172 this->target_ptr->redraw |= PR_UHEALTH;
175 (void)set_monster_csleep(this->target_ptr, this->m_idx, 0);
177 /* Hack - Cancel any special player stealth magics. -LM- */
178 if (this->target_ptr->special_defense & NINJA_S_STEALTH) {
179 set_superstealth(this->target_ptr, false);
182 /* Genocided by chaos patron */
183 if (this->m_idx == 0) {
187 m_ptr->hp -= this->dam;
188 m_ptr->dealt_damage += this->dam;
190 if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
191 m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
194 if (current_world_ptr->wizard) {
195 msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
200 GAME_TEXT m_name[MAX_NLEN];
202 if (r_info[m_ptr->r_idx].flags7 & RF7_TANUKI) {
203 /* You might have unmasked Tanuki first time */
204 r_ptr = &r_info[m_ptr->r_idx];
205 m_ptr->ap_r_idx = m_ptr->r_idx;
206 if (r_ptr->r_sights < MAX_SHORT)
210 if (m_ptr->mflag2.has(MFLAG2::CHAMELEON)) {
211 /* You might have unmasked Chameleon first time */
212 r_ptr = real_r_ptr(m_ptr);
213 if (r_ptr->r_sights < MAX_SHORT)
217 if (m_ptr->mflag2.has_not(MFLAG2::CLONED)) {
218 /* When the player kills a Unique, it stays dead */
219 if (r_ptr->flags1 & RF1_UNIQUE) {
222 /* Mega-Hack -- Banor & Lupart */
223 if ((m_ptr->r_idx == MON_BANOR) || (m_ptr->r_idx == MON_LUPART)) {
224 r_info[MON_BANORLUPART].max_num = 0;
225 r_info[MON_BANORLUPART].r_pkills++;
226 r_info[MON_BANORLUPART].r_akills++;
227 if (r_info[MON_BANORLUPART].r_tkills < MAX_SHORT)
228 r_info[MON_BANORLUPART].r_tkills++;
229 } else if (m_ptr->r_idx == MON_BANORLUPART) {
230 r_info[MON_BANOR].max_num = 0;
231 r_info[MON_BANOR].r_pkills++;
232 r_info[MON_BANOR].r_akills++;
233 if (r_info[MON_BANOR].r_tkills < MAX_SHORT)
234 r_info[MON_BANOR].r_tkills++;
235 r_info[MON_LUPART].max_num = 0;
236 r_info[MON_LUPART].r_pkills++;
237 r_info[MON_LUPART].r_akills++;
238 if (r_info[MON_LUPART].r_tkills < MAX_SHORT)
239 r_info[MON_LUPART].r_tkills++;
243 /* When the player kills a Nazgul, it stays dead */
244 else if (r_ptr->flags7 & RF7_NAZGUL)
248 /* Count all monsters killed */
249 if (r_ptr->r_akills < MAX_SHORT)
252 /* Recall even invisible uniques or winners */
253 if ((m_ptr->ml && !this->target_ptr->image) || (r_ptr->flags1 & RF1_UNIQUE)) {
254 /* Count kills this life */
255 if (m_ptr->mflag2.has(MFLAG2::KAGE) && (r_info[MON_KAGE].r_pkills < MAX_SHORT))
256 r_info[MON_KAGE].r_pkills++;
257 else if (r_ptr->r_pkills < MAX_SHORT)
260 /* Count kills in all lives */
261 if (m_ptr->mflag2.has(MFLAG2::KAGE) && (r_info[MON_KAGE].r_tkills < MAX_SHORT))
262 r_info[MON_KAGE].r_tkills++;
263 else if (r_ptr->r_tkills < MAX_SHORT)
266 /* Hack -- Auto-recall */
267 monster_race_track(this->target_ptr, m_ptr->ap_r_idx);
270 monster_desc(this->target_ptr, m_name, m_ptr, MD_TRUE_NAME);
272 /* Don't kill Amberites */
273 if ((r_ptr->flags3 & RF3_AMBERITE) && one_in_(2)) {
274 int curses = 1 + randint1(3);
275 bool stop_ty = false;
278 msg_format(_("%^sは恐ろしい血の呪いをあなたにかけた!", "%^s puts a terrible blood curse on you!"), m_name);
279 curse_equipment(this->target_ptr, 100, 50);
282 stop_ty = activate_ty_curse(this->target_ptr, stop_ty, &count);
286 if (r_ptr->flags2 & RF2_CAN_SPEAK) {
288 if (!get_rnd_line(_("mondeath_j.txt", "mondeath.txt"), m_ptr->r_idx, line_got)) {
289 msg_format("%^s %s", m_name, line_got);
293 if (m_ptr->r_idx == MON_SERPENT) {
294 screen_dump = make_screen_dump(this->target_ptr);
299 if (d_info[this->target_ptr->dungeon_idx].flags.has_not(DF::BEGINNER)) {
300 if (!this->target_ptr->current_floor_ptr->dun_level && !this->target_ptr->ambush_flag && !this->target_ptr->current_floor_ptr->inside_arena) {
301 chg_virtue(this->target_ptr, V_VALOUR, -1);
302 } else if (r_ptr->level > this->target_ptr->current_floor_ptr->dun_level) {
303 if (randint1(10) <= (r_ptr->level - this->target_ptr->current_floor_ptr->dun_level))
304 chg_virtue(this->target_ptr, V_VALOUR, 1);
306 if (r_ptr->level > 60) {
307 chg_virtue(this->target_ptr, V_VALOUR, 1);
309 if (r_ptr->level >= 2 * (this->target_ptr->lev + 1))
310 chg_virtue(this->target_ptr, V_VALOUR, 2);
313 if (r_ptr->flags1 & RF1_UNIQUE) {
314 if (r_ptr->flags3 & (RF3_EVIL | RF3_GOOD))
315 chg_virtue(this->target_ptr, V_HARMONY, 2);
317 if (r_ptr->flags3 & RF3_GOOD) {
318 chg_virtue(this->target_ptr, V_UNLIFE, 2);
319 chg_virtue(this->target_ptr, V_VITALITY, -2);
323 chg_virtue(this->target_ptr, V_INDIVIDUALISM, -1);
326 if (m_ptr->r_idx == MON_BEGGAR || m_ptr->r_idx == MON_LEPER) {
327 chg_virtue(this->target_ptr, V_COMPASSION, -1);
330 if ((r_ptr->flags3 & RF3_GOOD) && ((r_ptr->level) / 10 + (3 * this->target_ptr->current_floor_ptr->dun_level) >= randint1(100)))
331 chg_virtue(this->target_ptr, V_UNLIFE, 1);
333 if (r_ptr->d_char == 'A') {
334 if (r_ptr->flags1 & RF1_UNIQUE)
335 chg_virtue(this->target_ptr, V_FAITH, -2);
336 else if ((r_ptr->level) / 10 + (3 * this->target_ptr->current_floor_ptr->dun_level) >= randint1(100)) {
337 if (r_ptr->flags3 & RF3_GOOD)
338 chg_virtue(this->target_ptr, V_FAITH, -1);
340 chg_virtue(this->target_ptr, V_FAITH, 1);
342 } else if (r_ptr->flags3 & RF3_DEMON) {
343 if (r_ptr->flags1 & RF1_UNIQUE)
344 chg_virtue(this->target_ptr, V_FAITH, 2);
345 else if ((r_ptr->level) / 10 + (3 * this->target_ptr->current_floor_ptr->dun_level) >= randint1(100))
346 chg_virtue(this->target_ptr, V_FAITH, 1);
349 if ((r_ptr->flags3 & RF3_UNDEAD) && (r_ptr->flags1 & RF1_UNIQUE))
350 chg_virtue(this->target_ptr, V_VITALITY, 2);
352 if (r_ptr->r_deaths) {
353 if (r_ptr->flags1 & RF1_UNIQUE) {
354 chg_virtue(this->target_ptr, V_HONOUR, 10);
355 } else if ((r_ptr->level) / 10 + (2 * this->target_ptr->current_floor_ptr->dun_level) >= randint1(100)) {
356 chg_virtue(this->target_ptr, V_HONOUR, 1);
359 if ((r_ptr->flags2 & RF2_MULTIPLY) && (r_ptr->r_akills > 1000) && one_in_(10)) {
360 chg_virtue(this->target_ptr, V_VALOUR, -1);
363 for (i = 0; i < 4; i++) {
364 if (r_ptr->blow[i].d_dice != 0)
365 innocent = false; /* Murderer! */
367 if ((r_ptr->blow[i].effect == RBE_EAT_ITEM) || (r_ptr->blow[i].effect == RBE_EAT_GOLD))
369 thief = true; /* Thief! */
372 /* The new law says it is illegal to live in the dungeon */
373 if (r_ptr->level != 0)
377 if (r_ptr->flags1 & RF1_UNIQUE)
378 chg_virtue(this->target_ptr, V_JUSTICE, 3);
379 else if (1 + ((r_ptr->level) / 10 + (2 * this->target_ptr->current_floor_ptr->dun_level)) >= randint1(100))
380 chg_virtue(this->target_ptr, V_JUSTICE, 1);
381 } else if (innocent) {
382 chg_virtue(this->target_ptr, V_JUSTICE, -1);
385 auto magic_ability_flags = r_ptr->ability_flags;
386 magic_ability_flags.reset(RF_ABILITY_NOMAGIC_MASK);
387 if ((r_ptr->flags3 & RF3_ANIMAL) && !(r_ptr->flags3 & RF3_EVIL) && magic_ability_flags.none()) {
389 chg_virtue(this->target_ptr, V_NATURE, -1);
392 if ((r_ptr->flags1 & RF1_UNIQUE) && record_destroy_uniq) {
394 sprintf(note_buf, "%s%s", r_ptr->name.c_str(), m_ptr->mflag2.has(MFLAG2::CLONED) ? _("(クローン)", "(Clone)") : "");
395 exe_write_diary(this->target_ptr, DIARY_UNIQUE, 0, note_buf);
401 /* Death by Missile/Spell attack */
403 msg_format("%^s%s", m_name, note);
406 /* Death by physical attack -- invisible monster */
407 else if (!m_ptr->ml) {
409 if (is_echizen(this->target_ptr))
410 msg_format("せっかくだから%sを殺した。", m_name);
412 msg_format("%sを殺した。", m_name);
414 msg_format("You have killed %s.", m_name);
419 /* Death by Physical attack -- non-living monster */
420 else if (!monster_living(m_ptr->r_idx)) {
421 bool explode = false;
423 for (i = 0; i < 4; i++) {
424 if (r_ptr->blow[i].method == RBM_EXPLODE)
428 /* Special note at death */
430 msg_format(_("%sは爆発して粉々になった。", "%^s explodes into tiny shreds."), m_name);
433 if (is_echizen(this->target_ptr))
434 msg_format("せっかくだから%sを倒した。", m_name);
436 msg_format("%sを倒した。", m_name);
438 msg_format("You have destroyed %s.", m_name);
443 /* Death by Physical attack -- living monster */
446 if (is_echizen(this->target_ptr))
447 msg_format("せっかくだから%sを葬り去った。", m_name);
449 msg_format("%sを葬り去った。", m_name);
451 msg_format("You have slain %s.", m_name);
454 if ((r_ptr->flags1 & RF1_UNIQUE) && m_ptr->mflag2.has_not(MFLAG2::CLONED) && !vanilla_town) {
455 for (i = 0; i < MAX_BOUNTY; i++) {
456 if ((current_world_ptr->bounty_r_idx[i] == m_ptr->r_idx) && m_ptr->mflag2.has_not(MFLAG2::CHAMELEON)) {
457 msg_format(_("%sの首には賞金がかかっている。", "There is a price on %s's head."), m_name);
463 /* Generate treasure */
464 monster_death(this->target_ptr, this->m_idx, true);
466 // @todo デッドアタック扱いにしてここから削除したい.
467 bool is_special_summon = m_ptr->r_idx == MON_IKETA;
468 is_special_summon |= m_ptr->r_idx == MON_DOPPIO;
469 if (is_special_summon && !(this->target_ptr->current_floor_ptr->inside_arena || this->target_ptr->phase_out)) {
470 POSITION dummy_y = m_ptr->fy;
471 POSITION dummy_x = m_ptr->fx;
474 mode |= PM_FORCE_PET;
476 MONRACE_IDX new_unique_idx;
478 switch (m_ptr->r_idx) {
480 new_unique_idx = MON_BIKETAL;
481 mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha! *I* am Biketal!");
484 new_unique_idx = MON_DIAVOLO;
485 mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」",
486 "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
488 default: // バグでなければ入らない.
494 delete_monster_idx(this->target_ptr, this->m_idx);
495 if (summon_named_creature(this->target_ptr, 0, dummy_y, dummy_x, new_unique_idx, mode))
498 delete_monster_idx(this->target_ptr, this->m_idx);
501 this->get_exp_from_mon(&exp_mon, exp_mon.max_maxhp * 2);
506 /* Monster is dead */
509 /* Mega-Hack -- Pain cancels fear */
510 if (monster_fear_remaining(m_ptr) && (this->dam > 0)) {
512 if (set_monster_monfear(this->target_ptr, this->m_idx, monster_fear_remaining(m_ptr) - randint1(this->dam))) {
518 /* Sometimes a monster gets scared by damage */
519 if (!monster_fear_remaining(m_ptr) && none_bits(r_ptr->flags3, RF3_NO_FEAR)) {
520 /* Percentage of fully healthy */
521 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
524 * Run (sometimes) if at 10% or less of max hit points,
525 * or (usually) when hit for half its current hit points
527 if ((randint1(10) >= percentage) || ((this->dam >= m_ptr->hp) && (randint0(100) < 80))) {
528 /* Hack -- note fear */
531 /* Hack -- Add some timed fear */
532 (void)set_monster_monfear(this->target_ptr, this->m_idx, (randint1(10) + (((this->dam >= m_ptr->hp) && (percentage > 7)) ? 20 : ((11 - percentage) * 5))));