OSDN Git Service

Merge pull request #2313 from Slimebreath6078/feature/remove_lyeh
[hengbandforosx/hengbandosx.git] / src / monster / monster-damage.cpp
1 /*
2  * @brief モンスターがダメージを受けた時の処理と経験値の加算処理
3  * @date 2021/08/04
4  * @author Hourier
5  */
6
7 #include <algorithm>
8
9 #include "avatar/avatar-changer.h"
10 #include "core/player-redraw-types.h"
11 #include "core/speed-table.h"
12 #include "core/stuff-handler.h"
13 #include "game-option/birth-options.h"
14 #include "game-option/play-record-options.h"
15 #include "io/files-util.h"
16 #include "io/report.h"
17 #include "io/write-diary.h"
18 #include "main/sound-definitions-table.h"
19 #include "main/sound-of-music.h"
20 #include "mind/mind-ninja.h"
21 #include "monster-floor/monster-death.h"
22 #include "monster-floor/monster-remover.h"
23 #include "monster-floor/monster-summon.h"
24 #include "monster-floor/place-monster-types.h"
25 #include "monster-race/monster-race-hook.h"
26 #include "monster-race/monster-race.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/monster-damage.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 "timed-effect/player-hallucination.h"
48 #include "timed-effect/timed-effects.h"
49 #include "util/bit-flags-calculator.h"
50 #include "view/display-messages.h"
51 #include "world/world.h"
52
53 /*
54  * @brief コンストラクタ
55  * @param player_ptr プレイヤーへの参照ポインタ
56  * @param m_idx ダメージを与えたモンスターのID
57  * @param dam 与えたダメージ量
58  * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
59  * @param attribute 与えたダメージの種類 (単一属性)
60  * @param note モンスターが倒された際の特別なメッセージ述語
61  */
62 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeType attribute)
63     : player_ptr(player_ptr)
64     , m_idx(m_idx)
65     , dam(dam)
66     , fear(fear)
67 {
68     this->attribute_flags.clear();
69     this->attribute_flags.set((AttributeType)attribute);
70 }
71
72 /*
73  * @brief コンストラクタ
74  * @param player_ptr プレイヤーへの参照ポインタ
75  * @param m_idx ダメージを与えたモンスターのID
76  * @param dam 与えたダメージ量
77  * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
78  * @param attribute_flags 与えたダメージの種類 (複数属性)
79  * @param note モンスターが倒された際の特別なメッセージ述語
80  */
81 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeFlags attribute_flags)
82     : player_ptr(player_ptr)
83     , m_idx(m_idx)
84     , dam(dam)
85     , fear(fear)
86     , attribute_flags(attribute_flags)
87 {
88 }
89
90 /*!
91  * @brief モンスターのHPをダメージに応じて減算する /
92  * @return モンスターが生きていればfalse、死んだらtrue
93  */
94 bool MonsterDamageProcessor::mon_take_hit(concptr note)
95 {
96     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
97     auto exp_mon = *m_ptr;
98
99     auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
100
101     this->get_exp_from_mon(&exp_mon, exp_dam);
102     if (this->genocide_chaos_patron()) {
103         return true;
104     }
105
106     m_ptr->hp -= this->dam;
107     m_ptr->dealt_damage += this->dam;
108     if (m_ptr->dealt_damage > m_ptr->max_maxhp * 100) {
109         m_ptr->dealt_damage = m_ptr->max_maxhp * 100;
110     }
111
112     if (w_ptr->wizard) {
113         msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
114     }
115
116     if (this->process_dead_exp_virtue(note, &exp_mon)) {
117         return true;
118     }
119
120     this->add_monster_fear();
121     return false;
122 }
123
124 bool MonsterDamageProcessor::genocide_chaos_patron()
125 {
126     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
127     if (!monster_is_valid(m_ptr)) {
128         this->m_idx = 0;
129     }
130
131     this->set_redraw();
132     (void)set_monster_csleep(this->player_ptr, this->m_idx, 0);
133     set_superstealth(this->player_ptr, false);
134
135     return this->m_idx == 0;
136 }
137
138 bool MonsterDamageProcessor::process_dead_exp_virtue(concptr note, monster_type *exp_mon)
139 {
140     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
141     auto *r_ptr = real_r_ptr(m_ptr);
142     if (m_ptr->hp >= 0) {
143         return false;
144     }
145
146     this->death_special_flag_monster();
147     if (r_ptr->r_akills < MAX_SHORT) {
148         r_ptr->r_akills++;
149     }
150
151     this->increase_kill_numbers();
152     GAME_TEXT m_name[MAX_NLEN];
153     monster_desc(this->player_ptr, m_name, m_ptr, MD_TRUE_NAME);
154     this->death_amberites(m_name);
155     this->dying_scream(m_name);
156     AvatarChanger ac(player_ptr, m_ptr);
157     ac.change_virtue();
158     if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE) && record_destroy_uniq) {
159         char note_buf[160];
160         sprintf(note_buf, "%s%s", r_ptr->name.c_str(), m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) ? _("(クローン)", "(Clone)") : "");
161         exe_write_diary(this->player_ptr, DIARY_UNIQUE, 0, note_buf);
162     }
163
164     sound(SOUND_KILL);
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);
170     *this->fear = false;
171     return true;
172 }
173
174 /*
175  * @brief たぬき、カメレオン、ナズグル、ユニークの死亡時処理
176  * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
177  */
178 void MonsterDamageProcessor::death_special_flag_monster()
179 {
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 = &r_info[r_idx];
183     if (any_bits(r_info[r_idx].flags7, RF7_TANUKI)) {
184         r_ptr = &r_info[r_idx];
185         m_ptr->ap_r_idx = r_idx;
186         if (r_ptr->r_sights < MAX_SHORT) {
187             r_ptr->r_sights++;
188         }
189     }
190
191     if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
192         r_ptr = real_r_ptr(m_ptr);
193         r_idx = real_r_idx(m_ptr);
194         if (r_ptr->r_sights < MAX_SHORT) {
195             r_ptr->r_sights++;
196         }
197     }
198
199     if (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
200         return;
201     }
202
203     if (any_bits(r_ptr->flags7, RF7_NAZGUL)) {
204         r_ptr->max_num--;
205         return;
206     }
207
208     if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
209         return;
210     }
211
212     this->death_unique_monster((monster_race_type)r_idx);
213 }
214
215 /*
216  * @brief ユニークの死亡処理
217  * @param r_idx 死亡したユニークの種族番号
218  */
219 void MonsterDamageProcessor::death_unique_monster(monster_race_type r_idx)
220 {
221     r_info[r_idx].max_num = 0;
222     std::vector<monster_race_type> combined_unique_vec;
223     if (!check_combined_unique(r_idx, &combined_unique_vec)) {
224         return;
225     }
226
227     combined_uniques uniques;
228     const int one_unit = 3;
229     for (auto i = 0U; i < combined_unique_vec.size(); i += one_unit) {
230         auto unique = std::make_tuple(combined_unique_vec[i], combined_unique_vec[i + 1], combined_unique_vec[i + 2]);
231         uniques.push_back(unique);
232     }
233
234     this->death_combined_uniques(r_idx, &uniques);
235 }
236
237 /*
238  * @brief 死亡したモンスターが分裂/合体を行う特殊ユニークか否かの判定処理
239  * @param r_idx 死亡したモンスターの種族番号
240  * @param united_uniques 分裂/合体を行う特殊ユニーク
241  * @details 合体後、合体前1、合体前2 の順にpush_backすること
242  */
243 bool MonsterDamageProcessor::check_combined_unique(const monster_race_type r_idx, std::vector<monster_race_type> *combined_unique_vec)
244 {
245     combined_unique_vec->push_back(MON_BANORLUPART);
246     combined_unique_vec->push_back(MON_BANOR);
247     combined_unique_vec->push_back(MON_LUPART);
248
249     for (const auto &unique : *combined_unique_vec) {
250         if (r_idx == unique) {
251             return true;
252         }
253     }
254
255     return false;
256 }
257
258 /*
259  * @brief 分裂/合体を行う特殊ユニークの死亡処理
260  * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
261  * @uniques 分裂/合体を行う特殊ユニークのリスト
262  */
263 void MonsterDamageProcessor::death_combined_uniques(const monster_race_type r_idx, combined_uniques *combined_uniques)
264 {
265     for (const auto &unique : *combined_uniques) {
266         auto united = (monster_race_type)0;
267         auto split1 = (monster_race_type)0;
268         auto split2 = (monster_race_type)0;
269         std::tie(united, split1, split2) = unique;
270         if ((r_idx == split1) || (r_idx == split2)) {
271             r_info[united].max_num = 0;
272             r_info[united].r_pkills++;
273             r_info[united].r_akills++;
274             if (r_info[united].r_tkills < MAX_SHORT) {
275                 r_info[united].r_tkills++;
276             }
277
278             continue;
279         }
280
281         if (r_idx != united) {
282             continue;
283         }
284
285         r_info[split1].max_num = 0;
286         r_info[split1].r_pkills++;
287         r_info[split1].r_akills++;
288         if (r_info[split1].r_tkills < MAX_SHORT) {
289             r_info[split1].r_tkills++;
290         }
291
292         r_info[split2].max_num = 0;
293         r_info[split2].r_pkills++;
294         r_info[split2].r_akills++;
295         if (r_info[split2].r_tkills < MAX_SHORT) {
296             r_info[split2].r_tkills++;
297         }
298     }
299 }
300
301 void MonsterDamageProcessor::increase_kill_numbers()
302 {
303     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
304     auto *r_ptr = real_r_ptr(m_ptr);
305     auto is_hallucinated = this->player_ptr->effects()->hallucination()->is_hallucinated();
306     if (((m_ptr->ml == 0) || is_hallucinated) && r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
307         return;
308     }
309
310     if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (r_info[MON_KAGE].r_pkills < MAX_SHORT)) {
311         r_info[MON_KAGE].r_pkills++;
312     } else if (r_ptr->r_pkills < MAX_SHORT) {
313         r_ptr->r_pkills++;
314     }
315
316     if (m_ptr->mflag2.has(MonsterConstantFlagType::KAGE) && (r_info[MON_KAGE].r_tkills < MAX_SHORT)) {
317         r_info[MON_KAGE].r_tkills++;
318     } else if (r_ptr->r_tkills < MAX_SHORT) {
319         r_ptr->r_tkills++;
320     }
321
322     monster_race_track(this->player_ptr, m_ptr->ap_r_idx);
323 }
324
325 void MonsterDamageProcessor::death_amberites(GAME_TEXT *m_name)
326 {
327     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
328     auto *r_ptr = real_r_ptr(m_ptr);
329     if (r_ptr->kind_flags.has_not(MonsterKindType::AMBERITE) || one_in_(2)) {
330         return;
331     }
332
333     auto curses = 1 + randint1(3);
334     auto stop_ty = false;
335     auto count = 0;
336     msg_format(_("%^sは恐ろしい血の呪いをあなたにかけた!", "%^s puts a terrible blood curse on you!"), m_name);
337     curse_equipment(this->player_ptr, 100, 50);
338     do {
339         stop_ty = activate_ty_curse(this->player_ptr, stop_ty, &count);
340     } while (--curses);
341 }
342
343 void MonsterDamageProcessor::dying_scream(GAME_TEXT *m_name)
344 {
345     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
346     auto *r_ptr = real_r_ptr(m_ptr);
347     if (none_bits(r_ptr->flags2, RF2_CAN_SPEAK)) {
348         return;
349     }
350
351     char line_got[1024];
352     if (!get_rnd_line(_("mondeath_j.txt", "mondeath.txt"), m_ptr->r_idx, line_got)) {
353         msg_format("%^s %s", m_name, line_got);
354     }
355
356 #ifdef WORLD_SCORE
357     if (m_ptr->r_idx == MON_SERPENT) {
358         screen_dump = make_screen_dump(this->player_ptr);
359     }
360 #endif
361 }
362
363 void MonsterDamageProcessor::show_kill_message(concptr note, GAME_TEXT *m_name)
364 {
365     auto *floor_ptr = this->player_ptr->current_floor_ptr;
366     auto *m_ptr = &floor_ptr->m_list[this->m_idx];
367     auto *r_ptr = real_r_ptr(m_ptr);
368     if (note != nullptr) {
369         msg_format("%^s%s", m_name, note);
370         return;
371     }
372
373     if (!m_ptr->ml) {
374         auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have killed %s.")
375                                                 : _("%sを殺した。", "You have killed %s.");
376         msg_format(mes, m_name);
377         return;
378     }
379
380     if (monster_living(m_ptr->r_idx)) {
381         auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have slain %s.")
382                                                 : _("%sを殺した。", "You have slain %s.");
383         msg_format(mes, m_name);
384         return;
385     }
386
387     auto explode = false;
388     for (auto i = 0; i < 4; i++) {
389         if (r_ptr->blow[i].method == RaceBlowMethodType::EXPLODE) {
390             explode = true;
391         }
392     }
393
394     if (explode) {
395         msg_format(_("%sは爆発して粉々になった。", "%^s explodes into tiny shreds."), m_name);
396         return;
397     }
398
399     auto mes = is_echizen(this->player_ptr) ? _("せっかくだから%sを殺した。", "Because it's time, you have destroyed %s.")
400                                             : _("%sを殺した。", "You have destroyed %s.");
401     msg_format(mes, m_name);
402 }
403
404 void MonsterDamageProcessor::show_bounty_message(GAME_TEXT *m_name)
405 {
406     auto *floor_ptr = this->player_ptr->current_floor_ptr;
407     auto *m_ptr = &floor_ptr->m_list[this->m_idx];
408     auto *r_ptr = real_r_ptr(m_ptr);
409     if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE) || m_ptr->mflag2.has(MonsterConstantFlagType::CLONED) || vanilla_town) {
410         return;
411     }
412
413     for (auto i = 0; i < MAX_BOUNTY; i++) {
414         if ((w_ptr->bounty_r_idx[i] == m_ptr->r_idx) && m_ptr->mflag2.has_not(MonsterConstantFlagType::CHAMELEON)) {
415             msg_format(_("%sの首には賞金がかかっている。", "There is a price on %s's head."), m_name);
416             break;
417         }
418     }
419 }
420
421 /*!
422  * @brief モンスターに与えたダメージを元に経験値を加算する /
423  * Calculate experience point to be get
424  * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
425  * @details
426  * <pre>
427  * Even the 64 bit operation is not big enough to avoid overflaw
428  * unless we carefully choose orders of ENERGY_MULTIPLICATION and ENERGY_DIVISION.
429  * Get the coefficient first, and multiply (potentially huge) base
430  * experience point of a monster later.
431  * </pre>
432  */
433 void MonsterDamageProcessor::get_exp_from_mon(monster_type *m_ptr, int exp_dam)
434 {
435     auto *r_ptr = &r_info[m_ptr->r_idx];
436     if (!monster_is_valid(m_ptr) || is_pet(m_ptr) || this->player_ptr->phase_out) {
437         return;
438     }
439
440     /*
441      * - Ratio of monster's level to player's level effects
442      * - Varying speed effects
443      * - Get a fraction in proportion of damage point
444      */
445     auto new_exp = r_ptr->level * speed_to_energy(m_ptr->mspeed) * exp_dam;
446     auto new_exp_frac = 0U;
447     auto div_h = 0;
448     auto div_l = (uint)((this->player_ptr->max_plv + 2) * speed_to_energy(r_ptr->speed));
449
450     /* Use (average maxhp * 2) as a denominator */
451     auto compensation = any_bits(r_ptr->flags1, RF1_FORCE_MAXHP) ? r_ptr->hside * 2 : r_ptr->hside + 1;
452     s64b_mul(&div_h, &div_l, 0, r_ptr->hdice * (ironman_nightmare ? 2 : 1) * compensation);
453
454     /* Special penalty in the wilderness */
455     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))) {
456         s64b_mul(&div_h, &div_l, 0, 5);
457     }
458
459     /* Do ENERGY_DIVISION first to prevent overflaw */
460     s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
461
462     /* Special penalty for mutiply-monster */
463     if (any_bits(r_ptr->flags2, RF2_MULTIPLY) || (m_ptr->r_idx == MON_DAWN)) {
464         int monnum_penarty = r_ptr->r_akills / 400;
465         if (monnum_penarty > 8) {
466             monnum_penarty = 8;
467         }
468
469         while (monnum_penarty--) {
470             /* Divide by 4 */
471             s64b_rshift(&new_exp, &new_exp_frac, 2);
472         }
473     }
474
475     /* Special penalty for rest_and_shoot exp scum */
476     if ((m_ptr->dealt_damage > m_ptr->max_maxhp) && (m_ptr->hp >= 0)) {
477         int over_damage = m_ptr->dealt_damage / m_ptr->max_maxhp;
478         if (over_damage > 32) {
479             over_damage = 32;
480         }
481
482         while (over_damage--) {
483             /* 9/10 for once */
484             s64b_mul(&new_exp, &new_exp_frac, 0, 9);
485             s64b_div(&new_exp, &new_exp_frac, 0, 10);
486         }
487     }
488
489     s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
490     gain_exp_64(this->player_ptr, new_exp, new_exp_frac);
491 }
492
493 void MonsterDamageProcessor::set_redraw()
494 {
495     if (this->player_ptr->health_who == this->m_idx) {
496         this->player_ptr->redraw |= PR_HEALTH;
497     }
498
499     if (this->player_ptr->riding == this->m_idx) {
500         this->player_ptr->redraw |= PR_UHEALTH;
501     }
502 }
503
504 /*
505  * @brief 特定ユニークを倒した時に更にユニークを特殊召喚する処理
506  * @param m_ptr ダメージを与えた特定ユニークの構造体参照ポインタ
507  */
508 void MonsterDamageProcessor::summon_special_unique()
509 {
510     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
511     bool is_special_summon = m_ptr->r_idx == MON_IKETA;
512     is_special_summon |= m_ptr->r_idx == MON_DOPPIO;
513     if (!is_special_summon || this->player_ptr->current_floor_ptr->inside_arena || this->player_ptr->phase_out) {
514         delete_monster_idx(this->player_ptr, this->m_idx);
515         return;
516     }
517
518     auto dummy_y = m_ptr->fy;
519     auto dummy_x = m_ptr->fx;
520     auto mode = (BIT_FLAGS)0;
521     if (is_pet(m_ptr)) {
522         mode |= PM_FORCE_PET;
523     }
524
525     MONRACE_IDX new_unique_idx;
526     concptr mes;
527     switch (m_ptr->r_idx) {
528     case MON_IKETA:
529         new_unique_idx = MON_BIKETAL;
530         mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha!  *I* am Biketal!");
531         break;
532     case MON_DOPPIO:
533         new_unique_idx = MON_DIAVOLO;
534         mes = _("「これは『試練』だ 過去に打ち勝てという『試練』とオレは受けとった」", "This is a 'trial'. I took it as a 'trial' to overcome in the past.");
535         break;
536     default: // バグでなければ入らない.
537         new_unique_idx = 0;
538         mes = "";
539         break;
540     }
541
542     delete_monster_idx(this->player_ptr, this->m_idx);
543     if (summon_named_creature(this->player_ptr, 0, dummy_y, dummy_x, new_unique_idx, mode)) {
544         msg_print(mes);
545     }
546 }
547
548 void MonsterDamageProcessor::add_monster_fear()
549 {
550     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
551     if (monster_fear_remaining(m_ptr) && (this->dam > 0)) {
552         auto fear_remining = monster_fear_remaining(m_ptr) - randint1(this->dam);
553         if (set_monster_monfear(this->player_ptr, this->m_idx, fear_remining)) {
554             *this->fear = false;
555         }
556     }
557
558     auto *r_ptr = &r_info[m_ptr->r_idx];
559     if (monster_fear_remaining(m_ptr) || any_bits(r_ptr->flags3, RF3_NO_FEAR)) {
560         return;
561     }
562
563     int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
564     if ((randint1(10) < percentage) && ((this->dam < m_ptr->hp) || (randint0(100) >= 80))) {
565         return;
566     }
567
568     *this->fear = true;
569     auto fear_condition = (this->dam >= m_ptr->hp) && (percentage > 7);
570     auto fear_value = randint1(10) + (fear_condition ? 20 : (11 - percentage) * 5);
571     (void)set_monster_monfear(this->player_ptr, this->m_idx, fear_value);
572 }