forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_search.cpp
101 lines (95 loc) · 3.21 KB
/
item_search.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
#include "item_search.h"
#include <map>
#include <utility>
#include "cata_utility.h"
#include "item.h"
#include "item_category.h"
#include "item_factory.h"
#include "material.h"
#include "requirements.h"
#include "skill.h"
#include "string_id.h"
#include "type_id.h"
std::pair<std::string, std::string> get_both( const std::string &a );
std::function<bool( const item & )> basic_item_filter( std::string filter )
{
size_t colon;
char flag = '\0';
if( ( colon = filter.find( ':' ) ) != std::string::npos ) {
if( colon >= 1 ) {
flag = filter[colon - 1];
filter = filter.substr( colon + 1 );
}
}
switch( flag ) {
// category
case 'c':
return [filter]( const item & i ) {
return lcmatch( i.get_category().name(), filter );
};
// material
case 'm':
return [filter]( const item & i ) {
return std::any_of( i.made_of().begin(), i.made_of().end(),
[&filter]( const material_id & mat ) {
return lcmatch( mat->name(), filter );
} );
};
// qualities
case 'q':
return [filter]( const item & i ) {
return std::any_of( i.quality_of().begin(), i.quality_of().end(),
[&filter]( const std::pair<quality_id, int> &e ) {
return lcmatch( e.first->name, filter );
} );
};
// both
case 'b':
return [filter]( const item & i ) {
auto pair = get_both( filter );
return item_filter_from_string( pair.first )( i )
&& item_filter_from_string( pair.second )( i );
};
// disassembled components
case 'd':
return [filter]( const item & i ) {
const auto &components = i.get_uncraft_components();
for( auto &component : components ) {
if( lcmatch( component.to_string(), filter ) ) {
return true;
}
}
return false;
};
// item notes
case 'n':
return [filter]( const item & i ) {
const std::string note = i.get_var( "item_note" );
return !note.empty() && lcmatch( note, filter );
};
// skill taught
case 'k':
return [filter]( const item & i ) {
if( i.is_book() ) {
const islot_book &book = *i.type->book;
return lcmatch( book.skill->name(), filter );
}
return false;
};
// by name
default:
return [filter]( const item & a ) {
return lcmatch( a.tname(), filter );
};
}
}
std::function<bool( const item & )> item_filter_from_string( const std::string &filter )
{
return filter_from_string<item>( filter, basic_item_filter );
}
std::pair<std::string, std::string> get_both( const std::string &a )
{
size_t split_mark = a.find( ';' );
return std::make_pair( a.substr( 0, split_mark - 1 ),
a.substr( split_mark + 1 ) );
}