-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
spl-cast.cc
2907 lines (2471 loc) · 91.7 KB
/
spl-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 Spell casting and miscast functions.
**/
#include "AppHdr.h"
#include "spl-cast.h"
#include <iomanip>
#include <sstream>
#include <cmath>
#include "act-iter.h"
#include "areas.h"
#include "art-enum.h"
#include "beam.h"
#include "chardump.h"
#include "cloud.h"
#include "colour.h"
#include "coordit.h"
#include "database.h"
#include "describe.h"
#include "directn.h"
#include "english.h"
#include "env.h"
#include "evoke.h"
#include "exercise.h"
#include "format.h"
#include "god-abil.h"
#include "god-conduct.h"
#include "god-item.h"
#include "god-passive.h" // passive_t::shadow_spells
#include "hints.h"
#include "items.h"
#include "item-prop.h"
#include "item-use.h"
#include "libutil.h"
#include "macro.h"
#include "menu.h"
#include "message.h"
#include "misc.h"
#include "mon-behv.h"
#include "mon-cast.h"
#include "mon-explode.h"
#include "mon-place.h"
#include "mon-project.h"
#include "mon-util.h"
#include "mutation.h"
#include "options.h"
#include "ouch.h"
#include "output.h"
#include "player.h"
#include "player-stats.h"
#include "prompt.h"
#include "religion.h"
#include "shout.h"
#include "skills.h"
#include "spl-book.h"
#include "spl-clouds.h"
#include "spl-damage.h"
#include "spl-goditem.h"
#include "spl-miscast.h"
#include "spl-monench.h"
#include "spl-other.h"
#include "spl-selfench.h"
#include "spl-summoning.h"
#include "spl-transloc.h"
#include "spl-wpnench.h"
#include "spl-zap.h"
#include "state.h"
#include "stepdown.h"
#include "stringutil.h"
#include "tag-version.h"
#include "target.h"
#include "teleport.h"
#include "terrain.h"
#include "tilepick.h"
#include "transform.h"
#include "unicode.h"
#include "unwind.h"
#include "view.h"
#include "viewchar.h" // stringize_glyph
static int _spell_enhancement(spell_type spell);
static string _spell_failure_rate_description(spell_type spell);
void surge_power(const int enhanced)
{
if (enhanced) // one way or the other {dlb}
{
const string modifier = (enhanced < -2) ? "extraordinarily" :
(enhanced == -2) ? "extremely" :
(enhanced == 2) ? "strong" :
(enhanced > 2) ? "huge"
: "";
mprf("You feel %s %s",
!modifier.length() ? "a"
: article_a(modifier).c_str(),
(enhanced < 0) ? "numb sensation."
: "surge of power!");
}
}
void surge_power_wand(const int mp_cost)
{
if (mp_cost)
{
const bool slight = mp_cost < 3;
mprf("You feel a %ssurge of power%s",
slight ? "slight " : "",
slight ? "." : "!");
}
}
static string _spell_base_description(spell_type spell, bool viewing)
{
ostringstream desc;
int highlight = spell_highlight_by_utility(spell, COL_UNKNOWN, !viewing);
desc << "<" << colour_to_str(highlight) << ">" << left;
// spell name
desc << chop_string(spell_title(spell), 30);
// spell schools
desc << spell_schools_string(spell);
const int so_far = strwidth(desc.str()) - (strwidth(colour_to_str(highlight))+2);
if (so_far < 60)
desc << string(60 - so_far, ' ');
desc << "</" << colour_to_str(highlight) <<">";
// spell fail rate, level
const string failure_rate = spell_failure_rate_string(spell);
const int width = strwidth(formatted_string::parse_string(failure_rate).tostring());
desc << failure_rate << string(12-width, ' ');
desc << spell_difficulty(spell);
desc << " ";
return desc.str();
}
static string _spell_extra_description(spell_type spell, bool viewing)
{
ostringstream desc;
int highlight = spell_highlight_by_utility(spell, COL_UNKNOWN, !viewing);
desc << "<" << colour_to_str(highlight) << ">" << left;
// spell name
desc << chop_string(spell_title(spell), 30);
// spell power, spell range, noise
const string rangestring = spell_range_string(spell);
const string damagestring = spell_damage_string(spell);
desc << chop_string(spell_power_string(spell), 10)
<< chop_string(damagestring.length() ? damagestring : "N/A", 10)
<< chop_string(rangestring, 10)
<< chop_string(spell_noise_string(spell, 10), 14);
desc << "</" << colour_to_str(highlight) <<">";
return desc.str();
}
// selector is a boolean function that filters spells according
// to certain criteria. Currently used for Tiles to distinguish
// spells targeted on player vs. spells targeted on monsters.
int list_spells(bool toggle_with_I, bool viewing, bool allow_preselect,
const string &title, spell_selector selector)
{
if (toggle_with_I && get_spell_by_letter('I') != SPELL_NO_SPELL)
toggle_with_I = false;
ToggleableMenu spell_menu(MF_SINGLESELECT | MF_ANYPRINTABLE
| MF_NO_WRAP_ROWS | MF_ALWAYS_SHOW_MORE | MF_ALLOW_FORMATTING);
string titlestring = make_stringf("%-25.25s", title.c_str());
{
ToggleableMenuEntry* me =
new ToggleableMenuEntry(
titlestring + " Type Failure Level",
titlestring + " Power Damage Range Noise ",
MEL_TITLE);
spell_menu.set_title(me, true, true);
}
spell_menu.set_highlighter(nullptr);
spell_menu.set_tag("spell");
spell_menu.add_toggle_key('!');
string more_str = "Press '<w>!</w>' ";
if (toggle_with_I)
{
spell_menu.add_toggle_key('I');
more_str += "or '<w>I</w>' ";
}
if (!viewing)
spell_menu.menu_action = Menu::ACT_EXECUTE;
more_str += "to toggle spell view.";
spell_menu.set_more(formatted_string::parse_string(more_str));
// If there's only a single spell in the offered spell list,
// taking the selector function into account, preselect that one.
bool preselect_first = false;
if (allow_preselect)
{
int count = 0;
if (you.spell_no == 1)
count = 1;
else if (selector)
{
for (int i = 0; i < 52; ++i)
{
const char letter = index_to_letter(i);
const spell_type spell = get_spell_by_letter(letter);
if (!is_valid_spell(spell) || !(*selector)(spell))
continue;
// Break out early if we've got > 1 spells.
if (++count > 1)
break;
}
}
// Preselect the first spell if it's only spell applicable.
preselect_first = (count == 1);
}
// TODO: maybe fill this from the quiver if there's a quivered spell and
// no last cast one?
if (allow_preselect || preselect_first
&& you.last_cast_spell != SPELL_NO_SPELL)
{
spell_menu.set_flags(spell_menu.get_flags() | MF_PRESELECTED);
}
for (int i = 0; i < 52; ++i)
{
const char letter = index_to_letter(i);
const spell_type spell = get_spell_by_letter(letter);
if (!is_valid_spell(spell))
continue;
if (selector && !(*selector)(spell))
continue;
bool preselect = (preselect_first
|| allow_preselect && you.last_cast_spell == spell);
ToggleableMenuEntry* me =
new ToggleableMenuEntry(_spell_base_description(spell, viewing),
_spell_extra_description(spell, viewing),
MEL_ITEM, 1, letter, preselect);
me->add_tile(tile_def(tileidx_spell(spell)));
spell_menu.add_entry(me);
}
int choice = 0;
spell_menu.on_single_selection = [&choice, &spell_menu](const MenuEntry& item)
{
ASSERT(item.hotkeys.size() == 1);
if (spell_menu.menu_action == Menu::ACT_EXAMINE)
{
describe_spell(get_spell_by_letter(item.hotkeys[0]), nullptr);
return true;
}
else
{
choice = item.hotkeys[0];
return false;
}
};
spell_menu.show();
if (!crawl_state.doing_prev_cmd_again)
{
redraw_screen();
update_screen();
}
return choice;
}
static int _apply_spellcasting_success_boosts(spell_type spell, int chance)
{
int fail_reduce = 100;
if (have_passive(passive_t::spells_success) && vehumet_supports_spell(spell))
{
// [dshaligram] Fail rate multiplier used to be .5, scaled
// back to 67%.
fail_reduce = fail_reduce * 2 / 3;
}
const int wizardry = player_wizardry(spell);
if (wizardry > 0)
fail_reduce = fail_reduce * 6 / (7 + wizardry);
// Hard cap on fail rate reduction.
if (fail_reduce < 50)
fail_reduce = 50;
return chance * fail_reduce / 100;
}
/**
* Calculate the player's failure rate with the given spell, including all
* modifiers. (Armour, mutations, statuses effects, etc.)
*
* @param spell The spell in question.
* @return A failure rate. This is *not* a percentage - for a human-
* readable version, call _get_true_fail_rate().
*/
int raw_spell_fail(spell_type spell)
{
int chance = 60;
// Don't cap power for failure rate purposes.
// scale by 6, which I guess was chosen because it seems to work.
// realistic range for spellpower: -6 to -366 (before scale -1 to -61)
chance -= calc_spell_power(spell, false, true, false, 6);
chance -= (you.intel() * 2); // realistic range: -2 to -70
const int armour_shield_penalty = player_armour_shield_spell_penalty();
chance += armour_shield_penalty; // range: 0 to 500 in extreme cases.
// A midlevel melee character in plate
// might have 40 or 50, and a caster in a
// robe would usually have 0.
static const int difficulty_by_level[] =
{
0,
3,
15,
35,
70,
100,
150,
200,
260,
340,
};
const int spell_level = spell_difficulty(spell);
ASSERT_RANGE(spell_level, 0, (int) ARRAYSZ(difficulty_by_level));
chance += difficulty_by_level[spell_level]; // between 0 and 330
// since chance is passed through a 3rd degree polynomial, cap the
// value to avoid any overflow issues. We choose 210 by solving for chance2
// = 200 in the polynomial -- it gets capped at 100 ultimately, but we
// need a bunch of headroom in case some later calculations lower the value
// below 100 after this.
chance = min(chance, 210);
// This polynomial is a smoother approximation of a breakpoint-based
// calculation that originates pre-DCSS, mapping `chance` at this point to
// values from around 0 to around 45. (see
// https://crawl.develz.org/tavern/viewtopic.php?f=8&t=23414 for some of
// the history.) It was calculated by |amethyst (based on one from minmay
// in that thread) and converted to integer values using 262144 as a
// convenient power of 2 denominator, then converted to its current form
// by Horner's rule, and then tweaked slightly.
//
// The regular (integer) polynomial form before Horner's rule is:
// (x*x*x + 426*x*x + 82670*x + 7245398) / 262144
//
// https://www.wolframalpha.com/input/?i=graph+of+y%3D(((x+%2B+426)*x+%2B+82670)*x+%2B+7245398)+%2F+262144+and+y%3D100+and+x%3D125.1+with+x+from+-192+to+126.1
//
// If you think this is weird, you should see what was here before.
int chance2 = max((((chance + 426) * chance + 82670) * chance + 7245398)
/ 262144, 0);
chance2 += get_form()->spellcasting_penalty;
if (you.duration[DUR_EXCRUCIATING_WOUNDS])
chance2 += 10; // same as spider form
chance2 -= 2 * you.get_mutation_level(MUT_SUBDUED_MAGIC);
chance2 += 4 * you.get_mutation_level(MUT_WILD_MAGIC);
chance2 += 4 * you.get_mutation_level(MUT_ANTI_WIZARDRY);
chance2 += you.duration[DUR_VERTIGO] ? 7 : 0;
// Apply the effects of Vehumet and items of wizardry.
chance2 = _apply_spellcasting_success_boosts(spell, chance2);
return min(max(chance2, 0), 100);
}
/*
* Given some spellpower in centis, do a stepdown at around 50 (5000 in centis)
* and return a rescaled value.
*
* @param power the input spellpower in centis.
* @param scale a value to scale the result by, between 1 and 1000. Default is
* 1, which returns a regular spellpower. 1000 gives you millis, 100
* centis.
*/
int stepdown_spellpower(int power, int scale)
{
// use millis internally
ASSERT_RANGE(scale, 1, 1000);
const int divisor = 1000 / scale;
int result = stepdown_value(power * 10, 50000, 50000, 150000, 200000)
/ divisor;
return result;
}
static int _skill_power(spell_type spell)
{
int power = 0;
const spschools_type disciplines = get_spell_disciplines(spell);
const int skillcount = count_bits(disciplines);
if (skillcount)
{
for (const auto bit : spschools_type::range())
if (disciplines & bit)
power += you.skill(spell_type2skill(bit), 200);
power /= skillcount;
}
// Innate casters use spellcasting for every spell school.
const int splcast_mult = you.has_mutation(MUT_INNATE_CASTER) ? 250 : 50;
power += you.skill(SK_SPELLCASTING, splcast_mult);
return power;
}
/*
* Calculate spell power.
*
* @param spell the spell to check
* @param apply_intel whether to include intelligence in the calculation
* @param fail_rate_check is this just a plain failure rate check or should it
* incorporate situational facts and mutations?
* @param cap_power whether to apply the power cap for the spell (from
* `spell_power_cap(spell)`)
* @param scale what scale to apply to the result internally? This
* function has higher internal resolution than the default
* argument, so use this rather than dividing. This must be
* between 1 and 1000.
*
* @return the resulting spell power.
*/
int calc_spell_power(spell_type spell, bool apply_intel, bool fail_rate_check,
bool cap_power, int scale)
{
int power = _skill_power(spell);
if (you.divine_exegesis)
power += you.skill(SK_INVOCATIONS, 300);
if (fail_rate_check)
{
// Scale appropriately.
// The stepdown performs this step in the else block.
power *= scale;
power /= 100;
}
else
{
if (apply_intel)
power = (power * you.intel()) / 10;
// [dshaligram] Enhancers don't affect fail rates any more, only spell
// power. Note that this does not affect Vehumet's boost in castability.
power = apply_enhancement(power, _spell_enhancement(spell));
// Wild magic boosts spell power but decreases success rate.
power *= (10 + 3 * you.get_mutation_level(MUT_WILD_MAGIC));
power /= (10 + 3 * you.get_mutation_level(MUT_SUBDUED_MAGIC));
// Augmentation boosts spell power at high HP.
power *= 10 + 4 * augmentation_amount();
power /= 10;
// Each level of horror reduces spellpower by 10%
if (you.duration[DUR_HORROR])
{
power *= 10;
power /= 10 + (you.props[HORROR_PENALTY_KEY].get_int() * 3) / 2;
}
// at this point, `power` is assumed to be basically in centis.
// apply a stepdown, and scale.
power = stepdown_spellpower(power, scale);
}
const int cap = spell_power_cap(spell);
if (cap > 0 && cap_power)
power = min(power, cap * scale);
return power;
}
static int _spell_enhancement(spell_type spell)
{
const spschools_type typeflags = get_spell_disciplines(spell);
int enhanced = 0;
if (typeflags & spschool::conjuration)
enhanced += player_spec_conj();
if (typeflags & spschool::hexes)
enhanced += player_spec_hex();
if (typeflags & spschool::summoning)
enhanced += player_spec_summ();
if (typeflags & spschool::poison)
enhanced += player_spec_poison();
if (typeflags & spschool::necromancy)
enhanced += player_spec_death();
if (typeflags & spschool::fire)
enhanced += player_spec_fire();
if (typeflags & spschool::ice)
enhanced += player_spec_cold();
if (typeflags & spschool::earth)
enhanced += player_spec_earth();
if (typeflags & spschool::air)
enhanced += player_spec_air();
if (you.form == transformation::shadow)
enhanced -= 2;
enhanced += you.archmagi();
enhanced += you.duration[DUR_BRILLIANCE] > 0
|| player_equip_unrand(UNRAND_FOLLY);
// These are used in an exponential way, so we'll limit them a bit. -- bwr
if (enhanced > 3)
enhanced = 3;
else if (enhanced < -3)
enhanced = -3;
return enhanced;
}
/**
* Apply the effects of spell enhancers (and de-enhancers) on spellpower.
*
* @param initial_power The power of the spell before enhancers are added.
* @param enhancer_levels The number of enhancements levels to apply.
* @return The power of the spell with enhancers considered.
*/
int apply_enhancement(const int initial_power, const int enhancer_levels)
{
int power = initial_power;
if (enhancer_levels > 0)
{
for (int i = 0; i < enhancer_levels; i++)
{
power *= 15;
power /= 10;
}
}
else if (enhancer_levels < 0)
{
for (int i = enhancer_levels; i < 0; i++)
power /= 2;
}
return power;
}
void inspect_spells()
{
if (!you.spell_no)
{
canned_msg(MSG_NO_SPELLS);
return;
}
list_spells(true, true);
}
/**
* Can the player cast any spell at all? Checks for things that limit
* spellcasting regardless of the specific spell we want to cast.
*
* @param quiet If true, don't print a reason why no spell can be cast.
* @param exegesis If true, we're considering casting under Divine Exegesis.
* @return True if we could cast a spell, false otherwise.
*/
bool can_cast_spells(bool quiet, bool exegesis)
{
if (!get_form()->can_cast)
{
if (!quiet)
canned_msg(MSG_PRESENT_FORM);
return false;
}
if (you.duration[DUR_WATER_HOLD] && !you.res_water_drowning())
{
if (!quiet)
mpr("You cannot cast spells while unable to breathe!");
return false;
}
if (you.duration[DUR_BRAINLESS])
{
if (!quiet)
mpr("You lack the mental capacity to cast spells.");
return false;
}
if (you.duration[DUR_NO_CAST])
{
if (!quiet)
mpr("You are unable to access your magic!");
return false;
}
// Randart weapons.
if (you.no_cast())
{
if (!quiet)
mpr("Something interferes with your magic!");
return false;
}
// Check that we have a spell memorised. Divine Exegesis does not need this
// condition, but we can't just check you.divine_exegesis in all cases, as
// it may not be set yet. Check a passed parameter instead.
if (!exegesis && !you.spell_no)
{
if (!quiet)
canned_msg(MSG_NO_SPELLS);
return false;
}
if (you.berserk())
{
if (!quiet)
canned_msg(MSG_TOO_BERSERK);
return false;
}
if (you.confused())
{
if (!quiet)
mpr("You're too confused to cast spells.");
return false;
}
if (silenced(you.pos()))
{
if (!quiet)
mpr("You cannot cast spells when silenced!");
// included in default force_more_message
return false;
}
return true;
}
void do_cast_spell_cmd(bool force)
{
if (cast_a_spell(!force) == spret::abort)
flush_input_buffer(FLUSH_ON_FAILURE);
}
static void _handle_wucad_mu(int cost)
{
if (!player_equip_unrand(UNRAND_WUCAD_MU))
return;
if (you.has_mutation(MUT_HP_CASTING))
return;
if (!x_chance_in_y(you.skill(SK_EVOCATIONS), 54))
return;
did_god_conduct(DID_WIZARDLY_ITEM, 10);
// The chance of backfiring goes down with evo skill and up with cost
if (one_chance_in(max(you.skill(SK_EVOCATIONS) - cost, 1)))
{
mpr(random_choose("Weird images run through your mind.",
"Your head hurts.",
"You feel a strange surge of energy.",
"You feel uncomfortable."));
if (coinflip())
confuse_player(2 + random2(4));
else
lose_stat(STAT_INT, 1 + random2avg(5, 2));
}
mpr("Magical energy flows into your mind!");
inc_mp(cost, true);
}
/**
* Let the Majin-Bo congratulate you on casting a spell while using it.
*
* @param spell The spell just successfully cast.
*/
static void _majin_speak(spell_type spell)
{
// since this isn't obviously mental communication, let it be silenced
if (silenced(you.pos()))
return;
const int level = spell_difficulty(spell);
const bool weak = level <= 4;
const string lookup = weak ? "majin-bo cast weak" : "majin-bo cast";
const string msg = "A voice whispers, \"" + getSpeakString(lookup) + "\"";
mprf(MSGCH_TALK, "%s", msg.c_str());
}
static bool _majin_charge_hp()
{
return player_equip_unrand(UNRAND_MAJIN) && !you.duration[DUR_DEATHS_DOOR];
}
/**
* Cast a spell.
*
* Handles general preconditions & costs.
*
* @param check_range If true, abort if no targets are in range. (z vs Z)
* @param spell The type of spell to be cast.
* @param force_failure True if the spell's failure has already been determined
* in advance (for spells being cast via Divine Exegesis).
* @return Whether the spell was successfully cast.
**/
spret cast_a_spell(bool check_range, spell_type spell, dist *_target,
bool force_failure)
{
if (!can_cast_spells(false, you.divine_exegesis))
{
crawl_state.zero_turns_taken();
return spret::abort;
}
if (crawl_state.game_is_hints())
Hints.hints_spell_counter++;
if (spell == SPELL_NO_SPELL)
{
int keyin = 0;
string luachoice;
if (!clua.callfn("c_choose_spell", ">s", &luachoice))
{
if (!clua.error.empty())
mprf(MSGCH_ERROR, "Lua error: %s", clua.error.c_str());
}
else if (!luachoice.empty() && isalpha(luachoice[0]))
{
keyin = luachoice[0];
const spell_type spl = get_spell_by_letter(keyin);
// Bad entry from lua, defer to the user
if (!is_valid_spell(spl))
keyin = 0;
}
while (true)
{
#ifdef TOUCH_UI
keyin = list_spells(true, false);
if (!keyin)
keyin = ESCAPE;
if (!crawl_state.doing_prev_cmd_again)
{
redraw_screen();
update_screen();
}
if (isaalpha(keyin) || key_is_escape(keyin))
break;
else
clear_messages();
keyin = 0;
#else
if (keyin == 0 && !Options.spell_menu)
{
if (you.spell_no == 1)
{
// Set last_cast_spell to the current only spell.
for (int i = 0; i < 52; ++i)
{
const char letter = index_to_letter(i);
const spell_type spl = get_spell_by_letter(letter);
if (!is_valid_spell(spl))
continue;
you.last_cast_spell = spl;
break;
}
}
// We allow setting last cast spell by Divine Exegesis, but we
// don't allow recasting it with the UI unless we actually have
// the spell memorized.
if (you.last_cast_spell != SPELL_NO_SPELL
&& !you.has_spell(you.last_cast_spell))
{
you.last_cast_spell = SPELL_NO_SPELL;
}
if (you.last_cast_spell == SPELL_NO_SPELL
|| !Options.enable_recast_spell)
{
mprf(MSGCH_PROMPT, "Cast which spell? (? or * to list) ");
}
else
{
mprf(MSGCH_PROMPT, "Casting: <w>%s</w> <lightgrey>(%s)</lightgrey>",
spell_title(you.last_cast_spell),
_spell_failure_rate_description(you.last_cast_spell).c_str());
mprf(MSGCH_PROMPT, "Confirm with . or Enter, or press "
"? or * to list all spells.");
}
keyin = get_ch();
}
if (keyin == '?' || keyin == '*' || Options.spell_menu)
{
keyin = list_spells(true, false);
if (!keyin)
keyin = ESCAPE;
if (!crawl_state.doing_prev_cmd_again)
{
redraw_screen();
update_screen();
}
if (isaalpha(keyin) || key_is_escape(keyin))
break;
else
clear_messages();
keyin = 0;
}
else
break;
#endif
}
if (key_is_escape(keyin))
{
canned_msg(MSG_OK);
crawl_state.zero_turns_taken();
return spret::abort;
}
else if (Options.enable_recast_spell
&& (keyin == '.' || keyin == CK_ENTER))
{
spell = you.last_cast_spell;
}
else if (!isaalpha(keyin))
{
mpr("You don't know that spell.");
crawl_state.zero_turns_taken();
return spret::abort;
}
else
spell = get_spell_by_letter(keyin);
}
if (spell == SPELL_NO_SPELL)
{
mpr("You don't know that spell.");
crawl_state.zero_turns_taken();
return spret::abort;
}
// MP, confusion, Ru sacs
const auto reason = casting_uselessness_reason(spell, true);
if (!reason.empty())
{
mpr(reason);
crawl_state.zero_turns_taken();
return spret::abort;
}
if (check_range && spell_no_hostile_in_range(spell))
{
// Abort if there are no hostiles within range, but flash the range
// markers for a short while.
mpr("You can't see any susceptible monsters within range! "
"(Use <w>Z</w> to cast anyway.)");
if ((Options.use_animations & UA_RANGE) && Options.darken_beyond_range)
{
targeter_smite range(&you, calc_spell_range(spell), 0, 0, true);
range_view_annotator show_range(&range);
delay(50);
}
crawl_state.zero_turns_taken();
return spret::abort;
}
if (god_punishes_spell(spell, you.religion)
&& !crawl_state.disables[DIS_CONFIRMATIONS])
{
// None currently dock just piety, right?
if (!yesno(god_loathes_spell(spell, you.religion) ?
"Casting this spell will cause instant excommunication! "
"Really cast?" :
"Casting this spell will place you under penance. Really cast?",
true, 'n'))
{
canned_msg(MSG_OK);
crawl_state.zero_turns_taken();
return spret::abort;
}
}
you.last_cast_spell = spell;
// Silently take MP before the spell.
const int cost = spell_mana(spell);
pay_mp(cost);
// Majin Bo HP cost taken at the same time
// (but after hp costs from HP casting)
const int hp_cost = min(spell_mana(spell), you.hp - 1);
if (_majin_charge_hp())
pay_hp(hp_cost);
const spret cast_result = your_spells(spell, 0, !you.divine_exegesis,
nullptr, _target, force_failure);
if (cast_result == spret::abort
|| you.divine_exegesis && cast_result == spret::fail)
{
if (cast_result == spret::abort)
crawl_state.zero_turns_taken();
// Return the MP since the spell is aborted.
refund_mp(cost);
if (_majin_charge_hp())
refund_hp(hp_cost);
redraw_screen();
update_screen();
return cast_result;
}
practise_casting(spell, cast_result == spret::success);
if (cast_result == spret::success)
{
_handle_wucad_mu(cost);
if (player_equip_unrand(UNRAND_MAJIN) && one_chance_in(500))
_majin_speak(spell);
did_god_conduct(DID_SPELL_CASTING, 1 + random2(5));
count_action(CACT_CAST, spell);
}
finalize_mp_cost(_majin_charge_hp() ? hp_cost : 0);
you.turn_is_over = true;
alert_nearby_monsters();
return cast_result;
}
/**
* Handles divine response to spellcasting.
*
* @param spell The type of spell just cast.
*/
static void _spellcasting_god_conduct(spell_type spell)
{
// If you are casting while a god is acting, then don't do conducts.
// (Presumably Xom is forcing you to cast a spell.)
if (crawl_state.is_god_acting())
return;
const int conduct_level = 10 + spell_difficulty(spell);
if (is_evil_spell(spell) || you.spellcasting_unholy())
did_god_conduct(DID_EVIL, conduct_level);
if (is_unclean_spell(spell))
did_god_conduct(DID_UNCLEAN, conduct_level);
if (is_chaotic_spell(spell))
did_god_conduct(DID_CHAOS, conduct_level);
// not is_hasty_spell since the other ones handle the conduct themselves.
if (spell == SPELL_SWIFTNESS)
did_god_conduct(DID_HASTY, conduct_level);
if (spell == SPELL_SUBLIMATION_OF_BLOOD)