OSDN Git Service

Merge pull request #3195 from Hourier/Change-Avatar-ConcptrArray-StringVector
[hengbandforosx/hengbandosx.git] / src / combat / shoot.cpp
1 #include "combat/shoot.h"
2 #include "artifact/fixed-art-types.h"
3 #include "avatar/avatar.h"
4 #include "combat/attack-criticality.h"
5 #include "core/player-redraw-types.h"
6 #include "core/player-update-types.h"
7 #include "core/stuff-handler.h"
8 #include "effect/attribute-types.h"
9 #include "effect/effect-characteristics.h"
10 #include "effect/effect-processor.h"
11 #include "effect/spells-effect-util.h"
12 #include "flavor/flavor-describer.h"
13 #include "flavor/object-flavor-types.h"
14 #include "floor/cave.h"
15 #include "floor/floor-object.h"
16 #include "floor/geometry.h"
17 #include "game-option/cheat-types.h"
18 #include "game-option/special-options.h"
19 #include "grid/feature-flag-types.h"
20 #include "grid/feature.h"
21 #include "grid/grid.h"
22 #include "inventory/inventory-object.h"
23 #include "inventory/inventory-slot-types.h"
24 #include "io/cursor.h"
25 #include "io/screen-util.h"
26 #include "main/sound-definitions-table.h"
27 #include "main/sound-of-music.h"
28 #include "mind/mind-sniper.h"
29 #include "mind/snipe-types.h"
30 #include "monster-floor/monster-death.h"
31 #include "monster-floor/monster-move.h"
32 #include "monster-race/monster-race.h"
33 #include "monster-race/race-flags-resistance.h"
34 #include "monster-race/race-flags1.h"
35 #include "monster-race/race-flags2.h"
36 #include "monster-race/race-flags3.h"
37 #include "monster-race/race-flags7.h"
38 #include "monster-race/race-indice-types.h"
39 #include "monster-race/race-resistance-mask.h"
40 #include "monster/monster-damage.h"
41 #include "monster/monster-describer.h"
42 #include "monster/monster-info.h"
43 #include "monster/monster-pain-describer.h"
44 #include "monster/monster-status-setter.h"
45 #include "monster/monster-status.h"
46 #include "monster/monster-update.h"
47 #include "object/object-broken.h"
48 #include "object/object-flags.h"
49 #include "object/object-info.h"
50 #include "object/object-mark-types.h"
51 #include "player-base/player-class.h"
52 #include "player-info/class-info.h"
53 #include "player-info/sniper-data-type.h"
54 #include "player-status/player-energy.h"
55 #include "player/player-personality-types.h"
56 #include "player/player-skill.h"
57 #include "player/player-status-table.h"
58 #include "sv-definition/sv-bow-types.h"
59 #include "system/artifact-type-definition.h"
60 #include "system/baseitem-info.h"
61 #include "system/floor-type-definition.h"
62 #include "system/grid-type-definition.h"
63 #include "system/item-entity.h"
64 #include "system/monster-entity.h"
65 #include "system/monster-race-info.h"
66 #include "system/player-type-definition.h"
67 #include "target/projection-path-calculator.h"
68 #include "target/target-checker.h"
69 #include "target/target-getter.h"
70 #include "timed-effect/player-hallucination.h"
71 #include "timed-effect/timed-effects.h"
72 #include "util/bit-flags-calculator.h"
73 #include "view/display-messages.h"
74 #include "wizard/wizard-messages.h"
75 #include "world/world-object.h"
76
77 /*!
78  * @brief 矢弾の属性を定義する
79  * @param bow_ptr 弓のオブジェクト構造体参照ポインタ
80  * @param arrow_ptr 矢弾のオブジェクト構造体参照ポインタ
81  * @return スナイパーの射撃属性、弓矢の属性を考慮する。デフォルトはGF_PLAYER_SHOOT。
82  */
83 AttributeFlags shot_attribute(PlayerType *player_ptr, ItemEntity *bow_ptr, ItemEntity *arrow_ptr, SPELL_IDX snipe_type)
84 {
85     AttributeFlags attribute_flags{};
86     attribute_flags.set(AttributeType::PLAYER_SHOOT);
87
88     TrFlags flags{};
89     auto arrow_flags = object_flags(arrow_ptr);
90     auto bow_flags = object_flags(bow_ptr);
91
92     flags = bow_flags | arrow_flags;
93
94     static const struct snipe_convert_table_t {
95         SPELL_IDX snipe_type;
96         AttributeType attribute;
97     } snipe_convert_table[] = {
98         { SP_LITE, AttributeType::LITE },
99         { SP_FIRE, AttributeType::FIRE },
100         { SP_COLD, AttributeType::COLD },
101         { SP_ELEC, AttributeType::ELEC },
102         { SP_KILL_WALL, AttributeType::KILL_WALL },
103         { SP_EVILNESS, AttributeType::HELL_FIRE },
104         { SP_HOLYNESS, AttributeType::HOLY_FIRE },
105         { SP_FINAL, AttributeType::MANA },
106     };
107
108     static const struct brand_convert_table_t {
109         tr_type brand_type;
110         AttributeType attribute;
111     } brand_convert_table[] = {
112         { TR_BRAND_ACID, AttributeType::ACID },
113         { TR_BRAND_FIRE, AttributeType::FIRE },
114         { TR_BRAND_ELEC, AttributeType::ELEC },
115         { TR_BRAND_COLD, AttributeType::COLD },
116         { TR_BRAND_POIS, AttributeType::POIS },
117         { TR_SLAY_GOOD, AttributeType::HELL_FIRE },
118         { TR_KILL_GOOD, AttributeType::HELL_FIRE },
119         { TR_SLAY_EVIL, AttributeType::HOLY_FIRE },
120         { TR_KILL_EVIL, AttributeType::HOLY_FIRE },
121     };
122
123     for (size_t i = 0; i < sizeof(snipe_convert_table) / sizeof(snipe_convert_table[0]); ++i) {
124         const struct snipe_convert_table_t *p = &snipe_convert_table[i];
125
126         if (snipe_type == p->snipe_type) {
127             attribute_flags.set(p->attribute);
128         }
129     }
130
131     for (size_t i = 0; i < sizeof(brand_convert_table) / sizeof(brand_convert_table[0]); ++i) {
132         const struct brand_convert_table_t *p = &brand_convert_table[i];
133
134         if (flags.has(p->brand_type)) {
135             attribute_flags.set(p->attribute);
136         }
137     }
138
139     if ((flags.has(TR_FORCE_WEAPON)) && (player_ptr->csp > (player_ptr->msp / 30))) {
140         attribute_flags.set(AttributeType::MANA);
141     }
142
143     return attribute_flags;
144 }
145
146 /*!
147  * @brief 矢弾を射撃した際のスレイ倍率をかけた結果を返す /
148  * Determines the odds of an object breaking when thrown at a monster
149  * @param bow_ptr 弓のオブジェクト構造体参照ポインタ
150  * @param arrow_ptr 矢弾のオブジェクト構造体参照ポインタ
151  * @param tdam 計算途中のダメージ量
152  * @param monster_ptr 目標モンスターの構造体参照ポインタ
153  * @return スレイ倍率をかけたダメージ量
154  */
155 static MULTIPLY calc_shot_damage_with_slay(
156     PlayerType *player_ptr, ItemEntity *bow_ptr, ItemEntity *arrow_ptr, int tdam, MonsterEntity *monster_ptr, SPELL_IDX snipe_type)
157 {
158     MULTIPLY mult = 10;
159
160     MonsterRaceInfo *race_ptr = &monraces_info[monster_ptr->r_idx];
161
162     TrFlags flags{};
163     auto arrow_flags = object_flags(arrow_ptr);
164     auto bow_flags = object_flags(bow_ptr);
165
166     flags = bow_flags | arrow_flags;
167
168     /* Some "weapons" and "ammo" do extra damage */
169     switch (arrow_ptr->bi_key.tval()) {
170     case ItemKindType::SHOT:
171     case ItemKindType::ARROW:
172     case ItemKindType::BOLT: {
173         if ((flags.has(TR_SLAY_ANIMAL)) && race_ptr->kind_flags.has(MonsterKindType::ANIMAL)) {
174             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
175                 race_ptr->r_kind_flags.set(MonsterKindType::ANIMAL);
176             }
177             if (mult < 17) {
178                 mult = 17;
179             }
180         }
181
182         if ((flags.has(TR_KILL_ANIMAL)) && race_ptr->kind_flags.has(MonsterKindType::ANIMAL)) {
183             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
184                 race_ptr->r_kind_flags.set(MonsterKindType::ANIMAL);
185             }
186             if (mult < 27) {
187                 mult = 27;
188             }
189         }
190
191         if ((flags.has(TR_SLAY_EVIL)) && race_ptr->kind_flags.has(MonsterKindType::EVIL)) {
192             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
193                 race_ptr->r_kind_flags.set(MonsterKindType::EVIL);
194             }
195             if (mult < 15) {
196                 mult = 15;
197             }
198         }
199
200         if ((flags.has(TR_KILL_EVIL)) && race_ptr->kind_flags.has(MonsterKindType::EVIL)) {
201             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
202                 race_ptr->r_kind_flags.set(MonsterKindType::EVIL);
203             }
204             if (mult < 25) {
205                 mult = 25;
206             }
207         }
208
209         if ((flags.has(TR_SLAY_GOOD)) && race_ptr->kind_flags.has(MonsterKindType::GOOD)) {
210             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
211                 race_ptr->r_kind_flags.set(MonsterKindType::GOOD);
212             }
213             if (mult < 15) {
214                 mult = 15;
215             }
216         }
217
218         if ((flags.has(TR_KILL_GOOD)) && race_ptr->kind_flags.has(MonsterKindType::GOOD)) {
219             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
220                 race_ptr->r_kind_flags.set(MonsterKindType::GOOD);
221             }
222             if (mult < 25) {
223                 mult = 25;
224             }
225         }
226
227         if ((flags.has(TR_SLAY_HUMAN)) && race_ptr->kind_flags.has(MonsterKindType::HUMAN)) {
228             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
229                 race_ptr->r_kind_flags.set(MonsterKindType::HUMAN);
230             }
231             if (mult < 17) {
232                 mult = 17;
233             }
234         }
235
236         if ((flags.has(TR_KILL_HUMAN)) && race_ptr->kind_flags.has(MonsterKindType::HUMAN)) {
237             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
238                 race_ptr->r_kind_flags.set(MonsterKindType::HUMAN);
239             }
240             if (mult < 27) {
241                 mult = 27;
242             }
243         }
244
245         if ((flags.has(TR_SLAY_UNDEAD)) && race_ptr->kind_flags.has(MonsterKindType::UNDEAD)) {
246             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
247                 race_ptr->r_kind_flags.set(MonsterKindType::UNDEAD);
248             }
249             if (mult < 20) {
250                 mult = 20;
251             }
252         }
253
254         if ((flags.has(TR_KILL_UNDEAD)) && race_ptr->kind_flags.has(MonsterKindType::UNDEAD)) {
255             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
256                 race_ptr->r_kind_flags.set(MonsterKindType::UNDEAD);
257             }
258             if (mult < 30) {
259                 mult = 30;
260             }
261         }
262
263         if ((flags.has(TR_SLAY_DEMON)) && race_ptr->kind_flags.has(MonsterKindType::DEMON)) {
264             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
265                 race_ptr->r_kind_flags.set(MonsterKindType::DEMON);
266             }
267             if (mult < 20) {
268                 mult = 20;
269             }
270         }
271
272         if ((flags.has(TR_KILL_DEMON)) && race_ptr->kind_flags.has(MonsterKindType::DEMON)) {
273             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
274                 race_ptr->r_kind_flags.set(MonsterKindType::DEMON);
275             }
276             if (mult < 30) {
277                 mult = 30;
278             }
279         }
280
281         if ((flags.has(TR_SLAY_ORC)) && race_ptr->kind_flags.has(MonsterKindType::ORC)) {
282             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
283                 race_ptr->r_kind_flags.set(MonsterKindType::ORC);
284             }
285             if (mult < 20) {
286                 mult = 20;
287             }
288         }
289
290         if ((flags.has(TR_KILL_ORC)) && race_ptr->kind_flags.has(MonsterKindType::ORC)) {
291             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
292                 race_ptr->r_kind_flags.set(MonsterKindType::ORC);
293             }
294             if (mult < 30) {
295                 mult = 30;
296             }
297         }
298
299         if ((flags.has(TR_SLAY_TROLL)) && race_ptr->kind_flags.has(MonsterKindType::TROLL)) {
300             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
301                 race_ptr->r_kind_flags.set(MonsterKindType::TROLL);
302             }
303
304             if (mult < 20) {
305                 mult = 20;
306             }
307         }
308
309         if ((flags.has(TR_KILL_TROLL)) && race_ptr->kind_flags.has(MonsterKindType::TROLL)) {
310             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
311                 race_ptr->r_kind_flags.set(MonsterKindType::TROLL);
312             }
313             if (mult < 30) {
314                 mult = 30;
315             }
316         }
317
318         if ((flags.has(TR_SLAY_GIANT)) && race_ptr->kind_flags.has(MonsterKindType::GIANT)) {
319             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
320                 race_ptr->r_kind_flags.set(MonsterKindType::GIANT);
321             }
322             if (mult < 20) {
323                 mult = 20;
324             }
325         }
326
327         if ((flags.has(TR_KILL_GIANT)) && race_ptr->kind_flags.has(MonsterKindType::GIANT)) {
328             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
329                 race_ptr->r_kind_flags.set(MonsterKindType::GIANT);
330             }
331             if (mult < 30) {
332                 mult = 30;
333             }
334         }
335
336         if ((flags.has(TR_SLAY_DRAGON)) && race_ptr->kind_flags.has(MonsterKindType::DRAGON)) {
337             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
338                 race_ptr->r_kind_flags.set(MonsterKindType::DRAGON);
339             }
340             if (mult < 20) {
341                 mult = 20;
342             }
343         }
344
345         if ((flags.has(TR_KILL_DRAGON)) && race_ptr->kind_flags.has(MonsterKindType::DRAGON)) {
346             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
347                 race_ptr->r_kind_flags.set(MonsterKindType::DRAGON);
348             }
349             if (mult < 30) {
350                 mult = 30;
351             }
352
353             auto can_eliminate_smaug = arrow_ptr->is_specific_artifact(FixedArtifactId::BARD_ARROW);
354             can_eliminate_smaug &= player_ptr->inventory_list[INVEN_BOW].is_specific_artifact(FixedArtifactId::BARD);
355             can_eliminate_smaug &= monster_ptr->r_idx == MonsterRaceId::SMAUG;
356             if (can_eliminate_smaug) {
357                 mult *= 5;
358             }
359         }
360
361         if (flags.has(TR_BRAND_ACID)) {
362             /* Notice immunity */
363             if (race_ptr->resistance_flags.has_any_of(RFR_EFF_IM_ACID_MASK)) {
364                 if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
365                     race_ptr->r_resistance_flags.set(race_ptr->resistance_flags & RFR_EFF_IM_ACID_MASK);
366                 }
367             } else {
368                 if (mult < 17) {
369                     mult = 17;
370                 }
371             }
372         }
373
374         if (flags.has(TR_BRAND_ELEC)) {
375             /* Notice immunity */
376             if (race_ptr->resistance_flags.has_any_of(RFR_EFF_IM_ELEC_MASK)) {
377                 if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
378                     race_ptr->r_resistance_flags.set(race_ptr->resistance_flags & RFR_EFF_IM_ELEC_MASK);
379                 }
380             } else {
381                 if (mult < 17) {
382                     mult = 17;
383                 }
384             }
385         }
386
387         if (flags.has(TR_BRAND_FIRE)) {
388             /* Notice immunity */
389             if (race_ptr->resistance_flags.has_any_of(RFR_EFF_IM_FIRE_MASK)) {
390                 if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
391                     race_ptr->r_resistance_flags.set(race_ptr->resistance_flags & RFR_EFF_IM_FIRE_MASK);
392                 }
393             }
394             /* Otherwise, take the damage */
395             else {
396                 if (race_ptr->resistance_flags.has(MonsterResistanceType::HURT_FIRE)) {
397                     if (mult < 25) {
398                         mult = 25;
399                     }
400                     if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
401                         race_ptr->r_resistance_flags.set(MonsterResistanceType::HURT_FIRE);
402                     }
403                 } else if (mult < 17) {
404                     mult = 17;
405                 }
406             }
407         }
408
409         if (flags.has(TR_BRAND_COLD)) {
410             /* Notice immunity */
411             if (race_ptr->resistance_flags.has_any_of(RFR_EFF_IM_COLD_MASK)) {
412                 if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
413                     race_ptr->r_resistance_flags.set(race_ptr->resistance_flags & RFR_EFF_IM_COLD_MASK);
414                 }
415             }
416             /* Otherwise, take the damage */
417             else {
418                 if (race_ptr->resistance_flags.has(MonsterResistanceType::HURT_COLD)) {
419                     if (mult < 25) {
420                         mult = 25;
421                     }
422                     if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
423                         race_ptr->r_resistance_flags.set(MonsterResistanceType::HURT_COLD);
424                     }
425                 } else if (mult < 17) {
426                     mult = 17;
427                 }
428             }
429         }
430
431         if (flags.has(TR_BRAND_POIS)) {
432             /* Notice immunity */
433             if (race_ptr->resistance_flags.has_any_of(RFR_EFF_IM_POISON_MASK)) {
434                 if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
435                     race_ptr->r_resistance_flags.set(race_ptr->resistance_flags & RFR_EFF_IM_POISON_MASK);
436                 }
437             }
438             /* Otherwise, take the damage */
439             else {
440                 if (mult < 17) {
441                     mult = 17;
442                 }
443             }
444         }
445
446         if ((flags.has(TR_FORCE_WEAPON)) && (player_ptr->csp > (player_ptr->msp / 30))) {
447             player_ptr->csp -= (1 + (player_ptr->msp / 30));
448             set_bits(player_ptr->redraw, PR_MANA);
449             mult = mult * 5 / 2;
450         }
451         break;
452     }
453
454     default:
455         break;
456     }
457
458     /* Sniper */
459     if (snipe_type) {
460         mult = calc_snipe_damage_with_slay(player_ptr, mult, monster_ptr, snipe_type);
461     }
462
463     /* Return the total damage */
464     return tdam * mult / 10;
465 }
466
467 /*!
468  * @brief 射撃処理実行 /
469  * Fire an object from the pack or floor.
470  * @param item 射撃するオブジェクトの所持ID
471  * @param bow_ptr 射撃武器のオブジェクト参照ポインタ
472  * @details
473  * <pre>
474  * You may only fire items that "match" your missile launcher.
475  * You must use slings + pebbles/shots, bows + arrows, xbows + bolts.
476  * See "calc_bonuses()" for more calculations and such.
477  * Note that "firing" a missile is MUCH better than "throwing" it.
478  * Note: "unseen" monsters are very hard to hit.
479  * Objects are more likely to break if they "attempt" to hit a monster.
480  * Rangers (with Bows) and Anyone (with "Extra Shots") get extra shots.
481  * The "extra shot" code works by decreasing the amount of energy
482  * required to make each shot, spreading the shots out over time.
483  * Note that when firing missiles, the launcher multiplier is applied
484  * after all the bonuses are added in, making multipliers very useful.
485  * Note that Bows of "Extra Might" get extra range and an extra bonus
486  * for the damage multiplier.
487  * Note that Bows of "Extra Shots" give an extra shot.
488  * </pre>
489  */
490 void exe_fire(PlayerType *player_ptr, INVENTORY_IDX item, ItemEntity *j_ptr, SPELL_IDX snipe_type)
491 {
492     POSITION y, x, ny, nx, ty, tx, prev_y, prev_x;
493     ItemEntity forge;
494     ItemEntity *q_ptr;
495     ItemEntity *o_ptr;
496
497     AttributeFlags attribute_flags{};
498     attribute_flags.set(AttributeType::PLAYER_SHOOT);
499
500     auto hit_body = false;
501     auto stick_to = false;
502
503     /* Access the item (if in the pack) */
504     if (item >= 0) {
505         o_ptr = &player_ptr->inventory_list[item];
506     } else {
507         o_ptr = &player_ptr->current_floor_ptr->o_list[0 - item];
508     }
509
510     /* Sniper - Cannot shot a single arrow twice */
511     if ((snipe_type == SP_DOUBLE) && (o_ptr->number < 2)) {
512         snipe_type = SP_NONE;
513     }
514
515     const auto item_name = describe_flavor(player_ptr, o_ptr, OD_OMIT_PREFIX);
516
517     /* Use the proper number of shots */
518     auto thits = player_ptr->num_fire;
519
520     /* Use a base distance */
521     auto tdis = 10;
522
523     /* Base damage from thrown object plus launcher bonus */
524     auto tdam_base = damroll(o_ptr->dd, o_ptr->ds) + o_ptr->to_d + j_ptr->to_d;
525
526     /* Actually "fire" the object */
527     const auto tval = j_ptr->bi_key.tval();
528     const auto median_skill_exp = PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2;
529     const auto bonus = (player_ptr->to_h_b + o_ptr->to_h + j_ptr->to_h);
530     const auto &weapon_exps = player_ptr->weapon_exp[tval];
531     constexpr auto bow_magnification = 200;
532     constexpr auto xbow_magnification = 400;
533     int chance;
534     if (tval == ItemKindType::NONE) {
535         chance = (player_ptr->skill_thb + ((weapon_exps[0] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ);
536     } else {
537         const auto sval = j_ptr->bi_key.sval().value();
538         if (j_ptr->is_cross_bow()) {
539             chance = (player_ptr->skill_thb + (weapon_exps[sval] / xbow_magnification + bonus) * BTH_PLUS_ADJ);
540         } else {
541             chance = (player_ptr->skill_thb + ((weapon_exps[sval] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ);
542         }
543     }
544
545     PlayerEnergy(player_ptr).set_player_turn_energy(j_ptr->get_bow_energy());
546     auto tmul = j_ptr->get_arrow_magnification();
547
548     /* Get extra "power" from "extra might" */
549     if (player_ptr->xtra_might) {
550         tmul++;
551     }
552
553     tmul = tmul * (100 + (int)(adj_str_td[player_ptr->stat_index[A_STR]]) - 128);
554
555     /* Boost the damage */
556     tdam_base *= tmul;
557     tdam_base /= 100;
558
559     auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
560     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
561
562     /* Base range */
563     tdis = 13 + tmul / 80;
564     if (j_ptr->is_cross_bow()) {
565         tdis -= (5 - (sniper_concent + 1) / 2);
566     }
567
568     project_length = tdis + 1;
569
570     /* Get a direction (or cancel) */
571     DIRECTION dir;
572     if (!get_aim_dir(player_ptr, &dir)) {
573         PlayerEnergy(player_ptr).reset_player_turn();
574
575         if (snipe_type == SP_AWAY) {
576             snipe_type = SP_NONE;
577         }
578
579         /* need not to reset project_length (already did)*/
580
581         return;
582     }
583
584     if (snipe_type != SP_NONE) {
585         sound(SOUND_ZAP);
586     }
587
588     /* Predict the "target" location */
589     tx = player_ptr->x + 99 * ddx[dir];
590     ty = player_ptr->y + 99 * ddy[dir];
591
592     /* Check for "target request" */
593     if ((dir == 5) && target_okay(player_ptr)) {
594         tx = target_col;
595         ty = target_row;
596     }
597
598     /* Get projection path length */
599     projection_path path_g(player_ptr, project_length, player_ptr->y, player_ptr->x, ty, tx, PROJECT_PATH | PROJECT_THRU);
600     tdis = path_g.path_num() - 1;
601
602     project_length = 0; /* reset to default */
603
604     /* Don't shoot at my feet */
605     if (tx == player_ptr->x && ty == player_ptr->y) {
606         PlayerEnergy(player_ptr).reset_player_turn();
607
608         /* project_length is already reset to 0 */
609
610         return;
611     }
612
613     /* Take a (partial) turn */
614     PlayerEnergy(player_ptr).div_player_turn_energy(thits);
615     player_ptr->is_fired = true;
616
617     /* Sniper - Difficult to shot twice at 1 turn */
618     if (snipe_type == SP_DOUBLE) {
619         sniper_concent = (sniper_concent + 1) / 2;
620     }
621
622     /* Sniper - Repeat shooting when double shots */
623     for (auto i = 0; i < ((snipe_type == SP_DOUBLE) ? 2 : 1); i++) {
624         /* Start at the player */
625         y = player_ptr->y;
626         x = player_ptr->x;
627         q_ptr = &forge;
628         q_ptr->copy_from(o_ptr);
629
630         /* Single object */
631         q_ptr->number = 1;
632
633         vary_item(player_ptr, item, -1);
634
635         sound(SOUND_SHOOT);
636         handle_stuff(player_ptr);
637
638         prev_y = y;
639         prev_x = x;
640
641         /* The shot does not hit yet */
642         hit_body = false;
643
644         /* Travel until stopped */
645         for (auto cur_dis = 0; cur_dis <= tdis;) {
646             grid_type *g_ptr;
647
648             /* Hack -- Stop at the target */
649             if ((y == ty) && (x == tx)) {
650                 break;
651             }
652
653             /* Calculate the new location (see "project()") */
654             ny = y;
655             nx = x;
656             mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx);
657
658             /* Shatter Arrow */
659             if (snipe_type == SP_KILL_WALL) {
660                 g_ptr = &player_ptr->current_floor_ptr->grid_array[ny][nx];
661
662                 if (g_ptr->cave_has_flag(TerrainCharacteristics::HURT_ROCK) && !g_ptr->m_idx) {
663                     if (any_bits(g_ptr->info, (CAVE_MARK))) {
664                         msg_print(_("岩が砕け散った。", "Wall rocks were shattered."));
665                     }
666                     /* Forget the wall */
667                     reset_bits(g_ptr->info, (CAVE_MARK));
668                     set_bits(player_ptr->update, PU_VIEW | PU_LITE | PU_FLOW | PU_MON_LITE);
669
670                     /* Destroy the wall */
671                     cave_alter_feat(player_ptr, ny, nx, TerrainCharacteristics::HURT_ROCK);
672
673                     hit_body = true;
674                     break;
675                 }
676             }
677
678             /* Stopped by walls/doors */
679             if (!cave_has_flag_bold(player_ptr->current_floor_ptr, ny, nx, TerrainCharacteristics::PROJECT) && !player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx) {
680                 break;
681             }
682
683             /* Advance the distance */
684             cur_dis++;
685
686             /* Sniper */
687             if (snipe_type == SP_LITE) {
688                 set_bits(player_ptr->current_floor_ptr->grid_array[ny][nx].info, CAVE_GLOW);
689                 note_spot(player_ptr, ny, nx);
690                 lite_spot(player_ptr, ny, nx);
691             }
692
693             /* The player can see the (on screen) missile */
694             if (panel_contains(ny, nx) && player_can_see_bold(player_ptr, ny, nx)) {
695                 const auto a = q_ptr->get_color();
696                 const auto c = q_ptr->get_symbol();
697
698                 /* Draw, Hilite, Fresh, Pause, Erase */
699                 if (delay_factor > 0) {
700                     print_rel(player_ptr, c, a, ny, nx);
701                     move_cursor_relative(ny, nx);
702                     term_fresh();
703                     term_xtra(TERM_XTRA_DELAY, delay_factor);
704                     lite_spot(player_ptr, ny, nx);
705                     term_fresh();
706                 }
707             }
708
709             /* The player cannot see the missile */
710             else {
711                 /* Pause anyway, for consistancy **/
712                 if (delay_factor > 0) {
713                     term_xtra(TERM_XTRA_DELAY, delay_factor);
714                 }
715             }
716
717             /* Sniper */
718             if (snipe_type == SP_KILL_TRAP) {
719                 project(player_ptr, 0, 0, ny, nx, 0, AttributeType::KILL_TRAP, (PROJECT_JUMP | PROJECT_HIDE | PROJECT_GRID | PROJECT_ITEM));
720             }
721
722             /* Sniper */
723             if (snipe_type == SP_EVILNESS) {
724                 reset_bits(player_ptr->current_floor_ptr->grid_array[ny][nx].info, (CAVE_GLOW | CAVE_MARK));
725                 note_spot(player_ptr, ny, nx);
726                 lite_spot(player_ptr, ny, nx);
727             }
728
729             prev_y = y;
730             prev_x = x;
731
732             /* Save the new location */
733             x = nx;
734             y = ny;
735
736             /* Monster here, Try to hit it */
737             if (player_ptr->current_floor_ptr->grid_array[y][x].m_idx) {
738                 sound(SOUND_SHOOT_HIT);
739                 grid_type *c_mon_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
740
741                 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[c_mon_ptr->m_idx];
742                 auto *r_ptr = &monraces_info[m_ptr->r_idx];
743
744                 /* Check the visibility */
745                 auto visible = m_ptr->ml;
746
747                 /* Note the collision */
748                 hit_body = true;
749
750                 if (m_ptr->is_asleep()) {
751                     if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5)) {
752                         chg_virtue(player_ptr, Virtue::COMPASSION, -1);
753                     }
754                     if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5)) {
755                         chg_virtue(player_ptr, Virtue::HONOUR, -1);
756                     }
757                 }
758
759                 if ((r_ptr->level + 10) > player_ptr->lev) {
760                     PlayerSkill(player_ptr).gain_range_weapon_exp(j_ptr);
761                 }
762
763                 if (player_ptr->riding) {
764                     PlayerSkill(player_ptr).gain_riding_skill_exp_on_range_attack();
765                 }
766
767                 /* Did we hit it (penalize range) */
768                 if (test_hit_fire(player_ptr, chance - cur_dis, m_ptr, m_ptr->ml, item_name.data())) {
769                     bool fear = false;
770                     auto tdam = tdam_base; //!< @note 実際に与えるダメージ
771                     auto base_dam = tdam; //!< @note 補正前の与えるダメージ(無傷、全ての耐性など)
772
773                     /* Get extra damage from concentration */
774                     tdam = boost_concentration_damage(player_ptr, tdam);
775
776                     /* Handle unseen monster */
777                     if (!visible) {
778                         /* Invisible monster */
779                         msg_format(_("%sが敵を捕捉した。", "The %s finds a mark."), item_name.data());
780                     }
781
782                     /* Handle visible monster */
783                     else {
784                         /* Get "the monster" or "it" */
785                         const auto m_name = monster_desc(player_ptr, m_ptr, 0);
786
787                         msg_format(_("%sが%sに命中した。", "The %s hits %s."), item_name.data(), m_name.data());
788
789                         if (m_ptr->ml) {
790                             if (!player_ptr->effects()->hallucination()->is_hallucinated()) {
791                                 monster_race_track(player_ptr, m_ptr->ap_r_idx);
792                             }
793
794                             health_track(player_ptr, c_mon_ptr->m_idx);
795                         }
796                     }
797
798                     if (snipe_type == SP_NEEDLE) {
799                         const auto is_unique = r_ptr->kind_flags.has(MonsterKindType::UNIQUE);
800                         const auto fatality = randint1(r_ptr->level / (3 + sniper_concent)) + (8 - sniper_concent);
801                         if ((randint1(fatality) == 1) && !is_unique && none_bits(r_ptr->flags7, RF7_UNIQUE2)) {
802                             /* Get "the monster" or "it" */
803                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
804
805                             tdam = m_ptr->hp + 1;
806                             base_dam = tdam;
807                             msg_format(_("%sの急所に突き刺さった!", "Your shot hit a fatal spot of %s!"), m_name.data());
808                         } else {
809                             tdam = 1;
810                             base_dam = tdam;
811                         }
812                     } else {
813
814                         attribute_flags = shot_attribute(player_ptr, j_ptr, q_ptr, snipe_type);
815                         /* Apply special damage */
816                         tdam = calc_shot_damage_with_slay(player_ptr, j_ptr, q_ptr, tdam, m_ptr, snipe_type);
817                         tdam = critical_shot(player_ptr, q_ptr->weight, q_ptr->to_h, j_ptr->to_h, tdam);
818
819                         /* No negative damage */
820                         if (tdam < 0) {
821                             tdam = 0;
822                         }
823
824                         /* Modify the damage */
825                         base_dam = tdam;
826                         tdam = mon_damage_mod(player_ptr, m_ptr, tdam, false);
827                     }
828
829                     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), tdam,
830                         m_ptr->hp - tdam, m_ptr->maxhp, m_ptr->max_maxhp);
831
832                     /* Sniper */
833                     if (snipe_type == SP_EXPLODE) {
834                         uint16_t flg = (PROJECT_STOP | PROJECT_JUMP | PROJECT_KILL | PROJECT_GRID);
835
836                         sound(SOUND_EXPLODE); /* No explode sound - use breath fire instead */
837                         project(player_ptr, 0, ((sniper_concent + 1) / 2 + 1), ny, nx, base_dam, AttributeType::MISSILE, flg);
838                         break;
839                     }
840
841                     /* Sniper */
842                     if (snipe_type == SP_HOLYNESS) {
843                         set_bits(player_ptr->current_floor_ptr->grid_array[ny][nx].info, CAVE_GLOW);
844                         note_spot(player_ptr, ny, nx);
845                         lite_spot(player_ptr, ny, nx);
846                     }
847
848                     /* Hit the monster, check for death */
849                     MonsterDamageProcessor mdp(player_ptr, c_mon_ptr->m_idx, tdam, &fear, attribute_flags);
850                     if (mdp.mon_take_hit(extract_note_dies(m_ptr->get_real_r_idx()))) {
851                         /* Dead monster */
852                     }
853
854                     /* No death */
855                     else {
856                         /* STICK TO */
857                         if (q_ptr->is_fixed_artifact() && (sniper_concent == 0)) {
858                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
859
860                             stick_to = true;
861                             msg_format(_("%sは%sに突き刺さった!", "%s^ is stuck in %s!"), item_name.data(), m_name.data());
862                         }
863
864                         if (const auto pain_message = MonsterPainDescriber(player_ptr, c_mon_ptr->m_idx).describe(tdam);
865                             !pain_message.empty()) {
866                             msg_print(pain_message);
867                         }
868
869                         /* Anger the monster */
870                         if (tdam > 0) {
871                             anger_monster(player_ptr, m_ptr);
872                         }
873
874                         if (fear && m_ptr->ml) {
875                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
876                             sound(SOUND_FLEE);
877                             msg_format(_("%s^は恐怖して逃げ出した!", "%s^ flees in terror!"), m_name.data());
878                         }
879
880                         set_target(m_ptr, player_ptr->y, player_ptr->x);
881
882                         /* Sniper */
883                         if (snipe_type == SP_RUSH) {
884                             int n = randint1(5) + 3;
885                             MONSTER_IDX m_idx = c_mon_ptr->m_idx;
886
887                             for (; cur_dis <= tdis;) {
888                                 POSITION ox = nx;
889                                 POSITION oy = ny;
890
891                                 if (!n) {
892                                     break;
893                                 }
894
895                                 /* Calculate the new location (see "project()") */
896                                 mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx);
897
898                                 /* Stopped by wilderness boundary */
899                                 if (!in_bounds2(player_ptr->current_floor_ptr, ny, nx)) {
900                                     break;
901                                 }
902
903                                 /* Stopped by walls/doors */
904                                 if (!player_can_enter(player_ptr, player_ptr->current_floor_ptr->grid_array[ny][nx].feat, 0)) {
905                                     break;
906                                 }
907
908                                 /* Stopped by monsters */
909                                 if (!is_cave_empty_bold(player_ptr, ny, nx)) {
910                                     break;
911                                 }
912
913                                 player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
914                                 player_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
915
916                                 m_ptr->fx = nx;
917                                 m_ptr->fy = ny;
918
919                                 update_monster(player_ptr, m_idx, true);
920
921                                 if (delay_factor > 0) {
922                                     lite_spot(player_ptr, ny, nx);
923                                     lite_spot(player_ptr, oy, ox);
924                                     term_fresh();
925                                     term_xtra(TERM_XTRA_DELAY, delay_factor);
926                                 }
927
928                                 x = nx;
929                                 y = ny;
930                                 cur_dis++;
931                                 n--;
932                             }
933                         }
934                     }
935                 }
936
937                 /* Sniper */
938                 if (snipe_type == SP_PIERCE && sniper_concent > 0) {
939                     sniper_concent--;
940                     continue;
941                 }
942
943                 /* Stop looking */
944                 break;
945             }
946         }
947
948         /* Chance of breakage (during attacks) */
949         auto j = (hit_body ? breakage_chance(player_ptr, q_ptr, PlayerClass(player_ptr).equals(PlayerClassType::ARCHER), snipe_type) : 0);
950
951         if (stick_to) {
952             MONSTER_IDX m_idx = player_ptr->current_floor_ptr->grid_array[y][x].m_idx;
953             auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
954             OBJECT_IDX o_idx = o_pop(player_ptr->current_floor_ptr);
955
956             if (!o_idx) {
957                 msg_format(_("%sはどこかへ行った。", "The %s went somewhere."), item_name.data());
958                 if (q_ptr->is_fixed_artifact()) {
959                     artifacts_info.at(j_ptr->fixed_artifact_idx).is_generated = false;
960                 }
961                 return;
962             }
963
964             o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
965             o_ptr->copy_from(q_ptr);
966
967             /* Forget mark */
968             o_ptr->marked.reset(OmType::TOUCHED);
969
970             /* Forget location */
971             o_ptr->iy = o_ptr->ix = 0;
972
973             /* Memorize monster */
974             o_ptr->held_m_idx = m_idx;
975
976             /* Carry object */
977             m_ptr->hold_o_idx_list.add(player_ptr->current_floor_ptr, o_idx);
978         } else if (cave_has_flag_bold(player_ptr->current_floor_ptr, y, x, TerrainCharacteristics::PROJECT)) {
979             /* Drop (or break) near that location */
980             (void)drop_near(player_ptr, q_ptr, j, y, x);
981         } else {
982             /* Drop (or break) near that location */
983             (void)drop_near(player_ptr, q_ptr, j, prev_y, prev_x);
984         }
985
986         /* Sniper - Repeat shooting when double shots */
987     }
988
989     /* Sniper - Loose his/her concentration after any shot */
990     reset_concentration(player_ptr, false);
991 }
992
993 /*!
994  * @brief プレイヤーからモンスターへの射撃命中判定
995  * @param chance 基本命中値
996  * @param monster_ptr モンスターの構造体参照ポインタ
997  * @param vis 目標を視界に捕らえているならばTRUEを指定
998  * @param item_name 石川五右衛門専用メッセージ:無効化した矢弾の名前
999  * @return 命中と判定された場合TRUEを返す
1000  * @note 最低命中率5%、最大命中率95%
1001  */
1002 bool test_hit_fire(PlayerType *player_ptr, int chance, MonsterEntity *m_ptr, int vis, std::string_view item_name)
1003 {
1004     int k;
1005     ARMOUR_CLASS ac;
1006     auto *r_ptr = &monraces_info[m_ptr->r_idx];
1007
1008     /* Percentile dice */
1009     k = randint1(100);
1010
1011     auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
1012     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1013
1014     /* Snipers with high-concentration reduce instant miss percentage.*/
1015     k += sniper_concent;
1016
1017     /* Hack -- Instant miss or hit */
1018     if (k <= 5) {
1019         return false;
1020     }
1021     if (k > 95) {
1022         return true;
1023     }
1024
1025     if (player_ptr->ppersonality == PERSONALITY_LAZY) {
1026         if (one_in_(20)) {
1027             return false;
1028         }
1029     }
1030
1031     /* Never hit */
1032     if (chance <= 0) {
1033         return false;
1034     }
1035
1036     ac = r_ptr->ac;
1037     ac = ac * (8 - sniper_concent) / 8;
1038
1039     if (m_ptr->r_idx == MonsterRaceId::GOEMON && !m_ptr->is_asleep()) {
1040         ac *= 3;
1041     }
1042
1043     /* Invisible monsters are harder to hit */
1044     if (!vis) {
1045         chance = (chance + 1) / 2;
1046     }
1047
1048     /* Power competes against armor */
1049     if (randint0(chance) < (ac * 3 / 4)) {
1050         if (m_ptr->r_idx == MonsterRaceId::GOEMON && !m_ptr->is_asleep()) {
1051             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
1052             msg_format(_("%sは%sを斬り捨てた!", "%s cuts down %s!"), m_name.data(), item_name.data());
1053         }
1054         return false;
1055     }
1056
1057     /* Assume hit */
1058     return true;
1059 }
1060
1061 /*!
1062  * @brief プレイヤーからモンスターへの射撃クリティカル判定
1063  * @param weight 矢弾の重量
1064  * @param plus_ammo 矢弾の命中修正
1065  * @param plus_bow 弓の命中修正
1066  * @param dam 現在算出中のダメージ値
1067  * @return クリティカル修正が入ったダメージ値
1068  */
1069 int critical_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1070 {
1071     const auto &item = player_ptr->inventory_list[INVEN_BOW];
1072     const auto bonus = player_ptr->to_h_b + plus_ammo;
1073     const auto tval = item.bi_key.tval();
1074     const auto median_skill_exp = PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2;
1075     const auto &weapon_exps = player_ptr->weapon_exp[tval];
1076     constexpr auto bow_magnification = 200;
1077     constexpr auto xbow_magnification = 400;
1078     int power;
1079     if (tval == ItemKindType::NONE) {
1080         power = player_ptr->skill_thb + ((weapon_exps[0] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ;
1081     } else {
1082         const auto sval = item.bi_key.sval().value();
1083         const auto weapon_exp = weapon_exps[sval];
1084         if (player_ptr->tval_ammo == ItemKindType::BOLT) {
1085             power = (player_ptr->skill_thb + (weapon_exp / xbow_magnification + bonus) * BTH_PLUS_ADJ);
1086         } else {
1087             power = player_ptr->skill_thb + ((weapon_exp - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ;
1088         }
1089     }
1090
1091     PlayerClass pc(player_ptr);
1092     const auto sniper_data = pc.get_specific_data<sniper_data_type>();
1093     const auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1094
1095     /* Snipers can shot more critically with crossbows */
1096     power += ((power * sniper_concent) / 5);
1097     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT)) {
1098         power *= 2;
1099     }
1100
1101     /* Good bow makes more critical */
1102     power += plus_bow * 8 * (sniper_concent + 5);
1103     if (randint1(10000) > power) {
1104         return dam;
1105     }
1106
1107     const auto k = weight * randint1(500);
1108     if (k < 900) {
1109         msg_print(_("手ごたえがあった!", "It was a good hit!"));
1110         dam += (dam / 2);
1111     } else if (k < 1350) {
1112         msg_print(_("かなりの手ごたえがあった!", "It was a great hit!"));
1113         dam *= 2;
1114     } else {
1115         msg_print(_("会心の一撃だ!", "It was a superb hit!"));
1116         dam *= 3;
1117     }
1118
1119     return dam;
1120 }
1121
1122 /*!
1123  * @brief 射撃時クリティカルによるダメージ期待値修正計算(スナイパーの集中処理と武器経験値) / critical happens at i / 10000
1124  * @param player_ptr プレイヤーへの参照ポインタ
1125  * @param plus_ammo 矢弾のダメージ修正
1126  * @param plus_bow 弓のダメージ修正
1127  * @return ダメージ期待値
1128  * @note 基本ダメージ量と重量はこの部位では計算に加わらない。
1129  */
1130 int calc_crit_ratio_shot(PlayerType *player_ptr, int plus_ammo, int plus_bow)
1131 {
1132     auto *j_ptr = &player_ptr->inventory_list[INVEN_BOW];
1133
1134     /* Extract "shot" power */
1135     auto i = player_ptr->to_h_b + plus_ammo;
1136     const auto tval = j_ptr->bi_key.tval();
1137     const auto sval = j_ptr->bi_key.sval().value();
1138     if (player_ptr->tval_ammo == ItemKindType::BOLT) {
1139         i = (player_ptr->skill_thb + (player_ptr->weapon_exp[tval][sval] / 400 + i) * BTH_PLUS_ADJ);
1140     } else {
1141         i = (player_ptr->skill_thb + ((player_ptr->weapon_exp[tval][sval] - (PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2)) / 200 + i) * BTH_PLUS_ADJ);
1142     }
1143
1144     PlayerClass pc(player_ptr);
1145     auto sniper_data = pc.get_specific_data<sniper_data_type>();
1146     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1147
1148     /* Snipers can shot more critically with crossbows */
1149     i += ((i * sniper_concent) / 5);
1150     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT)) {
1151         i *= 2;
1152     }
1153
1154     /* Good bow makes more critical */
1155     i += plus_bow * 8 * (sniper_concent + 5);
1156
1157     if (i < 0) {
1158         i = 0;
1159     }
1160
1161     return i;
1162 }
1163
1164 /*!
1165  * @brief 射撃時クリティカルによるダメージ期待値修正計算(重量依存部分) / critical happens at i / 10000
1166  * @param weight 武器の重量
1167  * @param plus_ammo 矢弾のダメージ修正
1168  * @param plus_bow 弓のダメージ修正
1169  * @param dam 基本ダメージ量
1170  * @return ダメージ期待値
1171  */
1172 int calc_expect_crit_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1173 {
1174     uint32_t num;
1175     int i, k, crit;
1176     i = calc_crit_ratio_shot(player_ptr, plus_ammo, plus_bow);
1177
1178     k = 0;
1179     num = 0;
1180
1181     crit = std::min(500, 900 / weight);
1182     num += dam * 3 / 2 * crit;
1183     k = crit;
1184
1185     crit = std::min(500, 1350 / weight);
1186     crit -= k;
1187     num += dam * 2 * crit;
1188     k += crit;
1189
1190     if (k < 500) {
1191         crit = 500 - k;
1192         num += dam * 3 * crit;
1193     }
1194
1195     num /= 500;
1196
1197     num *= i;
1198     num += (10000 - i) * dam;
1199     num /= 10000;
1200
1201     return num;
1202 }
1203
1204 /*!
1205  * @brief 攻撃時クリティカルによるダメージ期待値修正計算(重量と毒針処理) / critical happens at i / 10000
1206  * @param player_ptr プレイヤーへの参照ポインタ
1207  * @param weight 武器の重量
1208  * @param plus 武器のダメージ修正
1209  * @param dam 基本ダメージ
1210  * @param meichuu 命中値
1211  * @param dokubari 毒針処理か否か
1212  * @param impact 強撃かどうか
1213  * @return ダメージ期待値
1214  */
1215 int calc_expect_crit(PlayerType *player_ptr, WEIGHT weight, int plus, int dam, int16_t meichuu, bool dokubari, bool impact)
1216 {
1217     if (dokubari) {
1218         return dam;
1219     }
1220
1221     int i = (weight + (meichuu * 3 + plus * 5) + player_ptr->skill_thn);
1222     if (i < 0) {
1223         i = 0;
1224     }
1225
1226     // 通常ダメージdam、武器重量weightでクリティカルが発生した時のクリティカルダメージ期待値
1227     auto calc_weight_expect_dam = [](int dam, WEIGHT weight) {
1228         int sum = 0;
1229         for (int d = 1; d <= 650; ++d) {
1230             int k = weight + d;
1231             sum += std::get<0>(apply_critical_norm_damage(k, dam));
1232         }
1233         return sum / 650;
1234     };
1235
1236     uint32_t num = 0;
1237
1238     if (impact) {
1239         for (int d = 1; d <= 650; ++d) {
1240             num += calc_weight_expect_dam(dam, weight + d);
1241         }
1242         num /= 650;
1243     } else {
1244         num += calc_weight_expect_dam(dam, weight);
1245     }
1246
1247     int pow = PlayerClass(player_ptr).equals(PlayerClassType::NINJA) ? 4444 : 5000;
1248     if (impact) {
1249         pow /= 2;
1250     }
1251
1252     num *= i;
1253     num += (pow - i) * dam;
1254     num /= pow;
1255
1256     return num;
1257 }