File: fineff.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (1835 lines) | stat: -rw-r--r-- 51,312 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
/**
 * @file
 * @brief Brand/ench/etc effects that might alter something in an
 *             unexpected way.
**/

#include "AppHdr.h"

#include "fineff.h"

#include "act-iter.h"
#include "actor.h"
#include "attitude-change.h"
#include "beam.h"
#include "bloodspatter.h"
#include "coordit.h"
#include "dactions.h"
#include "death-curse.h"
#include "delay.h" // stop_delay
#include "directn.h"
#include "english.h"
#include "env.h"
#include "evoke.h"
#include "fight.h"
#include "god-abil.h"
#include "god-companions.h"
#include "god-wrath.h" // lucy_check_meddling
#include "libutil.h"
#include "losglobal.h"
#include "melee-attack.h"
#include "message.h"
#include "mgen-data.h"
#include "mon-abil.h"
#include "mon-act.h"
#include "mon-behv.h"
#include "mon-cast.h"
#include "mon-death.h"
#include "mon-place.h"
#include "mon-util.h"
#include "monster.h"
#include "movement.h"
#include "ouch.h"
#include "religion.h"
#include "spl-damage.h"
#include "spl-monench.h"
#include "spl-summoning.h"
#include "state.h"
#include "stringutil.h"
#include "terrain.h"
#include "transform.h"
#include "view.h"

class final_effect
{
public:
    virtual ~final_effect() {}

    virtual bool mergeable(const final_effect& a) const = 0;
    virtual void merge(const final_effect&)
    {
    }

    virtual void fire() = 0;

protected:
    mid_t att, def;
    coord_def posn;
    actor* attacker() const { return actor_by_mid(att); }
    actor* defender() const { return actor_by_mid(def); }

    final_effect(const actor* attack, const actor* defend, const coord_def& pos)
        : att(attack ? attack->mid : 0),
        def(defend ? defend->mid : 0),
        posn(pos)
    {
    }
};

class mirror_damage_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    mirror_damage_fineff(const actor* attack, const actor* defend, int dam)
        : final_effect(attack, defend, coord_def()), damage(dam)
    {
    }
protected:
    int damage;
};

class anguish_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    anguish_fineff(const actor* attack, int dam)
        : final_effect(attack, nullptr, coord_def()), damage(dam)
    {
    }
protected:
    int damage;
};

class ru_retribution_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    ru_retribution_fineff(const actor* attack, const actor* defend, int dam)
        : final_effect(attack, defend, coord_def()), damage(dam)
    {
    }
protected:
    int damage;
};

class trample_follow_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    trample_follow_fineff(const actor* attack, const coord_def& pos)
        : final_effect(attack, 0, pos)
    {
    }
};

class blink_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    blink_fineff(const actor* blinker, const actor* o)
        : final_effect(o, blinker, coord_def())
    {
    }
};

class teleport_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    teleport_fineff(const actor* defend)
        : final_effect(0, defend, coord_def())
    {
    }
};

class trj_spawn_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    trj_spawn_fineff(const actor* attack, const actor* defend,
        const coord_def& pos, int dam)
        : final_effect(attack, defend, pos), damage(dam)
    {
    }
protected:
    int damage;
};

class blood_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;
    void merge(const final_effect& a) override;

    blood_fineff(const actor* defend, const coord_def& pos, int blood_amount)
        : final_effect(0, 0, pos), mtype(defend->type), blood(blood_amount)
    {
    }
protected:
    monster_type mtype;
    int blood;
};

class deferred_damage_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    deferred_damage_fineff(const actor* attack, const actor* defend,
        int dam, bool _attacker_effects, bool _fatal)
        : final_effect(attack, defend, coord_def()),
        damage(dam), attacker_effects(_attacker_effects), fatal(_fatal)
    {
    }
protected:
    int damage;
    bool attacker_effects;
    bool fatal;
};

class starcursed_merge_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    starcursed_merge_fineff(const actor* merger)
        : final_effect(0, merger, coord_def())
    {
    }
};

class shock_discharge_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void merge(const final_effect& a) override;
    void fire() override;

    shock_discharge_fineff(const actor* discharger, actor& rudedude,
        coord_def pos, int pow, string shock_src)
        : final_effect(0, discharger, coord_def()), oppressor(rudedude),
        position(pos), power(pow), shock_source(shock_src)
    {
    }
protected:
    actor& oppressor;
    coord_def position;
    int power;
    string shock_source;
};

class explosion_fineff : public final_effect
{
public:
    // One explosion at a time, please.
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    explosion_fineff(const bolt& beem, string boom, string sanct,
        explosion_fineff_type _typ, const actor* agent,
        string poof)
        : final_effect(0, 0, coord_def()), beam(beem),
        boom_message(boom), sanctuary_message(sanct),
        typ(_typ), flame_agent(agent), poof_message(poof)
    {
    }
protected:
    bolt beam;
    string boom_message;
    string sanctuary_message;
    explosion_fineff_type typ;
    const actor* flame_agent;
    string poof_message;
};

class splinterfrost_fragment_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    splinterfrost_fragment_fineff(bolt beem, string _msg)
        : final_effect(0, 0, coord_def()), beam(beem), msg(_msg)
    {
    }
protected:
    bolt beam;
    string msg;
};

// A fineff that triggers a daction; otherwise the daction
// occurs immediately (and then later) -- this might actually
// be too soon in some cases.
class delayed_action_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    virtual void fire() override;

    delayed_action_fineff(daction_type _action, const string& _final_msg)
        : final_effect(0, 0, coord_def()),
        action(_action), final_msg(_final_msg)
    {
    }
protected:
    daction_type action;
    string final_msg;
};

class kirke_death_fineff : public delayed_action_fineff
{
public:
    void fire() override;

    kirke_death_fineff(const string& _final_msg)
        : delayed_action_fineff(DACT_KIRKE_HOGS, _final_msg)
    {
    }
};

class rakshasa_clone_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    rakshasa_clone_fineff(const actor* defend, const coord_def& pos)
        : final_effect(0, defend, pos)
    {
    }
protected:
    int damage;
};

