forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.cpp
2126 lines (1910 loc) · 73.4 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 <array>
#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 "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "event.h"
#include "field.h"
#include "game.h"
#include "generic_factory.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "json.h"
#include "line.h"
#include "magic_enchantment.h"
#include "map.h"
#include "messages.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "output.h"
#include "pldata.h"
#include "point.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "translations.h"
#include "ui.h"
#include "units.h"
static const trait_id trait_NONE( "NONE" );
namespace io
{
// *INDENT-OFF*
template<>
std::string enum_to_string<valid_target>( valid_target data )
{
switch( data ) {
case valid_target::target_ally: return "ally";
case valid_target::target_hostile: return "hostile";
case valid_target::target_self: return "self";
case valid_target::target_ground: return "ground";
case valid_target::target_none: return "none";
case valid_target::target_item: return "item";
case valid_target::target_fd_fire: return "fd_fire";
case valid_target::target_fd_blood: return "fd_blood";
case valid_target::_LAST: break;
}
debugmsg( "Invalid valid_target" );
abort();
}
template<>
std::string enum_to_string<body_part>( body_part data )
{
switch( data ) {
case body_part::bp_torso: return "TORSO";
case body_part::bp_head: return "HEAD";
case body_part::bp_eyes: return "EYES";
case body_part::bp_mouth: return "MOUTH";
case body_part::bp_arm_l: return "ARM_L";
case body_part::bp_arm_r: return "ARM_R";
case body_part::bp_hand_l: return "HAND_L";
case body_part::bp_hand_r: return "HAND_R";
case body_part::bp_leg_l: return "LEG_L";
case body_part::bp_leg_r: return "LEG_R";
case body_part::bp_foot_l: return "FOOT_L";
case body_part::bp_foot_r: return "FOOT_R";
case body_part::num_bp: break;
}
debugmsg( "Invalid body_part" );
abort();
}
template<>
std::string enum_to_string<spell_flag>( spell_flag data )
{
switch( data ) {
case spell_flag::PERMANENT: return "PERMANENT";
case spell_flag::IGNORE_WALLS: return "IGNORE_WALLS";
case spell_flag::HOSTILE_SUMMON: return "HOSTILE_SUMMON";
case spell_flag::HOSTILE_50: return "HOSTILE_50";
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::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::MUTATE_TRAIT: return "MUTATE_TRAIT";
case spell_flag::PAIN_NORESIST: return "PAIN_NORESIST";
case spell_flag::NO_FAIL: return "NO_FAIL";
case spell_flag::WONDER: return "WONDER";
case spell_flag::LAST: break;
}
debugmsg( "Invalid spell_flag" );
abort();
}
// *INDENT-ON*
} // namespace io
// 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 energy_type energy_source_from_string( const std::string &str )
{
if( str == "MANA" ) {
return mana_energy;
} else if( str == "HP" ) {
return hp_energy;
} else if( str == "BIONIC" ) {
return bionic_energy;
} else if( str == "STAMINA" ) {
return stamina_energy;
} else if( str == "FATIGUE" ) {
return fatigue_energy;
} else if( str == "NONE" ) {
return none_energy;
} else {
debugmsg( _( "ERROR: Invalid energy string. Defaulting to NONE" ) );
return none_energy;
}
}
static damage_type damage_type_from_string( const std::string &str )
{
if( str == "fire" ) {
return DT_HEAT;
} else if( str == "acid" ) {
return DT_ACID;
} else if( str == "bash" ) {
return DT_BASH;
} else if( str == "bio" ) {
return DT_BIOLOGICAL;
} else if( str == "cold" ) {
return DT_COLD;
} else if( str == "cut" ) {
return DT_CUT;
} else if( str == "bullet" ) {
return DT_BULLET;
} else if( str == "electric" ) {
return DT_ELECTRIC;
} else if( str == "stab" ) {
return DT_STAB;
} else if( str == "none" || str == "NONE" ) {
return DT_TRUE;
} else {
debugmsg( _( "ERROR: Invalid damage type string. Defaulting to none" ) );
return DT_TRUE;
}
}
static std::string moves_to_string( const int moves )
{
if( moves < to_moves<int>( 2_seconds ) ) {
return string_format( _( "%d moves" ), moves );
} else {
return to_string( time_duration::from_turns( moves / 100 ) );
}
}
void spell_type::load( const JsonObject &jo, const std::string & )
{
static const
std::map<std::string, std::function<void( const spell &, Creature &, const tripoint & )>>
effect_map{
{ "pain_split", spell_effect::pain_split },
{ "target_attack", spell_effect::target_attack },
{ "projectile_attack", spell_effect::projectile_attack },
{ "cone_attack", spell_effect::cone_attack },
{ "line_attack", spell_effect::line_attack },
{ "teleport_random", spell_effect::teleport_random },
{ "spawn_item", spell_effect::spawn_ethereal_item },
{ "recover_energy", spell_effect::recover_energy },
{ "summon", spell_effect::spawn_summoned_monster },
{ "summon_vehicle", spell_effect::spawn_summoned_vehicle },
{ "translocate", spell_effect::translocate },
{ "area_pull", spell_effect::area_pull },
{ "area_push", spell_effect::area_push },
{ "timed_event", spell_effect::timed_event },
{ "ter_transform", spell_effect::transform_blast },
{ "noise", spell_effect::noise },
{ "vomit", spell_effect::vomit },
{ "explosion", spell_effect::explosion },
{ "flashbang", spell_effect::flashbang },
{ "mod_moves", spell_effect::mod_moves },
{ "map", spell_effect::map_area },
{ "morale", spell_effect::morale },
{ "charm_monster", spell_effect::charm_monster },
{ "mutate", spell_effect::mutate },
{ "bash", spell_effect::bash },
{ "none", spell_effect::none }
};
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "description", description );
optional( jo, was_loaded, "sprite", sprite, "" );
optional( jo, was_loaded, "skill", skill, skill_id( "spellcraft" ) );
optional( jo, was_loaded, "message", message, to_translation( "You cast %s!" ) );
optional( jo, was_loaded, "sound_description", sound_description,
to_translation( "an explosion" ) );
optional( jo, was_loaded, "sound_type", sound_type, sounds::sound_t::combat );
optional( jo, was_loaded, "sound_ambient", sound_ambient, false );
optional( jo, was_loaded, "sound_id", sound_id, "" );
optional( jo, was_loaded, "sound_variant", sound_variant, "default" );
mandatory( jo, was_loaded, "effect", effect_name );
const auto found_effect = effect_map.find( effect_name );
if( found_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;
}
const auto effect_targets_reader = enum_flags_reader<valid_target> { "effect_targets" };
optional( jo, was_loaded, "effect_filter", effect_targets, effect_targets_reader );
const auto targeted_monster_ids_reader = auto_flags_reader<mtype_id> {};
optional( jo, was_loaded, "targeted_monster_ids", targeted_monster_ids,
targeted_monster_ids_reader );
const auto trigger_reader = enum_flags_reader<valid_target> { "valid_targets" };
mandatory( jo, was_loaded, "valid_targets", valid_targets, trigger_reader );
if( jo.has_array( "extra_effects" ) ) {
for( JsonObject fake_spell_obj : jo.get_array( "extra_effects" ) ) {
fake_spell temp;
temp.load( fake_spell_obj );
additional_spells.emplace_back( temp );
}
}
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, "" );
std::string field_input;
optional( jo, was_loaded, "field_id", field_input, "none" );
if( field_input != "none" ) {
field = field_type_id( field_input );
}
optional( jo, was_loaded, "field_chance", field_chance, 1 );
optional( jo, was_loaded, "min_field_intensity", min_field_intensity, 0 );
optional( jo, was_loaded, "max_field_intensity", max_field_intensity, 0 );
optional( jo, was_loaded, "field_intensity_increment", field_intensity_increment, 0.0f );
optional( jo, was_loaded, "field_intensity_variance", field_intensity_variance, 0.0f );
optional( jo, was_loaded, "min_damage", min_damage, 0 );
optional( jo, was_loaded, "damage_increment", damage_increment, 0.0f );
optional( jo, was_loaded, "max_damage", max_damage, 0 );
optional( jo, was_loaded, "min_range", min_range, 0 );
optional( jo, was_loaded, "range_increment", range_increment, 0.0f );
optional( jo, was_loaded, "max_range", max_range, 0 );
optional( jo, was_loaded, "min_aoe", min_aoe, 0 );
optional( jo, was_loaded, "aoe_increment", aoe_increment, 0.0f );
optional( jo, was_loaded, "max_aoe", max_aoe, 0 );
optional( jo, was_loaded, "min_dot", min_dot, 0 );
optional( jo, was_loaded, "dot_increment", dot_increment, 0.0f );
optional( jo, was_loaded, "max_dot", max_dot, 0 );
optional( jo, was_loaded, "min_duration", min_duration, 0 );
optional( jo, was_loaded, "duration_increment", duration_increment, 0.0f );
optional( jo, was_loaded, "max_duration", max_duration, 0 );
optional( jo, was_loaded, "min_pierce", min_pierce, 0 );
optional( jo, was_loaded, "pierce_increment", pierce_increment, 0.0f );
optional( jo, was_loaded, "max_pierce", max_pierce, 0 );
optional( jo, was_loaded, "base_energy_cost", base_energy_cost, 0 );
optional( jo, was_loaded, "final_energy_cost", final_energy_cost, base_energy_cost );
optional( jo, was_loaded, "energy_increment", energy_increment, 0.0f );
std::string temp_string;
optional( jo, was_loaded, "spell_class", temp_string, "NONE" );
spell_class = trait_id( temp_string );
optional( jo, was_loaded, "energy_source", temp_string, "NONE" );
energy_source = energy_source_from_string( temp_string );
optional( jo, was_loaded, "damage_type", temp_string, "NONE" );
dmg_type = damage_type_from_string( temp_string );
optional( jo, was_loaded, "difficulty", difficulty, 0 );
optional( jo, was_loaded, "max_level", max_level, 0 );
optional( jo, was_loaded, "base_casting_time", base_casting_time, 0 );
optional( jo, was_loaded, "final_casting_time", final_casting_time, base_casting_time );
optional( jo, was_loaded, "casting_time_increment", casting_time_increment, 0.0f );
for( const JsonMember member : jo.get_object( "learn_spells" ) ) {
learn_spells.insert( std::pair<std::string, int>( member.name(), member.get_int() ) );
}
}
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_dot_time than max_dot_time", 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 );
}
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 );
}
}
}
std::string spell::damage_string() const
{
if( has_flag( spell_flag::RANDOM_DAMAGE ) ) {
return string_format( "%d-%d", min_leveled_damage(), type->max_damage );
} else {
const int dmg = damage();
if( dmg >= 0 ) {
return string_format( "%d", dmg );
} else {
return string_format( "+%d", std::abs( dmg ) );
}
}
}
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 );
}
}
}
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 );
}
}
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 {
return moves_to_string( duration() );
}
}
time_duration spell::duration_turns() const
{
return 1_turns * duration() / 100;
}
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.encumb( bp_hand_l ) + guy.encumb( bp_hand_r ) - 10 );
switch( type->energy_source ) {
default:
cost += 10 * hands_encumb;
break;
case hp_energy:
cost += hands_encumb;
break;
case stamina_energy:
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
{
switch( type->energy_source ) {
case mana_energy:
return guy.magic->available_mana() >= energy_cost( guy );
case stamina_energy:
return guy.get_stamina() >= energy_cost( guy );
case hp_energy: {
for( const std::pair<const bodypart_str_id, bodypart> &elem : guy.get_body() ) {
if( energy_cost( guy ) < elem.second.get_hp_cur() ) {
return true;
}
}
return false;
}
case bionic_energy:
return guy.get_power_level() >= units::from_kilojoule( energy_cost( guy ) );
case fatigue_energy:
return guy.get_fatigue() < fatigue_levels::exhausted;
case none_energy:
default:
return true;
}
}
int spell::get_difficulty() const
{
return type->difficulty;
}
int spell::casting_time( const Character &guy ) const
{
// casting time in moves
int casting_time = 0;
if( type->base_casting_time < type->final_casting_time ) {
casting_time = std::min( type->final_casting_time,
static_cast<int>( std::round( type->base_casting_time + type->casting_time_increment *
get_level() ) ) );
} else if( type->base_casting_time > type->final_casting_time ) {
casting_time = std::max( type->final_casting_time,
static_cast<int>( std::round( type->base_casting_time + type->casting_time_increment *
get_level() ) ) );
} else {
casting_time = type->base_casting_time;
}
if( !has_flag( spell_flag::NO_LEGS ) ) {
// the first 20 points of encumbrance combined is ignored
const int legs_encumb = std::max( 0, guy.encumb( bp_leg_l ) + guy.encumb( bp_leg_r ) - 20 );
casting_time += legs_encumb * 3;
}
if( has_flag( spell_flag::SOMATIC ) ) {
// the first 20 points of encumbrance combined is ignored
const int arms_encumb = std::max( 0, guy.encumb( bp_arm_l ) + guy.encumb( bp_arm_r ) - 20 );
casting_time += arms_encumb * 2;
}
return casting_time;
}
std::string spell::name() const
{
return type->name.translated();
}
std::string spell::message() const
{
if( !alt_message.empty() ) {
return alt_message.translated();
}
return type->message.translated();
}
float spell::spell_fail( const Character &guy ) const
{
if( has_flag( spell_flag::NO_FAIL ) ) {
return 0.0f;
}
// formula is based on the following:
// exponential curve
// effective skill of 0 or less is 100% failure
// effective skill of 8 (8 int, 0 spellcraft, 0 spell level, spell difficulty 0) is ~50% failure
// effective skill of 30 is 0% failure
const float effective_skill = 2 * ( get_level() - get_difficulty() ) + guy.get_int() +
guy.get_skill_level( skill() );
// add an if statement in here because sufficiently large numbers will definitely overflow because of exponents
if( effective_skill > 30.0f ) {
return 0.0f;
} else if( effective_skill < 0.0f ) {
return 1.0f;
}
float fail_chance = std::pow( ( effective_skill - 30.0f ) / 30.0f, 2 );
if( has_flag( spell_flag::SOMATIC ) && !guy.has_trait_flag( "SUBTLE_SPELL" ) ) {
// the first 20 points of encumbrance combined is ignored
const int arms_encumb = std::max( 0, guy.encumb( bp_arm_l ) + guy.encumb( bp_arm_r ) - 20 );
// each encumbrance point beyond the "gray" color counts as half an additional fail %
fail_chance += arms_encumb / 200.0f;
}
if( has_flag( spell_flag::VERBAL ) && !guy.has_trait_flag( "SILENT_SPELL" ) ) {
// a little bit of mouth encumbrance is allowed, but not much
const int mouth_encumb = std::max( 0, guy.encumb( bp_mouth ) - 5 );
fail_chance += mouth_encumb / 100.0f;
}
// concentration spells work better than you'd expect with a higher focus pool
if( has_flag( spell_flag::CONCENTRATE ) ) {
if( guy.focus_pool <= 0 ) {
return 0.0f;
}
fail_chance /= guy.focus_pool / 100.0f;
}
return clamp( fail_chance, 0.0f, 1.0f );
}
std::string spell::colorized_fail_percent( const Character &guy ) const
{
const float fail_fl = spell_fail( guy ) * 100.0f;
std::string fail_str;
fail_fl == 100.0f ? fail_str = _( "Too Difficult!" ) : fail_str = string_format( "%.1f %% %s",
fail_fl, _( "Failure Chance" ) );
nc_color color;
if( fail_fl > 90.0f ) {
color = c_magenta;
} else if( fail_fl > 75.0f ) {
color = c_red;
} else if( fail_fl > 60.0f ) {
color = c_light_red;
} else if( fail_fl > 35.0f ) {
color = c_yellow;
} else if( fail_fl > 15.0f ) {
color = c_green;
} else {
color = c_light_green;
}
return colorize( fail_str, color );
}
int spell::xp() const
{
return experience;
}
void spell::gain_exp( int nxp )
{
experience += nxp;
}
void spell::set_exp( int nxp )
{
experience = nxp;
}
std::string spell::energy_string() const
{
switch( type->energy_source ) {
case hp_energy:
return _( "health" );
case mana_energy:
return _( "mana" );
case stamina_energy:
return _( "stamina" );
case bionic_energy:
return _( "bionic power" );
case fatigue_energy:
return _( "fatigue" );
default:
return "";
}
}
std::string spell::energy_cost_string( const Character &guy ) const
{
if( energy_source() == none_energy ) {
return _( "none" );
}
if( energy_source() == bionic_energy || energy_source() == mana_energy ) {
return colorize( std::to_string( energy_cost( guy ) ), c_light_blue );
}
if( energy_source() == hp_energy ) {
auto pair = get_hp_bar( energy_cost( guy ), guy.get_hp_max() / num_hp_parts );
return colorize( pair.first, pair.second );
}
if( energy_source() == stamina_energy ) {
auto pair = get_hp_bar( energy_cost( guy ), guy.get_stamina_max() );
return colorize( pair.first, pair.second );
}
if( energy_source() == fatigue_energy ) {
return colorize( std::to_string( energy_cost( guy ) ), c_cyan );
}
debugmsg( "ERROR: Spell %s has invalid energy source.", id().c_str() );
return _( "error: energy_type" );
}
std::string spell::energy_cur_string( const Character &guy ) const
{
if( energy_source() == none_energy ) {
return _( "infinite" );
}
if( energy_source() == bionic_energy ) {
return colorize( std::to_string( units::to_kilojoule( guy.get_power_level() ) ), c_light_blue );
}
if( energy_source() == mana_energy ) {
return colorize( std::to_string( guy.magic->available_mana() ), c_light_blue );
}
if( energy_source() == stamina_energy ) {
auto pair = get_hp_bar( guy.get_stamina(), guy.get_stamina_max() );
return colorize( pair.first, pair.second );
}
if( energy_source() == hp_energy ) {
return "";
}
if( energy_source() == fatigue_energy ) {
const std::pair<std::string, nc_color> pair = guy.get_fatigue_description();
return colorize( pair.first, pair.second );
}
debugmsg( "ERROR: Spell %s has invalid energy source.", id().c_str() );
return _( "error: energy_type" );
}
bool spell::is_valid() const
{
return type.is_valid();
}
bool spell::bp_is_affected( body_part bp ) const
{
return type->affected_bps.count( convert_bp( bp ) );
}
void spell::create_field( const tripoint &at ) const
{
if( !type->field ) {
return;
}
const int intensity = field_intensity() + rng( -type->field_intensity_variance * field_intensity(),
type->field_intensity_variance * field_intensity() );
if( intensity <= 0 ) {
return;
}
if( one_in( type->field_chance ) ) {
map &here = get_map();
field_entry *field = here.get_field( at, *type->field );
if( field ) {
field->set_field_intensity( field->get_field_intensity() + intensity );
} else {
here.add_field( at, *type->field, intensity, -duration_turns() );
}
}
}
void spell::make_sound( const tripoint &target ) const
{
if( !has_flag( spell_flag::SILENT ) ) {
int loudness = std::abs( damage() ) / 3;
if( has_flag( spell_flag::LOUD ) ) {
loudness += 1 + damage() / 3;
}
make_sound( target, loudness );
}
}
void spell::make_sound( const tripoint &target, int loudness ) const
{
sounds::sound( target, loudness, type->sound_type, type->sound_description.translated(),
type->sound_ambient, type->sound_id, type->sound_variant );
}
std::string spell::effect() const
{
return type->effect_name;
}
energy_type spell::energy_source() const
{
return type->energy_source;
}
bool spell::is_target_in_range( const Creature &caster, const tripoint &p ) const
{
return rl_dist( caster.pos(), p ) <= range();
}
bool spell::is_valid_target( valid_target t ) const
{
return type->valid_targets[t];
}
bool spell::is_valid_target( const Creature &caster, const tripoint &p ) const
{
bool valid = false;
if( Creature *const cr = g->critter_at<Creature>( p ) ) {
Creature::Attitude cr_att = cr->attitude_to( caster );
valid = valid || ( cr_att != Creature::A_FRIENDLY && is_valid_target( target_hostile ) );
valid = valid || ( cr_att == Creature::A_FRIENDLY && is_valid_target( target_ally ) &&
p != caster.pos() );
valid = valid || ( is_valid_target( target_self ) && p == caster.pos() );
valid = valid && target_by_monster_id( p );
} else {
valid = is_valid_target( target_ground );
}
return valid;
}
bool spell::is_valid_effect_target( valid_target t ) const
{
return type->effect_targets[t];
}
bool spell::target_by_monster_id( const tripoint &p ) const
{
if( type->targeted_monster_ids.empty() ) {
return true;
}
bool valid = false;
if( monster *const target = g->critter_at<monster>( p ) ) {
if( type->targeted_monster_ids.find( target->type->id ) != type->targeted_monster_ids.end() ) {
valid = true;
}
}
return valid;
}
std::string spell::description() const
{
return type->description.translated();
}
nc_color spell::damage_type_color() const
{
switch( dmg_type() ) {
case DT_HEAT:
return c_red;
case DT_ACID: