OSDN Git Service

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