class bennu_revive_fineff : public final_effect
{
public:
    // Each trigger is from the death of a different bennu---no merging.
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    bennu_revive_fineff(const monster* bennu)
        : final_effect(bennu, 0, bennu->pos())
    {
        env.final_effect_monster_cache.push_back(*bennu);
    }
};

class avoided_death_fineff : public final_effect
{
public:
    // Each trigger is from the death of a different monster---no merging.
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    avoided_death_fineff(const actor* _def, int _hp)
        : final_effect(0, _def, coord_def()), hp(_hp)
    {
    }
protected:
    int hp;
};

class infestation_death_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    infestation_death_fineff(coord_def pos, const string& _name)
        : final_effect(0, 0, pos), name(_name)
    {
    }
protected:
    string name;
};

class make_derived_undead_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    make_derived_undead_fineff(coord_def pos, mgen_data _mg, int _xl,
        const string& _agent, const string& _msg,
        bool _act_immediately)
        : final_effect(0, 0, pos), mg(_mg), experience_level(_xl),
        agent(_agent), message(_msg), act_immediately(_act_immediately)
    {
    }
protected:
    mgen_data mg;
    int experience_level;
    string agent;
    string message;
    bool act_immediately;
};

class mummy_death_curse_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    mummy_death_curse_fineff(const actor* attack, const monster* source, killer_type _killer, int _pow)
        : final_effect(fixup_attacker(attack), 0, coord_def()),
        killer(_killer), pow(_pow)
    {
        // Cache the dying mummy so morgues can look up the monster source if it kills us.
        env.final_effect_monster_cache.push_back(*source);
        dead_mummy = source->mid;
    }
protected:
    const actor* fixup_attacker(const actor* a);

    killer_type killer;
    int pow;
    mid_t dead_mummy;
};

class summon_dismissal_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& fe) const override;
    void merge(const final_effect&) override;
    void fire() override;

    summon_dismissal_fineff(const actor* _defender)
        : final_effect(0, _defender, coord_def())
    {
    }
};

class spectral_weapon_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; };
    void fire() override;

    spectral_weapon_fineff(const actor& attack, const actor& defend,
        item_def* wpn)
        : final_effect(&attack, &defend, coord_def()), weapon(wpn)
    {
    }
protected:
    item_def* weapon;
};

class lugonu_meddle_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return true; };
    void fire() override;

    lugonu_meddle_fineff() : final_effect(nullptr, nullptr, coord_def()) {}
};

class jinxbite_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&/*a*/) const override { return false; };
    void fire() override;

    jinxbite_fineff(const actor* defend)
        : final_effect(nullptr, defend, coord_def())
    {
    }
};

class beogh_resurrection_fineff : public final_effect
{
public:
    bool mergeable(const final_effect& a) const override;
    void fire() override;

    beogh_resurrection_fineff(bool end_ostracism_only)
        : final_effect(nullptr, nullptr, coord_def()), ostracism_only(end_ostracism_only)
    {
    }
protected:
    const bool ostracism_only;
};

class dismiss_divine_allies_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    dismiss_divine_allies_fineff(const god_type _god)
        : final_effect(0, 0, coord_def()), god(_god)
    {
    }
protected:
    const god_type god;
};

class death_spawn_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return false; }
    void fire() override;

    death_spawn_fineff(mgen_data _mg)
        : final_effect(0, 0, _mg.pos), mg(_mg)
    {
    }
protected:
    const mgen_data mg;
};

class detonation_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&/*a*/) const override { return false; };
    void fire() override;

    detonation_fineff(const coord_def& pos, const item_def* wpn)
        : final_effect(&you, nullptr, pos), weapon(wpn)
    {
    }
protected:
    const item_def* weapon;
};

class stardust_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&/*a*/) const override { return false; };
    void fire() override;

    stardust_fineff(actor* agent, int _power, int _max, bool _is_star_jelly)
        : final_effect(agent, nullptr, you.pos()), power(_power), max_stars(_max),
                                                   is_star_jelly(_is_star_jelly)
    {
        // If this is a star jelly, cache it (even if it's not dead yet; since
        // it may die to further damage events within the same attack action.)
        if (is_star_jelly)
            env.final_effect_monster_cache.push_back(*agent->as_monster());
    }
protected:
    int power;
    int max_stars;
    bool is_star_jelly;
};

class pyromania_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&/*a*/) const override { return true; };
    void fire() override;

    pyromania_fineff()
        : final_effect(&you, nullptr, you.pos())
    {
    }
protected:
    int count;
    int power;
};

class celebrant_bloodrite_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return true; }
    void fire() override;

    celebrant_bloodrite_fineff()
        : final_effect(&you, nullptr, you.pos())
    {
    }
};

class eeljolt_fineff : public final_effect
{
public:
    bool mergeable(const final_effect&) const override { return true; }
    void fire() override;

    eeljolt_fineff()
        : final_effect(&you, nullptr, you.pos())
    {
    }
};

// Things to happen when the current attack/etc finishes.
static vector<final_effect*> _final_effects;

static void _schedule_final_effect(final_effect *eff)
{
    for (auto fe : _final_effects)
    {
        if (fe->mergeable(*eff))
        {
            fe->merge(*eff);
            delete eff;
            return;
        }
    }
    _final_effects.push_back(eff);
}

void schedule_mirror_damage_fineff(const actor* attack, const actor* defend,
                                   int dam)
{
    _schedule_final_effect(new mirror_damage_fineff(attack, defend, dam));
}

void schedule_anguish_fineff(const actor* attack, int dam)
{
    _schedule_final_effect(new anguish_fineff(attack, dam));
}

void schedule_ru_retribution_fineff(const actor* attack, const actor* defend,
                                    int dam)
{
    _schedule_final_effect(new ru_retribution_fineff(attack, defend, dam));
}

void schedule_trample_follow_fineff(const actor* attack, const coord_def& pos)
{
    _schedule_final_effect(new trample_follow_fineff(attack, pos));
}

void schedule_blink_fineff(const actor* blinker, const actor* other)
{
    _schedule_final_effect(new blink_fineff(blinker, other));
}

void schedule_teleport_fineff(const actor* defend)
{
    _schedule_final_effect(new teleport_fineff(defend));
}

void schedule_trj_spawn_fineff(const actor* attack, const actor* defend,
                               const coord_def& pos, int dam)
{
    _schedule_final_effect(new trj_spawn_fineff(attack, defend, pos, dam));
}

