OSDN Git Service

[Refactor] #37353 Separated hook-armor.c/h from object-hook.c/h
[hengbandforosx/hengbandosx.git] / src / monster-attack / monster-attack-player.c
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 "dungeon/dungeon.h"
16 #include "effect/effect-characteristics.h"
17 #include "main/sound-definitions-table.h"
18 #include "main/sound-of-music.h"
19 #include "mind/mind-ninja.h"
20 #include "mind/mind-samurai.h"
21 #include "monster-attack/monster-attack-describer.h"
22 #include "monster-attack/monster-attack-effect.h"
23 #include "monster-attack/monster-attack-switcher.h"
24 #include "monster-attack/monster-attack-util.h"
25 #include "monster-race/monster-race.h"
26 #include "monster-race/race-flags1.h"
27 #include "monster-race/race-flags3.h"
28 #include "monster/monster-description-types.h"
29 #include "monster/monster-describer.h"
30 #include "monster/monster-status.h"
31 #include "monster/monster-info.h"
32 #include "monster/smart-learn-types.h"
33 #include "object-hook/hook-armor.h"
34 #include "object/object-hook.h"
35 #include "pet/pet-fall-off.h"
36 #include "player/bad-status-setter.h"
37 #include "player/player-damage.h"
38 #include "player/player-effects.h"
39 #include "player/player-move.h"
40 #include "player/player-skill.h"
41 #include "player/special-defense-types.h"
42 #include "realm/realm-hex-numbers.h"
43 #include "spell-kind/spells-teleport.h"
44 #include "spell-realm/spells-hex.h"
45 #include "spell/process-effect.h"
46 #include "spell/spell-types.h"
47 #include "view/display-messages.h"
48
49 static bool check_no_blow(player_type *target_ptr, monap_type *monap_ptr)
50 {
51     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
52     if (r_ptr->flags1 & (RF1_NEVER_BLOW))
53         return FALSE;
54
55     if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
56         return FALSE;
57
58     if (!is_hostile(monap_ptr->m_ptr))
59         return FALSE;
60
61     return TRUE;
62 }
63
64 /*!
65  * @brief プレーヤー死亡等でモンスターからプレーヤーへの直接攻撃処理を途中で打ち切るかどうかを判定する
66  * @param target_ptr プレーヤーへの参照ポインタ
67  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
68  * @return 攻撃続行ならばTRUE、打ち切りになったらFALSE
69  */
70 static bool check_monster_attack_terminated(player_type *target_ptr, monap_type *monap_ptr)
71 {
72     if (!monster_is_valid(monap_ptr->m_ptr))
73         return FALSE;
74
75     if (monap_ptr->method == RBM_NONE)
76         return FALSE;
77
78     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
79     if (is_pet(monap_ptr->m_ptr) && (r_ptr->flags1 & RF1_UNIQUE) && (monap_ptr->method == RBM_EXPLODE)) {
80         monap_ptr->method = RBM_HIT;
81         monap_ptr->d_dice /= 10;
82     }
83
84     if (!target_ptr->playing || target_ptr->is_dead || (distance(target_ptr->y, target_ptr->x, monap_ptr->m_ptr->fy, monap_ptr->m_ptr->fx) > 1)
85         || target_ptr->leaving)
86         return FALSE;
87
88     return TRUE;
89 }
90
91 /*!
92  * @brief 対邪悪結界が効いている状態で邪悪なモンスターから直接攻撃を受けた時の処理
93  * @param target_ptr プレーヤーへの参照ポインタ
94  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
95  * @return briefに書いた条件+確率が満たされたらTRUE、それ以外はFALSE
96  */
97 static bool effect_protecion_from_evil(player_type *target_ptr, monap_type *monap_ptr)
98 {
99     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
100     if ((target_ptr->protevil <= 0) || ((r_ptr->flags3 & RF3_EVIL) == 0) || (target_ptr->lev < monap_ptr->rlev) || ((randint0(100) + target_ptr->lev) <= 50))
101         return FALSE;
102
103     if (is_original_ap_and_seen(target_ptr, monap_ptr->m_ptr))
104         r_ptr->r_flags3 |= RF3_EVIL;
105
106 #ifdef JP
107     if (monap_ptr->abbreviate)
108         msg_format("撃退した。");
109     else
110         msg_format("%^sは撃退された。", monap_ptr->m_name);
111
112     monap_ptr->abbreviate = 1; /* 2回目以降は省略 */
113 #else
114     msg_format("%^s is repelled.", monap_ptr->m_name);
115 #endif
116
117     return TRUE;
118 }
119
120 static void describe_silly_attacks(monap_type *monap_ptr)
121 {
122     if (monap_ptr->act == NULL)
123         return;
124
125     if (monap_ptr->do_silly_attack) {
126 #ifdef JP
127         monap_ptr->abbreviate = -1;
128 #endif
129         monap_ptr->act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
130     }
131
132 #ifdef JP
133     if (monap_ptr->abbreviate == 0)
134         msg_format("%^sに%s", monap_ptr->m_name, monap_ptr->act);
135     else if (monap_ptr->abbreviate == 1)
136         msg_format("%s", monap_ptr->act);
137     else /* if (monap_ptr->abbreviate == -1) */
138         msg_format("%^s%s", monap_ptr->m_name, monap_ptr->act);
139
140     monap_ptr->abbreviate = 1; /*2回目以降は省略 */
141 #else
142     msg_format("%^s %s%s", monap_ptr->m_name, monap_ptr->act, monap_ptr->do_silly_attack ? " you." : "");
143 #endif
144 }
145
146 /*!
147  * @brief 切り傷と朦朧が同時に発生した時、片方を無効にする
148  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
149  * @return なし
150  */
151 static void select_cut_stun(monap_type *monap_ptr)
152 {
153     if ((monap_ptr->do_cut == 0) || (monap_ptr->do_stun == 0))
154         return;
155
156     if (randint0(100) < 50)
157         monap_ptr->do_cut = 0;
158     else
159         monap_ptr->do_stun = 0;
160 }
161
162 static void calc_player_cut(player_type *target_ptr, monap_type *monap_ptr)
163 {
164     if (monap_ptr->do_cut == 0)
165         return;
166
167     int cut_plus = 0;
168     int criticality = calc_monster_critical(monap_ptr->d_dice, monap_ptr->d_side, monap_ptr->damage);
169     switch (criticality) {
170     case 0:
171         cut_plus = 0;
172         break;
173     case 1:
174         cut_plus = randint1(5);
175         break;
176     case 2:
177         cut_plus = randint1(5) + 5;
178         break;
179     case 3:
180         cut_plus = randint1(20) + 20;
181         break;
182     case 4:
183         cut_plus = randint1(50) + 50;
184         break;
185     case 5:
186         cut_plus = randint1(100) + 100;
187         break;
188     case 6:
189         cut_plus = 300;
190         break;
191     default:
192         cut_plus = 500;
193         break;
194     }
195
196     if (cut_plus > 0)
197         (void)set_cut(target_ptr, target_ptr->cut + cut_plus);
198 }
199
200 static void calc_player_stun(player_type *target_ptr, monap_type *monap_ptr)
201 {
202     if (monap_ptr->do_stun == 0)
203         return;
204
205     int stun_plus = 0;
206     int criticality = calc_monster_critical(monap_ptr->d_dice, monap_ptr->d_side, monap_ptr->damage);
207     switch (criticality) {
208     case 0:
209         stun_plus = 0;
210         break;
211     case 1:
212         stun_plus = randint1(5);
213         break;
214     case 2:
215         stun_plus = randint1(5) + 10;
216         break;
217     case 3:
218         stun_plus = randint1(10) + 20;
219         break;
220     case 4:
221         stun_plus = randint1(15) + 30;
222         break;
223     case 5:
224         stun_plus = randint1(20) + 40;
225         break;
226     case 6:
227         stun_plus = 80;
228         break;
229     default:
230         stun_plus = 150;
231         break;
232     }
233
234     if (stun_plus > 0)
235         (void)set_stun(target_ptr, target_ptr->stun + stun_plus);
236 }
237
238 static void monster_explode(player_type *target_ptr, monap_type *monap_ptr)
239 {
240     if (!monap_ptr->explode)
241         return;
242
243     sound(SOUND_EXPLODE);
244     if (mon_take_hit(target_ptr, monap_ptr->m_idx, monap_ptr->m_ptr->hp + 1, &monap_ptr->fear, NULL)) {
245         monap_ptr->blinked = FALSE;
246         monap_ptr->alive = FALSE;
247     }
248 }
249
250 static void describe_attack_evasion(player_type *target_ptr, monap_type *monap_ptr)
251 {
252     if (!monap_ptr->m_ptr->ml)
253         return;
254
255     disturb(target_ptr, TRUE, TRUE);
256 #ifdef JP
257     if (monap_ptr->abbreviate)
258         msg_format("%sかわした。", (target_ptr->special_attack & ATTACK_SUIKEN) ? "奇妙な動きで" : "");
259     else
260         msg_format("%s%^sの攻撃をかわした。", (target_ptr->special_attack & ATTACK_SUIKEN) ? "奇妙な動きで" : "", monap_ptr->m_name);
261
262     monap_ptr->abbreviate = 1; /* 2回目以降は省略 */
263 #else
264     msg_format("%^s misses you.", monap_ptr->m_name);
265 #endif
266 }
267
268 static void gain_armor_exp(player_type *target_ptr, monap_type *monap_ptr)
269 {
270     if (!object_is_armour(target_ptr, &target_ptr->inventory_list[INVEN_RARM]) && !object_is_armour(target_ptr, &target_ptr->inventory_list[INVEN_LARM]))
271         return;
272
273     int cur = target_ptr->skill_exp[GINOU_SHIELD];
274     int max = s_info[target_ptr->pclass].s_max[GINOU_SHIELD];
275     if (cur >= max)
276         return;
277
278     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
279     DEPTH targetlevel = r_ptr->level;
280     int inc = 0;
281     if ((cur / 100) < targetlevel) {
282         if ((cur / 100 + 15) < targetlevel)
283             inc += 1 + (targetlevel - (cur / 100 + 15));
284         else
285             inc += 1;
286     }
287
288     target_ptr->skill_exp[GINOU_SHIELD] = MIN(max, cur + inc);
289     target_ptr->update |= (PU_BONUS);
290 }
291
292 /*!
293  * @brief モンスターから直接攻撃を1回受けた時の処理
294  * @return 対邪悪結界により攻撃が当たらなかったらFALSE、それ以外はTRUE
295  * @param target_ptr プレーヤーへの参照ポインタ
296  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
297  * @details 最大4 回/モンスター/ターン、このルーチンを通る
298  */
299 static bool process_monster_attack_hit(player_type *target_ptr, monap_type *monap_ptr)
300 {
301     disturb(target_ptr, TRUE, TRUE);
302     if (effect_protecion_from_evil(target_ptr, monap_ptr))
303         return FALSE;
304
305     monap_ptr->do_cut = 0;
306     monap_ptr->do_stun = 0;
307     describe_monster_attack_method(monap_ptr);
308     describe_silly_attacks(monap_ptr);
309     monap_ptr->obvious = TRUE;
310     monap_ptr->damage = damroll(monap_ptr->d_dice, monap_ptr->d_side);
311     if (monap_ptr->explode)
312         monap_ptr->damage = 0;
313
314     switch_monster_blow_to_player(target_ptr, monap_ptr);
315     select_cut_stun(monap_ptr);
316     calc_player_cut(target_ptr, monap_ptr);
317     calc_player_stun(target_ptr, monap_ptr);
318     monster_explode(target_ptr, monap_ptr);
319     process_aura_counterattack(target_ptr, monap_ptr);
320     return TRUE;
321 }
322
323 /*!
324  * @brief 一部の打撃種別の場合のみ、避けた旨のメッセージ表示と盾技能スキル向上を行う
325  * @param target_ptr プレーヤーへの参照ポインタ
326  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
327  * @return なし
328  */
329 static void process_monster_attack_evasion(player_type *target_ptr, monap_type *monap_ptr)
330 {
331     switch (monap_ptr->method) {
332     case RBM_HIT:
333     case RBM_TOUCH:
334     case RBM_PUNCH:
335     case RBM_KICK:
336     case RBM_CLAW:
337     case RBM_BITE:
338     case RBM_STING:
339     case RBM_SLASH:
340     case RBM_BUTT:
341     case RBM_CRUSH:
342     case RBM_ENGULF:
343     case RBM_CHARGE:
344         describe_attack_evasion(target_ptr, monap_ptr);
345         gain_armor_exp(target_ptr, monap_ptr);
346         monap_ptr->damage = 0;
347         return;
348     default:
349         return;
350     }
351 }
352
353 static void increase_blow_type_seen(player_type *target_ptr, monap_type *monap_ptr)
354 {
355     if (!is_original_ap_and_seen(target_ptr, monap_ptr->m_ptr) || monap_ptr->do_silly_attack)
356         return;
357
358     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
359     if (!monap_ptr->obvious && (monap_ptr->damage == 0) && (r_ptr->r_blows[monap_ptr->ap_cnt] <= 10))
360         return;
361
362     if (r_ptr->r_blows[monap_ptr->ap_cnt] < MAX_UCHAR)
363         r_ptr->r_blows[monap_ptr->ap_cnt]++;
364 }
365
366 static bool process_monster_blows(player_type *target_ptr, monap_type *monap_ptr)
367 {
368     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
369     for (monap_ptr->ap_cnt = 0; monap_ptr->ap_cnt < 4; monap_ptr->ap_cnt++) {
370         monap_ptr->obvious = FALSE;
371         HIT_POINT power = 0;
372         monap_ptr->damage = 0;
373         monap_ptr->act = NULL;
374         monap_ptr->effect = r_ptr->blow[monap_ptr->ap_cnt].effect;
375         monap_ptr->method = r_ptr->blow[monap_ptr->ap_cnt].method;
376         monap_ptr->d_dice = r_ptr->blow[monap_ptr->ap_cnt].d_dice;
377         monap_ptr->d_side = r_ptr->blow[monap_ptr->ap_cnt].d_side;
378
379         if (!check_monster_attack_terminated(target_ptr, monap_ptr))
380             break;
381
382         if (monap_ptr->method == RBM_SHOOT)
383             continue;
384
385         power = mbe_info[monap_ptr->effect].power;
386         monap_ptr->ac = target_ptr->ac + target_ptr->to_a;
387         if ((monap_ptr->effect == RBE_NONE)
388             || check_hit_from_monster_to_player(target_ptr, power, monap_ptr->rlev, monster_stunned_remaining(monap_ptr->m_ptr)))
389             if (!process_monster_attack_hit(target_ptr, monap_ptr))
390                 continue;
391             else
392                 process_monster_attack_evasion(target_ptr, monap_ptr);
393
394         increase_blow_type_seen(target_ptr, monap_ptr);
395         check_fall_off_horse(target_ptr, monap_ptr);
396         if (target_ptr->special_defense & NINJA_KAWARIMI) {
397             if (kawarimi(target_ptr, FALSE))
398                 return TRUE;
399         }
400     }
401
402     return FALSE;
403 }
404
405 /*!
406  * @brief 呪術「目には目を」の効果処理
407  * @param target_ptr プレーヤーへの参照ポインタ
408  * @param monap_ptr モンスターからプレーヤーへの直接攻撃構造体への参照ポインタ
409  * @return なし
410  */
411 static void eyes_on_eyes(player_type *target_ptr, monap_type *monap_ptr)
412 {
413     if (((target_ptr->tim_eyeeye == 0) && !hex_spelling(target_ptr, HEX_EYE_FOR_EYE)) || (monap_ptr->get_damage == 0) || target_ptr->is_dead)
414         return;
415
416 #ifdef JP
417     msg_format("攻撃が%s自身を傷つけた!", monap_ptr->m_name);
418 #else
419     GAME_TEXT m_name_self[80];
420     monster_desc(target_ptr, m_name_self, monap_ptr->m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE | MD_OBJECTIVE);
421     msg_format("The attack of %s has wounded %s!", monap_ptr->m_name, m_name_self);
422 #endif
423     project(target_ptr, 0, 0, monap_ptr->m_ptr->fy, monap_ptr->m_ptr->fx, monap_ptr->get_damage, GF_MISSILE, PROJECT_KILL, -1);
424     if (target_ptr->tim_eyeeye)
425         set_tim_eyeeye(target_ptr, target_ptr->tim_eyeeye - 5, TRUE);
426 }
427
428 static void thief_teleport(player_type *target_ptr, monap_type *monap_ptr)
429 {
430     if (!monap_ptr->blinked || !monap_ptr->alive || target_ptr->is_dead)
431         return;
432
433     if (teleport_barrier(target_ptr, monap_ptr->m_idx)) {
434         msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But a magic barrier obstructs it."));
435     } else {
436         msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
437         teleport_away(target_ptr, monap_ptr->m_idx, MAX_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
438     }
439 }
440
441 static void postprocess_monster_blows(player_type *target_ptr, monap_type *monap_ptr)
442 {
443     revenge_store(target_ptr, monap_ptr->get_damage);
444     eyes_on_eyes(target_ptr, monap_ptr);
445     musou_counterattack(target_ptr, monap_ptr);
446     thief_teleport(target_ptr, monap_ptr);
447     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
448     if (target_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !target_ptr->current_floor_ptr->inside_arena)
449         r_ptr->r_deaths++;
450
451     if (monap_ptr->m_ptr->ml && monap_ptr->fear && monap_ptr->alive && !target_ptr->is_dead) {
452         sound(SOUND_FLEE);
453         msg_format(_("%^sは恐怖で逃げ出した!", "%^s flees in terror!"), monap_ptr->m_name);
454     }
455
456     if (target_ptr->special_defense & KATA_IAI)
457         set_action(target_ptr, ACTION_NONE);
458 }
459
460 /*!
461  * @brief モンスターからプレイヤーへの打撃処理 / Attack the player via physical attacks.
462  * @param m_idx 打撃を行うモンスターのID
463  * @return 実際に攻撃処理を行った場合TRUEを返す
464  */
465 bool make_attack_normal(player_type *target_ptr, MONSTER_IDX m_idx)
466 {
467     monap_type tmp_monap;
468     monap_type *monap_ptr = initialize_monap_type(target_ptr, &tmp_monap, m_idx);
469     check_no_blow(target_ptr, monap_ptr);
470
471     monster_race *r_ptr = &r_info[monap_ptr->m_ptr->r_idx];
472     monap_ptr->rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
473     monster_desc(target_ptr, monap_ptr->m_name, monap_ptr->m_ptr, 0);
474     monster_desc(target_ptr, monap_ptr->ddesc, monap_ptr->m_ptr, MD_WRONGDOER_NAME);
475     if (target_ptr->special_defense & KATA_IAI) {
476         msg_format(_("相手が襲いかかる前に素早く武器を振るった。", "You took sen, drew and cut in one motion before %s moved."), monap_ptr->m_name);
477         if (do_cmd_attack(target_ptr, monap_ptr->m_ptr->fy, monap_ptr->m_ptr->fx, HISSATSU_IAI))
478             return TRUE;
479     }
480
481     if ((target_ptr->special_defense & NINJA_KAWARIMI) && (randint0(55) < (target_ptr->lev * 3 / 5 + 20))) {
482         if (kawarimi(target_ptr, TRUE))
483             return TRUE;
484     }
485
486     monap_ptr->blinked = FALSE;
487     if (process_monster_blows(target_ptr, monap_ptr))
488         return TRUE;
489
490     postprocess_monster_blows(target_ptr, monap_ptr);
491     return TRUE;
492 }