forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_enchantment.cpp
638 lines (561 loc) · 23.5 KB
/
magic_enchantment.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
#include "magic_enchantment.h"
#include <cstdlib>
#include <set>
#include <string>
#include "calendar.h"
#include "character.h"
#include "condition.h"
#include "creature.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "generic_factory.h"
#include "item.h"
#include "json.h"
#include "map.h"
#include "point.h"
#include "rng.h"
#include "units.h"
namespace io
{
// *INDENT-OFF*
template<>
std::string enum_to_string<enchantment::has>( enchantment::has data )
{
switch ( data ) {
case enchantment::has::HELD: return "HELD";
case enchantment::has::WIELD: return "WIELD";
case enchantment::has::WORN: return "WORN";
case enchantment::has::NUM_HAS: break;
}
cata_fatal( "Invalid enchantment::has" );
}
template<>
std::string enum_to_string<enchantment::condition>( enchantment::condition data )
{
switch ( data ) {
case enchantment::condition::ALWAYS: return "ALWAYS";
case enchantment::condition::ACTIVE: return "ACTIVE";
case enchantment::condition::INACTIVE: return "INACTIVE";
case enchantment::condition::DIALOG_CONDITION: return "DIALOG_CONDITION";
case enchantment::condition::NUM_CONDITION: break;
}
cata_fatal( "Invalid enchantment::condition" );
}
template<>
std::string enum_to_string<enchant_vals::mod>( enchant_vals::mod data )
{
switch ( data ) {
case enchant_vals::mod::STRENGTH: return "STRENGTH";
case enchant_vals::mod::DEXTERITY: return "DEXTERITY";
case enchant_vals::mod::PERCEPTION: return "PERCEPTION";
case enchant_vals::mod::INTELLIGENCE: return "INTELLIGENCE";
case enchant_vals::mod::SPEED: return "SPEED";
case enchant_vals::mod::ATTACK_COST: return "ATTACK_COST";
case enchant_vals::mod::ATTACK_SPEED: return "ATTACK_SPEED";
case enchant_vals::mod::MOVE_COST: return "MOVE_COST";
case enchant_vals::mod::METABOLISM: return "METABOLISM";
case enchant_vals::mod::MAX_MANA: return "MAX_MANA";
case enchant_vals::mod::REGEN_MANA: return "REGEN_MANA";
case enchant_vals::mod::BIONIC_POWER: return "BIONIC_POWER";
case enchant_vals::mod::MAX_STAMINA: return "MAX_STAMINA";
case enchant_vals::mod::REGEN_STAMINA: return "REGEN_STAMINA";
case enchant_vals::mod::MAX_HP: return "MAX_HP";
case enchant_vals::mod::REGEN_HP: return "REGEN_HP";
case enchant_vals::mod::HUNGER: return "HUNGER";
case enchant_vals::mod::THIRST: return "THIRST";
case enchant_vals::mod::FATIGUE: return "FATIGUE";
case enchant_vals::mod::PAIN: return "PAIN";
case enchant_vals::mod::BONUS_DAMAGE: return "BONUS_DAMAGE";
case enchant_vals::mod::BONUS_BLOCK: return "BONUS_BLOCK";
case enchant_vals::mod::BONUS_DODGE: return "BONUS_DODGE";
case enchant_vals::mod::ATTACK_NOISE: return "ATTACK_NOISE";
case enchant_vals::mod::SHOUT_NOISE: return "SHOUT_NOISE";
case enchant_vals::mod::FOOTSTEP_NOISE: return "FOOTSTEP_NOISE";
case enchant_vals::mod::SIGHT_RANGE: return "SIGHT_RANGE";
case enchant_vals::mod::CARRY_WEIGHT: return "CARRY_WEIGHT";
case enchant_vals::mod::WEAPON_DISPERSION: return "WEAPON_DISPERSION";
case enchant_vals::mod::SOCIAL_LIE: return "SOCIAL_LIE";
case enchant_vals::mod::SOCIAL_PERSUADE: return "SOCIAL_PERSUADE";
case enchant_vals::mod::SOCIAL_INTIMIDATE: return "SOCIAL_INTIMIDATE";
case enchant_vals::mod::SLEEPY: return "SLEEPY";
case enchant_vals::mod::LUMINATION: return "LUMINATION";
case enchant_vals::mod::EFFECTIVE_HEALTH_MOD: return "EFFECTIVE_HEALTH_MOD";
case enchant_vals::mod::MOD_HEALTH: return "MOD_HEALTH";
case enchant_vals::mod::MOD_HEALTH_CAP: return "MOD_HEALTH_CAP";
case enchant_vals::mod::MAP_MEMORY: return "MAP_MEMORY";
case enchant_vals::mod::READING_EXP: return "READING_EXP";
case enchant_vals::mod::SKILL_RUST_RESIST: return "SKILL_RUST_RESIST";
case enchant_vals::mod::LEARNING_FOCUS: return "LEARNING_FOCUS";
case enchant_vals::mod::ARMOR_ACID: return "ARMOR_ACID";
case enchant_vals::mod::ARMOR_BASH: return "ARMOR_BASH";
case enchant_vals::mod::ARMOR_BIO: return "ARMOR_BIO";
case enchant_vals::mod::ARMOR_COLD: return "ARMOR_COLD";
case enchant_vals::mod::ARMOR_CUT: return "ARMOR_CUT";
case enchant_vals::mod::ARMOR_ELEC: return "ARMOR_ELEC";
case enchant_vals::mod::ARMOR_HEAT: return "ARMOR_HEAT";
case enchant_vals::mod::ARMOR_STAB: return "ARMOR_STAB";
case enchant_vals::mod::ARMOR_BULLET: return "ARMOR_BULLET";
case enchant_vals::mod::EXTRA_BASH: return "EXTRA_BASH";
case enchant_vals::mod::EXTRA_CUT: return "EXTRA_CUT";
case enchant_vals::mod::EXTRA_STAB: return "EXTRA_STAB";
case enchant_vals::mod::EXTRA_BULLET: return "EXTRA_BULLET";
case enchant_vals::mod::EXTRA_HEAT: return "EXTRA_HEAT";
case enchant_vals::mod::EXTRA_COLD: return "EXTRA_COLD";
case enchant_vals::mod::EXTRA_ELEC: return "EXTRA_ELEC";
case enchant_vals::mod::EXTRA_ACID: return "EXTRA_ACID";
case enchant_vals::mod::EXTRA_BIO: return "EXTRA_BIO";
case enchant_vals::mod::ITEM_DAMAGE_PURE: return "ITEM_DAMAGE_PURE";
case enchant_vals::mod::ITEM_DAMAGE_BASH: return "ITEM_DAMAGE_BASH";
case enchant_vals::mod::ITEM_DAMAGE_CUT: return "ITEM_DAMAGE_CUT";
case enchant_vals::mod::ITEM_DAMAGE_STAB: return "ITEM_DAMAGE_STAB";
case enchant_vals::mod::ITEM_DAMAGE_BULLET: return "ITEM_DAMAGE_BULLET";
case enchant_vals::mod::ITEM_DAMAGE_HEAT: return "ITEM_DAMAGE_HEAT";
case enchant_vals::mod::ITEM_DAMAGE_COLD: return "ITEM_DAMAGE_COLD";
case enchant_vals::mod::ITEM_DAMAGE_ELEC: return "ITEM_DAMAGE_ELEC";
case enchant_vals::mod::ITEM_DAMAGE_ACID: return "ITEM_DAMAGE_ACID";
case enchant_vals::mod::ITEM_DAMAGE_BIO: return "ITEM_DAMAGE_BIO";
case enchant_vals::mod::ITEM_DAMAGE_AP: return "ITEM_DAMAGE_AP";
case enchant_vals::mod::ITEM_ARMOR_BASH: return "ITEM_ARMOR_BASH";
case enchant_vals::mod::ITEM_ARMOR_CUT: return "ITEM_ARMOR_CUT";
case enchant_vals::mod::ITEM_ARMOR_STAB: return "ITEM_ARMOR_STAB";
case enchant_vals::mod::ITEM_ARMOR_BULLET: return "ITEM_ARMOR_BULLET";
case enchant_vals::mod::ITEM_ARMOR_HEAT: return "ITEM_ARMOR_HEAT";
case enchant_vals::mod::ITEM_ARMOR_COLD: return "ITEM_ARMOR_COLD";
case enchant_vals::mod::ITEM_ARMOR_ELEC: return "ITEM_ARMOR_ELEC";
case enchant_vals::mod::ITEM_ARMOR_ACID: return "ITEM_ARMOR_ACID";
case enchant_vals::mod::ITEM_ARMOR_BIO: return "ITEM_ARMOR_BIO";
case enchant_vals::mod::ITEM_WEIGHT: return "ITEM_WEIGHT";
case enchant_vals::mod::ITEM_ENCUMBRANCE: return "ITEM_ENCUMBRANCE";
case enchant_vals::mod::ITEM_VOLUME: return "ITEM_VOLUME";
case enchant_vals::mod::ITEM_COVERAGE: return "ITEM_COVERAGE";
case enchant_vals::mod::ITEM_ATTACK_SPEED: return "ITEM_ATTACK_SPEED";
case enchant_vals::mod::ITEM_WET_PROTECTION: return "ITEM_WET_PROTECTION";
case enchant_vals::mod::NUM_MOD: break;
}
cata_fatal( "Invalid enchant_vals::mod" );
}
// *INDENT-ON*
} // namespace io
namespace
{
generic_factory<enchantment> spell_factory( "enchantment" );
} // namespace
template<>
const enchantment &string_id<enchantment>::obj() const
{
return spell_factory.obj( *this );
}
template<>
bool string_id<enchantment>::is_valid() const
{
return spell_factory.is_valid( *this );
}
void enchantment::load_enchantment( const JsonObject &jo, const std::string &src )
{
spell_factory.load( jo, src );
}
void enchantment::reset()
{
spell_factory.reset();
}
enchantment_id enchantment::load_inline_enchantment( const JsonValue &jv, const std::string &src,
std::string &inline_id )
{
if( jv.test_string() ) {
return enchantment_id( jv.get_string() );
} else if( jv.test_object() ) {
if( inline_id.empty() ) {
jv.throw_error( "Inline enchantment cannot be created without an id." );
}
if( spell_factory.is_valid( enchantment_id( inline_id ) ) ) {
jv.throw_error( "Inline enchantment " + inline_id +
" cannot be created as an enchantment already has this id." );
}
enchantment inline_enchant;
inline_enchant.load( jv.get_object(), src, inline_id );
spell_factory.insert( inline_enchant );
return enchantment_id( inline_id );
} else {
jv.throw_error( "Enchantment needs to be either string or enchantment object." );
}
}
bool enchantment::is_active( const Character &guy, const item &parent ) const
{
if( !guy.has_item( parent ) ) {
return false;
}
if( active_conditions.first == has::HELD &&
active_conditions.second == condition::ALWAYS ) {
return true;
}
if( !( active_conditions.first == has::HELD ||
( active_conditions.first == has::WIELD && guy.is_wielding( parent ) ) ||
( active_conditions.first == has::WORN && guy.is_worn( parent ) ) ) ) {
return false;
}
return is_active( guy, parent.active );
}
bool enchantment::is_active( const Character &guy, const bool active ) const
{
if( active_conditions.second == condition::ACTIVE ) {
return active;
}
if( active_conditions.second == condition::INACTIVE ) {
return !active;
}
if( active_conditions.second == condition::ALWAYS ) {
return true;
}
if( active_conditions.second == condition::DIALOG_CONDITION ) {
dialogue d( get_talker_for( guy ), nullptr );
return dialog_condition( d );
}
return false;
}
bool enchantment::active_wield() const
{
return active_conditions.first == has::HELD || active_conditions.first == has::WIELD;
}
void enchantment::add_activation( const time_duration &dur, const fake_spell &fake )
{
intermittent_activation[dur].emplace_back( fake );
}
void enchantment::bodypart_changes::load( const JsonObject &jo )
{
optional( jo, was_loaded, "gain", gain );
optional( jo, was_loaded, "lose", lose );
}
void enchantment::bodypart_changes::deserialize( const JsonObject &jo )
{
load( jo );
}
void enchantment::bodypart_changes::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "gain", gain );
jsout.member( "lose", lose );
jsout.end_object();
}
void enchantment::load( const JsonObject &jo, const std::string &,
const cata::optional<std::string> &inline_id )
{
optional( jo, was_loaded, "id", id, enchantment_id( inline_id.value_or( "" ) ) );
jo.read( "hit_you_effect", hit_you_effect );
jo.read( "hit_me_effect", hit_me_effect );
jo.read( "emitter", emitter );
if( jo.has_object( "intermittent_activation" ) ) {
JsonObject jobj = jo.get_object( "intermittent_activation" );
for( const JsonObject effect_obj : jobj.get_array( "effects" ) ) {
time_duration dur = read_from_json_string<time_duration>( effect_obj.get_member( "frequency" ),
time_duration::units );
if( effect_obj.has_array( "spell_effects" ) ) {
for( const JsonObject fake_spell_obj : effect_obj.get_array( "spell_effects" ) ) {
fake_spell fake;
fake.load( fake_spell_obj );
add_activation( dur, fake );
}
} else if( effect_obj.has_object( "spell_effects" ) ) {
fake_spell fake;
JsonObject fake_spell_obj = effect_obj.get_object( "spell_effects" );
fake.load( fake_spell_obj );
add_activation( dur, fake );
}
}
}
active_conditions.first = io::string_to_enum<has>( jo.get_string( "has", "HELD" ) );
if( jo.has_string( "condition" ) ) {
std::string condit;
optional( jo, was_loaded, "condition", condit );
cata::optional<enchantment::condition> con = io::string_to_enum_optional<condition>( condit );
if( con.has_value() ) {
active_conditions.second = con.value();
} else {
active_conditions.second = condition::DIALOG_CONDITION;
read_condition<dialogue>( jo, "condition", dialog_condition, false );
}
} else if( jo.has_member( "condition" ) ) {
active_conditions.second = condition::DIALOG_CONDITION;
read_condition<dialogue>( jo, "condition", dialog_condition, false );
} else {
active_conditions.second = condition::ALWAYS;
}
for( JsonObject jsobj : jo.get_array( "ench_effects" ) ) {
ench_effects.emplace( efftype_id( jsobj.get_string( "effect" ) ), jsobj.get_int( "intensity" ) );
}
optional( jo, was_loaded, "modified_bodyparts", modified_bodyparts );
optional( jo, was_loaded, "mutations", mutations );
if( jo.has_array( "values" ) ) {
for( const JsonObject value_obj : jo.get_array( "values" ) ) {
const enchant_vals::mod value = io::string_to_enum<enchant_vals::mod>
( value_obj.get_string( "value" ) );
const int add = value_obj.get_int( "add", 0 );
const double mult = value_obj.get_float( "multiply", 0.0 );
if( add != 0 ) {
values_add.emplace( value, add );
}
if( mult != 0.0 ) {
values_multiply.emplace( value, mult );
}
}
}
}
void enchantment::serialize( JsonOut &jsout ) const
{
jsout.start_object();
if( !id.is_empty() ) {
jsout.member( "id", id );
jsout.end_object();
// if the enchantment has an id then it is defined elsewhere and does not need to be serialized.
return;
}
jsout.member( "has", io::enum_to_string<has>( active_conditions.first ) );
jsout.member( "condition", io::enum_to_string<condition>( active_conditions.second ) );
if( emitter ) {
jsout.member( "emitter", emitter );
}
if( !hit_you_effect.empty() ) {
jsout.member( "hit_you_effect", hit_you_effect );
}
if( !hit_me_effect.empty() ) {
jsout.member( "hit_me_effect", hit_me_effect );
}
if( !intermittent_activation.empty() ) {
jsout.member( "intermittent_activation" );
jsout.start_object();
jsout.member( "effects" );
jsout.start_array();
for( const std::pair<time_duration, std::vector<fake_spell>> pair : intermittent_activation ) {
jsout.start_object();
jsout.member( "frequency", to_string_writable( pair.first ) );
jsout.member( "spell_effects", pair.second );
jsout.end_object();
}
jsout.end_array();
jsout.end_object();
}
if( !ench_effects.empty() ) {
jsout.member( "ench_effects" );
jsout.start_array();
for( const std::pair<const efftype_id, int> &eff : ench_effects ) {
jsout.start_object();
jsout.member( "effect", eff.first );
jsout.member( "intensity", eff.second );
jsout.end_object();
}
jsout.end_array();
}
jsout.member( "modified_bodyparts", modified_bodyparts );
jsout.member( "mutations", mutations );
jsout.member( "values" );
jsout.start_array();
for( int value = 0; value < static_cast<int>( enchant_vals::mod::NUM_MOD ); value++ ) {
enchant_vals::mod enum_value = static_cast<enchant_vals::mod>( value );
if( get_value_add( enum_value ) == 0 && get_value_multiply( enum_value ) == 0.0 ) {
continue;
}
jsout.start_object();
jsout.member( "value", io::enum_to_string<enchant_vals::mod>( enum_value ) );
if( get_value_add( enum_value ) != 0 ) {
jsout.member( "add", get_value_add( enum_value ) );
}
if( get_value_multiply( enum_value ) != 0 ) {
jsout.member( "multiply", get_value_multiply( enum_value ) );
}
jsout.end_object();
}
jsout.end_array();
jsout.end_object();
}
bool enchantment::stacks_with( const enchantment &rhs ) const
{
return active_conditions == rhs.active_conditions;
}
bool enchantment::add( const enchantment &rhs )
{
if( !stacks_with( rhs ) ) {
return false;
}
force_add( rhs );
return true;
}
void enchantment::force_add( const enchantment &rhs )
{
for( const std::pair<const enchant_vals::mod, int> &pair_values : rhs.values_add ) {
values_add[pair_values.first] += pair_values.second;
}
for( const std::pair<const enchant_vals::mod, double> &pair_values : rhs.values_multiply ) {
// values do not multiply against each other, they add.
// so +10% and -10% will add to 0%
values_multiply[pair_values.first] += pair_values.second;
}
hit_me_effect.insert( hit_me_effect.end(), rhs.hit_me_effect.begin(), rhs.hit_me_effect.end() );
hit_you_effect.insert( hit_you_effect.end(), rhs.hit_you_effect.begin(), rhs.hit_you_effect.end() );
ench_effects.insert( rhs.ench_effects.begin(), rhs.ench_effects.end() );
if( rhs.emitter ) {
emitter = rhs.emitter;
}
for( const bodypart_changes &bp : rhs.modified_bodyparts ) {
modified_bodyparts.emplace_back( bp );
}
for( const trait_id &branch : rhs.mutations ) {
mutations.emplace( branch );
}
for( const std::pair<const time_duration, std::vector<fake_spell>> &act_pair :
rhs.intermittent_activation ) {
for( const fake_spell &fake : act_pair.second ) {
intermittent_activation[act_pair.first].emplace_back( fake );
}
}
}
void enchantment::set_has( enchantment::has value )
{
active_conditions.first = value;
}
void enchantment::add_value_add( enchant_vals::mod value, int add_value )
{
values_add[value] = add_value;
}
void enchantment::add_value_mult( enchant_vals::mod value, float mult_value )
{
values_multiply[value] = mult_value;
}
void enchantment::add_hit_me( const fake_spell &sp )
{
hit_me_effect.push_back( sp );
}
void enchantment::add_hit_you( const fake_spell &sp )
{
hit_you_effect.push_back( sp );
}
int enchantment::get_value_add( const enchant_vals::mod value ) const
{
const auto found = values_add.find( value );
if( found == values_add.cend() ) {
return 0;
}
return found->second;
}
double enchantment::get_value_multiply( const enchant_vals::mod value ) const
{
const auto found = values_multiply.find( value );
if( found == values_multiply.cend() ) {
return 0;
}
return found->second;
}
double enchantment::modify_value( const enchant_vals::mod mod_val, double value ) const
{
value += get_value_add( mod_val );
value *= 1.0 + get_value_multiply( mod_val );
return value;
}
units::energy enchantment::modify_value( const enchant_vals::mod mod_val,
units::energy value ) const
{
value += units::from_millijoule<int>( get_value_add( mod_val ) );
value *= 1.0 + get_value_multiply( mod_val );
return value;
}
units::mass enchantment::modify_value( const enchant_vals::mod mod_val,
units::mass value ) const
{
value += units::from_gram<int>( get_value_add( mod_val ) );
value *= 1.0 + get_value_multiply( mod_val );
return value;
}
int enchantment::mult_bonus( enchant_vals::mod value_type, int base_value ) const
{
return get_value_multiply( value_type ) * base_value;
}
bool enchantment::modifies_bodyparts() const
{
return !modified_bodyparts.empty();
}
body_part_set enchantment::modify_bodyparts( const body_part_set &unmodified ) const
{
body_part_set modified( unmodified );
for( const enchantment::bodypart_changes &changes : modified_bodyparts ) {
if( !changes.gain.is_empty() ) {
modified.set( changes.gain );
}
if( !changes.lose.is_empty() ) {
modified.reset( changes.lose );
}
}
return modified;
}
void enchantment::activate_passive( Character &guy ) const
{
guy.mod_str_bonus( get_value_add( enchant_vals::mod::STRENGTH ) );
guy.mod_str_bonus( mult_bonus( enchant_vals::mod::STRENGTH, guy.get_str_base() ) );
guy.mod_dex_bonus( get_value_add( enchant_vals::mod::DEXTERITY ) );
guy.mod_dex_bonus( mult_bonus( enchant_vals::mod::DEXTERITY, guy.get_dex_base() ) );
guy.mod_per_bonus( get_value_add( enchant_vals::mod::PERCEPTION ) );
guy.mod_per_bonus( mult_bonus( enchant_vals::mod::PERCEPTION, guy.get_per_base() ) );
guy.mod_int_bonus( get_value_add( enchant_vals::mod::INTELLIGENCE ) );
guy.mod_int_bonus( mult_bonus( enchant_vals::mod::INTELLIGENCE, guy.get_int_base() ) );
guy.mod_num_dodges_bonus( get_value_add( enchant_vals::mod::BONUS_DODGE ) );
guy.mod_num_dodges_bonus( mult_bonus( enchant_vals::mod::BONUS_DODGE, guy.get_num_dodges_base() ) );
if( emitter ) {
get_map().emit_field( guy.pos(), *emitter );
}
for( const std::pair<efftype_id, int> eff : ench_effects ) {
guy.add_effect( eff.first, 1_seconds, false, eff.second );
}
for( const std::pair<const time_duration, std::vector<fake_spell>> &activation :
intermittent_activation ) {
// a random approximation!
if( one_in( to_seconds<int>( activation.first ) ) ) {
for( const fake_spell &fake : activation.second ) {
fake.get_spell( 0 ).cast_all_effects( guy, guy.pos() );
}
}
}
}
void enchantment::cast_hit_you( Character &caster, const Creature &target ) const
{
for( const fake_spell &sp : hit_you_effect ) {
cast_enchantment_spell( caster, &target, sp );
}
}
void enchantment::cast_hit_me( Character &caster, const Creature *target ) const
{
for( const fake_spell &sp : hit_me_effect ) {
cast_enchantment_spell( caster, target, sp );
}
}
void enchantment::cast_enchantment_spell( Character &caster, const Creature *target,
const fake_spell &sp ) const
{
// check the chances
if( !one_in( sp.trigger_once_in ) ) {
return;
}
if( sp.self ) {
caster.add_msg_player_or_npc( m_good,
sp.trigger_message,
sp.npc_trigger_message,
caster.get_name() );
sp.get_spell( sp.level ).cast_all_effects( caster, caster.pos() );
} else if( target != nullptr ) {
const Creature &trg_crtr = *target;
const spell &spell_lvl = sp.get_spell( sp.level );
if( !spell_lvl.is_valid_target( caster, trg_crtr.pos() ) ||
!spell_lvl.is_target_in_range( caster, trg_crtr.pos() ) ) {
return;
}
caster.add_msg_player_or_npc( m_good,
sp.trigger_message,
sp.npc_trigger_message,
caster.get_name(), trg_crtr.disp_name() );
spell_lvl.cast_all_effects( caster, trg_crtr.pos() );
}
}
bool enchantment::operator==( const enchantment &rhs ) const
{
return this->id == rhs.id &&
this->get_mutations() == rhs.get_mutations() &&
this->values_multiply == rhs.values_multiply &&
this->values_add == rhs.values_add;
}