OSDN Git Service

2e390b4919f3b7e02d592cc5a69fd95923936906
[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/stuff-handler.h"
7 #include "effect/attribute-types.h"
8 #include "effect/effect-characteristics.h"
9 #include "effect/effect-processor.h"
10 #include "effect/spells-effect-util.h"
11 #include "flavor/flavor-describer.h"
12 #include "flavor/object-flavor-types.h"
13 #include "floor/cave.h"
14 #include "floor/floor-object.h"
15 #include "floor/geometry.h"
16 #include "game-option/cheat-types.h"
17 #include "game-option/special-options.h"
18 #include "grid/feature-flag-types.h"
19 #include "grid/feature.h"
20 #include "grid/grid.h"
21 #include "inventory/inventory-object.h"
22 #include "inventory/inventory-slot-types.h"
23 #include "io/cursor.h"
24 #include "io/screen-util.h"
25 #include "main/sound-definitions-table.h"
26 #include "main/sound-of-music.h"
27 #include "mind/mind-sniper.h"
28 #include "mind/snipe-types.h"
29 #include "monster-floor/monster-death.h"
30 #include "monster-floor/monster-move.h"
31 #include "monster-race/monster-race.h"
32 #include "monster-race/race-flags-resistance.h"
33 #include "monster-race/race-flags1.h"
34 #include "monster-race/race-flags2.h"
35 #include "monster-race/race-flags3.h"
36 #include "monster-race/race-flags7.h"
37 #include "monster-race/race-indice-types.h"
38 #include "monster-race/race-resistance-mask.h"
39 #include "monster/monster-damage.h"
40 #include "monster/monster-describer.h"
41 #include "monster/monster-info.h"
42 #include "monster/monster-pain-describer.h"
43 #include "monster/monster-status-setter.h"
44 #include "monster/monster-status.h"
45 #include "monster/monster-update.h"
46 #include "object/object-broken.h"
47 #include "object/object-flags.h"
48 #include "object/object-info.h"
49 #include "object/object-mark-types.h"
50 #include "player-base/player-class.h"
51 #include "player-info/class-info.h"
52 #include "player-info/sniper-data-type.h"
53 #include "player-status/player-energy.h"
54 #include "player/player-personality-types.h"
55 #include "player/player-skill.h"
56 #include "player/player-status-table.h"
57 #include "sv-definition/sv-bow-types.h"
58 #include "system/artifact-type-definition.h"
59 #include "system/baseitem-info.h"
60 #include "system/floor-type-definition.h"
61 #include "system/grid-type-definition.h"
62 #include "system/item-entity.h"
63 #include "system/monster-entity.h"
64 #include "system/monster-race-info.h"
65 #include "system/player-type-definition.h"
66 #include "system/redrawing-flags-updater.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             RedrawingFlagsUpdater::get_instance().set_flag(MainWindowRedrawingFlag::MP);
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     auto *floor_ptr = player_ptr->current_floor_ptr;
505     if (item >= 0) {
506         o_ptr = &player_ptr->inventory_list[item];
507     } else {
508         o_ptr = &floor_ptr->o_list[0 - item];
509     }
510
511     /* Sniper - Cannot shot a single arrow twice */
512     if ((snipe_type == SP_DOUBLE) && (o_ptr->number < 2)) {
513         snipe_type = SP_NONE;
514     }
515
516     const auto item_name = describe_flavor(player_ptr, o_ptr, OD_OMIT_PREFIX);
517
518     /* Use the proper number of shots */
519     auto thits = player_ptr->num_fire;
520
521     /* Use a base distance */
522     auto tdis = 10;
523
524     /* Base damage from thrown object plus launcher bonus */
525     auto tdam_base = damroll(o_ptr->dd, o_ptr->ds) + o_ptr->to_d + j_ptr->to_d;
526
527     /* Actually "fire" the object */
528     const auto tval = j_ptr->bi_key.tval();
529     const auto median_skill_exp = PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2;
530     const auto bonus = (player_ptr->to_h_b + o_ptr->to_h + j_ptr->to_h);
531     const auto &weapon_exps = player_ptr->weapon_exp[tval];
532     constexpr auto bow_magnification = 200;
533     constexpr auto xbow_magnification = 400;
534     int chance;
535     if (tval == ItemKindType::NONE) {
536         chance = (player_ptr->skill_thb + ((weapon_exps[0] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ);
537     } else {
538         const auto sval = j_ptr->bi_key.sval().value();
539         if (j_ptr->is_cross_bow()) {
540             chance = (player_ptr->skill_thb + (weapon_exps[sval] / xbow_magnification + bonus) * BTH_PLUS_ADJ);
541         } else {
542             chance = (player_ptr->skill_thb + ((weapon_exps[sval] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ);
543         }
544     }
545
546     PlayerEnergy(player_ptr).set_player_turn_energy(j_ptr->get_bow_energy());
547     auto tmul = j_ptr->get_arrow_magnification();
548
549     /* Get extra "power" from "extra might" */
550     if (player_ptr->xtra_might) {
551         tmul++;
552     }
553
554     tmul = tmul * (100 + (int)(adj_str_td[player_ptr->stat_index[A_STR]]) - 128);
555
556     /* Boost the damage */
557     tdam_base *= tmul;
558     tdam_base /= 100;
559
560     auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
561     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
562
563     /* Base range */
564     tdis = 13 + tmul / 80;
565     if (j_ptr->is_cross_bow()) {
566         tdis -= (5 - (sniper_concent + 1) / 2);
567     }
568
569     project_length = tdis + 1;
570
571     /* Get a direction (or cancel) */
572     DIRECTION dir;
573     if (!get_aim_dir(player_ptr, &dir)) {
574         PlayerEnergy(player_ptr).reset_player_turn();
575
576         if (snipe_type == SP_AWAY) {
577             snipe_type = SP_NONE;
578         }
579
580         /* need not to reset project_length (already did)*/
581
582         return;
583     }
584
585     if (snipe_type != SP_NONE) {
586         sound(SOUND_ZAP);
587     }
588
589     /* Predict the "target" location */
590     tx = player_ptr->x + 99 * ddx[dir];
591     ty = player_ptr->y + 99 * ddy[dir];
592
593     /* Check for "target request" */
594     if ((dir == 5) && target_okay(player_ptr)) {
595         tx = target_col;
596         ty = target_row;
597     }
598
599     /* Get projection path length */
600     projection_path path_g(player_ptr, project_length, player_ptr->y, player_ptr->x, ty, tx, PROJECT_PATH | PROJECT_THRU);
601     tdis = path_g.path_num() - 1;
602
603     project_length = 0; /* reset to default */
604
605     /* Don't shoot at my feet */
606     if (tx == player_ptr->x && ty == player_ptr->y) {
607         PlayerEnergy(player_ptr).reset_player_turn();
608
609         /* project_length is already reset to 0 */
610
611         return;
612     }
613
614     /* Take a (partial) turn */
615     PlayerEnergy(player_ptr).div_player_turn_energy(thits);
616     player_ptr->is_fired = true;
617
618     /* Sniper - Difficult to shot twice at 1 turn */
619     if (snipe_type == SP_DOUBLE) {
620         sniper_concent = (sniper_concent + 1) / 2;
621     }
622
623     /* Sniper - Repeat shooting when double shots */
624     for (auto i = 0; i < ((snipe_type == SP_DOUBLE) ? 2 : 1); i++) {
625         /* Start at the player */
626         y = player_ptr->y;
627         x = player_ptr->x;
628         q_ptr = &forge;
629         q_ptr->copy_from(o_ptr);
630
631         /* Single object */
632         q_ptr->number = 1;
633
634         vary_item(player_ptr, item, -1);
635
636         sound(SOUND_SHOOT);
637         handle_stuff(player_ptr);
638
639         prev_y = y;
640         prev_x = x;
641
642         /* The shot does not hit yet */
643         hit_body = false;
644
645         /* Travel until stopped */
646         for (auto cur_dis = 0; cur_dis <= tdis;) {
647             grid_type *g_ptr;
648
649             /* Hack -- Stop at the target */
650             if ((y == ty) && (x == tx)) {
651                 break;
652             }
653
654             /* Calculate the new location (see "project()") */
655             ny = y;
656             nx = x;
657             mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx);
658
659             /* Shatter Arrow */
660             if (snipe_type == SP_KILL_WALL) {
661                 g_ptr = &floor_ptr->grid_array[ny][nx];
662
663                 if (g_ptr->cave_has_flag(TerrainCharacteristics::HURT_ROCK) && !g_ptr->m_idx) {
664                     if (any_bits(g_ptr->info, (CAVE_MARK))) {
665                         msg_print(_("岩が砕け散った。", "Wall rocks were shattered."));
666                     }
667                     /* Forget the wall */
668                     reset_bits(g_ptr->info, (CAVE_MARK));
669                     const auto flags = {
670                         StatusRedrawingFlag::VIEW,
671                         StatusRedrawingFlag::LITE,
672                         StatusRedrawingFlag::FLOW,
673                         StatusRedrawingFlag::MONSTER_LITE,
674                     };
675                     RedrawingFlagsUpdater::get_instance().set_flags(flags);
676
677                     /* Destroy the wall */
678                     cave_alter_feat(player_ptr, ny, nx, TerrainCharacteristics::HURT_ROCK);
679
680                     hit_body = true;
681                     break;
682                 }
683             }
684
685             /* Stopped by walls/doors */
686             if (!cave_has_flag_bold(floor_ptr, ny, nx, TerrainCharacteristics::PROJECT) && !floor_ptr->grid_array[ny][nx].m_idx) {
687                 break;
688             }
689
690             /* Advance the distance */
691             cur_dis++;
692
693             /* Sniper */
694             if (snipe_type == SP_LITE) {
695                 set_bits(floor_ptr->grid_array[ny][nx].info, CAVE_GLOW);
696                 note_spot(player_ptr, ny, nx);
697                 lite_spot(player_ptr, ny, nx);
698             }
699
700             /* The player can see the (on screen) missile */
701             if (panel_contains(ny, nx) && player_can_see_bold(player_ptr, ny, nx)) {
702                 const auto a = q_ptr->get_color();
703                 const auto c = q_ptr->get_symbol();
704
705                 /* Draw, Hilite, Fresh, Pause, Erase */
706                 if (delay_factor > 0) {
707                     print_rel(player_ptr, c, a, ny, nx);
708                     move_cursor_relative(ny, nx);
709                     term_fresh();
710                     term_xtra(TERM_XTRA_DELAY, delay_factor);
711                     lite_spot(player_ptr, ny, nx);
712                     term_fresh();
713                 }
714             }
715
716             /* The player cannot see the missile */
717             else {
718                 /* Pause anyway, for consistancy **/
719                 if (delay_factor > 0) {
720                     term_xtra(TERM_XTRA_DELAY, delay_factor);
721                 }
722             }
723
724             /* Sniper */
725             if (snipe_type == SP_KILL_TRAP) {
726                 constexpr auto flags = PROJECT_JUMP | PROJECT_HIDE | PROJECT_GRID | PROJECT_ITEM;
727                 project(player_ptr, 0, 0, ny, nx, 0, AttributeType::KILL_TRAP, flags);
728             }
729
730             /* Sniper */
731             if (snipe_type == SP_EVILNESS) {
732                 reset_bits(floor_ptr->grid_array[ny][nx].info, (CAVE_GLOW | CAVE_MARK));
733                 note_spot(player_ptr, ny, nx);
734                 lite_spot(player_ptr, ny, nx);
735             }
736
737             prev_y = y;
738             prev_x = x;
739
740             /* Save the new location */
741             x = nx;
742             y = ny;
743
744             /* Monster here, Try to hit it */
745             if (floor_ptr->grid_array[y][x].m_idx) {
746                 sound(SOUND_SHOOT_HIT);
747                 grid_type *c_mon_ptr = &floor_ptr->grid_array[y][x];
748
749                 auto *m_ptr = &floor_ptr->m_list[c_mon_ptr->m_idx];
750                 auto *r_ptr = &monraces_info[m_ptr->r_idx];
751
752                 /* Check the visibility */
753                 auto visible = m_ptr->ml;
754
755                 /* Note the collision */
756                 hit_body = true;
757
758                 if (m_ptr->is_asleep()) {
759                     if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5)) {
760                         chg_virtue(player_ptr, Virtue::COMPASSION, -1);
761                     }
762                     if (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5)) {
763                         chg_virtue(player_ptr, Virtue::HONOUR, -1);
764                     }
765                 }
766
767                 if ((r_ptr->level + 10) > player_ptr->lev) {
768                     PlayerSkill(player_ptr).gain_range_weapon_exp(j_ptr);
769                 }
770
771                 if (player_ptr->riding) {
772                     PlayerSkill(player_ptr).gain_riding_skill_exp_on_range_attack();
773                 }
774
775                 /* Did we hit it (penalize range) */
776                 if (test_hit_fire(player_ptr, chance - cur_dis, m_ptr, m_ptr->ml, item_name.data())) {
777                     bool fear = false;
778                     auto tdam = tdam_base; //!< @note 実際に与えるダメージ
779                     auto base_dam = tdam; //!< @note 補正前の与えるダメージ(無傷、全ての耐性など)
780
781                     /* Get extra damage from concentration */
782                     tdam = boost_concentration_damage(player_ptr, tdam);
783
784                     /* Handle unseen monster */
785                     if (!visible) {
786                         /* Invisible monster */
787                         msg_format(_("%sが敵を捕捉した。", "The %s finds a mark."), item_name.data());
788                     }
789
790                     /* Handle visible monster */
791                     else {
792                         /* Get "the monster" or "it" */
793                         const auto m_name = monster_desc(player_ptr, m_ptr, 0);
794
795                         msg_format(_("%sが%sに命中した。", "The %s hits %s."), item_name.data(), m_name.data());
796
797                         if (m_ptr->ml) {
798                             if (!player_ptr->effects()->hallucination()->is_hallucinated()) {
799                                 monster_race_track(player_ptr, m_ptr->ap_r_idx);
800                             }
801
802                             health_track(player_ptr, c_mon_ptr->m_idx);
803                         }
804                     }
805
806                     if (snipe_type == SP_NEEDLE) {
807                         const auto is_unique = r_ptr->kind_flags.has(MonsterKindType::UNIQUE);
808                         const auto fatality = randint1(r_ptr->level / (3 + sniper_concent)) + (8 - sniper_concent);
809                         if ((randint1(fatality) == 1) && !is_unique && none_bits(r_ptr->flags7, RF7_UNIQUE2)) {
810                             /* Get "the monster" or "it" */
811                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
812
813                             tdam = m_ptr->hp + 1;
814                             base_dam = tdam;
815                             msg_format(_("%sの急所に突き刺さった!", "Your shot hit a fatal spot of %s!"), m_name.data());
816                         } else {
817                             tdam = 1;
818                             base_dam = tdam;
819                         }
820                     } else {
821
822                         attribute_flags = shot_attribute(player_ptr, j_ptr, q_ptr, snipe_type);
823                         /* Apply special damage */
824                         tdam = calc_shot_damage_with_slay(player_ptr, j_ptr, q_ptr, tdam, m_ptr, snipe_type);
825                         tdam = critical_shot(player_ptr, q_ptr->weight, q_ptr->to_h, j_ptr->to_h, tdam);
826
827                         /* No negative damage */
828                         if (tdam < 0) {
829                             tdam = 0;
830                         }
831
832                         /* Modify the damage */
833                         base_dam = tdam;
834                         tdam = mon_damage_mod(player_ptr, m_ptr, tdam, false);
835                     }
836
837                     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), tdam,
838                         m_ptr->hp - tdam, m_ptr->maxhp, m_ptr->max_maxhp);
839
840                     /* Sniper */
841                     if (snipe_type == SP_EXPLODE) {
842                         uint16_t flg = (PROJECT_STOP | PROJECT_JUMP | PROJECT_KILL | PROJECT_GRID);
843
844                         sound(SOUND_EXPLODE); /* No explode sound - use breath fire instead */
845                         project(player_ptr, 0, ((sniper_concent + 1) / 2 + 1), ny, nx, base_dam, AttributeType::MISSILE, flg);
846                         break;
847                     }
848
849                     /* Sniper */
850                     if (snipe_type == SP_HOLYNESS) {
851                         set_bits(floor_ptr->grid_array[ny][nx].info, CAVE_GLOW);
852                         note_spot(player_ptr, ny, nx);
853                         lite_spot(player_ptr, ny, nx);
854                     }
855
856                     /* Hit the monster, check for death */
857                     MonsterDamageProcessor mdp(player_ptr, c_mon_ptr->m_idx, tdam, &fear, attribute_flags);
858                     if (mdp.mon_take_hit(extract_note_dies(m_ptr->get_real_r_idx()))) {
859                         /* Dead monster */
860                     }
861
862                     /* No death */
863                     else {
864                         /* STICK TO */
865                         if (q_ptr->is_fixed_artifact() && (sniper_concent == 0)) {
866                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
867
868                             stick_to = true;
869                             msg_format(_("%sは%sに突き刺さった!", "%s^ is stuck in %s!"), item_name.data(), m_name.data());
870                         }
871
872                         if (const auto pain_message = MonsterPainDescriber(player_ptr, c_mon_ptr->m_idx).describe(tdam);
873                             !pain_message.empty()) {
874                             msg_print(pain_message);
875                         }
876
877                         /* Anger the monster */
878                         if (tdam > 0) {
879                             anger_monster(player_ptr, m_ptr);
880                         }
881
882                         if (fear && m_ptr->ml) {
883                             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
884                             sound(SOUND_FLEE);
885                             msg_format(_("%s^は恐怖して逃げ出した!", "%s^ flees in terror!"), m_name.data());
886                         }
887
888                         set_target(m_ptr, player_ptr->y, player_ptr->x);
889
890                         /* Sniper */
891                         if (snipe_type == SP_RUSH) {
892                             int n = randint1(5) + 3;
893                             MONSTER_IDX m_idx = c_mon_ptr->m_idx;
894
895                             for (; cur_dis <= tdis;) {
896                                 POSITION ox = nx;
897                                 POSITION oy = ny;
898
899                                 if (!n) {
900                                     break;
901                                 }
902
903                                 /* Calculate the new location (see "project()") */
904                                 mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx);
905
906                                 /* Stopped by wilderness boundary */
907                                 if (!in_bounds2(floor_ptr, ny, nx)) {
908                                     break;
909                                 }
910
911                                 /* Stopped by walls/doors */
912                                 if (!player_can_enter(player_ptr, floor_ptr->grid_array[ny][nx].feat, 0)) {
913                                     break;
914                                 }
915
916                                 /* Stopped by monsters */
917                                 if (!is_cave_empty_bold(player_ptr, ny, nx)) {
918                                     break;
919                                 }
920
921                                 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
922                                 floor_ptr->grid_array[oy][ox].m_idx = 0;
923
924                                 m_ptr->fx = nx;
925                                 m_ptr->fy = ny;
926
927                                 update_monster(player_ptr, m_idx, true);
928
929                                 if (delay_factor > 0) {
930                                     lite_spot(player_ptr, ny, nx);
931                                     lite_spot(player_ptr, oy, ox);
932                                     term_fresh();
933                                     term_xtra(TERM_XTRA_DELAY, delay_factor);
934                                 }
935
936                                 x = nx;
937                                 y = ny;
938                                 cur_dis++;
939                                 n--;
940                             }
941                         }
942                     }
943                 }
944
945                 /* Sniper */
946                 if (snipe_type == SP_PIERCE && sniper_concent > 0) {
947                     sniper_concent--;
948                     continue;
949                 }
950
951                 /* Stop looking */
952                 break;
953             }
954         }
955
956         /* Chance of breakage (during attacks) */
957         auto j = (hit_body ? breakage_chance(player_ptr, q_ptr, PlayerClass(player_ptr).equals(PlayerClassType::ARCHER), snipe_type) : 0);
958
959         if (stick_to) {
960             MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
961             auto *m_ptr = &floor_ptr->m_list[m_idx];
962             OBJECT_IDX o_idx = o_pop(floor_ptr);
963
964             if (!o_idx) {
965                 msg_format(_("%sはどこかへ行った。", "The %s went somewhere."), item_name.data());
966                 if (q_ptr->is_fixed_artifact()) {
967                     ArtifactsInfo::get_instance().get_artifact(j_ptr->fixed_artifact_idx).is_generated = false;
968                 }
969                 return;
970             }
971
972             o_ptr = &floor_ptr->o_list[o_idx];
973             o_ptr->copy_from(q_ptr);
974
975             /* Forget mark */
976             o_ptr->marked.reset(OmType::TOUCHED);
977
978             /* Forget location */
979             o_ptr->iy = o_ptr->ix = 0;
980
981             /* Memorize monster */
982             o_ptr->held_m_idx = m_idx;
983
984             /* Carry object */
985             m_ptr->hold_o_idx_list.add(floor_ptr, o_idx);
986         } else if (cave_has_flag_bold(floor_ptr, y, x, TerrainCharacteristics::PROJECT)) {
987             /* Drop (or break) near that location */
988             (void)drop_near(player_ptr, q_ptr, j, y, x);
989         } else {
990             /* Drop (or break) near that location */
991             (void)drop_near(player_ptr, q_ptr, j, prev_y, prev_x);
992         }
993
994         /* Sniper - Repeat shooting when double shots */
995     }
996
997     /* Sniper - Loose his/her concentration after any shot */
998     reset_concentration(player_ptr, false);
999 }
1000
1001 /*!
1002  * @brief プレイヤーからモンスターへの射撃命中判定
1003  * @param chance 基本命中値
1004  * @param monster_ptr モンスターの構造体参照ポインタ
1005  * @param vis 目標を視界に捕らえているならばTRUEを指定
1006  * @param item_name 石川五右衛門専用メッセージ:無効化した矢弾の名前
1007  * @return 命中と判定された場合TRUEを返す
1008  * @note 最低命中率5%、最大命中率95%
1009  */
1010 bool test_hit_fire(PlayerType *player_ptr, int chance, MonsterEntity *m_ptr, int vis, std::string_view item_name)
1011 {
1012     int k;
1013     ARMOUR_CLASS ac;
1014     auto *r_ptr = &monraces_info[m_ptr->r_idx];
1015
1016     /* Percentile dice */
1017     k = randint1(100);
1018
1019     auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
1020     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1021
1022     /* Snipers with high-concentration reduce instant miss percentage.*/
1023     k += sniper_concent;
1024
1025     /* Hack -- Instant miss or hit */
1026     if (k <= 5) {
1027         return false;
1028     }
1029     if (k > 95) {
1030         return true;
1031     }
1032
1033     if (player_ptr->ppersonality == PERSONALITY_LAZY) {
1034         if (one_in_(20)) {
1035             return false;
1036         }
1037     }
1038
1039     /* Never hit */
1040     if (chance <= 0) {
1041         return false;
1042     }
1043
1044     ac = r_ptr->ac;
1045     ac = ac * (8 - sniper_concent) / 8;
1046
1047     if (m_ptr->r_idx == MonsterRaceId::GOEMON && !m_ptr->is_asleep()) {
1048         ac *= 3;
1049     }
1050
1051     /* Invisible monsters are harder to hit */
1052     if (!vis) {
1053         chance = (chance + 1) / 2;
1054     }
1055
1056     /* Power competes against armor */
1057     if (randint0(chance) < (ac * 3 / 4)) {
1058         if (m_ptr->r_idx == MonsterRaceId::GOEMON && !m_ptr->is_asleep()) {
1059             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
1060             msg_format(_("%sは%sを斬り捨てた!", "%s cuts down %s!"), m_name.data(), item_name.data());
1061         }
1062         return false;
1063     }
1064
1065     /* Assume hit */
1066     return true;
1067 }
1068
1069 /*!
1070  * @brief プレイヤーからモンスターへの射撃クリティカル判定
1071  * @param weight 矢弾の重量
1072  * @param plus_ammo 矢弾の命中修正
1073  * @param plus_bow 弓の命中修正
1074  * @param dam 現在算出中のダメージ値
1075  * @return クリティカル修正が入ったダメージ値
1076  */
1077 int critical_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1078 {
1079     const auto &item = player_ptr->inventory_list[INVEN_BOW];
1080     const auto bonus = player_ptr->to_h_b + plus_ammo;
1081     const auto tval = item.bi_key.tval();
1082     const auto median_skill_exp = PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2;
1083     const auto &weapon_exps = player_ptr->weapon_exp[tval];
1084     constexpr auto bow_magnification = 200;
1085     constexpr auto xbow_magnification = 400;
1086     int power;
1087     if (tval == ItemKindType::NONE) {
1088         power = player_ptr->skill_thb + ((weapon_exps[0] - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ;
1089     } else {
1090         const auto sval = item.bi_key.sval().value();
1091         const auto weapon_exp = weapon_exps[sval];
1092         if (player_ptr->tval_ammo == ItemKindType::BOLT) {
1093             power = (player_ptr->skill_thb + (weapon_exp / xbow_magnification + bonus) * BTH_PLUS_ADJ);
1094         } else {
1095             power = player_ptr->skill_thb + ((weapon_exp - median_skill_exp) / bow_magnification + bonus) * BTH_PLUS_ADJ;
1096         }
1097     }
1098
1099     PlayerClass pc(player_ptr);
1100     const auto sniper_data = pc.get_specific_data<sniper_data_type>();
1101     const auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1102
1103     /* Snipers can shot more critically with crossbows */
1104     power += ((power * sniper_concent) / 5);
1105     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT)) {
1106         power *= 2;
1107     }
1108
1109     /* Good bow makes more critical */
1110     power += plus_bow * 8 * (sniper_concent + 5);
1111     if (randint1(10000) > power) {
1112         return dam;
1113     }
1114
1115     const auto k = weight * randint1(500);
1116     if (k < 900) {
1117         msg_print(_("手ごたえがあった!", "It was a good hit!"));
1118         dam += (dam / 2);
1119     } else if (k < 1350) {
1120         msg_print(_("かなりの手ごたえがあった!", "It was a great hit!"));
1121         dam *= 2;
1122     } else {
1123         msg_print(_("会心の一撃だ!", "It was a superb hit!"));
1124         dam *= 3;
1125     }
1126
1127     return dam;
1128 }
1129
1130 /*!
1131  * @brief 射撃時クリティカルによるダメージ期待値修正計算(スナイパーの集中処理と武器経験値) / critical happens at i / 10000
1132  * @param player_ptr プレイヤーへの参照ポインタ
1133  * @param plus_ammo 矢弾のダメージ修正
1134  * @param plus_bow 弓のダメージ修正
1135  * @return ダメージ期待値
1136  * @note 基本ダメージ量と重量はこの部位では計算に加わらない。
1137  */
1138 int calc_crit_ratio_shot(PlayerType *player_ptr, int plus_ammo, int plus_bow)
1139 {
1140     auto *j_ptr = &player_ptr->inventory_list[INVEN_BOW];
1141
1142     /* Extract "shot" power */
1143     auto i = player_ptr->to_h_b + plus_ammo;
1144     const auto tval = j_ptr->bi_key.tval();
1145     const auto sval = j_ptr->bi_key.sval().value();
1146     if (player_ptr->tval_ammo == ItemKindType::BOLT) {
1147         i = (player_ptr->skill_thb + (player_ptr->weapon_exp[tval][sval] / 400 + i) * BTH_PLUS_ADJ);
1148     } else {
1149         i = (player_ptr->skill_thb + ((player_ptr->weapon_exp[tval][sval] - (PlayerSkill::weapon_exp_at(PlayerSkillRank::MASTER) / 2)) / 200 + i) * BTH_PLUS_ADJ);
1150     }
1151
1152     PlayerClass pc(player_ptr);
1153     auto sniper_data = pc.get_specific_data<sniper_data_type>();
1154     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1155
1156     /* Snipers can shot more critically with crossbows */
1157     i += ((i * sniper_concent) / 5);
1158     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT)) {
1159         i *= 2;
1160     }
1161
1162     /* Good bow makes more critical */
1163     i += plus_bow * 8 * (sniper_concent + 5);
1164
1165     if (i < 0) {
1166         i = 0;
1167     }
1168
1169     return i;
1170 }
1171
1172 /*!
1173  * @brief 射撃時クリティカルによるダメージ期待値修正計算(重量依存部分) / critical happens at i / 10000
1174  * @param weight 武器の重量
1175  * @param plus_ammo 矢弾のダメージ修正
1176  * @param plus_bow 弓のダメージ修正
1177  * @param dam 基本ダメージ量
1178  * @return ダメージ期待値
1179  */
1180 int calc_expect_crit_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1181 {
1182     uint32_t num;
1183     int i, k, crit;
1184     i = calc_crit_ratio_shot(player_ptr, plus_ammo, plus_bow);
1185
1186     k = 0;
1187     num = 0;
1188
1189     crit = std::min(500, 900 / weight);
1190     num += dam * 3 / 2 * crit;
1191     k = crit;
1192
1193     crit = std::min(500, 1350 / weight);
1194     crit -= k;
1195     num += dam * 2 * crit;
1196     k += crit;
1197
1198     if (k < 500) {
1199         crit = 500 - k;
1200         num += dam * 3 * crit;
1201     }
1202
1203     num /= 500;
1204
1205     num *= i;
1206     num += (10000 - i) * dam;
1207     num /= 10000;
1208
1209     return num;
1210 }
1211
1212 /*!
1213  * @brief 攻撃時クリティカルによるダメージ期待値修正計算(重量と毒針処理) / critical happens at i / 10000
1214  * @param player_ptr プレイヤーへの参照ポインタ
1215  * @param weight 武器の重量
1216  * @param plus 武器のダメージ修正
1217  * @param dam 基本ダメージ
1218  * @param meichuu 命中値
1219  * @param dokubari 毒針処理か否か
1220  * @param impact 強撃かどうか
1221  * @return ダメージ期待値
1222  */
1223 int calc_expect_crit(PlayerType *player_ptr, WEIGHT weight, int plus, int dam, int16_t meichuu, bool dokubari, bool impact)
1224 {
1225     if (dokubari) {
1226         return dam;
1227     }
1228
1229     int i = (weight + (meichuu * 3 + plus * 5) + player_ptr->skill_thn);
1230     if (i < 0) {
1231         i = 0;
1232     }
1233
1234     // 通常ダメージdam、武器重量weightでクリティカルが発生した時のクリティカルダメージ期待値
1235     auto calc_weight_expect_dam = [](int dam, WEIGHT weight) {
1236         int sum = 0;
1237         for (int d = 1; d <= 650; ++d) {
1238             int k = weight + d;
1239             sum += std::get<0>(apply_critical_norm_damage(k, dam));
1240         }
1241         return sum / 650;
1242     };
1243
1244     uint32_t num = 0;
1245
1246     if (impact) {
1247         for (int d = 1; d <= 650; ++d) {
1248             num += calc_weight_expect_dam(dam, weight + d);
1249         }
1250         num /= 650;
1251     } else {
1252         num += calc_weight_expect_dam(dam, weight);
1253     }
1254
1255     int pow = PlayerClass(player_ptr).equals(PlayerClassType::NINJA) ? 4444 : 5000;
1256     if (impact) {
1257         pow /= 2;
1258     }
1259
1260     num *= i;
1261     num += (pow - i) * dam;
1262     num /= pow;
1263
1264     return num;
1265 }