OSDN Git Service

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