void schedule_blood_fineff(const actor* defend, const coord_def& pos,
                           int blood_amount)
{
    _schedule_final_effect(new blood_fineff(defend, pos, blood_amount));
}

void schedule_deferred_damage_fineff(const actor* attack, const actor* defend,
                                     int dam, bool attacker_effects,
                                     bool fatal)
{
    _schedule_final_effect(new deferred_damage_fineff(attack, defend, dam,
                                                      attacker_effects,
                                                      fatal));
}

void schedule_starcursed_merge_fineff(const actor* merger)
{
    _schedule_final_effect(new starcursed_merge_fineff(merger));
}

void schedule_shock_discharge_fineff(const actor* discharger, actor& oppressor,
                                     coord_def pos, int pow,
                                     string shock_source)
{
    _schedule_final_effect(new shock_discharge_fineff(discharger,
                                                      oppressor, pos, pow,
                                                      shock_source));
}

void schedule_explosion_fineff(bolt& beam, string boom, string sanct,
                               explosion_fineff_type typ,
                               const actor* flame_agent,
                               string poof)
{
    _schedule_final_effect(new explosion_fineff(beam, boom, sanct,
                                                typ, flame_agent, poof));
}

void schedule_splinterfrost_fragment_fineff(bolt& beam, string msg)
{
    _schedule_final_effect(new splinterfrost_fragment_fineff(beam, msg));
}

void schedule_delayed_action_fineff(daction_type action,
                                    const string& final_msg)
{
    _schedule_final_effect(new delayed_action_fineff(action, final_msg));
}

void schedule_kirke_death_fineff(const string& final_msg)
{
    _schedule_final_effect(new kirke_death_fineff(final_msg));
}

void schedule_rakshasa_clone_fineff(const actor* defend, const coord_def& pos)
{
    _schedule_final_effect(new rakshasa_clone_fineff(defend, pos));
}

void schedule_bennu_revive_fineff(const monster* bennu)
{
    _schedule_final_effect(new bennu_revive_fineff(bennu));
}

void schedule_avoided_death_fineff(monster* mons)
{
    // pretend to be dead until our revival, to prevent
    // sequencing errors from inadvertently making us change alignment
    const int realhp = mons->hit_points;
    mons->hit_points = -realhp;
    mons->flags |= MF_PENDING_REVIVAL;
    mons->props.erase(ATTACK_KILL_KEY);
    _schedule_final_effect(new avoided_death_fineff(mons, realhp));
}

void schedule_infestation_death_fineff(coord_def pos, const string& name)
{
    _schedule_final_effect(new infestation_death_fineff(pos, name));
}

void schedule_make_derived_undead_fineff(coord_def pos, mgen_data mg, int xl,
                                         const string& agent,
                                         const string& msg,
                                         bool act_immediately)
{
    _schedule_final_effect(new make_derived_undead_fineff(pos, mg, xl, agent,
                                                          msg,
                                                          act_immediately));
}

void schedule_mummy_death_curse_fineff(const actor* attack,
                                       const monster* dead_mummy,
                                       killer_type killer, int pow)
{
    _schedule_final_effect(new mummy_death_curse_fineff(attack, dead_mummy,
                                                        killer, pow));
}

void schedule_summon_dismissal_fineff(const actor* _defender)
{
    _schedule_final_effect(new summon_dismissal_fineff(_defender));
}

void schedule_spectral_weapon_fineff(const actor& attack, const actor& defend,
                                     item_def* weapon)
{
    _schedule_final_effect(new spectral_weapon_fineff(attack, defend, weapon));
}

void schedule_lugonu_meddle_fineff()
{
    _schedule_final_effect(new lugonu_meddle_fineff());
}

void schedule_jinxbite_fineff(const actor* defend)
{
    _schedule_final_effect(new jinxbite_fineff(defend));
}

void schedule_beogh_resurrection_fineff(bool end_ostracism_only)
{
    _schedule_final_effect(new beogh_resurrection_fineff(end_ostracism_only));
}

void schedule_dismiss_divine_allies_fineff(const god_type god)
{
    _schedule_final_effect(new dismiss_divine_allies_fineff(god));
}

void schedule_death_spawn_fineff(monster_type mon_type, coord_def pos, int dur,
                                 int summon_type)
{
    mgen_data _mg = mgen_data(mon_type, BEH_HOSTILE, pos,
                              MHITNOT, MG_FORCE_PLACE);
    _mg.set_summoned(nullptr, summon_type, dur, false, false);
    _schedule_final_effect(new death_spawn_fineff(_mg));
}

void schedule_death_spawn_fineff(mgen_data mg)
{
    _schedule_final_effect(new death_spawn_fineff(mg));
}

void schedule_detonation_fineff(const coord_def& pos, const item_def* wpn)
{
    _schedule_final_effect(new detonation_fineff(pos, wpn));
}

void schedule_stardust_fineff(actor* agent, int power, int max_stars, bool force_max)
{
    _schedule_final_effect(new stardust_fineff(agent, power, max_stars, force_max));
}

void schedule_pyromania_fineff()
{
    _schedule_final_effect(new pyromania_fineff());
}

void schedule_celebrant_bloodrite_fineff()
{
    _schedule_final_effect(new celebrant_bloodrite_fineff());
}

void schedule_eeljolt_fineff()
{
    _schedule_final_effect(new eeljolt_fineff());
}

bool mirror_damage_fineff::mergeable(const final_effect &fe) const
{
    const mirror_damage_fineff *o =
        dynamic_cast<const mirror_damage_fineff *>(&fe);
    return o && att == o->att && def == o->def;
}

bool anguish_fineff::mergeable(const final_effect &fe) const
{
    const anguish_fineff *o =
        dynamic_cast<const anguish_fineff *>(&fe);
    return o && att == o->att;
}

bool ru_retribution_fineff::mergeable(const final_effect &fe) const
{
    const ru_retribution_fineff *o =
        dynamic_cast<const ru_retribution_fineff *>(&fe);
    return o && att == o->att && def == o->def;
}

bool trample_follow_fineff::mergeable(const final_effect &fe) const
{
    const trample_follow_fineff *o =
        dynamic_cast<const trample_follow_fineff *>(&fe);
    return o && att == o->att;
}

