forked from Whales/Cataclysm
-
Notifications
You must be signed in to change notification settings - Fork 3
/
melee.cpp
1362 lines (1141 loc) · 44.8 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 "player.h"
#include "game.h"
#include "rng.h"
#include "line.h"
#include "recent_msg.h"
#include "stl_limits.h"
#include <sstream>
#include <stdlib.h>
/* Melee Functions!
* These all belong to class player.
*
* STATE QUERIES
* bool is_armed() - True if we are armed with any weapon.
* bool unarmed_attack() - True if we are NOT armed with any weapon, but still
* true if we're wielding a bionic weapon (at this point, just itm_bio_claws).
*
* HIT DETERMINATION
* int base_to_hit() - The base number of sides we get in hit_roll().
* Dexterity / 2 + sk_melee
* 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 encumberment penalties and drunken master bonuses.
*/
bool player::is_armed() const
{
return (weapon.type->id != 0 && !weapon.is_style());
}
bool player::unarmed_attack() const
{ // 2021-12-08 all styles have the IF_UNARMED_WEAPON set in their constructor
return (weapon.type->id == 0 || /* weapon.is_style() || */
weapon.has_flag(IF_UNARMED_WEAPON));
}
int player::base_to_hit(bool real_life, int stat) const
{
if (stat == -999) stat = (real_life ? dex_cur : dex_max);
return 1 + int(stat / 2) + sklevel[sk_melee];
}
int player::hit_roll() const
{
int stat = dex_cur;
// Some martial arts use something else to determine hits!
switch (weapon.type->id) {
case itm_style_tiger:
stat = (str_cur * 2 + dex_cur) / 3;
break;
case itm_style_leopard:
stat = (per_cur + int_cur + dex_cur * 2) / 4;
break;
case itm_style_snake:
stat = (per_cur + dex_cur) / 2;
break;
}
int numdice = base_to_hit(stat) + weapon.type->m_to_hit + disease_intensity(DI_ATTACK_BOOST);
int sides = 10 - encumb(bp_torso);
int best_bonus = 0;
if (sides < 2) sides = 2;
// Are we unarmed?
if (unarmed_attack()) {
best_bonus = sklevel[sk_unarmed];
if (sklevel[sk_unarmed] > 4) best_bonus += sklevel[sk_unarmed] - 4; // Extra bonus for high levels
}
// Using a bashing weapon?
if (weapon.is_bashing_weapon()) {
int bash_bonus = int(sklevel[sk_bashing] / 3);
if (bash_bonus > best_bonus) best_bonus = bash_bonus;
}
// Using a cutting weapon?
if (weapon.is_cutting_weapon()) {
int cut_bonus = int(sklevel[sk_cutting] / 2);
if (cut_bonus > best_bonus) best_bonus = cut_bonus;
}
// Using a spear?
if (weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB)) {
int stab_bonus = int(sklevel[sk_stabbing] / 2);
if (stab_bonus > best_bonus) best_bonus = stab_bonus;
}
numdice += best_bonus; // Use whichever bonus is best.
// Drunken master makes us hit better
if (has_trait(PF_DRUNKEN)) {
if (const auto drunk = disease_level(DI_DRUNK)) {
if (unarmed_attack())
numdice += drunk / MINUTES(30);
else
numdice += drunk / MINUTES(40);
}
}
if (numdice < 1) {
numdice = 1;
sides = 8 - encumb(bp_torso); // can result in sides < 1...problematic
}
return dice(numdice, sides);
}
static std::string melee_verb(technique_id tech, std::string your, player& p, int bash_dam, int cut_dam, int stab_dam)
{
if (tech) {
if (const auto style = p.weapon.is_style()) {
if (const auto m = style->data(tech)) return p.regular_verb_agreement(m->name);
}
}
std::ostringstream ret;
switch (tech) {
case TEC_SWEEP:
ret << p.regular_verb_agreement("sweep") << " " << your << " " << p.weapon.tname() << " at";
break;
case TEC_PRECISE:
ret << p.regular_verb_agreement("jab") << " " << your << " " << p.weapon.tname() << " at";
break;
case TEC_BRUTAL:
ret << p.regular_verb_agreement("slam") << " " << your << " " << p.weapon.tname() << " against";
break;
case TEC_GRAB:
ret << p.regular_verb_agreement("wrap") << " " << your << " " << p.weapon.tname() << " around";
break;
case TEC_WIDE:
ret << p.regular_verb_agreement("swing") << " " << your << " " << p.weapon.tname() << " wide at";
break;
case TEC_THROW:
ret << p.regular_verb_agreement("use") << " " << your << " " << p.weapon.tname() << " to toss";
break;
default: // No tech, so check our damage levels
if (bash_dam >= cut_dam && bash_dam >= stab_dam) {
if (bash_dam >= 30) return p.regular_verb_agreement("clobber");
if (bash_dam >= 20) return p.regular_verb_agreement("batter");
if (bash_dam >= 10) return p.regular_verb_agreement("whack");
return p.regular_verb_agreement("hit");
}
if (cut_dam >= stab_dam) {
if (cut_dam >= 30) return p.regular_verb_agreement("hack");
if (cut_dam >= 20) return p.regular_verb_agreement("slice");
if (cut_dam >= 10) return p.regular_verb_agreement("cut");
return p.regular_verb_agreement("nick");
}
// Only stab damage is left
if (stab_dam >= 30) return p.regular_verb_agreement("impale");
if (stab_dam >= 20) return p.regular_verb_agreement("pierce");
if (stab_dam >= 10) return p.regular_verb_agreement("stab");
return p.regular_verb_agreement("poke");
} // switch (tech)
return ret.str();
}
static void hit_message(std::string subject, std::string verb, std::string target, int dam, bool crit)
{
if (dam <= 0)
messages.add("%s %s %s but do%s no damage.", subject.c_str(), verb.c_str(),
target.c_str(), (subject == "You" ? "" : "es"));
else
messages.add("%s%s %s %s for %d damage.", (crit ? "Critical! " : ""),
subject.c_str(), verb.c_str(), target.c_str(), dam);
}
static int attack_speed(const player& u, bool missed)
{
int move_cost = u.weapon.attack_time() + 20 * u.encumb(bp_torso);
if (auto weak = u.has_light_bones()) move_cost *= mutation_branch::light_bones_attack_tus[weak - 1];
return clamped_lb<25>(move_cost - u.disease_intensity(DI_SPEED_BOOST));
}
// could micro-optimize based on call graph, but this is not CPU-intensive
static void melee_practice(player& u, bool hit, bool unarmed, bool bashing, bool cutting, bool stabbing)
{
if (!hit) {
u.practice(sk_melee, rng(2, 5));
if (unarmed) u.practice(sk_unarmed, 2);
if (bashing) u.practice(sk_bashing, 2);
if (cutting) u.practice(sk_cutting, 2);
if (stabbing) u.practice(sk_stabbing, 2);
} else {
u.practice(sk_melee, rng(5, 10));
if (unarmed) u.practice(sk_unarmed, rng(5, 10));
if (bashing) u.practice(sk_bashing, rng(5, 10));
if (cutting) u.practice(sk_cutting, rng(5, 10));
if (stabbing) u.practice(sk_stabbing, rng(5, 10));
}
}
/// <returns>move point cost to deduct</returns>
static int stumble(player& u)
{
int stumble_pen = 2 * u.weapon.volume() + u.weapon.weight();
if (u.has_trait(PF_DEFT)) stumble_pen = int(stumble_pen * .3) - 10;
if (stumble_pen < 0) stumble_pen = 0;
// TODO: Reflect high strength bonus in newcharacter.cpp
if (stumble_pen > 0 && (u.str_cur >= 15 || u.dex_cur >= 21 ||
one_in(16 - u.str_cur) || one_in(22 - u.dex_cur)))
stumble_pen = rng(0, stumble_pen);
return stumble_pen;
}
// PC-only
static auto interpret_miss(const player& u, int cost)
{
if (u.weapon.has_technique(TEC_FEINT, &u)) return "You feint.";
else if (60 <= cost) return "You miss and stumble with the momentum.";
else if (10 <= cost) return "You swing wildly and miss.";
return "You miss.";
}
static int roll_stuck_penalty(const player& u, const monster* z, bool stabbing)
{
int ret = 0;
const int basharm = (z ? z->armor_bash() : 6);
const int cutarm = (z ? z->armor_cut() : 6);
const int cutdam = u.weapon.damage_cut();
if (stabbing)
ret = cutdam * 3 + basharm * 3 + cutarm * 3 - dice(u.sklevel[sk_stabbing], 10);
else
ret = cutdam * 4 + basharm * 5 + cutarm * 4 - dice(u.sklevel[sk_cutting], 10);
if (const int ub = cutdam * 10; ret >= ub) return ub;
return (ret < 0 ? 0 : ret);
}
int player::hit_mon(game *g, monster *z, bool allow_grab) // defaults to true
{
bool is_u = (this == &(g->u)); // Affects how we'll display messages
if (is_u)
z->add_effect(ME_HIT_BY_PLAYER, 100); // Flag as attacked by us
const bool can_see = (is_u || g->u.see(pos)); // XXX this non-use suggests this function is never called by npcs
// If !allow_grab, then we already grabbed them--meaning their dodge is hampered
int mondodge = (allow_grab ? z->dodge_roll() : z->dodge_roll() / 3);
bool missed = (hit_roll() < mondodge ||
one_in(4 + dex_cur + weapon.type->m_to_hit));
int move_cost = attack_speed(*this, missed);
if (missed) {
int stumble_pen = stumble(*this);
if (is_u) messages.add(interpret_miss(*this, stumble_pen)); // Only display messages if this is the player
melee_practice(*this, false, unarmed_attack(),
weapon.is_bashing_weapon(), weapon.is_cutting_weapon(),
(weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB)));
move_cost += stumble_pen;
if (weapon.has_technique(TEC_FEINT, this)) move_cost = rng(move_cost / 3, move_cost);
moves -= move_cost;
return 0;
}
moves -= move_cost;
bool critical_hit = scored_crit(mondodge);
int bash_dam = roll_bash_damage(z, critical_hit);
int cut_dam = roll_cut_damage(z, critical_hit);
int stab_dam = roll_stab_damage(z, critical_hit);
int pain = 0; // Boost to pain; required for perform_technique
// Moves lost to getting your weapon stuck
const int stuck_penalty = weapon.is_style() ? 0 : roll_stuck_penalty(*this, z, (stab_dam >= cut_dam));
// Pick one or more special attacks
technique_id technique = pick_technique(z, nullptr, critical_hit, allow_grab);
// Handles effects as well; not done in melee_affect_*
perform_technique(technique, g, z, nullptr, bash_dam, cut_dam, stab_dam, pain);
z->speed -= int(pain / 2);
// Mutation-based attacks
perform_special_attacks(g, z, nullptr, bash_dam, cut_dam, stab_dam);
// Handles speed penalties to monster & us, etc
melee_special_effects(g, z, nullptr, critical_hit, bash_dam, cut_dam, stab_dam);
// Make a rather quiet sound, to alert any nearby monsters
if (weapon.type->id != itm_style_ninjutsu) g->sound(pos, 8, ""); // Ninjutsu is silent!
const std::string your = pronoun(grammar::noun::role::possessive);
const std::string verb = melee_verb(technique, your, *this, bash_dam, cut_dam, stab_dam);
int dam = bash_dam + (cut_dam > stab_dam ? cut_dam : stab_dam);
const std::string You = grammar::capitalize(subject());
const std::string target = z->desc(grammar::noun::role::direct_object, grammar::article::definite);
hit_message(You.c_str(), verb.c_str(), target.c_str(), dam, critical_hit);
bool bashing = (bash_dam >= 10 && !unarmed_attack());
bool cutting = (cut_dam >= 10 && cut_dam >= stab_dam);
bool stabbing = (stab_dam >= 10 && stab_dam >= cut_dam);
melee_practice(*this, true, unarmed_attack(), bashing, cutting, stabbing);
if (allow_grab && technique == TEC_GRAB) {
// Move our weapon to a temp slot, if it's not unarmed
if (!unarmed_attack()) {
item tmpweap = unwield();
dam += hit_mon(g, z, false); // False means a second grab isn't allowed
weapon = std::move(tmpweap);
} else
dam += hit_mon(g, z, false); // False means a second grab isn't allowed
}
if (dam >= 5 && has_artifact_with(AEP_SAP_LIFE)) healall( rng(dam / 10, dam / 5) );
return dam;
}
void player::hit_player(game *g, player &p, bool allow_grab)
{
const bool is_u = (this == &(g->u)); // Affects how we'll display messages
const bool can_see = (is_u || g->u.see(pos));
if (is_u && p.is_npc()) dynamic_cast<npc&>(p).make_angry();
// Divide their dodge roll by 2 if this is a grab
int target_dodge = p.dodge_roll()/(allow_grab ? 1 : 2);
bool missed = (hit_roll() <= 0);
int move_cost = attack_speed(*this, missed);
if (missed) {
int stumble_pen = stumble(*this);
if (is_u) messages.add(interpret_miss(*this, stumble_pen)); // Only display messages if this is the player
melee_practice(*this, false, unarmed_attack(),
weapon.is_bashing_weapon(), weapon.is_cutting_weapon(),
(weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB)));
move_cost += stumble_pen;
if (weapon.has_technique(TEC_FEINT, this))
move_cost = rng(move_cost / 3, move_cost);
moves -= move_cost;
return;
}
moves -= move_cost;
static auto hit_this = [&]() {
int hit_value = hit_roll() - target_dodge + rng(-10, 10);
if (hit_value >= 30) return bp_eyes;
else if (hit_value >= 20) return bp_head;
else if (hit_value >= 10) return bp_torso;
else if (one_in(4)) return bp_legs;
else return bp_arms;
};
body_part bp_hit = hit_this();
int side = rng(0, 1);
bool critical_hit = scored_crit(target_dodge);
int bash_dam = roll_bash_damage(nullptr, critical_hit);
int cut_dam = roll_cut_damage(nullptr, critical_hit);
int stab_dam = roll_stab_damage(nullptr, critical_hit);
technique_id tech_def = p.pick_defensive_technique(nullptr, this);
p.perform_defensive_technique(tech_def, g, nullptr, this, bp_hit, side,
bash_dam, cut_dam, stab_dam);
if (bash_dam + cut_dam + stab_dam <= 0) return; // Defensive technique canceled our attack!
if (critical_hit) // Crits cancel out Toad Style's armor boost
p.rem_disease(DI_ARMOR_BOOST);
int pain = 0; // Boost to pain; required for perform_technique
// Moves lost to getting your weapon stuck
const int stuck_penalty = weapon.is_style() ? 0 : roll_stuck_penalty(*this, nullptr, (stab_dam >= cut_dam));
// Pick one or more special attacks
technique_id technique = pick_technique(nullptr, &p, critical_hit, allow_grab);
// Handles effects as well; not done in melee_affect_*
perform_technique(technique, g, nullptr, &p, bash_dam, cut_dam, stab_dam, pain);
p.pain += pain;
// Mutation-based attacks
perform_special_attacks(g, nullptr, &p, bash_dam, cut_dam, stab_dam);
// Handles speed penalties to monster & us, etc
melee_special_effects(g, nullptr, &p, critical_hit, bash_dam, cut_dam, stab_dam);
// Make a rather quiet sound, to alert any nearby monsters
if (weapon.type->id != itm_style_ninjutsu) // Ninjutsu is silent!
g->sound(pos, 8, "");
p.hit(g, bp_hit, side, bash_dam, (cut_dam > stab_dam ? cut_dam : stab_dam));
const std::string your = pronoun(grammar::noun::role::possessive);
const std::string verb = melee_verb(technique, your, *this, bash_dam, cut_dam, stab_dam);
int dam = bash_dam + (cut_dam > stab_dam ? cut_dam : stab_dam);
const std::string You = grammar::capitalize(subject());
const std::string target = p.possessive() + " " + body_part_name(bp_hit, side);
hit_message(You.c_str(), verb.c_str(), target.c_str(), dam, critical_hit);
bool bashing = (bash_dam >= 10 && !unarmed_attack());
bool cutting = (cut_dam >= 10 && cut_dam >= stab_dam);
bool stabbing = (stab_dam >= 10 && stab_dam >= cut_dam);
melee_practice(*this, true, unarmed_attack(), bashing, cutting, stabbing);
if (dam >= 5 && has_artifact_with(AEP_SAP_LIFE)) healall( rng(dam / 10, dam / 5) );
if (allow_grab && technique == TEC_GRAB) {
// Move our weapon to a temp slot, if it's not unarmed
if (p.weapon.has_technique(TEC_BREAK, &p) &&
dice(p.melee_skill(), 12) > dice(melee_skill(), 10)) {
if (is_u)
messages.add("%s %s the grab!", target.c_str(), p.regular_verb_agreement("break").c_str());
} else if (!unarmed_attack()) {
item tmpweap = unwield();
hit_player(g, p, false); // False means a second grab isn't allowed
weapon = std::move(tmpweap);
} else
hit_player(g, p, false); // False means a second grab isn't allowed
}
if (tech_def == TEC_COUNTER) {
p.subjective_message("Counter-attack!");
p.hit_player(g, *this);
}
}
bool player::scored_crit(int target_dodge) const
{
bool to_hit_crit = false, dex_crit = false, skill_crit = false;
int num_crits = 0;
// Weapon to-hit roll
int chance = 25;
if (unarmed_attack()) { // Unarmed attack: 1/2 of unarmed skill is to-hit
for (int i = 1; i <= int(sklevel[sk_unarmed] * .5); i++)
chance += (50 / (2 + i));
}
if (weapon.type->m_to_hit > 0) {
for (int i = 1; i <= weapon.type->m_to_hit; i++)
chance += (50 / (2 + i));
} else if (chance < 0) {
for (int i = 0; i > weapon.type->m_to_hit; i--)
chance /= 2;
}
if (rng(0, 99) < chance + 4 * disease_intensity(DI_ATTACK_BOOST)) num_crits++;
// Dexterity to-hit roll
// ... except sometimes we don't use dexterity!
int stat = dex_cur;
// Some martial arts use something else to determine hits!
switch (weapon.type->id) {
case itm_style_tiger:
stat = (str_cur * 2 + dex_cur) / 3;
break;
case itm_style_leopard:
stat = (per_cur + int_cur + dex_cur * 2) / 4;
break;
case itm_style_snake:
stat = (per_cur + dex_cur) / 2;
break;
}
chance = 25;
if (stat > 8) {
for (int i = 9; i <= stat; i++) // max design stat 20 (pathological behavior above this)
chance += (21 - i); // 12, 11, 10...
} else {
int decrease = 5;
for (int i = 7; i >= stat; i--) {
chance -= decrease;
if (i % 2 == 0) decrease--;
}
}
if (rng(0, 99) < chance) num_crits++;
// Skill level roll
int best_skill = 0;
if (weapon.is_bashing_weapon() && sklevel[sk_bashing] > best_skill) best_skill = sklevel[sk_bashing];
if (weapon.is_cutting_weapon() && sklevel[sk_cutting] > best_skill) best_skill = sklevel[sk_cutting];
if ((weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB)) && sklevel[sk_stabbing] > best_skill) best_skill = sklevel[sk_stabbing];
if (unarmed_attack() && sklevel[sk_unarmed] > best_skill) best_skill = sklevel[sk_unarmed];
best_skill += int(sklevel[sk_melee] / 2.5);
chance = 25;
if (best_skill > 3) {
for (int i = 3; i < best_skill; i++)
chance += (50 / (2 + i));
} else if (chance < 3) {
for (int i = 3; i > best_skill; i--)
chance /= 2;
}
if (rng(0, 99) < chance + 4 * disease_intensity(DI_ATTACK_BOOST)) num_crits++;
if (num_crits == 3) return true;
else if (num_crits == 2) return (hit_roll() >= target_dodge * 1.5 && !one_in(4));
return false;
}
int player::dodge() const
{
if (has_disease(DI_SLEEP) || has_disease(DI_LYING_DOWN)) return 0;
if (activity.type != ACT_NULL) return 0;
int ret = 4 + (dex_cur / 2);
ret += sklevel[sk_dodge];
ret += disease_intensity(DI_DODGE_BOOST);
ret -= (encumb(bp_legs) / 2) + encumb(bp_torso);
ret += current_speed() / ((mobile::mp_turn/2)*3);
if (has_trait(PF_TAIL_LONG)) ret += 4;
if (has_trait(PF_TAIL_FLUFFY)) ret += 8;
if (has_trait(PF_WHISKERS)) ret += 1;
if (has_trait(PF_WINGS_BAT)) ret -= 3;
if (str_max >= 16) ret--; // Penalty if we're huge
else if (str_max <= 5) ret++; // Bonus if we're small
if (dodges_left <= 0) { // We already dodged this turn
if (rng(1, sklevel[sk_dodge] + dex_cur + 15) <= sklevel[sk_dodge] + dex_cur)
ret = rng(0, ret);
else
ret = 0;
}
dodges_left--;
// If we're over our cap, average it with our cap
const int cap = dex_cur / 2 + sklevel[sk_dodge] * 2;
if (ret > cap) ret = ( ret + cap) / 2;
return ret;
}
int player::dodge_roll() const { return dice(dodge(), 6); }
int player::base_damage(bool real_life, int stat) const
{
if (stat == -999)
stat = (real_life ? str_cur : str_max);
int dam = (real_life ? rng(0, stat / 2) : stat / 2);
// Bonus for statong characters
if (stat > 10)
dam += int((stat - 9) / 2);
// Big bonus for super-human characters
if (stat > 20)
dam += int((stat - 20) * 1.5);
return dam;
}
int player::roll_bash_damage(const monster *z, bool crit) const
{
int ret = 0;
int stat = str_cur; // Which stat determines damage?
int skill = sklevel[unarmed_attack() ? sk_unarmed : sk_bashing];
switch (weapon.type->id) { // Some martial arts change which stat
case itm_style_crane:
stat = (dex_cur * 2 + str_cur) / 3;
break;
case itm_style_snake:
stat = int(str_cur + per_cur) / 2;
break;
case itm_style_dragon:
stat = int(str_cur + int_cur) / 2;
break;
}
ret = base_damage(true, stat);
// Drunken Master damage bonuses
if (has_trait(PF_DRUNKEN)) {
if (const auto drunk = disease_level(DI_DRUNK)) { // zero if doesn't have this "disease"
// Remember, a single drink gives 1 hour's levels of DI_DRUNK
int mindrunk, maxdrunk;
if (unarmed_attack()) {
mindrunk = drunk / MINUTES(60);
maxdrunk = drunk / MINUTES(25);
} else {
mindrunk = drunk / MINUTES(90);
maxdrunk = drunk / MINUTES(40);
}
ret += rng(mindrunk, maxdrunk);
}
}
int bash_dam = int(stat / 2) + weapon.damage_bash(),
bash_cap = 5 + stat + skill;
if (unarmed_attack())
bash_dam = rng(0, int(stat / 2) + sklevel[sk_unarmed]);
if (crit) {
cataclysm::rational_scale<3,2>(bash_dam);
bash_cap *= 2;
}
if (bash_dam > bash_cap)// Cap for weak characters
bash_dam = (bash_cap * 3 + bash_dam) / 4;
if (is<MF_PLASTIC>(z)) bash_dam /= rng(2, 4);
int bash_min = bash_dam / 4;
bash_dam = rng(bash_min, bash_dam);
if (bash_dam < skill + int(stat / 2))
bash_dam = rng(bash_dam, skill + int(stat / 2));
ret += bash_dam;
ret += disease_intensity(DI_DAMAGE_BOOST);
// Finally, extra crit effects
if (crit) {
ret += int(stat / 2);
ret += skill;
}
if (z) ret -= z->armor_bash();
return (ret < 0 ? 0 : ret);
}
int player::roll_cut_damage(const monster *z, bool crit) const
{
if (weapon.has_flag(IF_SPEAR)) return 0; // Stabs, doesn't cut!
int z_armor_cut = z ? z->armor_cut() - sklevel[sk_cutting] / 2 : 0;
if (crit) z_armor_cut /= 2;
if (z_armor_cut < 0) z_armor_cut = 0;
int ret = weapon.damage_cut() - z_armor_cut;
if (unarmed_attack() && !wearing_something_on(bp_hands)) {
if (has_trait(PF_CLAWS)) ret += 6;
if (has_trait(PF_TALONS)) ret += 6 + (sklevel[sk_unarmed] > 8 ? 8 : sklevel[sk_unarmed]);
if (has_trait(PF_SLIME_HANDS) && !is<MF_ACIDPROOF>(z)) ret += rng(4, 6);
}
if (ret <= 0) return 0; // No negative damage!
// 80%, 88%, 96%, 104%, 112%, 116%, 120%, 124%, 128%, 132%
if (sklevel[sk_cutting] <= 5)
ret += ret * (2 * sklevel[sk_cutting] - 5) / 25;
else
ret += ret * (sklevel[sk_cutting] - 2) / 25;
if (crit) ret += ret * sklevel[sk_cutting] / 12;
return ret;
}
int player::roll_stab_damage(const monster *z, bool crit) const
{
int ret = 0;
int z_armor = (z == nullptr ? 0 : z->armor_cut() - 3 * sklevel[sk_stabbing]);
if (crit) z_armor /= 3;
if (z_armor < 0) z_armor = 0;
if (unarmed_attack() && !wearing_something_on(bp_hands)) {
ret = 0 - z_armor;
if (has_trait(PF_CLAWS)) ret += 6;
if (has_trait(PF_NAILS) && z_armor == 0) ret++;
if (has_trait(PF_THORNS)) ret += 4;
} else if (weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB))
ret = int((weapon.damage_cut() - z_armor) / 4);
else
return 0; // Can't stab at all!
if (z != nullptr && z->speed > 100) { // Bonus against fast monsters
int speed_min = (z->speed - 100) / 10, speed_max = (z->speed - 100) / 5;
int speed_dam = rng(speed_min, speed_max);
if (speed_dam > ret * 2) speed_dam = ret * 2;
if (speed_dam > 0) ret += speed_dam;
}
if (ret <= 0) return 0; // No negative stabbing!
if (crit) {
int numerator = 10 + 2 * sklevel[sk_stabbing];
if (25 < numerator) numerator = 25;
ret += (ret*(numerator - 10)) / 10;
}
return ret;
}
technique_id player::pick_technique(const monster *z, const player *p, bool crit, bool allowgrab) const
{
if (z == nullptr && p == nullptr) return TEC_NULL; // \todo error condition?
const mobile* const mob = z ? static_cast<const mobile*>(z) : p;
const bool downed = mob->has(effect::DOWNED);
const int base_str_req = mob->min_technique_power();
std::vector<technique_id> possible;
if (allowgrab) { // Check if grabs AREN'T REALLY ALLOWED
if (is<MF_PLASTIC>(z)) allowgrab = false;
}
if (crit) { // Some are crit-only
if (weapon.has_technique(TEC_SWEEP, this) && !is<MF_FLIES>(z) && !downed)
possible.push_back(TEC_SWEEP);
if (weapon.has_technique(TEC_PRECISE, this)) possible.push_back(TEC_PRECISE);
if (weapon.has_technique(TEC_BRUTAL, this) && !downed &&
str_cur + sklevel[sk_melee] >= 4 + base_str_req)
possible.push_back(TEC_BRUTAL);
}
const auto g = game::active();
if (possible.empty()) { // Use non-crits only if any crit-onlies aren't used
if (weapon.has_technique(TEC_DISARM, this) && !z && !p->unarmed_attack() &&
dice(dex_cur + sklevel[sk_unarmed], 8) > dice(p->melee_skill(), 10))
possible.push_back(TEC_DISARM);
if (weapon.has_technique(TEC_GRAB, this) && allowgrab) possible.push_back(TEC_GRAB);
if (weapon.has_technique(TEC_RAPID, this)) possible.push_back(TEC_RAPID);
if (weapon.has_technique(TEC_THROW, this) && !downed &&
str_cur + sklevel[sk_melee] >= 4 + base_str_req * 4 + rng(-4, 4))
possible.push_back(TEC_THROW);
if (weapon.has_technique(TEC_WIDE, this)) { // Count monsters
int enemy_count = 0;
for (decltype(auto) delta : Direction::vector) {
if (auto mob = g->mob_at(GPSpos + delta)) {
if (std::visit(player::is_enemy_of(*this), *mob)) enemy_count++;
else enemy_count -= 2;
}
}
if (enemy_count >= (possible.empty() ? 2 : 3)) possible.push_back(TEC_WIDE);
}
} // if (possible.empty())
if (possible.empty()) return TEC_NULL;
possible.push_back(TEC_NULL); // Always a chance to not use any technique
return possible[rng(0, possible.size() - 1)];
}
struct tech_wide_secondary_hit
{
player& who;
std::optional<std::string> You;
std::optional<std::string> hit;
tech_wide_secondary_hit(player& who) noexcept : who(who) {
if (!who.is_npc() || game::active()->u.see(who.GPSpos)) {
You = who.desc(grammar::noun::role::subject);
hit = who.regular_verb_agreement("hit");
}
}
tech_wide_secondary_hit(const tech_wide_secondary_hit& src) = delete;
tech_wide_secondary_hit(tech_wide_secondary_hit&& src) = delete;
tech_wide_secondary_hit& operator=(const tech_wide_secondary_hit& src) = delete;
tech_wide_secondary_hit& operator=(tech_wide_secondary_hit&& src) = delete;
~tech_wide_secondary_hit() = default;
void operator()(monster* target) {
int dam = who.roll_bash_damage(target, false) + who.roll_cut_damage(target, false);
target->hurt(dam);
if (You) messages.add("%s %s %s for %d damage!", You->c_str(), hit->c_str(), target->desc(grammar::noun::role::direct_object, grammar::article::definite).c_str(), dam);
}
void operator()(player* target) {
int dam = who.roll_bash_damage(nullptr, false); // looks like (n)PC armor won't work, but covered in the hit function
int cut = who.roll_cut_damage(nullptr, false);
target->hit(game::active(), bp_legs, 3, dam, cut);
if (You) messages.add("%s %s %s for %d damage!", You->c_str(), hit->c_str(), target->name.c_str(), dam + cut);
}
};
void player::perform_technique(technique_id technique, game *g, monster *z,
player *p, int &bash_dam, int &cut_dam,
int &stab_dam, int &pain)
{
assert(z || p);
// self-affecting techniques
switch (technique) {
case TEC_RAPID:
moves += attack_speed(*this, false) / 2;
return;
case TEC_BLOCK:
cataclysm::rational_scale<7, 10>(bash_dam);
return;
}
mobile* const mob = z ? static_cast<mobile*>(z) : p;
const std::string You = desc(grammar::noun::role::subject);
const std::string target = mob->desc(grammar::noun::role::direct_object, grammar::article::definite);
const bool u_see = (!is_npc() || g->u.see(pos));
// The rest affect our target, and thus depend on z vs. p
switch (technique) {
case TEC_SWEEP:
if (is_not<MF_FLIES>(z)) {
z->add_effect(ME_DOWNED, rng(1, 2));
bash_dam += z->fall_damage();
} else if (p != nullptr && p->weapon.type->id != itm_style_judo) {
p->add_disease(DI_DOWNED, rng(1, 2));
bash_dam += 3;
}
break;
case TEC_PRECISE:
mob->add(effect::STUNNED, rng(1, 4));
pain += rng(5, 8);
break;
case TEC_BRUTAL:
mob->add(effect::STUNNED, 1);
mob->knockback_from(GPSpos);
break;
case TEC_THROW:
// Throws are less predictable than brutal strikes.
// We knock them back from a tile adjacent to us!
mob->knockback_from(GPSpos + rng(within_rldist<1>));
if (z != nullptr) {
z->add_effect(ME_DOWNED, rng(1, 2));
} else if (p != nullptr) {
if (p->weapon.type->id != itm_style_judo)
p->add_disease(DI_DOWNED, rng(1, 2));
}
break;
case TEC_WIDE: {
int count_hit = 0;
const auto tar = mob->GPSpos;
for (int dir = direction::NORTH; dir <= direction::NORTHWEST; ++dir) {
const auto test = tar + direction_vector((direction)dir);
if (test == GPSpos) continue; // Don't self-hit
if (const auto _mob = g->mob_at(test)) {
if (hit_roll() >= rng(0, 5) + std::visit(mobile::cast(), *_mob)->dodge_roll()) {
count_hit++;
std::visit(tech_wide_secondary_hit(*this), *_mob);
}
}
}
if (!is_npc()) messages.add("%d enemies hit!", count_hit);
} break;
case TEC_DISARM:
p->GPSpos.add(p->unwield());
if (u_see) messages.add("%s %s %s!", You.c_str(), regular_verb_agreement("disarm").c_str(), target.c_str());
break;
} // switch (tech)
}
technique_id player::pick_defensive_technique(const monster *z, player *p)
{
if (blocks_left == 0) return TEC_NULL;
assert(z || p);
const mobile* const mob = z ? static_cast<const mobile*>(z) : p;
const int foe_melee_skill = mob->melee_skill();
const int foe_dodge = mob->dodge_roll();
int foe_size = 0;
if (z) foe_size = 4 + z->type->size * 4;
else if (p) {
foe_size = 12;
if (p->str_max <= 5) foe_size -= 3;
if (p->str_max >= 12) foe_size += 3;
}
blocks_left--;
if (weapon.has_technique(TEC_WBLOCK_3) &&
dice(melee_skill(), 12) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_3;
if (weapon.has_technique(TEC_WBLOCK_2) &&
dice(melee_skill(), 6) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_2;
if (weapon.has_technique(TEC_WBLOCK_1) &&
dice(melee_skill(), 3) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_1;
if (weapon.has_technique(TEC_DEF_DISARM, this) &&
z == nullptr && !p->unarmed_attack() &&
dice(dex_cur + sklevel[sk_unarmed], 8) > dice(foe_melee_skill, 10))
return TEC_DEF_DISARM;
if (weapon.has_technique(TEC_DEF_THROW, this) &&
str_cur + sklevel[sk_melee] >= foe_size + rng(-4, 4) &&
hit_roll() > rng(1, 5) + foe_dodge && !one_in(3))
return TEC_DEF_THROW;
if (weapon.has_technique(TEC_COUNTER, this) &&
hit_roll() > rng(1, 10) + foe_dodge && !one_in(3))
return TEC_COUNTER;
if (weapon.has_technique(TEC_BLOCK_LEGS, this) &&
(hp_cur[hp_leg_l] >= 20 || hp_cur[hp_leg_r] >= 20) &&
dice(melee_skill() + sklevel[sk_unarmed], 13) >
dice(8 + foe_melee_skill, 10))
return TEC_BLOCK_LEGS;
if (weapon.has_technique(TEC_BLOCK, this) &&
(hp_cur[hp_arm_l] >= 20 || hp_cur[hp_arm_r] >= 20) &&
dice(melee_skill() + sklevel[sk_unarmed], 16) >
dice(6 + foe_melee_skill, 10))
return TEC_BLOCK;
blocks_left++; // We didn't use any blocks, so give it back!
return TEC_NULL;
}
void player::perform_defensive_technique(
technique_id technique, game *g, monster *z, player *p,
body_part &bp_hit, int &side, int &bash_dam, int &cut_dam, int &stab_dam)
{
assert(z || p);
mobile* const mob = z ? static_cast<mobile*>(z) : p;
const std::string You = grammar::capitalize(desc(grammar::noun::role::subject));
const std::string your = pronoun(grammar::noun::role::possessive);
const std::string target = mob->desc(grammar::noun::role::direct_object, grammar::article::definite);
const bool u_see = (!is_npc() || g->u.see(pos));
static auto enhanced_block = [&](int ma) { // XXX itype_id
switch (ma)
{
case itm_style_tai_chi: return per_cur - 6;
case itm_style_taekwando: return str_cur - 6;
default: return 0;
}
};
switch (technique) {
case TEC_BLOCK:
case TEC_BLOCK_LEGS: {
if (technique == TEC_BLOCK) {
bp_hit = bp_arms;
side = (hp_cur[hp_arm_l] >= hp_cur[hp_arm_r]) ? 0 : 1;
} else { // Blocking with our legs
bp_hit = bp_legs;
side = (hp_cur[hp_leg_l] >= hp_cur[hp_leg_r]) ? 0 : 1;
}
if (u_see)
messages.add("%s %s with %s %s.", You.c_str(), regular_verb_agreement("block").c_str(), your.c_str(), body_part_name(bp_hit, side));
bash_dam /= 2;
// styles never are worse than an unskilled block
if (const int bonus = enhanced_block(weapon.type->id); 0 < bonus) {
bash_dam *= clamped_lb<30>(100 - 8 * bonus);
bash_dam /= 100;
}
} break;
case TEC_WBLOCK_1:
case TEC_WBLOCK_2:
case TEC_WBLOCK_3:
// TODO: Cause weapon damage
bash_dam = 0;
cut_dam = 0;
stab_dam = 0;
if (u_see)
messages.add("%s %s with %s %s.", You.c_str(), regular_verb_agreement("block").c_str(), your.c_str(), weapon.tname().c_str());
[[fallthrough]];
case TEC_COUNTER:
break; // Handled elsewhere
case TEC_DEF_THROW:
if (u_see)
messages.add("%s %s %s!", You.c_str(), regular_verb_agreement("throw").c_str(), target.c_str());
bash_dam = 0;
cut_dam = 0;
stab_dam = 0;
mob->add(effect::DOWNED, rng(1, 2));
mob->knockback_from(GPSpos + rng(within_rldist<1>));
break;
case TEC_DEF_DISARM:
p->GPSpos.add(p->unwield());
// Re-roll damage, without our weapon
bash_dam = p->roll_bash_damage(nullptr, false);
cut_dam = p->roll_cut_damage(nullptr, false);
stab_dam = p->roll_stab_damage(nullptr, false);
if (u_see)
messages.add("%s disarm%s %s!", You.c_str(), regular_verb_agreement("disarm").c_str(), target.c_str());
break;
} // switch (technique)
}
void player::perform_special_attacks(game *g, monster *z, player *p,