OSDN Git Service

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