OSDN Git Service

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