forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
melee.cpp
1593 lines (1370 loc) · 45.3 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 "bionics.h"
#include "game.h"
#include "keypress.h"
#include <sstream>
#include <stdlib.h>
#include "cursesdef.h"
void hit_message(game *g, std::string subject, std::string verb,
std::string target, int dam, bool crit);
void melee_practice(player &u, bool hit, bool unarmed, bool bashing,
bool cutting, bool stabbing);
int attack_speed(player &u, bool missed);
int stumble(player &u);
std::string melee_verb(technique_id tech, std::string your, player &p,
int bash_dam, int cut_dam, int stab_dam);
/* 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()
{
return (weapon.typeId() != 0 && !weapon.is_style());
}
bool player::unarmed_attack()
{
return (weapon.typeId() == 0 || weapon.is_style() ||
weapon.has_flag(IF_UNARMED_WEAPON));
}
int player::base_to_hit(bool real_life, int stat)
{
if (stat == -999)
stat = (real_life ? dex_cur : dex_max);
return 1 + int(stat / 2) + skillLevel("melee");
}
int player::hit_roll()
{
int stat = dex_cur;
// Some martial arts use something else to determine hits!
switch (weapon.typeId()) {
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 = skillLevel("unarmed");
if (skillLevel("unarmed") > 4)
best_bonus += skillLevel("unarmed") - 4; // Extra bonus for high levels
}
// Using a bashing weapon?
if (weapon.is_bashing_weapon()) {
int bash_bonus = int(skillLevel("bashing") / 3);
if (bash_bonus > best_bonus)
best_bonus = bash_bonus;
}
// Using a cutting weapon?
if (weapon.is_cutting_weapon()) {
int cut_bonus = int(skillLevel("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(skillLevel("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 (unarmed_attack())
numdice += int(disease_level(DI_DRUNK) / 300);
else
numdice += int(disease_level(DI_DRUNK) / 400);
}
if (numdice < 1) {
numdice = 1;
sides = 8 - encumb(bp_torso);
}
return dice(numdice, sides);
}
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
std::string You = (is_u ? "You" : name);
std::string Your = (is_u ? "Your" : name + "'s");
std::string your = (is_u ? "your" : (male ? "his" : "her"));
std::string verb = "hit";
std::string target = "the " + z->name();
// 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) { // Only display messages if this is the player
if (weapon.has_technique(TEC_FEINT, this))
g->add_msg("You feint.");
else if (stumble_pen >= 60)
g->add_msg("You miss and stumble with the momentum.");
else if (stumble_pen >= 10)
g->add_msg("You swing wildly and miss.");
else
g->add_msg("You miss.");
}
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
int stuck_penalty = roll_stuck_penalty(z, (stab_dam >= cut_dam));
if (weapon.is_style())
stuck_penalty = 0;
// Pick one or more special attacks
technique_id technique = pick_technique(g, z, NULL, critical_hit, allow_grab);
// Handles effects as well; not done in melee_affect_*
perform_technique(technique, g, z, NULL, bash_dam, cut_dam, stab_dam, pain);
z->speed -= int(pain / 2);
// Mutation-based attacks
perform_special_attacks(g, z, NULL, bash_dam, cut_dam, stab_dam);
// Handles speed penalties to monster & us, etc
melee_special_effects(g, z, NULL, critical_hit, bash_dam, cut_dam, stab_dam);
// Make a rather quiet sound, to alert any nearby monsters
if (weapon.typeId() != itm_style_ninjutsu) // Ninjutsu is silent!
g->sound(posx, posy, 8, "");
verb = melee_verb(technique, your, *this, bash_dam, cut_dam, stab_dam);
int dam = bash_dam + (cut_dam > stab_dam ? cut_dam : stab_dam);
hit_message(g, 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 = remove_weapon();
dam += hit_mon(g, z, false); // False means a second grab isn't allowed
weapon = 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)
{
bool is_u = (this == &(g->u)); // Affects how we'll display messages
if (is_u && p.is_npc()) {
npc* npcPtr = dynamic_cast<npc*>(&p);
npcPtr->make_angry();
}
std::string You = (is_u ? "You" : name);
std::string Your = (is_u ? "Your" : name + "'s");
std::string your = (is_u ? "your" : (male ? "his" : "her"));
std::string verb = "hit";
// Divide their dodge roll by 2 if this is a grab
int target_dodge = (allow_grab ? p.dodge_roll(g) : p.dodge_roll(g) / 2);
int hit_value = hit_roll() - target_dodge;
bool missed = (hit_roll() <= 0);
int move_cost = attack_speed(*this, missed);
if (missed) {
int stumble_pen = stumble(*this);
if (is_u) { // Only display messages if this is the player
if (weapon.has_technique(TEC_FEINT, this))
g->add_msg("You feint.");
else if (stumble_pen >= 60)
g->add_msg("You miss and stumble with the momentum.");
else if (stumble_pen >= 10)
g->add_msg("You swing wildly and miss.");
else
g->add_msg("You miss.");
}
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;
body_part bp_hit;
int side = rng(0, 1);
hit_value += rng(-10, 10);
if (hit_value >= 30)
bp_hit = bp_eyes;
else if (hit_value >= 20)
bp_hit = bp_head;
else if (hit_value >= 10)
bp_hit = bp_torso;
else if (one_in(4))
bp_hit = bp_legs;
else
bp_hit = bp_arms;
std::string target = (p.is_npc() ? p.name + "'s " : "your ");
target += body_part_name(bp_hit, side);
bool critical_hit = scored_crit(target_dodge);
int bash_dam = roll_bash_damage(NULL, critical_hit);
int cut_dam = roll_cut_damage(NULL, critical_hit);
int stab_dam = roll_stab_damage(NULL, critical_hit);
technique_id tech_def = p.pick_defensive_technique(g, NULL, this);
p.perform_defensive_technique(tech_def, g, NULL, 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
int stuck_penalty = roll_stuck_penalty(NULL, (stab_dam >= cut_dam));
if (weapon.is_style())
stuck_penalty = 0;
// Pick one or more special attacks
technique_id technique = pick_technique(g, NULL, &p, critical_hit, allow_grab);
// Handles effects as well; not done in melee_affect_*
perform_technique(technique, g, NULL, &p, bash_dam, cut_dam, stab_dam, pain);
p.pain += pain;
// Mutation-based attacks
perform_special_attacks(g, NULL, &p, bash_dam, cut_dam, stab_dam);
// Handles speed penalties to monster & us, etc
melee_special_effects(g, NULL, &p, critical_hit, bash_dam, cut_dam, stab_dam);
// Make a rather quiet sound, to alert any nearby monsters
if (weapon.typeId() != itm_style_ninjutsu) // Ninjutsu is silent!
g->sound(posx, posy, 8, "");
p.hit(g, bp_hit, side, bash_dam, (cut_dam > stab_dam ? cut_dam : stab_dam));
verb = melee_verb(technique, your, *this, bash_dam, cut_dam, stab_dam);
int dam = bash_dam + (cut_dam > stab_dam ? cut_dam : stab_dam);
hit_message(g, 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.dex_cur + p.skillLevel("melee"), 12) >
dice(dex_cur + skillLevel("melee"), 10)) {
if (is_u)
g->add_msg("%s break%s the grab!", target.c_str(), (p.is_npc() ? "s" : ""));
} else if (!unarmed_attack()) {
item tmpweap = remove_weapon();
hit_player(g, p, false); // False means a second grab isn't allowed
weapon = tmpweap;
} else
hit_player(g, p, false); // False means a second grab isn't allowed
}
if (tech_def == TEC_COUNTER) {
if (!p.is_npc())
g->add_msg("Counter-attack!");
p.hit_player(g, *this);
}
}
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;
}
bool player::scored_crit(int target_dodge)
{
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(skillLevel("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 dexteiry!
int stat = dex_cur;
// Some martial arts use something else to determine hits!
switch (weapon.typeId()) {
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++)
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() && skillLevel("bashing") > best_skill)
best_skill = skillLevel("bashing");
if (weapon.is_cutting_weapon() && skillLevel("cutting") > best_skill)
best_skill = skillLevel("cutting");
if ((weapon.has_flag(IF_SPEAR) || weapon.has_flag(IF_STAB)) &&
skillLevel("stabbing") > best_skill)
best_skill = skillLevel("stabbing");
if (unarmed_attack() && skillLevel("unarmed") > best_skill)
best_skill = skillLevel("unarmed");
best_skill += int(skillLevel("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(game *g)
{
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 += skillLevel("dodge");
ret += disease_intensity(DI_DODGE_BOOST);
ret -= (encumb(bp_legs) / 2) + encumb(bp_torso);
ret += int(current_speed(g) / 150);
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 hyuuge
else if (str_max <= 5)
ret++; // Bonus if we're small
if (dodges_left <= 0) { // We already dodged this turn
if (rng(1, skillLevel("dodge") + dex_cur + 15) <= skillLevel("dodge") + dex_cur)
ret = rng(0, ret);
else
ret = 0;
}
dodges_left--;
// If we're over our cap, average it with our cap
if (ret > int(dex_cur / 2) + skillLevel("dodge") * 2)
ret = ( ret + int(dex_cur / 2) + skillLevel("dodge") * 2 ) / 2;
return ret;
}
int player::dodge_roll(game *g)
{
return dice(dodge(g), 6);
}
int player::base_damage(bool real_life, int stat)
{
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(monster *z, bool crit)
{
int ret = 0;
int stat = str_cur; // Which stat determines damage?
int skill = skillLevel("bashing"); // Which skill determines damage?
if (unarmed_attack())
skill = skillLevel("unarmed");
switch (weapon.typeId()) { // 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) && has_disease(DI_DRUNK)) {
// Remember, a single drink gives 600 levels of DI_DRUNK
int mindrunk, maxdrunk;
if (unarmed_attack()) {
mindrunk = disease_level(DI_DRUNK) / 600;
maxdrunk = disease_level(DI_DRUNK) / 250;
} else {
mindrunk = disease_level(DI_DRUNK) / 900;
maxdrunk = disease_level(DI_DRUNK) / 400;
}
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) + skillLevel("unarmed"));
if (crit) {
bash_dam *= 1.5;
bash_cap *= 2;
}
if (bash_dam > bash_cap)// Cap for weak characters
bash_dam = (bash_cap * 3 + bash_dam) / 4;
if (z != NULL && z->has_flag(MF_PLASTIC))
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 != NULL)
ret -= z->armor_bash() / 2;
} else if (z != NULL)
ret -= z->armor_bash();
return (ret < 0 ? 0 : ret);
}
int player::roll_cut_damage(monster *z, bool crit)
{
if (weapon.has_flag(IF_SPEAR))
return 0; // Stabs, doesn't cut!
int z_armor_cut = (z == NULL ? 0 : z->armor_cut() - skillLevel("cutting") / 2);
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 + ((int)skillLevel("unarmed") > 8 ? 8 : (int)skillLevel("unarmed"));
if (has_trait(PF_SLIME_HANDS) && (z == NULL || !z->has_flag(MF_ACIDPROOF)))
ret += rng(4, 6);
}
if (ret <= 0)
return 0; // No negative damage!
// 80%, 88%, 96%, 104%, 112%, 116%, 120%, 124%, 128%, 132%
if (skillLevel("cutting") <= 5)
ret *= double( 0.8 + 0.08 * skillLevel("cutting") );
else
ret *= double( 0.92 + 0.04 * skillLevel("cutting") );
if (crit)
ret *= double( 1.0 + double(skillLevel("cutting") / 12) );
return ret;
}
int player::roll_stab_damage(monster *z, bool crit)
{
int ret = 0;
int z_armor = (z == NULL ? 0 : z->armor_cut() - 3 * skillLevel("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 != NULL && 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 multiplier = double( 1.0 + double(skillLevel("stabbing") / 5) );
if (multiplier > 2.5)
multiplier = 2.5;
ret *= multiplier;
}
return ret;
}
int player::roll_stuck_penalty(monster *z, bool stabbing)
{
int ret = 0;
int basharm = (z == NULL ? 6 : z->armor_bash()),
cutarm = (z == NULL ? 6 : z->armor_cut());
if (stabbing)
ret = weapon.damage_cut() * 3 + basharm * 3 + cutarm * 3 -
dice(skillLevel("stabbing"), 10);
else
ret = weapon.damage_cut() * 4 + basharm * 5 + cutarm * 4 -
dice(skillLevel("cutting"), 10);
if (ret >= weapon.damage_cut() * 10)
return weapon.damage_cut() * 10;
return (ret < 0 ? 0 : ret);
}
technique_id player::pick_technique(game *g, monster *z, player *p,
bool crit, bool allowgrab)
{
if (z == NULL && p == NULL)
return TEC_NULL;
std::vector<technique_id> possible;
bool downed = ((z && !z->has_effect(ME_DOWNED)) ||
(p && !p->has_disease(DI_DOWNED)) );
int base_str_req = 0;
if (z)
base_str_req = z->type->size;
else if (p)
base_str_req = 1 + (2 + p->str_cur) / 4;
if (allowgrab) { // Check if grabs AREN'T REALLY ALLOWED
if (z && z->has_flag(MF_PLASTIC))
allowgrab = false;
}
if (crit) { // Some are crit-only
if (weapon.has_technique(TEC_SWEEP, this) &&
(!z || !z->has_flag(MF_FLIES)) && !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 + skillLevel("melee") >= 4 + base_str_req)
possible.push_back(TEC_BRUTAL);
}
if (possible.empty()) { // Use non-crits only if any crit-onlies aren't used
if (weapon.has_technique(TEC_DISARM, this) && !z &&
p->weapon.typeId() != 0 && !p->weapon.has_flag(IF_UNARMED_WEAPON) &&
dice( dex_cur + skillLevel("unarmed"), 8) >
dice(p->dex_cur + p->skillLevel("melee"), 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 + skillLevel("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 (int x = posx - 1; x <= posx + 1; x++) {
for (int y = posy - 1; y <= posy + 1; y++) {
int mondex = g->mon_at(x, y);
if (mondex != -1) {
if (g->z[mondex].friendly == 0)
enemy_count++;
else
enemy_count -= 2;
}
int npcdex = g->npc_at(x, y);
if (npcdex != -1) {
if (g->active_npc[npcdex].attitude == NPCATT_KILL)
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) ];
}
void player::perform_technique(technique_id technique, game *g, monster *z,
player *p, int &bash_dam, int &cut_dam,
int &stab_dam, int &pain)
{
bool mon = (z != NULL);
std::string You = (is_npc() ? name : "You");
std::string target = (mon ? "the " + z->name() :
(p->is_npc() ? p->name : "you"));
std::string s = (is_npc() ? "s" : "");
int tarx = (mon ? z->posx : p->posx), tary = (mon ? z->posy : p->posy);
int junk;
bool u_see = (!is_npc() || g->u_see(posx, posy, junk));
if (technique == TEC_RAPID) {
moves += int( attack_speed(*this, false) / 2);
return;
}
if (technique == TEC_BLOCK) {
bash_dam *= .7;
return;
}
// The rest affect our target, and thus depend on z vs. p
switch (technique) {
case TEC_SWEEP:
if (z != NULL && !z->has_flag(MF_FLIES)) {
z->add_effect(ME_DOWNED, rng(1, 2));
bash_dam += z->fall_damage();
} else if (p != NULL && !p->weapon.typeId() == itm_style_judo) {
p->add_disease(DI_DOWNED, rng(1, 2), g);
bash_dam += 3;
}
break;
case TEC_PRECISE:
if (z != NULL)
z->add_effect(ME_STUNNED, rng(1, 4));
else if (p != NULL)
p->add_disease(DI_STUNNED, rng(1, 2), g);
pain += rng(5, 8);
break;
case TEC_BRUTAL:
if (z != NULL) {
z->add_effect(ME_STUNNED, 1);
z->knock_back_from(g, posx, posy);
} else if (p != NULL) {
p->add_disease(DI_STUNNED, 1, g);
p->knock_back_from(g, posy, posy);
}
break;
case TEC_THROW:
// Throws are less predictable than brutal strikes.
// We knock them back from a tile adjacent to us!
if (z != NULL) {
z->add_effect(ME_DOWNED, rng(1, 2));
z->knock_back_from(g, posx + rng(-1, 1), posy + rng(-1, 1));
} else if (p != NULL) {
p->knock_back_from(g, posx + rng(-1, 1), posy + rng(-1, 1));
if (!p->weapon.typeId() == itm_style_judo)
p->add_disease(DI_DOWNED, rng(1, 2), g);
}
break;
case TEC_WIDE: {
int count_hit = 0;
for (int x = posx - 1; x <= posx + 1; x++) {
for (int y = posy - 1; y <= posy + 1; y++) {
if (x != tarx || y != tary) { // Don't double-hit our target
int mondex = g->mon_at(x, y);
if (mondex != -1 && hit_roll() >= rng(0, 5) + g->z[mondex].dodge_roll()) {
count_hit++;
int dam = roll_bash_damage(&(g->z[mondex]), false) +
roll_cut_damage (&(g->z[mondex]), false);
g->z[mondex].hurt(dam);
if (u_see)
g->add_msg("%s hit%s %s for %d damage!", You.c_str(), s.c_str(),
target.c_str(), dam);
}
int npcdex = g->npc_at(x, y);
if (npcdex != -1 &&
hit_roll() >= rng(0, 5) + g->active_npc[npcdex].dodge_roll(g)) {
count_hit++;
int dam = roll_bash_damage(NULL, false);
int cut = roll_cut_damage (NULL, false);
g->active_npc[npcdex].hit(g, bp_legs, 3, dam, cut);
if (u_see)
g->add_msg("%s hit%s %s for %d damage!", You.c_str(), s.c_str(),
g->active_npc[npcdex].name.c_str(), dam + cut);
}
}
}
}
if (!is_npc())
g->add_msg("%d enemies hit!", count_hit);
} break;
case TEC_DISARM:
g->m.add_item(p->posx, p->posy, p->remove_weapon());
if (u_see)
g->add_msg("%s disarm%s %s!", You.c_str(), s.c_str(), target.c_str());
break;
} // switch (tech)
}
technique_id player::pick_defensive_technique(game *g, monster *z, player *p)
{
if (blocks_left == 0)
return TEC_NULL;
int foe_melee_skill = 0;
if (z != NULL)
foe_melee_skill = z->type->melee_skill;
else if (p != NULL)
foe_melee_skill = p->dex_cur + p->skillLevel("melee");
int foe_dodge = 0;
if (z != NULL)
foe_dodge = z->dodge_roll();
else if (p != NULL)
foe_dodge = p->dodge_roll(g);
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(dex_cur + skillLevel("melee"), 12) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_3;
if (weapon.has_technique(TEC_WBLOCK_2) &&
dice(dex_cur + skillLevel("melee"), 6) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_2;
if (weapon.has_technique(TEC_WBLOCK_1) &&
dice(dex_cur + skillLevel("melee"), 3) > dice(foe_melee_skill, 10))
return TEC_WBLOCK_1;
if (weapon.has_technique(TEC_DEF_DISARM, this) &&
z == NULL && p->weapon.typeId() != 0 &&
!p->weapon.has_flag(IF_UNARMED_WEAPON) &&
dice( dex_cur + skillLevel("unarmed"), 8) >
dice(p->dex_cur + p->skillLevel("melee"), 10))
return TEC_DEF_DISARM;
if (weapon.has_technique(TEC_DEF_THROW, this) &&
str_cur + skillLevel("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(dex_cur + skillLevel("unarmed") + skillLevel("melee"), 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(dex_cur + skillLevel("unarmed") + skillLevel("melee"), 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)
{
int junk;
bool mon = (z != NULL);
std::string You = (is_npc() ? name : "You");
std::string your = (is_npc() ? (male ? "his" : "her") : "your");
std::string target = (mon ? "the " + z->name() : p->name);
bool u_see = (!is_npc() || g->u_see(posx, posy, junk));
switch (technique) {
case TEC_BLOCK:
case TEC_BLOCK_LEGS: {
if (technique == TEC_BLOCK) {
bp_hit = bp_arms;
if (hp_cur[hp_arm_l] >= hp_cur[hp_arm_r])
side = 0;
else
side = 1;
} else { // Blocking with our legs
bp_hit = bp_legs;
if (hp_cur[hp_leg_l] >= hp_cur[hp_leg_r])
side = 0;
else
side = 1;
}
if (u_see)
g->add_msg("%s block%s with %s %s.", You.c_str(), (is_npc() ? "s" : ""),
your.c_str(), body_part_name(bp_hit, side).c_str());
bash_dam *= .5;
double reduction = 1.0;
// Special reductions for certain styles
if (weapon.typeId() == itm_style_tai_chi)
reduction -= double(0.08 * double(per_cur - 6));
if (weapon.typeId() == itm_style_taekwando)
reduction -= double(0.08 * double(str_cur - 6));
if (reduction > 1.0)
reduction = 1.0;
if (reduction < 0.3)
reduction = 0.3;
bash_dam *= reduction;
} break;