-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mon-cast.cc
7838 lines (6801 loc) · 249 KB
/
mon-cast.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 Monster spell casting.
**/
#include "AppHdr.h"
#include "mon-cast.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <unordered_set>
#include "abyss.h"
#include "act-iter.h"
#include "areas.h"
#include "attack.h"
#include "bloodspatter.h"
#include "branch.h"
#include "cleansing-flame-source-type.h"
#include "cloud.h"
#include "colour.h"
#include "coordit.h"
#include "database.h"
#include "delay.h"
#include "directn.h"
#include "english.h"
#include "env.h"
#include "tile-env.h"
#include "evoke.h"
#include "exclude.h"
#include "fight.h"
#include "fprop.h"
#include "god-passive.h"
#include "items.h"
#include "level-state-type.h"
#include "libutil.h"
#include "losglobal.h"
#include "mapmark.h"
#include "message.h"
#include "misc.h"
#include "mon-act.h"
#include "mon-ai-action.h"
#include "mon-behv.h"
#include "mon-clone.h"
#include "mon-death.h"
#include "mon-gear.h"
#include "mon-pathfind.h"
#include "mon-pick.h"
#include "mon-place.h"
#include "mon-project.h"
#include "mon-speak.h"
#include "mon-tentacle.h"
#include "mutation.h"
#include "player-stats.h"
#include "random.h"
#include "religion.h"
#include "shout.h"
#include "spl-clouds.h"
#include "spl-damage.h"
#include "spl-goditem.h"
#include "spl-monench.h"
#include "spl-summoning.h"
#include "spl-transloc.h"
#include "spl-util.h"
#include "spl-zap.h" // spell_to_zap
#include "state.h"
#include "stepdown.h"
#include "stringutil.h"
#include "tag-version.h"
#include "target.h"
#include "teleport.h"
#include "terrain.h"
#ifdef USE_TILE
#include "rltiles/tiledef-dngn.h"
#endif
#include "tileview.h"
#include "timed-effects.h"
#include "traps.h"
#include "travel.h"
#include "unwind.h"
#include "view.h"
#include "viewchar.h"
#include "xom.h"
static bool _valid_mon_spells[NUM_SPELLS];
static const string MIRROR_RECAST_KEY = "mirror_recast_time";
static god_type _find_god(const monster &mons, mon_spell_slot_flags flags);
static monster* _get_allied_target(const monster &caster, bolt &tracer);
static void _fire_simple_beam(monster &caster, mon_spell_slot, bolt &beam);
static void _fire_direct_explosion(monster &caster, mon_spell_slot, bolt &beam);
static int _mons_mesmerise(monster* mons, bool actual = true);
static int _mons_cause_fear(monster* mons, bool actual = true);
static int _mons_mass_confuse(monster* mons, bool actual = true);
static coord_def _mons_fragment_target(const monster &mons);
static coord_def _mons_awaken_earth_target(const monster& mon);
static void _maybe_throw_ally(const monster &mons);
static void _siren_sing(monster* mons, bool avatar);
static void _doom_howl(monster &mon);
static void _mons_awaken_earth(monster &mon, const coord_def &target);
static void _corrupt_locale(monster &mon);
static ai_action::goodness _monster_spell_goodness(monster* mon, mon_spell_slot slot);
static string _god_name(god_type god);
static coord_def _mons_ghostly_sacrifice_target(const monster &caster,
bolt tracer);
static function<void(bolt&, const monster&, int)>
_selfench_beam_setup(beam_type flavour);
static function<void(bolt&, const monster&, int)>
_zap_setup(spell_type spell);
static function<void(bolt&, const monster&, int)>
_buff_beam_setup(beam_type flavour);
static function<void(bolt&, const monster&, int)>
_target_beam_setup(function<coord_def(const monster&)> targeter);
static void _setup_minor_healing(bolt &beam, const monster &caster,
int = -1);
static void _setup_heal_other(bolt &beam, const monster &caster, int = -1);
static void _setup_creeping_frost(bolt &beam, const monster &caster, int pow);
static void _setup_pyroclastic_surge(bolt &beam, const monster &caster, int pow);
static ai_action::goodness _negative_energy_spell_goodness(const actor* foe);
static ai_action::goodness _caster_sees_foe(const monster &caster);
static ai_action::goodness _foe_sleep_viable(const monster &caster);
static ai_action::goodness _foe_tele_goodness(const monster &caster);
static ai_action::goodness _foe_mr_lower_goodness(const monster &caster);
static ai_action::goodness _still_winds_goodness(const monster &caster);
static ai_action::goodness _foe_near_wall(const monster &caster);
static ai_action::goodness _foe_not_nearby(const monster &caster);
static ai_action::goodness _foe_near_lava(const monster &caster);
static ai_action::goodness _mons_likes_blinking(const monster &caster);
static void _cast_injury_mirror(monster &mons, mon_spell_slot, bolt&);
static void _cast_smiting(monster &mons, mon_spell_slot slot, bolt&);
static void _cast_resonance_strike(monster &mons, mon_spell_slot, bolt&);
static void _cast_creeping_frost(monster &caster, mon_spell_slot, bolt&);
static void _cast_call_down_lightning(monster &caster, mon_spell_slot, bolt&);
static void _cast_pyroclastic_surge(monster &caster, mon_spell_slot, bolt&);
static void _cast_flay(monster &caster, mon_spell_slot, bolt&);
static void _flay(const monster &caster, actor &defender, int damage);
static void _cast_still_winds(monster &caster, mon_spell_slot, bolt&);
static void _mons_summon_elemental(monster &caster, mon_spell_slot, bolt&);
static bool _los_spell_worthwhile(const monster &caster, spell_type spell);
static void _setup_fake_beam(bolt& beam, const monster&, int = -1);
static void _branch_summon(monster &caster, mon_spell_slot slot, bolt&);
static void _branch_summon_helper(monster* mons, spell_type spell_cast);
static void _cast_marshlight(monster &caster, mon_spell_slot slot, bolt&);
static bool _prepare_ghostly_sacrifice(monster &caster, bolt &beam);
static void _setup_ghostly_beam(bolt &beam, int power, int dice);
static void _setup_ghostly_sacrifice_beam(bolt& beam, const monster& caster,
int power);
static ai_action::goodness _seracfall_goodness(const monster &caster);
static bool _prepare_seracfall(monster &caster, bolt &beam);
static void _setup_seracfall_beam(bolt& beam, const monster& caster, int power);
static function<ai_action::goodness(const monster&)> _setup_hex_check(spell_type spell);
static ai_action::goodness _hexing_goodness(const monster &caster, spell_type spell);
static bool _torment_vulnerable(const actor* victim);
static void _cast_grasping_roots(monster &caster, mon_spell_slot, bolt&);
static int _monster_abjuration(const monster& caster, bool actual);
static ai_action::goodness _mons_will_abjure(const monster& mons);
static bool _maybe_irradiate(monster *mons);
enum spell_logic_flag
{
MSPELL_NO_AUTO_NOISE = 1 << 0, ///< silent, or noise generated specially
};
DEF_BITFIELD(spell_logic_flags, spell_logic_flag);
constexpr spell_logic_flags MSPELL_LOGIC_NONE{};
struct mons_spell_logic
{
/// Can casting this spell right now accomplish anything useful?
function<ai_action::goodness(const monster&)> calc_goodness;
/// Actually cast the given spell.
function<void(monster&, mon_spell_slot, bolt&)> cast;
/// Setup a targeting/effect beam for the given spell, if applicable.
function<void(bolt&, const monster&, int)> setup_beam;
/// What special behaviors does this spell have for monsters?
spell_logic_flags flags;
/// How much 'power' is this spell cast with per caster HD? If 0, ignore
int power_hd_factor;
};
static ai_action::goodness _always_worthwhile(const monster &/*caster*/)
{
return ai_action::good(); // or neutral?
}
static function<ai_action::goodness(const monster&)> _should_selfench(enchant_type ench);
static mons_spell_logic _conjuration_logic(spell_type spell);
static mons_spell_logic _hex_logic(spell_type spell,
function<ai_action::goodness(const monster&)> extra_logic
= nullptr,
int power_hd_factor = 0);
/// How do monsters go about casting spells?
static const map<spell_type, mons_spell_logic> spell_to_logic = {
{ SPELL_MIGHT, {
_should_selfench(ENCH_MIGHT),
_fire_simple_beam,
_selfench_beam_setup(BEAM_MIGHT),
} },
{ SPELL_INVISIBILITY, {
_should_selfench(ENCH_INVIS),
_fire_simple_beam,
_selfench_beam_setup(BEAM_INVISIBILITY),
} },
{ SPELL_HASTE, {
_should_selfench(ENCH_HASTE),
_fire_simple_beam,
_selfench_beam_setup(BEAM_HASTE),
} },
{ SPELL_MINOR_HEALING, {
[](const monster &caster) {
return ai_action::good_or_bad(caster.hit_points <= caster.max_hit_points / 2);
},
_fire_simple_beam,
_setup_minor_healing,
} },
{ SPELL_SLUG_DART, _conjuration_logic(SPELL_SLUG_DART) },
{ SPELL_VAMPIRIC_DRAINING, {
[](const monster &caster)
{
const actor* foe = caster.get_foe();
ASSERT(foe);
// always cast at < 1/3rd hp, never at > 2/3rd hp
const bool low_hp = x_chance_in_y(caster.max_hit_points * 2 / 3
- caster.hit_points,
caster.max_hit_points / 3);
if (!adjacent(caster.pos(), foe->pos()))
return ai_action::impossible();
if (!actor_is_susceptible_to_vampirism(*foe))
return ai_action::impossible();
return min(_negative_energy_spell_goodness(foe),
ai_action::good_or_bad(low_hp));
},
_fire_simple_beam,
_zap_setup(SPELL_VAMPIRIC_DRAINING),
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_CANTRIP, {
[](const monster &caster)
{
return ai_action::good_or_bad(caster.get_foe());
},
nullptr,
nullptr,
} },
{ SPELL_INJURY_MIRROR, {
[](const monster &caster) {
return ai_action::good_or_impossible(
!caster.has_ench(ENCH_MIRROR_DAMAGE)
&& (!caster.props.exists(MIRROR_RECAST_KEY)
|| you.elapsed_time >=
caster.props[MIRROR_RECAST_KEY].get_int()));
},
_cast_injury_mirror,
nullptr,
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_DRAIN_LIFE, {
[](const monster &caster) {
return ai_action::good_or_bad(
_los_spell_worthwhile(caster, SPELL_DRAIN_LIFE)
&& (!caster.friendly()
|| !you.visible_to(&caster)
|| player_prot_life(false) >= 3));
},
[](monster &caster, mon_spell_slot slot, bolt&) {
const int splpow = mons_spellpower(caster, slot.spell);
int damage = 0;
fire_los_attack_spell(slot.spell, splpow, &caster, false, &damage);
if (damage > 0 && caster.heal(damage))
simple_monster_message(caster, " is healed.");
},
_zap_setup(SPELL_DRAIN_LIFE),
MSPELL_NO_AUTO_NOISE,
1,
} },
{ SPELL_OZOCUBUS_REFRIGERATION, {
[](const monster &caster) {
return ai_action::good_or_bad(
_los_spell_worthwhile(caster, SPELL_OZOCUBUS_REFRIGERATION)
&& (!caster.friendly() || !you.visible_to(&caster)));
},
[](monster &caster, mon_spell_slot slot, bolt&) {
const int splpow = mons_spellpower(caster, slot.spell);
fire_los_attack_spell(slot.spell, splpow, &caster, false);
},
_zap_setup(SPELL_OZOCUBUS_REFRIGERATION),
MSPELL_LOGIC_NONE,
5,
} },
{ SPELL_TROGS_HAND, {
[](const monster &caster) {
return ai_action::good_or_impossible(
!caster.has_ench(ENCH_STRONG_WILLED)
&& !caster.has_ench(ENCH_REGENERATION));
},
[](monster &caster, mon_spell_slot, bolt&) {
const string god = apostrophise(god_name(caster.god));
const string msg = make_stringf(" invokes %s protection!",
god.c_str());
simple_monster_message(caster, msg.c_str(), MSGCH_MONSTER_SPELL);
// Not spell_hd(spell_cast); this is an invocation
const int dur = BASELINE_DELAY
* min(5 + roll_dice(2, (caster.get_hit_dice() * 10) / 3 + 1),
100);
caster.add_ench(mon_enchant(ENCH_STRONG_WILLED, 0, &caster, dur));
caster.add_ench(mon_enchant(ENCH_REGENERATION, 0, &caster, dur));
dprf("Trog's Hand cast (dur: %d aut)", dur);
},
nullptr,
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_LEDAS_LIQUEFACTION, {
[](const monster &caster) {
return ai_action::good_or_impossible(!caster.has_ench(ENCH_LIQUEFYING));
},
[](monster &caster, mon_spell_slot, bolt&) {
if (you.can_see(caster))
{
mprf("%s liquefies the ground around %s!",
caster.name(DESC_THE).c_str(),
caster.pronoun(PRONOUN_REFLEXIVE).c_str());
flash_view_delay(UA_MONSTER, BROWN, 80);
}
caster.add_ench(ENCH_LIQUEFYING);
invalidate_agrid(true);
},
nullptr,
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_FORCEFUL_INVITATION, {
_always_worthwhile,
_branch_summon,
nullptr,
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_PLANEREND, {
_always_worthwhile,
_branch_summon,
nullptr,
MSPELL_NO_AUTO_NOISE,
} },
{ SPELL_MARSHLIGHT, {
_always_worthwhile,
_cast_marshlight,
} },
{ SPELL_STILL_WINDS, { _still_winds_goodness, _cast_still_winds } },
{ SPELL_SMITING, { _always_worthwhile, _cast_smiting, } },
{ SPELL_CALL_DOWN_LIGHTNING, { _foe_not_nearby, _cast_call_down_lightning, _zap_setup(SPELL_CALL_DOWN_LIGHTNING) } },
{ SPELL_RESONANCE_STRIKE, { _always_worthwhile, _cast_resonance_strike, } },
{ SPELL_CREEPING_FROST, { _foe_near_wall, _cast_creeping_frost, _setup_creeping_frost } },
{ SPELL_PYROCLASTIC_SURGE, { _foe_near_lava, _cast_pyroclastic_surge, _setup_pyroclastic_surge } },
{ SPELL_FLAY, {
[](const monster &caster) {
const actor* foe = caster.get_foe(); // XXX: check vis?
ASSERT(foe);
return ai_action::good_or_impossible(!!(foe->holiness() & MH_NATURAL));
},
_cast_flay,
} },
{ SPELL_PARALYSIS_GAZE, {
_caster_sees_foe,
[](monster &caster, mon_spell_slot, bolt&) {
caster.get_foe()->paralyse(&caster, 2 + random2(3));
},
} },
{ SPELL_DRAINING_GAZE, {
_caster_sees_foe,
[](monster &caster, mon_spell_slot slot, bolt&) {
enchant_actor_with_flavour(caster.get_foe(), &caster,
BEAM_DRAIN_MAGIC,
mons_spellpower(caster, slot.spell));
},
} },
{ SPELL_WEAKENING_GAZE, {
_caster_sees_foe,
[](monster &caster, mon_spell_slot, bolt&) {
caster.get_foe()->weaken(&caster, caster.get_hit_dice());
},
} },
{ SPELL_WATER_ELEMENTALS, { _always_worthwhile, _mons_summon_elemental } },
{ SPELL_EARTH_ELEMENTALS, { _always_worthwhile, _mons_summon_elemental } },
{ SPELL_AIR_ELEMENTALS, { _always_worthwhile, _mons_summon_elemental } },
{ SPELL_FIRE_ELEMENTALS, { _always_worthwhile, _mons_summon_elemental } },
{ SPELL_HASTE_OTHER, {
_always_worthwhile,
_fire_simple_beam,
_buff_beam_setup(BEAM_HASTE)
} },
{ SPELL_MIGHT_OTHER, {
_always_worthwhile,
_fire_simple_beam,
_buff_beam_setup(BEAM_MIGHT)
} },
{ SPELL_INVISIBILITY_OTHER, {
_always_worthwhile,
_fire_simple_beam,
_buff_beam_setup(BEAM_INVISIBILITY)
} },
{ SPELL_HEAL_OTHER, {
_always_worthwhile,
_fire_simple_beam,
_setup_heal_other,
} },
{ SPELL_LRD, {
_always_worthwhile,
[](monster &caster, mon_spell_slot slot, bolt& pbolt) {
const int splpow = mons_spellpower(caster, slot.spell);
cast_fragmentation(splpow, &caster, pbolt.target, false);
},
_target_beam_setup(_mons_fragment_target),
MSPELL_LOGIC_NONE, 6
} },
{ SPELL_AWAKEN_EARTH, {
_always_worthwhile,
[](monster &caster, mon_spell_slot, bolt& pbolt) {
_mons_awaken_earth(caster, pbolt.target);
},
_target_beam_setup(_mons_awaken_earth_target),
} },
{ SPELL_GHOSTLY_SACRIFICE, {
_always_worthwhile,
[](monster &caster, mon_spell_slot slot, bolt& pbolt) {
if (_prepare_ghostly_sacrifice(caster, pbolt))
_fire_direct_explosion(caster, slot, pbolt);
else if (you.can_see(caster))
canned_msg(MSG_NOTHING_HAPPENS);
},
_setup_ghostly_sacrifice_beam,
} },
{ SPELL_SLOW, _hex_logic(SPELL_SLOW) },
{ SPELL_CONFUSE, _hex_logic(SPELL_CONFUSE) },
{ SPELL_BANISHMENT, _hex_logic(SPELL_BANISHMENT) },
{ SPELL_PARALYSE, _hex_logic(SPELL_PARALYSE) },
{ SPELL_PETRIFY, _hex_logic(SPELL_PETRIFY) },
{ SPELL_PAIN, _hex_logic(SPELL_PAIN, [](const monster& caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
// why does this check rTorment, not rN?
return ai_action::good_or_impossible(_torment_vulnerable(foe));
}) },
{ SPELL_MINDBURST, _hex_logic(SPELL_MINDBURST) },
{ SPELL_CORONA, _hex_logic(SPELL_CORONA, [](const monster& caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(!foe->backlit());
}) },
{ SPELL_POLYMORPH, _hex_logic(SPELL_POLYMORPH, [](const monster& caster) {
return ai_action::good_or_bad(!caster.friendly()); // too dangerous to let allies use
}) },
{ SPELL_SLEEP, _hex_logic(SPELL_SLEEP, _foe_sleep_viable, 6) },
{ SPELL_HIBERNATION, _hex_logic(SPELL_HIBERNATION, _foe_sleep_viable) },
{ SPELL_TELEPORT_OTHER, _hex_logic(SPELL_TELEPORT_OTHER,
_foe_tele_goodness) },
{ SPELL_DIMENSION_ANCHOR, _hex_logic(SPELL_DIMENSION_ANCHOR, nullptr, 6)},
{ SPELL_AGONY_RANGE, _hex_logic(SPELL_AGONY_RANGE, [](const monster &caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(_torment_vulnerable(foe));
}, 6)
},
{ SPELL_AGONY, _hex_logic(SPELL_AGONY, [](const monster &caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(_torment_vulnerable(foe));
}, 6)
},
{ SPELL_STRIP_WILLPOWER,
_hex_logic(SPELL_STRIP_WILLPOWER, _foe_mr_lower_goodness, 6)
},
{ SPELL_SENTINEL_MARK, _hex_logic(SPELL_SENTINEL_MARK, nullptr, 16) },
{ SPELL_SAP_MAGIC, {
_always_worthwhile, _fire_simple_beam, _zap_setup(SPELL_SAP_MAGIC),
MSPELL_LOGIC_NONE, 10,
} },
{ SPELL_DRAIN_MAGIC, _hex_logic(SPELL_DRAIN_MAGIC, nullptr, 6) },
{ SPELL_VIRULENCE, _hex_logic(SPELL_VIRULENCE, [](const monster &caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(foe->res_poison(false) < 3);
}, 6) },
{ SPELL_GRASPING_ROOTS, {
[](const monster &caster)
{
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(caster.can_constrict(foe, false));
}, _cast_grasping_roots, } },
{ SPELL_ABJURATION, {
_mons_will_abjure,
[] (const monster &caster, mon_spell_slot /*slot*/, bolt& /*beem*/) {
_monster_abjuration(caster, true);
}, nullptr, MSPELL_LOGIC_NONE, 20, } },
{ SPELL_CONCENTRATE_VENOM, {
_always_worthwhile,
_fire_simple_beam,
_buff_beam_setup(BEAM_CONCENTRATE_VENOM)
} },
{ SPELL_BLINK, {
_mons_likes_blinking,
[] (monster &caster, mon_spell_slot /*slot*/, bolt& /*beem*/) {
monster_blink(&caster);
}
}},
{ SPELL_BLINK_RANGE, {
_mons_likes_blinking,
[] (monster &caster, mon_spell_slot /*slot*/, bolt& /*beem*/) {
blink_range(&caster);
}
} },
{ SPELL_BLINK_AWAY, {
_mons_likes_blinking,
[] (monster &caster, mon_spell_slot /*slot*/, bolt& /*beem*/) {
blink_away(&caster, true);
}
} },
{ SPELL_BLINK_CLOSE, {
[](const monster &caster) {
const actor* foe = caster.get_foe();
ASSERT(foe);
if (adjacent(caster.pos(), foe->pos()))
return ai_action::bad();
return _mons_likes_blinking(caster);
},
[] (monster &caster, mon_spell_slot /*slot*/, bolt& /*beem*/) {
blink_close(&caster);
}
} },
{ SPELL_CORRUPT_LOCALE, {
[](const monster & /* caster */) {
return ai_action::good_or_impossible(!player_in_branch(BRANCH_ABYSS));
},
[](monster &caster, mon_spell_slot, bolt&) {
_corrupt_locale(caster);
},
}, },
{ SPELL_SERACFALL, {
_seracfall_goodness,
[](monster &caster, mon_spell_slot slot, bolt& pbolt) {
if (_prepare_seracfall(caster, pbolt))
_fire_simple_beam(caster, slot, pbolt);
else if (you.can_see(caster))
canned_msg(MSG_NOTHING_HAPPENS);
},
_setup_seracfall_beam,
MSPELL_LOGIC_NONE,
30, // 2.5x iceblast
} },
};
/// Create the appropriate casting logic for a simple conjuration.
static mons_spell_logic _conjuration_logic(spell_type spell)
{
return { _always_worthwhile, _fire_simple_beam, _zap_setup(spell), };
}
/**
* Create the appropriate casting logic for a simple mr-checking hex.
*
* @param spell The hex in question; e.g. SPELL_CORONA.
* @param extra_logic An additional pre-casting condition, beyond the
* normal hex logic.
* @param power_hd_factor If nonzero, how much spellpower the spell has per
* caster HD.
*/
static mons_spell_logic _hex_logic(spell_type spell,
function<ai_action::goodness(const monster&)> extra_logic,
int power_hd_factor)
{
function<ai_action::goodness(const monster&)> calc_goodness = nullptr;
if (!extra_logic)
calc_goodness = _setup_hex_check(spell);
else
{
calc_goodness = [spell, extra_logic](const monster& caster) {
return min(_hexing_goodness(caster, spell), extra_logic(caster));
};
}
return { calc_goodness, _fire_simple_beam, _zap_setup(spell),
MSPELL_LOGIC_NONE, power_hd_factor * ENCH_POW_FACTOR };
}
/**
* Take the given beam and fire it, handling screen-refresh issues in the
* process.
*
* @param caster The monster casting the spell that produced the beam.
* @param pbolt A pre-setup & aimed spell beam. (For e.g. FIREBALL.)
*/
static void _fire_simple_beam(monster &/*caster*/, mon_spell_slot, bolt &pbolt)
{
// If a monster just came into view and immediately cast a spell,
// we need to refresh the screen before drawing the beam.
viewwindow();
update_screen();
pbolt.fire();
}
/**
* Take the given explosion and fire it, handling screen-refresh issues in the
* process.
*
* @param caster The monster casting the spell that produced the beam.
* @param pbolt A pre-setup & aimed spell beam. (For e.g. FIRE_STORM.)
*/
static void _fire_direct_explosion(monster &caster, mon_spell_slot, bolt &pbolt)
{
// If a monster just came into view and immediately cast a spell,
// we need to refresh the screen before drawing the beam.
viewwindow();
update_screen();
const actor* foe = caster.get_foe();
const bool need_more = foe && (foe->is_player()
|| you.see_cell(foe->pos()));
pbolt.in_explosion_phase = false;
pbolt.refine_for_explosion();
pbolt.explode(need_more);
}
static ai_action::goodness _caster_sees_foe(const monster &caster)
{
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(caster.can_see(*foe));
}
static ai_action::goodness _foe_sleep_viable(const monster &caster)
{
const actor* foe = caster.get_foe();
ASSERT(foe);
return ai_action::good_or_impossible(foe->can_sleep());
}
/// use when a dur/ench-type effect can only be cast once
static ai_action::goodness _foe_effect_viable(const monster &caster, duration_type d, enchant_type e)
{
const actor* foe = caster.get_foe();
ASSERT(foe);
if (foe->is_player())
return ai_action::good_or_impossible(!you.duration[d]);
else
return ai_action::good_or_impossible(!foe->as_monster()->has_ench(e));
}
static ai_action::goodness _foe_tele_goodness(const monster &caster)
{
return _foe_effect_viable(caster, DUR_TELEPORT, ENCH_TP);
}
static ai_action::goodness _foe_mr_lower_goodness(const monster &caster)
{
return _foe_effect_viable(caster, DUR_LOWERED_WL, ENCH_LOWERED_WL);
}
/**
* Build a function to set up a beam to buff the caster.
*
* @param flavour The flavour to buff a caster with.
* @return A function that sets up a beam to buff its caster with
* the given flavour.
*/
static function<void(bolt&, const monster&, int)>
_selfench_beam_setup(beam_type flavour)
{
return [flavour](bolt &beam, const monster &caster, int)
{
beam.flavour = flavour;
if (!mons_is_player_shadow(caster))
beam.target = caster.pos();
};
}
/**
* Build a function that tests whether it's worth casting a buff spell that
* applies the given enchantment to the caster.
*/
static function<ai_action::goodness(const monster&)> _should_selfench(enchant_type ench)
{
return [ench](const monster &caster)
{
// keep non-summoned pals with haste from spamming constantly
if (caster.friendly() && !caster.get_foe() && !caster.is_summoned())
return ai_action::bad();
return ai_action::good_or_impossible(!caster.has_ench(ench));
};
}
/**
* Build a function that sets up and targets a buffing beam at one of the
* caster's allies. If the function fails to find an ally, the beam will be
* targeted at an out-of-bounds tile to signal failure.
*
* @param flavour The flavour to buff an ally with.
* @return A function that sets up a single-target buff beam.
*/
static function<void(bolt&, const monster&, int)>
_buff_beam_setup(beam_type flavour)
{
return [flavour](bolt &beam, const monster &caster, int)
{
beam.flavour = flavour;
const monster* target = _get_allied_target(caster, beam);
beam.target = target ? target->pos() : coord_def(GXM+1, GYM+1);
};
}
/**
* Build a function to set up a beam with spl-to-zap.
*
* @param spell The spell for which beams will be set up.
* @return A function that sets up a beam to zap the given spell.
*/
static function<void(bolt&, const monster&, int)> _zap_setup(spell_type spell)
{
return [spell](bolt &beam, const monster &, int power)
{
zappy(spell_to_zap(spell), power, true, beam);
};
}
static void _setup_healing_beam(bolt &beam, const monster &caster)
{
beam.damage = dice_def(2, caster.spell_hd(SPELL_MINOR_HEALING) / 2);
beam.flavour = BEAM_HEALING;
}
static void _setup_minor_healing(bolt &beam, const monster &caster, int)
{
_setup_healing_beam(beam, caster);
if (!mons_is_player_shadow(caster))
beam.target = caster.pos();
}
static void _setup_heal_other(bolt &beam, const monster &caster, int)
{
_setup_healing_beam(beam, caster);
const monster* target = _get_allied_target(caster, beam);
beam.target = target ? target->pos() : coord_def(GXM+1, GYM+1);
}
/**
* Build a function that sets up a fake beam for targeting special spells.
*
* @param targeter A function that finds a target for the given spell.
* Expected to return an out-of-bounds coord on failure.
* @return A function that initializes a fake targetting beam.
*/
static function<void(bolt&, const monster&, int)>
_target_beam_setup(function<coord_def(const monster&)> targeter)
{
return [targeter](bolt& beam, const monster& caster, int)
{
_setup_fake_beam(beam, caster);
// Your shadow keeps your targetting.
if (mons_is_player_shadow(caster))
return;
beam.target = targeter(caster);
beam.aimed_at_spot = true; // to get noise to work properly
};
}
static void _cast_injury_mirror(monster &mons, mon_spell_slot /*slot*/, bolt&)
{
const string msg
= make_stringf(" offers %s to %s, and fills with unholy energy.",
mons.pronoun(PRONOUN_REFLEXIVE).c_str(),
god_name(mons.god).c_str());
simple_monster_message(mons, msg.c_str(), MSGCH_MONSTER_SPELL);
mons.add_ench(mon_enchant(ENCH_MIRROR_DAMAGE, 0, &mons,
random_range(7, 9) * BASELINE_DELAY));
mons.props[MIRROR_RECAST_KEY].get_int()
= you.elapsed_time + 150 + random2(60);
}
static void _cast_smiting(monster &caster, mon_spell_slot slot, bolt&)
{
const god_type god = _find_god(caster, slot.flags);
actor* foe = caster.get_foe();
ASSERT(foe);
if (foe->is_player())
mprf("%s smites you!", _god_name(god).c_str());
else
simple_monster_message(*foe->as_monster(), " is smitten.");
foe->hurt(&caster, 7 + random2avg(11, 2), BEAM_MISSILE, KILLED_BY_BEAM,
"", "by divine providence");
}
static void _cast_grasping_roots(monster &caster, mon_spell_slot, bolt&)
{
actor* foe = caster.get_foe();
ASSERT(foe);
const int turns = 4 + random2avg(div_rand_round(
mons_spellpower(caster, SPELL_GRASPING_ROOTS), 10), 2);
dprf("Grasping roots turns: %d", turns);
mpr("Roots burst forth from the earth!");
if (foe->is_player())
{
you.increase_duration(DUR_GRASPING_ROOTS, turns);
caster.start_constricting(you);
mprf(MSGCH_WARN, "The grasping roots grab you!");
}
else
{
foe->as_monster()->add_ench(mon_enchant(ENCH_GRASPING_ROOTS, 0, &caster,
turns * BASELINE_DELAY));
}
}
/// Is the given full-LOS attack spell worth casting for the given monster?
static bool _los_spell_worthwhile(const monster &mons, spell_type spell)
{
return trace_los_attack_spell(spell, mons_spellpower(mons, spell), &mons)
== spret::success;
}
/// Set up a fake beam, for noise-generating purposes (?)
static void _setup_fake_beam(bolt& beam, const monster&, int)
{
beam.flavour = BEAM_DEVASTATION;
beam.pierce = true;
// Doesn't take distance into account, but this is just a tracer so
// we'll ignore that. We need some damage on the tracer so the monster
// doesn't think the spell is useless against other monsters.
beam.damage = CONVENIENT_NONZERO_DAMAGE;
beam.range = LOS_RADIUS;
}
/**
* Create a summoned monster.
*
* @param summoner The monster doing the summoning.
* @param mtyp The type of monster to summon.
* @param dur The duration for which the monster should last.
* Not in aut or turns; nonlinear. Sorry!
* @param slot The spell & slot flags.
* @return The summoned creature, if any.
*/
static monster* _summon(const monster &summoner, monster_type mtyp, int dur,
mon_spell_slot slot)
{
const god_type god = _find_god(summoner, slot.flags);
return create_monster(
mgen_data(mtyp, SAME_ATTITUDE((&summoner)), summoner.pos(),
summoner.foe)
.set_summoned(&summoner, dur, slot.spell, god));
}
void init_mons_spells()
{
monster fake_mon;
fake_mon.type = MONS_BLACK_DRACONIAN;
fake_mon.hit_points = 1;
fake_mon.mid = MID_NOBODY; // used indirectly, through _mon_special_name
bolt pbolt;
for (int i = 0; i < NUM_SPELLS; i++)
{
spell_type spell = (spell_type) i;
_valid_mon_spells[i] = false;
if (!is_valid_spell(spell))
continue;
if (setup_mons_cast(&fake_mon, pbolt, spell, false, true))
_valid_mon_spells[i] = true;
}
}
bool is_valid_mon_spell(spell_type spell)
{
if (spell < 0 || spell >= NUM_SPELLS)
return false;
return _valid_mon_spells[spell];
}
/// Is the current spell being cast via player wizmode &z by a dummy mons?
static bool _is_wiz_cast()
{
#ifdef WIZARD
// iffy logic but might be right enough
return crawl_state.prev_cmd == CMD_WIZARD;
#else
return false;
#endif
}
static bool _flavour_benefits_monster(beam_type flavour, monster& monster)
{
switch (flavour)
{
case BEAM_HASTE:
return !monster.has_ench(ENCH_HASTE);
case BEAM_MIGHT:
return !monster.has_ench(ENCH_MIGHT);
case BEAM_INVISIBILITY:
return !monster.has_ench(ENCH_INVIS);
case BEAM_HEALING:
return monster.hit_points != monster.max_hit_points;
case BEAM_AGILITY:
return !monster.has_ench(ENCH_AGILE);
case BEAM_RESISTANCE:
return !monster.has_ench(ENCH_RESISTANCE);
case BEAM_CONCENTRATE_VENOM:
return !monster.has_ench(ENCH_CONCENTRATE_VENOM)
&& (monster.has_spell(SPELL_SPIT_POISON)
|| monster.has_attack_flavour(AF_POISON)
|| monster.has_attack_flavour(AF_POISON_STRONG));
default:
return false;
}
}
/**
* Will the given monster consider buffing the given target? (Are they close
* enough in type, genus, attitude, etc?)
*
* @param caster The monster casting a targeted buff spell.
* @param targ The monster to be buffed.
* @return Whether the monsters are similar enough.
*/
static bool _monster_will_buff(const monster &caster, const monster &targ)
{
if (mons_is_firewood(targ))
return false;
if (!mons_aligned(&targ, &caster))
return false;
// don't buff only temporarily-aligned pals (charmed, hexed)
if (!mons_atts_aligned(caster.temp_attitude(), targ.real_attitude()))
return false;
if (caster.type == MONS_IRONBOUND_CONVOKER || mons_bound_soul(caster))
return true; // will buff any ally
if (targ.is_holy() && caster.is_holy())
return true;
const monster_type caster_genus = mons_genus(caster.type);
// Order level buffs
if (caster_genus == MONS_NAGA && mons_genus(targ.type) == MONS_SNAKE)
return true;
return mons_genus(targ.type) == caster_genus
|| mons_genus(targ.base_monster) == caster_genus;
}
/// Find an allied monster to cast a beneficial beam spell at.
static monster* _get_allied_target(const monster &caster, bolt &tracer)
{
monster* selected_target = nullptr;
int min_distance = tracer.range;
for (monster_near_iterator targ(&caster, LOS_NO_TRANS); targ; ++targ)
{
if (*targ == &caster
|| !_monster_will_buff(caster, **targ)
|| !_flavour_benefits_monster(tracer.flavour, **targ))
{
continue;
}
// prefer the closest ally we can find (why?)
const int targ_distance = grid_distance(targ->pos(), caster.pos());
if (targ_distance < min_distance)
{
// Make sure we won't hit someone other than we're aiming at.
tracer.target = targ->pos();
fire_tracer(&caster, tracer);
if (!mons_should_fire(tracer)
|| tracer.path_taken.back() != tracer.target)
{
continue;
}
min_distance = targ_distance;
selected_target = *targ;