OSDN Git Service

Merge branch 'master' of https://github.com/hengband/hengband
[hengbandforosx/hengbandosx.git] / src / melee / melee-postprocess.cpp
1 /*!
2  * @brief モンスター同士の打撃後処理 / Melee post-process.
3  * @date 2014/01/17
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  * This software may be copied and distributed for educational, research,\n
7  * and not for profit purposes provided that this copyright and statement\n
8  * are included in all such copies.  Other copyrights may also apply.\n
9  * 2014 Deskull rearranged comment for Doxygen.\n
10  * @details
11  */
12
13 #include "melee/melee-postprocess.h"
14 #include "core/disturbance.h"
15 #include "effect/attribute-types.h"
16 #include "floor/cave.h"
17 #include "floor/geometry.h"
18 #include "grid/grid.h"
19 #include "main/sound-definitions-table.h"
20 #include "main/sound-of-music.h"
21 #include "monster-attack/monster-attack-table.h"
22 #include "monster-floor/monster-death.h"
23 #include "monster-floor/monster-move.h"
24 #include "monster-floor/monster-remover.h"
25 #include "monster-race/monster-race-hook.h"
26 #include "monster-race/race-flags-resistance.h"
27 #include "monster/monster-describer.h"
28 #include "monster/monster-description-types.h"
29 #include "monster/monster-info.h"
30 #include "monster/monster-status-setter.h"
31 #include "monster/monster-status.h"
32 #include "pet/pet-fall-off.h"
33 #include "player-info/class-info.h"
34 #include "player-info/race-types.h"
35 #include "player/player-personality-types.h"
36 #include "system/angband-system.h"
37 #include "system/floor-type-definition.h"
38 #include "system/monster-entity.h"
39 #include "system/monster-race-info.h"
40 #include "system/player-type-definition.h"
41 #include "system/redrawing-flags-updater.h"
42 #include "tracking/health-bar-tracker.h"
43 #include "util/bit-flags-calculator.h"
44 #include "util/string-processor.h"
45 #include "view/display-messages.h"
46 #include <string>
47
48 // Melee-post-process-type
49 struct mam_pp_type {
50     mam_pp_type(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *dead, bool *fear, std::string_view note, MONSTER_IDX src_idx);
51     MONSTER_IDX m_idx;
52     MonsterEntity *m_ptr;
53     int dam;
54     bool *dead;
55     bool *fear;
56     std::string note;
57     MONSTER_IDX src_idx;
58     bool seen;
59     bool known; /* Can the player be aware of this attack? */
60     std::string m_name;
61 };
62
63 mam_pp_type::mam_pp_type(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *dead, bool *fear, std::string_view note, MONSTER_IDX src_idx)
64     : m_idx(m_idx)
65     , m_ptr(&player_ptr->current_floor_ptr->m_list[m_idx])
66     , dam(dam)
67     , dead(dead)
68     , fear(fear)
69     , note(note)
70     , src_idx(src_idx)
71 {
72     this->seen = is_seen(player_ptr, this->m_ptr);
73     this->known = this->m_ptr->cdis <= MAX_PLAYER_SIGHT;
74     this->m_name = monster_desc(player_ptr, m_ptr, 0);
75 }
76
77 static void prepare_redraw(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
78 {
79     if (!mam_pp_ptr->m_ptr->ml) {
80         return;
81     }
82
83     HealthBarTracker::get_instance().set_flag_if_tracking(mam_pp_ptr->m_idx);
84     if (player_ptr->riding == mam_pp_ptr->m_idx) {
85         RedrawingFlagsUpdater::get_instance().set_flag(MainWindowRedrawingFlag::UHEALTH);
86     }
87 }
88
89 /*!
90  * @brief モンスターが無敵だった場合の処理
91  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
92  * @return 無敵ノーダメならTRUE、無敵でないか無敵を貫通したらFALSE
93  */
94 static bool process_invulnerability(mam_pp_type *mam_pp_ptr)
95 {
96     if (mam_pp_ptr->m_ptr->is_invulnerable() && randint0(PENETRATE_INVULNERABILITY)) {
97         return false;
98     }
99
100     if (mam_pp_ptr->seen) {
101         msg_format(_("%s^はダメージを受けない。", "%s^ is unharmed."), mam_pp_ptr->m_name.data());
102     }
103
104     return true;
105 }
106
107 /*!
108  * @brief 魔法完全防御持ちの処理
109  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
110  * @return ノーダメならTRUE、 そうでないならFALSE
111  */
112 static bool process_all_resistances(mam_pp_type *mam_pp_ptr)
113 {
114     auto *r_ptr = &mam_pp_ptr->m_ptr->get_monrace();
115     if (r_ptr->resistance_flags.has_not(MonsterResistanceType::RESIST_ALL)) {
116         return false;
117     }
118
119     if (mam_pp_ptr->dam > 0) {
120         mam_pp_ptr->dam /= 100;
121         if ((mam_pp_ptr->dam == 0) && one_in_(3)) {
122             mam_pp_ptr->dam = 1;
123         }
124     }
125
126     if (mam_pp_ptr->dam != 0) {
127         return false;
128     }
129
130     if (mam_pp_ptr->seen) {
131         msg_format(_("%s^はダメージを受けない。", "%s^ is unharmed."), mam_pp_ptr->m_name.data());
132     }
133
134     return true;
135 }
136
137 /*!
138  * @brief モンスター死亡時のメッセージ表示
139  * @param player_ptr プレイヤーへの参照ポインタ
140  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
141  * @details
142  * 見えない位置で死んだら何も表示しない
143  * 爆発して粉々になった等ならその旨を、残りは生命か無生命かで分岐
144  */
145 static void print_monster_dead_by_monster(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
146 {
147     if (!mam_pp_ptr->known) {
148         return;
149     }
150
151     mam_pp_ptr->m_name = monster_desc(player_ptr, mam_pp_ptr->m_ptr, MD_TRUE_NAME);
152     if (!mam_pp_ptr->seen) {
153         player_ptr->current_floor_ptr->monster_noise = true;
154         return;
155     }
156
157     if (!mam_pp_ptr->note.empty()) {
158         sound_type kill_sound = mam_pp_ptr->m_ptr->has_living_flag() ? SOUND_KILL : SOUND_N_KILL;
159         sound(kill_sound);
160         msg_format(_("%s^%s", "%s^%s"), mam_pp_ptr->m_name.data(), mam_pp_ptr->note.data());
161         return;
162     }
163
164     if (!mam_pp_ptr->m_ptr->has_living_flag()) {
165         sound(SOUND_N_KILL);
166         msg_format(_("%s^は破壊された。", "%s^ is destroyed."), mam_pp_ptr->m_name.data());
167         return;
168     }
169
170     sound(SOUND_KILL);
171     msg_format(_("%s^は殺された。", "%s^ is killed."), mam_pp_ptr->m_name.data());
172 }
173
174 /*!
175  * @brief ダメージを受けたモンスターのHPが0未満になった際の処理
176  * @param player_ptr プレイヤーへの参照ポインタ
177  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
178  * @return 生きていたらTRUE、それ以外 (ユニークは@以外の攻撃では死なない)はFALSE
179  */
180 static bool check_monster_hp(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
181 {
182     const auto &monrace = mam_pp_ptr->m_ptr->get_monrace();
183     if (mam_pp_ptr->m_ptr->hp < 0) {
184         return false;
185     }
186
187     auto is_like_unique = monrace.kind_flags.has(MonsterKindType::UNIQUE);
188     is_like_unique |= monrace.misc_flags.has(MonsterMiscType::QUESTOR);
189     is_like_unique |= monrace.population_flags.has(MonsterPopulationType::NAZGUL);
190     if (is_like_unique && !AngbandSystem::get_instance().is_phase_out()) {
191         mam_pp_ptr->m_ptr->hp = 1;
192         return false;
193     }
194
195     *(mam_pp_ptr->dead) = true;
196     print_monster_dead_by_monster(player_ptr, mam_pp_ptr);
197     monster_gain_exp(player_ptr, mam_pp_ptr->src_idx, mam_pp_ptr->m_ptr->r_idx);
198     monster_death(player_ptr, mam_pp_ptr->m_idx, false, AttributeType::NONE);
199     delete_monster_idx(player_ptr, mam_pp_ptr->m_idx);
200     *(mam_pp_ptr->fear) = false;
201     return true;
202 }
203
204 /*!
205  * @brief 死亡等で恐慌状態をキャンセルする
206  * @param player_ptr プレイヤーへの参照ポインタ
207  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
208  */
209 static void cancel_fear_by_pain(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
210 {
211     const auto &m_ref = *mam_pp_ptr->m_ptr;
212     const auto dam = mam_pp_ptr->dam;
213     if (!m_ref.is_fearful() || (dam <= 0) || !set_monster_monfear(player_ptr, mam_pp_ptr->m_idx, m_ref.get_remaining_fear() - randint1(dam / 4))) {
214         return;
215     }
216
217     *(mam_pp_ptr->fear) = false;
218 }
219
220 /*!
221  * @biref HP残量などに応じてモンスターを恐慌状態にする
222  * @param player_ptr プレイヤーへの参照ポインタ
223  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
224  */
225 static void make_monster_fear(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
226 {
227     auto *r_ptr = &mam_pp_ptr->m_ptr->get_monrace();
228     if (mam_pp_ptr->m_ptr->is_fearful() || (r_ptr->resistance_flags.has_not(MonsterResistanceType::NO_FEAR))) {
229         return;
230     }
231
232     int percentage = (100L * mam_pp_ptr->m_ptr->hp) / mam_pp_ptr->m_ptr->maxhp;
233     bool can_make_fear = ((percentage <= 10) && (randint0(10) < percentage)) || ((mam_pp_ptr->dam >= mam_pp_ptr->m_ptr->hp) && (randint0(100) < 80));
234     if (!can_make_fear) {
235         return;
236     }
237
238     *(mam_pp_ptr->fear) = true;
239     (void)set_monster_monfear(
240         player_ptr, mam_pp_ptr->m_idx, (randint1(10) + (((mam_pp_ptr->dam >= mam_pp_ptr->m_ptr->hp) && (percentage > 7)) ? 20 : ((11 - percentage) * 5))));
241 }
242
243 /*!
244  * @brief モンスター同士の乱闘による落馬処理
245  * @param player_ptr プレイヤーへの参照ポインタ
246  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
247  */
248 static void fall_off_horse_by_melee(PlayerType *player_ptr, mam_pp_type *mam_pp_ptr)
249 {
250     if (!player_ptr->riding || (player_ptr->riding != mam_pp_ptr->m_idx) || (mam_pp_ptr->dam <= 0)) {
251         return;
252     }
253
254     mam_pp_ptr->m_name = monster_desc(player_ptr, mam_pp_ptr->m_ptr, 0);
255     if (mam_pp_ptr->m_ptr->hp > mam_pp_ptr->m_ptr->maxhp / 3) {
256         mam_pp_ptr->dam = (mam_pp_ptr->dam + 1) / 2;
257     }
258
259     if (process_fall_off_horse(player_ptr, (mam_pp_ptr->dam > 200) ? 200 : mam_pp_ptr->dam, false)) {
260         msg_format(_("%s^に振り落とされた!", "You have been thrown off from %s!"), mam_pp_ptr->m_name.data());
261     }
262 }
263
264 /*!
265  * @brief モンスターが敵モンスターに行う打撃処理 /
266  * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
267  * @param m_idx 目標となるモンスターの参照ID
268  * @param dam ダメージ量
269  * @param dead 目標となったモンスターの死亡状態を返す参照ポインタ
270  * @param fear 目標となったモンスターの恐慌状態を返す参照ポインタ
271  * @param note 目標モンスターが死亡した場合の特別メッセージ(nullptrならば標準表示を行う)
272  * @param src_idx 打撃を行ったモンスターの参照ID
273  * @todo 打撃が当たった時の後処理 (爆発持ちのモンスターを爆発させる等)なので、関数名を変更する必要あり
274  */
275 void mon_take_hit_mon(PlayerType *player_ptr, MONSTER_IDX m_idx, int dam, bool *dead, bool *fear, std::string_view note, MONSTER_IDX src_idx)
276 {
277     auto *floor_ptr = player_ptr->current_floor_ptr;
278     auto *m_ptr = &floor_ptr->m_list[m_idx];
279     mam_pp_type tmp_mam_pp(player_ptr, m_idx, dam, dead, fear, note, src_idx);
280     mam_pp_type *mam_pp_ptr = &tmp_mam_pp;
281     prepare_redraw(player_ptr, mam_pp_ptr);
282     (void)set_monster_csleep(player_ptr, m_idx, 0);
283
284     if (player_ptr->riding && (m_idx == player_ptr->riding)) {
285         disturb(player_ptr, true, true);
286     }
287
288     if (process_invulnerability(mam_pp_ptr) || process_all_resistances(mam_pp_ptr)) {
289         return;
290     }
291
292     m_ptr->hp -= dam;
293     if (check_monster_hp(player_ptr, mam_pp_ptr)) {
294         return;
295     }
296
297     *dead = false;
298     cancel_fear_by_pain(player_ptr, mam_pp_ptr);
299     make_monster_fear(player_ptr, mam_pp_ptr);
300     if ((dam > 0) && !m_ptr->is_pet() && !m_ptr->is_friendly() && (mam_pp_ptr->src_idx != m_idx)) {
301         const auto &m_ref = floor_ptr->m_list[src_idx];
302         if (m_ref.is_pet() && !player_ptr->is_located_at({ m_ptr->target_y, m_ptr->target_x })) {
303             set_target(m_ptr, m_ref.fy, m_ref.fx);
304         }
305     }
306
307     fall_off_horse_by_melee(player_ptr, mam_pp_ptr);
308 }