/
attack.cc
1783 lines (1535 loc) · 53.5 KB
/
attack.cc
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
/*
* File: attack.cc
* Summary: Methods of the attack class, generalized functions which may
* be overloaded by inheriting classes.
* Written by: Robert Burnham
*/
#include "AppHdr.h"
#include "attack.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include "art-enum.h"
#include "chardump.h"
#include "delay.h"
#include "english.h"
#include "env.h"
#include "exercise.h"
#include "fight.h"
#include "fineff.h"
#include "god-conduct.h"
#include "god-passive.h" // passive_t::no_haste
#include "item-name.h"
#include "item-prop.h"
#include "message.h"
#include "mon-behv.h"
#include "mon-clone.h"
#include "mon-death.h"
#include "nearby-danger.h"
#include "pronoun-type.h"
#include "religion.h"
#include "spl-util.h"
#include "state.h"
#include "stepdown.h"
#include "stringutil.h"
#include "tag-version.h"
#include "transform.h"
#include "xom.h"
/*
**************************************************
* BEGIN PUBLIC FUNCTIONS *
**************************************************
*/
attack::attack(actor *attk, actor *defn, actor *blame)
: attacker(attk), defender(defn), responsible(blame ? blame : attk),
attack_occurred(false), cancel_attack(false), did_hit(false),
needs_message(false), attacker_visible(false), defender_visible(false),
perceived_attack(false), obvious_effect(false), to_hit(0),
damage_done(0), special_damage(0), aux_damage(0), min_delay(0),
final_attack_delay(0), special_damage_flavour(BEAM_NONE),
stab_attempt(false), stab_bonus(0), ev_margin(0), weapon(nullptr),
damage_brand(SPWPN_NORMAL), wpn_skill(SK_UNARMED_COMBAT),
shield(nullptr), art_props(0), unrand_entry(nullptr),
attacker_to_hit_penalty(0), attack_verb("bug"), verb_degree(),
no_damage_message(), special_damage_message(), aux_attack(), aux_verb(),
attacker_armour_tohit_penalty(0), attacker_shield_tohit_penalty(0),
defender_shield(nullptr), fake_chaos_attack(false), simu(false),
aux_source(""), kill_type(KILLED_BY_MONSTER)
{
// No effective code should execute, we'll call init_attack again from
// the child class, since initializing an attack will vary based the within
// type of attack actually being made (melee, ranged, etc.)
}
bool attack::handle_phase_attempted()
{
return true;
}
bool attack::handle_phase_blocked()
{
damage_done = 0;
if (attacker->is_player())
behaviour_event(defender->as_monster(), ME_WHACK, attacker);
return true;
}
bool attack::handle_phase_damaged()
{
// We have to check in_bounds() because removed kraken tentacles are
// temporarily returned to existence (without a position) when they
// react to damage.
if (defender->can_bleed()
&& !defender->is_summoned()
&& !defender->submerged()
&& in_bounds(defender->pos())
&& !simu)
{
int blood = damage_done;
if (blood > defender->stat_hp())
blood = defender->stat_hp();
if (blood)
blood_fineff::schedule(defender, defender->pos(), blood);
}
announce_hit();
// Inflict stored damage
damage_done = inflict_damage(damage_done);
// TODO: Unify these, added here so we can get rid of player_attack
if (attacker->is_player())
{
if (damage_done)
player_exercise_combat_skills();
}
else
{
if (!mons_attack_effects())
return false;
}
// It's okay if a monster took lethal damage, but we should stop
// the combat if it was already reset (e.g. a spectral weapon that
// took damage and then noticed that its caster is gone).
return defender->is_player() || !invalid_monster(defender->as_monster());
}
bool attack::handle_phase_killed()
{
monster* mon = defender->as_monster();
if (!invalid_monster(mon)) {
// Was this a reflected missile from the player?
if (responsible->mid == MID_YOU_FAULTLESS)
monster_die(*mon, KILL_YOU_MISSILE, YOU_FAULTLESS);
else
monster_die(*mon, attacker);
}
return true;
}
bool attack::handle_phase_end()
{
return true;
}
/**
* Calculate to-hit penalties from the attacker's armour and shield, if any.
* Set them into the appropriate fields.
*
* @param random If false, calculate average to-hit penalties deterministically.
*/
void attack::calc_encumbrance_penalties(bool random)
{
attacker_armour_tohit_penalty =
maybe_random_div(attacker->armour_tohit_penalty(true, 20), 20, random);
attacker_shield_tohit_penalty =
maybe_random_div(attacker->shield_tohit_penalty(true, 20), 20, random);
}
/**
* Calculate the to-hit for an attacker before the main die roll.
*
* @param random If false, calculate average to-hit deterministically.
*/
int attack::calc_pre_roll_to_hit(bool random)
{
if (using_weapon()
&& (is_unrandom_artefact(*weapon, UNRAND_WOE)
|| is_unrandom_artefact(*weapon, UNRAND_SNIPER)))
{
return AUTOMATIC_HIT;
}
int mhit = attacker->is_player() ?
15 + (you.dex() / 2)
: calc_mon_to_hit_base();
// This if statement is temporary, it should be removed when the
// implementation of a more universal (and elegant) to-hit calculation
// is designed. The actual code is copied from the old mons_to_hit and
// player_to_hit methods.
if (attacker->is_player())
{
// fighting contribution
mhit += maybe_random_div(you.skill(SK_FIGHTING, 100), 100, random);
// weapon skill contribution
if (using_weapon())
{
if (wpn_skill != SK_FIGHTING)
{
if (you.skill(wpn_skill) < 1 && player_in_a_dangerous_place() && random)
xom_is_stimulated(10); // Xom thinks that is mildly amusing.
mhit += maybe_random_div(you.skill(wpn_skill, 100), 100,
random);
}
}
else if (you.form_uses_xl())
mhit += maybe_random_div(you.experience_level * 100, 100, random);
else
{
// Claws give a slight bonus to accuracy when active
mhit += (you.get_mutation_level(MUT_CLAWS) > 0
&& wpn_skill == SK_UNARMED_COMBAT) ? 4 : 2;
mhit += maybe_random_div(you.skill(wpn_skill, 100), 100,
random);
}
// weapon bonus contribution
if (using_weapon())
{
if (weapon->base_type == OBJ_WEAPONS)
{
mhit += weapon->plus;
mhit += property(*weapon, PWPN_HIT);
}
else if (weapon->base_type == OBJ_STAVES)
mhit += property(*weapon, PWPN_HIT);
}
// slaying bonus
mhit += slaying_bonus(wpn_skill == SK_THROWING
|| (weapon && is_range_weapon(*weapon)
&& using_weapon()));
// armour penalty (already calculated if random is true)
if (!random)
calc_encumbrance_penalties(random);
mhit -= (attacker_armour_tohit_penalty + attacker_shield_tohit_penalty);
// vertigo penalty
if (you.duration[DUR_VERTIGO])
mhit -= 5;
// mutation
if (you.get_mutation_level(MUT_EYEBALLS))
mhit += 2 * you.get_mutation_level(MUT_EYEBALLS) + 1;
}
else // Monster to-hit.
{
if (using_weapon())
mhit += weapon->plus + property(*weapon, PWPN_HIT);
const int jewellery = attacker->as_monster()->inv[MSLOT_JEWELLERY];
if (jewellery != NON_ITEM
&& env.item[jewellery].is_type(OBJ_JEWELLERY, RING_SLAYING))
{
mhit += env.item[jewellery].plus;
}
mhit += attacker->scan_artefacts(ARTP_SLAYING);
}
return mhit;
}
/**
* Calculate to-hit modifiers for an attacker that apply after the player's roll.
*
* @param mhit The post-roll player's to-hit value.
*/
int attack::post_roll_to_hit_modifiers(int mhit, bool /*random*/)
{
int modifiers = 0;
// Penalties for both players and monsters:
modifiers -= 5 * attacker->inaccuracy();
if (attacker->confused())
modifiers -= 5;
// If no defender, we're calculating to-hit for debug-display
// purposes, so don't drop down to defender code below
if (defender == nullptr)
return modifiers;
if (!defender->visible_to(attacker))
{
if (attacker->is_player())
modifiers -= 6;
else
modifiers -= mhit * 35 / 100;
}
else
{
// This can only help if you're visible!
const int how_transparent = you.get_mutation_level(MUT_TRANSLUCENT_SKIN);
if (defender->is_player() && how_transparent)
modifiers -= 2 * how_transparent;
// defender backlight bonus and umbra penalty.
if (defender->backlit(false))
modifiers += BACKLIGHT_TO_HIT_BONUS;
if (!attacker->nightvision() && defender->umbra())
modifiers += UMBRA_TO_HIT_MALUS;
}
return modifiers;
}
/**
* Calculate the to-hit for an attacker
*
* @param random If false, calculate average to-hit deterministically.
*/
int attack::calc_to_hit(bool random)
{
int mhit = calc_pre_roll_to_hit(random);
if (mhit == AUTOMATIC_HIT)
return AUTOMATIC_HIT;
// hit roll
if (attacker->is_player())
mhit = maybe_random2(mhit, random);
mhit += post_roll_to_hit_modifiers(mhit, random);
// We already did this roll for players.
if (!attacker->is_player())
mhit = maybe_random2(mhit + 1, random);
dprf(DIAG_COMBAT, "%s: to-hit: %d",
attacker->name(DESC_PLAIN).c_str(), mhit);
return mhit;
}
/* Returns an actor's name
*
* Takes into account actor visibility/invisibility and the type of description
* to be used (capitalization, possessiveness, etc.)
*/
string attack::actor_name(const actor *a, description_level_type desc,
bool actor_visible)
{
return actor_visible ? a->name(desc) : anon_name(desc);
}
/* Returns an actor's pronoun
*
* Takes into account actor visibility
*/
string attack::actor_pronoun(const actor *a, pronoun_type pron,
bool actor_visible)
{
return actor_visible ? a->pronoun(pron) : anon_pronoun(pron);
}
/* Returns an anonymous actor's name
*
* Given the actor visible or invisible, returns the
* appropriate possessive pronoun.
*/
string attack::anon_name(description_level_type desc)
{
switch (desc)
{
case DESC_NONE:
return "";
case DESC_YOUR:
case DESC_ITS:
return "something's";
case DESC_THE:
case DESC_A:
case DESC_PLAIN:
default:
return "something";
}
}
/* Returns an anonymous actor's pronoun
*
* Given invisibility (whether out of LOS or just invisible), returns the
* appropriate possessive, inflexive, capitalised pronoun.
*/
string attack::anon_pronoun(pronoun_type pron)
{
return decline_pronoun(GENDER_NEUTER, pron);
}
/* Initializes an attack, setting up base variables and values
*
* Does not make any changes to any actors, items, or the environment,
* in case the attack is cancelled or otherwise fails. Only initializations
* that are universal to all types of attacks should go into this method,
* any initialization properties that are specific to one attack or another
* should go into their respective init_attack.
*
* Although this method will get overloaded by the derived class, we are
* calling it from attack::attack(), before the overloading has taken place.
*/
void attack::init_attack(skill_type unarmed_skill, int attack_number)
{
ASSERT(attacker);
weapon = attacker->weapon(attack_number);
wpn_skill = weapon ? item_attack_skill(*weapon) : unarmed_skill;
if (attacker->is_player() && you.form_uses_xl())
wpn_skill = SK_FIGHTING; // for stabbing, mostly
calc_encumbrance_penalties(true);
to_hit = calc_to_hit(true);
shield = attacker->shield();
defender_shield = defender ? defender->shield() : defender_shield;
if (weapon && weapon->base_type == OBJ_WEAPONS && is_artefact(*weapon))
{
artefact_properties(*weapon, art_props);
if (is_unrandom_artefact(*weapon))
unrand_entry = get_unrand_entry(weapon->unrand_idx);
}
attacker_visible = attacker->observable();
defender_visible = defender && defender->observable();
needs_message = (attacker_visible || defender_visible);
if (attacker->is_monster())
{
mon_attack_def mon_attk = mons_attack_spec(*attacker->as_monster(),
attack_number,
false);
attk_type = mon_attk.type;
attk_flavour = mon_attk.flavour;
// Don't scale damage for YOU_FAULTLESS etc.
if (attacker->get_experience_level() == 0)
attk_damage = mon_attk.damage;
else
{
attk_damage = div_rand_round(mon_attk.damage
* attacker->get_hit_dice(),
attacker->get_experience_level());
}
if (attk_type == AT_WEAP_ONLY)
{
int weap = attacker->as_monster()->inv[MSLOT_WEAPON];
if (weap == NON_ITEM || is_range_weapon(env.item[weap]))
attk_type = AT_NONE;
else
attk_type = AT_HIT;
}
else if (attk_type == AT_TRUNK_SLAP && attacker->type == MONS_SKELETON)
{
// Elephant trunks have no bones inside.
attk_type = AT_NONE;
}
}
else
{
attk_type = AT_HIT;
attk_flavour = AF_PLAIN;
}
}
void attack::alert_defender()
{
// Allow monster attacks to draw the ire of the defender. Player
// attacks are handled elsewhere.
if (perceived_attack
&& defender->is_monster()
&& attacker->is_monster()
&& attacker->alive() && defender->alive()
&& (defender->as_monster()->foe == MHITNOT || one_chance_in(3)))
{
behaviour_event(defender->as_monster(), ME_WHACK, attacker);
}
// If an enemy attacked a friend, set the pet target if it isn't set
// already, but not if sanctuary is in effect (pet target must be
// set explicitly by the player during sanctuary).
if (perceived_attack && attacker->alive()
&& (defender->is_player() || defender->as_monster()->friendly())
&& !attacker->is_player()
&& !crawl_state.game_is_arena()
&& !attacker->as_monster()->wont_attack())
{
if (defender->is_player())
{
interrupt_activity(activity_interrupt::monster_attacks,
attacker->as_monster());
}
if (you.pet_target == MHITNOT && env.sanctuary_time <= 0)
you.pet_target = attacker->mindex();
}
}
bool attack::distortion_affects_defender()
{
enum disto_effect
{
SMALL_DMG,
BIG_DMG,
BANISH,
BLINK,
NONE
};
const disto_effect choice = random_choose_weighted(35, SMALL_DMG,
25, BIG_DMG,
5, BANISH,
20, BLINK,
15, NONE);
if (simu && !(choice == SMALL_DMG || choice == BIG_DMG))
return false;
switch (choice)
{
case SMALL_DMG:
special_damage += 1 + random2avg(7, 2);
special_damage_message = make_stringf("Space bends around %s%s",
defender_name(false).c_str(),
attack_strength_punctuation(special_damage).c_str());
break;
case BIG_DMG:
special_damage += 3 + random2avg(24, 2);
special_damage_message =
make_stringf("Space warps horribly around %s%s",
defender_name(false).c_str(),
attack_strength_punctuation(special_damage).c_str());
break;
case BLINK:
if (defender_visible)
obvious_effect = true;
if (!defender->no_tele(true, false))
blink_fineff::schedule(defender);
break;
case BANISH:
if (defender_visible)
obvious_effect = true;
defender->banish(attacker, attacker->name(DESC_PLAIN, true),
attacker->get_experience_level());
return true;
case NONE:
// Do nothing
break;
}
return false;
}
void attack::antimagic_affects_defender(int pow)
{
obvious_effect =
enchant_actor_with_flavour(defender, nullptr, BEAM_DRAIN_MAGIC, pow);
}
/// Whose skill should be used for a pain-weapon effect?
static actor* _pain_weapon_user(actor* attacker)
{
if (attacker->type != MONS_SPECTRAL_WEAPON)
return attacker;
const mid_t summoner_mid = attacker->as_monster()->summoner;
if (summoner_mid == MID_NOBODY)
return attacker;
actor* summoner = actor_by_mid(attacker->as_monster()->summoner);
if (!summoner || !summoner->alive())
return attacker;
return summoner;
}
void attack::pain_affects_defender()
{
actor* user = _pain_weapon_user(attacker);
if (!one_chance_in(user->skill_rdiv(SK_NECROMANCY) + 1))
{
special_damage += resist_adjust_damage(defender, BEAM_NEG,
random2(1 + user->skill_rdiv(SK_NECROMANCY)));
if (special_damage && defender_visible)
{
special_damage_message =
make_stringf("%s %s in agony%s",
defender->name(DESC_THE).c_str(),
defender->conj_verb("writhe").c_str(),
attack_strength_punctuation(special_damage).c_str());
}
}
}
static bool _is_chaos_polyable(const actor &defender)
{
if (!defender.can_safely_mutate())
return false; // no polymorphing undead
const monster* mon = defender.as_monster();
if (!mon)
return true;
return !mons_is_firewood(*mon) && !mons_invuln_will(*mon);
}
static bool _is_chaos_slowable(const actor &defender)
{
const monster* mon = defender.as_monster();
if (!mon)
return true;
return !mons_is_firewood(*mon) && !mon->is_stationary();
}
struct chaos_effect
{
string name;
int chance;
function<bool(const actor& def)> valid;
beam_type flavour;
function<bool(attack &attack)> misc_effect;
};
static const vector<chaos_effect> chaos_effects = {
{
"clone", 1, [](const actor &d) {
return d.is_monster() && mons_clonable(d.as_monster(), true);
},
BEAM_NONE, [](attack &attack) {
actor &defender = *attack.defender;
ASSERT(defender.is_monster());
monster *clone = clone_mons(defender.as_monster(), true);
if (!clone)
return false;
const bool obvious_effect = you.can_see(defender) && you.can_see(*clone);
if (one_chance_in(3))
clone->attitude = coinflip() ? ATT_FRIENDLY : ATT_NEUTRAL;
// The player shouldn't get new permanent followers from cloning.
if (clone->attitude == ATT_FRIENDLY && !clone->is_summoned())
clone->mark_summoned(6, true, MON_SUMM_CLONE);
// Monsters being cloned is interesting.
xom_is_stimulated(clone->friendly() ? 12 : 25);
return obvious_effect;
},
},
{
"polymorph", 2, _is_chaos_polyable, BEAM_POLYMORPH,
},
{
"shifter", 1, [](const actor &defender)
{
const monster *mon = defender.as_monster();
return _is_chaos_polyable(defender)
&& mon && !mon->is_shapeshifter()
&& defender.holiness() & MH_NATURAL;
},
BEAM_NONE, [](attack &attack) {
monster* mon = attack.defender->as_monster();
ASSERT(_is_chaos_polyable(*attack.defender));
ASSERT(mon);
ASSERT(!mon->is_shapeshifter());
const bool obvious_effect = you.can_see(*attack.defender);
mon->add_ench(one_chance_in(3) ? ENCH_GLOWING_SHAPESHIFTER
: ENCH_SHAPESHIFTER);
// Immediately polymorph monster, just to make the effect obvious.
mon->polymorph();
// Xom loves it if this happens!
const int friend_factor = mon->friendly() ? 1 : 2;
const int glow_factor = mon->has_ench(ENCH_SHAPESHIFTER) ? 1 : 2;
xom_is_stimulated(64 * friend_factor * glow_factor);
return obvious_effect;
},
},
{
"rage", 5, [](const actor &defender) {
return defender.can_go_berserk();
}, BEAM_NONE, [](attack &attack) {
attack.defender->go_berserk(false);
return you.can_see(*attack.defender);
},
},
{ "hasting", 10, _is_chaos_slowable, BEAM_HASTE },
{ "mighting", 10, nullptr, BEAM_MIGHT },
{ "agilitying", 10, nullptr, BEAM_AGILITY },
{ "invisible", 10, nullptr, BEAM_INVISIBILITY, },
{ "slowing", 10, _is_chaos_slowable, BEAM_SLOW },
{
"paralysis", 5, [](const actor &defender) {
return !defender.is_monster()
|| !mons_is_firewood(*defender.as_monster());
}, BEAM_PARALYSIS,
},
{
"petrify", 5, [](const actor &defender) {
return _is_chaos_slowable(defender) && !defender.res_petrify();
}, BEAM_PETRIFY,
},
};
void attack::chaos_affects_defender()
{
ASSERT(defender);
vector<pair<const chaos_effect&, int>> weights;
for (const chaos_effect &effect : chaos_effects)
if (!effect.valid || effect.valid(*defender))
weights.push_back({effect, effect.chance});
const chaos_effect &effect = *random_choose_weighted(weights);
#ifdef NOTE_DEBUG_CHAOS_EFFECTS
take_note(Note(NOTE_MESSAGE, 0, 0, "CHAOS effect: " + effect.name), true);
#endif
if (effect.misc_effect && effect.misc_effect(*this))
obvious_effect = true;
bolt beam;
beam.flavour = effect.flavour;
if (beam.flavour != BEAM_NONE)
{
if (defender->is_player() && have_passive(passive_t::no_haste)
&& beam.flavour == BEAM_HASTE)
{
simple_god_message(" protects you from inadvertent hurry.");
obvious_effect = true;
return;
}
beam.glyph = 0;
beam.range = 0;
beam.colour = BLACK;
beam.effect_known = false;
// Wielded brand is always known, but maybe this was from a call
// to chaos_affect_actor.
beam.effect_wanton = !fake_chaos_attack;
if (using_weapon() && you.can_see(*attacker))
{
beam.name = wep_name(DESC_YOUR);
beam.item = weapon;
}
else
beam.name = atk_name(DESC_THE);
beam.thrower =
(attacker->is_player()) ? KILL_YOU
: attacker->as_monster()->confused_by_you() ? KILL_YOU_CONF
: KILL_MON;
if (beam.thrower == KILL_YOU || attacker->as_monster()->friendly())
beam.attitude = ATT_FRIENDLY;
beam.source_id = attacker->mid;
beam.source = defender->pos();
beam.target = defender->pos();
beam.damage = dice_def(damage_done + special_damage + aux_damage, 1);
beam.ench_power = beam.damage.num;
const bool you_could_see = you.can_see(*defender);
beam.fire();
if (you_could_see)
obvious_effect = beam.obvious_effect;
}
if (!you.can_see(*attacker))
obvious_effect = false; // XXX: VERY dubious!
}
struct chaos_attack_type
{
attack_flavour flavour;
brand_type brand;
int chance;
function<bool(const actor& def)> valid;
};
// Chaos melee attacks randomly choose a brand from here, with brands that
// definitely won't affect the target being invalid. Chaos itself should
// always be a valid option, triggering a more unpredictable chaos_effect
// instead of a normal attack brand when selected.
static const vector<chaos_attack_type> chaos_types = {
{ AF_FIRE, SPWPN_FLAMING, 10,
[](const actor &d) { return !d.is_fiery(); } },
{ AF_COLD, SPWPN_FREEZING, 10,
[](const actor &d) { return !d.is_icy(); } },
{ AF_ELEC, SPWPN_ELECTROCUTION, 10,
nullptr },
{ AF_POISON, SPWPN_VENOM, 10,
[](const actor &d) {
return !(d.holiness() & (MH_UNDEAD | MH_NONLIVING)); } },
{ AF_CHAOTIC, SPWPN_CHAOS, 10,
nullptr },
{ AF_DRAIN, SPWPN_DRAINING, 5,
[](const actor &d) { return bool(d.holiness() & MH_NATURAL); } },
{ AF_VAMPIRIC, SPWPN_VAMPIRISM, 5,
[](const actor &d) {
return !d.is_summoned() && bool(d.holiness() & MH_NATURAL); } },
{ AF_HOLY, SPWPN_HOLY_WRATH, 5,
[](const actor &d) { return d.holy_wrath_susceptible(); } },
{ AF_ANTIMAGIC, SPWPN_ANTIMAGIC, 5,
[](const actor &d) { return d.antimagic_susceptible(); } },
{ AF_CONFUSE, SPWPN_CONFUSE, 2,
[](const actor &d) { return !d.clarity(); } },
{ AF_DISTORT, SPWPN_DISTORTION, 2,
nullptr },
};
brand_type attack::random_chaos_brand()
{
vector<pair<brand_type, int>> weights;
for (const chaos_attack_type &choice : chaos_types)
if (!choice.valid || choice.valid(*defender))
weights.push_back({choice.brand, choice.chance});
ASSERT(!weights.empty());
brand_type brand = *random_choose_weighted(weights);
#ifdef NOTE_DEBUG_CHAOS_BRAND
string brand_name = "CHAOS brand: ";
switch (brand)
{
case SPWPN_FLAMING: brand_name += "flaming"; break;
case SPWPN_FREEZING: brand_name += "freezing"; break;
case SPWPN_HOLY_WRATH: brand_name += "holy wrath"; break;
case SPWPN_ELECTROCUTION: brand_name += "electrocution"; break;
case SPWPN_VENOM: brand_name += "venom"; break;
case SPWPN_DRAINING: brand_name += "draining"; break;
case SPWPN_DISTORTION: brand_name += "distortion"; break;
case SPWPN_VAMPIRISM: brand_name += "vampirism"; break;
case SPWPN_ANTIMAGIC: brand_name += "antimagic"; break;
case SPWPN_CHAOS: brand_name += "chaos"; break;
case SPWPN_CONFUSE: brand_name += "confusion"; break;
default: brand_name += "BUGGY"; break;
}
// Pretty much duplicated by the chaos effect note,
// which will be much more informative.
if (brand != SPWPN_CHAOS)
take_note(Note(NOTE_MESSAGE, 0, 0, brand_name), true);
#endif
return brand;
}
attack_flavour attack::random_chaos_attack_flavour()
{
vector<pair<attack_flavour, int>> weights;
for (const chaos_attack_type &choice : chaos_types)
if (!choice.valid || choice.valid(*defender))
weights.push_back({choice.flavour, choice.chance});
ASSERT(!weights.empty());
return *random_choose_weighted(weights);
}
void attack::drain_defender()
{
if (defender->is_monster() && coinflip())
return;
if (!(defender->holiness() & MH_NATURAL))
return;
special_damage = resist_adjust_damage(defender, BEAM_NEG,
(1 + random2(damage_done)) / 2);
if (defender->drain(attacker, true, 1 + damage_done))
{
if (defender->is_player())
obvious_effect = true;
else if (defender_visible)
{
special_damage_message =
make_stringf(
"%s %s %s%s",
atk_name(DESC_THE).c_str(),
attacker->conj_verb("drain").c_str(),
defender_name(true).c_str(),
attack_strength_punctuation(special_damage).c_str());
}
}
}
void attack::drain_defender_speed()
{
if (needs_message)
{
mprf("%s %s %s vigour!",
atk_name(DESC_THE).c_str(),
attacker->conj_verb("drain").c_str(),
def_name(DESC_ITS).c_str());
}
defender->slow_down(attacker, 5 + random2(7));
}
int attack::inflict_damage(int dam, beam_type flavour, bool clean)
{
if (flavour == NUM_BEAMS)
flavour = special_damage_flavour;
// Auxes temporarily clear damage_brand so we don't need to check
if (damage_brand == SPWPN_REAPING
|| damage_brand == SPWPN_CHAOS && one_chance_in(100))
{
defender->props["reaping_damage"].get_int() += dam;
// With two reapers of different friendliness, the most recent one
// gets the zombie. Too rare a case to care any more.
defender->props["reaper"].get_int() = attacker->mid;
}
return defender->hurt(responsible, dam, flavour, kill_type,
"", aux_source.c_str(), clean);
}
/* If debug, return formatted damage done
*
*/
string attack::debug_damage_number()
{
#ifdef DEBUG_DIAGNOSTICS
return make_stringf(" for %d", damage_done);
#else
return "";
#endif
}
/* Returns standard attack punctuation
*
* Used in player / monster (both primary and aux) attacks
*/
string attack_strength_punctuation(int dmg)
{
if (dmg < HIT_WEAK)
return ".";
else if (dmg < HIT_MED)
return "!";
else if (dmg < HIT_STRONG)
return "!!";
else
return string(3 + (int) log2(dmg / HIT_STRONG), '!');
}
/* Returns evasion adverb
*
*/
string attack::evasion_margin_adverb()
{
return (ev_margin <= -20) ? " completely" :
(ev_margin <= -12) ? "" :
(ev_margin <= -6) ? " closely"
: " barely";
}
void attack::stab_message()
{
defender->props["helpless"] = true;
switch (stab_bonus)
{
case 6: // big melee, monster surrounded/not paying attention
if (coinflip())
{
mprf("You %s %s from a blind spot!",
(you.species == SP_FELID) ? "pounce on" : "strike",
defender->name(DESC_THE).c_str());
}
else
{
mprf("You catch %s momentarily off-guard.",
defender->name(DESC_THE).c_str());
}
break;
case 4: // confused/fleeing
if (!one_chance_in(3))
{
mprf("You catch %s completely off-guard!",
defender->name(DESC_THE).c_str());
}
else
{
mprf("You %s %s from behind!",
(you.species == SP_FELID) ? "pounce on" : "strike",
defender->name(DESC_THE).c_str());
}
break;
case 2:
case 1:
if (you.species == SP_FELID && coinflip())
{
mprf("You pounce on the unaware %s!",
defender->name(DESC_PLAIN).c_str());
break;
}
mprf("%s fails to defend %s.",
defender->name(DESC_THE).c_str(),