OSDN Git Service

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