bool blink_fineff::mergeable(const final_effect &fe) const
{
    const blink_fineff *o = dynamic_cast<const blink_fineff *>(&fe);
    return o && def == o->def && att == o->att;
}

bool teleport_fineff::mergeable(const final_effect &fe) const
{
    const teleport_fineff *o =
        dynamic_cast<const teleport_fineff *>(&fe);
    return o && def == o->def;
}

bool trj_spawn_fineff::mergeable(const final_effect &fe) const
{
    const trj_spawn_fineff *o = dynamic_cast<const trj_spawn_fineff *>(&fe);
    return o && att == o->att && def == o->def && posn == o->posn;
}

bool blood_fineff::mergeable(const final_effect &fe) const
{
    const blood_fineff *o = dynamic_cast<const blood_fineff *>(&fe);
    return o && posn == o->posn && mtype == o->mtype;
}

bool deferred_damage_fineff::mergeable(const final_effect &fe) const
{
    const deferred_damage_fineff *o = dynamic_cast<const deferred_damage_fineff *>(&fe);
    return o && att == o->att && def == o->def
           && attacker_effects == o->attacker_effects && fatal == o->fatal;
}

bool starcursed_merge_fineff::mergeable(const final_effect &fe) const
{
    const starcursed_merge_fineff *o = dynamic_cast<const starcursed_merge_fineff *>(&fe);
    return o && def == o->def;
}

bool shock_discharge_fineff::mergeable(const final_effect &fe) const
{
    const shock_discharge_fineff *o = dynamic_cast<const shock_discharge_fineff *>(&fe);
    return o && def == o->def;
}

bool delayed_action_fineff::mergeable(const final_effect &) const
{
    return false;
}

bool rakshasa_clone_fineff::mergeable(const final_effect &fe) const
{
    const rakshasa_clone_fineff *o =
        dynamic_cast<const rakshasa_clone_fineff *>(&fe);
    return o && att == o->att && def == o->def && posn == o->posn;
}

bool summon_dismissal_fineff::mergeable(const final_effect &fe) const
{
    const summon_dismissal_fineff *o =
        dynamic_cast<const summon_dismissal_fineff *>(&fe);
    return o && def == o->def;
}

void mirror_damage_fineff::merge(const final_effect &fe)
{
    const mirror_damage_fineff *mdfe =
        dynamic_cast<const mirror_damage_fineff *>(&fe);
    ASSERT(mdfe);
    ASSERT(mergeable(*mdfe));
    damage += mdfe->damage;
}

void anguish_fineff::merge(const final_effect &fe)
{
    const anguish_fineff *afe =
        dynamic_cast<const anguish_fineff *>(&fe);
    ASSERT(afe);
    ASSERT(mergeable(*afe));
    damage += afe->damage;
}

void ru_retribution_fineff::merge(const final_effect &fe)
{
    const ru_retribution_fineff *mdfe =
        dynamic_cast<const ru_retribution_fineff *>(&fe);
    ASSERT(mdfe);
    ASSERT(mergeable(*mdfe));
}

void trj_spawn_fineff::merge(const final_effect &fe)
{
    const trj_spawn_fineff *trjfe =
        dynamic_cast<const trj_spawn_fineff *>(&fe);
    ASSERT(trjfe);
    ASSERT(mergeable(*trjfe));
    damage += trjfe->damage;
}

void blood_fineff::merge(const final_effect &fe)
{
    const blood_fineff *bfe = dynamic_cast<const blood_fineff *>(&fe);
    ASSERT(bfe);
    ASSERT(mergeable(*bfe));
    blood += bfe->blood;
}

void deferred_damage_fineff::merge(const final_effect &fe)
{
    const deferred_damage_fineff *ddamfe =
        dynamic_cast<const deferred_damage_fineff *>(&fe);
    ASSERT(ddamfe);
    ASSERT(mergeable(*ddamfe));
    damage += ddamfe->damage;
}

void shock_discharge_fineff::merge(const final_effect &fe)
{
    const shock_discharge_fineff *ssdfe =
        dynamic_cast<const shock_discharge_fineff *>(&fe);
    power += ssdfe->power;
}

void summon_dismissal_fineff::merge(const final_effect &)
{
    // no damage to accumulate, but no need to fire this more than once
    return;
}

void mirror_damage_fineff::fire()
{
    actor *attack = attacker();
    if (!attack || attack == defender() || !attack->alive())
        return;
    // defender being dead is ok, if we killed them we still suffer

    god_acting gdact(GOD_YREDELEMNUL); // XXX: remove?

    if (att == MID_PLAYER)
    {
        const monster* reflector = defender() ?
                                   defender()->as_monster() : nullptr;
        if (reflector)
        {
            mprf("%s reflects your damage back at you!",
                 reflector->name(DESC_THE).c_str());
        }
        else
            mpr("Your damage is reflected back at you!");
        ouch(damage, KILLED_BY_MIRROR_DAMAGE);
    }
    else
    {
        simple_monster_message(*monster_by_mid(att), " suffers a backlash!");
        attack->hurt(defender(), damage);
    }
}

void anguish_fineff::fire()
{
    actor *attack = attacker();
    if (!attack || !attack->alive())
        return;

    const string punct = attack_strength_punctuation(damage);
    const string msg = make_stringf(" is wracked by anguish%s", punct.c_str());
    simple_monster_message(*monster_by_mid(att), msg.c_str());
    attack->hurt(monster_by_mid(MID_YOU_FAULTLESS), damage);
}

void ru_retribution_fineff::fire()
{
    actor *attack = attacker();
    if (!attack || attack == defender() || !attack->alive())
        return;
    if (def == MID_PLAYER)
        ru_do_retribution(monster_by_mid(att), damage);
}

void trample_follow_fineff::fire()
{
    actor *attack = attacker();
    if (attack
        && attack->pos() != posn
        && adjacent(attack->pos(), posn)
        && attack->is_habitable(posn)
        && !monster_at(posn))
    {
        attack->move_to(posn, MV_DELIBERATE);
    }
}

