forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
melee.cpp
2499 lines (2148 loc) · 91.6 KB
/
melee.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "melee.h"
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "avatar.h"
#include "avatar_functions.h"
#include "bodypart.h"
#include "bionics.h"
#include "cached_options.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "character_functions.h"
#include "character_martial_arts.h"
#include "creature.h"
#include "damage.h"
#include "debug.h"
#include "enums.h"
#include "game.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_location.h"
#include "itype.h"
#include "line.h"
#include "magic_enchantment.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "martialarts.h"
#include "messages.h"
#include "monattack.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "npc.h"
#include "output.h"
#include "player.h"
#include "pldata.h"
#include "point.h"
#include "projectile.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "translations.h"
#include "type_id.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "weighted_list.h"
static const bionic_id bio_cqb( "bio_cqb" );
static const bionic_id bio_memory( "bio_memory" );
static const itype_id itype_fur( "fur" );
static const itype_id itype_leather( "leather" );
static const itype_id itype_rag( "rag" );
static const matec_id tec_none( "tec_none" );
static const matec_id WBLOCK_1( "WBLOCK_1" );
static const matec_id WBLOCK_2( "WBLOCK_2" );
static const matec_id WBLOCK_3( "WBLOCK_3" );
static const skill_id skill_stabbing( "stabbing" );
static const skill_id skill_cutting( "cutting" );
static const skill_id skill_unarmed( "unarmed" );
static const skill_id skill_bashing( "bashing" );
static const skill_id skill_melee( "melee" );
static const efftype_id effect_badpoison( "badpoison" );
static const efftype_id effect_beartrap( "beartrap" );
static const efftype_id effect_bouldering( "bouldering" );
static const efftype_id effect_contacts( "contacts" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_drunk( "drunk" );
static const efftype_id effect_grabbed( "grabbed" );
static const efftype_id effect_grabbing( "grabbing" );
static const efftype_id effect_heavysnare( "heavysnare" );
static const efftype_id effect_hit_by_player( "hit_by_player" );
static const efftype_id effect_lightsnare( "lightsnare" );
static const efftype_id effect_narcosis( "narcosis" );
static const efftype_id effect_poison( "poison" );
static const efftype_id effect_stunned( "stunned" );
static const trait_id trait_ARM_TENTACLES( "ARM_TENTACLES" );
static const trait_id trait_ARM_TENTACLES_4( "ARM_TENTACLES_4" );
static const trait_id trait_ARM_TENTACLES_8( "ARM_TENTACLES_8" );
static const trait_id trait_BEAK_PECK( "BEAK_PECK" );
static const trait_id trait_CLAWS_TENTACLE( "CLAWS_TENTACLE" );
static const trait_id trait_DEBUG_NIGHTVISION( "DEBUG_NIGHTVISION" );
static const trait_id trait_DEFT( "DEFT" );
static const trait_id trait_DRUNKEN( "DRUNKEN" );
static const trait_id trait_HYPEROPIC( "HYPEROPIC" );
static const trait_id trait_POISONOUS2( "POISONOUS2" );
static const trait_id trait_POISONOUS( "POISONOUS" );
static const trait_id trait_PROF_SKATER( "PROF_SKATER" );
static const trait_id trait_VINES2( "VINES2" );
static const trait_id trait_VINES3( "VINES3" );
static const efftype_id effect_amigara( "amigara" );
static const species_id HUMAN( "HUMAN" );
void player_hit_message( Character *attacker, const std::string &message,
Creature &t, int dam, bool crit = false );
int stumble( Character &u, const item &weap );
std::string melee_message( const ma_technique &tec, Character &p,
const dealt_damage_instance &ddi );
/* Melee Functions!
* These all belong to class player.
*
* STATE QUERIES
* bool is_armed() - True if we are armed with any item.
* bool unarmed_attack() - True if we are attacking with a "fist" weapon
* (cestus, bionic claws etc.) or no weapon.
*
* HIT DETERMINATION
* int hit_roll() - The player's hit roll, to be compared to a monster's or
* player's dodge_roll(). This handles weapon bonuses, weapon-specific
* skills, torso encumbrance penalties and drunken master bonuses.
*/
const item &Character::used_weapon() const
{
return martial_arts_data->selected_force_unarmed() ? null_item_reference() : primary_weapon();
}
item &Character::used_weapon()
{
return const_cast<item &>( const_cast<const Character *>( this )->used_weapon() );
}
const item &Character::primary_weapon() const
{
return weapon;
}
item &Character::primary_weapon()
{
return const_cast<item &>( const_cast<const Character *>( this )->primary_weapon() );
}
std::vector<item *> Character::wielded_items()
{
if( !weapon.is_null() ) {
return {&weapon};
}
return {};
}
std::vector<const item *> Character::wielded_items() const
{
if( !weapon.is_null() ) {
return {&weapon};
}
return {};
}
bool Character::is_armed() const
{
return !primary_weapon().is_null();
}
bool Character::unarmed_attack() const
{
const item &weap = used_weapon();
return weap.is_null() || weap.has_flag( "UNARMED_WEAPON" );
}
bool Character::handle_melee_wear( item &shield, float wear_multiplier )
{
if( wear_multiplier <= 0.0f ) {
return false;
}
// Here is where we handle wear and tear on things we use as melee weapons or shields.
if( shield.is_null() ) {
return false;
}
// UNBREAKABLE_MELEE items can't be damaged through melee combat usage.
if( shield.has_flag( "UNBREAKABLE_MELEE" ) ) {
return false;
}
/** @EFFECT_DEX reduces chance of damaging your melee weapon */
/** @EFFECT_STR increases chance of damaging your melee weapon (NEGATIVE) */
/** @EFFECT_MELEE reduces chance of damaging your melee weapon */
const float stat_factor = dex_cur / 2.0f
+ get_skill_level( skill_melee )
+ ( 64.0f / std::max( str_cur, 4 ) );
float material_factor;
itype_id weak_comp;
itype_id big_comp = itype_id::NULL_ID();
// Fragile items that fall apart easily when used as a weapon due to poor construction quality
if( shield.has_flag( "FRAGILE_MELEE" ) ) {
const float fragile_factor = 6;
int weak_chip = INT_MAX;
units::volume big_vol = 0_ml;
// Items that should have no bearing on durability
const std::set<itype_id> blacklist = { itype_rag, itype_leather, itype_fur };
for( auto &comp : shield.components ) {
if( blacklist.count( comp.typeId() ) <= 0 ) {
if( weak_chip > comp.chip_resistance() ) {
weak_chip = comp.chip_resistance();
weak_comp = comp.typeId();
}
}
if( comp.volume() > big_vol ) {
big_vol = comp.volume();
big_comp = comp.typeId();
}
}
material_factor = ( weak_chip < INT_MAX ? weak_chip : shield.chip_resistance() ) / fragile_factor;
} else {
material_factor = shield.chip_resistance();
}
int damage_chance = static_cast<int>( stat_factor * material_factor / wear_multiplier );
// DURABLE_MELEE items are made to hit stuff and they do it well, so they're considered to be a lot tougher
// than other weapons made of the same materials.
if( shield.has_flag( "DURABLE_MELEE" ) ) {
damage_chance *= 4;
}
if( damage_chance > 0 && !one_in( damage_chance ) ) {
return false;
}
auto str = shield.tname(); // save name before we apply damage
if( !shield.inc_damage() ) {
add_msg_player_or_npc( m_bad, _( "Your %s is damaged by the force of the blow!" ),
_( "<npcname>'s %s is damaged by the force of the blow!" ),
str );
return false;
}
// Dump its contents on the ground
// Destroy irremovable mods, if any
for( auto mod : shield.gunmods() ) {
if( mod->is_irremovable() ) {
remove_item( *mod );
}
}
// Preserve item temporarily for component breakdown
item temp = shield;
shield.contents.spill_contents( pos() );
remove_item( shield );
// Breakdown fragile weapons into components
if( temp.has_flag( "FRAGILE_MELEE" ) && !temp.components.empty() ) {
add_msg_player_or_npc( m_bad, _( "Your %s breaks apart!" ),
_( "<npcname>'s %s breaks apart!" ),
str );
for( auto &comp : temp.components ) {
int break_chance = comp.typeId() == weak_comp ? 2 : 8;
if( one_in( break_chance ) ) {
add_msg_if_player( m_bad, _( "The %s is destroyed!" ), comp.tname() );
continue;
}
if( comp.typeId() == big_comp && !is_armed() ) {
wield( comp );
} else {
g->m.add_item_or_charges( pos(), comp );
}
}
} else {
add_msg_player_or_npc( m_bad, _( "Your %s is destroyed by the blow!" ),
_( "<npcname>'s %s is destroyed by the blow!" ),
str );
}
return true;
}
float Character::get_hit_weapon( const item &weap ) const
{
/** @EFFECT_UNARMED improves hit chance for unarmed weapons */
/** @EFFECT_BASHING improves hit chance for bashing weapons */
/** @EFFECT_CUTTING improves hit chance for cutting weapons */
/** @EFFECT_STABBING improves hit chance for piercing weapons */
auto skill = get_skill_level( weap.melee_skill() );
// CQB bionic acts as a lower bound providing item uses a weapon skill
if( skill < BIO_CQB_LEVEL && has_active_bionic( bio_cqb ) ) {
skill = BIO_CQB_LEVEL;
}
/** @EFFECT_MELEE improves hit chance for all items (including non-weapons) */
return ( skill / 3.0f ) + ( get_skill_level( skill_melee ) / 2.0f ) + weap.type->m_to_hit;
}
float Character::get_melee_hit_base() const
{
// Character::get_hit_base includes stat calculations already
return Character::get_hit_base() + get_hit_weapon( used_weapon() ) + mabuff_tohit_bonus();
}
float Character::hit_roll() const
{
// Dexterity, skills, weapon and martial arts
float hit = get_melee_hit_base();
// Farsightedness makes us hit worse
if( has_trait( trait_HYPEROPIC ) && !worn_with_flag( "FIX_FARSIGHT" ) &&
!has_effect( effect_contacts ) ) {
hit -= 2.0f;
}
//Unstable ground chance of failure
if( has_effect( effect_bouldering ) ) {
hit *= 0.75f;
}
hit *= std::max( 0.25f, 1.0f - encumb( bp_torso ) / 100.0f );
return melee::melee_hit_range( hit );
}
void Character::add_miss_reason( const std::string &reason, const unsigned int weight )
{
melee_miss_reasons.add( reason, weight );
}
void Character::clear_miss_reasons()
{
melee_miss_reasons.clear();
}
std::string Character::get_miss_reason()
{
// everything that lowers accuracy in player::hit_roll()
// adding it in hit_roll() might not be safe if it's called multiple times
// in one turn
add_miss_reason(
_( "Your torso encumbrance throws you off-balance." ),
roll_remainder( encumb( bp_torso ) / 10.0 ) );
const int farsightedness = 2 * ( has_trait( trait_HYPEROPIC ) &&
!worn_with_flag( "FIX_FARSIGHT" ) &&
!has_effect( effect_contacts ) );
add_miss_reason(
_( "You can't hit reliably due to your farsightedness." ),
farsightedness );
const std::string *const reason = melee_miss_reasons.pick();
if( reason == nullptr ) {
return std::string();
}
return *reason;
}
void Character::roll_all_damage( bool crit, damage_instance &di, bool average,
const item &weap ) const
{
roll_bash_damage( crit, di, average, weap );
roll_cut_damage( crit, di, average, weap );
roll_stab_damage( crit, di, average, weap );
}
static void melee_train( Character &p, int lo, int hi, const item &weap )
{
player &u = *p.as_player();
u.practice( skill_melee, std::ceil( rng( lo, hi ) / 2.0 ), hi );
// allocate XP proportional to damage stats
// Pure unarmed needs a special case because it has 0 weapon damage
int cut = weap.damage_melee( DT_CUT );
int stab = weap.damage_melee( DT_STAB );
int bash = weap.damage_melee( DT_BASH ) + ( weap.is_null() ? 1 : 0 );
float total = std::max( cut + stab + bash, 1 );
// Unarmed may deal cut, stab, and bash damage depending on the weapon
if( weap.is_unarmed_weapon() ) {
u.practice( skill_unarmed, std::ceil( 1 * rng( lo, hi ) ), hi );
} else {
u.practice( skill_cutting, std::ceil( cut / total * rng( lo, hi ) ), hi );
u.practice( skill_stabbing, std::ceil( stab / total * rng( lo, hi ) ), hi );
u.practice( skill_bashing, std::ceil( bash / total * rng( lo, hi ) ), hi );
}
}
// Melee calculation is in parts. This sets up the attack, then in deal_melee_attack,
// we calculate if we would hit. In Creature::deal_melee_hit, we calculate if the target dodges.
void Character::melee_attack( Creature &t, bool allow_special, const matec_id *force_technique,
bool allow_unarmed )
{
melee::melee_stats.attack_count += 1;
int hit_spread = t.deal_melee_attack( this, hit_roll() );
if( !t.is_player() ) {
// TODO: Per-NPC tracking? Right now monster hit by either npc or player will draw aggro...
t.add_effect( effect_hit_by_player, 10_minutes ); // Flag as attacked by us for AI
}
if( is_mounted() ) {
auto mons = mounted_creature.get();
if( mons->has_flag( MF_RIDEABLE_MECH ) ) {
if( !mons->check_mech_powered() ) {
add_msg( m_bad, _( "The %s has dead batteries and will not move its arms." ),
mons->get_name() );
return;
}
if( mons->type->has_special_attack( "SMASH" ) && one_in( 3 ) ) {
add_msg( m_info, _( "The %s hisses as its hydraulic arm pumps forward!" ),
mons->get_name() );
mattack::smash_specific( mons, &t );
} else {
mons->use_mech_power( -2 );
mons->melee_attack( t );
}
mod_moves( -mons->type->attack_cost );
return;
}
}
item &cur_weapon = allow_unarmed ? used_weapon() : primary_weapon();
if( cur_weapon.attack_cost() > attack_cost( cur_weapon ) * 20 ) {
add_msg( m_bad, _( "This weapon is too unwieldy to attack with!" ) );
return;
}
int move_cost = attack_cost( cur_weapon );
if( hit_spread < 0 ) {
int stumble_pen = stumble( *this, cur_weapon );
sfx::generate_melee_sound( pos(), t.pos(), false, false );
if( is_player() ) { // Only display messages if this is the player
if( one_in( 2 ) ) {
const std::string reason_for_miss = get_miss_reason();
if( !reason_for_miss.empty() ) {
add_msg( reason_for_miss );
}
}
if( can_miss_recovery( cur_weapon ) ) {
ma_technique tec = martial_arts_data->get_miss_recovery_tec( cur_weapon );
add_msg( _( tec.avatar_message ), t.disp_name() );
} else if( stumble_pen >= 60 ) {
add_msg( m_bad, _( "You miss and stumble with the momentum." ) );
} else if( stumble_pen >= 10 ) {
add_msg( _( "You swing wildly and miss." ) );
} else {
add_msg( _( "You miss." ) );
}
} else if( g->u.sees( *this ) ) {
if( stumble_pen >= 60 ) {
add_msg( _( "%s misses and stumbles with the momentum." ), name );
} else if( stumble_pen >= 10 ) {
add_msg( _( "%s swings wildly and misses." ), name );
} else {
add_msg( _( "%s misses." ), name );
}
}
// Practice melee and relevant weapon skill (if any) except when using CQB bionic
if( !has_active_bionic( bio_cqb ) ) {
melee_train( *this, 2, 5, cur_weapon );
}
// Cap stumble penalty, heavy weapons are quite weak already
move_cost += std::min( 60, stumble_pen );
if( martial_arts_data->has_miss_recovery_tec( cur_weapon ) ) {
move_cost /= 2;
}
// trigger martial arts on-miss effects
martial_arts_data->ma_onmiss_effects( *this );
} else {
melee::melee_stats.hit_count += 1;
// Remember if we see the monster at start - it may change
const bool seen = g->u.sees( t );
// Start of attacks.
const bool critical_hit = scored_crit( t.dodge_roll(), cur_weapon );
if( critical_hit ) {
melee::melee_stats.actual_crit_count += 1;
}
damage_instance d;
roll_all_damage( critical_hit, d, false, cur_weapon );
const bool has_force_technique = force_technique;
// Pick one or more special attacks
matec_id technique_id;
if( allow_special && !has_force_technique ) {
technique_id = pick_technique( t, cur_weapon, critical_hit, false, false );
} else if( has_force_technique ) {
technique_id = *force_technique;
} else {
technique_id = tec_none;
}
// if you have two broken arms you aren't doing any martial arts
// and your hits are not going to hurt very much
if( get_working_arm_count() < 1 ) {
technique_id = tec_none;
d.mult_damage( 0.1 );
}
// polearms and pikes (but not spears) do less damage to adjacent targets
if( cur_weapon.reach_range( *this ) > 1 && !reach_attacking &&
cur_weapon.has_flag( "POLEARM" ) ) {
d.mult_damage( 0.7 );
}
const ma_technique &technique = technique_id.obj();
// Handles effects as well; not done in melee_affect_*
if( technique.id != tec_none ) {
perform_technique( technique, t, d, move_cost );
}
// Proceed with melee attack.
if( !t.is_dead_state() ) {
// Handles speed penalties to monster & us, etc
std::string specialmsg = melee_special_effects( t, d, cur_weapon );
// gets overwritten with the dealt damage values
dealt_damage_instance dealt_dam;
dealt_damage_instance dealt_special_dam;
if( allow_special ) {
perform_special_attacks( t, dealt_special_dam );
}
t.deal_melee_hit( this, hit_spread, critical_hit, d, dealt_dam );
if( dealt_special_dam.type_damage( DT_CUT ) > 0 ||
dealt_special_dam.type_damage( DT_STAB ) > 0 ||
( cur_weapon.is_null() && ( dealt_dam.type_damage( DT_CUT ) > 0 ||
dealt_dam.type_damage( DT_STAB ) > 0 ) ) ) {
if( has_trait( trait_POISONOUS ) ) {
add_msg_if_player( m_good, _( "You poison %s!" ), t.disp_name() );
t.add_effect( effect_poison, 6_turns );
} else if( has_trait( trait_POISONOUS2 ) ) {
add_msg_if_player( m_good, _( "You inject your venom into %s!" ),
t.disp_name() );
t.add_effect( effect_badpoison, 6_turns );
}
}
// Make a rather quiet sound, to alert any nearby monsters
if( !is_quiet() ) { // check martial arts silence
//sound generated later
sounds::sound( pos(), 8, sounds::sound_t::combat, "whack!" );
}
std::string material = "flesh";
if( t.is_monster() ) {
const monster *m = dynamic_cast<const monster *>( &t );
if( m->made_of( material_id( "steel" ) ) ) {
material = "steel";
}
}
sfx::generate_melee_sound( pos(), t.pos(), true, t.is_monster(), material );
int dam = dealt_dam.total_damage();
melee::melee_stats.damage_amount += dam;
// Practice melee and relevant weapon skill (if any) except when using CQB bionic
if( !has_active_bionic( bio_cqb ) ) {
melee_train( *this, 5, 10, cur_weapon );
}
if( dam >= 5 && has_artifact_with( AEP_SAP_LIFE ) ) {
healall( rng( dam / 10, dam / 5 ) );
}
// Treat monster as seen if we see it before or after the attack
if( seen || g->u.sees( t ) ) {
std::string message = melee_message( technique, *this, dealt_dam );
player_hit_message( this, message, t, dam, critical_hit );
} else {
add_msg_player_or_npc( m_good, _( "You hit something." ),
_( "<npcname> hits something." ) );
}
if( !specialmsg.empty() ) {
add_msg_if_player( m_neutral, specialmsg );
}
if( critical_hit ) {
// trigger martial arts on-crit effects
martial_arts_data->ma_oncrit_effects( *this );
}
}
t.check_dead_state();
did_hit( t );
if( t.is_dead_state() ) {
// trigger martial arts on-kill effects
martial_arts_data->ma_onkill_effects( *this );
}
}
const int melee = get_skill_level( skill_melee );
// Previously calculated as 2_gram * std::max( 1, str_cur )
// using 16_gram normalizes it to 8 str. Same effort expenditure
// for each strike, regardless of weight. This is compensated
// for by the additional move cost as weapon weight increases
const int weight_cost = cur_weapon.weight() / ( 16_gram );
const int encumbrance_cost = roll_remainder( ( encumb( bp_arm_l ) + encumb( bp_arm_r ) ) *
2.0f );
const int deft_bonus = hit_spread < 0 && has_trait( trait_DEFT ) ? 50 : 0;
const float skill_cost = std::max( 0.667f, static_cast<float>( ( 30.0f - melee ) / 30.0f ) );
/** @EFFECT_MELEE reduces stamina cost of melee attacks */
const int mod_sta = ( weight_cost + encumbrance_cost - deft_bonus + 50 ) * -1 * skill_cost;
mod_stamina( std::min( -50, mod_sta ) );
add_msg( m_debug, "Stamina burn: %d", std::min( -50, mod_sta ) );
mod_moves( -move_cost );
// trigger martial arts on-attack effects
martial_arts_data->ma_onattack_effects( *this );
// some things (shattering weapons) can harm the attacking creature.
check_dead_state();
if( t.as_character() ) {
dealt_projectile_attack dp = dealt_projectile_attack();
t.as_character()->on_hit( this, bodypart_id( "num_bp" ), &dp );
}
return;
}
void Character::reach_attack( const tripoint &p )
{
matec_id force_technique = tec_none;
/** @EFFECT_MELEE >5 allows WHIP_DISARM technique */
if( primary_weapon().has_flag( "WHIP" ) && ( get_skill_level( skill_melee ) > 5 ) && one_in( 3 ) ) {
force_technique = matec_id( "WHIP_DISARM" );
}
map &here = get_map();
Creature *critter = g->critter_at( p );
// Original target size, used when there are monsters in front of our target
int target_size = critter != nullptr ? ( critter->get_size() + 1 ) : 2;
// Reset last target pos
as_player()->last_target_pos = std::nullopt;
// Max out recoil
recoil = MAX_RECOIL;
int move_cost = attack_cost( primary_weapon() );
int skill = std::min( 10, get_skill_level( skill_stabbing ) );
int t = 0;
std::vector<tripoint> path = line_to( pos(), p, t, 0 );
tripoint last_point = pos();
path.pop_back(); // Last point is our critter
for( const tripoint &path_point : path ) {
// Possibly hit some unintended target instead
Creature *inter = g->critter_at( path_point );
int inter_block_size = inter != nullptr ? ( inter->get_size() + 1 ) : 2;
/** @EFFECT_STABBING decreases chance of hitting intervening target on reach attack */
if( inter != nullptr &&
!x_in_y( ( target_size * target_size + 1 ) * skill,
( inter_block_size * inter_block_size + 1 ) * 10 ) ) {
// Even if we miss here, low roll means weapon is pushed away or something like that
critter = inter;
break;
} else if( here.obstructed_by_vehicle_rotation( last_point, path_point ) ) {
tripoint rand = path_point;
if( one_in( 2 ) ) {
rand.x = last_point.x;
} else {
rand.y = last_point.y;
}
here.bash( rand, str_cur + primary_weapon().damage_melee( DT_BASH ) );
handle_melee_wear( primary_weapon() );
mod_moves( -move_cost );
return;
/** @EFFECT_STABBING increases ability to reach attack through fences */
} else if( here.impassable( path_point ) &&
// Fences etc. Spears can stab through those
!( primary_weapon().has_flag( "SPEAR" ) &&
g->m.has_flag( "THIN_OBSTACLE", path_point ) &&
x_in_y( skill, 10 ) ) ) {
/** @EFFECT_STR increases bash effects when reach attacking past something */
here.bash( path_point, str_cur + primary_weapon().damage_melee( DT_BASH ) );
handle_melee_wear( primary_weapon() );
mod_moves( -move_cost );
return;
}
last_point = path_point;
}
if( here.obstructed_by_vehicle_rotation( last_point, p ) ) {
tripoint rand = p;
if( one_in( 2 ) ) {
rand.x = last_point.x;
} else {
rand.y = last_point.y;
}
here.bash( rand, str_cur + primary_weapon().damage_melee( DT_BASH ) );
handle_melee_wear( primary_weapon() );
mod_moves( -move_cost );
return;
}
if( critter == nullptr ) {
add_msg_if_player( _( "You swing at the air." ) );
if( martial_arts_data->has_miss_recovery_tec( primary_weapon() ) ) {
move_cost /= 3; // "Probing" is faster than a regular miss
}
mod_moves( -move_cost );
return;
}
reach_attacking = true;
melee_attack( *critter, false, &force_technique, false );
reach_attacking = false;
}
int stumble( Character &u, const item &weap )
{
if( u.has_trait( trait_DEFT ) ) {
return 0;
}
// Examples:
// 10 str with a hatchet: 4 + 8 = 12
// 5 str with a battle axe: 26 + 49 = 75
// Fist: 0
/** @EFFECT_STR reduces chance of stumbling with heavier weapons */
return ( weap.volume() / 125_ml ) +
( weap.weight() / ( u.str_cur * 10_gram + 13.0_gram ) );
}
bool Character::scored_crit( float target_dodge, const item &weap ) const
{
return rng_float( 0, 1.0 ) < crit_chance( hit_roll(), target_dodge, weap );
}
/**
* Limits a probability to be between 0.0 and 1.0
*/
inline double limit_probability( double unbounded_probability )
{
return std::max( std::min( unbounded_probability, 1.0 ), 0.0 );
}
double Character::crit_chance( float roll_hit, float target_dodge, const item &weap ) const
{
// Weapon to-hit roll
double weapon_crit_chance = 0.5;
if( weap.is_unarmed_weapon() ) {
// Unarmed attack: 1/2 of unarmed skill is to-hit
/** @EFFECT_UNARMED increases critical chance with UNARMED_WEAPON */
weapon_crit_chance = 0.5 + 0.05 * get_skill_level( skill_unarmed );
}
if( weap.type->m_to_hit > 0 ) {
weapon_crit_chance = std::max( weapon_crit_chance, 0.5 + 0.1 * weap.type->m_to_hit );
} else if( weap.type->m_to_hit < 0 ) {
weapon_crit_chance += 0.1 * weap.type->m_to_hit;
}
weapon_crit_chance = limit_probability( weapon_crit_chance );
// Dexterity and perception
/** @EFFECT_DEX increases chance for critical hits */
/** @EFFECT_PER increases chance for critical hits */
const double stat_crit_chance = limit_probability( 0.25 + 0.01 * dex_cur + ( 0.02 * per_cur ) );
/** @EFFECT_BASHING increases critical chance with bashing weapons */
/** @EFFECT_CUTTING increases critical chance with cutting weapons */
/** @EFFECT_STABBING increases critical chance with piercing weapons */
/** @EFFECT_UNARMED increases critical chance with unarmed weapons */
int sk = get_skill_level( weap.melee_skill() );
if( has_active_bionic( bio_cqb ) ) {
sk = std::max( sk, BIO_CQB_LEVEL );
}
/** @EFFECT_MELEE slightly increases critical chance with any item */
sk += get_skill_level( skill_melee ) / 2.5;
const double skill_crit_chance = limit_probability( 0.25 + sk * 0.025 );
// Examples (survivor stats/chances of each critical):
// Fresh (skill-less) 8/8/8/8, unarmed:
// 50%, 49%, 25%; ~1/16 guaranteed critical + ~1/8 if roll>dodge*1.5
// Expert (skills 10) 10/10/10/10, unarmed:
// 100%, 55%, 60%; ~1/3 guaranteed critical + ~4/10 if roll>dodge*1.5
// Godlike with combat CBM 20/20/20/20, pipe (+1 accuracy):
// 60%, 100%, 42%; ~1/4 guaranteed critical + ~3/8 if roll>dodge*1.5
// Note: the formulas below are only valid if none of the 3 critical chance values go above 1.0
// It is therefore important to limit them to between 0.0 and 1.0
// Chance to get all 3 criticals (a guaranteed critical regardless of hit/dodge)
const double chance_triple = weapon_crit_chance * stat_crit_chance * skill_crit_chance;
// Only check double critical (one that requires hit/dodge comparison) if we have good
// hit vs dodge
if( roll_hit > target_dodge * 3 / 2 ) {
const double chance_double = 0.5 * (
weapon_crit_chance * stat_crit_chance +
stat_crit_chance * skill_crit_chance +
weapon_crit_chance * skill_crit_chance -
( 3 * chance_triple ) );
// Because chance_double already removed the triples with -( 3 * chance_triple ),
// chance_triple and chance_double are mutually exclusive probabilities and can just
// be added together.
melee::melee_stats.double_crit_count += 1;
melee::melee_stats.double_crit_chance += chance_double + chance_triple;
return chance_triple + chance_double;
}
melee::melee_stats.crit_count += 1;
melee::melee_stats.crit_chance += chance_triple;
return chance_triple;
}
float Character::get_dodge() const
{
//If we're asleep or busy we can't dodge
if( in_sleep_state() || has_effect( effect_narcosis ) ) {
return 0.0f;
}
float ret = Creature::get_dodge();
// Chop in half if we are unable to move
if( has_effect( effect_beartrap ) || has_effect( effect_lightsnare ) ||
has_effect( effect_heavysnare ) ) {
ret /= 2;
}
if( has_effect( effect_grabbed ) ) {
int zed_number = 0;
for( auto &dest : g->m.points_in_radius( pos(), 1, 0 ) ) {
const monster *const mon = g->critter_at<monster>( dest );
if( mon && mon->has_effect( effect_grabbing ) ) {
zed_number++;
}
}
ret *= 1.0f - ( 0.25f * zed_number );
}
if( worn_with_flag( "ROLLER_INLINE" ) ||
worn_with_flag( "ROLLER_QUAD" ) ||
worn_with_flag( "ROLLER_ONE" ) ) {
ret /= has_trait( trait_PROF_SKATER ) ? 2 : 5;
}
if( has_effect( effect_bouldering ) ) {
ret /= 4;
}
// Each dodge after the first subtracts equivalent of 2 points of dodge skill
if( dodges_left <= 0 ) {
ret += dodges_left * 2 - 2;
}
return std::max( 0.0f, ret );
}
float Character::dodge_roll()
{
return get_dodge() * 5;
}
float Character::get_melee() const
{
return get_skill_level( skill_id( "melee" ) );
}
float Character::bonus_damage( bool random ) const
{
/** @EFFECT_STR increases bashing damage */
if( random ) {
return rng_float( get_str() / 2.0f, get_str() );
}
return get_str() * 0.75f;
}
void Character::roll_bash_damage( bool crit, damage_instance &di, bool average,
const item &weap ) const
{
float bash_dam = 0.0f;
const bool unarmed = weap.is_unarmed_weapon();
int skill = get_skill_level( unarmed ? skill_unarmed : skill_bashing );
if( has_active_bionic( bio_cqb ) ) {
skill = BIO_CQB_LEVEL;
}
const int stat = get_str();
/** @EFFECT_STR increases bashing damage */
float stat_bonus = bonus_damage( !average );
stat_bonus += mabuff_damage_bonus( DT_BASH );
// Drunken Master damage bonuses
if( has_trait( trait_DRUNKEN ) && has_effect( effect_drunk ) ) {
// Remember, a single drink gives 600 levels of "drunk"
int mindrunk = 0;
int maxdrunk = 0;
const time_duration drunk_dur = get_effect_dur( effect_drunk );
if( unarmed ) {
mindrunk = drunk_dur / 1_hours;
maxdrunk = drunk_dur / 25_minutes;
} else {
mindrunk = drunk_dur / 90_minutes;
maxdrunk = drunk_dur / 40_minutes;
}
bash_dam += average ? ( mindrunk + maxdrunk ) * 0.5f : rng( mindrunk, maxdrunk );
}
if( unarmed ) {
const bool left_empty = !natural_attack_restricted_on( bodypart_id( "hand_l" ) );
const bool right_empty = !natural_attack_restricted_on( bodypart_id( "hand_r" ) ) &&
weap.is_null();
if( left_empty || right_empty ) {
float per_hand = 0.0f;
for( const trait_id &mut : get_mutations() ) {
if( mut->flags.count( "NEED_ACTIVE_TO_MELEE" ) > 0 && !has_active_mutation( mut ) ) {
continue;
}
float unarmed_bonus = 0.0f;
const int bash_bonus = mut->bash_dmg_bonus;
if( mut->flags.count( "UNARMED_BONUS" ) > 0 && bash_bonus > 0 ) {
unarmed_bonus += std::min( get_skill_level( skill_unarmed ) / 2, 4 );
}
per_hand += bash_bonus + unarmed_bonus;
const std::pair<int, int> rand_bash = mut->rand_bash_bonus;
per_hand += average ? ( rand_bash.first + rand_bash.second ) / 2.0f : rng( rand_bash.first,
rand_bash.second );
}
bash_dam += per_hand; // First hand
if( left_empty && right_empty ) {
// Second hand
bash_dam += per_hand;
}
}
}
/** @EFFECT_STR increases bashing damage */
float weap_dam = weap.damage_melee( DT_BASH ) + stat_bonus;
/** @EFFECT_UNARMED caps bash damage with unarmed weapons */
/** @EFFECT_BASHING caps bash damage with bashing weapons */
float bash_cap = 2 * stat + 2 * skill;
float bash_mul = 1.0f;
// 80%, 88%, 96%, 104%, 112%, 116%, 120%, 124%, 128%, 132%
if( skill < 5 ) {
bash_mul = 0.8 + 0.08 * skill;
} else {
bash_mul = 0.96 + 0.04 * skill;
}
if( bash_cap < weap_dam && !weap.is_null() ) {
// If damage goes over cap due to low stats/skills,
// scale the post-armor damage down halfway between damage and cap
bash_mul *= ( 1.0f + ( bash_cap / weap_dam ) ) / 2.0f;
}
/** @EFFECT_STR boosts low cap on bashing damage */
const float low_cap = std::min( 1.0f, stat / 20.0f );
const float bash_min = low_cap * weap_dam;
weap_dam = average ? ( bash_min + weap_dam ) * 0.5f : rng_float( bash_min, weap_dam );
bash_dam += weap_dam;
bash_mul *= mabuff_damage_mult( DT_BASH );
float armor_mult = 1.0f;
int arpen = mabuff_arpen_bonus( DT_BASH );
// Finally, extra critical effects
if( crit ) {
bash_mul *= 1.5f;
// 50% armor penetration
armor_mult = 0.5f;
}