OSDN Git Service

b2a8d00d14ecbac6fa0424cdf2aa64d7454d42db
[hengband/hengband.git] / src / effect / effect-player.c
1 /*!
2  * @brief 魔法によるプレーヤーへの効果まとめ
3  * @date 2020/04/29
4  * @author Hourier
5  */
6
7 #include "effect/effect-player.h"
8 #include "effect/effect-characteristics.h"
9 #include "effect/effect-player-switcher.h"
10 #include "effect/effect-player-util.h"
11 #include "effect/spells-effect-util.h"
12 #include "main/sound-definitions-table.h"
13 #include "main/sound-of-music.h"
14 #include "mind/mind-ninja.h"
15 #include "monster/monster-describer.h"
16 #include "monster/monster-description-types.h"
17 #include "player/player-effects.h"
18 #include "player/player-move.h"
19 #include "realm/realm-hex-numbers.h"
20 #include "spell-realm/spells-hex.h"
21 #include "spell/spell-types.h"
22 #include "view/display-messages.h"
23
24 typedef enum effect_player_check_result {
25     EP_CHECK_FALSE = 0,
26     EP_CHECK_TRUE = 1,
27     EP_CHECK_CONTINUE = 2,
28 } ep_check_result;
29
30 /*!
31  * @brief effect_player_type構造体を初期化する
32  * @param ep_ptr 初期化前の構造体
33  * @param who 魔法を唱えたモンスター (0ならプレーヤー自身)
34  * @param dam 基本威力
35  * @param effect_type 効果属性
36  * @param flag 効果フラグ
37  * @param monspell 効果元のモンスター魔法ID
38  * @return 初期化後の構造体ポインタ
39  */
40 static effect_player_type *initialize_effect_player(
41     effect_player_type *ep_ptr, MONSTER_IDX who, HIT_POINT dam, EFFECT_ID effect_type, BIT_FLAGS flag, int monspell)
42 {
43     ep_ptr->rlev = 0;
44     ep_ptr->m_ptr = NULL;
45     ep_ptr->get_damage = 0;
46     ep_ptr->who = who;
47     ep_ptr->dam = dam;
48     ep_ptr->effect_type = effect_type;
49     ep_ptr->flag = flag;
50     ep_ptr->monspell = monspell;
51     return ep_ptr;
52 }
53
54 /*!
55  * @brief ボルト魔法を反射する
56  * @param target_ptr プレーヤーへの参照ポインタ
57  * @param ep_ptr プレーヤー効果構造体への参照ポインタ
58  * @return 当たったらFALSE、反射したらTRUE
59  */
60 static bool process_bolt_reflection(player_type *target_ptr, effect_player_type *ep_ptr, project_func project)
61 {
62     bool can_bolt_hit = target_ptr->reflect || (((target_ptr->special_defense & KATA_FUUJIN) != 0) && !target_ptr->blind);
63     can_bolt_hit &= (ep_ptr->flag & PROJECT_REFLECTABLE) != 0;
64     can_bolt_hit &= !one_in_(10);
65     if (!can_bolt_hit)
66         return FALSE;
67
68     POSITION t_y, t_x;
69     int max_attempts = 10;
70     sound(SOUND_REFLECT);
71
72     if (target_ptr->blind)
73         msg_print(_("何かが跳ね返った!", "Something bounces!"));
74     else if (target_ptr->special_defense & KATA_FUUJIN)
75         msg_print(_("風の如く武器を振るって弾き返した!", "The attack bounces!"));
76     else
77         msg_print(_("攻撃が跳ね返った!", "The attack bounces!"));
78
79     if (ep_ptr->who > 0) {
80         floor_type *floor_ptr = target_ptr->current_floor_ptr;
81         monster_type m_type = floor_ptr->m_list[ep_ptr->who];
82         do {
83             t_y = m_type.fy - 1 + randint1(3);
84             t_x = m_type.fx - 1 + randint1(3);
85             max_attempts--;
86         } while (max_attempts && in_bounds2u(floor_ptr, t_y, t_x) && !projectable(target_ptr, target_ptr->y, target_ptr->x, t_y, t_x));
87
88         if (max_attempts < 1) {
89             t_y = m_type.fy;
90             t_x = m_type.fx;
91         }
92     } else {
93         t_y = target_ptr->y - 1 + randint1(3);
94         t_x = target_ptr->x - 1 + randint1(3);
95     }
96
97     (*project)(target_ptr, 0, 0, t_y, t_x, ep_ptr->dam, ep_ptr->effect_type, (PROJECT_STOP | PROJECT_KILL | PROJECT_REFLECTABLE), ep_ptr->monspell);
98     disturb(target_ptr, TRUE, TRUE);
99     return TRUE;
100 }
101
102 /*!
103  * @brief 反射・忍者の変わり身などでそもそも当たらない状況を判定する
104  * @param target_ptr プレーヤーへの参照ポインタ
105  * @param ep_ptr プレーヤー効果構造体への参照ポインタ
106  * @param y 目標Y座標
107  * @param x 目標X座標
108  * @return 当たらなかったらFALSE、反射したらTRUE、当たったらCONTINUE
109  */
110 static ep_check_result check_continue_player_effect(player_type *target_ptr, effect_player_type *ep_ptr, POSITION y, POSITION x, project_func project)
111 {
112     if (!player_bold(target_ptr, y, x))
113         return EP_CHECK_FALSE;
114
115     if (((target_ptr->special_defense & NINJA_KAWARIMI) != 0) && (ep_ptr->dam > 0) && (randint0(55) < (target_ptr->lev * 3 / 5 + 20)) && (ep_ptr->who > 0)
116         && (ep_ptr->who != target_ptr->riding) && kawarimi(target_ptr, TRUE))
117         return EP_CHECK_FALSE;
118
119     if ((ep_ptr->who == 0) || (ep_ptr->who == target_ptr->riding))
120         return EP_CHECK_FALSE;
121
122     if (process_bolt_reflection(target_ptr, ep_ptr, project))
123         return EP_CHECK_TRUE;
124
125     return EP_CHECK_CONTINUE;
126 }
127
128 /*!
129  * @brief 魔法を発したモンスター名を記述する
130  * @param target_ptr プレーヤーへの参照ポインタ
131  * @param ep_ptr プレーヤー効果構造体への参照ポインタ
132  * @param who_name モンスター名
133  * @return なし
134  */
135 static void describe_effect_source(player_type *target_ptr, effect_player_type *ep_ptr, concptr who_name)
136 {
137     if (ep_ptr->who > 0) {
138         ep_ptr->m_ptr = &target_ptr->current_floor_ptr->m_list[ep_ptr->who];
139         ep_ptr->rlev = (&r_info[ep_ptr->m_ptr->r_idx])->level >= 1 ? (&r_info[ep_ptr->m_ptr->r_idx])->level : 1;
140         monster_desc(target_ptr, ep_ptr->m_name, ep_ptr->m_ptr, 0);
141         strcpy(ep_ptr->killer, who_name);
142         return;
143     }
144
145     switch (ep_ptr->who) {
146     case PROJECT_WHO_UNCTRL_POWER:
147         strcpy(ep_ptr->killer, _("制御できない力の氾流", "uncontrollable power storm"));
148         break;
149     case PROJECT_WHO_GLASS_SHARDS:
150         strcpy(ep_ptr->killer, _("ガラスの破片", "shards of glass"));
151         break;
152     default:
153         strcpy(ep_ptr->killer, _("罠", "a trap"));
154         break;
155     }
156
157     strcpy(ep_ptr->m_name, ep_ptr->killer);
158 }
159
160 /*!
161  * @brief 汎用的なビーム/ボルト/ボール系によるプレイヤーへの効果処理 / Helper function for "project()" below.
162  * @param who 魔法を発動したモンスター(0ならばプレイヤー、負値ならば自然発生) / Index of "source" monster (zero for "player")
163  * @param who_name 効果を起こしたモンスターの名前
164  * @param r 効果半径(ビーム/ボルト = 0 / ボール = 1以上) / Radius of explosion (0 = beam/bolt, 1 to 9 = ball)
165  * @param y 目標Y座標 / Target y location (or location to travel "towards")
166  * @param x 目標X座標 / Target x location (or location to travel "towards")
167  * @param dam 基本威力 / Base damage roll to apply to affected monsters (or player)
168  * @param effect_type 効果属性 / Type of damage to apply to monsters (and objects)
169  * @param flag 効果フラグ
170  * @param monspell 効果元のモンスター魔法ID
171  * @return 何か一つでも効力があればTRUEを返す / TRUE if any "effects" of the projection were observed, else FALSE
172  */
173 bool affect_player(MONSTER_IDX who, player_type *target_ptr, concptr who_name, int r, POSITION y, POSITION x, HIT_POINT dam, EFFECT_ID effect_type,
174     BIT_FLAGS flag, int monspell, project_func project)
175 {
176     effect_player_type tmp_effect;
177     effect_player_type *ep_ptr = initialize_effect_player(&tmp_effect, who, dam, effect_type, flag, monspell);
178     ep_check_result check_result = check_continue_player_effect(target_ptr, ep_ptr, y, x, project);
179     if (check_result != EP_CHECK_CONTINUE)
180         return check_result;
181
182     if (ep_ptr->dam > 1600)
183         ep_ptr->dam = 1600;
184
185     ep_ptr->dam = (ep_ptr->dam + r) / (r + 1);
186     describe_effect_source(target_ptr, ep_ptr, who_name);
187     switch_effects_player(target_ptr, ep_ptr);
188
189     revenge_store(target_ptr, ep_ptr->get_damage);
190     if ((target_ptr->tim_eyeeye || hex_spelling(target_ptr, HEX_EYE_FOR_EYE)) && (ep_ptr->get_damage > 0) && !target_ptr->is_dead && (ep_ptr->who > 0)) {
191         GAME_TEXT m_name_self[80];
192         monster_desc(target_ptr, m_name_self, ep_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE);
193         msg_format(_("攻撃が%s自身を傷つけた!", "The attack of %s has wounded %s!"), ep_ptr->m_name, m_name_self);
194         (*project)(target_ptr, 0, 0, ep_ptr->m_ptr->fy, ep_ptr->m_ptr->fx, ep_ptr->get_damage, GF_MISSILE, PROJECT_KILL, -1);
195         if (target_ptr->tim_eyeeye)
196             set_tim_eyeeye(target_ptr, target_ptr->tim_eyeeye - 5, TRUE);
197     }
198
199     if (target_ptr->riding && ep_ptr->dam > 0) {
200         rakubadam_p = (ep_ptr->dam > 200) ? 200 : ep_ptr->dam;
201     }
202
203     disturb(target_ptr, TRUE, TRUE);
204     if ((target_ptr->special_defense & NINJA_KAWARIMI) && ep_ptr->dam && ep_ptr->who && (ep_ptr->who != target_ptr->riding)) {
205         (void)kawarimi(target_ptr, FALSE);
206     }
207
208     return TRUE;
209 }