forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe_dictionary.cpp
687 lines (591 loc) · 21.6 KB
/
recipe_dictionary.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
#include "recipe_dictionary.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <new>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include "cata_algo.h"
#include "cata_utility.h"
#include "crafting_gui.h"
#include "debug.h"
#include "init.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "json.h"
#include "make_static.h"
#include "mapgen.h"
#include "output.h"
#include "requirements.h"
#include "skill.h"
#include "translations.h"
#include "uistate.h"
#include "units.h"
#include "value_ptr.h"
static const requirement_id requirement_data_uncraft_book( "uncraft_book" );
recipe_dictionary recipe_dict;
namespace
{
void delete_if( std::map<recipe_id, recipe> &data,
const std::function<bool( const recipe & )> &pred )
{
for( auto it = data.begin(); it != data.end(); ) {
if( pred( it->second ) ) {
it = data.erase( it );
} else {
++it;
}
}
}
} // namespace
static recipe null_recipe;
static std::set<const recipe *> null_match;
static DynamicDataLoader::deferred_json deferred;
template<>
const recipe &string_id<recipe>::obj() const
{
const auto iter = recipe_dict.recipes.find( *this );
if( iter != recipe_dict.recipes.end() ) {
if( iter->second.obsolete ) {
return null_recipe;
}
return iter->second;
}
if( *this != NULL_ID() ) {
debugmsg( "invalid recipe id \"%s\"", c_str() );
}
return null_recipe;
}
template<>
bool string_id<recipe>::is_valid() const
{
return recipe_dict.recipes.find( *this ) != recipe_dict.recipes.end();
}
const recipe &recipe_dictionary::get_uncraft( const itype_id &id )
{
auto iter = recipe_dict.uncraft.find( recipe_id( id.str() ) );
return iter != recipe_dict.uncraft.end() ? iter->second : null_recipe;
}
const recipe &recipe_dictionary::get_craft( const itype_id &id )
{
auto iter = recipe_dict.recipes.find( recipe_id( id.str() ) );
return iter != recipe_dict.recipes.end() ? iter->second : null_recipe;
}
// searches for left-anchored partial match in the relevant recipe requirements set
template <class group>
bool search_reqs( group gp, const std::string &txt )
{
return std::any_of( gp.begin(), gp.end(), [&]( const typename group::value_type & opts ) {
return std::any_of( opts.begin(),
opts.end(), [&]( const typename group::value_type::value_type & e ) {
return lcmatch( e.to_string(), txt );
} );
} );
}
// template specialization to make component searches easier
template<>
bool search_reqs( std::vector<std::vector<item_comp> > gp,
const std::string &txt )
{
return std::any_of( gp.begin(), gp.end(), [&]( const std::vector<item_comp> &opts ) {
return std::any_of( opts.begin(), opts.end(), [&]( const item_comp & ic ) {
return lcmatch( item::nname( ic.type ), txt );
} );
} );
}
std::vector<const recipe *> recipe_subset::favorite() const
{
std::vector<const recipe *> res;
std::copy_if( recipes.begin(), recipes.end(), std::back_inserter( res ), [&]( const recipe * r ) {
if( !*r || r->obsolete ) {
return false;
}
return uistate.favorite_recipes.find( r->ident() ) != uistate.favorite_recipes.end();
} );
return res;
}
std::vector<const recipe *> recipe_subset::hidden() const
{
std::vector<const recipe *> res;
std::copy_if( recipes.begin(), recipes.end(), std::back_inserter( res ), [&]( const recipe * r ) {
if( !*r || r->obsolete ) {
return false;
}
return uistate.hidden_recipes.find( r->ident() ) != uistate.hidden_recipes.end();
} );
return res;
}
std::vector<const recipe *> recipe_subset::recent() const
{
std::vector<const recipe *> res;
for( auto rec_id = uistate.recent_recipes.rbegin(); rec_id != uistate.recent_recipes.rend();
++rec_id ) {
std::copy_if( recipes.begin(), recipes.end(), std::back_inserter( res ),
[&rec_id]( const recipe * r ) {
return *r && !( *rec_id != r->ident() || r->obsolete );
} );
}
return res;
}
std::vector<const recipe *> recipe_subset::search(
const std::string &txt, const search_type key,
const std::function<void( size_t, size_t )> &progress_callback ) const
{
auto predicate = [&]( const recipe * r ) {
if( !*r || r->obsolete ) {
return false;
}
switch( key ) {
case search_type::name:
return lcmatch( r->result_name(), txt );
case search_type::exclude_name:
return !lcmatch( r->result_name(), txt );
case search_type::skill: {
if( r->skill_used && lcmatch( r->skill_used->name(), txt ) ) {
return true;
}
const auto &skills = r->required_skills;
return std::any_of( skills.begin(), skills.end(), [&]( const std::pair<skill_id, int> &e ) {
return lcmatch( e.first->name(), txt );
} );
}
case search_type::primary_skill:
return lcmatch( r->skill_used->name(), txt );
case search_type::component:
return search_reqs( r->simple_requirements().get_components(), txt );
case search_type::tool:
return search_reqs( r->simple_requirements().get_tools(), txt );
case search_type::quality:
return search_reqs( r->simple_requirements().get_qualities(), txt );
case search_type::quality_result: {
const auto &quals = item::find_type( r->result() )->qualities;
return std::any_of( quals.begin(), quals.end(), [&]( const std::pair<quality_id, int> &e ) {
return lcmatch( e.first->name, txt );
} );
}
case search_type::description_result: {
if( r->is_practice() ) {
return lcmatch( r->description.translated(), txt );
} else {
const item result = r->create_result();
return lcmatch( remove_color_tags( result.info( true ) ), txt );
}
}
case search_type::proficiency:
return lcmatch( r->recipe_proficiencies_string(), txt );
case search_type::difficulty: {
std::string range_start;
std::string range_end;
bool use_range = false;
for( const char &chr : txt ) {
if( std::isdigit( chr ) ) {
if( use_range ) {
range_end += chr;
} else {
range_start += chr;
}
} else if( chr == '~' ) {
use_range = true;
} else {
// unexpected character
return true;
}
}
int start = 0;
int end = INT_MAX;
if( use_range ) {
if( !range_start.empty() ) {
start = std::stoi( range_start );
}
if( !range_end.empty() ) {
end = std::stoi( range_end );
}
if( range_start.empty() && range_end.empty() ) {
return true;
}
} else {
if( !range_start.empty() ) {
start = std::stoi( range_start );
}
if( range_start.empty() && range_end.empty() ) {
return true;
}
}
if( use_range && start > end ) {
int swap = start;
start = end;
end = swap;
}
if( use_range ) {
// check if number is between two numbers inclusive
return r->difficulty == clamp( r->difficulty, start, end );
} else {
return r->difficulty == start;
}
}
default:
return false;
}
};
std::vector<const recipe *> res;
size_t i = 0;
for( const recipe *r : recipes ) {
if( progress_callback ) {
progress_callback( i, recipes.size() );
}
if( predicate( r ) ) {
res.push_back( r );
}
++i;
}
return res;
}
recipe_subset::recipe_subset( const recipe_subset &src, const std::vector<const recipe *> &recipes )
{
for( const recipe *elem : recipes ) {
include( elem, src.get_custom_difficulty( elem ) );
}
}
recipe_subset recipe_subset::reduce(
const std::string &txt, const search_type key,
const std::function<void( size_t, size_t )> &progress_callback ) const
{
return recipe_subset( *this, search( txt, key, progress_callback ) );
}
recipe_subset recipe_subset::intersection( const recipe_subset &subset ) const
{
std::vector<const recipe *> intersection_result;
std::set_intersection( this->begin(), this->end(), subset.begin(), subset.end(),
std::back_inserter( intersection_result ) );
return recipe_subset( *this, intersection_result );
}
recipe_subset recipe_subset::difference( const recipe_subset &subset ) const
{
std::vector<const recipe *> difference_result;
std::set_difference( this->begin(), this->end(), subset.begin(), subset.end(),
std::back_inserter( difference_result ) );
return recipe_subset( *this, difference_result );
}
std::vector<const recipe *> recipe_subset::search_result( const itype_id &item ) const
{
std::vector<const recipe *> res;
std::copy_if( recipes.begin(), recipes.end(), std::back_inserter( res ), [&]( const recipe * r ) {
if( r->obsolete ) {
return false;
}
return item == r->result() || r->in_byproducts( item );
} );
return res;
}
bool recipe_subset::empty_category( const std::string &cat, const std::string &subcat ) const
{
if( subcat == "CSC_*_FAVORITE" ) {
return uistate.favorite_recipes.empty();
} else if( subcat == "CSC_*_RECENT" ) {
return uistate.recent_recipes.empty();
} else if( subcat == "CSC_*_HIDDEN" ) {
return uistate.hidden_recipes.empty();
}
auto iter = category.find( cat );
if( iter != category.end() ) {
if( subcat.empty() ) {
return false;
} else {
for( const recipe *e : iter->second ) {
if( e->subcategory == subcat ) {
return false;
}
}
}
}
return true;
}
std::vector<const recipe *> recipe_subset::in_category( const std::string &cat,
const std::string &subcat ) const
{
std::vector<const recipe *> res;
auto iter = category.find( cat );
if( iter != category.end() ) {
if( subcat.empty() ) {
std::copy_if( iter->second.begin(), iter->second.end(),
std::back_inserter( res ), [&]( const recipe * e ) {
return !e->obsolete;
} );
} else {
std::copy_if( iter->second.begin(), iter->second.end(),
std::back_inserter( res ), [&subcat]( const recipe * e ) {
if( e->obsolete ) {
return false;
}
return e->subcategory == subcat;
} );
}
}
return res;
}
const std::set<const recipe *> &recipe_subset::of_component( const itype_id &id ) const
{
auto iter = component.find( id );
return iter != component.end() ? iter->second : null_match;
}
void recipe_dictionary::load_recipe( const JsonObject &jo, const std::string &src )
{
load( jo, src, recipe_dict.recipes );
}
void recipe_dictionary::load_uncraft( const JsonObject &jo, const std::string &src )
{
load( jo, src, recipe_dict.uncraft );
}
void recipe_dictionary::load_practice( const JsonObject &jo, const std::string &src )
{
load( jo, src, recipe_dict.recipes );
}
recipe &recipe_dictionary::load( const JsonObject &jo, const std::string &src,
std::map<recipe_id, recipe> &out )
{
recipe r;
// defer entries dependent upon as-yet unparsed definitions
if( jo.has_string( "copy-from" ) ) {
auto base = recipe_id( jo.get_string( "copy-from" ) );
if( !out.count( base ) ) {
deferred.emplace_back( jo.get_source_location(), src );
jo.allow_omitted_members();
return null_recipe;
}
r = out[ base ];
}
r.load( jo, src );
r.was_loaded = true;
return out[ r.ident() ] = std::move( r );
}
size_t recipe_dictionary::size() const
{
return recipes.size();
}
std::map<recipe_id, recipe>::const_iterator recipe_dictionary::begin() const
{
return recipes.begin();
}
std::map<recipe_id, recipe>::const_iterator recipe_dictionary::end() const
{
return recipes.end();
}
bool recipe_dictionary::is_item_on_loop( const itype_id &i ) const
{
return items_on_loops.count( i );
}
void recipe_dictionary::finalize_internal( std::map<recipe_id, recipe> &obj )
{
for( auto &elem : obj ) {
elem.second.finalize();
inp_mngr.pump_events();
}
// remove any blacklisted or invalid recipes...
delete_if( []( const recipe & elem ) {
if( elem.is_blacklisted() ) {
return true;
}
if( elem.obsolete ) {
return false;
}
const std::string error = elem.get_consistency_error();
if( !error.empty() ) {
debugmsg( "Recipe %s %s.", elem.ident().c_str(), error.c_str() );
}
return !error.empty();
} );
}
void recipe_dictionary::find_items_on_loops()
{
// Check for infinite recipe loops in food (which are problematic for
// nutrient calculations).
//
// Start by building a directed graph of itypes to potential components of
// those itypes.
items_on_loops.clear();
std::unordered_map<itype_id, std::vector<itype_id>> potential_components_of;
for( const itype *i : item_controller->all() ) {
if( !i->comestible || i->has_flag( STATIC( flag_id( "NUTRIENT_OVERRIDE" ) ) ) ) {
continue;
}
std::vector<itype_id> &potential_components = potential_components_of[i->get_id()];
for( const recipe_id &rec : i->recipes ) {
const requirement_data requirements = rec->simple_requirements();
const requirement_data::alter_item_comp_vector &component_requirements =
requirements.get_components();
for( const std::vector<item_comp> &component_options : component_requirements ) {
for( const item_comp &component_option : component_options ) {
potential_components.push_back( component_option.type );
}
}
}
}
// Now check that graph for loops
std::vector<std::vector<itype_id>> loops = cata::find_cycles( potential_components_of );
for( const std::vector<itype_id> &loop : loops ) {
std::string error_message =
"loop in comestible recipes detected: " + loop.back().str();
for( const itype_id &i : loop ) {
error_message += " -> " + i.str();
items_on_loops.insert( i );
}
error_message += ". Such loops can be broken by either removing or altering "
"recipes or marking one of the items involved with the NUTRIENT_OVERRIDE "
"flag";
debugmsg( error_message );
}
}
void recipe_dictionary::finalize()
{
DynamicDataLoader::get_instance().load_deferred( deferred );
// remove abstract recipes
delete_if( []( const recipe & element ) {
return element.abstract;
} );
finalize_internal( recipe_dict.recipes );
finalize_internal( recipe_dict.uncraft );
for( const auto &e : recipe_dict.recipes ) {
const recipe &r = e.second;
if( r.obsolete ) {
continue;
}
for( const auto &bk : r.booksets ) {
const itype *booktype = item::find_type( bk.first );
int req = bk.second.skill_req > 0 ? bk.second.skill_req : std::max( booktype->book->req,
r.difficulty );
islot_book::recipe_with_description_t desc{ &r, req, bk.second.alt_name, bk.second.hidden };
const_cast<islot_book &>( *booktype->book ).recipes.insert( desc );
}
// if reversible and no specific uncraft recipe exists use this recipe
const string_id<recipe> uncraft_id = recipe_id( r.result().str() );
if( r.is_reversible() && !recipe_dict.uncraft.count( uncraft_id ) ) {
recipe_dict.uncraft[ uncraft_id ] = r;
if( r.uncraft_time > 0 ) {
// If a specified uncraft time has been given, use that in the uncraft
// recipe rather than the original.
recipe_dict.uncraft[ uncraft_id ].time = r.uncraft_time;
}
}
}
// add pseudo uncrafting recipes
for( const itype *e : item_controller->all() ) {
const itype_id id = e->get_id();
const recipe_id rid = recipe_id( id.str() );
// books that don't already have an uncrafting recipe
if( e->book && !recipe_dict.uncraft.count( rid ) && e->volume > 0_ml ) {
int pages = e->volume / 12.5_ml;
recipe &bk = recipe_dict.uncraft[rid];
bk.ident_ = rid;
bk.result_ = id;
bk.reversible = true;
bk.requirements_ = *requirement_data_uncraft_book * pages;
// TODO: allow specifying time in requirement_data
bk.time = pages * 10;
}
}
// Cache auto-learn recipes and blueprints
for( const auto &e : recipe_dict.recipes ) {
if( e.second.autolearn ) {
recipe_dict.autolearn.insert( &e.second );
}
if( e.second.is_blueprint() ) {
recipe_dict.blueprints.insert( &e.second );
}
}
recipe_dict.find_items_on_loops();
}
void recipe_dictionary::check_consistency()
{
for( auto &e : recipe_dict.recipes ) {
recipe &r = e.second;
if( r.category.empty() ) {
if( !r.subcategory.empty() ) {
debugmsg( "recipe %s has subcategory but no category", r.ident().str() );
}
continue;
}
const std::vector<std::string> *subcategories = subcategories_for_category( r.category );
if( !subcategories ) {
debugmsg( "recipe %s has invalid category %s", r.ident().str(), r.category );
continue;
}
if( !r.subcategory.empty() ) {
auto it = std::find( subcategories->begin(), subcategories->end(), r.subcategory );
if( it == subcategories->end() ) {
debugmsg(
"recipe %s has subcategory %s which is invalid or doesn't match category %s",
r.ident().str(), r.subcategory, r.category );
}
}
}
for( auto &e : recipe_dict.recipes ) {
recipe &r = e.second;
if( !r.blueprint.is_empty() && !has_update_mapgen_for( r.blueprint ) ) {
debugmsg( "recipe %s specifies invalid construction_blueprint %s; that should be a "
"defined update_mapgen_id but is not", r.ident().str(), r.blueprint.str() );
}
}
}
void recipe_dictionary::reset()
{
recipe_dict.blueprints.clear();
recipe_dict.autolearn.clear();
recipe_dict.recipes.clear();
recipe_dict.uncraft.clear();
recipe_dict.items_on_loops.clear();
}
void recipe_dictionary::delete_if( const std::function<bool( const recipe & )> &pred )
{
::delete_if( recipe_dict.recipes, pred );
::delete_if( recipe_dict.uncraft, pred );
}
void recipe_subset::include( const recipe *r, int custom_difficulty )
{
if( custom_difficulty < 0 ) {
custom_difficulty = r->difficulty;
}
// We always prefer lower difficulty for the subset, but we save it only if it's not default
if( recipes.count( r ) > 0 ) {
const auto iter = difficulties.find( r );
// See if we need to lower the difficulty of the existing recipe
if( iter != difficulties.end() && custom_difficulty < iter->second ) {
if( custom_difficulty != r->difficulty ) {
iter->second = custom_difficulty; // Added again with lower difficulty
} else {
difficulties.erase( iter ); // No need to keep the default difficulty. Free some memory
}
} else if( custom_difficulty < r->difficulty ) {
difficulties[r] = custom_difficulty; // Added again with lower difficulty
}
} else {
// add recipe to category and component caches
for( const auto &opts : r->simple_requirements().get_components() ) {
for( const item_comp &comp : opts ) {
component[comp.type].insert( r );
}
}
category[r->category].insert( r );
// Set the difficulty is it's not the default
if( custom_difficulty != r->difficulty ) {
difficulties[r] = custom_difficulty;
}
// insert the recipe
recipes.insert( r );
}
}
void recipe_subset::include( const recipe_subset &subset )
{
for( const recipe * const &elem : subset ) {
include( elem, subset.get_custom_difficulty( elem ) );
}
}
int recipe_subset::get_custom_difficulty( const recipe *r ) const
{
const auto iter = difficulties.find( r );
if( iter != difficulties.end() ) {
return iter->second;
}
return r->difficulty;
}