forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_item_stack.cpp
118 lines (99 loc) · 3.24 KB
/
map_item_stack.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
#include "map_item_stack.h"
#include <algorithm>
#include <functional>
#include <iterator>
#include "item.h"
#include "item_category.h"
#include "item_search.h"
#include "line.h"
map_item_stack::item_group::item_group() : count( 0 ), it( nullptr )
{
}
map_item_stack::item_group::item_group( const tripoint &p, const int arg_count,
const item *itm ) : pos( p ),
count( arg_count ), it( itm )
{
}
map_item_stack::map_item_stack() : example( nullptr ), totalcount( 0 )
{
vIG.emplace_back( );
}
map_item_stack::map_item_stack( const item *const it, const tripoint &pos ) : example( it ),
totalcount( it->count() )
{
vIG.emplace_back( pos, totalcount, it );
}
void map_item_stack::add_at_pos( const item *const it, const tripoint &pos )
{
const int amount = it->count();
if( vIG.empty() || vIG.back().pos != pos ) {
vIG.emplace_back( pos, amount, it );
} else {
vIG.back().count += amount;
}
totalcount += amount;
}
bool map_item_stack::map_item_stack_sort( const map_item_stack &lhs, const map_item_stack &rhs )
{
const item_category &lhs_cat = lhs.example->get_category_of_contents();
const item_category &rhs_cat = rhs.example->get_category_of_contents();
if( lhs_cat == rhs_cat ) {
return square_dist( tripoint_zero, lhs.vIG[0].pos ) <
square_dist( tripoint_zero, rhs.vIG[0].pos );
}
return lhs_cat < rhs_cat;
}
std::vector<map_item_stack> filter_item_stacks( const std::vector<map_item_stack> &stack,
const std::string &filter )
{
std::vector<map_item_stack> ret;
auto z = item_filter_from_string( filter );
std::copy_if( stack.begin(), stack.end(),
std::back_inserter( ret ), [z]( const map_item_stack & a ) {
if( a.example != nullptr ) {
return z( *a.example );
}
return false;
} );
return ret;
}
//returns the first non priority items.
int list_filter_high_priority( std::vector<map_item_stack> &stack, const std::string &priorities )
{
// TODO:optimize if necessary
std::vector<map_item_stack> tempstack;
const auto filter_fn = item_filter_from_string( priorities );
for( auto it = stack.begin(); it != stack.end(); ) {
if( priorities.empty() || ( it->example != nullptr && !filter_fn( *it->example ) ) ) {
tempstack.push_back( *it );
it = stack.erase( it );
} else {
it++;
}
}
int id = stack.size();
for( map_item_stack &elem : tempstack ) {
stack.push_back( elem );
}
return id;
}
int list_filter_low_priority( std::vector<map_item_stack> &stack, const int start,
const std::string &priorities )
{
// TODO:optimize if necessary
std::vector<map_item_stack> tempstack;
const auto filter_fn = item_filter_from_string( priorities );
for( auto it = stack.begin() + start; it != stack.end(); ) {
if( !priorities.empty() && it->example != nullptr && filter_fn( *it->example ) ) {
tempstack.push_back( *it );
it = stack.erase( it );
} else {
it++;
}
}
int id = stack.size();
for( map_item_stack &elem : tempstack ) {
stack.push_back( elem );
}
return id;
}