OSDN Git Service

[Refactor] #2204 HIT_POINTエイリアスをintに揃えた
[hengbandforosx/hengbandosx.git] / src / cmd-action / cmd-attack.cpp
1 /*!
2  * @brief 攻撃コマンド処理
3  * @date 2020/05/23
4  * @author Hourier
5  */
6
7 #include "cmd-action/cmd-attack.h"
8 #include "artifact/fixed-art-types.h"
9 #include "avatar/avatar.h"
10 #include "combat/attack-accuracy.h"
11 #include "combat/attack-criticality.h"
12 #include "core/asking-player.h"
13 #include "core/disturbance.h"
14 #include "core/player-update-types.h"
15 #include "core/stuff-handler.h"
16 #include "dungeon/dungeon-flag-types.h"
17 #include "dungeon/dungeon.h"
18 #include "effect/effect-characteristics.h"
19 #include "effect/effect-processor.h"
20 #include "game-option/cheat-types.h"
21 #include "inventory/inventory-slot-types.h"
22 #include "main/sound-definitions-table.h"
23 #include "main/sound-of-music.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-flags1.h"
26 #include "monster-race/race-flags2.h"
27 #include "monster-race/race-flags3.h"
28 #include "monster/monster-damage.h"
29 #include "monster/monster-describer.h"
30 #include "monster/monster-info.h"
31 #include "monster/monster-status-setter.h"
32 #include "monster/monster-status.h"
33 #include "mutation/mutation-flag-types.h"
34 #include "object/item-use-flags.h"
35 #include "player-attack/player-attack.h"
36 #include "player-base/player-class.h"
37 #include "player-info/equipment-info.h"
38 #include "player-info/samurai-data-type.h"
39 #include "player-status/player-energy.h"
40 #include "player-status/player-hand-types.h"
41 #include "player/attack-defense-types.h"
42 #include "player/player-damage.h"
43 #include "player/player-skill.h"
44 #include "player/player-status-flags.h"
45 #include "player/player-status.h"
46 #include "player/special-defense-types.h"
47 #include "status/action-setter.h"
48 #include "system/floor-type-definition.h"
49 #include "system/grid-type-definition.h"
50 #include "system/monster-race-definition.h"
51 #include "system/monster-type-definition.h"
52 #include "system/object-type-definition.h"
53 #include "system/player-type-definition.h"
54 #include "timed-effect/player-stun.h"
55 #include "timed-effect/timed-effects.h"
56 #include "util/bit-flags-calculator.h"
57 #include "view/display-messages.h"
58 #include "wizard/wizard-messages.h"
59
60 /*!
61  * @brief プレイヤーの変異要素による打撃処理
62  * @param player_ptr プレイヤーへの参照ポインタ
63  * @param m_idx 攻撃目標となったモンスターの参照ID
64  * @param attack 変異要素による攻撃要素の種類
65  * @param fear 攻撃を受けたモンスターが恐慌状態に陥ったかを返す参照ポインタ
66  * @param mdeath 攻撃を受けたモンスターが死亡したかを返す参照ポインタ
67  */
68 static void natural_attack(PlayerType *player_ptr, MONSTER_IDX m_idx, PlayerMutationType attack, bool *fear, bool *mdeath)
69 {
70     WEIGHT n_weight = 0;
71     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
72     auto *r_ptr = &r_info[m_ptr->r_idx];
73
74     int dice_num, dice_side;
75     concptr atk_desc;
76     switch (attack) {
77     case PlayerMutationType::SCOR_TAIL:
78         dice_num = 3;
79         dice_side = 7;
80         n_weight = 5;
81         atk_desc = _("尻尾", "tail");
82         break;
83     case PlayerMutationType::HORNS:
84         dice_num = 2;
85         dice_side = 6;
86         n_weight = 15;
87         atk_desc = _("角", "horns");
88         break;
89     case PlayerMutationType::BEAK:
90         dice_num = 2;
91         dice_side = 4;
92         n_weight = 5;
93         atk_desc = _("クチバシ", "beak");
94         break;
95     case PlayerMutationType::TRUNK:
96         dice_num = 1;
97         dice_side = 4;
98         n_weight = 35;
99         atk_desc = _("象の鼻", "trunk");
100         break;
101     case PlayerMutationType::TENTACLES:
102         dice_num = 2;
103         dice_side = 5;
104         n_weight = 5;
105         atk_desc = _("触手", "tentacles");
106         break;
107     default:
108         dice_num = dice_side = n_weight = 1;
109         atk_desc = _("未定義の部位", "undefined body part");
110     }
111
112     GAME_TEXT m_name[MAX_NLEN];
113     monster_desc(player_ptr, m_name, m_ptr, 0);
114
115     int bonus = player_ptr->to_h_m + (player_ptr->lev * 6 / 5);
116     int chance = (player_ptr->skill_thn + (bonus * BTH_PLUS_ADJ));
117
118     bool is_hit = (r_ptr->kind_flags.has_not(MonsterKindType::QUANTUM)) || !randint0(2);
119     is_hit &= test_hit_norm(player_ptr, chance, r_ptr->ac, m_ptr->ml);
120     if (!is_hit) {
121         sound(SOUND_MISS);
122         msg_format(_("ミス! %sにかわされた。", "You miss %s."), m_name);
123         return;
124     }
125
126     sound(SOUND_HIT);
127     msg_format(_("%sを%sで攻撃した。", "You hit %s with your %s."), m_name, atk_desc);
128
129     int k = damroll(dice_num, dice_side);
130     k = critical_norm(player_ptr, n_weight, bonus, k, (int16_t)bonus, HISSATSU_NONE);
131     k += player_ptr->to_d_m;
132     if (k < 0)
133         k = 0;
134
135     k = mon_damage_mod(player_ptr, m_ptr, k, false);
136     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), k, m_ptr->hp - k,
137         m_ptr->maxhp, m_ptr->max_maxhp);
138     if (k > 0)
139         anger_monster(player_ptr, m_ptr);
140
141     switch (attack) {
142     case PlayerMutationType::SCOR_TAIL:
143         project(player_ptr, 0, 0, m_ptr->fy, m_ptr->fx, k, AttributeType::POIS, PROJECT_KILL);
144         *mdeath = (m_ptr->r_idx == 0);
145         break;
146     case PlayerMutationType::HORNS:
147     case PlayerMutationType::BEAK:
148     case PlayerMutationType::TRUNK:
149     case PlayerMutationType::TENTACLES:
150     default: {
151         MonsterDamageProcessor mdp(player_ptr, m_idx, k, fear, AttributeType::ATTACK);
152         *mdeath = mdp.mon_take_hit(nullptr);
153         break;
154     }
155     }
156
157     touch_zap_player(m_ptr, player_ptr);
158 }
159
160 /*!
161  * @brief プレイヤーの打撃処理メインルーチン
162  * @param y 攻撃目標のY座標
163  * @param x 攻撃目標のX座標
164  * @param mode 発動中の剣術ID
165  * @return 実際に攻撃処理が行われた場合TRUEを返す。
166  * @details
167  * If no "weapon" is available, then "punch" the monster one time.
168  */
169 bool do_cmd_attack(PlayerType *player_ptr, POSITION y, POSITION x, combat_options mode)
170 {
171     auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
172     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
173     auto *r_ptr = &r_info[m_ptr->r_idx];
174     GAME_TEXT m_name[MAX_NLEN];
175
176     const std::initializer_list<PlayerMutationType> mutation_attack_methods = { PlayerMutationType::HORNS, PlayerMutationType::BEAK, PlayerMutationType::SCOR_TAIL, PlayerMutationType::TRUNK, PlayerMutationType::TENTACLES };
177
178     disturb(player_ptr, false, true);
179
180     PlayerEnergy(player_ptr).set_player_turn_energy(100);
181
182     if (!can_attack_with_main_hand(player_ptr) && !can_attack_with_sub_hand(player_ptr) && player_ptr->muta.has_none_of(mutation_attack_methods)) {
183         msg_format(_("%s攻撃できない。", "You cannot attack."), (empty_hands(player_ptr, false) == EMPTY_HAND_NONE) ? _("両手がふさがって", "") : "");
184         return false;
185     }
186
187     monster_desc(player_ptr, m_name, m_ptr, 0);
188
189     if (m_ptr->ml) {
190         if (!player_ptr->hallucinated)
191             monster_race_track(player_ptr, m_ptr->ap_r_idx);
192
193         health_track(player_ptr, g_ptr->m_idx);
194     }
195
196     auto effects = player_ptr->effects();
197     auto is_stunned = effects->stun()->is_stunned();
198     if (any_bits(r_ptr->flags1, RF1_FEMALE) && !(is_stunned || player_ptr->confused || player_ptr->hallucinated || !m_ptr->ml)) {
199         if ((player_ptr->inventory_list[INVEN_MAIN_HAND].name1 == ART_ZANTETSU) || (player_ptr->inventory_list[INVEN_SUB_HAND].name1 == ART_ZANTETSU)) {
200             msg_print(_("拙者、おなごは斬れぬ!", "I can not attack women!"));
201             return false;
202         }
203     }
204
205     if (d_info[player_ptr->dungeon_idx].flags.has(DungeonFeatureType::NO_MELEE)) {
206         msg_print(_("なぜか攻撃することができない。", "Something prevents you from attacking."));
207         return false;
208     }
209
210     bool stormbringer = false;
211     if (!is_hostile(m_ptr) && !(is_stunned || player_ptr->confused || player_ptr->hallucinated || is_shero(player_ptr) || !m_ptr->ml)) {
212         if (player_ptr->inventory_list[INVEN_MAIN_HAND].name1 == ART_STORMBRINGER)
213             stormbringer = true;
214         if (player_ptr->inventory_list[INVEN_SUB_HAND].name1 == ART_STORMBRINGER)
215             stormbringer = true;
216         if (stormbringer) {
217             msg_format(_("黒い刃は強欲に%sを攻撃した!", "Your black blade greedily attacks %s!"), m_name);
218             chg_virtue(player_ptr, V_INDIVIDUALISM, 1);
219             chg_virtue(player_ptr, V_HONOUR, -1);
220             chg_virtue(player_ptr, V_JUSTICE, -1);
221             chg_virtue(player_ptr, V_COMPASSION, -1);
222         } else if (!PlayerClass(player_ptr).equals(PlayerClassType::BERSERKER)) {
223             if (get_check(_("本当に攻撃しますか?", "Really hit it? "))) {
224                 chg_virtue(player_ptr, V_INDIVIDUALISM, 1);
225                 chg_virtue(player_ptr, V_HONOUR, -1);
226                 chg_virtue(player_ptr, V_JUSTICE, -1);
227                 chg_virtue(player_ptr, V_COMPASSION, -1);
228             } else {
229                 msg_format(_("%sを攻撃するのを止めた。", "You stop to avoid hitting %s."), m_name);
230                 return false;
231             }
232         }
233     }
234
235     if (player_ptr->afraid) {
236         if (m_ptr->ml)
237             msg_format(_("恐くて%sを攻撃できない!", "You are too afraid to attack %s!"), m_name);
238         else
239             msg_format(_("そっちには何か恐いものがいる!", "There is something scary in your way!"));
240
241         (void)set_monster_csleep(player_ptr, g_ptr->m_idx, 0);
242         return false;
243     }
244
245     if (monster_csleep_remaining(m_ptr)) {
246         if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5))
247             chg_virtue(player_ptr, V_COMPASSION, -1);
248         if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5))
249             chg_virtue(player_ptr, V_HONOUR, -1);
250     }
251
252     if (can_attack_with_main_hand(player_ptr) && can_attack_with_sub_hand(player_ptr)) {
253         if (((player_ptr->skill_exp[PlayerSkillKindType::TWO_WEAPON] - 1000) / 200) < r_ptr->level) {
254             PlayerSkill(player_ptr).gain_two_weapon_skill_exp();
255         }
256     }
257
258     if (player_ptr->riding) {
259         PlayerSkill(player_ptr).gain_riding_skill_exp_on_melee_attack(r_ptr);
260     }
261
262     player_ptr->riding_t_m_idx = g_ptr->m_idx;
263     bool fear = false;
264     bool mdeath = false;
265     if (can_attack_with_main_hand(player_ptr))
266         exe_player_attack_to_monster(player_ptr, y, x, &fear, &mdeath, 0, mode);
267     if (can_attack_with_sub_hand(player_ptr) && !mdeath)
268         exe_player_attack_to_monster(player_ptr, y, x, &fear, &mdeath, 1, mode);
269
270     if (!mdeath) {
271         for (auto m : mutation_attack_methods) {
272             if (player_ptr->muta.has(m) && !mdeath) {
273                 natural_attack(player_ptr, g_ptr->m_idx, m, &fear, &mdeath);
274             }
275         }
276     }
277
278     if (fear && m_ptr->ml && !mdeath) {
279         sound(SOUND_FLEE);
280         msg_format(_("%^sは恐怖して逃げ出した!", "%^s flees in terror!"), m_name);
281     }
282
283     if (PlayerClass(player_ptr).samurai_stance_is(SamuraiStanceType::IAI) && ((mode != HISSATSU_IAI) || mdeath)) {
284         set_action(player_ptr, ACTION_NONE);
285     }
286
287     return mdeath;
288 }