forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
martialarts.cpp
2169 lines (1882 loc) · 74.5 KB
/
martialarts.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 "martialarts.h"
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "bodypart.h"
#include "character.h"
#include "character_martial_arts.h"
#include "color.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "effect.h"
#include "enums.h"
#include "game_constants.h"
#include "generic_factory.h"
#include "input.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "json.h"
#include "localized_comparator.h"
#include "map.h"
#include "output.h"
#include "pimpl.h"
#include "point.h"
#include "skill.h"
#include "string_formatter.h"
#include "translations.h"
#include "ui_manager.h"
#include "value_ptr.h"
static const bionic_id bio_armor_arms( "bio_armor_arms" );
static const bionic_id bio_armor_legs( "bio_armor_legs" );
static const bionic_id bio_cqb( "bio_cqb" );
static const flag_id json_flag_UNARMED_WEAPON( "UNARMED_WEAPON" );
static const json_character_flag json_flag_ALWAYS_BLOCK( "ALWAYS_BLOCK" );
static const json_character_flag json_flag_NONSTANDARD_BLOCK( "NONSTANDARD_BLOCK" );
static const limb_score_id limb_score_block( "block" );
static const matec_id tec_none( "tec_none" );
static const skill_id skill_unarmed( "unarmed" );
static const weapon_category_id weapon_category_OTHER_INVALID_WEAP_CAT( "OTHER_INVALID_WEAP_CAT" );
namespace
{
generic_factory<weapon_category> weapon_category_factory( "weapon category" );
generic_factory<ma_technique> ma_techniques( "martial art technique" );
generic_factory<martialart> martialarts( "martial art style" );
generic_factory<ma_buff> ma_buffs( "martial art buff" );
} // namespace
template<>
const weapon_category &weapon_category_id::obj() const
{
return weapon_category_factory.obj( *this );
}
/** @relates string_id */
template<>
bool weapon_category_id::is_valid() const
{
return weapon_category_factory.is_valid( *this );
}
void weapon_category::load_weapon_categories( const JsonObject &jo, const std::string &src )
{
weapon_category_factory.load( jo, src );
}
void weapon_category::reset()
{
weapon_category_factory.reset();
}
void weapon_category::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name_ );
}
const std::vector<weapon_category> &weapon_category::get_all()
{
return weapon_category_factory.get_all();
}
matype_id martial_art_learned_from( const itype &type )
{
if( !type.can_use( "MA_MANUAL" ) ) {
return {};
}
if( !type.book || type.book->martial_art.is_null() ) {
debugmsg( "Item '%s' which claims to teach a martial art is missing martial_art",
type.get_id().str() );
return {};
}
return type.book->martial_art;
}
void load_technique( const JsonObject &jo, const std::string &src )
{
ma_techniques.load( jo, src );
}
// To avoid adding empty entries
template <typename Container>
void add_if_exists( const JsonObject &jo, Container &cont, bool was_loaded,
const std::string &json_key, const typename Container::key_type &id )
{
if( jo.has_member( json_key ) ) {
mandatory( jo, was_loaded, json_key, cont[id] );
}
}
class ma_skill_reader : public generic_typed_reader<ma_skill_reader>
{
public:
std::pair<skill_id, int> get_next( const JsonObject &jo ) const {
return std::pair<skill_id, int>( skill_id( jo.get_string( "name" ) ), jo.get_int( "level" ) );
}
template<typename C>
void erase_next( std::string &&skill_id_str, C &container ) const {
const skill_id id = skill_id( std::move( skill_id_str ) );
reader_detail::handler<C>().erase_if( container, [&id]( const std::pair<skill_id, int> &e ) {
return e.first == id;
} );
}
};
class ma_weapon_damage_reader : public generic_typed_reader<ma_weapon_damage_reader>
{
public:
std::map<std::string, damage_type> dt_map = get_dt_map();
std::pair<damage_type, int> get_next( const JsonObject &jo ) const {
std::string type = jo.get_string( "type" );
const auto iter = get_dt_map().find( type );
if( iter == get_dt_map().end() ) {
jo.throw_error( "Invalid damage type" );
}
const damage_type dt = iter->second;
return std::pair<damage_type, int>( dt, jo.get_int( "min" ) );
}
template<typename C>
void erase_next( const JsonObject &jo, C &container ) const {
std::string type = jo.get_string( "type" );
const auto iter = get_dt_map().find( type );
if( iter == get_dt_map().end() ) {
jo.throw_error( "Invalid damage type" );
}
damage_type id = iter->second;
reader_detail::handler<C>().erase_if( container, [&id]( const std::pair<damage_type, int> &e ) {
return e.first == id;
} );
}
};
tech_effect_data load_tech_effect_data( const JsonObject &e )
{
return tech_effect_data( efftype_id( e.get_string( "id" ) ), e.get_int( "duration", 0 ),
e.get_bool( "permanent", false ), e.get_bool( "on_damage", true ),
e.get_int( "chance", 100 ), e.get_string( "message", "" ),
json_character_flag( e.get_string( "req_flag", "NULL" ) ) );
}
class tech_effect_reader : public generic_typed_reader<tech_effect_reader>
{
public:
tech_effect_data get_next( JsonValue &jv ) const {
JsonObject e = jv.get_object();
return load_tech_effect_data( e );
}
template<typename C>
void erase_next( std::string &&eff_str, C &container ) const {
const efftype_id id = efftype_id( std::move( eff_str ) );
reader_detail::handler<C>().erase_if( container, [&id]( const tech_effect_data & e ) {
return e.id == id;
} );
}
};
void ma_requirements::load( const JsonObject &jo, const std::string & )
{
optional( jo, was_loaded, "unarmed_allowed", unarmed_allowed, false );
optional( jo, was_loaded, "melee_allowed", melee_allowed, false );
optional( jo, was_loaded, "unarmed_weapons_allowed", unarmed_weapons_allowed, true );
if( jo.has_string( "weapon_categories_allowed" ) ) {
weapon_category_id tmp_id;
mandatory( jo, was_loaded, "weapon_categories_allowed", tmp_id );
weapon_categories_allowed.emplace_back( tmp_id );
} else {
optional( jo, was_loaded, "weapon_categories_allowed", weapon_categories_allowed );
}
optional( jo, was_loaded, "strictly_unarmed", strictly_unarmed, false );
optional( jo, was_loaded, "wall_adjacent", wall_adjacent, false );
optional( jo, was_loaded, "required_buffs_all", req_buffs_all, string_id_reader<::ma_buff> {} );
optional( jo, was_loaded, "required_buffs_any", req_buffs_any, string_id_reader<::ma_buff> {} );
optional( jo, was_loaded, "forbidden_buffs_all", forbid_buffs_all, string_id_reader<::ma_buff> {} );
optional( jo, was_loaded, "forbidden_buffs_any", forbid_buffs_any, string_id_reader<::ma_buff> {} );
optional( jo, was_loaded, "req_flags", req_flags, string_id_reader<::json_flag> {} );
optional( jo, was_loaded, "required_char_flags", req_char_flags );
optional( jo, was_loaded, "required_char_flags_all", req_char_flags_all );
optional( jo, was_loaded, "forbidden_char_flags", forbidden_char_flags );
optional( jo, was_loaded, "skill_requirements", min_skill, ma_skill_reader {} );
optional( jo, was_loaded, "weapon_damage_requirements", min_damage, ma_weapon_damage_reader {} );
}
void ma_technique::load( const JsonObject &jo, const std::string &src )
{
mandatory( jo, was_loaded, "name", name );
optional( jo, was_loaded, "description", description, translation() );
if( jo.has_member( "messages" ) ) {
JsonArray jsarr = jo.get_array( "messages" );
jsarr.read( 0, avatar_message );
jsarr.read( 1, npc_message );
}
optional( jo, was_loaded, "crit_tec", crit_tec, false );
optional( jo, was_loaded, "crit_ok", crit_ok, false );
optional( jo, was_loaded, "attack_override", attack_override, false );
optional( jo, was_loaded, "downed_target", downed_target, false );
optional( jo, was_loaded, "stunned_target", stunned_target, false );
optional( jo, was_loaded, "wall_adjacent", wall_adjacent, false );
optional( jo, was_loaded, "human_target", human_target, false );
optional( jo, was_loaded, "needs_ammo", needs_ammo, false );
optional( jo, was_loaded, "defensive", defensive, false );
optional( jo, was_loaded, "disarms", disarms, false );
optional( jo, was_loaded, "take_weapon", take_weapon, false );
optional( jo, was_loaded, "side_switch", side_switch, false );
optional( jo, was_loaded, "dummy", dummy, false );
optional( jo, was_loaded, "dodge_counter", dodge_counter, false );
optional( jo, was_loaded, "block_counter", block_counter, false );
optional( jo, was_loaded, "miss_recovery", miss_recovery, false );
optional( jo, was_loaded, "grab_break", grab_break, false );
optional( jo, was_loaded, "weighting", weighting, 1 );
optional( jo, was_loaded, "repeat_min", repeat_min, 1 );
optional( jo, was_loaded, "repeat_max", repeat_max, 1 );
optional( jo, was_loaded, "down_dur", down_dur, 0 );
optional( jo, was_loaded, "stun_dur", stun_dur, 0 );
optional( jo, was_loaded, "knockback_dist", knockback_dist, 0 );
optional( jo, was_loaded, "knockback_spread", knockback_spread, 0 );
optional( jo, was_loaded, "powerful_knockback", powerful_knockback, false );
optional( jo, was_loaded, "knockback_follow", knockback_follow, false );
optional( jo, was_loaded, "aoe", aoe, "" );
optional( jo, was_loaded, "flags", flags, auto_flags_reader<> {} );
optional( jo, was_loaded, "tech_effects", tech_effects, tech_effect_reader{} );
optional( jo, was_loaded, "attack_vectors", attack_vectors, {} );
optional( jo, was_loaded, "attack_vectors_random", attack_vectors_random, {} );
for( JsonValue jv : jo.get_array( "eocs" ) ) {
eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
reqs.load( jo, src );
bonuses.load( jo );
}
// Not implemented on purpose (martialart objects have no integer id)
// int_id<T> string_id<mabuff>::id() const;
/** @relates string_id */
template<>
const ma_technique &string_id<ma_technique>::obj() const
{
return ma_techniques.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<ma_technique>::is_valid() const
{
return ma_techniques.is_valid( *this );
}
void ma_buff::load( const JsonObject &jo, const std::string &src )
{
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "description", description );
optional( jo, was_loaded, "buff_duration", buff_duration, 2_turns );
optional( jo, was_loaded, "max_stacks", max_stacks, 1 );
optional( jo, was_loaded, "persists", persists, false );
optional( jo, was_loaded, "bonus_dodges", dodges_bonus, 0 );
optional( jo, was_loaded, "bonus_blocks", blocks_bonus, 0 );
optional( jo, was_loaded, "quiet", quiet, false );
optional( jo, was_loaded, "throw_immune", throw_immune, false );
optional( jo, was_loaded, "stealthy", stealthy, false );
optional( jo, was_loaded, "melee_bash_damage_cap_bonus", melee_bash_damage_cap_bonus, false );
reqs.load( jo, src );
bonuses.load( jo );
}
// Not implemented on purpose (martialart objects have no integer id)
// int_id<T> string_id<mabuff>::id() const;
/** @relates string_id */
template<>
const ma_buff &string_id<ma_buff>::obj() const
{
return ma_buffs.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<ma_buff>::is_valid() const
{
return ma_buffs.is_valid( *this );
}
void load_martial_art( const JsonObject &jo, const std::string &src )
{
martialarts.load( jo, src );
}
class ma_buff_reader : public generic_typed_reader<ma_buff_reader>
{
public:
mabuff_id get_next( JsonValue jin ) const {
if( jin.test_string() ) {
return mabuff_id( jin.get_string() );
}
JsonObject jsobj = jin.get_object();
ma_buffs.load( jsobj, "" );
return mabuff_id( jsobj.get_string( "id" ) );
}
};
void martialart::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name );
mandatory( jo, was_loaded, "description", description );
mandatory( jo, was_loaded, "initiate", initiate );
for( JsonArray skillArray : jo.get_array( "autolearn" ) ) {
std::string skill_name = skillArray.get_string( 0 );
int skill_level = skillArray.get_int( 1 );
autolearn_skills.emplace_back( skill_name, skill_level );
}
optional( jo, was_loaded, "primary_skill", primary_skill, skill_unarmed );
optional( jo, was_loaded, "learn_difficulty", learn_difficulty );
optional( jo, was_loaded, "static_buffs", static_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onmove_buffs", onmove_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onpause_buffs", onpause_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onhit_buffs", onhit_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onattack_buffs", onattack_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "ondodge_buffs", ondodge_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onblock_buffs", onblock_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "ongethit_buffs", ongethit_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onmiss_buffs", onmiss_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "oncrit_buffs", oncrit_buffs, ma_buff_reader{} );
optional( jo, was_loaded, "onkill_buffs", onkill_buffs, ma_buff_reader{} );
for( JsonValue jv : jo.get_array( "static_eocs" ) ) {
static_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onmove_eocs" ) ) {
onmove_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onpause_eocs" ) ) {
onpause_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onhit_eocs" ) ) {
onhit_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onattack_eocs" ) ) {
onattack_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "ondodge_eocs" ) ) {
ondodge_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onblock_eocs" ) ) {
onblock_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "ongethit_eocs" ) ) {
ongethit_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onmiss_eocs" ) ) {
onmiss_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "oncrit_eocs" ) ) {
oncrit_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
for( JsonValue jv : jo.get_array( "onkill_eocs" ) ) {
onkill_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}
optional( jo, was_loaded, "techniques", techniques, string_id_reader<::ma_technique> {} );
optional( jo, was_loaded, "weapons", weapons, string_id_reader<::itype> {} );
optional( jo, was_loaded, "weapon_category", weapon_category, auto_flags_reader<weapon_category_id> {} );
optional( jo, was_loaded, "strictly_melee", strictly_melee, false );
optional( jo, was_loaded, "strictly_unarmed", strictly_unarmed, false );
optional( jo, was_loaded, "allow_all_weapons", allow_all_weapons, false );
optional( jo, was_loaded, "force_unarmed", force_unarmed, false );
optional( jo, was_loaded, "prevent_weapon_blocking", prevent_weapon_blocking, false );
optional( jo, was_loaded, "leg_block", leg_block, 99 );
optional( jo, was_loaded, "arm_block", arm_block, 99 );
optional( jo, was_loaded, "nonstandard_block", nonstandard_block, 99 );
optional( jo, was_loaded, "arm_block_with_bio_armor_arms", arm_block_with_bio_armor_arms, false );
optional( jo, was_loaded, "leg_block_with_bio_armor_legs", leg_block_with_bio_armor_legs, false );
}
// Not implemented on purpose (martialart objects have no integer id)
// int_id<T> string_id<martialart>::id() const;
/** @relates string_id */
template<>
const martialart &string_id<martialart>::obj() const
{
return martialarts.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<martialart>::is_valid() const
{
return martialarts.is_valid( *this );
}
std::vector<matype_id> all_martialart_types()
{
std::vector<matype_id> result;
for( const martialart &ma : martialarts.get_all() ) {
result.push_back( ma.id );
}
return result;
}
std::vector<matype_id> autolearn_martialart_types()
{
std::vector<matype_id> result;
for( const martialart &ma : martialarts.get_all() ) {
if( ma.autolearn_skills.empty() ) {
continue;
}
result.push_back( ma.id );
}
return result;
}
static void check( const ma_requirements &req, const std::string &display_text )
{
for( const mabuff_id &r : req.req_buffs_all ) {
if( !r.is_valid() ) {
debugmsg( "ma buff %s of %s does not exist", r.c_str(), display_text );
}
}
for( const mabuff_id &r : req.req_buffs_any ) {
if( !r.is_valid() ) {
debugmsg( "ma buff %s of %s does not exist", r.c_str(), display_text );
}
}
for( const mabuff_id &r : req.forbid_buffs_all ) {
if( !r.is_valid() ) {
debugmsg( "ma buff %s of %s does not exist", r.c_str(), display_text );
}
}
for( const mabuff_id &r : req.forbid_buffs_any ) {
if( !r.is_valid() ) {
debugmsg( "ma buff %s of %s does not exist", r.c_str(), display_text );
}
}
}
void check_martialarts()
{
for( const martialart &ma : martialarts.get_all() ) {
for( auto technique = ma.techniques.cbegin();
technique != ma.techniques.cend(); ++technique ) {
if( !technique->is_valid() ) {
debugmsg( "Technique with id %s in style %s doesn't exist.",
technique->c_str(), ma.name );
}
}
for( auto weapon = ma.weapons.cbegin();
weapon != ma.weapons.cend(); ++weapon ) {
if( !item::type_is_defined( *weapon ) ) {
debugmsg( "Weapon %s in style %s doesn't exist.",
weapon->c_str(), ma.name );
}
}
}
for( const ma_technique &t : ma_techniques.get_all() ) {
::check( t.reqs, string_format( "technique %s", t.id.c_str() ) );
}
for( const ma_buff &b : ma_buffs.get_all() ) {
::check( b.reqs, string_format( "buff %s", b.id.c_str() ) );
}
}
/**
* This is a wrapper class to get access to the protected members of effect_type, it creates
* the @ref effect_type that is used to store a @ref ma_buff in the creatures effect map.
* Note: this class must not contain any new members, it will be converted to a plain
* effect_type later and that would slice the new members of.
*/
class ma_buff_effect_type : public effect_type
{
public:
explicit ma_buff_effect_type( const ma_buff &buff ) {
id = buff.get_effect_id();
max_intensity = buff.max_stacks;
// add_effect add the duration to an existing effect, but it must never be
// above buff_duration, this keeps the old ma_buff behavior
max_duration = buff.buff_duration;
dur_add_perc = 100;
show_intensity = true;
// each add_effect call increases the intensity by 1
int_add_val = 1;
// effect intensity increases by -1 each turn.
int_decay_step = -1;
int_decay_tick = 1;
int_dur_factor = 0_turns;
int_decay_remove = false;
name.push_back( buff.name );
desc.push_back( buff.description );
rating = e_good;
}
};
void finalize_martial_arts()
{
// This adds an effect type for each ma_buff, so we can later refer to it and don't need a
// redundant definition of those effects in json.
for( const ma_buff &buff : ma_buffs.get_all() ) {
const ma_buff_effect_type new_eff( buff );
// Note the slicing here: new_eff is converted to a plain effect_type, but this doesn't
// bother us because ma_buff_effect_type does not have any members that can be sliced.
effect_type::register_ma_buff_effect( new_eff );
}
}
std::string martialart_difficulty( const matype_id &mstyle )
{
std::string diff;
if( mstyle->learn_difficulty <= 2 ) {
diff = _( "easy" );
} else if( mstyle->learn_difficulty <= 4 ) {
diff = _( "moderately hard" );
} else if( mstyle->learn_difficulty <= 6 ) {
diff = _( "hard" );
} else if( mstyle->learn_difficulty <= 8 ) {
diff = _( "very hard" );
} else {
diff = _( "extremely hard" );
}
return diff;
}
void clear_techniques_and_martial_arts()
{
martialarts.reset();
ma_buffs.reset();
ma_techniques.reset();
}
bool ma_requirements::buff_requirements_satisfied( const Character &u ) const
{
const auto having_buff = [&u]( const mabuff_id & buff_id ) {
return u.has_mabuff( buff_id );
};
if( std::any_of( forbid_buffs_any.begin(), forbid_buffs_any.end(), having_buff ) ) {
return false;
}
if( !forbid_buffs_all.empty() ) {
if( std::all_of( forbid_buffs_all.begin(), forbid_buffs_all.end(), having_buff ) ) {
return false;
}
}
return std::all_of( req_buffs_all.begin(), req_buffs_all.end(), having_buff ) &&
( req_buffs_any.empty() || std::any_of( req_buffs_any.begin(), req_buffs_any.end(), having_buff ) );
}
bool ma_requirements::is_valid_character( const Character &u ) const
{
if( !buff_requirements_satisfied( u ) ) {
return false;
}
//A technique is valid if it applies to unarmed strikes, if it applies generally
//to all weapons (such as Ninjutsu sneak attacks or innate weapon techniques like RAPID)
//or if the weapon is flagged as being compatible with the style. Some techniques have
//further restrictions on required weapon properties (is_valid_weapon).
bool cqb = u.has_active_bionic( bio_cqb );
// There are 4 different cases of "armedness":
// Truly unarmed, unarmed weapon, style-allowed weapon, generic weapon
const item_location weapon = u.get_wielded_item();
bool melee_style = u.martial_arts_data->selected_strictly_melee();
bool is_armed = u.is_armed();
bool forced_unarmed = u.martial_arts_data->selected_force_unarmed();
bool unarmed_weapon = is_armed && !forced_unarmed && weapon->has_flag( json_flag_UNARMED_WEAPON );
bool weapon_ok = melee_allowed && weapon && is_valid_weapon( *weapon );
bool style_weapon = weapon && u.martial_arts_data->selected_has_weapon( weapon->typeId() );
bool all_weapons = u.martial_arts_data->selected_allow_all_weapons();
bool unarmed_ok = !is_armed || ( unarmed_weapon && unarmed_weapons_allowed );
bool melee_ok = weapon_ok && ( style_weapon || all_weapons );
bool valid_unarmed = !melee_style && unarmed_allowed && unarmed_ok;
bool valid_melee = !strictly_unarmed && ( forced_unarmed || melee_ok );
if( !valid_unarmed && !valid_melee ) {
return false;
}
if( wall_adjacent && !get_map().is_wall_adjacent( u.pos() ) ) {
return false;
}
for( const auto &pr : min_skill ) {
if( ( cqb ? 5 : u.get_skill_level( pr.first ) ) < pr.second ) {
return false;
}
}
if( !req_char_flags.empty() ) {
bool has_flag = false;
for( const json_character_flag &flag : req_char_flags ) {
if( u.has_flag( flag ) ) {
has_flag = true;
}
}
if( !has_flag ) {
return false;
}
}
for( const json_character_flag &flag : req_char_flags_all ) {
if( !u.has_flag( flag ) ) {
return false;
}
}
if( !forbidden_char_flags.empty() ) {
bool has_flag = false;
for( const json_character_flag &flag : forbidden_char_flags ) {
if( u.has_flag( flag ) ) {
has_flag = true;
}
}
if( has_flag ) {
return false;
}
}
if( !weapon_categories_allowed.empty() ) {
bool valid_weap_cat = false;
for( const weapon_category_id &w_cat : weapon_categories_allowed ) {
if( u.used_weapon() && u.used_weapon()->typeId()->weapon_category.count( w_cat ) > 0 ) {
valid_weap_cat = true;
}
}
if( !valid_weap_cat ) {
return false;
}
}
return true;
}
bool ma_requirements::is_valid_weapon( const item &i ) const
{
for( const flag_id &flag : req_flags ) {
if( !i.has_flag( flag ) ) {
return false;
}
}
for( const auto &pr : min_damage ) {
if( i.damage_melee( pr.first ) < pr.second ) {
return false;
}
}
return true;
}
std::string ma_requirements::get_description( bool buff ) const
{
std::string dump;
if( std::any_of( min_skill.begin(), min_skill.end(), []( const std::pair<skill_id, int> &pr ) {
return pr.second > 0;
} ) ) {
dump += string_format( _( "<bold>%s required: </bold>" ),
n_gettext( "Skill", "Skills", min_skill.size() ) );
dump += enumerate_as_string( min_skill.begin(),
min_skill.end(), []( const std::pair<skill_id, int> &pr ) {
Character &u = get_player_character();
int player_skill = u.get_skill_level( skill_id( pr.first ) );
if( u.has_active_bionic( bio_cqb ) ) {
player_skill = BIO_CQB_LEVEL;
}
return string_format( "%s: <stat>%d</stat>/<stat>%d</stat>", pr.first->name(), player_skill,
pr.second );
}, enumeration_conjunction::none ) + "\n";
}
if( std::any_of( min_damage.begin(),
min_damage.end(), []( const std::pair<damage_type, int> &pr ) {
return pr.second > 0;
} ) ) {
dump += n_gettext( "<bold>Damage type required: </bold>",
"<bold>Damage types required: </bold>", min_damage.size() );
dump += enumerate_as_string( min_damage.begin(),
min_damage.end(), []( const std::pair<damage_type, int> &pr ) {
return string_format( _( "%s: <stat>%d</stat>" ), name_by_dt( pr.first ), pr.second );
}, enumeration_conjunction::none ) + "\n";
}
if( !weapon_categories_allowed.empty() ) {
dump += n_gettext( "<bold>Weapon category required: </bold>",
"<bold>Weapon categories required: </bold>", weapon_categories_allowed.size() );
dump += enumerate_as_string( weapon_categories_allowed.begin(),
weapon_categories_allowed.end(), []( const weapon_category_id & w_cat ) {
if( !w_cat.is_valid() ) {
return w_cat.str();
}
return w_cat->name().translated();
} ) + "\n";
}
if( !req_buffs_all.empty() ) {
dump += _( "<bold>Requires (all):</bold> " );
dump += enumerate_as_string( req_buffs_all.begin(),
req_buffs_all.end(), []( const mabuff_id & bid ) {
return bid->name.translated();
}, enumeration_conjunction::none ) + "\n";
}
if( !req_buffs_any.empty() ) {
dump += _( "<bold>Requires (any):</bold> " );
dump += enumerate_as_string( req_buffs_any.begin(),
req_buffs_any.end(), []( const mabuff_id & bid ) {
return bid->name.translated();
}, enumeration_conjunction::none ) + "\n";
}
if( !forbid_buffs_all.empty() ) {
dump += _( "<bold>Forbidden (all):</bold> " );
dump += enumerate_as_string( forbid_buffs_all.begin(),
forbid_buffs_all.end(), []( const mabuff_id & bid ) {
return bid->name.translated();
}, enumeration_conjunction::none ) + "\n";
}
if( !forbid_buffs_any.empty() ) {
dump += _( "<bold>Forbidden (any):</bold> " );
dump += enumerate_as_string( forbid_buffs_any.begin(),
forbid_buffs_any.end(), []( const mabuff_id & bid ) {
return bid->name.translated();
}, enumeration_conjunction::none ) + "\n";
}
const std::string type = buff ? _( "activate" ) : _( "be used" );
if( unarmed_allowed && melee_allowed ) {
dump += string_format( _( "* Can %s while <info>armed</info> or <info>unarmed</info>" ),
type ) + "\n";
if( unarmed_weapons_allowed ) {
dump += string_format( _( "* Can %s while using <info>any unarmed weapon</info>" ),
type ) + "\n";
}
} else if( unarmed_allowed ) {
dump += string_format( _( "* Can <info>only</info> %s while <info>unarmed</info>" ),
type ) + "\n";
if( unarmed_weapons_allowed ) {
dump += string_format( _( "* Can %s while using <info>any unarmed weapon</info>" ),
type ) + "\n";
}
} else if( melee_allowed ) {
dump += string_format( _( "* Can <info>only</info> %s while <info>armed</info>" ),
type ) + "\n";
}
if( wall_adjacent ) {
dump += string_format( _( "* Can %s while <info>near</info> to a <info>wall</info>" ),
type ) + "\n";
}
return dump;
}
ma_technique::ma_technique()
{
crit_tec = false;
crit_ok = false;
defensive = false;
side_switch = false; // moves the target behind user
dummy = false;
down_dur = 0;
stun_dur = 0;
knockback_dist = 0;
knockback_spread = 0; // adding randomness to knockback, like tec_throw
powerful_knockback = false;
knockback_follow = false; // player follows the knocked-back party into their former tile
// offensive
disarms = false; // like tec_disarm
take_weapon = false; // disarms and equips weapon if hands are free
dodge_counter = false; // like tec_grab
block_counter = false; // like tec_counter
// conditional
downed_target = false; // only works on downed enemies
stunned_target = false; // only works on stunned enemies
wall_adjacent = false; // only works near a wall
human_target = false; // only works on humanoid enemies
needs_ammo = false; // technique only works if the item is loaded with ammo
miss_recovery = false; // reduces the total move cost of a miss by 50%, post stumble modifier
grab_break = false; // allows grab_breaks, like tec_break
}
bool ma_technique::is_valid_character( const Character &u ) const
{
return reqs.is_valid_character( u );
}
ma_buff::ma_buff()
: buff_duration( 1_turns )
{
max_stacks = 1; // total number of stacks this buff can have
dodges_bonus = 0; // extra dodges, like karate
blocks_bonus = 0; // extra blocks, like karate
throw_immune = false;
}
efftype_id ma_buff::get_effect_id() const
{
return efftype_id( std::string( "mabuff:" ) + id.str() );
}
const ma_buff *ma_buff::from_effect( const effect &eff )
{
const std::string &id = eff.get_effect_type()->id.str();
// Same as in get_effect_id!
if( id.compare( 0, 7, "mabuff:" ) != 0 ) {
return nullptr;
}
return &mabuff_id( id.substr( 7 ) ).obj();
}
void ma_buff::apply_buff( Character &u ) const
{
u.add_effect( get_effect_id(), time_duration::from_turns( buff_duration ) );
}
bool ma_buff::is_valid_character( const Character &u ) const
{
return reqs.is_valid_character( u );
}
void ma_buff::apply_character( Character &u ) const
{
u.mod_num_dodges_bonus( dodges_bonus );
u.set_num_blocks_bonus( u.get_num_blocks_bonus() + blocks_bonus );
}
int ma_buff::hit_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::HIT );
}
int ma_buff::critical_hit_chance_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::CRITICAL_HIT_CHANCE );
}
int ma_buff::dodge_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::DODGE );
}
int ma_buff::block_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::BLOCK );
}
int ma_buff::arpen_bonus( const Character &u, damage_type dt ) const
{
return bonuses.get_flat( u, affected_stat::ARMOR_PENETRATION, dt );
}
int ma_buff::block_effectiveness_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::BLOCK_EFFECTIVENESS );
}
int ma_buff::speed_bonus( const Character &u ) const
{
return bonuses.get_flat( u, affected_stat::SPEED );
}
int ma_buff::armor_bonus( const Character &guy, damage_type dt ) const
{
return bonuses.get_flat( guy, affected_stat::ARMOR, dt );
}
float ma_buff::damage_bonus( const Character &u, damage_type dt ) const
{
return bonuses.get_flat( u, affected_stat::DAMAGE, dt );
}
float ma_buff::damage_mult( const Character &u, damage_type dt ) const
{
return bonuses.get_mult( u, affected_stat::DAMAGE, dt );
}
bool ma_buff::is_throw_immune() const
{
return throw_immune;
}
bool ma_buff::is_melee_bash_damage_cap_bonus() const
{
return melee_bash_damage_cap_bonus;
}
bool ma_buff::is_quiet() const
{
return quiet;
}
bool ma_buff::is_stealthy() const
{
return stealthy;
}
bool ma_buff::can_melee() const
{
return melee_allowed;
}
std::string ma_buff::get_description( bool passive ) const
{
std::string dump;
dump += string_format( _( "<bold>Buff technique:</bold> %s" ), name ) + "\n";
std::string temp = bonuses.get_description();
if( !temp.empty() ) {
dump += string_format( _( "<bold>%s:</bold> " ),
n_gettext( "Bonus", "Bonus/stack", max_stacks ) ) + "\n" + temp;
}
dump += reqs.get_description( true );
if( max_stacks > 1 ) {
dump += string_format( _( "* Will <info>stack</info> up to <stat>%d</stat> times" ),
max_stacks ) + "\n";
}
const int turns = to_turns<int>( buff_duration );
if( !passive && turns ) {
dump += string_format( _( "* Will <info>last</info> for <stat>%d %s</stat>" ),
turns, n_gettext( "turn", "turns", turns ) ) + "\n";
}
if( dodges_bonus > 0 ) {
dump += string_format( _( "* Will give a <good>+%s</good> bonus to <info>dodge</info>%s" ),
dodges_bonus, n_gettext( " for the stack", " per stack", max_stacks ) ) + "\n";
} else if( dodges_bonus < 0 ) {
dump += string_format( _( "* Will give a <bad>%s</bad> penalty to <info>dodge</info>%s" ),
dodges_bonus, n_gettext( " for the stack", " per stack", max_stacks ) ) + "\n";
}
if( blocks_bonus > 0 ) {
dump += string_format( _( "* Will give a <good>+%s</good> bonus to <info>block</info>%s" ),
blocks_bonus, n_gettext( " for the stack", " per stack", max_stacks ) ) + "\n";
} else if( blocks_bonus < 0 ) {