-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
magic.cpp
3310 lines (2996 loc) · 128 KB
/
magic.cpp
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
#include "magic.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <set>
#include <utility>
#include "avatar.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_imgui.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "color.h"
#include "condition.h"
#include "creature.h"
#include "creature_tracker.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "field.h"
#include "generic_factory.h"
#include "imgui/imgui.h"
#include "input_context.h"
#include "inventory.h"
#include "item.h"
#include "json.h"
#include "line.h"
#include "localized_comparator.h"
#include "magic_enchantment.h"
#include "map.h"
#include "map_iterator.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "npc.h"
#include "output.h"
#include "pimpl.h"
#include "point.h"
#include "projectile.h"
#include "requirements.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "translations.h"
#include "ui.h"
#include "units.h"
static const ammo_effect_str_id ammo_effect_MAGIC( "MAGIC" );
static const json_character_flag json_flag_CANNOT_ATTACK( "CANNOT_ATTACK" );
static const json_character_flag json_flag_NO_PSIONICS( "NO_PSIONICS" );
static const json_character_flag json_flag_NO_SPELLCASTING( "NO_SPELLCASTING" );
static const json_character_flag json_flag_SILENT_SPELL( "SILENT_SPELL" );
static const json_character_flag json_flag_SUBTLE_SPELL( "SUBTLE_SPELL" );
static const proficiency_id proficiency_prof_concentration_basic( "prof_concentration_basic" );
static const proficiency_id
proficiency_prof_concentration_intermediate( "prof_concentration_intermediate" );
static const proficiency_id proficiency_prof_concentration_master( "prof_concentration_master" );
static const skill_id skill_spellcraft( "spellcraft" );
static const trait_id trait_NONE( "NONE" );
static std::string target_to_string( spell_target data )
{
switch( data ) {
case spell_target::ally:
return pgettext( "Valid spell target", "ally" );
case spell_target::hostile:
return pgettext( "Valid spell target", "hostile" );
case spell_target::self:
return pgettext( "Valid spell target", "self" );
case spell_target::ground:
return pgettext( "Valid spell target", "ground" );
case spell_target::none:
return pgettext( "Valid spell target", "none" );
case spell_target::item:
return pgettext( "Valid spell target", "item" );
case spell_target::field:
return pgettext( "Valid spell target", "field" );
case spell_target::num_spell_targets:
break;
}
debugmsg( "Invalid valid_target" );
return "THIS IS A BUG";
}
namespace io
{
// *INDENT-OFF*
template<>
std::string enum_to_string<spell_target>( spell_target data )
{
switch( data ) {
case spell_target::ally: return "ally";
case spell_target::hostile: return "hostile";
case spell_target::self: return "self";
case spell_target::ground: return "ground";
case spell_target::none: return "none";
case spell_target::item: return "item";
case spell_target::field: return "field";
case spell_target::num_spell_targets: break;
}
cata_fatal( "Invalid valid_target" );
}
template<>
std::string enum_to_string<spell_shape>( spell_shape data )
{
switch( data ) {
case spell_shape::blast: return "blast";
case spell_shape::cone: return "cone";
case spell_shape::line: return "line";
case spell_shape::num_shapes: break;
}
cata_fatal( "Invalid spell_shape" );
}
template<>
std::string enum_to_string<spell_flag>( spell_flag data )
{
switch( data ) {
case spell_flag::PERMANENT: return "PERMANENT";
case spell_flag::PERMANENT_ALL_LEVELS: return "PERMANENT_ALL_LEVELS";
case spell_flag::PERCENTAGE_DAMAGE: return "PERCENTAGE_DAMAGE";
case spell_flag::SPLIT_DAMAGE: return "SPLIT_DAMAGE";
case spell_flag::IGNORE_WALLS: return "IGNORE_WALLS";
case spell_flag::NO_PROJECTILE: return "NO_PROJECTILE";
case spell_flag::HOSTILE_SUMMON: return "HOSTILE_SUMMON";
case spell_flag::HOSTILE_50: return "HOSTILE_50";
case spell_flag::FRIENDLY_POLY: return "FRIENDLY_POLY";
case spell_flag::POLYMORPH_GROUP: return "POLYMORPH_GROUP";
case spell_flag::SILENT: return "SILENT";
case spell_flag::NO_EXPLOSION_SFX: return "NO_EXPLOSION_SFX";
case spell_flag::LOUD: return "LOUD";
case spell_flag::VERBAL: return "VERBAL";
case spell_flag::SOMATIC: return "SOMATIC";
case spell_flag::NO_HANDS: return "NO_HANDS";
case spell_flag::NO_LEGS: return "NO_LEGS";
case spell_flag::UNSAFE_TELEPORT: return "UNSAFE_TELEPORT";
case spell_flag::TARGET_TELEPORT: return "TARGET_TELEPORT";
case spell_flag::SWAP_POS: return "SWAP_POS";
case spell_flag::CONCENTRATE: return "CONCENTRATE";
case spell_flag::RANDOM_AOE: return "RANDOM_AOE";
case spell_flag::RANDOM_DAMAGE: return "RANDOM_DAMAGE";
case spell_flag::RANDOM_DURATION: return "RANDOM_DURATION";
case spell_flag::RANDOM_TARGET: return "RANDOM_TARGET";
case spell_flag::RANDOM_CRITTER: return "RANDOM_CRITTER";
case spell_flag::MUTATE_TRAIT: return "MUTATE_TRAIT";
case spell_flag::PAIN_NORESIST: return "PAIN_NORESIST";
case spell_flag::SPAWN_GROUP: return "SPAWN_GROUP";
case spell_flag::IGNITE_FLAMMABLE: return "IGNITE_FLAMMABLE";
case spell_flag::NO_FAIL: return "NO_FAIL";
case spell_flag::WONDER: return "WONDER";
case spell_flag::EXTRA_EFFECTS_FIRST: return "EXTRA_EFFECTS_FIRST";
case spell_flag::MUST_HAVE_CLASS_TO_LEARN: return "MUST_HAVE_CLASS_TO_LEARN";
case spell_flag::SPAWN_WITH_DEATH_DROPS: return "SPAWN_WITH_DEATH_DROPS";
case spell_flag::NO_CORPSE_QUIET: return "NO_CORPSE_QUIET";
case spell_flag::NON_MAGICAL: return "NON_MAGICAL";
case spell_flag::PSIONIC: return "PSIONIC";
case spell_flag::RECHARM: return "RECHARM";
case spell_flag::EVOCATION_SPELL: return "EVOCATION_SPELL";
case spell_flag::CHANNELING_SPELL: return "CHANNELING_SPELL";
case spell_flag::CONJURATION_SPELL: return "CONJURATION_SPELL";
case spell_flag::ENHANCEMENT_SPELL: return "ENHANCEMENT_SPELL";
case spell_flag::ENERVATION_SPELL: return "ENERVATION_SPELL";
case spell_flag::CONVEYANCE_SPELL: return "CONVEYANCE_SPELL";
case spell_flag::RESTORATION_SPELL: return "RESTORATION_SPELL";
case spell_flag::TRANSFORMATION_SPELL: return "TRANSFORMATION_SPELL";
case spell_flag::LAST: break;
}
cata_fatal( "Invalid spell_flag" );
}
template<>
std::string enum_to_string<magic_energy_type>( magic_energy_type data )
{
switch( data ) {
case magic_energy_type::bionic: return "BIONIC";
case magic_energy_type::hp: return "HP";
case magic_energy_type::mana: return "MANA";
case magic_energy_type::none: return "NONE";
case magic_energy_type::stamina: return "STAMINA";
case magic_energy_type::last: break;
}
cata_fatal( "Invalid magic_energy_type" );
}
// *INDENT-ON*
} // namespace io
const std::optional<int> fake_spell::max_level_default = std::nullopt;
const int fake_spell::level_default = 0;
const bool fake_spell::self_default = false;
const int fake_spell::trigger_once_in_default = 1;
const skill_id spell_type::skill_default = skill_spellcraft;
// empty string
const requirement_id spell_type::spell_components_default;
const translation spell_type::message_default = to_translation( "You cast %s!" );
const translation spell_type::sound_description_default = to_translation( "an explosion." );
const sounds::sound_t spell_type::sound_type_default = sounds::sound_t::combat;
const bool spell_type::sound_ambient_default = false;
// empty string
const std::string spell_type::sound_id_default;
const std::string spell_type::sound_variant_default = "default";
// empty string
const std::string spell_type::effect_str_default;
const std::optional<field_type_id> spell_type::field_default = std::nullopt;
const int spell_type::field_chance_default = 1;
const int spell_type::min_field_intensity_default = 0;
const int spell_type::max_field_intensity_default = 0;
const float spell_type::field_intensity_increment_default = 0.0f;
const float spell_type::field_intensity_variance_default = 0.0f;
const int spell_type::min_accuracy_default = 20;
const float spell_type::accuracy_increment_default = 0.0f;
const int spell_type::max_accuracy_default = 20;
const int spell_type::min_damage_default = 0;
const float spell_type::damage_increment_default = 0.0f;
const int spell_type::max_damage_default = 0;
const int spell_type::min_range_default = 0;
const float spell_type::range_increment_default = 0.0f;
const int spell_type::max_range_default = 0;
const int spell_type::min_aoe_default = 0;
const float spell_type::aoe_increment_default = 0.0f;
const int spell_type::max_aoe_default = 0;
const int spell_type::min_dot_default = 0;
const float spell_type::dot_increment_default = 0.0f;
const int spell_type::max_dot_default = 0;
const int spell_type::min_duration_default = 0;
const int spell_type::duration_increment_default = 0;
const int spell_type::max_duration_default = 0;
const int spell_type::min_pierce_default = 0;
const float spell_type::pierce_increment_default = 0.0f;
const int spell_type::max_pierce_default = 0;
const float spell_type::min_bash_scaling_default = 0.0f;
const float spell_type::max_bash_scaling_default = 0.0f;
const float spell_type::bash_scaling_increment_default = 0.0f;
const int spell_type::base_energy_cost_default = 0;
const float spell_type::energy_increment_default = 0.0f;
const trait_id spell_type::spell_class_default = trait_NONE;
const magic_energy_type spell_type::energy_source_default = magic_energy_type::none;
const damage_type_id spell_type::dmg_type_default = damage_type_id::NULL_ID();
const int spell_type::multiple_projectiles_default = 0;
const int spell_type::difficulty_default = 0;
const int spell_type::max_level_default = 0;
const int spell_type::base_casting_time_default = 0;
const float spell_type::casting_time_increment_default = 0.0f;
// LOADING
// spell_type
namespace
{
generic_factory<spell_type> spell_factory( "spell" );
} // namespace
template<>
const spell_type &string_id<spell_type>::obj() const
{
return spell_factory.obj( *this );
}
template<>
bool string_id<spell_type>::is_valid() const
{
return spell_factory.is_valid( *this );
}
void spell_type::load_spell( const JsonObject &jo, const std::string &src )
{
spell_factory.load( jo, src );
}
static std::string moves_to_string( const int moves )
{
if( moves < to_moves<int>( 2_seconds ) ) {
return string_format( n_gettext( "%d move", "%d moves", moves ), moves );
} else {
return to_string( time_duration::from_moves( moves ) );
}
}
void spell_type::load( const JsonObject &jo, const std::string_view src )
{
src_mod = mod_id( src );
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "description", description );
optional( jo, was_loaded, "skill", skill, skill_default );
optional( jo, was_loaded, "teachable", teachable, true );
optional( jo, was_loaded, "components", spell_components, spell_components_default );
optional( jo, was_loaded, "message", message, message_default );
optional( jo, was_loaded, "sound_description", sound_description, sound_description_default );
optional( jo, was_loaded, "sound_type", sound_type, sound_type_default );
optional( jo, was_loaded, "sound_ambient", sound_ambient, sound_ambient_default );
optional( jo, was_loaded, "sound_id", sound_id, sound_id_default );
optional( jo, was_loaded, "sound_variant", sound_variant, sound_variant_default );
mandatory( jo, was_loaded, "effect", effect_name );
const auto found_effect = spell_effect::effect_map.find( effect_name );
if( found_effect == spell_effect::effect_map.cend() ) {
effect = spell_effect::none;
debugmsg( "ERROR: spell %s has invalid effect %s", id.c_str(), effect_name );
} else {
effect = found_effect->second;
}
mandatory( jo, was_loaded, "shape", spell_area );
spell_area_function = spell_effect::shape_map.at( spell_area );
const auto targeted_monster_ids_reader = string_id_reader<::mtype> {};
optional( jo, was_loaded, "targeted_monster_ids", targeted_monster_ids,
targeted_monster_ids_reader );
const auto targeted_monster_species_reader = string_id_reader<::species_type> {};
optional( jo, was_loaded, "targeted_monster_species", targeted_species_ids,
targeted_monster_species_reader );
const auto ignored_monster_species_reader = string_id_reader<::species_type> {};
optional( jo, was_loaded, "ignored_monster_species", ignored_species_ids,
ignored_monster_species_reader );
const auto trigger_reader = enum_flags_reader<spell_target> { "valid_targets" };
mandatory( jo, was_loaded, "valid_targets", valid_targets, trigger_reader );
optional( jo, was_loaded, "extra_effects", additional_spells );
optional( jo, was_loaded, "affected_body_parts", affected_bps );
if( jo.has_array( "flags" ) ) {
for( auto &flag : jo.get_string_array( "flags" ) ) {
// Save all provided flags as strings in spell_type.flags
// If the flag is listed as a possible enum of type spell_flag, we also save it to spell_type.spell_tags
flags.insert( flag );
std::optional<spell_flag> f = io::string_to_enum_optional<spell_flag>( flag );
if( f.has_value() ) {
spell_tags.set( f.value() );
}
}
} else if( jo.has_string( "flags" ) ) {
const std::string flag = jo.get_string( "flags" );
flags.insert( flag );
std::optional<spell_flag> f = io::string_to_enum_optional<spell_flag>( flag );
if( f.has_value() ) {
spell_tags.set( f.value() );
}
}
optional( jo, was_loaded, "effect_str", effect_str, effect_str_default );
std::string field_input;
// Because the field this is loading into is not part of this type,
// the default value will not be supplied when using copy-from if we pass was_loaded
// So just pass false instead
optional( jo, false, "field_id", field_input, "none" );
if( field_input != "none" ) {
field = field_type_id( field_input );
}
if( !was_loaded || jo.has_member( "field_chance" ) ) {
field_chance = get_dbl_or_var( jo, "field_chance", false, field_chance_default );
}
if( !was_loaded || jo.has_member( "min_field_intensity" ) ) {
min_field_intensity = get_dbl_or_var( jo, "min_field_intensity", false,
min_field_intensity_default );
}
if( !was_loaded || jo.has_member( "max_field_intensity" ) ) {
max_field_intensity = get_dbl_or_var( jo, "max_field_intensity", false,
max_field_intensity_default );
}
if( !was_loaded || jo.has_member( "field_intensity_increment" ) ) {
field_intensity_increment = get_dbl_or_var( jo, "field_intensity_increment", false,
field_intensity_increment_default );
}
if( !was_loaded || jo.has_member( "field_intensity_variance" ) ) {
field_intensity_variance = get_dbl_or_var( jo, "field_intensity_variance", false,
field_intensity_variance_default );
}
if( !was_loaded || jo.has_member( "min_accuracy" ) ) {
min_accuracy = get_dbl_or_var( jo, "min_accuracy", false, min_accuracy_default );
}
if( !was_loaded || jo.has_member( "accuracy_increment" ) ) {
accuracy_increment = get_dbl_or_var( jo, "accuracy_increment", false,
accuracy_increment_default );
}
if( !was_loaded || jo.has_member( "max_accuracy" ) ) {
max_accuracy = get_dbl_or_var( jo, "max_accuracy", false, max_accuracy_default );
}
if( !was_loaded || jo.has_member( "min_damage" ) ) {
min_damage = get_dbl_or_var( jo, "min_damage", false, min_damage_default );
}
if( !was_loaded || jo.has_member( "damage_increment" ) ) {
damage_increment = get_dbl_or_var( jo, "damage_increment", false,
damage_increment_default );
}
if( !was_loaded || jo.has_member( "max_damage" ) ) {
max_damage = get_dbl_or_var( jo, "max_damage", false, max_damage_default );
}
if( !was_loaded || jo.has_member( "min_range" ) ) {
min_range = get_dbl_or_var( jo, "min_range", false, min_range_default );
}
if( !was_loaded || jo.has_member( "range_increment" ) ) {
range_increment = get_dbl_or_var( jo, "range_increment", false, range_increment_default );
}
if( !was_loaded || jo.has_member( "max_range" ) ) {
max_range = get_dbl_or_var( jo, "max_range", false, max_range_default );
}
if( !was_loaded || jo.has_member( "min_aoe" ) ) {
min_aoe = get_dbl_or_var( jo, "min_aoe", false, min_aoe_default );
}
if( !was_loaded || jo.has_member( "aoe_increment" ) ) {
aoe_increment = get_dbl_or_var( jo, "aoe_increment", false, aoe_increment_default );
}
if( !was_loaded || jo.has_member( "max_aoe" ) ) {
max_aoe = get_dbl_or_var( jo, "max_aoe", false, max_aoe_default );
}
if( !was_loaded || jo.has_member( "min_dot" ) ) {
min_dot = get_dbl_or_var( jo, "min_dot", false, min_dot_default );
}
if( !was_loaded || jo.has_member( "dot_increment" ) ) {
dot_increment = get_dbl_or_var( jo, "dot_increment", false, dot_increment_default );
}
if( !was_loaded || jo.has_member( "max_dot" ) ) {
max_dot = get_dbl_or_var( jo, "max_dot", false, max_dot_default );
}
if( !was_loaded || jo.has_member( "min_duration" ) ) {
min_duration = get_dbl_or_var( jo, "min_duration", false, min_duration_default );
}
if( !was_loaded || jo.has_member( "duration_increment" ) ) {
duration_increment = get_dbl_or_var( jo, "duration_increment", false,
duration_increment_default );
}
if( !was_loaded || jo.has_member( "max_duration" ) ) {
max_duration = get_dbl_or_var( jo, "max_duration", false, max_duration_default );
}
if( !was_loaded || jo.has_member( "min_pierce" ) ) {
min_pierce = get_dbl_or_var( jo, "min_pierce", false, min_pierce_default );
}
if( !was_loaded || jo.has_member( "pierce_increment" ) ) {
pierce_increment = get_dbl_or_var( jo, "pierce_increment", false,
pierce_increment_default );
}
if( !was_loaded || jo.has_member( "max_pierce" ) ) {
max_pierce = get_dbl_or_var( jo, "max_pierce", false, max_pierce_default );
}
if( !was_loaded || jo.has_member( "min_bash_scaling" ) ) {
min_bash_scaling = get_dbl_or_var( jo, "min_bash_scaling", false, min_bash_scaling_default );
}
if( !was_loaded || jo.has_member( "bash_scaling_increment" ) ) {
bash_scaling_increment = get_dbl_or_var( jo, "bash_scaling_increment", false,
bash_scaling_increment_default );
}
if( !was_loaded || jo.has_member( "max_bash_scaling" ) ) {
max_bash_scaling = get_dbl_or_var( jo, "max_bash_scaling", false, max_bash_scaling_default );
}
if( !was_loaded || jo.has_member( "base_energy_cost" ) ) {
base_energy_cost = get_dbl_or_var( jo, "base_energy_cost", false,
base_energy_cost_default );
}
if( jo.has_member( "final_energy_cost" ) ) {
final_energy_cost = get_dbl_or_var( jo, "final_energy_cost" );
} else if( !was_loaded ) {
final_energy_cost = base_energy_cost;
}
if( !was_loaded || jo.has_member( "energy_increment" ) ) {
energy_increment = get_dbl_or_var( jo, "energy_increment", false,
energy_increment_default );
}
optional( jo, was_loaded, "spell_class", spell_class, spell_class_default );
optional( jo, was_loaded, "energy_source", energy_source, energy_source_default );
optional( jo, was_loaded, "damage_type", dmg_type, dmg_type_default );
if( !was_loaded || jo.has_member( "difficulty" ) ) {
difficulty = get_dbl_or_var( jo, "difficulty", false, difficulty_default );
}
if( !was_loaded || jo.has_member( "multiple_projectiles" ) ) {
multiple_projectiles = get_dbl_or_var( jo, "multiple_projectiles", false,
multiple_projectiles_default );
}
if( !was_loaded || jo.has_member( "max_level" ) ) {
max_level = get_dbl_or_var( jo, "max_level", false, max_level_default );
}
if( !was_loaded || jo.has_member( "base_casting_time" ) ) {
base_casting_time = get_dbl_or_var( jo, "base_casting_time", false,
base_casting_time_default );
}
if( jo.has_member( "final_casting_time" ) ) {
final_casting_time = get_dbl_or_var( jo, "final_casting_time" );
} else if( !was_loaded ) {
final_casting_time = base_casting_time;
}
if( !was_loaded || jo.has_member( "max_damage" ) ) {
max_damage = get_dbl_or_var( jo, "max_damage", false, max_damage_default );
}
if( !was_loaded || jo.has_member( "casting_time_increment" ) ) {
casting_time_increment = get_dbl_or_var( jo, "casting_time_increment", false,
casting_time_increment_default );
}
for( const JsonMember member : jo.get_object( "learn_spells" ) ) {
learn_spells.insert( std::pair<std::string, int>( member.name(), member.get_int() ) );
}
}
void spell_type::serialize( JsonOut &json ) const
{
json.start_object();
json.member( "type", "SPELL" );
json.member( "id", id );
json.member( "src_mod", src_mod );
json.member( "name", name.translated() );
json.member( "description", description.translated() );
json.member( "effect", effect_name );
json.member( "shape", io::enum_to_string( spell_area ) );
json.member( "valid_targets", valid_targets, enum_bitset<spell_target> {} );
json.member( "effect_str", effect_str, effect_str_default );
json.member( "skill", skill, skill_default );
json.member( "teachable", teachable, true );
json.member( "components", spell_components, spell_components_default );
json.member( "message", message.translated(), message_default.translated() );
json.member( "sound_description", sound_description.translated(),
sound_description_default.translated() );
json.member( "sound_type", io::enum_to_string( sound_type ),
io::enum_to_string( sound_type_default ) );
json.member( "sound_ambient", sound_ambient, sound_ambient_default );
json.member( "sound_id", sound_id, sound_id_default );
json.member( "sound_variant", sound_variant, sound_variant_default );
json.member( "targeted_monster_ids", targeted_monster_ids, std::set<mtype_id> {} );
json.member( "targeted_monster_species", targeted_species_ids, std::set<species_id> {} );
json.member( "ignored_monster_species", ignored_species_ids, std::set<species_id> {} );
json.member( "extra_effects", additional_spells, std::vector<fake_spell> {} );
if( !affected_bps.none() ) {
json.member( "affected_body_parts", affected_bps );
}
json.member( "flags", flags, std::set<std::string> {} );
if( field ) {
json.member( "field_id", field->id().str() );
json.member( "field_chance", static_cast<int>( field_chance.min.dbl_val.value() ),
field_chance_default );
json.member( "max_field_intensity", static_cast<int>( max_field_intensity.min.dbl_val.value() ),
max_field_intensity_default );
json.member( "min_field_intensity", static_cast<int>( min_field_intensity.min.dbl_val.value() ),
min_field_intensity_default );
json.member( "field_intensity_increment",
static_cast<float>( field_intensity_increment.min.dbl_val.value() ),
field_intensity_increment_default );
json.member( "field_intensity_variance",
static_cast<float>( field_intensity_variance.min.dbl_val.value() ),
field_intensity_variance_default );
}
json.member( "min_damage", static_cast<int>( min_damage.min.dbl_val.value() ), min_damage_default );
json.member( "max_damage", static_cast<int>( max_damage.min.dbl_val.value() ), max_damage_default );
json.member( "damage_increment", static_cast<float>( damage_increment.min.dbl_val.value() ),
damage_increment_default );
json.member( "min_accuracy", static_cast<int>( min_accuracy.min.dbl_val.value() ),
min_accuracy_default );
json.member( "accuracy_increment", static_cast<float>( accuracy_increment.min.dbl_val.value() ),
accuracy_increment_default );
json.member( "max_accuracy", static_cast<int>( max_accuracy.min.dbl_val.value() ),
max_accuracy_default );
json.member( "min_range", static_cast<int>( min_range.min.dbl_val.value() ), min_range_default );
json.member( "max_range", static_cast<int>( max_range.min.dbl_val.value() ), min_range_default );
json.member( "range_increment", static_cast<float>( range_increment.min.dbl_val.value() ),
range_increment_default );
json.member( "min_aoe", static_cast<int>( min_aoe.min.dbl_val.value() ), min_aoe_default );
json.member( "max_aoe", static_cast<int>( max_aoe.min.dbl_val.value() ), max_aoe_default );
json.member( "aoe_increment", static_cast<float>( aoe_increment.min.dbl_val.value() ),
aoe_increment_default );
json.member( "min_dot", static_cast<int>( min_dot.min.dbl_val.value() ), min_dot_default );
json.member( "max_dot", static_cast<int>( max_dot.min.dbl_val.value() ), max_dot_default );
json.member( "dot_increment", static_cast<float>( dot_increment.min.dbl_val.value() ),
dot_increment_default );
json.member( "min_duration", static_cast<int>( min_duration.min.dbl_val.value() ),
min_duration_default );
json.member( "max_duration", static_cast<int>( max_duration.min.dbl_val.value() ),
max_duration_default );
json.member( "duration_increment", static_cast<int>( duration_increment.min.dbl_val.value() ),
duration_increment_default );
json.member( "min_pierce", static_cast<int>( min_pierce.min.dbl_val.value() ), min_pierce_default );
json.member( "max_pierce", static_cast<int>( max_pierce.min.dbl_val.value() ), max_pierce_default );
json.member( "pierce_increment", static_cast<float>( pierce_increment.min.dbl_val.value() ),
pierce_increment_default );
json.member( "min_bash_scaling", static_cast<float>( min_bash_scaling.min.dbl_val.value() ),
min_bash_scaling_default );
json.member( "max_bash_scaling", static_cast<float>( max_bash_scaling.min.dbl_val.value() ),
max_bash_scaling_default );
json.member( "bash_scaling_increment",
static_cast<float>( bash_scaling_increment.min.dbl_val.value() ), bash_scaling_increment_default );
json.member( "base_energy_cost", static_cast<int>( base_energy_cost.min.dbl_val.value() ),
base_energy_cost_default );
json.member( "final_energy_cost", static_cast<int>( final_energy_cost.min.dbl_val.value() ),
static_cast<int>( base_energy_cost.min.dbl_val.value() ) );
json.member( "energy_increment", static_cast<float>( energy_increment.min.dbl_val.value() ),
energy_increment_default );
json.member( "spell_class", spell_class, spell_class_default );
json.member( "energy_source", io::enum_to_string( energy_source ),
io::enum_to_string( energy_source_default ) );
json.member( "damage_type", dmg_type, dmg_type_default );
json.member( "difficulty", static_cast<int>( difficulty.min.dbl_val.value() ), difficulty_default );
json.member( "multiple_projectiles", static_cast<int>( multiple_projectiles.min.dbl_val.value() ),
multiple_projectiles_default );
json.member( "max_level", static_cast<int>( max_level.min.dbl_val.value() ), max_level_default );
json.member( "base_casting_time", static_cast<int>( base_casting_time.min.dbl_val.value() ),
base_casting_time_default );
json.member( "final_casting_time", static_cast<int>( final_casting_time.min.dbl_val.value() ),
static_cast<int>( base_casting_time.min.dbl_val.value() ) );
json.member( "casting_time_increment",
static_cast<float>( casting_time_increment.min.dbl_val.value() ), casting_time_increment_default );
if( !learn_spells.empty() ) {
json.member( "learn_spells" );
json.start_object();
for( const std::pair<const std::string, int> &sp : learn_spells ) {
json.member( sp.first, sp.second );
}
json.end_object();
}
json.end_object();
}
static bool spell_infinite_loop_check( std::set<spell_id> spell_effects, const spell_id &sp )
{
if( spell_effects.count( sp ) ) {
return true;
}
spell_effects.emplace( sp );
std::set<spell_id> unique_spell_list;
for( const fake_spell &fake_sp : sp->additional_spells ) {
unique_spell_list.emplace( fake_sp.id );
}
for( const spell_id &other_sp : unique_spell_list ) {
if( spell_infinite_loop_check( spell_effects, other_sp ) ) {
return true;
}
}
return false;
}
void spell_type::check_consistency()
{
for( const spell_type &sp_t : get_all() ) {
if( sp_t.effect_name == "summon_vehicle" ) {
if( !sp_t.effect_str.empty() && !vproto_id( sp_t.effect_str ).is_valid() ) {
debugmsg( "ERROR %s specifies a vehicle to summon, but vehicle %s is not valid", sp_t.id.c_str(),
sp_t.effect_str );
}
}
std::set<spell_id> spell_effect_list;
if( spell_infinite_loop_check( spell_effect_list, sp_t.id ) ) {
debugmsg( "ERROR: %s has infinite loop in extra_effects", sp_t.id.c_str() );
}
if( sp_t.spell_tags[spell_flag::WONDER] && sp_t.additional_spells.empty() ) {
debugmsg( "ERROR: %s has WONDER flag but no spells to choose from!", sp_t.id.c_str() );
}
}
}
const std::vector<spell_type> &spell_type::get_all()
{
return spell_factory.get_all();
}
void spell_type::reset_all()
{
spell_factory.reset();
}
bool spell_type::is_valid() const
{
return spell_factory.is_valid( this->id );
}
// spell
spell::spell( spell_id sp, int xp, int level_adjustment ) :
type( sp ),
experience( xp ),
temp_level_adjustment( level_adjustment )
{}
void spell::set_message( const translation &msg )
{
alt_message = msg;
}
spell_id spell::id() const
{
return type;
}
trait_id spell::spell_class() const
{
return type->spell_class;
}
skill_id spell::skill() const
{
return type->skill;
}
int spell::field_intensity( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
return std::min( static_cast<int>( type->max_field_intensity.evaluate( d ) ),
static_cast<int>( type->min_field_intensity.evaluate( d ) + std::round( get_effective_level() *
type->field_intensity_increment.evaluate( d ) ) ) );
}
double spell::bash_scaling( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const double leveled_scaling = type->min_bash_scaling.evaluate( d ) + get_effective_level() *
type->bash_scaling_increment.evaluate( d );
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
return rng( std::min( leveled_scaling,
static_cast<double>( type->max_bash_scaling.evaluate( d ) ) ),
std::max( leveled_scaling,
static_cast<double>( type->max_bash_scaling.evaluate( d ) ) ) );
} else {
if( type->max_bash_scaling.evaluate( d ) >= type->min_bash_scaling.evaluate( d ) ) {
return std::min( leveled_scaling, static_cast<double>( type->max_bash_scaling.evaluate( d ) ) );
} else {
return std::max( leveled_scaling, static_cast<double>( type->max_bash_scaling.evaluate( d ) ) );
}
}
}
int spell::min_leveled_damage( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
return type->min_damage.evaluate( d ) + std::round( get_effective_level() *
type->damage_increment.evaluate(
d ) );
}
float spell::dps( const Character &caster, const Creature & ) const
{
if( type->effect_name != "attack" ) {
return 0.0f;
}
const float time_modifier = 100.0f / casting_time( caster );
const float failure_modifier = 1.0f - spell_fail( caster );
const float raw_dps = damage( caster ) + damage_dot( caster ) * duration_turns( caster ) / 1_turns;
// TODO: calculate true dps with armor and resistances and any caster bonuses
return raw_dps * time_modifier * failure_modifier;
}
int spell::damage( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const int leveled_damage = min_leveled_damage( caster );
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
return rng( std::min( leveled_damage, static_cast<int>( type->max_damage.evaluate( d ) ) ),
std::max( leveled_damage,
static_cast<int>( type->max_damage.evaluate( d ) ) ) ) * temp_damage_multiplyer;
} else {
if( type->min_damage.evaluate( d ) >= 0 ||
type->max_damage.evaluate( d ) >= type->min_damage.evaluate( d ) ) {
return std::min( leveled_damage,
static_cast<int>( type->max_damage.evaluate( d ) ) ) * temp_damage_multiplyer;
} else { // if it's negative, min and max work differently
return std::max( leveled_damage,
static_cast<int>( type->max_damage.evaluate( d ) ) ) * temp_damage_multiplyer;
}
}
}
int spell::min_leveled_accuracy( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
return type->min_accuracy.evaluate( d ) + std::round( get_effective_level() *
type->accuracy_increment.evaluate( d ) );
}
int spell::accuracy( Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const int leveled_accuracy = min_leveled_accuracy( caster );
if( type->min_accuracy.evaluate( d ) >= 0 ||
type->max_accuracy.evaluate( d ) >= type->min_accuracy.evaluate( d ) ) {
return std::min( leveled_accuracy, static_cast<int>( type->max_accuracy.evaluate( d ) ) );
} else { // if it's negative, min and max work differently
return std::max( leveled_accuracy, static_cast<int>( type->max_accuracy.evaluate( d ) ) );
}
}
double spell::min_leveled_dot( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
return type->min_dot.evaluate( d ) + std::round( get_effective_level() *
type->dot_increment.evaluate( d ) );
}
double spell::damage_dot( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const double leveled_dot = min_leveled_dot( caster );
if( type->min_dot.evaluate( d ) >= 0.0 ||
type->max_dot.evaluate( d ) >= type->min_dot.evaluate( d ) ) {
return std::min( leveled_dot, type->max_dot.evaluate( d ) );
} else { // if it's negative, min and max work differently
return std::max( leveled_dot, type->max_dot.evaluate( d ) );
}
}
damage_over_time_data spell::damage_over_time( const std::vector<bodypart_id> &bps,
const Creature &caster ) const
{
damage_over_time_data temp;
temp.bps = bps;
temp.duration = duration_turns( caster );
temp.amount = damage_dot( caster );
temp.type = dmg_type();
return temp;
}
std::string spell::damage_string( const Character &caster ) const
{
std::string damage_string;
const_dialogue d( get_const_talker_for( caster ), nullptr );
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
damage_string = string_format( "%d-%d", min_leveled_damage( caster ),
static_cast<int>( type->max_damage.evaluate( d ) ) );
} else {
const int dmg = damage( caster );
if( dmg >= 0 ) {
damage_string = string_format( "%d", dmg );
} else {
damage_string = string_format( "+%d", std::abs( dmg ) );
}
}
if( has_flag( spell_flag::PERCENTAGE_DAMAGE ) ) {
damage_string = string_format( "%s%% %s", damage_string, _( "of current HP" ) );
}
return damage_string;
}
std::optional<tripoint_bub_ms> spell::select_target( Creature *source )
{
tripoint_bub_ms target = source->pos_bub();
bool target_is_valid = false;
if( range( *source ) > 0 && !is_valid_target( spell_target::none ) &&
!has_flag( spell_flag::RANDOM_TARGET ) ) {
if( source->is_avatar() ) {
do {
avatar &source_avatar = *source->as_avatar();
std::vector<tripoint_bub_ms> trajectory = target_handler::mode_spell( source_avatar, *this, true,
true );
if( !trajectory.empty() ) {
target = trajectory.back();
target_is_valid = is_valid_target( source_avatar, target );
if( !( is_valid_target( spell_target::ground ) || source_avatar.sees( target ) ) ) {
target_is_valid = false;
}
} else {
target_is_valid = false;
}
if( !target_is_valid ) {
if( query_yn( _( "Stop targeting? Time spent will be lost." ) ) ) {
return std::nullopt;
}
}
} while( !target_is_valid );
} else if( source->is_npc() ) {
npc &source_npc = *source->as_npc();
npc_attack_spell npc_spell( id() );
// recalculate effectiveness because it's been a few turns since the npc started casting.
const npc_attack_rating effectiveness = npc_spell.evaluate( source_npc,
source_npc.last_target.lock().get() );
if( effectiveness < 0 ) {
add_msg_debug( debugmode::debug_filter::DF_NPC, "%s cancels casting %s, target lost",
source_npc.disp_name(), name() );
return std::nullopt;
} else {
target = effectiveness.target();
}
} // TODO: move monster spell attack targeting here
} else if( has_flag( spell_flag::RANDOM_TARGET ) ) {
const std::optional<tripoint_bub_ms> target_ = random_valid_target( *source, source->pos_bub() );
if( !target_ ) {
if( !type->description.empty() ) {
source->add_msg_if_player( game_message_params{ m_bad, gmf_bypass_cooldown },
_( "You can't find a suitable target." ) );
}
return std::nullopt;
}
target = *target_;
}
return target;
}
int spell::min_leveled_aoe( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
return type->min_aoe.evaluate( d ) + std::round( get_effective_level() *
type->aoe_increment.evaluate( d ) );
}
int spell::aoe( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const int leveled_aoe = min_leveled_aoe( caster );
int return_value;
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return_value = rng( std::min( leveled_aoe, static_cast<int>( type->max_aoe.evaluate( d ) ) ),
std::max( leveled_aoe, static_cast<int>( type->max_aoe.evaluate( d ) ) ) );
} else {
if( type->max_aoe.evaluate( d ) >= type->min_aoe.evaluate( d ) ) {
return_value = std::min( leveled_aoe, static_cast<int>( type->max_aoe.evaluate( d ) ) );
} else {
return_value = std::max( leveled_aoe, static_cast<int>( type->max_aoe.evaluate( d ) ) );
}
}
return return_value * temp_aoe_multiplyer;
}
std::set<tripoint_bub_ms> spell::effect_area( const spell_effect::override_parameters ¶ms,
const tripoint_bub_ms &source, const tripoint_bub_ms &target ) const
{
return type->spell_area_function( params, source, target );
}
std::set<tripoint_bub_ms> spell::effect_area( const tripoint_bub_ms &source,
const tripoint_bub_ms &target, const Creature &caster ) const
{
return effect_area( spell_effect::override_parameters( *this, caster ), source, target );
}
bool spell::in_aoe( const tripoint_bub_ms &source, const tripoint_bub_ms &target,
const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return rl_dist( source, target ) <= type->max_aoe.evaluate( d );
} else {
return rl_dist( source, target ) <= aoe( caster );
}
}
std::string spell::aoe_string( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return string_format( "%d-%d", min_leveled_aoe( caster ), type->max_aoe.evaluate( d ) );
} else {
return string_format( "%d", aoe( caster ) );
}
}
int spell::range( const Creature &caster ) const
{
const_dialogue d( get_const_talker_for( caster ), nullptr );
const int leveled_range = type->min_range.evaluate( d ) + std::round( get_effective_level() *
type->range_increment.evaluate( d ) );
float range;
if( type->max_range.evaluate( d ) >= type->min_range.evaluate( d ) ) {
range = std::min( leveled_range, static_cast<int>( type->max_range.evaluate( d ) ) );
} else {
range = std::max( leveled_range, static_cast<int>( type->max_range.evaluate( d ) ) );
}
return std::max( range * temp_range_multiplyer, 0.0f );
}
std::vector<tripoint_bub_ms> spell::targetable_locations( const Character &source ) const
{
const tripoint_bub_ms char_pos = source.pos_bub();
const bool select_ground = is_valid_target( spell_target::ground );
const bool ignore_walls = has_flag( spell_flag::NO_PROJECTILE );
map &here = get_map();
// TODO: put this in a namespace for reuse
const auto has_obstruction = [&]( const tripoint_bub_ms & at ) {
for( const tripoint_bub_ms &line_point : line_to( char_pos, at ) ) {
if( here.impassable( line_point ) ) {