/
melee-attack.cc
3657 lines (3127 loc) · 107 KB
/
melee-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
* @brief melee_attack class and associated melee_attack methods
*/
#include "AppHdr.h"
#include "melee-attack.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "areas.h"
#include "art-enum.h"
#include "attitude-change.h"
#include "bloodspatter.h"
#include "butcher.h"
#include "chardump.h"
#include "cloud.h"
#include "coordit.h"
#include "delay.h"
#include "english.h"
#include "env.h"
#include "exercise.h"
#include "fineff.h"
#include "food.h"
#include "god-conduct.h"
#include "god-item.h"
#include "god-passive.h" // passive_t::convert_orcs
#include "hints.h"
#include "item-prop.h"
#include "mapdef.h"
#include "message.h"
#include "misc.h"
#include "mon-behv.h"
#include "mon-poly.h"
#include "mon-tentacle.h"
#include "prompt.h"
#include "religion.h"
#include "shout.h"
#include "spl-damage.h"
#include "spl-summoning.h"
#include "state.h"
#include "stepdown.h"
#include "stringutil.h"
#include "target.h"
#include "terrain.h"
#include "transform.h"
#include "traps.h"
#include "unwind.h"
#include "view.h"
#include "xom.h"
#ifdef NOTE_DEBUG_CHAOS_BRAND
#define NOTE_DEBUG_CHAOS_EFFECTS
#endif
#ifdef NOTE_DEBUG_CHAOS_EFFECTS
#include "notes.h"
#endif
/*
**************************************************
* BEGIN PUBLIC FUNCTIONS *
**************************************************
*/
melee_attack::melee_attack(actor *attk, actor *defn,
int attack_num, int effective_attack_num,
bool is_cleaving, coord_def attack_pos)
: // Call attack's constructor
::attack(attk, defn),
attack_number(attack_num), effective_attack_number(effective_attack_num),
cleaving(is_cleaving), is_riposte(false),
wu_jian_attack(WU_JIAN_ATTACK_NONE),
wu_jian_number_of_targets(1)
{
attack_occurred = false;
damage_brand = attacker->damage_brand(attack_number);
init_attack(SK_UNARMED_COMBAT, attack_number);
if (weapon && !using_weapon())
wpn_skill = SK_FIGHTING;
attack_position = attacker->pos();
}
bool melee_attack::can_reach()
{
return attk_type == AT_HIT && weapon && weapon_reach(*weapon) > REACH_NONE
|| attk_flavour == AF_REACH
|| attk_flavour == AF_REACH_STING;
}
bool melee_attack::handle_phase_attempted()
{
// Skip invalid and dummy attacks.
if (defender && (!adjacent(attack_position, defender->pos())
&& !can_reach())
|| attk_type == AT_CONSTRICT
&& (!attacker->can_constrict(defender, true)
|| attacker->is_monster() && attacker->mid == MID_PLAYER))
{
--effective_attack_number;
return false;
}
if (attacker->is_player() && defender && defender->is_monster())
{
// Unrands with secondary effects that can harm nearby friendlies.
// Don't prompt for confirmation (and leak information about the
// monster's position) if the player can't see the monster.
if (weapon && is_unrandom_artefact(*weapon, UNRAND_DEVASTATOR)
&& you.can_see(*defender))
{
targeter_smite hitfunc(attacker, 1, 1, 1, false);
hitfunc.set_aim(defender->pos());
if (stop_attack_prompt(hitfunc, "attack", nullptr, nullptr,
defender->as_monster()))
{
cancel_attack = true;
return false;
}
}
else if (weapon &&
(is_unrandom_artefact(*weapon, UNRAND_SINGING_SWORD)
|| is_unrandom_artefact(*weapon, UNRAND_VARIABILITY)
|| is_unrandom_artefact(*weapon, UNRAND_SPELLBINDER))
&& you.can_see(*defender))
{
targeter_los hitfunc(&you, LOS_NO_TRANS);
if (stop_attack_prompt(hitfunc, "attack",
[](const actor *act)
{
return !(you.deity() == GOD_FEDHAS
&& fedhas_protects(*act->as_monster()));
}, nullptr, defender->as_monster()))
{
cancel_attack = true;
return false;
}
}
else if (weapon && is_unrandom_artefact(*weapon, UNRAND_TORMENT)
&& you.can_see(*defender))
{
targeter_los hitfunc(&you, LOS_NO_TRANS);
if (stop_attack_prompt(hitfunc, "attack",
[] (const actor *m)
{
return !m->res_torment();
},
nullptr, defender->as_monster()))
{
cancel_attack = true;
return false;
}
}
else if (weapon && is_unrandom_artefact(*weapon, UNRAND_ARC_BLADE)
&& you.can_see(*defender))
{
vector<const actor *> exclude;
if (!safe_discharge(defender->pos(), exclude))
{
cancel_attack = true;
return false;
}
}
else if (!cleave_targets.empty())
{
targeter_cleave hitfunc(attacker, defender->pos());
if (stop_attack_prompt(hitfunc, "attack"))
{
cancel_attack = true;
return false;
}
}
else if (stop_attack_prompt(defender->as_monster(), false,
attack_position))
{
cancel_attack = true;
return false;
}
}
if (attacker->is_player())
{
// Set delay now that we know the attack won't be cancelled.
if (!is_riposte
&& (wu_jian_attack == WU_JIAN_ATTACK_NONE))
{
you.time_taken = you.attack_delay().roll();
}
const caction_type cact_typ = is_riposte ? CACT_RIPOSTE : CACT_MELEE;
if (weapon)
{
if (weapon->base_type == OBJ_WEAPONS)
if (is_unrandom_artefact(*weapon)
&& get_unrand_entry(weapon->unrand_idx)->type_name)
{
count_action(cact_typ, weapon->unrand_idx);
}
else
count_action(cact_typ, weapon->sub_type);
else if (weapon->base_type == OBJ_STAVES)
count_action(cact_typ, WPN_STAFF);
}
else
count_action(cact_typ, -1, -1); // unarmed subtype/auxtype
}
else
{
// Only the first attack costs any energy.
if (!effective_attack_number)
{
int energy = attacker->as_monster()->action_energy(EUT_ATTACK);
int delay = attacker->attack_delay().roll();
dprf(DIAG_COMBAT, "Attack delay %d, multiplier %1.1f", delay, energy * 0.1);
ASSERT(energy > 0);
ASSERT(delay > 0);
attacker->as_monster()->speed_increment
-= div_rand_round(energy * delay, 10);
}
// Statues and other special monsters which have AT_NONE need to lose
// energy, but otherwise should exit the melee attack now.
if (attk_type == AT_NONE)
return false;
}
if (attacker != defender)
{
// Allow setting of your allies' target, etc.
attacker->attacking(defender);
check_autoberserk();
}
// The attacker loses nutrition.
attacker->make_hungry(3, true);
// Xom thinks fumbles are funny...
if (attacker->fumbles_attack())
{
// ... and thinks fumbling when trying to hit yourself is just
// hilarious.
xom_is_stimulated(attacker == defender ? 200 : 10);
return false;
}
// Non-fumbled self-attacks due to confusion are still pretty funny, though.
else if (attacker == defender && attacker->confused())
xom_is_stimulated(100);
// Any attack against a monster we're afraid of has a chance to fail
if (attacker->is_player() && defender &&
you.afraid_of(defender->as_monster()) && one_chance_in(3))
{
mprf("You attempt to attack %s, but flinch away in fear!",
defender->name(DESC_THE).c_str());
return false;
}
if (attk_flavour == AF_SHADOWSTAB
&& defender && !defender->can_see(*attacker))
{
if (you.see_cell(attack_position))
{
mprf("%s strikes at %s from the darkness!",
attacker->name(DESC_THE, true).c_str(),
defender->name(DESC_THE).c_str());
}
to_hit = AUTOMATIC_HIT;
needs_message = false;
}
else if (attacker->is_monster()
&& attacker->type == MONS_DROWNED_SOUL)
{
to_hit = AUTOMATIC_HIT;
}
attack_occurred = true;
// Check for player practicing dodging
if (defender && defender->is_player())
practise_being_attacked();
return true;
}
bool melee_attack::handle_phase_dodged()
{
did_hit = false;
if (needs_message)
{
// TODO: Unify these, placed player_warn_miss here so I can remove
// player_attack
if (attacker->is_player())
player_warn_miss();
else
{
mprf("%s%s misses %s%s",
atk_name(DESC_THE).c_str(),
evasion_margin_adverb().c_str(),
defender_name(true).c_str(),
attack_strength_punctuation(damage_done).c_str());
}
}
if (defender->is_player())
count_action(CACT_DODGE, DODGE_EVASION);
if (attacker != defender && adjacent(defender->pos(), attack_position)
&& attacker->alive() && defender->can_see(*attacker)
&& !defender->cannot_act() && !defender->confused()
&& (!defender->is_player() || (!you.duration[DUR_LIFESAVING]
&& !attacker->as_monster()->neutral()))
&& !mons_aligned(attacker, defender) // confused friendlies attacking
// Retaliation only works on the first attack in a round.
// FIXME: player's attack is -1, even for auxes
&& effective_attack_number <= 0)
{
if (defender->is_player() ?
you.species == SP_MINOTAUR :
mons_species(mons_base_type(*defender->as_monster()))
== MONS_MINOTAUR)
{
do_minotaur_retaliation();
}
// Retaliations can kill!
if (!attacker->alive())
return false;
if (defender->is_player())
{
const bool using_lbl = defender->weapon()
&& item_attack_skill(*defender->weapon()) == SK_LONG_BLADES;
const bool using_fencers
= player_equip_unrand(UNRAND_FENCERS);
const int chance = using_lbl + using_fencers;
if (x_chance_in_y(chance, 3) && !is_riposte) // no ping-pong!
riposte();
// Retaliations can kill!
if (!attacker->alive())
return false;
}
}
return true;
}
void melee_attack::apply_black_mark_effects()
{
// Less reliable effects for players.
if (attacker->is_player()
&& you.has_mutation(MUT_BLACK_MARK)
&& one_chance_in(5)
|| attacker->is_monster()
&& attacker->as_monster()->has_ench(ENCH_BLACK_MARK))
{
if (!defender->alive())
return;
switch (random2(3))
{
case 0:
antimagic_affects_defender(damage_done * 8);
break;
case 1:
defender->weaken(attacker, 6);
break;
case 2:
defender->drain_exp(attacker, false, 10);
break;
}
}
}
/* An attack has been determined to have hit something
*
* Handles to-hit effects for both attackers and defenders,
* Determines damage and passes off execution to handle_phase_damaged
* Also applies weapon brands
*
* Returns true if combat should continue, false if it should end here.
*/
bool melee_attack::handle_phase_hit()
{
did_hit = true;
perceived_attack = true;
bool hit_woke_orc = false;
if (attacker->is_player())
{
if (crawl_state.game_is_hints())
Hints.hints_melee_counter++;
// TODO: Remove this (placed here so I can get rid of player_attack)
if (have_passive(passive_t::convert_orcs)
&& mons_genus(defender->mons_species()) == MONS_ORC
&& !defender->is_summoned()
&& !defender->as_monster()->is_shapeshifter()
&& you.see_cell(defender->pos()) && defender->asleep())
{
hit_woke_orc = true;
}
}
damage_done = 0;
// Slimify does no damage and serves as an on-hit effect, handle it
if (attacker->is_player() && you.duration[DUR_SLIMIFY]
&& mon_can_be_slimified(defender->as_monster())
&& !cleaving)
{
// Bail out after sliming so we don't get aux unarmed and
// attack a fellow slime.
slimify_monster(defender->as_monster());
you.duration[DUR_SLIMIFY] = 0;
return false;
}
if (attacker->is_player() && you.duration[DUR_INFUSION])
{
if (enough_mp(1, true, false))
{
// infusion_power is set when the infusion spell is cast
const int pow = you.props["infusion_power"].get_int();
const int dmg = 2 + div_rand_round(pow, 12);
const int hurt = defender->apply_ac(dmg);
dprf(DIAG_COMBAT, "Infusion: dmg = %d hurt = %d", dmg, hurt);
if (hurt > 0)
{
damage_done = hurt;
dec_mp(1);
}
}
}
// This does more than just calculate the damage, it also sets up
// messages, etc. It also wakes nearby creatures on a failed stab,
// meaning it could have made the attacked creature vanish. That
// will be checked early in player_monattack_hit_effects
damage_done += calc_damage();
bool stop_hit = false;
// Check if some hit-effect killed the monster.
if (attacker->is_player())
stop_hit = !player_monattk_hit_effects();
// check_unrand_effects is safe to call with a dead defender, so always
// call it, even if the hit effects said to stop.
if (stop_hit)
{
check_unrand_effects();
return false;
}
if (damage_done > 0 || flavour_triggers_damageless(attk_flavour))
{
if (!handle_phase_damaged())
return false;
// TODO: Remove this, (placed here to remove player_attack)
if (attacker->is_player() && hit_woke_orc)
{
// Call function of orcs first noticing you, but with
// beaten-up conversion messages (if applicable).
beogh_follower_convert(defender->as_monster(), true);
}
}
else if (needs_message)
{
attack_verb = attacker->is_player()
? attack_verb
: attacker->conj_verb(mons_attack_verb());
// TODO: Clean this up if possible, checking atype for do / does is ugly
mprf("%s %s %s but %s no damage.",
attacker->name(DESC_THE).c_str(),
attack_verb.c_str(),
defender_name(true).c_str(),
attacker->is_player() ? "do" : "does");
}
// Check for weapon brand & inflict that damage too
apply_damage_brand();
// Fireworks when using Serpent's Lash to kill.
if (!defender->alive()
&& defender->as_monster()->can_bleed()
&& wu_jian_has_momentum(wu_jian_attack))
{
blood_spray(defender->pos(), defender->as_monster()->type,
damage_done / 5);
defender->as_monster()->flags |= MF_EXPLODE_KILL;
}
if (check_unrand_effects())
return false;
if (damage_done > 0)
apply_black_mark_effects();
if (attacker->is_player())
{
// Always upset monster regardless of damage.
// However, successful stabs inhibit shouting.
behaviour_event(defender->as_monster(), ME_WHACK, attacker,
coord_def(), !stab_attempt);
// [ds] Monster may disappear after behaviour event.
if (!defender->alive())
return true;
}
else if (defender->is_player())
{
// These effects (mutations right now) are only triggered when
// the player is hit, each of them will verify their own required
// parameters.
do_passive_freeze();
emit_foul_stench();
}
return true;
}
bool melee_attack::handle_phase_damaged()
{
bool shroud_broken = false;
// TODO: Move this somewhere else, this is a terrible place for a
// block-like (prevents all damage) effect.
if (attacker != defender
&& (defender->is_player() && you.duration[DUR_SHROUD_OF_GOLUBRIA]
|| defender->is_monster()
&& defender->as_monster()->has_ench(ENCH_SHROUD))
&& !one_chance_in(3))
{
// Chance of the shroud falling apart increases based on the
// strain of it, i.e. the damage it is redirecting.
if (x_chance_in_y(damage_done, 10+damage_done))
{
// Delay the message for the shroud breaking until after
// the attack message.
shroud_broken = true;
if (defender->is_player())
you.duration[DUR_SHROUD_OF_GOLUBRIA] = 0;
else
defender->as_monster()->del_ench(ENCH_SHROUD);
}
else
{
if (needs_message)
{
mprf("%s shroud bends %s attack away%s",
def_name(DESC_ITS).c_str(),
atk_name(DESC_ITS).c_str(),
attack_strength_punctuation(damage_done).c_str());
}
did_hit = false;
damage_done = 0;
return false;
}
}
if (!attack::handle_phase_damaged())
return false;
if (shroud_broken && needs_message)
{
mprf(defender->is_player() ? MSGCH_WARN : MSGCH_PLAIN,
"%s shroud falls apart!",
def_name(DESC_ITS).c_str());
}
return true;
}
bool melee_attack::handle_phase_aux()
{
if (attacker->is_player()
&& !cleaving
&& wu_jian_attack != WU_JIAN_ATTACK_TRIGGERED_AUX)
{
// returns whether an aux attack successfully took place
// additional attacks from cleave don't get aux
if (!defender->as_monster()->friendly()
&& adjacent(defender->pos(), attack_position))
{
player_aux_unarmed();
}
// Don't print wounds after the first attack with Gyre/Gimble.
// DUR_CLEAVE and Gyre/Gimble interact poorly together at the moment,
// so don't try to skip print_wounds in that case.
if (!(weapon && is_unrandom_artefact(*weapon, UNRAND_GYRE)
&& !you.duration[DUR_CLEAVE]))
{
print_wounds(*defender->as_monster());
}
}
return true;
}
/**
* Devour a monster whole!
*
* @param defender The monster in question.
*/
static void _hydra_devour(monster &victim)
{
// what's the highest hunger level this lets the player get to?
const hunger_state_t max_hunger = player_likes_chunks() ? HS_ENGORGED
: HS_SATIATED;
// will eating this actually fill the player up?
const bool filling = !have_passive(passive_t::goldify_corpses)
&& you.get_mutation_level(MUT_HERBIVOROUS, false) == 0
&& you.hunger_state <= max_hunger
&& you.hunger_state < HS_ENGORGED;
mprf("You %sdevour %s!",
filling ? "hungrily " : "",
victim.name(DESC_THE).c_str());
// give a clearer message for eating invisible things
if (!you.can_see(victim))
{
mprf("It tastes like %s.",
mons_type_name(mons_genus(victim.type), DESC_PLAIN).c_str());
// this could be the actual creature name, but it feels more
// 'flavourful' this way??
// feel free to just use the actual creature name if this has buggy
// edge cases or such
}
if (victim.has_ench(ENCH_STICKY_FLAME))
mprf("Spicy!");
// nutrition (maybe)
if (filling)
{
const int equiv_chunks =
1 + random2(max_corpse_chunks(victim.type));
lessen_hunger(CHUNK_BASE_NUTRITION * equiv_chunks, false, max_hunger);
}
// healing
if (!you.duration[DUR_DEATHS_DOOR])
{
const int healing = 1 + victim.get_experience_level() * 3 / 4
+ random2(victim.get_experience_level() * 3 / 4);
you.heal(healing);
calc_hp();
canned_msg(MSG_GAIN_HEALTH);
dprf("healed for %d (%d hd)", healing, victim.get_experience_level());
}
// and devour the corpse.
victim.props[NEVER_CORPSE_KEY] = true;
}
/**
* Possibly devour the defender whole.
*
* @param defender The defender in question.
*/
static void _hydra_consider_devouring(monster &defender)
{
ASSERT(!crawl_state.game_is_arena());
dprf("considering devouring");
// no unhealthy food
if (determine_chunk_effect(mons_corpse_effect(defender.type)) != CE_CLEAN)
return;
dprf("chunk ok");
// shapeshifters are mutagenic
if (defender.is_shapeshifter())
{
// handle this carefully, so the player knows what's going on
mprf("You spit out %s as %s %s & %s in your mouth!",
defender.name(DESC_THE).c_str(),
defender.pronoun(PRONOUN_SUBJECTIVE).c_str(),
conjugate_verb("twist", defender.pronoun_plurality()).c_str(),
conjugate_verb("change", defender.pronoun_plurality()).c_str());
return;
}
dprf("shifter ok");
// or food that would incur divine penance...
if (god_hates_eating(you.religion, defender.type))
return;
dprf("god ok");
// can't eat enemies that leave no corpses...
if (!mons_class_can_leave_corpse(mons_species(defender.type))
|| defender.is_summoned()
|| defender.flags & MF_HARD_RESET)
{
return;
}
dprf("corpse ok");
// chow down.
_hydra_devour(defender);
}
/**
* Handle effects that fire when the defender (the target of the attack) is
* killed.
*
* @return Not sure; it seems to never be checked & always be true?
*/
bool melee_attack::handle_phase_killed()
{
if (attacker->is_player() && you.form == transformation::hydra
&& defender->is_monster() // better safe than sorry
&& defender->type != MONS_NO_MONSTER) // already reset
{
_hydra_consider_devouring(*defender->as_monster());
}
// Wyrmbane needs to be notified of deaths, including ones due to aux
// attacks, but other users of melee_effects() don't want to possibly
// be called twice. Adding another entry for a single artefact would
// be overkill, so here we call it by hand. check_unrand_effects()
// avoided triggering Wyrmbane's death effect earlier in the attack.
if (unrand_entry && weapon && weapon->unrand_idx == UNRAND_WYRMBANE)
{
unrand_entry->melee_effects(weapon, attacker, defender,
true, special_damage);
}
return attack::handle_phase_killed();
}
bool melee_attack::handle_phase_end()
{
if (!cleave_targets.empty())
{
attack_cleave_targets(*attacker, cleave_targets, attack_number,
effective_attack_number, wu_jian_attack);
}
// Check for passive mutation effects.
if (defender->is_player() && defender->alive() && attacker != defender)
{
mons_do_eyeball_confusion();
mons_do_tendril_disarm();
}
return attack::handle_phase_end();
}
/* Initiate the processing of the attack
*
* Called from the main code (fight.cc), this method begins the actual combat
* for a particular attack and is responsible for looping through each of the
* appropriate phases (which then can call other related phases within
* themselves).
*
* Returns whether combat was completely successful
* If combat was not successful, it could be any number of reasons, like
* the defender or attacker dying during the attack? or a defender moving
* from its starting position.
*/
bool melee_attack::attack()
{
if (!cleaving)
{
cleave_setup();
if (!handle_phase_attempted())
return false;
}
if (attacker != defender && attacker->is_monster()
&& mons_self_destructs(*attacker->as_monster()))
{
attacker->self_destruct();
return did_hit = perceived_attack = true;
}
string saved_gyre_name;
if (weapon && is_unrandom_artefact(*weapon, UNRAND_GYRE))
{
saved_gyre_name = get_artefact_name(*weapon);
set_artefact_name(*weapon, cleaving ? "quick blade \"Gimble\""
: "quick blade \"Gyre\"");
}
// Restore gyre's name before we return. We cannot use an unwind_var here
// because the precise address of the ARTEFACT_NAME_KEY property might
// change, for example if a summoned item is reset.
ON_UNWIND
{
if (!saved_gyre_name.empty() && weapon
&& is_unrandom_artefact(*weapon, UNRAND_GYRE))
{
set_artefact_name(*weapon, saved_gyre_name);
}
};
// Attacker might have died from effects of cleaving handled prior to this
if (!attacker->alive())
return false;
// We might have killed the kraken target by cleaving a tentacle.
if (!defender->alive())
{
handle_phase_killed();
handle_phase_end();
return attack_occurred;
}
// Apparently I'm insane for believing that we can still stay general past
// this point in the combat code, mebe I am! --Cryptic
// Calculate various ev values and begin to check them to determine the
// correct handle_phase_ handler.
const int ev = defender->evasion(ev_ignore::none, attacker);
ev_margin = test_hit(to_hit, ev, !attacker->is_player());
bool shield_blocked = attack_shield_blocked(true);
// Stuff for god conduct, this has to remain here for scope reasons.
god_conduct_trigger conducts[3];
// Remove sanctuary if - through some attack - it was violated.
if (env.sanctuary_time > 0 && attack_occurred && !cancel_attack
&& attacker != defender
&& (is_sanctuary(attack_position) || is_sanctuary(defender->pos()))
&& (attacker->is_player()
// XXX: Can friendly monsters actually violate sanctuary?
|| attacker->as_monster()->friendly() && !attacker->confused()))
{
remove_sanctuary(true);
}
if (attacker->is_player() && attacker != defender)
{
set_attack_conducts(conducts, *defender->as_monster(),
you.can_see(*defender));
if (player_under_penance(GOD_ELYVILON)
&& god_hates_your_god(GOD_ELYVILON)
&& ev_margin >= 0
&& one_chance_in(20))
{
simple_god_message(" blocks your attack.", GOD_ELYVILON);
handle_phase_end();
return false;
}
// Check for stab (and set stab_attempt and stab_bonus)
player_stab_check();
// Make sure we hit if we passed the stab check.
if (stab_attempt && stab_bonus > 0)
{
ev_margin = AUTOMATIC_HIT;
shield_blocked = false;
}
// Serpent's Lash does not miss
if (wu_jian_has_momentum(wu_jian_attack))
ev_margin = AUTOMATIC_HIT;
}
if (shield_blocked)
handle_phase_blocked();
else
{
if (attacker != defender && adjacent(defender->pos(), attack_position)
&& !is_riposte)
{
// Check for defender Spines
do_spines();
// Spines can kill! With Usk's pain bond, they can even kill the
// defender.
if (!attacker->alive() || !defender->alive())
return false;
}
if (ev_margin >= 0)
{
bool cont = handle_phase_hit();
attacker_sustain_passive_damage();
if (!cont)
{
if (!defender->alive())
handle_phase_killed();
handle_phase_end();
return false;
}
}
else
handle_phase_dodged();
}
if (attacker->is_player())
do_miscast();
// don't crash on banishment
if (!defender->pos().origin())
handle_noise(defender->pos());
// Noisy weapons.
if (attacker->is_player()
&& weapon
&& is_artefact(*weapon)
&& artefact_property(*weapon, ARTP_NOISE))
{
noisy_equipment();
}
alert_defender();
if (!defender->alive())
handle_phase_killed();
handle_phase_aux();
handle_phase_end();
return attack_occurred;
}
void melee_attack::check_autoberserk()
{
if (defender->is_monster() && mons_is_firewood(*defender->as_monster()))
return;
if (attacker->is_player())
{
for (int i = EQ_FIRST_EQUIP; i < NUM_EQUIP; ++i)
{
const item_def *item = you.slot_item(static_cast<equipment_type>(i));
if (!item)
continue;
if (!is_artefact(*item))
continue;
if (x_chance_in_y(artefact_property(*item, ARTP_ANGRY), 100))
{
attacker->go_berserk(false);
return;
}
}
}
else
{
for (int i = MSLOT_WEAPON; i <= MSLOT_JEWELLERY; ++i)
{
const item_def *item =
attacker->as_monster()->mslot_item(static_cast<mon_inv_type>(i));
if (!item)
continue;
if (!is_artefact(*item))
continue;
if (x_chance_in_y(artefact_property(*item, ARTP_ANGRY), 100))
{
attacker->go_berserk(false);
return;
}
}
}
}
bool melee_attack::check_unrand_effects()
{
if (unrand_entry && unrand_entry->melee_effects && weapon)
{
const bool died = !defender->alive();
// Don't trigger the Wyrmbane death effect yet; that is done in
// handle_phase_killed().
if (weapon->unrand_idx == UNRAND_WYRMBANE && died)
return true;