OSDN Git Service

93f1678a9720a95d1ca4856685076e7ed039b1dd
[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 "core/player-redraw-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-types.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/monster-race.h"
27 #include "monster-race/race-flags-resistance.h"
28 #include "monster-race/race-flags1.h"
29 #include "monster-race/race-flags3.h"
30 #include "monster-race/race-flags7.h"
31 #include "monster/monster-describer.h"
32 #include "monster/monster-description-types.h"
33 #include "monster/monster-info.h"
34 #include "monster/monster-status-setter.h"
35 #include "monster/monster-status.h"
36 #include "pet/pet-fall-off.h"
37 #include "player/player-class.h"
38 #include "player/player-personality-types.h"
39 #include "player/player-race-types.h"
40 #include "system/floor-type-definition.h"
41 #include "system/monster-race-definition.h"
42 #include "system/monster-type-definition.h"
43 #include "system/player-type-definition.h"
44 #include "view/display-messages.h"
45
46 // Melee-post-process-type
47 typedef struct mam_pp_type {
48     MONSTER_IDX m_idx;
49     monster_type *m_ptr;
50     bool seen;
51     GAME_TEXT m_name[160];
52     HIT_POINT dam;
53     bool known; /* Can the player be aware of this attack? */
54     concptr note;
55     bool *dead;
56     bool *fear;
57     MONSTER_IDX who;
58 } mam_pp_type;
59
60 mam_pp_type *initialize_mam_pp_type(
61     player_type *player_ptr, mam_pp_type *mam_pp_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *dead, bool *fear, concptr note, MONSTER_IDX who)
62 {
63     mam_pp_ptr->m_idx = m_idx;
64     mam_pp_ptr->m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
65     mam_pp_ptr->seen = is_seen(player_ptr, mam_pp_ptr->m_ptr);
66     mam_pp_ptr->dam = dam;
67     mam_pp_ptr->known = mam_pp_ptr->m_ptr->cdis <= MAX_SIGHT;
68     mam_pp_ptr->dead = dead;
69     mam_pp_ptr->fear = fear;
70     mam_pp_ptr->note = note;
71     mam_pp_ptr->who = who;
72     return mam_pp_ptr;
73 }
74
75 static void prepare_redraw(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
76 {
77     if (!mam_pp_ptr->m_ptr->ml)
78         return;
79
80     if (player_ptr->health_who == mam_pp_ptr->m_idx)
81         player_ptr->redraw |= (PR_HEALTH);
82
83     if (player_ptr->riding == mam_pp_ptr->m_idx)
84         player_ptr->redraw |= (PR_UHEALTH);
85 }
86
87 /*!
88  * @brief モンスターが無敵だった場合の処理
89  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
90  * @return 無敵ノーダメならTRUE、無敵でないか無敵を貫通したらFALSE
91  */
92 static bool process_invulnerability(mam_pp_type *mam_pp_ptr)
93 {
94     if (monster_invulner_remaining(mam_pp_ptr->m_ptr) && randint0(PENETRATE_INVULNERABILITY))
95         return false;
96
97     if (mam_pp_ptr->seen)
98         msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), mam_pp_ptr->m_name);
99
100     return true;
101 }
102
103 /*!
104  * @brief 魔法完全防御持ちの処理
105  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
106  * @return ノーダメならTRUE、 そうでないならFALSE
107  */
108 static bool process_all_resistances(mam_pp_type *mam_pp_ptr)
109 {
110     monster_race *r_ptr = &r_info[mam_pp_ptr->m_ptr->r_idx];
111     if ((r_ptr->flagsr & RFR_RES_ALL) == 0)
112         return false;
113
114     if (mam_pp_ptr->dam > 0) {
115         mam_pp_ptr->dam /= 100;
116         if ((mam_pp_ptr->dam == 0) && one_in_(3))
117             mam_pp_ptr->dam = 1;
118     }
119
120     if (mam_pp_ptr->dam != 0)
121         return false;
122
123     if (mam_pp_ptr->seen)
124         msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), mam_pp_ptr->m_name);
125
126     return true;
127 }
128
129 /*!
130  * @brief モンスター死亡時のメッセージ表示
131  * @param player_ptr プレーヤーへの参照ポインタ
132  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
133  * @details
134  * 見えない位置で死んだら何も表示しない
135  * 爆発して粉々になった等ならその旨を、残りは生命か無生命かで分岐
136  */
137 static void print_monster_dead_by_monster(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
138 {
139     if (!mam_pp_ptr->known)
140         return;
141
142     monster_desc(player_ptr, mam_pp_ptr->m_name, mam_pp_ptr->m_ptr, MD_TRUE_NAME);
143     if (!mam_pp_ptr->seen) {
144         player_ptr->current_floor_ptr->monster_noise = true;
145         return;
146     }
147
148     if (mam_pp_ptr->note) {
149         msg_format(_("%^s%s", "%^s%s"), mam_pp_ptr->m_name, mam_pp_ptr->note);
150         return;
151     }
152
153     if (!monster_living(mam_pp_ptr->m_ptr->r_idx)) {
154         msg_format(_("%^sは破壊された。", "%^s is destroyed."), mam_pp_ptr->m_name);
155         return;
156     }
157
158     msg_format(_("%^sは殺された。", "%^s is killed."), mam_pp_ptr->m_name);
159 }
160
161 /*!
162  * @brief ダメージを受けたモンスターのHPが0未満になった際の処理
163  * @param player_ptr プレーヤーへの参照ポインタ
164  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
165  * @return 生きていたらTRUE、それ以外 (ユニークは@以外の攻撃では死なない)はFALSE
166  */
167 static bool check_monster_hp(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
168 {
169     monster_race *r_ptr = &r_info[mam_pp_ptr->m_ptr->r_idx];
170     if (mam_pp_ptr->m_ptr->hp < 0)
171         return false;
172
173     if (((r_ptr->flags1 & (RF1_UNIQUE | RF1_QUESTOR)) || (r_ptr->flags7 & RF7_NAZGUL)) && !player_ptr->phase_out) {
174         mam_pp_ptr->m_ptr->hp = 1;
175         return false;
176     }
177
178     sound_type kill_sound = monster_living(mam_pp_ptr->m_ptr->r_idx) ? SOUND_KILL : SOUND_N_KILL;
179     sound(kill_sound);
180     *(mam_pp_ptr->dead) = true;
181     print_monster_dead_by_monster(player_ptr, mam_pp_ptr);
182     monster_gain_exp(player_ptr, mam_pp_ptr->who, mam_pp_ptr->m_ptr->r_idx);
183     monster_death(player_ptr, mam_pp_ptr->m_idx, false);
184     delete_monster_idx(player_ptr, mam_pp_ptr->m_idx);
185     *(mam_pp_ptr->fear) = false;
186     return true;
187 }
188
189 /*!
190  * @brief 死亡等で恐慌状態をキャンセルする
191  * @param player_ptr プレーヤーへの参照ポインタ
192  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
193  */
194 static void cancel_fear_by_pain(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
195 {
196     if (!monster_fear_remaining(mam_pp_ptr->m_ptr) || (mam_pp_ptr->dam <= 0)
197         || !set_monster_monfear(player_ptr, mam_pp_ptr->m_idx, monster_fear_remaining(mam_pp_ptr->m_ptr) - randint1(mam_pp_ptr->dam / 4)))
198         return;
199
200     *(mam_pp_ptr->fear) = false;
201 }
202
203 /*!
204  * @biref HP残量などに応じてモンスターを恐慌状態にする
205  * @param player_ptr プレーヤーへの参照ポインタ
206  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
207  */
208 static void make_monster_fear(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
209 {
210     monster_race *r_ptr = &r_info[mam_pp_ptr->m_ptr->r_idx];
211     if (monster_fear_remaining(mam_pp_ptr->m_ptr) || ((r_ptr->flags3 & RF3_NO_FEAR) == 0))
212         return;
213
214     int percentage = (100L * mam_pp_ptr->m_ptr->hp) / mam_pp_ptr->m_ptr->maxhp;
215     bool can_make_fear = ((percentage <= 10) && (randint0(10) < percentage)) || ((mam_pp_ptr->dam >= mam_pp_ptr->m_ptr->hp) && (randint0(100) < 80));
216     if (!can_make_fear)
217         return;
218
219     *(mam_pp_ptr->fear) = true;
220     (void)set_monster_monfear(
221         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))));
222 }
223
224 /*!
225  * @brief モンスター同士の乱闘による落馬処理
226  * @param player_ptr プレーヤーへの参照ポインタ
227  * @param mam_pp_ptr 標的モンスター構造体への参照ポインタ
228  */
229 static void fall_off_horse_by_melee(player_type *player_ptr, mam_pp_type *mam_pp_ptr)
230 {
231     if (!player_ptr->riding || (player_ptr->riding != mam_pp_ptr->m_idx) || (mam_pp_ptr->dam <= 0))
232         return;
233
234     monster_desc(player_ptr, mam_pp_ptr->m_name, mam_pp_ptr->m_ptr, 0);
235     if (mam_pp_ptr->m_ptr->hp > mam_pp_ptr->m_ptr->maxhp / 3)
236         mam_pp_ptr->dam = (mam_pp_ptr->dam + 1) / 2;
237
238     if (process_fall_off_horse(player_ptr, (mam_pp_ptr->dam > 200) ? 200 : mam_pp_ptr->dam, false))
239         msg_format(_("%^sに振り落とされた!", "You have been thrown off from %s!"), mam_pp_ptr->m_name);
240 }
241
242 /*!
243  * @brief モンスターが敵モンスターに行う打撃処理 /
244  * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
245  * @param m_idx 目標となるモンスターの参照ID
246  * @param dam ダメージ量
247  * @param dead 目標となったモンスターの死亡状態を返す参照ポインタ
248  * @param fear 目標となったモンスターの恐慌状態を返す参照ポインタ
249  * @param note 目標モンスターが死亡した場合の特別メッセージ(NULLならば標準表示を行う)
250  * @param who 打撃を行ったモンスターの参照ID
251  * @todo 打撃が当たった時の後処理 (爆発持ちのモンスターを爆発させる等)なので、関数名を変更する必要あり
252  */
253 void mon_take_hit_mon(player_type *player_ptr, MONSTER_IDX m_idx, HIT_POINT dam, bool *dead, bool *fear, concptr note, MONSTER_IDX who)
254 {
255     floor_type *floor_ptr = player_ptr->current_floor_ptr;
256     monster_type *m_ptr = &floor_ptr->m_list[m_idx];
257     mam_pp_type tmp_mam_pp;
258     mam_pp_type *mam_pp_ptr = initialize_mam_pp_type(player_ptr, &tmp_mam_pp, m_idx, dam, dead, fear, note, who);
259     monster_desc(player_ptr, mam_pp_ptr->m_name, m_ptr, 0);
260     prepare_redraw(player_ptr, mam_pp_ptr);
261     (void)set_monster_csleep(player_ptr, m_idx, 0);
262
263     if (player_ptr->riding && (m_idx == player_ptr->riding))
264         disturb(player_ptr, true, true);
265
266     if (process_invulnerability(mam_pp_ptr) || process_all_resistances(mam_pp_ptr))
267         return;
268
269     m_ptr->hp -= dam;
270     if (check_monster_hp(player_ptr, mam_pp_ptr))
271         return;
272
273     *dead = false;
274     cancel_fear_by_pain(player_ptr, mam_pp_ptr);
275     make_monster_fear(player_ptr, mam_pp_ptr);
276     if ((dam > 0) && !is_pet(m_ptr) && !is_friendly(m_ptr) && (mam_pp_ptr->who != m_idx)) {
277         if (is_pet(&floor_ptr->m_list[mam_pp_ptr->who]) && !player_bold(player_ptr, m_ptr->target_y, m_ptr->target_x)) {
278             set_target(m_ptr, floor_ptr->m_list[mam_pp_ptr->who].fy, floor_ptr->m_list[mam_pp_ptr->who].fx);
279         }
280     }
281
282     fall_off_horse_by_melee(player_ptr, mam_pp_ptr);
283 }