forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.cpp
2896 lines (2751 loc) · 114 KB
/
condition.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 "condition.h"
#include <climits>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <new>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include "avatar.h"
#include "calendar.h"
#include "character.h"
#include "coordinates.h"
#include "debug.h"
#include "enum_conversions.h"
#include "field.h"
#include "game.h"
#include "generic_factory.h"
#include "global_vars.h"
#include "item.h"
#include "item_category.h"
#include "json.h"
#include "kill_tracker.h"
#include "line.h"
#include "map.h"
#include "mapdata.h"
#include "mission.h"
#include "mtype.h"
#include "npc.h"
#include "optional.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "point.h"
#include "recipe_groups.h"
#include "talker.h"
#include "type_id.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "widget.h"
class basecamp;
class recipe;
static const efftype_id effect_currently_busy( "currently_busy" );
static const json_character_flag json_flag_MUTATION_THRESHOLD( "MUTATION_THRESHOLD" );
template<class T>
std::string get_talk_varname( const JsonObject &jo, const std::string &member,
bool check_value, int_or_var<T> &default_val )
{
if( check_value && !( jo.has_string( "value" ) || jo.has_member( "time" ) ||
jo.has_array( "possible_values" ) ) ) {
jo.throw_error( "invalid " + member + " condition in " + jo.str() );
}
const std::string &var_basename = jo.get_string( member );
const std::string &type_var = jo.get_string( "type", "" );
const std::string &var_context = jo.get_string( "context", "" );
default_val = get_int_or_var<T>( jo, "default", false );
if( jo.has_member( "default_time" ) ) {
int_or_var<T> value;
time_duration max_time;
mandatory( jo, false, "default_time", max_time );
value.min.int_val = to_turns<int>( max_time );
default_val = value;
}
return "npctalk_var" + ( type_var.empty() ? "" : "_" + type_var ) + ( var_context.empty() ? "" : "_"
+ var_context ) + "_" + var_basename;
}
template<class T>
int_or_var_part<T> get_int_or_var_part( const JsonValue &jv, std::string member, bool required,
int default_val )
{
int_or_var_part<T> ret_val;
if( jv.test_int() ) {
ret_val.int_val = jv.get_int();
} else if( jv.test_object() ) {
JsonObject jo = jv.get_object();
jo.allow_omitted_members();
if( jo.has_array( "arithmetic" ) ) {
talk_effect_fun_t<T> arith;
arith.set_arithmetic( jo, "arithmetic", true );
ret_val.arithmetic_val = arith;
} else {
ret_val.var_val = read_var_info( jo, true );
}
} else if( required ) {
jv.throw_error( "No valid value for " + member );
} else {
ret_val.int_val = default_val;
}
return ret_val;
}
template<class T>
int_or_var<T> get_int_or_var( const JsonObject &jo, std::string member, bool required,
int default_val )
{
int_or_var<T> ret_val;
if( jo.has_array( member ) ) {
JsonArray ja = jo.get_array( member );
ret_val.min = get_int_or_var_part<T>( ja.next(), member );
ret_val.max = get_int_or_var_part<T>( ja.next(), member );
ret_val.pair = true;
} else if( required ) {
ret_val.min = get_int_or_var_part<T>( jo.get_member( member ), member, required, default_val );
} else {
if( jo.has_member( member ) ) {
ret_val.min = get_int_or_var_part<T>( jo.get_member( member ), member, required, default_val );
} else {
ret_val.min.int_val = default_val;
}
}
return ret_val;
}
template<class T>
duration_or_var_part<T> get_duration_or_var_part( const JsonValue &jv, std::string member,
bool required, time_duration default_val )
{
duration_or_var_part<T> ret_val;
if( jv.test_string() ) {
if( jv.get_string() == "infinite" ) {
ret_val.dur_val = time_duration::from_turns( calendar::INDEFINITELY_LONG );
} else {
ret_val.dur_val = read_from_json_string<time_duration>( jv, time_duration::units );
}
} else if( jv.test_int() ) {
ret_val.dur_val = time_duration::from_turns( jv.get_int() );
} else if( jv.test_object() ) {
JsonObject jo = jv.get_object();
jo.allow_omitted_members();
if( jo.has_array( "arithmetic" ) ) {
talk_effect_fun_t<T> arith;
arith.set_arithmetic( jo, "arithmetic", true );
ret_val.arithmetic_val = arith;
} else {
ret_val.var_val = read_var_info( jo, true );
}
} else if( required ) {
jv.throw_error( "No valid value for " + member );
} else {
ret_val.dur_val = default_val;
}
return ret_val;
}
template<class T>
duration_or_var<T> get_duration_or_var( const JsonObject &jo, std::string member, bool required,
time_duration default_val )
{
duration_or_var<T> ret_val;
if( jo.has_array( member ) ) {
JsonArray ja = jo.get_array( member );
ret_val.min = get_duration_or_var_part<T>( ja.next(), member );
ret_val.max = get_duration_or_var_part<T>( ja.next(), member );
ret_val.pair = true;
} else if( required ) {
ret_val.min = get_duration_or_var_part<T>( jo.get_member( member ), member, required, default_val );
} else {
if( jo.has_member( member ) ) {
ret_val.min = get_duration_or_var_part<T>( jo.get_member( member ), member, required, default_val );
} else {
ret_val.min.dur_val = default_val;
}
}
return ret_val;
}
template<class T>
str_or_var<T> get_str_or_var( const JsonValue &jv, std::string member, bool required,
std::string default_val )
{
str_or_var<T> ret_val;
if( jv.test_string() ) {
ret_val.str_val = jv.get_string();
} else if( jv.test_object() ) {
ret_val.var_val = read_var_info( jv.get_object(), true );
} else if( required ) {
jv.throw_error( "No valid value for " + member );
} else {
ret_val.str_val = default_val;
}
return ret_val;
}
template<class T>
tripoint_abs_ms get_tripoint_from_var( cata::optional<var_info> var, const T &d )
{
tripoint_abs_ms target_pos = get_map().getglobal( d.actor( false )->pos() );
if( var.has_value() ) {
std::string value = read_var_value<T>( var.value(), d );
if( !value.empty() ) {
target_pos = tripoint_abs_ms( tripoint::from_string( value ) );
}
}
return target_pos;
}
var_info read_var_info( const JsonObject &jo, bool require_default )
{
std::string default_val;
int_or_var<dialogue> empty;
var_type type;
std::string name;
if( jo.has_string( "default_str" ) ) {
default_val = jo.get_string( "default_str" );
} else if( jo.has_string( "default" ) ) {
default_val = std::to_string( to_turns<int>( read_from_json_string<time_duration>
( jo.get_member( "default" ), time_duration::units ) ) );
} else if( jo.has_int( "default" ) ) {
default_val = std::to_string( jo.get_int( "default" ) );
} else if( jo.has_member( "default_time" ) ) {
time_duration max_time;
mandatory( jo, false, "default_time", max_time );
default_val = std::to_string( to_turns<int>( max_time ) );
} else if( require_default ) {
jo.throw_error( "No default value provided." );
}
if( jo.has_string( "var_name" ) ) {
const std::string &type_var = jo.get_string( "type", "" );
const std::string &var_context = jo.get_string( "context", "" );
name = "npctalk_var_" + type_var + ( type_var.empty() ? "" : "_" ) + var_context +
( var_context.empty() ? "" : "_" )
+ jo.get_string( "var_name" );
}
if( jo.has_member( "u_val" ) ) {
type = var_type::u;
if( name.empty() ) {
name = get_talk_varname( jo, "u_val", false, empty );
}
} else if( jo.has_member( "npc_val" ) ) {
type = var_type::npc;
if( name.empty() ) {
name = get_talk_varname( jo, "npc_val", false, empty );
}
} else if( jo.has_member( "global_val" ) ) {
type = var_type::global;
if( name.empty() ) {
name = get_talk_varname( jo, "global_val", false, empty );
}
} else if( jo.has_member( "faction_val" ) ) {
type = var_type::faction;
if( name.empty() ) {
name = get_talk_varname( jo, "faction_val", false, empty );
}
} else if( jo.has_member( "party_val" ) ) {
type = var_type::party;
if( name.empty() ) {
name = get_talk_varname( jo, "party_val", false, empty );
}
} else {
jo.throw_error( "Invalid variable type." );
}
return var_info( type, name, default_val );
}
void write_var_value( var_type type, std::string name, talker *talk, std::string value )
{
global_variables &globvars = get_globals();
switch( type ) {
case var_type::global:
globvars.set_global_value( name, value );
break;
case var_type::u:
case var_type::npc:
talk->set_value( name, value );
break;
case var_type::faction:
debugmsg( "Not implemented yet." );
break;
case var_type::party:
debugmsg( "Not implemented yet." );
break;
default:
debugmsg( "Invalid type." );
break;
}
}
static bodypart_id get_bp_from_str( const std::string &ctxt )
{
bodypart_id bid = bodypart_str_id::NULL_ID();
if( !ctxt.empty() ) {
bid = bodypart_id( ctxt );
if( !bid.is_valid() ) {
bid = bodypart_str_id::NULL_ID();
}
}
return bid;
}
template<class T>
void read_condition( const JsonObject &jo, const std::string &member_name,
std::function<bool( const T & )> &condition, bool default_val )
{
const auto null_function = [default_val]( const T & ) {
return default_val;
};
if( !jo.has_member( member_name ) ) {
condition = null_function;
} else if( jo.has_string( member_name ) ) {
const std::string type = jo.get_string( member_name );
conditional_t<T> sub_condition( type );
condition = [sub_condition]( const T & d ) {
return sub_condition( d );
};
} else if( jo.has_object( member_name ) ) {
JsonObject con_obj = jo.get_object( member_name );
conditional_t<T> sub_condition( con_obj );
condition = [sub_condition]( const T & d ) {
return sub_condition( d );
};
} else {
jo.throw_error_at( member_name, "invalid condition syntax" );
}
}
template<class T>
void conditional_t<T>::set_has_any_trait( const JsonObject &jo, const std::string &member,
bool is_npc )
{
std::vector<trait_id> traits_to_check;
for( auto&& f : jo.get_string_array( member ) ) { // *NOPAD*
traits_to_check.emplace_back( f );
}
condition = [traits_to_check, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
for( const auto &trait : traits_to_check ) {
if( actor->has_trait( trait ) ) {
return true;
}
}
return false;
};
}
template<class T>
void conditional_t<T>::set_has_trait( const JsonObject &jo, const std::string &member, bool is_npc )
{
const std::string &trait_to_check = jo.get_string( member );
condition = [trait_to_check, is_npc]( const T & d ) {
return d.actor( is_npc )->has_trait( trait_id( trait_to_check ) );
};
}
template<class T>
void conditional_t<T>::set_has_flag( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const json_character_flag &trait_flag_to_check = json_character_flag( jo.get_string( member ) );
condition = [trait_flag_to_check, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
if( trait_flag_to_check == json_flag_MUTATION_THRESHOLD ) {
return actor->crossed_threshold();
}
return actor->has_flag( trait_flag_to_check );
};
}
template<class T>
void conditional_t<T>::set_has_activity( bool is_npc )
{
condition = [is_npc]( const T & d ) {
return d.actor( is_npc )->has_activity();
};
}
template<class T>
void conditional_t<T>::set_is_riding( bool is_npc )
{
condition = [is_npc]( const T & d ) {
return d.actor( is_npc )->is_mounted();
};
}
template<class T>
void conditional_t<T>::set_npc_has_class( const JsonObject &jo, bool is_npc )
{
const std::string &class_to_check = jo.get_string( "npc_has_class" );
condition = [class_to_check, is_npc]( const T & d ) {
return d.actor( is_npc )->is_myclass( npc_class_id( class_to_check ) );
};
}
template<class T>
void conditional_t<T>::set_u_has_mission( const JsonObject &jo )
{
const std::string &u_mission = jo.get_string( "u_has_mission" );
condition = [u_mission]( const T & ) {
for( mission *miss_it : get_avatar().get_active_missions() ) {
if( miss_it->mission_id() == mission_type_id( u_mission ) ) {
return true;
}
}
return false;
};
}
template<class T>
void conditional_t<T>::set_u_monsters_in_direction( const JsonObject &jo )
{
const std::string &dir = jo.get_string( "u_monsters_in_direction" );
condition = [dir]( const T & ) {
//This string_to_enum function is defined in widget.h. Should it be moved?
const int card_dir = static_cast<int>( io::string_to_enum<cardinal_direction>( dir ) );
int monster_count = get_avatar().get_mon_visible().unique_mons[card_dir].size();
return monster_count > 0;
};
}
template<class T>
void conditional_t<T>::set_u_safe_mode_trigger( const JsonObject &jo )
{
const std::string &dir = jo.get_string( "u_safe_mode_trigger" );
condition = [dir]( const T & ) {
//This string_to_enum function is defined in widget.h. Should it be moved?
const int card_dir = static_cast<int>( io::string_to_enum<cardinal_direction>( dir ) );
return get_avatar().get_mon_visible().dangerous[card_dir];
};
}
template<class T>
void conditional_t<T>::set_has_strength( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<T> iov = get_int_or_var<T>( jo, member );
condition = [iov, is_npc]( const T & d ) {
return d.actor( is_npc )->str_cur() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_has_dexterity( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<T> iov = get_int_or_var<T>( jo, member );
condition = [iov, is_npc]( const T & d ) {
return d.actor( is_npc )->dex_cur() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_has_intelligence( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<T> iov = get_int_or_var<T>( jo, member );
condition = [iov, is_npc]( const T & d ) {
return d.actor( is_npc )->int_cur() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_has_perception( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<T> iov = get_int_or_var<T>( jo, member );
condition = [iov, is_npc]( const T & d ) {
return d.actor( is_npc )->per_cur() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_has_hp( const JsonObject &jo, const std::string &member, bool is_npc )
{
int_or_var<T> iov = get_int_or_var<T>( jo, member );
cata::optional<bodypart_id> bp;
optional( jo, false, "bodypart", bp );
condition = [iov, bp, is_npc]( const T & d ) {
bodypart_id bid = bp.value_or( get_bp_from_str( d.reason ) );
return d.actor( is_npc )->get_cur_hp( bid ) >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_is_wearing( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const itype_id item_id( jo.get_string( member ) );
condition = [item_id, is_npc]( const T & d ) {
return d.actor( is_npc )->is_wearing( item_id );
};
}
template<class T>
void conditional_t<T>::set_has_item( const JsonObject &jo, const std::string &member, bool is_npc )
{
const itype_id item_id( jo.get_string( member ) );
condition = [item_id, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
return actor->charges_of( item_id ) > 0 || actor->has_amount( item_id, 1 );
};
}
template<class T>
void conditional_t<T>::set_has_items( const JsonObject &jo, const std::string &member, bool is_npc )
{
JsonObject has_items = jo.get_object( member );
if( !has_items.has_string( "item" ) || ( !has_items.has_int( "count" ) &&
!has_items.has_int( "charges" ) ) ) {
condition = []( const T & ) {
return false;
};
} else {
const itype_id item_id( has_items.get_string( "item" ) );
int count = 0;
if( has_items.has_int( "count" ) ) {
count = has_items.get_int( "count" );
}
int charges = 0;
if( has_items.has_int( "charges" ) ) {
charges = has_items.get_int( "charges" );
}
condition = [item_id, count, charges, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
if( charges == 0 && item::count_by_charges( item_id ) ) {
return actor->has_charges( item_id, count, true );
}
if( charges > 0 && count == 0 ) {
return actor->has_charges( item_id, charges, true );
}
bool has_enough_charges = true;
if( charges > 0 ) {
has_enough_charges = actor->has_charges( item_id, charges, true );
}
return has_enough_charges && actor->has_amount( item_id, count );
};
}
}
template<class T>
void conditional_t<T>::set_has_item_with_flag( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const std::string flag( jo.get_string( member ) );
condition = [flag, is_npc]( const T & d ) {
return d.actor( is_npc )->has_item_with_flag( flag_id( flag ) );
};
}
template<class T>
void conditional_t<T>::set_has_item_category( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const item_category_id category_id = item_category_id( jo.get_string( member ) );
size_t count = 1;
if( jo.has_int( "count" ) ) {
int tcount = jo.get_int( "count" );
if( tcount > 1 && tcount < INT_MAX ) {
count = static_cast<size_t>( tcount );
}
}
condition = [category_id, count, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
const auto items_with = actor->const_items_with( [category_id]( const item & it ) {
return it.get_category_shallow().get_id() == category_id;
} );
return items_with.size() >= count;
};
}
template<class T>
void conditional_t<T>::set_has_bionics( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const std::string bionics_id = jo.get_string( member );
condition = [bionics_id, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
if( bionics_id == "ANY" ) {
return actor->num_bionics() > 0 || actor->has_max_power();
}
return actor->has_bionic( bionic_id( bionics_id ) );
};
}
template<class T>
void conditional_t<T>::set_has_effect( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const std::string &effect_id = jo.get_string( member );
cata::optional<int> intensity;
cata::optional<bodypart_id> bp;
optional( jo, false, "intensity", intensity );
optional( jo, false, "bodypart", bp );
condition = [effect_id, intensity, bp, is_npc]( const T & d ) {
bodypart_id bid = bp.value_or( get_bp_from_str( d.reason ) );
effect target = d.actor( is_npc )->get_effect( efftype_id( effect_id ), bid );
return !target.is_null() && intensity.value_or( -1 ) <= target.get_intensity();
};
}
template<class T>
void conditional_t<T>::set_need( const JsonObject &jo, const std::string &member, bool is_npc )
{
const std::string &need = jo.get_string( member );
int_or_var<T> iov;
if( jo.has_int( "amount" ) ) {
iov.min.int_val = jo.get_int( "amount" );
} else if( jo.has_object( "amount" ) ) {
iov = get_int_or_var<T>( jo, "amount" );
} else if( jo.has_string( "level" ) ) {
const std::string &level = jo.get_string( "level" );
auto flevel = fatigue_level_strs.find( level );
if( flevel != fatigue_level_strs.end() ) {
iov.min.int_val = static_cast<int>( flevel->second );
}
}
condition = [need, iov, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
int amount = iov.evaluate( d );
return ( actor->get_fatigue() > amount && need == "fatigue" ) ||
( actor->get_hunger() > amount && need == "hunger" ) ||
( actor->get_thirst() > amount && need == "thirst" );
};
}
template<class T>
void conditional_t<T>::set_at_om_location( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const std::string &location = jo.get_string( member );
condition = [location, is_npc]( const T & d ) {
const tripoint_abs_omt omt_pos = d.actor( is_npc )->global_omt_location();
const oter_id &omt_ter = overmap_buffer.ter( omt_pos );
const std::string &omt_str = omt_ter.id().c_str();
if( location == "FACTION_CAMP_ANY" ) {
cata::optional<basecamp *> bcp = overmap_buffer.find_camp( omt_pos.xy() );
if( bcp ) {
return true;
}
// legacy check
return omt_str.find( "faction_base_camp" ) != std::string::npos;
} else if( location == "FACTION_CAMP_START" ) {
return !recipe_group::get_recipes_by_id( "all_faction_base_types", omt_str ).empty();
} else {
return oter_no_dir( omt_ter ) == location;
}
};
}
template<class T>
void conditional_t<T>::set_near_om_location( const JsonObject &jo, const std::string &member,
bool is_npc )
{
const std::string &location = jo.get_string( member );
const int_or_var<T> range = get_int_or_var<T>( jo, "range", false, 1 );
condition = [location, range, is_npc]( const T & d ) {
const tripoint_abs_omt omt_pos = d.actor( is_npc )->global_omt_location();
for( const tripoint_abs_omt &curr_pos : points_in_radius( omt_pos,
range.evaluate( d ) ) ) {
const oter_id &omt_ter = overmap_buffer.ter( curr_pos );
const std::string &omt_str = omt_ter.id().c_str();
if( location == "FACTION_CAMP_ANY" ) {
cata::optional<basecamp *> bcp = overmap_buffer.find_camp( curr_pos.xy() );
if( bcp ) {
return true;
}
// legacy check
if( omt_str.find( "faction_base_camp" ) != std::string::npos ) {
return true;
}
} else if( location == "FACTION_CAMP_START" &&
!recipe_group::get_recipes_by_id( "all_faction_base_types", omt_str ).empty() ) {
return true;
} else {
if( oter_no_dir( omt_ter ) == location ) {
return true;
}
}
}
// should never get here this is for safety
return false;
};
}
template<class T>
void conditional_t<T>::set_has_var( const JsonObject &jo, const std::string &member, bool is_npc )
{
int_or_var<dialogue> empty;
const std::string var_name = get_talk_varname( jo, member, false, empty );
const std::string &value = jo.has_member( "value" ) ? jo.get_string( "value" ) : std::string();
const bool time_check = jo.has_member( "time" ) && jo.get_bool( "time" );
condition = [var_name, value, time_check, is_npc]( const T & d ) {
const talker *actor = d.actor( is_npc );
if( time_check ) {
return !actor->get_value( var_name ).empty();
}
return actor->get_value( var_name ) == value;
};
}
template<class T>
void conditional_t<T>::set_compare_var( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<dialogue> empty;
const std::string var_name = get_talk_varname( jo, member, false, empty );
const std::string &op = jo.get_string( "op" );
int_or_var<T> iov = get_int_or_var<T>( jo, "value" );
condition = [var_name, op, iov, is_npc]( const T & d ) {
int stored_value = 0;
int value = iov.evaluate( d );
const std::string &var = d.actor( is_npc )->get_value( var_name );
if( !var.empty() ) {
stored_value = std::stoi( var );
}
if( op == "==" ) {
return stored_value == value;
} else if( op == "!=" ) {
return stored_value != value;
} else if( op == "<=" ) {
return stored_value <= value;
} else if( op == ">=" ) {
return stored_value >= value;
} else if( op == "<" ) {
return stored_value < value;
} else if( op == ">" ) {
return stored_value > value;
}
return false;
};
}
template<class T>
void conditional_t<T>::set_compare_time_since_var( const JsonObject &jo, const std::string &member,
bool is_npc )
{
int_or_var<dialogue> empty;
const std::string var_name = get_talk_varname( jo, member, false, empty );
const std::string &op = jo.get_string( "op" );
const int value = to_turns<int>( read_from_json_string<time_duration>( jo.get_member( "time" ),
time_duration::units ) );
condition = [var_name, op, value, is_npc]( const T & d ) {
int stored_value = 0;
const std::string &var = d.actor( is_npc )->get_value( var_name );
if( var.empty() ) {
return false;
} else {
stored_value = std::stoi( var );
}
stored_value += value;
int now = to_turn<int>( calendar::turn );
if( op == "==" ) {
return stored_value == now;
} else if( op == "!=" ) {
return stored_value != now;
} else if( op == "<=" ) {
return now <= stored_value;
} else if( op == ">=" ) {
return now >= stored_value;
} else if( op == "<" ) {
return now < stored_value;
} else if( op == ">" ) {
return now > stored_value;
}
return false;
};
}
template<class T>
void conditional_t<T>::set_npc_role_nearby( const JsonObject &jo )
{
const std::string &role = jo.get_string( "npc_role_nearby" );
condition = [role]( const T & d ) {
const std::vector<npc *> available = g->get_npcs_if( [&]( const npc & guy ) {
return d.actor( false )->posz() == guy.posz() && guy.companion_mission_role_id == role &&
( rl_dist( d.actor( false )->pos(), guy.pos() ) <= 48 );
} );
return !available.empty();
};
}
template<class T>
void conditional_t<T>::set_npc_allies( const JsonObject &jo )
{
int_or_var<T> iov = get_int_or_var<T>( jo, "npc_allies" );
condition = [iov]( const T & d ) {
return g->allies().size() >= static_cast<std::vector<npc *>::size_type>( iov.evaluate( d ) );
};
}
template<class T>
void conditional_t<T>::set_npc_allies_global( const JsonObject &jo )
{
int_or_var<T> iov = get_int_or_var<T>( jo, "npc_allies_global" );
condition = [iov]( const T & d ) {
const auto all_npcs = overmap_buffer.get_overmap_npcs();
const size_t count = std::count_if( all_npcs.begin(),
all_npcs.end(), []( const shared_ptr_fast<npc> &ptr ) {
return ptr.get()->is_player_ally() && !ptr.get()->hallucination && !ptr.get()->is_dead();
} );
return count >= static_cast<size_t>( iov.evaluate( d ) );
};
}
template<class T>
void conditional_t<T>::set_u_has_cash( const JsonObject &jo )
{
int_or_var<T> iov = get_int_or_var<T>( jo, "u_has_cash" );
condition = [iov]( const T & d ) {
return d.actor( false )->cash() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_u_are_owed( const JsonObject &jo )
{
int_or_var<T> iov = get_int_or_var<T>( jo, "u_are_owed" );
condition = [iov]( const T & d ) {
return d.actor( true )->debt() >= iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_npc_aim_rule( const JsonObject &jo, bool is_npc )
{
const std::string &setting = jo.get_string( "npc_aim_rule" );
condition = [setting, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "aim_rule", setting );
};
}
template<class T>
void conditional_t<T>::set_npc_engagement_rule( const JsonObject &jo, bool is_npc )
{
const std::string &setting = jo.get_string( "npc_engagement_rule" );
condition = [setting, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "engagement_rule", setting );
};
}
template<class T>
void conditional_t<T>::set_npc_cbm_reserve_rule( const JsonObject &jo, bool is_npc )
{
const std::string &setting = jo.get_string( "npc_cbm_reserve_rule" );
condition = [setting, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "cbm_reserve_rule", setting );
};
}
template<class T>
void conditional_t<T>::set_npc_cbm_recharge_rule( const JsonObject &jo, bool is_npc )
{
const std::string &setting = jo.get_string( "npc_cbm_recharge_rule" );
condition = [setting, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "cbm_recharge_rule", setting );
};
}
template<class T>
void conditional_t<T>::set_npc_rule( const JsonObject &jo, bool is_npc )
{
std::string rule = jo.get_string( "npc_rule" );
condition = [rule, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "ally_rule", rule );
};
}
template<class T>
void conditional_t<T>::set_npc_override( const JsonObject &jo, bool is_npc )
{
std::string rule = jo.get_string( "npc_override" );
condition = [rule, is_npc]( const T & d ) {
return d.actor( is_npc )->has_ai_rule( "ally_override", rule );
};
}
template<class T>
void conditional_t<T>::set_days_since( const JsonObject &jo )
{
int_or_var<T> iov = get_int_or_var<T>( jo, "days_since_cataclysm" );
condition = [iov]( const T & d ) {
return calendar::turn >= calendar::start_of_cataclysm + 1_days * iov.evaluate( d );
};
}
template<class T>
void conditional_t<T>::set_is_season( const JsonObject &jo )
{
std::string season_name = jo.get_string( "is_season" );
condition = [season_name]( const T & ) {
const season_type season = season_of_year( calendar::turn );
return ( season == SPRING && season_name == "spring" ) ||
( season == SUMMER && season_name == "summer" ) ||
( season == AUTUMN && season_name == "autumn" ) ||
( season == WINTER && season_name == "winter" );
};
}
template<class T>
void conditional_t<T>::set_mission_goal( const JsonObject &jo, bool is_npc )
{
std::string mission_goal_str = jo.get_string( "mission_goal" );
condition = [mission_goal_str, is_npc]( const T & d ) {
mission *miss = d.actor( is_npc )->selected_mission();
if( !miss ) {
return false;
}
const mission_goal mgoal = io::string_to_enum<mission_goal>( mission_goal_str );
return miss->get_type().goal == mgoal;
};
}
template<class T>
void conditional_t<T>::set_is_gender( bool is_male, bool is_npc )
{
condition = [is_male, is_npc]( const T & d ) {
return d.actor( is_npc )->is_male() == is_male;
};
}
template<class T>
void conditional_t<T>::set_no_assigned_mission()
{
condition = []( const T & d ) {
return d.missions_assigned.empty();
};
}
template<class T>
void conditional_t<T>::set_has_assigned_mission()
{
condition = []( const T & d ) {
return d.missions_assigned.size() == 1;
};
}
template<class T>
void conditional_t<T>::set_has_many_assigned_missions()
{
condition = []( const T & d ) {
return d.missions_assigned.size() >= 2;
};
}
template<class T>
void conditional_t<T>::set_no_available_mission( bool is_npc )
{
condition = [is_npc]( const T & d ) {
return d.actor( is_npc )->available_missions().empty();
};
}
template<class T>
void conditional_t<T>::set_has_available_mission( bool is_npc )
{
condition = [is_npc]( const T & d ) {
return d.actor( is_npc )->available_missions().size() == 1;
};
}
template<class T>
void conditional_t<T>::set_has_many_available_missions( bool is_npc )
{
condition = [is_npc]( const T & d ) {
return d.actor( is_npc )->available_missions().size() >= 2;
};
}
template<class T>
void conditional_t<T>::set_mission_complete( bool is_npc )
{
condition = [is_npc]( const T & d ) {
mission *miss = d.actor( is_npc )->selected_mission();
return miss && miss->is_complete( d.actor( is_npc )->getID() );
};
}