-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
spl-util.cc
1589 lines (1360 loc) · 46.4 KB
/
spl-util.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 data handlers for player-available spell list
**/
#include "AppHdr.h"
#include "spl-util.h"
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "areas.h"
#include "coordit.h"
#include "directn.h"
#include "english.h"
#include "env.h"
#include "god-passive.h"
#include "god-abil.h"
#include "item-prop.h"
#include "level-state-type.h"
#include "libutil.h"
#include "message.h"
#include "notes.h"
#include "options.h"
#include "orb.h"
#include "output.h"
#include "prompt.h"
#include "religion.h"
#include "spl-book.h"
#include "spl-damage.h"
#include "spl-summoning.h"
#include "spl-zap.h"
#include "stringutil.h"
#include "target.h"
#include "terrain.h"
#include "tiledef-gui.h" // spell tiles
#include "tiles-build-specific.h"
#include "transform.h"
struct spell_desc
{
spell_type id;
const char *title;
spschools_type disciplines;
unsigned int flags; // bitfield
unsigned int level;
// Usually in the range 0..200 (0 means uncapped).
// Note that some spells are also capped through zap_type.
// See spell_power_cap below.
int power_cap;
// At power 0, you get min_range. At power power_cap, you get max_range.
int min_range;
int max_range;
// Noise made directly by casting this spell.
// Noise used to be based directly on spell level:
// * for conjurations: spell level
// * for non-conj pois/air: spell level / 2 (rounded up)
// * for others: spell level * 3/4 (rounded up)
// These are probably good guidelines for new spells.
int noise;
// Some spells have a noise at their place of effect, in addition
// to at the place of casting. effect_noise handles that, and is also
// used even if the spell is not casted directly (by Xom, for instance).
int effect_noise;
/// Icon for the spell in e.g. spellbooks, casting menus, etc.
tileidx_t tile;
};
#include "spl-data.h"
static int spell_list[NUM_SPELLS];
#define SPELLDATASIZE ARRAYSZ(spelldata)
static const struct spell_desc *_seekspell(spell_type spellid);
//
// BEGIN PUBLIC FUNCTIONS
//
// All this does is merely refresh the internal spell list {dlb}:
void init_spell_descs()
{
for (int i = 0; i < NUM_SPELLS; i++)
spell_list[i] = -1;
for (unsigned int i = 0; i < SPELLDATASIZE; i++)
{
const spell_desc &data = spelldata[i];
ASSERTM(data.id >= SPELL_NO_SPELL && data.id < NUM_SPELLS,
"spell #%d has invalid id %d", i, data.id);
ASSERTM(data.title != nullptr && *data.title,
"spell #%d, id %d has no name", i, data.id);
ASSERTM(data.level >= 1 && data.level <= 9,
"spell '%s' has invalid level %d", data.title, data.level);
ASSERTM(data.min_range <= data.max_range,
"spell '%s' has min_range larger than max_range", data.title);
ASSERTM(!(data.flags & SPFLAG_TARGETING_MASK)
|| (data.min_range >= 0 && data.max_range > 0),
"targeted/directed spell '%s' has invalid range", data.title);
ASSERTM(!(data.flags & SPFLAG_MONSTER && is_player_spell(data.id)),
"spell '%s' is declared as a monster spell but is a player spell", data.title);
spell_list[data.id] = i;
}
}
typedef map<string, spell_type> spell_name_map;
static spell_name_map spell_name_cache;
void init_spell_name_cache()
{
for (int i = 0; i < NUM_SPELLS; i++)
{
spell_type type = static_cast<spell_type>(i);
if (!is_valid_spell(type))
continue;
const char *sptitle = spell_title(type);
ASSERT(sptitle);
const string spell_name = lowercase_string(sptitle);
spell_name_cache[spell_name] = type;
}
}
spell_type spell_by_name(string name, bool partial_match)
{
if (name.empty())
return SPELL_NO_SPELL;
lowercase(name);
if (!partial_match)
return lookup(spell_name_cache, name, SPELL_NO_SPELL);
const spell_type sp = find_earliest_match(name, SPELL_NO_SPELL, NUM_SPELLS,
is_valid_spell, spell_title);
return sp == NUM_SPELLS ? SPELL_NO_SPELL : sp;
}
spschool_flag_type school_by_name(string name)
{
spschool_flag_type short_match, long_match;
int short_matches, long_matches;
short_match = long_match = SPTYP_NONE;
short_matches = long_matches = 0;
lowercase(name);
for (int i = 0; i <= SPTYP_RANDOM; i++)
{
const auto type = spschools_type::exponent(i);
string short_name = spelltype_short_name(type);
string long_name = spelltype_long_name(type);
lowercase(short_name);
lowercase(long_name);
if (name == short_name)
return type;
if (name == long_name)
return type;
if (short_name.find(name) != string::npos)
{
short_match = type;
short_matches++;
}
if (long_name.find(name) != string::npos)
{
long_match = type;
long_matches++;
}
}
if (short_matches != 1 && long_matches != 1)
return SPTYP_NONE;
if (short_matches == 1 && long_matches != 1)
return short_match;
if (short_matches != 1 && long_matches == 1)
return long_match;
if (short_match == long_match)
return short_match;
return SPTYP_NONE;
}
int get_spell_slot_by_letter(char letter)
{
ASSERT(isaalpha(letter));
const int index = letter_to_index(letter);
if (you.spell_letter_table[ index ] == -1)
return -1;
return you.spell_letter_table[index];
}
static int _get_spell_slot(spell_type spell)
{
// you.spells is a FixedVector of spells in some arbitrary order. It
// doesn't corespond to letters.
auto i = find(begin(you.spells), end(you.spells), spell);
return i == end(you.spells) ? -1 : i - begin(you.spells);
}
static int _get_spell_letter_from_slot(int slot)
{
// you.spell_letter_table is a FixedVector that is basically a mapping
// from alpha char indices to spell slots (e.g. indices in you.spells).
auto letter = find(begin(you.spell_letter_table), end(you.spell_letter_table), slot);
return letter == end(you.spell_letter_table) ? -1 : letter - begin(you.spell_letter_table);
}
int get_spell_letter(spell_type spell)
{
int i = _get_spell_letter_from_slot(_get_spell_slot(spell));
return (i == -1) ? -1 : index_to_letter(i);
}
spell_type get_spell_by_letter(char letter)
{
ASSERT(isaalpha(letter));
const int slot = get_spell_slot_by_letter(letter);
return (slot == -1) ? SPELL_NO_SPELL : you.spells[slot];
}
bool add_spell_to_memory(spell_type spell)
{
int slot_i;
int letter_j = -1;
string sname = spell_title(spell);
lowercase(sname);
// first we find a slot in our head:
for (slot_i = 0; slot_i < MAX_KNOWN_SPELLS; slot_i++)
{
if (you.spells[slot_i] == SPELL_NO_SPELL)
break;
}
you.spells[slot_i] = spell;
// now we find an available label:
// first check to see whether we've chosen an automatic label:
bool overwrite = false;
for (const auto &entry : Options.auto_spell_letters)
{
if (!entry.first.matches(sname))
continue;
for (char ch : entry.second)
{
if (ch == '+')
overwrite = true;
else if (ch == '-')
overwrite = false;
else if (isaalpha(ch))
{
const int new_letter = letter_to_index(ch);
const int existing_slot = you.spell_letter_table[new_letter];
if (existing_slot == -1)
{
letter_j = new_letter;
break;
}
else if (overwrite)
{
const string ename = lowercase_string(
spell_title(get_spell_by_letter(ch)));
// Don't overwrite a spell matched by the same rule.
if (!entry.first.matches(ename))
{
letter_j = new_letter;
break;
}
}
// Otherwise continue on to the next letter in this rule.
}
}
if (letter_j != -1)
break;
}
// If we didn't find a label above, choose the first available one.
if (letter_j == -1)
for (letter_j = 0; letter_j < 52; letter_j++)
{
if (you.spell_letter_table[letter_j] == -1)
break;
}
if (you.num_turns)
mprf("Spell assigned to '%c'.", index_to_letter(letter_j));
// Swapping with an existing spell.
if (you.spell_letter_table[letter_j] != -1)
{
// Find a spot for the spell being moved. Assumes there will be one.
for (int free_letter = 0; free_letter < 52; free_letter++)
if (you.spell_letter_table[free_letter] == -1)
{
you.spell_letter_table[free_letter] = you.spell_letter_table[letter_j];
break;
}
}
you.spell_letter_table[letter_j] = slot_i;
you.spell_no++;
take_note(Note(NOTE_LEARN_SPELL, spell));
spell_skills(spell, you.start_train);
#ifdef USE_TILE_LOCAL
tiles.layout_statcol();
redraw_screen();
#endif
return true;
}
static void _remove_spell_attributes(spell_type spell)
{
switch (spell)
{
case SPELL_DEFLECT_MISSILES:
if (you.attribute[ATTR_DEFLECT_MISSILES])
{
const int orig_defl = you.missile_deflection();
you.attribute[ATTR_DEFLECT_MISSILES] = 0;
mprf(MSGCH_DURATION, "You feel %s from missiles.",
you.missile_deflection() < orig_defl
? "less protected"
: "your spell is no longer protecting you");
}
break;
default:
break;
}
return;
}
bool del_spell_from_memory_by_slot(int slot)
{
ASSERT_RANGE(slot, 0, MAX_KNOWN_SPELLS);
if (you.last_cast_spell == you.spells[slot])
you.last_cast_spell = SPELL_NO_SPELL;
spell_skills(you.spells[slot], you.stop_train);
mprf("Your memory of %s unravels.", spell_title(you.spells[slot]));
_remove_spell_attributes(you.spells[slot]);
you.spells[slot] = SPELL_NO_SPELL;
for (int j = 0; j < 52; j++)
if (you.spell_letter_table[j] == slot)
you.spell_letter_table[j] = -1;
you.spell_no--;
#ifdef USE_TILE_LOCAL
tiles.layout_statcol();
redraw_screen();
#endif
return true;
}
bool del_spell_from_memory(spell_type spell)
{
int i = _get_spell_slot(spell);
if (i == -1)
return false;
else
return del_spell_from_memory_by_slot(i);
}
int spell_hunger(spell_type which_spell)
{
if (player_energy())
return 0;
const int level = spell_difficulty(which_spell);
const int basehunger[] = { 50, 100, 150, 250, 400, 550, 700, 850, 1000 };
int hunger;
if (level < 10 && level > 0)
hunger = basehunger[level-1];
else
hunger = (basehunger[0] * level * level) / 4;
hunger -= you.skill(SK_SPELLCASTING, you.intel());
if (hunger < 0)
hunger = 0;
return hunger;
}
// Checks if the spell is an explosion that can be placed anywhere even without
// an unobstructed beam path, such as fire storm.
bool spell_is_direct_explosion(spell_type spell)
{
return spell == SPELL_FIRE_STORM || spell == SPELL_CALL_DOWN_DAMNATION
|| spell == SPELL_GHOSTLY_SACRIFICE || spell == SPELL_UPHEAVAL;
}
bool spell_harms_target(spell_type spell)
{
const unsigned int flags = _seekspell(spell)->flags;
if (flags & (SPFLAG_HELPFUL | SPFLAG_NEUTRAL))
return false;
if (flags & SPFLAG_TARGETING_MASK)
return true;
return false;
}
bool spell_harms_area(spell_type spell)
{
const unsigned int flags = _seekspell(spell)->flags;
if (flags & (SPFLAG_HELPFUL | SPFLAG_NEUTRAL))
return false;
if (flags & SPFLAG_AREA)
return true;
return false;
}
// applied to spell misfires (more power = worse) and triggers
// for Xom acting (more power = more likely to grab his attention) {dlb}
int spell_mana(spell_type which_spell)
{
return _seekspell(which_spell)->level;
}
// applied in naughties (more difficult = higher level knowledge = worse)
// and triggers for Sif acting (same reasoning as above, just good) {dlb}
int spell_difficulty(spell_type which_spell)
{
return _seekspell(which_spell)->level;
}
int spell_levels_required(spell_type which_spell)
{
int levels = spell_difficulty(which_spell);
#if TAG_MAJOR_VERSION == 34
if (which_spell == SPELL_DELAYED_FIREBALL
&& you.has_spell(SPELL_FIREBALL))
{
levels -= spell_difficulty(SPELL_FIREBALL);
}
else if (which_spell == SPELL_FIREBALL
&& you.has_spell(SPELL_DELAYED_FIREBALL))
{
levels = 0;
}
#endif
return levels;
}
unsigned int get_spell_flags(spell_type which_spell)
{
return _seekspell(which_spell)->flags;
}
const char *get_spell_target_prompt(spell_type which_spell)
{
switch (which_spell)
{
case SPELL_APPORTATION:
return "Apport";
case SPELL_SMITING:
return "Smite";
case SPELL_LRD:
return "Fragment what (e.g. wall or brittle monster)?";
default:
return nullptr;
}
}
/// What's the icon for the given spell?
tileidx_t get_spell_tile(spell_type which_spell)
{
return _seekspell(which_spell)->tile;
}
bool spell_typematch(spell_type which_spell, spschool_flag_type which_disc)
{
return bool(get_spell_disciplines(which_spell) & which_disc);
}
//jmf: next two for simple bit handling
spschools_type get_spell_disciplines(spell_type spell)
{
return _seekspell(spell)->disciplines;
}
int count_bits(uint64_t bits)
{
uint64_t n;
int c = 0;
for (n = 1; n; n <<= 1)
if (n & bits)
c++;
return c;
}
const char *spell_title(spell_type spell)
{
return _seekspell(spell)->title;
}
// FUNCTION APPLICATORS: Idea from Juho Snellman <jsnell@lyseo.edu.ouka.fi>
// on the Roguelike News pages, Development section.
// <URL:http://www.skoardy.demon.co.uk/rlnews/>
// Here are some function applicators: sort of like brain-dead,
// home-grown iterators for the container "dungeon".
// Apply a function-pointer to all visible squares
// Returns summation of return values from passed in function.
int apply_area_visible(cell_func cf, const coord_def &where)
{
int rv = 0;
for (radius_iterator ri(where, LOS_NO_TRANS); ri; ++ri)
rv += cf(*ri);
return rv;
}
// Applies the effect to all nine squares around/including the target.
// Returns summation of return values from passed in function.
static int _apply_area_square(cell_func cf, const coord_def& where)
{
int rv = 0;
for (adjacent_iterator ai(where, false); ai; ++ai)
rv += cf(*ai);
return rv;
}
// Applies the effect to the eight squares beside the target.
// Returns summation of return values from passed in function.
static int _apply_area_around_square(cell_func cf, const coord_def& where)
{
int rv = 0;
for (adjacent_iterator ai(where, true); ai; ++ai)
rv += cf(*ai);
return rv;
}
// Affect up to max_targs monsters around a point, chosen randomly.
// Return varies with the function called; return values will be added up.
int apply_random_around_square(cell_func cf, const coord_def& where,
bool exclude_center, int max_targs)
{
int rv = 0;
if (max_targs <= 0)
return 0;
if (max_targs >= 9 && !exclude_center)
return _apply_area_square(cf, where);
if (max_targs >= 8 && exclude_center)
return _apply_area_around_square(cf, where);
coord_def targs[8];
int count = 0;
for (adjacent_iterator ai(where, exclude_center); ai; ++ai)
{
if (monster_at(*ai) == nullptr && *ai != you.pos())
continue;
// Found target
count++;
// Slight difference here over the basic algorithm...
//
// For cases where the number of choices <= max_targs it's
// obvious (all available choices will be selected).
//
// For choices > max_targs, here's a brief proof:
//
// Let m = max_targs, k = choices - max_targs, k > 0.
//
// Proof, by induction (over k):
//
// 1) Show n = m + 1 (k = 1) gives uniform distribution,
// P(new one not chosen) = 1 / (m + 1).
// m 1 1
// P(specific previous one replaced) = --- * --- = ---
// m+1 m m+1
//
// So the probablity is uniform (ie. any element has
// a 1/(m+1) chance of being in the unchosen slot).
//
// 2) Assume the distribution is uniform at n = m+k.
// (ie. the probablity that any of the found elements
// was chosen = m / (m+k) (the slots are symmetric,
// so it's the sum of the probabilities of being in
// any of them)).
//
// 3) Show n = m + k + 1 gives a uniform distribution.
// P(new one chosen) = m / (m + k + 1)
// P(any specific previous choice remaining chosen)
// = [1 - P(swapped into m+k+1 position)] * P(prev. chosen)
// m 1 m
// = [ 1 - ----- * --- ] * ---
// m+k+1 m m+k
//
// m+k m m
// = ----- * --- = -----
// m+k+1 m+k m+k+1
//
// Therefore, it's uniform for n = m + k + 1. QED
//
// The important thing to note in calculating the last
// probability is that the chosen elements have already
// passed tests which verify that they *don't* belong
// in slots m+1...m+k, so the only positions an already
// chosen element can end up in are its original
// position (in one of the chosen slots), or in the
// new slot.
//
// The new item can, of course, be placed in any slot,
// swapping the value there into the new slot... we
// just don't care about the non-chosen slots enough
// to store them, so it might look like the item
// automatically takes the new slot when not chosen
// (although, by symmetry all the non-chosen slots are
// the same... and similarly, by symmetry, all chosen
// slots are the same).
//
// Yes, that's a long comment for a short piece of
// code, but I want people to have an understanding
// of why this works (or at least make them wary about
// changing it without proof and breaking this code). -- bwr
// Accept the first max_targs choices, then when
// new choices come up, replace one of the choices
// at random, max_targs/count of the time (the rest
// of the time it replaces an element in an unchosen
// slot -- but we don't care about them).
if (count <= max_targs)
targs[count - 1] = *ai;
else if (x_chance_in_y(max_targs, count))
{
const int pick = random2(max_targs);
targs[ pick ] = *ai;
}
}
const int targs_found = min(count, max_targs);
if (targs_found)
{
// Used to divide the power up among the targets here, but
// it's probably better to allow the full power through and
// balance the called function. -- bwr
for (int i = 0; i < targs_found; i++)
{
ASSERT(!targs[i].origin());
rv += cf(targs[i]);
}
}
return rv;
}
void apply_area_cloud(cloud_func func, const coord_def& where,
int pow, int number, cloud_type ctype,
const actor *agent, int spread_rate, int excl_rad)
{
if (number <= 0)
return;
targeter_cloud place(agent, GDM, number, number);
if (!place.set_aim(where))
return;
unsigned int dist = 0;
while (number > 0)
{
while (place.queue[dist].empty())
if (++dist >= place.queue.size())
return;
vector<coord_def> &q = place.queue[dist];
int el = random2(q.size());
coord_def c = q[el];
q[el] = q[q.size() - 1];
q.pop_back();
if (place.seen[c] <= 0 || cell_is_solid(c))
continue;
func(c, pow, spread_rate, ctype, agent, excl_rad);
number--;
}
}
/**
* Select a spell target and fill dist and pbolt appropriately.
*
* @param[out] spelld the output of the direction() call.
* @param[in, out] pbolt a beam; its range is used if none is set in args, and
* its source and target are set if the direction() call
* succeeds.
* @param[in] args The arguments for the direction() call. May be null,
* which case a default is used.
* @return false if the user cancelled, true otherwise.
*/
bool spell_direction(dist &spelld, bolt &pbolt, direction_chooser_args *args)
{
direction_chooser_args newargs;
// This should be before the overwrite, so callers can specify a different
// mode if they want.
newargs.mode = TARG_HOSTILE;
if (args)
newargs = *args;
if (newargs.range < 1)
newargs.range = (pbolt.range < 1) ? you.current_vision : pbolt.range;
direction(spelld, newargs);
if (!spelld.isValid)
{
// Check for user cancel.
canned_msg(MSG_OK);
return false;
}
pbolt.set_target(spelld);
pbolt.source = you.pos();
return true;
}
const char* spelltype_short_name(spschool_flag_type which_spelltype)
{
switch (which_spelltype)
{
case SPTYP_CONJURATION:
return "Conj";
case SPTYP_HEXES:
return "Hex";
case SPTYP_CHARMS:
return "Chrm";
case SPTYP_FIRE:
return "Fire";
case SPTYP_ICE:
return "Ice";
case SPTYP_TRANSMUTATION:
return "Tmut";
case SPTYP_NECROMANCY:
return "Necr";
case SPTYP_SUMMONING:
return "Summ";
case SPTYP_TRANSLOCATION:
return "Tloc";
case SPTYP_POISON:
return "Pois";
case SPTYP_EARTH:
return "Erth";
case SPTYP_AIR:
return "Air";
case SPTYP_RANDOM:
return "Rndm";
default:
return "Bug";
}
}
const char* spelltype_long_name(spschool_flag_type which_spelltype)
{
switch (which_spelltype)
{
case SPTYP_CONJURATION:
return "Conjuration";
case SPTYP_HEXES:
return "Hexes";
case SPTYP_CHARMS:
return "Charms";
case SPTYP_FIRE:
return "Fire";
case SPTYP_ICE:
return "Ice";
case SPTYP_TRANSMUTATION:
return "Transmutation";
case SPTYP_NECROMANCY:
return "Necromancy";
case SPTYP_SUMMONING:
return "Summoning";
case SPTYP_TRANSLOCATION:
return "Translocation";
case SPTYP_POISON:
return "Poison";
case SPTYP_EARTH:
return "Earth";
case SPTYP_AIR:
return "Air";
case SPTYP_RANDOM:
return "Random";
default:
return "Bug";
}
}
skill_type spell_type2skill(spschool_flag_type spelltype)
{
switch (spelltype)
{
case SPTYP_CONJURATION: return SK_CONJURATIONS;
case SPTYP_HEXES: return SK_HEXES;
case SPTYP_CHARMS: return SK_CHARMS;
case SPTYP_FIRE: return SK_FIRE_MAGIC;
case SPTYP_ICE: return SK_ICE_MAGIC;
case SPTYP_TRANSMUTATION: return SK_TRANSMUTATIONS;
case SPTYP_NECROMANCY: return SK_NECROMANCY;
case SPTYP_SUMMONING: return SK_SUMMONINGS;
case SPTYP_TRANSLOCATION: return SK_TRANSLOCATIONS;
case SPTYP_POISON: return SK_POISON_MAGIC;
case SPTYP_EARTH: return SK_EARTH_MAGIC;
case SPTYP_AIR: return SK_AIR_MAGIC;
default:
dprf("spell_type2skill: called with spelltype %u", spelltype);
return SK_NONE;
}
}
spschool_flag_type skill2spell_type(skill_type spell_skill)
{
switch (spell_skill)
{
case SK_CONJURATIONS: return SPTYP_CONJURATION;
case SK_HEXES: return SPTYP_HEXES;
case SK_CHARMS: return SPTYP_CHARMS;
case SK_FIRE_MAGIC: return SPTYP_FIRE;
case SK_ICE_MAGIC: return SPTYP_ICE;
case SK_TRANSMUTATIONS: return SPTYP_TRANSMUTATION;
case SK_NECROMANCY: return SPTYP_NECROMANCY;
case SK_SUMMONINGS: return SPTYP_SUMMONING;
case SK_TRANSLOCATIONS: return SPTYP_TRANSLOCATION;
case SK_POISON_MAGIC: return SPTYP_POISON;
case SK_EARTH_MAGIC: return SPTYP_EARTH;
case SK_AIR_MAGIC: return SPTYP_AIR;
default:
return SPTYP_NONE;
}
}
/*
**************************************************
* *
* END PUBLIC FUNCTIONS *
* *
**************************************************
*/
//jmf: Simplified; moved init code to top function, init_spell_descs().
static const spell_desc *_seekspell(spell_type spell)
{
ASSERT_RANGE(spell, 0, NUM_SPELLS);
const int index = spell_list[spell];
ASSERT(index != -1);
return &spelldata[index];
}
bool is_valid_spell(spell_type spell)
{
return spell > SPELL_NO_SPELL && spell < NUM_SPELLS
&& spell_list[spell] != -1;
}
static bool _spell_range_varies(spell_type spell)
{
int minrange = _seekspell(spell)->min_range;
int maxrange = _seekspell(spell)->max_range;
return minrange < maxrange;
}
int spell_power_cap(spell_type spell)
{
const int scap = _seekspell(spell)->power_cap;
const int zcap = spell_zap_power_cap(spell);
if (scap == 0)
return zcap;
else if (zcap == 0)
return scap;
else
{
// Two separate power caps; pre-zapping spell power
// goes into range.
if (scap <= zcap || _spell_range_varies(spell))
return scap;
else
return zcap;
}
}
int spell_range(spell_type spell, int pow, bool allow_bonus)
{
int minrange = _seekspell(spell)->min_range;
int maxrange = _seekspell(spell)->max_range;
ASSERT(maxrange >= minrange);
// spells with no range have maxrange == minrange == -1
if (maxrange < 0)
return maxrange;
if (allow_bonus
&& vehumet_supports_spell(spell)
&& have_passive(passive_t::spells_range)
&& maxrange > 1
&& spell != SPELL_GLACIATE)
{
maxrange++;
minrange++;
}
if (minrange == maxrange)
return min(minrange, (int)you.current_vision);
const int powercap = spell_power_cap(spell);
if (powercap <= pow)
return min(maxrange, (int)you.current_vision);
// Round appropriately.
return min((int)you.current_vision,
(pow * (maxrange - minrange) + powercap / 2) / powercap + minrange);
}
/**
* Spell casting noise.
*
* @param spell The spell being casted.
* @return The amount of noise generated on cast.
*/
int spell_noise(spell_type spell)
{
return _seekspell(spell)->noise;
}
/**
* Miscellaneous spell casting noise.
*
* This returns the usual spell noise for the effects of this spell.
* Used for various noisy() calls, as well as the I screen; see effect_noise
* comment above for more information.
* @param spell The spell being casted.
* @return The amount of noise generated by the effects of the spell.
*/
int spell_effect_noise(spell_type spell)
{
int expl_size;
switch (spell)