OSDN Git Service

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