void blink_fineff::fire()
{
    actor *defend = defender();
    if (!defend || !defend->alive() || defend->no_tele())
        return;

    // if we're doing 'blink with', only blink if we have a partner
    actor *pal = attacker();
    if (pal && (!pal->alive() || pal->no_tele()))
        return;

    defend->blink();
    if (!defend->alive())
        return;

    // Is something else also getting blinked?
    if (!pal || !pal->alive() || pal->no_tele())
        return;

    for (fair_adjacent_iterator ai(defend->pos()); ai; ++ai)
    {
        // No blinking into teleport closets.
        if (testbits(env.pgrid(*ai), FPROP_NO_TELE_INTO))
            continue;
        // XXX: allow fedhasites to be blinked into plants?
        if (actor_at(*ai) || !pal->is_habitable(*ai))
            continue;

        pal->blink_to(*ai);
        break;
    }
}

void teleport_fineff::fire()
{
    actor *defend = defender();
    if (defend && defend->alive() && !defend->no_tele())
        defend->teleport(true);
}

void trj_spawn_fineff::fire()
{
    const actor *attack = attacker();
    actor *trj = defender();

    int tospawn = div_rand_round(damage, 12);

    if (tospawn <= 0)
        return;

    dprf("Trying to spawn %d jellies.", tospawn);

    unsigned short foe = attack && attack->alive() ? attack->mindex() : MHITNOT;
    // may be ANON_FRIENDLY_MONSTER
    if (invalid_monster_index(foe) && foe != MHITYOU)
        foe = MHITNOT;

    // Give spawns the same attitude as TRJ; if TRJ is now dead, make them
    // hostile.
    const beh_type spawn_beh = trj
        ? attitude_creation_behavior(trj->as_monster()->attitude)
        : BEH_HOSTILE;

    // No permanent friendly jellies from a charmed TRJ.
    if (spawn_beh == BEH_FRIENDLY && !crawl_state.game_is_arena())
        return;

    int spawned = 0;
    for (int i = 0; i < tospawn; ++i)
    {
        const monster_type jelly = royal_jelly_ejectable_monster();
        if (monster *mons = create_monster(
                              mgen_data(jelly, spawn_beh, posn, foe,
                                        MG_DONT_COME, GOD_JIYVA)
                              .set_summoned(trj, 0)
                              .set_range(1, LOS_RADIUS)
                              .copy_from_parent(trj)))
        {
            // Don't allow milking the Royal Jelly.
            mons->flags |= MF_NO_REWARD | MF_HARD_RESET;
            spawned++;
        }
    }

    if (!spawned || !you.see_cell(posn))
        return;

    if (trj)
    {
        const string monnam = trj->name(DESC_THE);
        mprf("%s shudders%s.", monnam.c_str(),
             spawned >= 5 ? " alarmingly" :
             spawned >= 3 ? " violently" :
             spawned > 1 ? " vigorously" : "");

        if (spawned == 1)
            mprf("%s spits out another jelly.", monnam.c_str());
        else
        {
            mprf("%s spits out %s more jellies.",
                 monnam.c_str(),
                 number_in_words(spawned).c_str());
        }
    }
    else if (spawned == 1)
        mpr("One of the Royal Jelly's fragments survives.");
    else
    {
        mprf("The dying Royal Jelly spits out %s more jellies.",
             number_in_words(spawned).c_str());
    }
}

void blood_fineff::fire()
{
    bleed_onto_floor(posn, mtype, blood, true);
}

void deferred_damage_fineff::fire()
{
    actor *df = defender();
    if (!df)
        return;

    // Once we actually apply damage to tentacle monsters,
    // remove their protection from further damage.
    if (df->props.exists(TENTACLE_LORD_HITS))
        df->props.erase(TENTACLE_LORD_HITS);

    if (!fatal)
    {
        // Cap non-fatal damage by the defender's hit points
        // FIXME: Consider adding a 'fatal' parameter to ::hurt
        //        to better interact with damage reduction/boosts
        //        which may be applied later.
        int df_hp = df->is_player() ? you.hp
                                    : df->as_monster()->hit_points;
        damage = min(damage, df_hp - 1);
    }

    df->hurt(attacker(), damage, BEAM_MISSILE, KILLED_BY_MONSTER, "", "",
             true, attacker_effects);
}

static void _do_merge_masses(monster* initial_mass, monster* merge_to)
{
    // Combine enchantment durations.
    merge_ench_durations(*initial_mass, *merge_to);

    merge_to->blob_size += initial_mass->blob_size;
    merge_to->max_hit_points += initial_mass->max_hit_points;
    merge_to->hit_points += initial_mass->hit_points;

    // Merge monster flags (mostly so that MF_CREATED_NEUTRAL, etc. are
    // passed on if the merged slime subsequently splits. Hopefully
    // this won't do anything weird.
    merge_to->flags |= initial_mass->flags;

    // Overwrite the state of the slime getting merged into, because it
    // might have been resting or something.
    merge_to->behaviour = initial_mass->behaviour;
    merge_to->foe = initial_mass->foe;

    behaviour_event(merge_to, ME_EVAL);

    // Have to 'kill' the slime doing the merging.
    monster_die(*initial_mass, KILL_RESET, NON_MONSTER, true);
}

void starcursed_merge_fineff::fire()
{
    actor *defend = defender();
    if (!defend || !defend->alive())
        return;

    monster *mon = defend->as_monster();

    // Find a random adjacent starcursed mass and merge with it.
    for (fair_adjacent_iterator ai(mon->pos()); ai; ++ai)
    {
        monster* mergee = monster_at(*ai);
        if (mergee && mergee->alive() && mergee->type == MONS_STARCURSED_MASS)
        {
            simple_monster_message(*mon,
                    " shudders and is absorbed by its neighbour.");
            _do_merge_masses(mon, mergee);
            return;
        }
    }

    // If there was nothing adjacent to merge with, at least try to move toward
    // another starcursed mass
    for (distance_iterator di(mon->pos(), true, true, LOS_RADIUS); di; ++di)
    {
        monster* ally = monster_at(*di);
        if (ally && ally->alive() && ally->type == MONS_STARCURSED_MASS
            && mon->can_see(*ally))
        {
            bool moved = false;

            coord_def sgn = (*di - mon->pos()).sgn();
            if (mon_can_move_to_pos(mon, sgn))
            {
                mon->move_to(mon->pos()+sgn, MV_DELIBERATE, true);
                moved = true;
            }
            else if (abs(sgn.x) != 0)
            {
                coord_def dx(sgn.x, 0);
                if (mon_can_move_to_pos(mon, dx))
                {
                    mon->move_to(mon->pos()+dx, MV_DELIBERATE, true);
                    moved = true;
                }
            }
            else if (abs(sgn.y) != 0)
            {
                coord_def dy(0, sgn.y);
                if (mon_can_move_to_pos(mon, dy))
                {
                    mon->move_to(mon->pos()+dy, MV_DELIBERATE, true);
                    moved = true;
                }
            }

            if (moved)
            {
                simple_monster_message(*mon, " shudders and withdraws towards its neighbour.");
                mon->speed_increment -= 10;
                mon->finalise_movement();
            }
        }
    }
}

