OSDN Git Service

[Refactor] #2204 HIT_POINTエイリアスをintに揃えた
[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/monster-damage.h"
40 #include "monster/monster-describer.h"
41 #include "monster/monster-info.h"
42 #include "monster/monster-status-setter.h"
43 #include "monster/monster-status.h"
44 #include "monster/monster-update.h"
45 #include "object/object-broken.h"
46 #include "object/object-flags.h"
47 #include "object/object-info.h"
48 #include "object/object-kind.h"
49 #include "object/object-mark-types.h"
50 #include "player-base/player-class.h"
51 #include "player-info/class-info.h"
52 #include "player-info/sniper-data-type.h"
53 #include "player-status/player-energy.h"
54 #include "player/player-personality-types.h"
55 #include "player/player-skill.h"
56 #include "player/player-status-table.h"
57 #include "sv-definition/sv-bow-types.h"
58 #include "system/artifact-type-definition.h"
59 #include "system/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, int 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)) && race_ptr->kind_flags.has(MonsterKindType::ANIMAL)) {
167             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
168                 race_ptr->r_kind_flags.set(MonsterKindType::ANIMAL);
169             }
170             if (mult < 17)
171                 mult = 17;
172         }
173
174         if ((flags.has(TR_KILL_ANIMAL)) && race_ptr->kind_flags.has(MonsterKindType::ANIMAL)) {
175             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
176                 race_ptr->r_kind_flags.set(MonsterKindType::ANIMAL);
177             }
178             if (mult < 27)
179                 mult = 27;
180         }
181
182         if ((flags.has(TR_SLAY_EVIL)) && race_ptr->kind_flags.has(MonsterKindType::EVIL)) {
183             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
184                 race_ptr->r_kind_flags.set(MonsterKindType::EVIL);
185             }
186             if (mult < 15)
187                 mult = 15;
188         }
189
190         if ((flags.has(TR_KILL_EVIL)) && race_ptr->kind_flags.has(MonsterKindType::EVIL)) {
191             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
192                 race_ptr->r_kind_flags.set(MonsterKindType::EVIL);
193             }
194             if (mult < 25)
195                 mult = 25;
196         }
197
198         if ((flags.has(TR_SLAY_GOOD)) && race_ptr->kind_flags.has(MonsterKindType::GOOD)) {
199             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
200                 race_ptr->r_kind_flags.set(MonsterKindType::GOOD);
201             }
202             if (mult < 15)
203                 mult = 15;
204         }
205
206         if ((flags.has(TR_KILL_GOOD)) && race_ptr->kind_flags.has(MonsterKindType::GOOD)) {
207             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
208                 race_ptr->r_kind_flags.set(MonsterKindType::GOOD);
209             }
210             if (mult < 25)
211                 mult = 25;
212         }
213
214         if ((flags.has(TR_SLAY_HUMAN)) && race_ptr->kind_flags.has(MonsterKindType::HUMAN)) {
215             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
216                 race_ptr->r_kind_flags.set(MonsterKindType::HUMAN);
217             }
218             if (mult < 17)
219                 mult = 17;
220         }
221
222         if ((flags.has(TR_KILL_HUMAN)) && race_ptr->kind_flags.has(MonsterKindType::HUMAN)) {
223             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
224                 race_ptr->r_kind_flags.set(MonsterKindType::HUMAN);
225             }
226             if (mult < 27)
227                 mult = 27;
228         }
229
230         if ((flags.has(TR_SLAY_UNDEAD)) && race_ptr->kind_flags.has(MonsterKindType::UNDEAD)) {
231             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
232                 race_ptr->r_kind_flags.set(MonsterKindType::UNDEAD);
233             }
234             if (mult < 20)
235                 mult = 20;
236         }
237
238         if ((flags.has(TR_KILL_UNDEAD)) && race_ptr->kind_flags.has(MonsterKindType::UNDEAD)) {
239             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
240                 race_ptr->r_kind_flags.set(MonsterKindType::UNDEAD);
241             }
242             if (mult < 30)
243                 mult = 30;
244         }
245
246         if ((flags.has(TR_SLAY_DEMON)) && race_ptr->kind_flags.has(MonsterKindType::DEMON)) {
247             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
248                 race_ptr->r_kind_flags.set(MonsterKindType::DEMON);
249             }
250             if (mult < 20)
251                 mult = 20;
252         }
253
254         if ((flags.has(TR_KILL_DEMON)) && race_ptr->kind_flags.has(MonsterKindType::DEMON)) {
255             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
256                 race_ptr->r_kind_flags.set(MonsterKindType::DEMON);
257             }
258             if (mult < 30)
259                 mult = 30;
260         }
261
262         if ((flags.has(TR_SLAY_ORC)) && race_ptr->kind_flags.has(MonsterKindType::ORC)) {
263             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
264                 race_ptr->r_kind_flags.set(MonsterKindType::ORC);
265             }
266             if (mult < 20)
267                 mult = 20;
268         }
269
270         if ((flags.has(TR_KILL_ORC)) && race_ptr->kind_flags.has(MonsterKindType::ORC)) {
271             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
272                 race_ptr->r_kind_flags.set(MonsterKindType::ORC);
273             }
274             if (mult < 30)
275                 mult = 30;
276         }
277
278         if ((flags.has(TR_SLAY_TROLL)) && race_ptr->kind_flags.has(MonsterKindType::TROLL)) {
279             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
280                 race_ptr->r_kind_flags.set(MonsterKindType::TROLL);
281             }
282
283             if (mult < 20)
284                 mult = 20;
285         }
286
287         if ((flags.has(TR_KILL_TROLL)) && race_ptr->kind_flags.has(MonsterKindType::TROLL)) {
288             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
289                 race_ptr->r_kind_flags.set(MonsterKindType::TROLL);
290             }
291             if (mult < 30)
292                 mult = 30;
293         }
294
295         if ((flags.has(TR_SLAY_GIANT)) && race_ptr->kind_flags.has(MonsterKindType::GIANT)) {
296             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
297                 race_ptr->r_kind_flags.set(MonsterKindType::GIANT);
298             }
299             if (mult < 20)
300                 mult = 20;
301         }
302
303         if ((flags.has(TR_KILL_GIANT)) && race_ptr->kind_flags.has(MonsterKindType::GIANT)) {
304             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
305                 race_ptr->r_kind_flags.set(MonsterKindType::GIANT);
306             }
307             if (mult < 30)
308                 mult = 30;
309         }
310
311         if ((flags.has(TR_SLAY_DRAGON)) && race_ptr->kind_flags.has(MonsterKindType::DRAGON)) {
312             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
313                 race_ptr->r_kind_flags.set(MonsterKindType::DRAGON);
314             }
315             if (mult < 20)
316                 mult = 20;
317         }
318
319         if ((flags.has(TR_KILL_DRAGON)) && race_ptr->kind_flags.has(MonsterKindType::DRAGON)) {
320             if (is_original_ap_and_seen(player_ptr, monster_ptr)) {
321                 race_ptr->r_kind_flags.set(MonsterKindType::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 (r_ptr->kind_flags.has_not(MonsterKindType::EVIL) || one_in_(5))
703                         chg_virtue(player_ptr, V_COMPASSION, -1);
704                     if (r_ptr->kind_flags.has_not(MonsterKindType::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) && r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE) && none_bits(r_ptr->flags7, RF7_UNIQUE2)) {
749                             GAME_TEXT m_name[MAX_NLEN];
750
751                             /* Get "the monster" or "it" */
752                             monster_desc(player_ptr, m_name, m_ptr, 0);
753
754                             tdam = m_ptr->hp + 1;
755                             base_dam = tdam;
756                             msg_format(_("%sの急所に突き刺さった!", "Your shot hit a fatal spot of %s!"), m_name);
757                         } else {
758                             tdam = 1;
759                             base_dam = tdam;
760                         }
761                     } else {
762
763                         attribute_flags = shot_attribute(player_ptr, j_ptr, q_ptr, snipe_type);
764                         /* Apply special damage */
765                         tdam = calc_shot_damage_with_slay(player_ptr, j_ptr, q_ptr, tdam, m_ptr, snipe_type);
766                         tdam = critical_shot(player_ptr, q_ptr->weight, q_ptr->to_h, j_ptr->to_h, tdam);
767
768                         /* No negative damage */
769                         if (tdam < 0)
770                             tdam = 0;
771
772                         /* Modify the damage */
773                         base_dam = tdam;
774                         tdam = mon_damage_mod(player_ptr, m_ptr, tdam, false);
775                     }
776
777                     msg_format_wizard(player_ptr, CHEAT_MONSTER, _("%dのダメージを与えた。(残りHP %d/%d(%d))", "You do %d damage. (left HP %d/%d(%d))"), tdam,
778                         m_ptr->hp - tdam, m_ptr->maxhp, m_ptr->max_maxhp);
779
780                     /* Sniper */
781                     if (snipe_type == SP_EXPLODE) {
782                         uint16_t flg = (PROJECT_STOP | PROJECT_JUMP | PROJECT_KILL | PROJECT_GRID);
783
784                         sound(SOUND_EXPLODE); /* No explode sound - use breath fire instead */
785                         project(player_ptr, 0, ((sniper_concent + 1) / 2 + 1), ny, nx, base_dam, AttributeType::MISSILE, flg);
786                         break;
787                     }
788
789                     /* Sniper */
790                     if (snipe_type == SP_HOLYNESS) {
791                         set_bits(player_ptr->current_floor_ptr->grid_array[ny][nx].info, CAVE_GLOW);
792                         note_spot(player_ptr, ny, nx);
793                         lite_spot(player_ptr, ny, nx);
794                     }
795
796                     /* Hit the monster, check for death */
797                     MonsterDamageProcessor mdp(player_ptr, c_mon_ptr->m_idx, tdam, &fear, attribute_flags);
798                     if (mdp.mon_take_hit(extract_note_dies(real_r_idx(m_ptr)))) {
799                         /* Dead monster */
800                     }
801
802                     /* No death */
803                     else {
804                         /* STICK TO */
805                         if (q_ptr->is_fixed_artifact() && (sniper_concent == 0)) {
806                             GAME_TEXT m_name[MAX_NLEN];
807
808                             monster_desc(player_ptr, m_name, m_ptr, 0);
809
810                             stick_to = true;
811                             msg_format(_("%sは%sに突き刺さった!", "%^s is stuck in %s!"), o_name, m_name);
812                         }
813
814                         message_pain(player_ptr, c_mon_ptr->m_idx, tdam);
815
816                         /* Anger the monster */
817                         if (tdam > 0)
818                             anger_monster(player_ptr, m_ptr);
819
820                         if (fear && m_ptr->ml) {
821                             GAME_TEXT m_name[MAX_NLEN];
822                             sound(SOUND_FLEE);
823                             monster_desc(player_ptr, m_name, m_ptr, 0);
824                             msg_format(_("%^sは恐怖して逃げ出した!", "%^s flees in terror!"), m_name);
825                         }
826
827                         set_target(m_ptr, player_ptr->y, player_ptr->x);
828
829                         /* Sniper */
830                         if (snipe_type == SP_RUSH) {
831                             int n = randint1(5) + 3;
832                             MONSTER_IDX m_idx = c_mon_ptr->m_idx;
833
834                             for (; cur_dis <= tdis;) {
835                                 POSITION ox = nx;
836                                 POSITION oy = ny;
837
838                                 if (!n)
839                                     break;
840
841                                 /* Calculate the new location (see "project()") */
842                                 mmove2(&ny, &nx, player_ptr->y, player_ptr->x, ty, tx);
843
844                                 /* Stopped by wilderness boundary */
845                                 if (!in_bounds2(player_ptr->current_floor_ptr, ny, nx))
846                                     break;
847
848                                 /* Stopped by walls/doors */
849                                 if (!player_can_enter(player_ptr, player_ptr->current_floor_ptr->grid_array[ny][nx].feat, 0))
850                                     break;
851
852                                 /* Stopped by monsters */
853                                 if (!is_cave_empty_bold(player_ptr, ny, nx))
854                                     break;
855
856                                 player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
857                                 player_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
858
859                                 m_ptr->fx = nx;
860                                 m_ptr->fy = ny;
861
862                                 update_monster(player_ptr, m_idx, true);
863
864                                 if (delay_factor > 0) {
865                                     lite_spot(player_ptr, ny, nx);
866                                     lite_spot(player_ptr, oy, ox);
867                                     term_fresh();
868                                     term_xtra(TERM_XTRA_DELAY, delay_factor);
869                                 }
870
871                                 x = nx;
872                                 y = ny;
873                                 cur_dis++;
874                                 n--;
875                             }
876                         }
877                     }
878                 }
879
880                 /* Sniper */
881                 if (snipe_type == SP_PIERCE && sniper_concent > 0) {
882                     sniper_concent--;
883                     continue;
884                 }
885
886                 /* Stop looking */
887                 break;
888             }
889         }
890
891         /* Chance of breakage (during attacks) */
892         j = (hit_body ? breakage_chance(player_ptr, q_ptr, PlayerClass(player_ptr).equals(PlayerClassType::ARCHER), snipe_type) : 0);
893
894         if (stick_to) {
895             MONSTER_IDX m_idx = player_ptr->current_floor_ptr->grid_array[y][x].m_idx;
896             auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
897             OBJECT_IDX o_idx = o_pop(player_ptr->current_floor_ptr);
898
899             if (!o_idx) {
900                 msg_format(_("%sはどこかへ行った。", "The %s went somewhere."), o_name);
901                 if (q_ptr->is_fixed_artifact()) {
902                     a_info[j_ptr->name1].cur_num = 0;
903                 }
904                 return;
905             }
906
907             o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
908             o_ptr->copy_from(q_ptr);
909
910             /* Forget mark */
911             reset_bits(o_ptr->marked, OM_TOUCHED);
912
913             /* Forget location */
914             o_ptr->iy = o_ptr->ix = 0;
915
916             /* Memorize monster */
917             o_ptr->held_m_idx = m_idx;
918
919             /* Carry object */
920             m_ptr->hold_o_idx_list.add(player_ptr->current_floor_ptr, o_idx);
921         } else if (cave_has_flag_bold(player_ptr->current_floor_ptr, y, x, FloorFeatureType::PROJECT)) {
922             /* Drop (or break) near that location */
923             (void)drop_near(player_ptr, q_ptr, j, y, x);
924         } else {
925             /* Drop (or break) near that location */
926             (void)drop_near(player_ptr, q_ptr, j, prev_y, prev_x);
927         }
928
929         /* Sniper - Repeat shooting when double shots */
930     }
931
932     /* Sniper - Loose his/her concentration after any shot */
933     reset_concentration(player_ptr, false);
934 }
935
936 /*!
937  * @brief プレイヤーからモンスターへの射撃命中判定 /
938  * Determine if the player "hits" a monster (normal combat).
939  * @param chance 基本命中値
940  * @param monster_ptr モンスターの構造体参照ポインタ
941  * @param vis 目標を視界に捕らえているならばTRUEを指定
942  * @param o_name メッセージ表示時のモンスター名
943  * @return 命中と判定された場合TRUEを返す
944  * @note Always miss 5%, always hit 5%, otherwise random.
945  */
946 bool test_hit_fire(PlayerType *player_ptr, int chance, monster_type *m_ptr, int vis, char *o_name)
947 {
948     int k;
949     ARMOUR_CLASS ac;
950     auto *r_ptr = &r_info[m_ptr->r_idx];
951
952     /* Percentile dice */
953     k = randint1(100);
954
955     auto sniper_data = PlayerClass(player_ptr).get_specific_data<sniper_data_type>();
956     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
957
958     /* Snipers with high-concentration reduce instant miss percentage.*/
959     k += sniper_concent;
960
961     /* Hack -- Instant miss or hit */
962     if (k <= 5)
963         return false;
964     if (k > 95)
965         return true;
966
967     if (player_ptr->ppersonality == PERSONALITY_LAZY)
968         if (one_in_(20))
969             return false;
970
971     /* Never hit */
972     if (chance <= 0)
973         return false;
974
975     ac = r_ptr->ac;
976     ac = ac * (8 - sniper_concent) / 8;
977
978     if (m_ptr->r_idx == MON_GOEMON && !monster_csleep_remaining(m_ptr))
979         ac *= 3;
980
981     /* Invisible monsters are harder to hit */
982     if (!vis)
983         chance = (chance + 1) / 2;
984
985     /* Power competes against armor */
986     if (randint0(chance) < (ac * 3 / 4)) {
987         if (m_ptr->r_idx == MON_GOEMON && !monster_csleep_remaining(m_ptr)) {
988             GAME_TEXT m_name[MAX_NLEN];
989             monster_desc(player_ptr, m_name, m_ptr, 0);
990             msg_format(_("%sは%sを斬り捨てた!", "%s cuts down %s!"), m_name, o_name);
991         }
992         return false;
993     }
994
995     /* Assume hit */
996     return true;
997 }
998
999 /*!
1000  * @brief プレイヤーからモンスターへの射撃クリティカル判定 /
1001  * Critical hits (from objects thrown by player) Factor in item weight, total plusses, and player level.
1002  * @param weight 矢弾の重量
1003  * @param plus_ammo 矢弾の命中修正
1004  * @param plus_bow 弓の命中修正
1005  * @param dam 現在算出中のダメージ値
1006  * @return クリティカル修正が入ったダメージ値
1007  */
1008 int critical_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1009 {
1010     int i, k;
1011     auto *j_ptr = &player_ptr->inventory_list[INVEN_BOW];
1012
1013     /* Extract "shot" power */
1014     i = player_ptr->to_h_b + plus_ammo;
1015
1016     if (player_ptr->tval_ammo == ItemKindType::BOLT)
1017         i = (player_ptr->skill_thb + (player_ptr->weapon_exp[j_ptr->tval][j_ptr->sval] / 400 + i) * BTH_PLUS_ADJ);
1018     else
1019         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);
1020
1021     PlayerClass pc(player_ptr);
1022     auto sniper_data = pc.get_specific_data<sniper_data_type>();
1023     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1024
1025     /* Snipers can shot more critically with crossbows */
1026     i += ((i * sniper_concent) / 5);
1027     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT))
1028         i *= 2;
1029
1030     /* Good bow makes more critical */
1031     i += plus_bow * 8 * (sniper_concent + 5);
1032
1033     /* Critical hit */
1034     if (randint1(10000) <= i) {
1035         k = weight * randint1(500);
1036
1037         if (k < 900) {
1038             msg_print(_("手ごたえがあった!", "It was a good hit!"));
1039             dam += (dam / 2);
1040         } else if (k < 1350) {
1041             msg_print(_("かなりの手ごたえがあった!", "It was a great hit!"));
1042             dam *= 2;
1043         } else {
1044             msg_print(_("会心の一撃だ!", "It was a superb hit!"));
1045             dam *= 3;
1046         }
1047     }
1048
1049     return dam;
1050 }
1051
1052 /*!
1053  * @brief 射撃武器の攻撃に必要な基本消費エネルギーを返す/Return bow energy
1054  * @param sval 射撃武器のアイテム副分類ID
1055  * @return 消費する基本エネルギー
1056  */
1057 ENERGY bow_energy(OBJECT_SUBTYPE_VALUE sval)
1058 {
1059     ENERGY energy = 10000;
1060
1061     /* Analyze the launcher */
1062     switch (sval) {
1063         /* Sling and ammo */
1064     case SV_SLING: {
1065         energy = 8000;
1066         break;
1067     }
1068
1069     /* Short Bow and Arrow */
1070     case SV_SHORT_BOW: {
1071         energy = 10000;
1072         break;
1073     }
1074
1075     /* Long Bow and Arrow */
1076     case SV_LONG_BOW: {
1077         energy = 10000;
1078         break;
1079     }
1080
1081     /* Bow of irresponsiblity and Arrow */
1082     case SV_NAMAKE_BOW: {
1083         energy = 7777;
1084         break;
1085     }
1086
1087     /* Light Crossbow and Bolt */
1088     case SV_LIGHT_XBOW: {
1089         energy = 12000;
1090         break;
1091     }
1092
1093     /* Heavy Crossbow and Bolt */
1094     case SV_HEAVY_XBOW: {
1095         energy = 13333;
1096         break;
1097     }
1098     }
1099
1100     return energy;
1101 }
1102
1103 /*
1104  * Return bow tmul
1105  */
1106 int bow_tmul(OBJECT_SUBTYPE_VALUE sval)
1107 {
1108     int tmul = 0;
1109
1110     /* Analyze the launcher */
1111     switch (sval) {
1112         /* Sling and ammo */
1113     case SV_SLING: {
1114         tmul = 2;
1115         break;
1116     }
1117
1118     /* Short Bow and Arrow */
1119     case SV_SHORT_BOW: {
1120         tmul = 2;
1121         break;
1122     }
1123
1124     /* Long Bow and Arrow */
1125     case SV_LONG_BOW: {
1126         tmul = 3;
1127         break;
1128     }
1129
1130     /* Bow of irresponsiblity and Arrow */
1131     case SV_NAMAKE_BOW: {
1132         tmul = 3;
1133         break;
1134     }
1135
1136     /* Light Crossbow and Bolt */
1137     case SV_LIGHT_XBOW: {
1138         tmul = 3;
1139         break;
1140     }
1141
1142     /* Heavy Crossbow and Bolt */
1143     case SV_HEAVY_XBOW: {
1144         tmul = 4;
1145         break;
1146     }
1147     }
1148
1149     return tmul;
1150 }
1151
1152 /*!
1153  * @brief 射撃時クリティカルによるダメージ期待値修正計算(スナイパーの集中処理と武器経験値) / critical happens at i / 10000
1154  * @param player_ptr プレイヤーへの参照ポインタ
1155  * @param plus_ammo 矢弾のダメージ修正
1156  * @param plus_bow 弓のダメージ修正
1157  * @return ダメージ期待値
1158  * @note 基本ダメージ量と重量はこの部位では計算に加わらない。
1159  */
1160 int calc_crit_ratio_shot(PlayerType *player_ptr, int plus_ammo, int plus_bow)
1161 {
1162     int i;
1163     auto *j_ptr = &player_ptr->inventory_list[INVEN_BOW];
1164
1165     /* Extract "shot" power */
1166     i = player_ptr->to_h_b + plus_ammo;
1167
1168     if (player_ptr->tval_ammo == ItemKindType::BOLT)
1169         i = (player_ptr->skill_thb + (player_ptr->weapon_exp[j_ptr->tval][j_ptr->sval] / 400 + i) * BTH_PLUS_ADJ);
1170     else
1171         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);
1172
1173     PlayerClass pc(player_ptr);
1174     auto sniper_data = pc.get_specific_data<sniper_data_type>();
1175     auto sniper_concent = sniper_data ? sniper_data->concent : 0;
1176
1177     /* Snipers can shot more critically with crossbows */
1178     i += ((i * sniper_concent) / 5);
1179     if (pc.equals(PlayerClassType::SNIPER) && (player_ptr->tval_ammo == ItemKindType::BOLT))
1180         i *= 2;
1181
1182     /* Good bow makes more critical */
1183     i += plus_bow * 8 * (sniper_concent + 5);
1184
1185     if (i < 0)
1186         i = 0;
1187
1188     return i;
1189 }
1190
1191 /*!
1192  * @brief 射撃時クリティカルによるダメージ期待値修正計算(重量依存部分) / critical happens at i / 10000
1193  * @param weight 武器の重量
1194  * @param plus_ammo 矢弾のダメージ修正
1195  * @param plus_bow 弓のダメージ修正
1196  * @param dam 基本ダメージ量
1197  * @return ダメージ期待値
1198  */
1199 int calc_expect_crit_shot(PlayerType *player_ptr, WEIGHT weight, int plus_ammo, int plus_bow, int dam)
1200 {
1201     uint32_t num;
1202     int i, k, crit;
1203     i = calc_crit_ratio_shot(player_ptr, plus_ammo, plus_bow);
1204
1205     k = 0;
1206     num = 0;
1207
1208     crit = std::min(500, 900 / weight);
1209     num += dam * 3 / 2 * crit;
1210     k = crit;
1211
1212     crit = std::min(500, 1350 / weight);
1213     crit -= k;
1214     num += dam * 2 * crit;
1215     k += crit;
1216
1217     if (k < 500) {
1218         crit = 500 - k;
1219         num += dam * 3 * crit;
1220     }
1221
1222     num /= 500;
1223
1224     num *= i;
1225     num += (10000 - i) * dam;
1226     num /= 10000;
1227
1228     return num;
1229 }
1230
1231 /*!
1232  * @brief 攻撃時クリティカルによるダメージ期待値修正計算(重量と毒針処理) / critical happens at i / 10000
1233  * @param player_ptr プレイヤーへの参照ポインタ
1234  * @param weight 武器の重量
1235  * @param plus 武器のダメージ修正
1236  * @param dam 基本ダメージ
1237  * @param meichuu 命中値
1238  * @param dokubari 毒針処理か否か
1239  * @param impact 強撃かどうか
1240  * @return ダメージ期待値
1241  */
1242 int calc_expect_crit(PlayerType *player_ptr, WEIGHT weight, int plus, int dam, int16_t meichuu, bool dokubari, bool impact)
1243 {
1244     if (dokubari)
1245         return dam;
1246
1247     int i = (weight + (meichuu * 3 + plus * 5) + player_ptr->skill_thn);
1248     if (i < 0)
1249         i = 0;
1250
1251     // 通常ダメージdam、武器重量weightでクリティカルが発生した時のクリティカルダメージ期待値
1252     auto calc_weight_expect_dam = [](int dam, WEIGHT weight) {
1253         int sum = 0;
1254         for (int d = 1; d <= 650; ++d) {
1255             int k = weight + d;
1256             sum += std::get<0>(apply_critical_norm_damage(k, dam));
1257         }
1258         return sum / 650;
1259     };
1260
1261     uint32_t num = 0;
1262
1263     if (impact) {
1264         for (int d = 1; d <= 650; ++d) {
1265             num += calc_weight_expect_dam(dam, weight + d);
1266         }
1267         num /= 650;
1268     } else {
1269         num += calc_weight_expect_dam(dam, weight);
1270     }
1271
1272     int pow = PlayerClass(player_ptr).equals(PlayerClassType::NINJA) ? 4444 : 5000;
1273     if (impact)
1274         pow /= 2;
1275
1276     num *= i;
1277     num += (pow - i) * dam;
1278     num /= pow;
1279
1280     return num;
1281 }