OSDN Git Service

28afd149c0aeab41b01e198080db6b23d48eb73d
[hengbandforosx/hengbandosx.git] / src / monster / monster-damage.cpp
1 /*
2  * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
3  * @date 2021/08/04
4  * @author Hourier
5  */
6
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"
51
52 /*
53  * @brief コンストラクタ
54  * @param target_ptr プレーヤーへの参照ポインタ
55  * @param m_idx ダメージを与えたモンスターのID
56  * @param dam 与えたダメージ量
57  * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
58  * @param note モンスターが倒された際の特別なメッセージ述語
59  */
60 MonsterDamageProcessor::MonsterDamageProcessor(player_type *target_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *fear)
61     : target_ptr(target_ptr)
62     , m_idx(m_idx)
63     , dam(dam)
64     , fear(fear)
65 {
66 }
67
68 /*!
69  * @brief モンスターに与えたダメージを元に経験値を加算する /
70  * Calculate experience point to be get
71  * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
72  * @details
73  * <pre>
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.
78  * </pre>
79  */
80 void MonsterDamageProcessor::get_exp_from_mon(monster_type *m_ptr, HIT_POINT exp_dam)
81 {
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) {
84         return;
85     }
86
87     /*
88      * - Ratio of monster's level to player's level effects
89      * - Varying speed effects
90      * - Get a fraction in proportion of damage point
91      */
92     auto new_exp = r_ptr->level * SPEED_TO_ENERGY(m_ptr->mspeed) * exp_dam;
93     auto new_exp_frac = 0U;
94     auto div_h = 0;
95     auto div_l = (uint)((this->target_ptr->max_plv + 2) * SPEED_TO_ENERGY(r_ptr->speed));
96
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);
100
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);
104     }
105
106     /* Do ENERGY_DIVISION first to prevent overflaw */
107     s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
108
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) {
113             monnum_penarty = 8;
114         }
115
116         while (monnum_penarty--) {
117             /* Divide by 4 */
118             s64b_rshift(&new_exp, &new_exp_frac, 2);
119         }
120     }
121
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) {
126             over_damage = 32;
127         }
128
129         while (over_damage--) {
130             /* 9/10 for once */
131             s64b_mul(&new_exp, &new_exp_frac, 0, 9);
132             s64b_div(&new_exp, &new_exp_frac, 0, 10);
133         }
134     }
135
136     s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
137     gain_exp_64(this->target_ptr, new_exp, new_exp_frac);
138 }
139
140 /*!
141  * @brief モンスターのHPをダメージに応じて減算する /
142  * @return モンスターが生きていればfalse、死んだらtrue
143  */
144 bool MonsterDamageProcessor::mon_take_hit(concptr note)
145 {
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];
148
149     /* Innocent until proven otherwise */
150     auto innocent = true;
151     auto thief = false;
152     int i;
153
154     monster_type exp_mon;
155     (void)COPY(&exp_mon, m_ptr, monster_type);
156
157     auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
158
159     this->get_exp_from_mon(&exp_mon, exp_dam);
160
161     /* Genocided by chaos patron */
162     if (!monster_is_valid(m_ptr)) {
163         this->m_idx = 0;
164     }
165
166     /* Redraw (later) if needed */
167     if (this->target_ptr->health_who == this->m_idx) {
168         this->target_ptr->redraw |= PR_HEALTH;
169     }
170
171     if (this->target_ptr->riding == this->m_idx) {
172         this->target_ptr->redraw |= PR_UHEALTH;
173     }
174
175     (void)set_monster_csleep(this->target_ptr, this->m_idx, 0);
176
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);
180     }
181
182     /* Genocided by chaos patron */
183     if (this->m_idx == 0) {
184         return true;
185     }
186
187     m_ptr->hp -= this->dam;
188     m_ptr->dealt_damage += this->dam;
189
190     if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
191         m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
192     }
193
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);
196     }
197
198     /* It is dead now */
199     if (m_ptr->hp < 0) {
200         GAME_TEXT m_name[MAX_NLEN];
201
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)
207                 r_ptr->r_sights++;
208         }
209
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)
214                 r_ptr->r_sights++;
215         }
216
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) {
220                 r_ptr->max_num = 0;
221
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++;
240                 }
241             }
242
243             /* When the player kills a Nazgul, it stays dead */
244             else if (r_ptr->flags7 & RF7_NAZGUL)
245                 r_ptr->max_num--;
246         }
247
248         /* Count all monsters killed */
249         if (r_ptr->r_akills < MAX_SHORT)
250             r_ptr->r_akills++;
251
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)
258                 r_ptr->r_pkills++;
259
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)
264                 r_ptr->r_tkills++;
265
266             /* Hack -- Auto-recall */
267             monster_race_track(this->target_ptr, m_ptr->ap_r_idx);
268         }
269
270         monster_desc(this->target_ptr, m_name, m_ptr, MD_TRUE_NAME);
271
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;
276             int count = 0;
277
278             msg_format(_("%^sは恐ろしい血の呪いをあなたにかけた!", "%^s puts a terrible blood curse on you!"), m_name);
279             curse_equipment(this->target_ptr, 100, 50);
280
281             do {
282                 stop_ty = activate_ty_curse(this->target_ptr, stop_ty, &count);
283             } while (--curses);
284         }
285
286         if (r_ptr->flags2 & RF2_CAN_SPEAK) {
287             char line_got[1024];
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);
290             }
291
292 #ifdef WORLD_SCORE
293             if (m_ptr->r_idx == MON_SERPENT) {
294                 screen_dump = make_screen_dump(this->target_ptr);
295             }
296 #endif
297         }
298
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);
305             }
306             if (r_ptr->level > 60) {
307                 chg_virtue(this->target_ptr, V_VALOUR, 1);
308             }
309             if (r_ptr->level >= 2 * (this->target_ptr->lev + 1))
310                 chg_virtue(this->target_ptr, V_VALOUR, 2);
311         }
312
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);
316
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);
320             }
321
322             if (one_in_(3))
323                 chg_virtue(this->target_ptr, V_INDIVIDUALISM, -1);
324         }
325
326         if (m_ptr->r_idx == MON_BEGGAR || m_ptr->r_idx == MON_LEPER) {
327             chg_virtue(this->target_ptr, V_COMPASSION, -1);
328         }
329
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);
332
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);
339                 else
340                     chg_virtue(this->target_ptr, V_FAITH, 1);
341             }
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);
347         }
348
349         if ((r_ptr->flags3 & RF3_UNDEAD) && (r_ptr->flags1 & RF1_UNIQUE))
350             chg_virtue(this->target_ptr, V_VITALITY, 2);
351
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);
357             }
358         }
359         if ((r_ptr->flags2 & RF2_MULTIPLY) && (r_ptr->r_akills > 1000) && one_in_(10)) {
360             chg_virtue(this->target_ptr, V_VALOUR, -1);
361         }
362
363         for (i = 0; i < 4; i++) {
364             if (r_ptr->blow[i].d_dice != 0)
365                 innocent = false; /* Murderer! */
366
367             if ((r_ptr->blow[i].effect == RBE_EAT_ITEM) || (r_ptr->blow[i].effect == RBE_EAT_GOLD))
368
369                 thief = true; /* Thief! */
370         }
371
372         /* The new law says it is illegal to live in the dungeon */
373         if (r_ptr->level != 0)
374             innocent = false;
375
376         if (thief) {
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);
383         }
384
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()) {
388             if (one_in_(4))
389                 chg_virtue(this->target_ptr, V_NATURE, -1);
390         }
391
392         if ((r_ptr->flags1 & RF1_UNIQUE) && record_destroy_uniq) {
393             char note_buf[160];
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);
396         }
397
398         /* Make a sound */
399         sound(SOUND_KILL);
400
401         /* Death by Missile/Spell attack */
402         if (note) {
403             msg_format("%^s%s", m_name, note);
404         }
405
406         /* Death by physical attack -- invisible monster */
407         else if (!m_ptr->ml) {
408 #ifdef JP
409             if (is_echizen(this->target_ptr))
410                 msg_format("せっかくだから%sを殺した。", m_name);
411             else
412                 msg_format("%sを殺した。", m_name);
413 #else
414             msg_format("You have killed %s.", m_name);
415 #endif
416
417         }
418
419         /* Death by Physical attack -- non-living monster */
420         else if (!monster_living(m_ptr->r_idx)) {
421             bool explode = false;
422
423             for (i = 0; i < 4; i++) {
424                 if (r_ptr->blow[i].method == RBM_EXPLODE)
425                     explode = true;
426             }
427
428             /* Special note at death */
429             if (explode)
430                 msg_format(_("%sは爆発して粉々になった。", "%^s explodes into tiny shreds."), m_name);
431             else {
432 #ifdef JP
433                 if (is_echizen(this->target_ptr))
434                     msg_format("せっかくだから%sを倒した。", m_name);
435                 else
436                     msg_format("%sを倒した。", m_name);
437 #else
438                 msg_format("You have destroyed %s.", m_name);
439 #endif
440             }
441         }
442
443         /* Death by Physical attack -- living monster */
444         else {
445 #ifdef JP
446             if (is_echizen(this->target_ptr))
447                 msg_format("せっかくだから%sを葬り去った。", m_name);
448             else
449                 msg_format("%sを葬り去った。", m_name);
450 #else
451             msg_format("You have slain %s.", m_name);
452 #endif
453         }
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);
458                     break;
459                 }
460             }
461         }
462
463         /* Generate treasure */
464         monster_death(this->target_ptr, this->m_idx, true);
465
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;
472             BIT_FLAGS mode = 0L;
473             if (is_pet(m_ptr))
474                 mode |= PM_FORCE_PET;
475
476             MONRACE_IDX new_unique_idx;
477             concptr mes;
478             switch (m_ptr->r_idx) {
479             case MON_IKETA:
480                 new_unique_idx = MON_BIKETAL;
481                 mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha!  *I* am Biketal!");
482                 break;
483             case MON_DOPPIO:
484                 new_unique_idx = MON_DIAVOLO;
485                 mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」",
486                     "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
487                 break;
488             default: // バグでなければ入らない.
489                 new_unique_idx = 0;
490                 mes = "";
491                 break;
492             }
493
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))
496                 msg_print(mes);
497         } else {
498             delete_monster_idx(this->target_ptr, this->m_idx);
499         }
500
501         this->get_exp_from_mon(&exp_mon, exp_mon.max_maxhp * 2);
502
503         /* Not afraid */
504         *this->fear = false;
505
506         /* Monster is dead */
507         return true;
508     }
509     /* Mega-Hack -- Pain cancels fear */
510     if (monster_fear_remaining(m_ptr) && (this->dam > 0)) {
511         /* Cure fear */
512         if (set_monster_monfear(this->target_ptr, this->m_idx, monster_fear_remaining(m_ptr) - randint1(this->dam))) {
513             /* No more fear */
514             *this->fear = false;
515         }
516     }
517
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;
522
523         /*
524          * Run (sometimes) if at 10% or less of max hit points,
525          * or (usually) when hit for half its current hit points
526          */
527         if ((randint1(10) >= percentage) || ((this->dam >= m_ptr->hp) && (randint0(100) < 80))) {
528             /* Hack -- note fear */
529             *this->fear = true;
530
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))));
533         }
534     }
535
536     /* Not dead yet */
537     return false;
538 }