void shock_discharge_fineff::fire()
{
    if (!oppressor.alive())
        return;

    const int max_range = 3; // v0v
    if (grid_distance(oppressor.pos(), position) > max_range
        || !cell_see_cell(position, oppressor.pos(), LOS_SOLID_SEE))
    {
        return;
    }

    const int amount = roll_dice(3, 4 + power * 3 / 2);
    int final_dmg = resist_adjust_damage(&oppressor, BEAM_ELECTRICITY, amount);
    final_dmg = oppressor.apply_ac(final_dmg, 0, ac_type::half);

    const actor *serpent = defender();
    if (serpent && you.can_see(*serpent))
    {
        mprf("%s %s discharges%s, shocking %s%s",
             serpent->name(DESC_ITS).c_str(),
             shock_source.c_str(),
             power < 4 ? "" : " violently",
             oppressor.name(DESC_THE).c_str(),
             attack_strength_punctuation(final_dmg).c_str());
    }
    else if (you.can_see(oppressor))
    {
        mprf("The air sparks with electricity, shocking %s%s",
             oppressor.name(DESC_THE).c_str(),
             attack_strength_punctuation(final_dmg).c_str());
    }

    bolt beam;
    beam.flavour = BEAM_ELECTRICITY;
    const string name = serpent && serpent->alive_or_reviving() ?
                        serpent->name(DESC_A, true) :
                        "a shock serpent"; // dubious
    oppressor.hurt(serpent, final_dmg, beam.flavour, KILLED_BY_BEAM,
                   name.c_str(), shock_source.c_str());

    // Do resist messaging
    if (oppressor.alive())
    {
        oppressor.beam_resists(beam, amount, true);
        if (final_dmg)
            oppressor.expose_to_element(beam.flavour, final_dmg);
    }
}

void explosion_fineff::fire()
{
    if (is_sanctuary(beam.target))
    {
        if (you.see_cell(beam.target))
            mprf(MSGCH_GOD, "%s", sanctuary_message.c_str());
        return;
    }

    if (you.see_cell(beam.target))
    {
        if (typ == EXPLOSION_FINEFF_CONCUSSION || typ == EXPLOSION_FINEFF_PYROMANIA)
            mpr(boom_message);
        else
            mprf(MSGCH_MONSTER_DAMAGE, MDAM_DEAD, "%s", boom_message.c_str());
    }

    if (typ == EXPLOSION_FINEFF_INNER_FLAME)
        for (adjacent_iterator ai(beam.target, false); ai; ++ai)
            if (!one_chance_in(5))
                place_cloud(CLOUD_FIRE, *ai, 10 + random2(10), flame_agent);

    beam.explode(true, typ == EXPLOSION_FINEFF_PYROMANIA);

    if (typ == EXPLOSION_FINEFF_CONCUSSION)
    {
        for (fair_adjacent_iterator ai(beam.target); ai; ++ai)
        {
            actor *act = actor_at(*ai);
            if (!act
                || act->is_stationary()
                || !could_harm(&you, act))
            {
                continue;
            }

            const coord_def newpos = (*ai - beam.target) + *ai;
            if (newpos == *ai || actor_at(newpos) || !act->is_habitable(newpos))
                continue;

            act->move_to(newpos, MV_DEFAULT, true);
            if (you.can_see(*act))
            {
                mprf("%s %s knocked back by the blast.",
                     act->name(DESC_THE).c_str(),
                     act->conj_verb("are").c_str());
            }
            act->finalise_movement();
        }
    }

    if (you.see_cell(beam.target) && !poof_message.empty())
        mprf(MSGCH_MONSTER_TIMEOUT, "%s", poof_message.c_str());
}

void delayed_action_fineff::fire()
{
    if (!final_msg.empty())
        mpr(final_msg);
    add_daction(action);
}

void kirke_death_fineff::fire()
{
    delayed_action_fineff::fire();

    // Revert the player last
    if (you.form == transformation::pig)
        return_to_default_form();
}

void rakshasa_clone_fineff::fire()
{
    actor *defend = defender();
    if (!defend)
        return;

    monster *rakshasa = defend->as_monster();
    ASSERT(rakshasa);

    // Using SPELL_NO_SPELL to prevent overwriting normal clones
    cast_phantom_mirror(rakshasa, rakshasa, 50, SPELL_NO_SPELL);
    cast_phantom_mirror(rakshasa, rakshasa, 50, SPELL_NO_SPELL);
    rakshasa->lose_energy(EUT_SPELL);

    if (you.can_see(*rakshasa))
    {
        mprf(MSGCH_MONSTER_SPELL,
             "The injured %s weaves a defensive illusion!",
             rakshasa->name(DESC_PLAIN).c_str());
    }
}

void bennu_revive_fineff::fire()
{
    bool res_visible = you.see_cell(posn);

    const monster* orig = cached_monster_copy_by_mid(att);

    monster *newmons = create_monster(mgen_data(MONS_BENNU, BEH_HOSTILE, posn, orig->foe,
                                                res_visible ? MG_DONT_COME
                                                            : MG_NONE).copy_from_parent(orig));

    if (!newmons)
        return;

    const int old_revives = orig->props.exists(BENNU_REVIVES_KEY) ? orig->props[BENNU_REVIVES_KEY].get_byte() : 0;
    newmons->props[BENNU_REVIVES_KEY].get_byte() = old_revives + 1;

    // If we were dueling the original bennu, the duel continues.
    if (orig->props.exists(OKAWARU_DUEL_TARGET_KEY))
    {
        newmons->props[OKAWARU_DUEL_TARGET_KEY] = true;
        newmons->props[OKAWARU_DUEL_CURRENT_KEY] = true;
    }
}

