OSDN Git Service

[Refactor] #3627 Controller的な処理 MonsterDamageProcessor::death_combined_uniques()...
[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 "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"
50 #include <optional>
51 #include <sstream>
52 #include <string>
53
54 /*
55  * @brief コンストラクタ
56  * @param player_ptr プレイヤーへの参照ポインタ
57  * @param m_idx ダメージを与えたモンスターのID
58  * @param dam 与えたダメージ量
59  * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
60  * @param attribute 与えたダメージの種類 (単一属性)
61  * @param note モンスターが倒された際の特別なメッセージ述語
62  */
63 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeType attribute)
64     : player_ptr(player_ptr)
65     , m_idx(m_idx)
66     , dam(dam)
67     , fear(fear)
68 {
69     this->attribute_flags.clear();
70     this->attribute_flags.set((AttributeType)attribute);
71 }
72
73 /*
74  * @brief コンストラクタ
75  * @param player_ptr プレイヤーへの参照ポインタ
76  * @param m_idx ダメージを与えたモンスターのID
77  * @param dam 与えたダメージ量
78  * @param fear ダメージによってモンスターが恐慌状態に陥ったならばtrue
79  * @param attribute_flags 与えたダメージの種類 (複数属性)
80  * @param note モンスターが倒された際の特別なメッセージ述語
81  */
82 MonsterDamageProcessor::MonsterDamageProcessor(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *fear, AttributeFlags attribute_flags)
83     : player_ptr(player_ptr)
84     , m_idx(m_idx)
85     , dam(dam)
86     , fear(fear)
87     , attribute_flags(attribute_flags)
88 {
89 }
90
91 /*!
92  * @brief モンスターのHPをダメージに応じて減算する /
93  * @return モンスターが生きていればfalse、死んだらtrue
94  */
95 bool MonsterDamageProcessor::mon_take_hit(std::string_view note)
96 {
97     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
98     auto exp_mon = *m_ptr;
99
100     auto exp_dam = (m_ptr->hp > this->dam) ? this->dam : m_ptr->hp;
101
102     this->get_exp_from_mon(&exp_mon, exp_dam);
103     if (this->genocide_chaos_patron()) {
104         return true;
105     }
106
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;
111     }
112
113     if (w_ptr->wizard) {
114         msg_format(_("合計%d/%dのダメージを与えた。", "You do %d (out of %d) damage."), m_ptr->dealt_damage, m_ptr->maxhp);
115     }
116
117     if (this->process_dead_exp_virtue(note, &exp_mon)) {
118         return true;
119     }
120
121     this->add_monster_fear();
122     return false;
123 }
124
125 bool MonsterDamageProcessor::genocide_chaos_patron()
126 {
127     auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
128     if (!m_ptr->is_valid()) {
129         this->m_idx = 0;
130     }
131
132     this->set_redraw();
133     (void)set_monster_csleep(this->player_ptr, this->m_idx, 0);
134     set_superstealth(this->player_ptr, false);
135
136     return this->m_idx == 0;
137 }
138
139 bool MonsterDamageProcessor::process_dead_exp_virtue(std::string_view note, MonsterEntity *exp_mon)
140 {
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) {
144         return false;
145     }
146
147     this->death_special_flag_monster();
148     if (r_ref.r_akills < MAX_SHORT) {
149         r_ref.r_akills++;
150     }
151
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);
157     ac.change_virtue();
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());
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 = &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) {
187             r_ptr->r_sights++;
188         }
189     }
190
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++;
196         }
197     }
198
199     if (m_ptr->mflag2.has(MonsterConstantFlagType::CLONED)) {
200         return;
201     }
202
203     if (r_ptr->population_flags.has(MonsterPopulationType::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(r_idx);
213 }
214
215 /*
216  * @brief ユニークの死亡処理
217  * @param r_idx 死亡したユニークの種族番号
218  */
219 void MonsterDamageProcessor::death_unique_monster(MonsterRaceId r_idx)
220 {
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);
225     }
226 }
227
228 void MonsterDamageProcessor::increase_kill_numbers()
229 {
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)) {
234         return;
235     }
236
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) {
240         r_ref.r_pkills++;
241     }
242
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) {
246         r_ref.r_tkills++;
247     }
248
249     monster_race_track(this->player_ptr, m_ptr->ap_r_idx);
250 }
251
252 void MonsterDamageProcessor::death_amberites(std::string_view m_name)
253 {
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)) {
257         return;
258     }
259
260     auto curses = 1 + randint1(3);
261     auto stop_ty = false;
262     auto count = 0;
263     msg_format(_("%s^は恐ろしい血の呪いをあなたにかけた!", "%s^ puts a terrible blood curse on you!"), m_name.data());
264     curse_equipment(this->player_ptr, 100, 50);
265     do {
266         stop_ty = activate_ty_curse(this->player_ptr, stop_ty, &count);
267     } while (--curses);
268 }
269
270 void MonsterDamageProcessor::dying_scream(std::string_view m_name)
271 {
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 })) {
275         return;
276     }
277
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());
281     }
282
283 #ifdef WORLD_SCORE
284     if (m_ptr->r_idx == MonsterRaceId::SERPENT) {
285         screen_dump = make_screen_dump(this->player_ptr);
286     }
287 #endif
288 }
289
290 void MonsterDamageProcessor::show_kill_message(std::string_view note, std::string_view m_name)
291 {
292     auto *floor_ptr = this->player_ptr->current_floor_ptr;
293     auto *m_ptr = &floor_ptr->m_list[this->m_idx];
294     if (!note.empty()) {
295         msg_format("%s^%s", m_name.data(), note.data());
296         return;
297     }
298
299     if (!m_ptr->ml) {
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());
303         return;
304     }
305
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()) {
309         if (is_explodable) {
310             this->show_explosion_message(died_mes, m_name);
311             return;
312         }
313
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());
317         return;
318     }
319
320     if (is_explodable) {
321         this->show_explosion_message(died_mes, m_name);
322         return;
323     }
324
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());
328 }
329
330 void MonsterDamageProcessor::show_explosion_message(std::string_view died_mes, std::string_view m_name)
331 {
332     std::stringstream ss;
333     ss << _(m_name, format("%s^", m_name.data()));
334     ss << died_mes;
335     msg_print(ss.str());
336     return;
337 }
338
339 void MonsterDamageProcessor::show_bounty_message(std::string_view m_name)
340 {
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) {
345         return;
346     }
347
348     if (m_ptr->mflag2.has(MonsterConstantFlagType::CHAMELEON)) {
349         return;
350     }
351
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());
354     }
355 }
356
357 /*!
358  * @brief モンスターに与えたダメージを元に経験値を加算する /
359  * Calculate experience point to be get
360  * @param m_ptr ダメージを与えたモンスターの構造体参照ポインタ
361  * @details
362  * <pre>
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.
367  * </pre>
368  */
369 void MonsterDamageProcessor::get_exp_from_mon(MonsterEntity *m_ptr, int exp_dam)
370 {
371     auto *r_ptr = &m_ptr->get_monrace();
372     if (!m_ptr->is_valid() || m_ptr->is_pet() || this->player_ptr->phase_out) {
373         return;
374     }
375
376     /*
377      * - Ratio of monster's level to player's level effects
378      * - Varying speed effects
379      * - Get a fraction in proportion of damage point
380      */
381     auto new_exp = r_ptr->level * speed_to_energy(m_ptr->mspeed) * exp_dam;
382     auto new_exp_frac = 0U;
383     auto div_h = 0;
384     auto div_l = (uint)((this->player_ptr->max_plv + 2) * speed_to_energy(r_ptr->speed));
385
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);
389
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);
396         }
397     }
398
399     /* Do ENERGY_DIVISION first to prevent overflaw */
400     s64b_div(&new_exp, &new_exp_frac, div_h, div_l);
401
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) {
406             monnum_penarty = 8;
407         }
408
409         while (monnum_penarty--) {
410             /* Divide by 4 */
411             s64b_rshift(&new_exp, &new_exp_frac, 2);
412         }
413     }
414
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) {
419             over_damage = 32;
420         }
421
422         while (over_damage--) {
423             /* 9/10 for once */
424             s64b_mul(&new_exp, &new_exp_frac, 0, 9);
425             s64b_div(&new_exp, &new_exp_frac, 0, 10);
426         }
427     }
428
429     s64b_mul(&new_exp, &new_exp_frac, 0, r_ptr->mexp);
430     gain_exp_64(this->player_ptr, new_exp, new_exp_frac);
431 }
432
433 void MonsterDamageProcessor::set_redraw()
434 {
435     auto &rfu = RedrawingFlagsUpdater::get_instance();
436     if (this->player_ptr->health_who == this->m_idx) {
437         rfu.set_flag(MainWindowRedrawingFlag::HEALTH);
438     }
439
440     if (this->player_ptr->riding == this->m_idx) {
441         rfu.set_flag(MainWindowRedrawingFlag::UHEALTH);
442     }
443 }
444
445 /*
446  * @brief 特定ユニークを倒した時に更にユニークを特殊召喚する処理
447  * @param m_ptr ダメージを与えた特定ユニークの構造体参照ポインタ
448  */
449 void MonsterDamageProcessor::summon_special_unique()
450 {
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);
456         return;
457     }
458
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;
464     }
465
466     MonsterRaceId new_unique_idx;
467     concptr mes;
468     switch (m_ptr->r_idx) {
469     case MonsterRaceId::IKETA:
470         new_unique_idx = MonsterRaceId::BIKETAL;
471         mes = _("「ハァッハッハッハ!!私がバイケタルだ!!」", "Uwa-hahaha!  *I* am Biketal!");
472         break;
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.");
476         break;
477     default: // バグでなければ入らない.
478         new_unique_idx = MonsterRace::empty_id();
479         mes = "";
480         break;
481     }
482
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)) {
485         msg_print(mes);
486     }
487 }
488
489 void MonsterDamageProcessor::add_monster_fear()
490 {
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)) {
495             *this->fear = false;
496         }
497     }
498
499     auto *r_ptr = &m_ptr->get_monrace();
500     if (m_ptr->is_fearful() || any_bits(r_ptr->flags3, RF3_NO_FEAR)) {
501         return;
502     }
503
504     int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
505     if ((randint1(10) < percentage) && ((this->dam < m_ptr->hp) || (randint0(100) >= 80))) {
506         return;
507     }
508
509     *this->fear = true;
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);
513 }