OSDN Git Service

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