OSDN Git Service

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