OSDN Git Service

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