OSDN Git Service

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