forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requirements.cpp
1704 lines (1540 loc) · 62.7 KB
/
requirements.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 "requirements.h"
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iterator>
#include <limits>
#include <list>
#include <memory>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include "cata_assert.h"
#include "cata_utility.h"
#include "character.h"
#include "color.h"
#include "debug.h"
#include "debug_menu.h"
#include "enum_traits.h"
#include "generic_factory.h"
#include "inventory.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "json.h"
#include "localized_comparator.h"
#include "make_static.h"
#include "output.h"
#include "point.h"
#include "string_formatter.h"
#include "translations.h"
#include "value_ptr.h"
#include "visitable.h"
static const itype_id itype_UPS( "UPS" );
static const itype_id itype_char_forge( "char_forge" );
static const itype_id itype_crucible( "crucible" );
static const itype_id itype_fire( "fire" );
static const itype_id itype_forge( "forge" );
static const itype_id itype_mold_plastic( "mold_plastic" );
static const itype_id itype_oxy_torch( "oxy_torch" );
static const itype_id itype_press( "press" );
static const itype_id itype_sewing_kit( "sewing_kit" );
static const itype_id itype_welder( "welder" );
static const itype_id itype_welder_crude( "welder_crude" );
static const quality_id qual_CUT( "CUT" );
static const quality_id qual_GLARE( "GLARE" );
static const quality_id qual_KNIT( "KNIT" );
static const quality_id qual_PULL( "PULL" );
static const quality_id qual_SAW_M_FINE( "SAW_M_FINE" );
static const quality_id qual_SEW( "SEW" );
static const trait_id trait_DEBUG_HS( "DEBUG_HS" );
static std::map<requirement_id, requirement_data> requirements_all;
static bool a_satisfies_b( const quality_requirement &a, const quality_requirement &b );
static bool a_satisfies_b( const std::vector<quality_requirement> &a,
const std::vector<quality_requirement> &b );
/** @relates string_id */
template<>
bool string_id<requirement_data>::is_valid() const
{
return requirements_all.count( *this );
}
/** @relates string_id */
template<>
const requirement_data &string_id<requirement_data>::obj() const
{
const auto found = requirements_all.find( *this );
if( found == requirements_all.end() ) {
debugmsg( "Tried to get invalid requirements: %s", c_str() );
static const requirement_data null_requirement{};
return null_requirement;
}
return found->second;
}
std::vector<requirement_data> requirement_data::get_all()
{
std::vector<requirement_data> ret;
ret.reserve( requirements_all.size() );
for( const std::pair<const requirement_id, requirement_data> &pair : requirements_all ) {
ret.push_back( pair.second );
}
return ret;
}
namespace
{
generic_factory<quality> quality_factory( "tool quality" );
} // namespace
void quality::reset()
{
quality_factory.reset();
}
void quality::load_static( const JsonObject &jo, const std::string &src )
{
quality_factory.load( jo, src );
}
void quality::load( const JsonObject &jo, const std::string & )
{
mandatory( jo, was_loaded, "name", name );
for( JsonArray levels : jo.get_array( "usages" ) ) {
const int level = levels.get_int( 0 );
for( const std::string line : levels.get_array( 1 ) ) {
usages.emplace_back( level, line );
}
}
}
/** @relates string_id */
template<>
const quality &string_id<quality>::obj() const
{
return quality_factory.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<quality>::is_valid() const
{
return quality_factory.is_valid( *this );
}
std::string quality_requirement::to_string( const int, const int ) const
{
//~ %1$d: tool count, %2$s: quality requirement name, %3$d: quality level requirement
return string_format( n_gettext( "%1$d tool with %2$s of %3$d or more.",
"%1$d tools with %2$s of %3$d or more.", count ),
count, type.obj().name, level );
}
std::string quality_requirement::to_colored_string() const
{
//~ %1$d: tool count, %2$s: quality requirement name, %3$d: quality level requirement
return string_format( n_gettext( "%1$d tool with <info>%2$s of %3$d</info> or more",
"%1$d tools with <info>%2$s of %3$d</info> or more", count ),
count, type.obj().name, level );
}
bool tool_comp::by_charges() const
{
return count > 0;
}
std::string tool_comp::to_string( const int batch, const int ) const
{
if( by_charges() ) {
int charge_total = count * batch;
//~ %1$s: tool name, %2$d: charge requirement
return string_format( npgettext( "requirement", "%1$s (%2$d charge)", "%1$s (%2$d charges)",
charge_total ),
item::nname( type ), charge_total );
} else {
return item::nname( type, std::abs( count ) );
}
}
std::string item_comp::to_string( const int batch, const int avail ) const
{
const int c = std::abs( count ) * batch;
const itype *type_ptr = item::find_type( type );
if( type_ptr->count_by_charges() ) {
// Count-by-charge
if( avail == item::INFINITE_CHARGES ) {
//~ %1$s: item name, %2$d: charge requirement
return string_format( npgettext( "requirement", "%2$d %1$s (have infinite)",
"%2$d %1$s (have infinite)",
c ),
type_ptr->nname( 1 ), c );
} else if( avail > 0 ) {
//~ %1$s: item name, %2$d: charge requirement, %3%d: available charges
return string_format( npgettext( "requirement", "%2$d %1$s (have %3$d)",
"%2$d %1$s (have %3$d)", c ),
type_ptr->nname( 1 ), c, avail );
} else {
//~ %1$s: item name, %2$d: charge requirement
return string_format( npgettext( "requirement", "%2$d %1$s", "%2$d %1$s", c ),
type_ptr->nname( 1 ), c );
}
} else {
if( avail == item::INFINITE_CHARGES ) {
//~ %1$s: item name, %2$d: required count
return string_format( npgettext( "requirement", "%2$d %1$s (have infinite)",
"%2$d %1$s (have infinite)",
c ),
type_ptr->nname( c ), c );
} else if( avail > 0 ) {
//~ %1$s: item name, %2$d: required count, %3%d: available count
return string_format( npgettext( "requirement", "%2$d %1$s (have %3$d)",
"%2$d %1$s (have %3$d)", c ),
type_ptr->nname( c ), c, avail );
} else {
//~ %1$s: item name, %2$d: required count
return string_format( npgettext( "requirement", "%2$d %1$s", "%2$d %1$s", c ),
type_ptr->nname( c ), c );
}
}
}
void quality_requirement::load( const JsonValue &value )
{
const JsonObject quality_data = value.get_object();
type = quality_id( quality_data.get_string( "id" ) );
level = quality_data.get_int( "level", 1 );
count = quality_data.get_int( "amount", 1 );
if( count <= 0 ) {
quality_data.throw_error_at( "amount", "quality amount must be a positive number" );
}
// Note: level is not checked, negative values and 0 are allow, see butchering quality.
}
void quality_requirement::dump( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "id", type );
if( level != 1 ) {
jsout.member( "level", level );
}
if( count != 1 ) {
jsout.member( "amount", count );
}
jsout.end_object();
}
void tool_comp::load( const JsonValue &value )
{
if( value.test_string() ) {
// constructions uses this format: [ "tool", ... ]
value.read( type, true );
count = -1;
} else {
JsonArray comp = value.get_array();
comp.read( 0, type, true );
count = comp.get_int( 1 );
requirement = comp.size() > 2 && comp.get_string( 2 ) == "LIST";
}
if( count == 0 ) {
value.throw_error( "tool count must not be 0" );
}
// Note: negative count means charges (of the tool) should be consumed
}
void tool_comp::dump( JsonOut &jsout ) const
{
jsout.start_array();
jsout.write( type );
jsout.write( count );
if( requirement ) {
jsout.write( "LIST" );
}
jsout.end_array();
}
void item_comp::load( const JsonValue &value )
{
JsonArray comp = value.get_array();
comp.read( 0, type, true );
count = comp.get_int( 1 );
size_t handled = 2;
while( comp.size() > handled ) {
const std::string &flag = comp.get_string( handled++ );
if( flag == "NO_RECOVER" ) {
recoverable = false;
} else if( flag == "LIST" ) {
requirement = true;
}
}
if( count <= 0 ) {
value.throw_error( "item count must be a positive number" );
}
}
void item_comp::dump( JsonOut &jsout ) const
{
jsout.start_array();
jsout.write( type );
jsout.write( count );
if( !recoverable ) {
jsout.write( "NO_RECOVER" );
}
if( requirement ) {
jsout.write( "LIST" );
}
jsout.end_array();
}
template<typename T>
void requirement_data::load_obj_list( const JsonArray &jsarr, std::vector< std::vector<T> > &objs )
{
for( const JsonValue entry : jsarr ) {
if( entry.test_array() ) {
std::vector<T> choices;
for( const JsonValue subentry : entry.get_array() ) {
choices.push_back( T() );
choices.back().load( subentry );
}
if( !choices.empty() ) {
objs.push_back( choices );
}
} else {
// tool qualities don't normally use a list of alternatives
// each quality is mandatory.
objs.push_back( std::vector<T>( 1 ) );
objs.back().back().load( entry );
}
}
}
requirement_data requirement_data::operator*( unsigned scalar ) const
{
requirement_data res = *this;
for( auto &group : res.components ) {
for( item_comp &e : group ) {
e.count = std::max( e.count * static_cast<int>( scalar ), -1 );
}
}
for( auto &group : res.tools ) {
for( tool_comp &e : group ) {
e.count = std::max( e.count * static_cast<int>( scalar ), -1 );
}
}
return res;
}
static bool a_satisfies_b( const quality_requirement &a, const quality_requirement &b )
{
return a.type == b.type && a.level >= b.level
&& a.requirement == b.requirement && a.count == b.count;
}
static bool a_satisfies_b( const std::vector<quality_requirement> &a,
const std::vector<quality_requirement> &b )
{
// every b_x is satisfied by some a_x
for( const quality_requirement &b_x : b ) {
bool satisfied = false;
for( const quality_requirement &a_x : a ) {
if( a_satisfies_b( a_x, b_x ) ) {
satisfied = true;
break;
}
}
if( !satisfied ) {
return false;
}
}
return true;
}
requirement_data requirement_data::operator+( const requirement_data &rhs ) const
{
requirement_data res = *this;
res.components.insert( res.components.end(), rhs.components.begin(), rhs.components.end() );
res.tools.insert( res.tools.end(), rhs.tools.begin(), rhs.tools.end() );
/*
TODO: what is / isn't implemented:
I will represent type by letter and level by number, always true: count = 1 && requirement = false
A1 then has type A with level 1, (count = 1, requirement = false)
X) [implemented ? "x" else " "] What's required -> How should it be displayed
1) [x] A1 && A1 -> A1
2) [x] A1 && A2 -> A2
3) [x] (A1 || B1) && A1 -> A1
4) [ ] A1 || A2 -> A1
5) [ ] (A1 || B1) && A2 -> A2
6) [ ] (A1 || B1 || C1) || (A1 || B1) -> A1 || B1 || C1
Note: (1) covers most cases, (2) probably the rest, (3..6) probably isn't anywhere?
It all takes O(n^2), but that's acceptable since n is very small.
@Brambor
*/
for( const std::vector<quality_requirement> &new_quality : rhs.qualities ) {
bool add = true;
for( std::vector<quality_requirement> &old_quality : res.qualities ) {
if( a_satisfies_b( new_quality, old_quality ) ) {
add = false;
old_quality = new_quality;
break;
} else if( a_satisfies_b( old_quality, new_quality ) ) {
add = false;
break;
}
}
if( add ) {
res.qualities.emplace_back( new_quality );
}
}
// combined result is temporary which caller could store via @ref save_requirement
res.id_ = requirement_id::NULL_ID();
// TODO: combine other requirements
// if either operand was blacklisted then their summation should also be
res.blacklisted |= rhs.blacklisted;
return res;
}
requirement_data requirement_data::operator+(
const std::pair<const requirement_id, int> &rhs ) const
{
return *this + *rhs.first * rhs.second;
}
requirement_data requirement_data::operator+( const std::pair<requirement_id, int> &rhs ) const
{
return *this + *rhs.first * rhs.second;
}
void requirement_data::load_requirement( const JsonObject &jsobj, const requirement_id &id,
const bool check_extend )
{
requirement_data req;
requirement_data ext;
if( check_extend && jsobj.has_object( "extend" ) ) {
JsonObject jext = jsobj.get_object( "extend" );
if( jext.has_member( "components" ) ) {
load_obj_list( jext.get_array( "components" ), ext.components );
}
if( jext.has_member( "qualities" ) ) {
load_obj_list( jext.get_array( "qualities" ), ext.qualities );
}
if( jext.has_member( "tools" ) ) {
load_obj_list( jext.get_array( "tools" ), ext.tools );
}
}
if( ext.components.empty() || jsobj.has_member( "components" ) ) {
load_obj_list( jsobj.get_array( "components" ), req.components );
}
if( ext.qualities.empty() || jsobj.has_member( "qualities" ) ) {
load_obj_list( jsobj.get_array( "qualities" ), req.qualities );
}
if( ext.tools.empty() || jsobj.has_member( "tools" ) ) {
load_obj_list( jsobj.get_array( "tools" ), req.tools );
}
if( !id.is_null() ) {
req.id_ = id;
} else if( jsobj.has_string( "id" ) ) {
req.id_ = requirement_id( jsobj.get_string( "id" ) );
} else {
jsobj.throw_error( "id was not specified for requirement" );
}
save_requirement( req, string_id<requirement_data>::NULL_ID(), &ext );
}
void requirement_data::save_requirement( const requirement_data &req, const requirement_id &id,
const requirement_data *extend )
{
requirement_data dup = req;
if( !id.is_null() ) {
dup.id_ = id;
}
if( requirements_all.count( dup.id_ ) == 0 ) {
requirements_all[ dup.id_ ] = dup;
}
requirement_data &r = requirements_all[ dup.id_ ];
if( !dup.components.empty() ) {
r.components.clear();
r.components.insert( r.components.end(), dup.components.begin(), dup.components.end() );
}
if( !dup.tools.empty() ) {
r.tools.clear();
r.tools.insert( r.tools.end(), dup.tools.begin(), dup.tools.end() );
}
if( !dup.qualities.empty() ) {
r.qualities.clear();
r.qualities.insert( r.qualities.end(), dup.qualities.begin(), dup.qualities.end() );
}
if( !!extend ) {
for( unsigned i = 0; i < r.components.size() && i < extend->components.size(); i++ ) {
r.components[i].insert( r.components[i].end(), extend->components[i].begin(),
extend->components[i].end() );
}
for( unsigned i = 0; i < r.tools.size() && i < extend->tools.size(); i++ ) {
r.tools[i].insert( r.tools[i].end(), extend->tools[i].begin(), extend->tools[i].end() );
}
for( unsigned i = 0; i < r.qualities.size() && i < extend->qualities.size(); i++ ) {
r.qualities[i].insert( r.qualities[i].end(), extend->qualities[i].begin(),
extend->qualities[i].end() );
}
}
}
template<typename T>
bool requirement_data::any_marked_available( const std::vector<T> &comps )
{
for( const auto &comp : comps ) {
if( comp.available == available_status::a_true ) {
return true;
}
}
return false;
}
template<typename T>
std::string requirement_data::print_all_objs( const std::string &header,
const std::vector< std::vector<T> > &objs )
{
std::string buffer;
for( const auto &list : objs ) {
if( !buffer.empty() ) {
buffer += std::string( "\n" ) + _( "and " );
}
std::vector<std::string> alternatives;
std::transform( list.begin(), list.end(), std::back_inserter( alternatives ),
[]( const T & t ) {
return t.to_string();
} );
std::sort( alternatives.begin(), alternatives.end(), localized_compare );
buffer += join( alternatives, _( " or " ) );
}
if( buffer.empty() ) {
return std::string();
}
return header + "\n" + buffer + "\n";
}
std::string requirement_data::list_all() const
{
std::string buffer;
buffer += print_all_objs( _( "These tools are required:" ), tools );
buffer += print_all_objs( _( "These tools are required:" ), qualities );
buffer += print_all_objs( _( "These components are required:" ), components );
return buffer;
}
template<typename T>
std::string requirement_data::print_missing_objs( const std::string &header,
const std::vector< std::vector<T> > &objs )
{
std::string separator_and = _( "and " );
std::string separator_or = _( " or " );
std::string buffer;
for( const auto &list : objs ) {
if( any_marked_available( list ) ) {
continue;
}
if( !buffer.empty() ) {
buffer += std::string( "\n" ) + separator_and;
}
for( auto it = list.begin(); it != list.end(); ++it ) {
if( it != list.begin() ) {
buffer += separator_or;
}
buffer += it->to_string();
}
}
if( buffer.empty() ) {
return std::string();
}
return header + "\n" + buffer + "\n";
}
std::string requirement_data::list_missing() const
{
std::string buffer;
buffer += print_missing_objs( _( "These tools are missing:" ), tools );
buffer += print_missing_objs( _( "These tools are missing:" ), qualities );
buffer += print_missing_objs( _( "These components are missing:" ), components );
return buffer;
}
void quality_requirement::check_consistency( const std::string &display_name ) const
{
if( !type.is_valid() ) {
debugmsg( "Unknown quality %s in %s", type.c_str(), display_name );
}
}
void component::check_consistency( const std::string &display_name ) const
{
if( !item::type_is_defined( type ) ) {
debugmsg( "%s in %s is not a valid item template", type.c_str(), display_name );
}
}
template<typename T>
void requirement_data::check_consistency( const std::vector< std::vector<T> > &vec,
const std::string &display_name )
{
for( const auto &list : vec ) {
for( const auto &comp : list ) {
if( comp.requirement ) {
debugmsg( "Finalization failed to inline %s in %s", comp.type.c_str(), display_name );
}
comp.check_consistency( display_name );
}
}
}
const std::map<requirement_id, requirement_data> &requirement_data::all()
{
return requirements_all;
}
void requirement_data::check_consistency()
{
for( const auto &r : all() ) {
check_consistency( r.second.tools, r.first.str() );
check_consistency( r.second.components, r.first.str() );
check_consistency( r.second.qualities, r.first.str() );
}
}
template <typename T>
void inline_requirements( std::vector<std::vector<T>> &list,
const std::function<const std::vector<std::vector<T>> & ( const requirement_data & )> &getter )
{
// add a single component to the vector. If component already exists, chooses min count
const auto add_component = []( const T & comp, std::vector<T> &accum ) {
auto iter = std::find_if( accum.begin(), accum.end(), [&]( const T & req ) {
return !req.requirement && req.type == comp.type;
} );
if( iter == accum.end() ) {
accum.push_back( comp ); // component doesn't exist yet, adding it
} else {
// this is the list of alternatives (a OR b OR c), so if there are two identical components
// with the different counts as an alternatives, the one with the lowest count is selected
iter->count = std::min( comp.count, iter->count );
}
};
// if `comp` is a component, adds it to the `accum`
// if it's a requirement, recursively expands it into simple components and adds them to the vector
const auto inline_one = [&]( const T & comp, std::vector<T> &accum ) {
std::set<requirement_id> already_nested;
std::list<requirement_id> stack;
std::function<void( const T &comp )> rec;
rec = [&]( const T & comp ) {
// add simple component to the vector
if( !comp.requirement ) {
add_component( comp, accum );
return;
}
// otherwise expand component as requirement
const requirement_id r( comp.type.str() );
if( !r.is_valid() ) {
debugmsg( "Tried to inline unknown requirement %s", r.c_str() );
return;
}
// stack just holds the current path of inlining for debug purposes
stack.push_back( r );
if( already_nested.count( r ) ) {
// print debug msg and skip just this one requirement
debugmsg( "Tried to inline requirement %s which forms a cycle: %s",
r.c_str(), debug_menu::iterable_to_string( stack, " -> ", []( const requirement_id & r ) {
return r.str();
} ) );
stack.pop_back();
return;
}
already_nested.insert( r );
const requirement_data &req = r.obj();
const requirement_data multiplied = req * comp.count;
const std::vector<std::vector<T>> &to_inline = getter( multiplied );
if( !to_inline.empty() ) {
const std::vector<T> &nested = to_inline.front();
for( const T &nested_comp : nested ) {
rec( nested_comp );
}
}
already_nested.erase( r );
stack.pop_back();
};
rec( comp );
};
for( std::vector<T> &vec : list ) {
const std::vector<T> vec_copy = vec;
vec.clear();
for( const T &comp : vec_copy ) {
inline_one( comp, vec );
}
}
}
void requirement_data::finalize()
{
for( auto &r : const_cast<std::map<requirement_id, requirement_data> &>( all() ) ) {
inline_requirements<tool_comp>( r.second.tools, []( const requirement_data & d ) -> const auto & {
return d.get_tools();
} );
inline_requirements<item_comp>( r.second.components,
[]( const requirement_data & d ) -> const auto & {
return d.get_components();
} );
requirement_data::alter_tool_comp_vector &vec = r.second.tools;
for( auto &list : vec ) {
std::vector<tool_comp> new_list;
for( tool_comp &comp : list ) {
const auto replacements = item_controller->subtype_replacement( comp.type );
for( const auto &replaced_type : replacements ) {
new_list.emplace_back( replaced_type, comp.count );
}
}
list = new_list;
}
}
}
void requirement_data::reset()
{
requirements_all.clear();
}
std::vector<std::string> requirement_data::get_folded_components_list( int width, nc_color col,
const read_only_visitable &crafting_inv, const std::function<bool( const item & )> &filter,
int batch,
const std::string &hilite, requirement_display_flags flags ) const
{
std::vector<std::string> out_buffer;
if( components.empty() ) {
return out_buffer;
}
out_buffer.push_back( colorize( _( "Components required:" ), col ) );
std::vector<std::string> folded_buffer =
get_folded_list( width, crafting_inv, filter, components, batch, hilite, flags );
out_buffer.insert( out_buffer.end(), folded_buffer.begin(), folded_buffer.end() );
return out_buffer;
}
template<typename T>
std::vector<std::string> requirement_data::get_folded_list( int width,
const read_only_visitable &crafting_inv, const std::function<bool( const item & )> &filter,
const std::vector< std::vector<T> > &objs, int batch, const std::string &hilite,
requirement_display_flags flags ) const
{
// hack: ensure 'cached' availability is up to date
can_make_with_inventory( crafting_inv, filter );
const bool no_unavailable =
static_cast<bool>( flags & requirement_display_flags::no_unavailable );
std::vector<std::string> out_buffer;
for( const auto &comp_list : objs ) {
const bool has_one = any_marked_available( comp_list );
std::vector<std::string> list_as_string;
std::vector<std::string> list_as_string_unavailable;
std::vector<std::string> buffer_has;
for( const T &component : comp_list ) {
nc_color color = component.get_color( has_one, crafting_inv, filter, batch );
const std::string color_tag = get_tag_from_color( color );
int qty = 0;
if( component.get_component_type() == component_type::ITEM ) {
const itype_id item_id = itype_id( component.type.str() );
if( item::count_by_charges( item_id ) ) {
qty = crafting_inv.charges_of( item_id, INT_MAX, filter );
} else {
qty = crafting_inv.amount_of( item_id, false, INT_MAX, filter );
}
}
const std::string text = component.to_string( batch, qty );
if( std::find( buffer_has.begin(), buffer_has.end(), text + color_tag ) != buffer_has.end() ) {
continue;
}
if( !hilite.empty() && lcmatch( text, hilite ) ) {
color = yellow_background( color );
}
if( component.has( crafting_inv, filter, batch ) ) {
list_as_string.push_back( colorize( text, color ) );
} else if( !no_unavailable ) {
list_as_string_unavailable.push_back( colorize( text, color ) );
}
buffer_has.push_back( text + color_tag );
}
std::sort( list_as_string.begin(), list_as_string.end(), localized_compare );
std::sort( list_as_string_unavailable.begin(), list_as_string_unavailable.end(),
localized_compare );
list_as_string.insert( list_as_string.end(), list_as_string_unavailable.begin(),
list_as_string_unavailable.end() );
const std::string separator = colorize( _( " OR " ), c_white );
const std::string unfolded = join( list_as_string, separator );
std::vector<std::string> folded = foldstring( unfolded, width - 2 );
for( size_t i = 0; i < folded.size(); i++ ) {
if( i == 0 ) {
out_buffer.push_back( std::string( "> " ).append( folded[i] ) );
} else {
out_buffer.push_back( std::string( " " ).append( folded[i] ) );
}
}
}
return out_buffer;
}
std::vector<std::string> requirement_data::get_folded_tools_list( int width, nc_color col,
const read_only_visitable &crafting_inv, int batch ) const
{
std::vector<std::string> output_buffer;
output_buffer.push_back( colorize( _( "Tools required:" ), col ) );
if( tools.empty() && qualities.empty() ) {
output_buffer.push_back( colorize( "> ", col ) + colorize( _( "NONE" ), c_green ) );
return output_buffer;
}
std::vector<std::string> folded_qualities = get_folded_list( width, crafting_inv, return_true<item>,
qualities );
output_buffer.insert( output_buffer.end(), folded_qualities.begin(), folded_qualities.end() );
std::vector<std::string> folded_tools = get_folded_list( width, crafting_inv, return_true<item>,
tools,
batch );
output_buffer.insert( output_buffer.end(), folded_tools.begin(), folded_tools.end() );
return output_buffer;
}
bool requirement_data::can_make_with_inventory( const read_only_visitable &crafting_inv,
const std::function<bool( const item & )> &filter, int batch, craft_flags flags ) const
{
if( get_player_character().has_trait( trait_DEBUG_HS ) ) {
return true;
}
bool retval = true;
// All functions must be called to update the available settings in the components.
if( !has_comps( crafting_inv, qualities, return_true<item> ) ) {
retval = false;
}
if( !has_comps( crafting_inv, tools, return_true<item>, batch, flags ) ) {
retval = false;
}
if( !has_comps( crafting_inv, components, filter, batch ) ) {
retval = false;
}
if( !check_enough_materials( crafting_inv, filter, batch ) ) {
retval = false;
}
return retval;
}
template<typename T>
bool requirement_data::has_comps( const read_only_visitable &crafting_inv,
const std::vector< std::vector<T> > &vec,
const std::function<bool( const item & )> &filter,
int batch, craft_flags flags )
{
bool retval = true;
int total_UPS_charges_used = 0;
for( const std::vector<T> &set_of_tools : vec ) {
bool has_tool_in_set = false;
int UPS_charges_used = std::numeric_limits<int>::max();
const std::function<void( int )> use_ups = [ &UPS_charges_used ]( int charges ) {
UPS_charges_used = std::min( UPS_charges_used, charges );
};
for( const T &tool : set_of_tools ) {
if( tool.has( crafting_inv, filter, batch, flags, use_ups ) ) {
tool.available = available_status::a_true;
} else {
// Trying to track down why the crafting tests are failing?
// Uncomment the below to see the group of requirements that are lacking satisfaction
// Add a printf("\n") to the loop above this to separate different groups onto a separate line
// printf( "T: %s ", tool.type.str().c_str() );
tool.available = available_status::a_false;
}
has_tool_in_set = has_tool_in_set || tool.available == available_status::a_true;
}
if( !has_tool_in_set ) {
retval = false;
}
if( UPS_charges_used != std::numeric_limits<int>::max() ) {
total_UPS_charges_used += UPS_charges_used;
}
}
if( total_UPS_charges_used > 0 &&
total_UPS_charges_used > crafting_inv.charges_of( itype_UPS ) ) {
return false;
}
return retval;
}
bool quality_requirement::has(
const read_only_visitable &crafting_inv, const std::function<bool( const item & )> &, int,
craft_flags, const std::function<void( int )> & ) const
{
if( get_player_character().has_trait( trait_DEBUG_HS ) ) {
return true;
}
return crafting_inv.has_quality( type, level, count );
}
nc_color quality_requirement::get_color( bool has_one, const read_only_visitable &,
const std::function<bool( const item & )> &, int ) const
{
if( get_player_character().has_trait( trait_DEBUG_HS ) ||
available == available_status::a_true ) {
return c_green;
}
return has_one ? c_dark_gray : c_red;
}
bool tool_comp::has(
const read_only_visitable &crafting_inv, const std::function<bool( const item & )> &filter,
int batch,
craft_flags flags, const std::function<void( int )> &visitor ) const
{
if( get_player_character().has_trait( trait_DEBUG_HS ) ) {
return true;
}
if( !by_charges() ) {
return crafting_inv.has_tools( type, std::abs( count ), filter );
} else {
int charges_required = count * batch * item::find_type( type )->charge_factor();
// The `type->tool` check excludes items counted by charge used as tools,
// such as water purification tablets.
if( ( flags & craft_flags::start_only ) != craft_flags::none && type->tool ) {
// See Character::craft_consume_tools. In theory only
// `charges_required / 20 + charges_required % 20` charges are
// consumed during the first 5% progress, however that equation
// sometimes decreases when the batch size increases, so we take
// the largest remainder value 19 to make this function return
// false consistently for large batch sizes.
charges_required = std::min( charges_required, charges_required / 20 + 19 );
}
int charges_found = crafting_inv.charges_of( type, charges_required, filter, visitor );
return charges_found == charges_required;
}
}
nc_color tool_comp::get_color( bool has_one, const read_only_visitable &crafting_inv,
const std::function<bool( const item & )> &filter, int batch ) const
{
if( available == available_status::a_insufficient ) {
return c_brown;
} else if( has( crafting_inv, filter, batch ) ) {
return c_green;
}
return has_one ? c_dark_gray : c_red;
}
bool item_comp::has(
const read_only_visitable &crafting_inv, const std::function<bool( const item & )> &filter,
int batch,
craft_flags, const std::function<void( int )> & ) const
{
if( get_player_character().has_trait( trait_DEBUG_HS ) ) {
return true;
}
const int cnt = std::abs( count ) * batch;
if( item::count_by_charges( type ) ) {
return crafting_inv.has_charges( type, cnt, filter );
} else {
return crafting_inv.has_components( type, cnt, filter );
}
}
nc_color item_comp::get_color( bool has_one, const read_only_visitable &crafting_inv,
const std::function<bool( const item & )> &filter, int batch ) const
{
if( available == available_status::a_insufficient ) {
return c_brown;
} else if( has( crafting_inv, filter, batch ) ) {
const inventory *inv = static_cast<const inventory *>( &crafting_inv );
// Will use non-empty liquid container
if( std::any_of( type->pockets.begin(), type->pockets.end(), []( const pocket_data & d ) {
return d.type == item_pocket::pocket_type::CONTAINER && d.watertight;
} ) && inv != nullptr && inv->must_use_liq_container( type, count * batch ) ) {
return c_magenta;
}
// Will use favorited component