void avoided_death_fineff::fire()
{
    ASSERT(defender() && defender()->is_monster());
    defender()->as_monster()->hit_points = hp;
    defender()->as_monster()->flags &= ~MF_PENDING_REVIVAL;
}

void infestation_death_fineff::fire()
{
    if (monster *scarab = create_monster(mgen_data(MONS_DEATH_SCARAB,
                                                   BEH_FRIENDLY, posn,
                                                   MHITYOU, MG_AUTOFOE)
                                         .set_summoned(&you, SPELL_INFESTATION,
                                                       summ_dur(5), false),
                                         false))
    {
        if (you.see_cell(posn) || you.can_see(*scarab))
        {
            mprf("%s bursts from %s!", scarab->name(DESC_A, true).c_str(),
                                       name.c_str());
        }
    }
}

void make_derived_undead_fineff::fire()
{
    monster *undead = create_monster(mg);
    if (!undead)
        return;

    if (!message.empty() && you.can_see(*undead))
        mpr(message);

    // If the original monster has been levelled up, its HD might be
    // different from its class HD, in which case its HP should be
    // rerolled to match.
    if (undead->get_experience_level() != experience_level)
    {
        undead->set_hit_dice(max(experience_level, 1));
        roll_zombie_hp(undead);
    }

    if (!agent.empty())
        mons_add_blame(undead, "animated by " + agent);

    if (act_immediately)
    {
        undead->flags &= ~MF_JUST_SUMMONED;
        undead->speed_increment = 80;
        queue_monster_for_action(undead);
    }
}

const actor *mummy_death_curse_fineff::fixup_attacker(const actor *a)
{
    if (a && a->is_monster() && a->as_monster()->friendly()
        && !crawl_state.game_is_arena())
    {
        // Mummies are smart enough not to waste curses on summons or allies.
        return &you;
    }
    return a;
}

void mummy_death_curse_fineff::fire()
{
    if (pow <= 0)
        return;

    switch (killer)
    {
        // Mummy killed by trap or something other than the player or
        // another monster, so no curse.
        case KILL_NON_ACTOR:
        case KILL_RESET:
        case KILL_RESET_KEEP_ITEMS:
        // Mummy sent to the Abyss wasn't actually killed, so no curse.
        case KILL_BANISHED:
            return;

        default:
            break;
    }

    actor* victim;

    if (YOU_KILL(killer))
        victim = &you;
    // Killed by a Zot trap, a god, etc, or suicide.
    else if (!attacker() || attacker() == defender())
        return;
    else
        victim = attacker();

    // Mummy was killed by a ballistomycete spore or ball lightning?
    if (!victim->alive())
        return;

    // Stepped from time?
    if (!in_bounds(victim->pos()))
        return;

    if (victim->is_player())
        mprf(MSGCH_MONSTER_SPELL, "You feel extremely nervous for a moment...");
    else if (you.can_see(*victim))
    {
        mprf(MSGCH_MONSTER_SPELL, "A malignant aura surrounds %s.",
             victim->name(DESC_THE).c_str());
    }
    // The real mummy is dead, but we pass along a cached copy save at the time
    // they died (for morgue purposes)
    death_curse(*victim, cached_monster_copy_by_mid(dead_mummy), "", pow);
}

void summon_dismissal_fineff::fire()
{
    if (defender() && defender()->alive())
        monster_die(*(defender()->as_monster()), KILL_TIMEOUT, NON_MONSTER);
}

void spectral_weapon_fineff::fire()
{
    actor *atkr = attacker();
    actor *defend = defender();
    if (!defend || !atkr || !defend->alive() || !atkr->alive())
        return;

    if (!weapon || !weapon->defined())
        return;

    const coord_def target = defend->pos();

    // Do we already have a spectral weapon?
    monster* sw = find_spectral_weapon(*weapon);
    if (sw)
    {
        if (sw == defend)
            return; // don't attack yourself. too silly.
        // Is it already in range?
        const int sw_range = sw->reach_range();
        if (sw_range > 1
            && can_reach_attack_between(sw->pos(), target, sw_range)
            || adjacent(sw->pos(), target))
        {
            // Just attack.
            melee_attack melee_attk(sw, defend);
            melee_attk.attack();
            return;
        }
    }

    // Can we find a nearby space to attack from?
    const int atk_range = atkr->reach_range();
    int seen_valid = 0;
    coord_def chosen_pos;
    // Try only spaces adjacent to the attacker.
    for (adjacent_iterator ai(atkr->pos()); ai; ++ai)
    {
        if (actor_at(*ai)
            || !monster_habitable_grid(MONS_SPECTRAL_WEAPON, *ai))
        {
            continue;
        }
        // ... and only spaces the weapon could attack the defender from.
        if (grid_distance(*ai, target) > 1
            && (atk_range <= 1
                || !can_reach_attack_between(*ai, target, atk_range)))
        {
            continue;
        }
        // Reservoir sampling.
        seen_valid++;
        if (one_chance_in(seen_valid))
            chosen_pos = *ai;
    }
    if (!seen_valid)
        return;

    monster *mons = create_spectral_weapon(*atkr, chosen_pos, *weapon);
    if (!mons)
        return;

    melee_attack melee_attk(mons, defend);
    melee_attk.attack();
}

void lugonu_meddle_fineff::fire() {
    lucy_check_meddling();
}

void jinxbite_fineff::fire()
{
    actor* defend = defender();
    if (defend && defend->alive())
        attempt_jinxbite_hit(*defend);
}

bool beogh_resurrection_fineff::mergeable(const final_effect &fe) const
{
    const beogh_resurrection_fineff *o =
        dynamic_cast<const beogh_resurrection_fineff *>(&fe);
    return o && ostracism_only == o->ostracism_only;
}

void beogh_resurrection_fineff::fire()
{
    beogh_resurrect_followers(ostracism_only);
}

void dismiss_divine_allies_fineff::fire()
{
    if (god == GOD_BEOGH)
        beogh_do_ostracism();
    else
        dismiss_god_summons(god);
}

