OSDN Git Service

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