OSDN Git Service

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