void death_spawn_fineff::fire()
{
    create_monster(mg);
}

void splinterfrost_fragment_fineff::fire()
{
    if (!msg.empty())
        mprf(MSGCH_MONSTER_DAMAGE, MDAM_DEAD, "%s", msg.c_str());

    beam.fire();
}

void detonation_fineff::fire()
{
    do_catalyst_explosion(posn, weapon);
}

void stardust_fineff::fire()
{
    actor* agent = actor_by_mid(att);

    // In case the agent is dead, check for a cached copy.
    if (!agent)
        agent = cached_monster_copy_by_mid(att);
    if (!agent)
        return;

    if (agent->is_player() && is_sanctuary(you.pos()))
        return;

    int count = 0;
    for (actor_near_iterator ai(agent->pos(), LOS_NO_TRANS); ai; ++ai)
        if (!ai->is_firewood() && !mons_aligned(agent, *ai))
            ++count;

    // Don't activate or go on cooldown if there's nothing to shoot at.
    if (count == 0)
        return;

    if (is_star_jelly)
        mprf("A flurry of magic pours from %s injured body!", agent->name(DESC_ITS).c_str());
    else
        mprf("%s orb unleashes a flurry of shooting stars!", agent->name(DESC_ITS).c_str());

    count = is_star_jelly ? max_stars : min(max_stars, count + 1);
    const int foe = agent->is_player() ? int{MHITYOU} : agent->as_monster()->foe;
    for (int i = 0; i < count; ++i)
    {
        mgen_data mg(MONS_SHOOTING_STAR, BEH_COPY, agent->pos(),
                     foe, MG_FORCE_BEH | MG_FORCE_PLACE | MG_AUTOFOE);

        mg.set_summoned(agent, MON_SUMM_STARDUST, 200, false, false);
        mg.set_range(1, 3);
        mg.hd = power;
        mg.hp = 100;
        if (monster* mon = create_monster(mg))
            mon->steps_remaining = 12;
    }

    if (agent->is_player())
        you.duration[DUR_STARDUST_COOLDOWN] = random_range(40, 70);
    else if (!is_star_jelly)
        agent->as_monster()->add_ench(mon_enchant(ENCH_ORB_COOLDOWN, agent, random_range(300, 500)));
}

void pyromania_fineff::fire()
{
    // Test if there's at least *something* worth hitting before exploding
    // (primarily to avoid wasting the player's time). Note: we don't check the
    // the player *wants* to hit what is there, just that there is something
    // there to hit. Burning down random plants is thematic here.
    bool found = false;
    for (radius_iterator ri(you.pos(), 3, C_SQUARE, LOS_NO_TRANS); ri; ++ri)
    {
        if (monster* mon = monster_at(*ri))
        {
            if (could_harm(&you, mon))
            {
                found = true;
                break;
            }
        }
    }

    if (!found)
        return;

    bolt exp;
    zappy(ZAP_FIREBALL, 50, false, exp);
    exp.damage = pyromania_damage();
    exp.set_agent(&you);
    exp.target = you.pos();
    exp.source = you.pos();
    exp.ex_size = 3;

    mpr("Your orb flickers with a hungry flame!");
    exp.explode(true, true);
}

constexpr int BLOODRITE_MIN_SHOTS = 8;

void celebrant_bloodrite_fineff::fire()
{
    vector<coord_def> targs;
    for (monster_near_iterator mi(you.pos(), LOS_NO_TRANS); mi; ++mi)
    {
        if (!mi->wont_attack() && !mi->is_firewood())
            targs.push_back(mi->pos());
    }

    // Don't activate or go on cooldown if there's nothing to shoot at.
    if (targs.empty())
        return;

    mpr("You consecrate your suffering and invoke the rites of blood!");

    // Set cooldown before firing, in case we recieve damage during the volley
    // (eg: via reflected projectiles) that would trigger this again.
    you.duration[DUR_CELEBRANT_COOLDOWN] = 1;

    shuffle_array(targs);

    int shots_fired = 0;
    int repeats = 0;

    bolt beam;
    beam.range        = you.current_vision;
    beam.source       = you.pos();
    beam.source_id    = MID_PLAYER;
    beam.attitude     = ATT_FRIENDLY;
    beam.thrower      = KILL_YOU;
    zappy(ZAP_BLOOD_ARROW, 15 + you.skill(SK_INVOCATIONS, 2), false, beam);

    beam.draw_delay   = 10;

    // Fire once at every visible target. If that doesn't hit the minimum number
    // of shots, fire at most a second time at each of these targets.
    while (shots_fired < BLOODRITE_MIN_SHOTS && repeats < 2)
    {
        for (size_t i = 0; i < targs.size()
                && (repeats == 0 || shots_fired < BLOODRITE_MIN_SHOTS); ++i)
        {
            bolt shot = beam;
            shot.target = targs[i];
            shot.fire();
            view_clear_overlays();
            ++shots_fired;
        }
        ++repeats;
        shuffle_array(targs);
    }

    // If firing twice at every target still didn't hit the minimum number of
    // shots, fire the rest of them completely at random.
    while (shots_fired < BLOODRITE_MIN_SHOTS)
    {
        coord_def targ = you.pos();
        targ.x += random_range(-LOS_RADIUS, LOS_RADIUS);
        targ.y += random_range(-LOS_RADIUS, LOS_RADIUS);

        if (targ == you.pos())
            continue;

        bolt shot = beam;
        shot.target = targ;
        shot.fire();
        view_clear_overlays();
        ++shots_fired;
    }
}

void eeljolt_fineff::fire()
{
    do_eel_arcjolt();
}

// Effects that occur after all other effects, even if the monster is dead.
// For example, explosions that would hit other creatures, but we want
// to deal with only one creature at a time, so that's handled last.
void fire_final_effects()
{
    while (!_final_effects.empty())
    {
        // Remove it first so nothing can merge with it.
        unique_ptr<final_effect> eff(_final_effects.back());
        _final_effects.pop_back();
        eff->fire();
    }

    // Clear all cached monster copies
    env.final_effect_monster_cache.clear();
}

void clear_final_effects()
{
    deleteAll(_final_effects);
}