OSDN Git Service

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