forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.cpp
2551 lines (2293 loc) · 93.6 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 <memory>
#include <set>
#include <tuple>
#include <utility>
#include "avatar.h"
#include "calendar.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "color.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 "flat_set.h"
#include "generic_factory.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "json.h"
#include "line.h"
#include "make_static.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 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 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::PERCENTAGE_DAMAGE: return "PERCENTAGE_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::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::WITH_CONTAINER: return "WITH_CONTAINER";
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::NON_MAGICAL: return "NON_MAGICAL";
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 cata::optional<int> fake_spell::max_level_default = cata::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 cata::optional<field_type_id> spell_type::field_default = cata::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 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 spell_type::dmg_type_default = damage_type::NONE;
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 & )
{
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "description", description );
optional( jo, was_loaded, "skill", skill, skill_default );
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 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 );
const auto flag_reader = enum_flags_reader<spell_flag> { "flags" };
optional( jo, was_loaded, "flags", spell_tags, flag_reader );
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 );
}
optional( jo, was_loaded, "field_chance", field_chance, field_chance_default );
optional( jo, was_loaded, "min_field_intensity", min_field_intensity, min_field_intensity_default );
optional( jo, was_loaded, "max_field_intensity", max_field_intensity, max_field_intensity_default );
optional( jo, was_loaded, "field_intensity_increment", field_intensity_increment,
field_intensity_increment_default );
optional( jo, was_loaded, "field_intensity_variance", field_intensity_variance,
field_intensity_variance_default );
optional( jo, was_loaded, "min_accuracy", min_accuracy, min_accuracy_default );
optional( jo, was_loaded, "accuracy_increment", accuracy_increment, accuracy_increment_default );
optional( jo, was_loaded, "max_accuracy", max_accuracy, max_accuracy_default );
optional( jo, was_loaded, "min_damage", min_damage, min_damage_default );
optional( jo, was_loaded, "damage_increment", damage_increment, damage_increment_default );
optional( jo, was_loaded, "max_damage", max_damage, max_damage_default );
optional( jo, was_loaded, "min_range", min_range, min_range_default );
optional( jo, was_loaded, "range_increment", range_increment, range_increment_default );
optional( jo, was_loaded, "max_range", max_range, max_range_default );
optional( jo, was_loaded, "min_aoe", min_aoe, min_aoe_default );
optional( jo, was_loaded, "aoe_increment", aoe_increment, aoe_increment_default );
optional( jo, was_loaded, "max_aoe", max_aoe, max_aoe_default );
optional( jo, was_loaded, "min_dot", min_dot, min_dot_default );
optional( jo, was_loaded, "dot_increment", dot_increment, dot_increment_default );
optional( jo, was_loaded, "max_dot", max_dot, max_dot_default );
optional( jo, was_loaded, "min_duration", min_duration, min_duration_default );
optional( jo, was_loaded, "duration_increment", duration_increment, duration_increment_default );
optional( jo, was_loaded, "max_duration", max_duration, max_duration_default );
optional( jo, was_loaded, "min_pierce", min_pierce, min_pierce_default );
optional( jo, was_loaded, "pierce_increment", pierce_increment, pierce_increment_default );
optional( jo, was_loaded, "max_pierce", max_pierce, max_pierce_default );
optional( jo, was_loaded, "base_energy_cost", base_energy_cost, base_energy_cost_default );
optional( jo, was_loaded, "final_energy_cost", final_energy_cost, base_energy_cost );
optional( jo, was_loaded, "energy_increment", energy_increment, 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 );
optional( jo, was_loaded, "difficulty", difficulty, difficulty_default );
optional( jo, was_loaded, "max_level", max_level, max_level_default );
optional( jo, was_loaded, "base_casting_time", base_casting_time, base_casting_time_default );
optional( jo, was_loaded, "final_casting_time", final_casting_time, base_casting_time );
optional( jo, was_loaded, "casting_time_increment", casting_time_increment,
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( "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( "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( "extra_effects", additional_spells, std::vector<fake_spell> {} );
if( !affected_bps.none() ) {
json.member( "affected_body_parts", affected_bps );
}
json.member( "flags", spell_tags, enum_bitset<spell_flag> {} );
if( field ) {
json.member( "field_id", field->id().str() );
json.member( "field_chance", field_chance, field_chance_default );
json.member( "max_field_intensity", max_field_intensity, max_field_intensity_default );
json.member( "min_field_intensity", min_field_intensity, min_field_intensity_default );
json.member( "field_intensity_increment", field_intensity_increment,
field_intensity_increment_default );
json.member( "field_intensity_variance", field_intensity_variance,
field_intensity_variance_default );
}
json.member( "min_damage", min_damage, min_damage_default );
json.member( "max_damage", max_damage, max_damage_default );
json.member( "damage_increment", damage_increment, damage_increment_default );
json.member( "min_accuracy", min_accuracy, min_accuracy_default );
json.member( "accuracy_increment", accuracy_increment, accuracy_increment_default );
json.member( "max_accuracy", max_accuracy, max_accuracy_default );
json.member( "min_range", min_range, min_range_default );
json.member( "max_range", max_range, min_range_default );
json.member( "range_increment", range_increment, range_increment_default );
json.member( "min_aoe", min_aoe, min_aoe_default );
json.member( "max_aoe", max_aoe, max_aoe_default );
json.member( "aoe_increment", aoe_increment, aoe_increment_default );
json.member( "min_dot", min_dot, min_dot_default );
json.member( "max_dot", max_dot, max_dot_default );
json.member( "dot_increment", dot_increment, dot_increment_default );
json.member( "min_duration", min_duration, min_duration_default );
json.member( "max_duration", max_duration, max_duration_default );
json.member( "duration_increment", duration_increment, duration_increment_default );
json.member( "min_pierce", min_pierce, min_pierce_default );
json.member( "max_pierce", max_pierce, max_pierce_default );
json.member( "pierce_increment", pierce_increment, pierce_increment_default );
json.member( "base_energy_cost", base_energy_cost, base_energy_cost_default );
json.member( "final_energy_cost", final_energy_cost, base_energy_cost );
json.member( "energy_increment", energy_increment, 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", io::enum_to_string( dmg_type ),
io::enum_to_string( dmg_type_default ) );
json.member( "difficulty", difficulty, difficulty_default );
json.member( "max_level", max_level, max_level_default );
json.member( "base_casting_time", base_casting_time, base_casting_time_default );
json.member( "final_casting_time", final_casting_time, base_casting_time );
json.member( "casting_time_increment", casting_time_increment, 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.min_aoe > sp_t.max_aoe && sp_t.aoe_increment > 0 ) ||
( sp_t.min_aoe < sp_t.max_aoe && sp_t.aoe_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_aoe than max_aoe", sp_t.id.c_str() );
}
if( ( sp_t.min_damage > sp_t.max_damage && sp_t.damage_increment > 0 ) ||
( sp_t.min_damage < sp_t.max_damage && sp_t.damage_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_damage than max_damage", sp_t.id.c_str() );
}
if( ( sp_t.min_range > sp_t.max_range && sp_t.range_increment > 0 ) ||
( sp_t.min_range < sp_t.max_range && sp_t.range_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_range than max_range", sp_t.id.c_str() );
}
if( ( sp_t.min_dot > sp_t.max_dot && sp_t.dot_increment > 0 ) ||
( sp_t.min_dot < sp_t.max_dot && sp_t.dot_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_dot than max_dot", sp_t.id.c_str() );
}
if( ( sp_t.min_duration > sp_t.max_duration && sp_t.duration_increment > 0 ) ||
( sp_t.min_duration < sp_t.max_duration && sp_t.duration_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_duration than max_duration", sp_t.id.c_str() );
}
if( ( sp_t.min_pierce > sp_t.max_pierce && sp_t.pierce_increment > 0 ) ||
( sp_t.min_pierce < sp_t.max_pierce && sp_t.pierce_increment < 0 ) ) {
debugmsg( "ERROR: %s has higher min_pierce than max_pierce", sp_t.id.c_str() );
}
if( sp_t.casting_time_increment < 0.0f && sp_t.base_casting_time < sp_t.final_casting_time ) {
debugmsg( "ERROR: %s has negative increment and base_casting_time < final_casting_time",
sp_t.id.c_str() );
}
if( sp_t.casting_time_increment > 0.0f && sp_t.base_casting_time > sp_t.final_casting_time ) {
debugmsg( "ERROR: %s has positive increment and base_casting_time > final_casting_time",
sp_t.id.c_str() );
}
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.field ) {
if( sp_t.field_chance <= 0 ) {
debugmsg( "ERROR: %s must have a positive field chance.", sp_t.id.c_str() );
}
if( sp_t.field_intensity_increment > 0 && sp_t.max_field_intensity < sp_t.min_field_intensity ) {
debugmsg( "ERROR: max_field_intensity must be greater than min_field_intensity with positive increment: %s",
sp_t.id.c_str() );
} else if( sp_t.field_intensity_increment < 0 &&
sp_t.max_field_intensity > sp_t.min_field_intensity ) {
debugmsg( "ERROR: min_field_intensity must be greater than max_field_intensity with negative increment: %s",
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 ) :
type( sp ),
experience( xp )
{}
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
{
return std::min( type->max_field_intensity,
static_cast<int>( type->min_field_intensity + std::round( get_level() *
type->field_intensity_increment ) ) );
}
int spell::min_leveled_damage() const
{
return type->min_damage + std::round( get_level() * type->damage_increment );
}
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() + damage_dot() * duration_turns() / 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
{
const int leveled_damage = min_leveled_damage();
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
return rng( std::min( leveled_damage, type->max_damage ), std::max( leveled_damage,
type->max_damage ) );
} else {
if( type->min_damage >= 0 || type->max_damage >= type->min_damage ) {
return std::min( leveled_damage, type->max_damage );
} else { // if it's negative, min and max work differently
return std::max( leveled_damage, type->max_damage );
}
}
}
int spell::min_leveled_accuracy() const
{
return type->min_accuracy + std::round( get_level() * type->accuracy_increment );
}
int spell::accuracy() const
{
const int leveled_accuracy = min_leveled_accuracy();
if( type->min_accuracy >= 0 || type->max_accuracy >= type->min_accuracy ) {
return std::min( leveled_accuracy, type->max_accuracy );
} else { // if it's negative, min and max work differently
return std::max( leveled_accuracy, type->max_accuracy );
}
}
int spell::min_leveled_dot() const
{
return type->min_dot + std::round( get_level() * type->dot_increment );
}
int spell::damage_dot() const
{
const int leveled_dot = min_leveled_dot();
if( type->min_dot >= 0 || type->max_dot >= type->min_dot ) {
return std::min( leveled_dot, type->max_dot );
} else { // if it's negative, min and max work differently
return std::max( leveled_dot, type->max_dot );
}
}
damage_over_time_data spell::damage_over_time( const std::vector<bodypart_str_id> &bps ) const
{
damage_over_time_data temp;
temp.bps = bps;
temp.duration = duration_turns();
temp.amount = damage_dot();
temp.type = dmg_type();
return temp;
}
std::string spell::damage_string() const
{
std::string damage_string;
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
damage_string = string_format( "%d-%d", min_leveled_damage(), type->max_damage );
} else {
const int dmg = damage();
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;
}
cata::optional<tripoint> spell::select_target( Creature *source )
{
tripoint target = source->pos();
bool target_is_valid = false;
if( range() > 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> 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 cata::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 cata::nullopt;
} else {
target = effectiveness.target();
}
} // TODO: move monster spell attack targeting here
} else if( has_flag( spell_flag::RANDOM_TARGET ) ) {
const cata::optional<tripoint> target_ = random_valid_target( *source, source->pos() );
if( !target_ ) {
source->add_msg_if_player( game_message_params{ m_bad, gmf_bypass_cooldown },
_( "You can't find a suitable target." ) );
return cata::nullopt;
}
target = *target_;
}
return target;
}
int spell::min_leveled_aoe() const
{
return type->min_aoe + std::round( get_level() * type->aoe_increment );
}
int spell::aoe() const
{
const int leveled_aoe = min_leveled_aoe();
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return rng( std::min( leveled_aoe, type->max_aoe ), std::max( leveled_aoe, type->max_aoe ) );
} else {
if( type->max_aoe >= type->min_aoe ) {
return std::min( leveled_aoe, type->max_aoe );
} else {
return std::max( leveled_aoe, type->max_aoe );
}
}
}
std::set<tripoint> spell::effect_area( const spell_effect::override_parameters ¶ms,
const tripoint &source, const tripoint &target ) const
{
return type->spell_area_function( params, source, target );
}
std::set<tripoint> spell::effect_area( const tripoint &source, const tripoint &target ) const
{
return effect_area( spell_effect::override_parameters( *this ), source, target );
}
bool spell::in_aoe( const tripoint &source, const tripoint &target ) const
{
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return rl_dist( source, target ) <= type->max_aoe;
} else {
return rl_dist( source, target ) <= aoe();
}
}
std::string spell::aoe_string() const
{
if( has_flag( spell_flag::RANDOM_AOE ) ) {
return string_format( "%d-%d", min_leveled_aoe(), type->max_aoe );
} else {
return string_format( "%d", aoe() );
}
}
int spell::range() const
{
const int leveled_range = type->min_range + std::round( get_level() * type->range_increment );
if( type->max_range >= type->min_range ) {
return std::min( leveled_range, type->max_range );
} else {
return std::max( leveled_range, type->max_range );
}
}
std::vector<tripoint> spell::targetable_locations( const Character &source ) const
{
const tripoint char_pos = source.pos();
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 & at ) {
for( const tripoint &line_point : line_to( char_pos, at ) ) {
if( here.impassable( line_point ) ) {
return true;
}
}
return false;
};
std::vector<tripoint> selectable_targets;
for( const tripoint &query : here.points_in_radius( char_pos, range() ) ) {
if( !ignore_walls && has_obstruction( query ) ) {
// it's blocked somewhere!
continue;
}
if( !select_ground ) {
if( !source.sees( query ) ) {
// can't target a critter you can't see
continue;
}
}
if( is_valid_target( source, query ) ) {
selectable_targets.push_back( query );
}
}
return selectable_targets;
}
int spell::min_leveled_duration() const
{
return type->min_duration + std::round( get_level() * type->duration_increment );
}
int spell::duration() const
{
const int leveled_duration = min_leveled_duration();
if( has_flag( spell_flag::RANDOM_DURATION ) ) {
return rng( std::min( leveled_duration, type->max_duration ), std::max( leveled_duration,
type->max_duration ) );
} else {
if( type->max_duration >= type->min_duration ) {
return std::min( leveled_duration, type->max_duration );
} else {
return std::max( leveled_duration, type->max_duration );
}
}
}
std::string spell::duration_string() const
{
if( has_flag( spell_flag::RANDOM_DURATION ) ) {
return string_format( "%s - %s", moves_to_string( min_leveled_duration() ),
moves_to_string( type->max_duration ) );
} else if( has_flag( spell_flag::PERMANENT ) && ( is_max_level() || effect() == "summon" ) ) {
return _( "Permanent" );
} else {
return moves_to_string( duration() );
}
}
time_duration spell::duration_turns() const
{
return time_duration::from_moves( duration() );
}
void spell::gain_level()
{
gain_exp( exp_to_next_level() );
}
void spell::gain_levels( int gains )
{
if( gains < 1 ) {
return;
}
for( int gained = 0; gained < gains && !is_max_level(); gained++ ) {
gain_level();
}
}
void spell::set_level( int nlevel )
{
experience = 0;
gain_levels( nlevel );
}
bool spell::is_max_level() const
{
return get_level() >= type->max_level;
}
bool spell::can_learn( const Character &guy ) const
{
if( type->spell_class == trait_NONE ) {
return true;
}
return guy.has_trait( type->spell_class );
}
int spell::energy_cost( const Character &guy ) const
{
int cost;
if( type->base_energy_cost < type->final_energy_cost ) {
cost = std::min( type->final_energy_cost,
static_cast<int>( std::round( type->base_energy_cost + type->energy_increment * get_level() ) ) );
} else if( type->base_energy_cost > type->final_energy_cost ) {
cost = std::max( type->final_energy_cost,
static_cast<int>( std::round( type->base_energy_cost + type->energy_increment * get_level() ) ) );
} else {
cost = type->base_energy_cost;
}
if( !has_flag( spell_flag::NO_HANDS ) ) {
// the first 10 points of combined encumbrance is ignored, but quickly adds up
const int hands_encumb = std::max( 0,
guy.avg_encumb_of_limb_type( body_part_type::type::hand ) - 5 );
switch( type->energy_source ) {
default:
cost += 10 * hands_encumb;
break;
case magic_energy_type::hp:
cost += hands_encumb;
break;
case magic_energy_type::stamina:
cost += 100 * hands_encumb;
break;
}
}
return cost;
}
bool spell::has_flag( const spell_flag &flag ) const
{
return type->spell_tags[flag];
}
bool spell::is_spell_class( const trait_id &mid ) const
{
return mid == type->spell_class;
}
bool spell::can_cast( const Character &guy ) const
{
if( guy.has_flag( json_flag_NO_SPELLCASTING ) ) {
return false;
}
// only required because crafting_inventory always rebuilds the cache. maybe a const version doesn't write to cache.
Character &guy_inv = const_cast<Character &>( guy );
if( !type->spell_components.is_empty() &&
!type->spell_components->can_make_with_inventory( guy_inv.crafting_inventory( guy.pos(), 0 ),
return_true<item> ) ) {
return false;
}
return guy.magic->has_enough_energy( guy, *this );
}
void spell::use_components( Character &guy ) const
{
if( type->spell_components.is_empty() ) {
return;
}
const requirement_data &spell_components = type->spell_components.obj();
// if we're here, we're assuming the Character has the correct components (using can_cast())
inventory map_inv;
for( const std::vector<item_comp> &comp_vec : spell_components.get_components() ) {
guy.consume_items( guy.select_item_component( comp_vec, 1, map_inv ), 1 );
}
for( const std::vector<tool_comp> &tool_vec : spell_components.get_tools() ) {
guy.consume_tools( guy.select_tool_component( tool_vec, 1, map_inv ), 1 );
}
}
bool spell::check_if_component_in_hand( Character &guy ) const
{
if( type->spell_components.is_empty() ) {
return false;
}
const requirement_data &spell_components = type->spell_components.obj();
if( guy.has_weapon() ) {
if( spell_components.can_make_with_inventory( *guy.get_wielded_item(), return_true<item> ) ) {
return true;
}
}
// if it isn't in hand, return false
return false;
}
int spell::get_difficulty() const
{
return type->difficulty;
}
int spell::casting_time( const Character &guy, bool ignore_encumb ) const
{