OSDN Git Service

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