OSDN Git Service

Merge pull request #2122 from sikabane-works/release/3.0.0Alpha52
[hengbandforosx/hengbandosx.git] / src / monster-attack / monster-attack-player.cpp
1 /*!
2  * @brief モンスターからプレイヤーへの直接攻撃処理
3  * @date 2020/05/23
4  * @author Hourier
5  */
6
7 #include "monster-attack/monster-attack-player.h"
8 #include "cmd-action/cmd-attack.h"
9 #include "cmd-action/cmd-pet.h"
10 #include "combat/attack-accuracy.h"
11 #include "combat/attack-criticality.h"
12 #include "combat/aura-counterattack.h"
13 #include "combat/combat-options-type.h"
14 #include "combat/hallucination-attacks-table.h"
15 #include "core/disturbance.h"
16 #include "core/player-update-types.h"
17 #include "dungeon/dungeon-flag-types.h"
18 #include "dungeon/dungeon.h"
19 #include "floor/geometry.h"
20 #include "inventory/inventory-slot-types.h"
21 #include "main/sound-definitions-table.h"
22 #include "main/sound-of-music.h"
23 #include "mind/mind-ninja.h"
24 #include "mind/mind-samurai.h"
25 #include "monster-attack/monster-attack-describer.h"
26 #include "monster-attack/monster-attack-effect.h"
27 #include "monster-attack/monster-attack-switcher.h"
28 #include "monster-attack/monster-attack-types.h"
29 #include "monster-race/monster-race.h"
30 #include "monster-race/race-flags1.h"
31 #include "monster-race/race-flags3.h"
32 #include "monster/monster-damage.h"
33 #include "monster/monster-describer.h"
34 #include "monster/monster-description-types.h"
35 #include "monster/monster-info.h"
36 #include "monster/monster-status.h"
37 #include "monster/smart-learn-types.h"
38 #include "object-hook/hook-armor.h"
39 #include "object/item-tester-hooker.h"
40 #include "pet/pet-fall-off.h"
41 #include "player-base/player-class.h"
42 #include "player-info/samurai-data-type.h"
43 #include "player/attack-defense-types.h"
44 #include "player/player-damage.h"
45 #include "player/player-skill.h"
46 #include "player/special-defense-types.h"
47 #include "spell-realm/spells-hex.h"
48 #include "status/action-setter.h"
49 #include "status/bad-status-setter.h"
50 #include "system/angband.h"
51 #include "system/floor-type-definition.h"
52 #include "system/monster-race-definition.h"
53 #include "system/monster-type-definition.h"
54 #include "system/object-type-definition.h"
55 #include "system/player-type-definition.h"
56 #include "timed-effect/player-cut.h"
57 #include "timed-effect/player-stun.h"
58 #include "util/bit-flags-calculator.h"
59 #include "view/display-messages.h"
60
61 /*!
62  * @brief コンストラクタ
63  * @param player_ptr プレイヤーへの参照ポインタ
64  * @param m_idx 打撃を行うモンスターのID
65  */
66 MonsterAttackPlayer::MonsterAttackPlayer(PlayerType *player_ptr, short m_idx)
67     : m_idx(m_idx)
68     , m_ptr(&player_ptr->current_floor_ptr->m_list[m_idx])
69     , method(RaceBlowMethodType::NONE)
70     , effect(RaceBlowEffectType::NONE)
71     , do_silly_attack(one_in_(2) && player_ptr->hallucinated)
72     , player_ptr(player_ptr)
73 {
74 }
75
76 /*!
77  * @brief モンスターからプレイヤーへの打撃処理 / Attack the player via physical attacks.
78  */
79 void MonsterAttackPlayer::make_attack_normal()
80 {
81     if (!this->check_no_blow()) {
82         return;
83     }
84
85     auto *r_ptr = &r_info[this->m_ptr->r_idx];
86     this->rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
87     monster_desc(this->player_ptr, this->m_name, this->m_ptr, 0);
88     monster_desc(this->player_ptr, this->ddesc, this->m_ptr, MD_WRONGDOER_NAME);
89     if (PlayerClass(this->player_ptr).samurai_stance_is(SamuraiStanceType::IAI)) {
90         msg_format(_("相手が襲いかかる前に素早く武器を振るった。", "You took sen, drew and cut in one motion before %s moved."), this->m_name);
91         if (do_cmd_attack(this->player_ptr, this->m_ptr->fy, this->m_ptr->fx, HISSATSU_IAI)) {
92             return;
93         }
94     }
95
96     auto can_activate_kawarimi = randint0(55) < (this->player_ptr->lev * 3 / 5 + 20);
97     if (can_activate_kawarimi && kawarimi(this->player_ptr, true)) {
98         return;
99     }
100
101     this->blinked = false;
102     if (this->process_monster_blows()) {
103         return;
104     }
105
106     this->postprocess_monster_blows();
107 }
108
109 /*!
110  * @brief 能力値の実値を求める
111  * @param raw PlayerTypeに格納されている生値
112  * @return 実値
113  * @details AD&Dの記法に則り、19以上の値を取らなくしているので、格納方法が面倒
114  */
115 int MonsterAttackPlayer::stat_value(const int raw)
116 {
117     if (raw <= 18) {
118         return raw;
119     }
120
121     return (raw - 18) / 10 + 18;
122 }
123
124 bool MonsterAttackPlayer::check_no_blow()
125 {
126     auto *r_ptr = &r_info[this->m_ptr->r_idx];
127     if (r_ptr->behavior_flags.has(MonsterBehaviorType::NEVER_BLOW)) {
128         return false;
129     }
130
131     if (d_info[this->player_ptr->dungeon_idx].flags.has(DungeonFeatureType::NO_MELEE)) {
132         return false;
133     }
134
135     return is_hostile(this->m_ptr);
136 }
137
138 /*!
139  * @brief モンスターからプレイヤーへの打撃処理本体
140  * @return 打撃に反応してプレイヤーがその場から離脱したかどうか
141  */
142 bool MonsterAttackPlayer::process_monster_blows()
143 {
144     auto *r_ptr = &r_info[this->m_ptr->r_idx];
145     for (auto ap_cnt = 0; ap_cnt < MAX_NUM_BLOWS; ap_cnt++) {
146         this->obvious = false;
147         this->damage = 0;
148         this->act = nullptr;
149         this->effect = r_ptr->blow[ap_cnt].effect;
150         this->method = r_ptr->blow[ap_cnt].method;
151         this->d_dice = r_ptr->blow[ap_cnt].d_dice;
152         this->d_side = r_ptr->blow[ap_cnt].d_side;
153
154         if (!this->check_monster_continuous_attack()) {
155             break;
156         }
157
158         // effect が RaceBlowEffectType::NONE (無効値)になることはあり得ないはずだが、万一そう
159         // なっていたら単に攻撃を打ち切る。
160         // r_info.txt の "B:" トークンに effect 以降を書き忘れた場合が該当する。
161         if (this->effect == RaceBlowEffectType::NONE) {
162             plog("unexpected: MonsterAttackPlayer::effect == RaceBlowEffectType::NONE");
163             break;
164         }
165
166         if (this->method == RaceBlowMethodType::SHOOT) {
167             continue;
168         }
169
170         // フレーバーの打撃は必中扱い。それ以外は通常の命中判定を行う。
171         this->ac = this->player_ptr->ac + this->player_ptr->to_a;
172         bool hit;
173         if (this->effect == RaceBlowEffectType::FLAVOR) {
174             hit = true;
175         } else {
176             const int power = mbe_info[enum2i(this->effect)].power;
177             hit = check_hit_from_monster_to_player(this->player_ptr, power, this->rlev, monster_stunned_remaining(this->m_ptr));
178         }
179
180         if (hit) {
181             // 命中した。命中処理と思い出処理を行う。
182             // 打撃そのものは対邪悪結界で撃退した可能性がある。
183             const bool protect = !this->process_monster_attack_hit();
184             this->increase_blow_type_seen(ap_cnt);
185
186             // 撃退成功時はそのまま次の打撃へ移行。
187             if (protect)
188                 continue;
189
190             // 撃退失敗時は落馬処理、変わり身のテレポート処理を行う。
191             check_fall_off_horse(this->player_ptr, this);
192
193             // 変わり身のテレポートが成功したら攻撃を打ち切り、プレイヤーが離脱した旨を返す。
194             if (kawarimi(this->player_ptr, false)) {
195                 return true;
196             }
197         } else {
198             // 命中しなかった。回避時の処理、思い出処理を行う。
199             this->process_monster_attack_evasion();
200             this->increase_blow_type_seen(ap_cnt);
201         }
202     }
203
204     // 通常はプレイヤーはその場にとどまる。
205     return false;
206 }
207
208 /*!
209  * @brief プレイヤー死亡等でモンスターからプレイヤーへの直接攻撃処理を途中で打ち切るかどうかを判定する
210  * @return 攻撃続行ならばTRUE、打ち切りになったらFALSE
211  */
212 bool MonsterAttackPlayer::check_monster_continuous_attack()
213 {
214     if (!monster_is_valid(this->m_ptr) || (this->method == RaceBlowMethodType::NONE)) {
215         return false;
216     }
217
218     auto *r_ptr = &r_info[this->m_ptr->r_idx];
219     if (is_pet(this->m_ptr) && (r_ptr->flags1 & RF1_UNIQUE) && (this->method == RaceBlowMethodType::EXPLODE)) {
220         this->method = RaceBlowMethodType::HIT;
221         this->d_dice /= 10;
222     }
223
224     auto is_neighbor = distance(this->player_ptr->y, this->player_ptr->x, this->m_ptr->fy, this->m_ptr->fx) <= 1;
225     return this->player_ptr->playing && !this->player_ptr->is_dead && is_neighbor && !this->player_ptr->leaving;
226 }
227
228 /*!
229  * @brief モンスターから直接攻撃を1回受けた時の処理
230  * @return 対邪悪結界により攻撃が当たらなかったらFALSE、それ以外はTRUE
231  * @param this->player_ptr プレイヤーへの参照ポインタ
232  * @param monap_ptr モンスターからプレイヤーへの直接攻撃構造体への参照ポインタ
233  * @details 最大4 回/モンスター/ターン、このルーチンを通る
234  */
235 bool MonsterAttackPlayer::process_monster_attack_hit()
236 {
237     disturb(this->player_ptr, true, true);
238     if (this->effect_protecion_from_evil()) {
239         return false;
240     }
241
242     this->do_cut = 0;
243     this->do_stun = 0;
244     describe_monster_attack_method(this);
245     this->describe_silly_attacks();
246     this->obvious = true;
247     this->damage = damroll(this->d_dice, this->d_side);
248     if (this->explode) {
249         this->damage = 0;
250     }
251
252     switch_monster_blow_to_player(this->player_ptr, this);
253     this->select_cut_stun();
254     this->calc_player_cut();
255     this->process_player_stun();
256     this->monster_explode();
257     process_aura_counterattack(this->player_ptr, this);
258     return true;
259 }
260
261 /*!
262  * @brief 対邪悪結界が効いている状態で邪悪なモンスターから直接攻撃を受けた時の処理
263  * @return briefに書いた条件+確率が満たされたらTRUE、それ以外はFALSE
264  */
265 bool MonsterAttackPlayer::effect_protecion_from_evil()
266 {
267     auto *r_ptr = &r_info[this->m_ptr->r_idx];
268     if ((this->player_ptr->protevil <= 0) || none_bits(r_ptr->flags3, RF3_EVIL) || (this->player_ptr->lev < this->rlev) || ((randint0(100) + this->player_ptr->lev) <= 50)) {
269         return false;
270     }
271
272     if (is_original_ap_and_seen(this->player_ptr, this->m_ptr)) {
273         r_ptr->r_flags3 |= RF3_EVIL;
274     }
275
276 #ifdef JP
277     this->abbreviate ? msg_format("撃退した。") : msg_format("%^sは撃退された。", this->m_name);
278     this->abbreviate = 1; /* 2回目以降は省略 */
279 #else
280     msg_format("%^s is repelled.", this->m_name);
281 #endif
282
283     return true;
284 }
285
286 void MonsterAttackPlayer::describe_silly_attacks()
287 {
288     if (this->act == nullptr) {
289         return;
290     }
291
292     if (this->do_silly_attack) {
293 #ifdef JP
294         this->abbreviate = -1;
295 #endif
296         this->act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
297     }
298
299 #ifdef JP
300     if (this->abbreviate == 0) {
301         msg_format("%^sに%s", this->m_name, this->act);
302     } else if (this->abbreviate == 1) {
303         msg_format("%s", this->act);
304     } else {
305         /* if (this->abbreviate == -1) */
306         msg_format("%^s%s", this->m_name, this->act);
307     }
308
309     this->abbreviate = 1; /*2回目以降は省略 */
310 #else
311     msg_format("%^s %s%s", this->m_name, this->act, this->do_silly_attack ? " you." : "");
312 #endif
313 }
314
315 /*!
316  * @brief 切り傷と朦朧が同時に発生した時、片方を無効にする
317  */
318 void MonsterAttackPlayer::select_cut_stun()
319 {
320     if ((this->do_cut == 0) || (this->do_stun == 0)) {
321         return;
322     }
323
324     if (randint0(100) < 50) {
325         this->do_cut = 0;
326     } else {
327         this->do_stun = 0;
328     }
329 }
330
331 void MonsterAttackPlayer::calc_player_cut()
332 {
333     if (this->do_cut == 0) {
334         return;
335     }
336
337     auto cut_plus = PlayerCut::get_accumulation(this->d_dice * this->d_side, this->damage);
338     if (cut_plus > 0) {
339         (void)BadStatusSetter(this->player_ptr).mod_cut(cut_plus);
340     }
341 }
342
343 /*!
344  * @brief 朦朧を蓄積させる
345  * @param this->player_ptr プレイヤーへの参照ポインタ
346  * @param monap_ptr モンスター打撃への参照ポインタ
347  * @details
348  * 痛恨の一撃ならば朦朧蓄積ランクを1上げる.
349  * 2%の確率で朦朧蓄積ランクを1上げる.
350  * 肉体のパラメータが合計80を超える水準に強化されていたら朦朧蓄積ランクを1下げる.
351  */
352 void MonsterAttackPlayer::process_player_stun()
353 {
354     if (this->do_stun == 0) {
355         return;
356     }
357
358     auto total = this->d_dice * this->d_side;
359     auto accumulation_rank = PlayerStun::get_accumulation_rank(total, this->damage);
360     if (accumulation_rank == 0) {
361         return;
362     }
363
364     if ((total < this->damage) && (accumulation_rank <= 6)) {
365         accumulation_rank++;
366     }
367
368     if (one_in_(50)) {
369         accumulation_rank++;
370     }
371
372     auto str = this->stat_value(this->player_ptr->stat_cur[A_STR]);
373     auto dex = this->stat_value(this->player_ptr->stat_cur[A_DEX]);
374     auto con = this->stat_value(this->player_ptr->stat_cur[A_CON]);
375     auto is_powerful_body = str + dex + con > 80;
376     if (is_powerful_body) {
377         accumulation_rank--;
378     }
379
380     auto stun_plus = PlayerStun::get_accumulation(accumulation_rank);
381     if (stun_plus > 0) {
382         (void)BadStatusSetter(this->player_ptr).mod_stun(stun_plus);
383     }
384 }
385
386 void MonsterAttackPlayer::monster_explode()
387 {
388     if (!this->explode) {
389         return;
390     }
391
392     sound(SOUND_EXPLODE);
393     MonsterDamageProcessor mdp(this->player_ptr, this->m_idx, this->m_ptr->hp + 1, &this->fear, AttributeType::NONE);
394     if (mdp.mon_take_hit(nullptr)) {
395         this->blinked = false;
396         this->alive = false;
397     }
398 }
399
400 /*!
401  * @brief 一部の打撃種別の場合のみ、避けた旨のメッセージ表示と盾技能スキル向上を行う
402  * @param this->player_ptr プレイヤーへの参照ポインタ
403  * @param monap_ptr モンスターからプレイヤーへの直接攻撃構造体への参照ポインタ
404  */
405 void MonsterAttackPlayer::process_monster_attack_evasion()
406 {
407     switch (this->method) {
408     case RaceBlowMethodType::HIT:
409     case RaceBlowMethodType::TOUCH:
410     case RaceBlowMethodType::PUNCH:
411     case RaceBlowMethodType::KICK:
412     case RaceBlowMethodType::CLAW:
413     case RaceBlowMethodType::BITE:
414     case RaceBlowMethodType::STING:
415     case RaceBlowMethodType::SLASH:
416     case RaceBlowMethodType::BUTT:
417     case RaceBlowMethodType::CRUSH:
418     case RaceBlowMethodType::ENGULF:
419     case RaceBlowMethodType::CHARGE:
420         this->describe_attack_evasion();
421         this->gain_armor_exp();
422         this->damage = 0;
423         return;
424     default:
425         return;
426     }
427 }
428
429 void MonsterAttackPlayer::describe_attack_evasion()
430 {
431     if (!this->m_ptr->ml) {
432         return;
433     }
434
435     disturb(this->player_ptr, true, true);
436 #ifdef JP
437     auto is_suiken = any_bits(this->player_ptr->special_attack, ATTACK_SUIKEN);
438     if (this->abbreviate) {
439         msg_format("%sかわした。", is_suiken ? "奇妙な動きで" : "");
440     } else {
441         msg_format("%s%^sの攻撃をかわした。", is_suiken ? "奇妙な動きで" : "", this->m_name);
442     }
443
444     this->abbreviate = 1; /* 2回目以降は省略 */
445 #else
446     msg_format("%^s misses you.", this->m_name);
447 #endif
448 }
449
450 void MonsterAttackPlayer::gain_armor_exp()
451 {
452     const auto o_ptr_mh = &this->player_ptr->inventory_list[INVEN_MAIN_HAND];
453     const auto o_ptr_sh = &this->player_ptr->inventory_list[INVEN_SUB_HAND];
454     if (!o_ptr_mh->is_armour() && !o_ptr_sh->is_armour()) {
455         return;
456     }
457
458     auto cur = this->player_ptr->skill_exp[PlayerSkillKindType::SHIELD];
459     auto max = s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::SHIELD];
460     if (cur >= max) {
461         return;
462     }
463
464     auto *r_ptr = &r_info[this->m_ptr->r_idx];
465     auto target_level = r_ptr->level;
466     short increment = 0;
467     if ((cur / 100) < target_level) {
468         auto addition = (cur / 100 + 15) < target_level ? (target_level - (cur / 100 + 15)) : 0;
469         increment += 1 + addition;
470     }
471
472     this->player_ptr->skill_exp[PlayerSkillKindType::SHIELD] = std::min<short>(max, cur + increment);
473     this->player_ptr->update |= (PU_BONUS);
474 }
475
476 /*!
477  * @brief モンスターの打撃情報を蓄積させる
478  * @param ap_cnt モンスターの打撃 N回目
479  * @details どの敵が何をしてきたか正しく認識できていなければ情報を蓄積しない.
480  * 非自明な類の打撃については、そのダメージが 0 ならば基本的に知識が増えない.
481  * 但し、既に一定以上の知識があれば常に知識が増える(何をされたのか察知できる).
482  */
483 void MonsterAttackPlayer::increase_blow_type_seen(const int ap_cnt)
484 {
485     if (!is_original_ap_and_seen(this->player_ptr, this->m_ptr) || this->do_silly_attack) {
486         return;
487     }
488
489     auto *r_ptr = &r_info[this->m_ptr->r_idx];
490     if (!this->obvious && (this->damage == 0) && (r_ptr->r_blows[ap_cnt] <= 10)) {
491         return;
492     }
493
494     if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR) {
495         r_ptr->r_blows[ap_cnt]++;
496     }
497 }
498
499 void MonsterAttackPlayer::postprocess_monster_blows()
500 {
501     SpellHex spell_hex(this->player_ptr, this);
502     spell_hex.store_vengeful_damage(this->get_damage);
503     spell_hex.eyes_on_eyes();
504     musou_counterattack(this->player_ptr, this);
505     spell_hex.thief_teleport();
506     auto *r_ptr = &r_info[this->m_ptr->r_idx];
507     if (this->player_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !this->player_ptr->current_floor_ptr->inside_arena) {
508         r_ptr->r_deaths++;
509     }
510
511     if (this->m_ptr->ml && this->fear && this->alive && !this->player_ptr->is_dead) {
512         sound(SOUND_FLEE);
513         msg_format(_("%^sは恐怖で逃げ出した!", "%^s flees in terror!"), this->m_name);
514     }
515
516     PlayerClass(this->player_ptr).break_samurai_stance({ SamuraiStanceType::IAI });
517 }