OSDN Git Service

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