forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcraft_command.cpp
254 lines (219 loc) · 8.16 KB
/
craft_command.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
#include "craft_command.h"
#include "debug.h"
#include "game_constants.h"
#include "item.h"
#include "itype.h"
#include "inventory.h"
#include "output.h"
#include "player.h"
#include "recipe.h"
#include "requirements.h"
#include "translations.h"
#include "crafting.h"
#include <list>
#include <sstream>
#include <string>
#include <vector>
template<typename CompType>
std::string comp_selection<CompType>::nname() const
{
switch( use_from ) {
case use_from_map:
return item::nname( comp.type, comp.count ) + _( " (nearby)" );
case use_from_both:
return item::nname( comp.type, comp.count ) + _( " (person & nearby)" );
case use_from_player: // Is the same as the default return;
case use_from_none:
case cancel:
break;
}
return item::nname( comp.type, comp.count );
}
void craft_command::execute()
{
if( empty() ) {
return;
}
bool need_selections = true;
inventory map_inv;
map_inv.form_from_map( crafter->pos(), PICKUP_RANGE );
if( has_cached_selections() ) {
std::vector<comp_selection<item_comp>> missing_items = check_item_components_missing( map_inv );
std::vector<comp_selection<tool_comp>> missing_tools = check_tool_components_missing( map_inv );
if( missing_items.empty() && missing_tools.empty() ) {
// All items we used previously are still there, so we don't need to do selection.
need_selections = false;
} else if( !query_continue( missing_items, missing_tools ) ) {
return;
}
}
const auto needs = rec->requirements();
if( need_selections ) {
item_selections.clear();
for( const auto &it : needs.get_components() ) {
comp_selection<item_comp> is = crafter->select_item_component( it, batch_size, map_inv, true );
if( is.use_from == cancel ) {
return;
}
item_selections.push_back( is );
}
tool_selections.clear();
for( const auto &it : needs.get_tools() ) {
comp_selection<tool_comp> ts = crafter->select_tool_component(
it, batch_size, map_inv, DEFAULT_HOTKEYS, true );
if( ts.use_from == cancel ) {
return;
}
tool_selections.push_back( ts );
}
}
auto type = activity_id( is_long ? "ACT_LONGCRAFT" : "ACT_CRAFT" );
auto activity = player_activity( type, crafter->time_to_craft( *rec, batch_size ), -1, INT_MIN,
rec->ident() );
activity.values.push_back( batch_size );
crafter->assign_activity( activity );
/* legacy support for lua bindings to last_batch and lastrecipe */
crafter->last_batch = batch_size;
crafter->lastrecipe = rec->ident();
}
/** Does a string join with ', ' of the components in the passed vector and inserts into 'str' */
template<typename T>
static void component_list_string( std::stringstream &str,
const std::vector<comp_selection<T>> &components )
{
str << enumerate_as_string( components.begin(), components.end(),
[]( const comp_selection<T> &comp ) {
return comp.nname();
} );
}
bool craft_command::query_continue( const std::vector<comp_selection<item_comp>> &missing_items,
const std::vector<comp_selection<tool_comp>> &missing_tools )
{
std::stringstream ss;
ss << _( "Some components used previously are missing. Continue?" );
if( !missing_items.empty() ) {
ss << std::endl << _( "Item(s): " );
component_list_string( ss, missing_items );
}
if( !missing_tools.empty() ) {
ss << std::endl << _( "Tool(s): " );
component_list_string( ss, missing_tools );
}
std::vector<std::string> options;
options.push_back( _( "Yes" ) );
options.push_back( _( "No" ) );
// We NEED a copy.
const std::string str = ss.str();
int selection = menu_vec( true, str.c_str(), options );
return selection == 1;
}
std::list<item> craft_command::consume_components()
{
std::list<item> used;
if( crafter->has_trait( trait_id( "DEBUG_HS" ) ) ) {
return used;
}
if( empty() ) {
debugmsg( "Warning: attempted to consume items from an empty craft_command" );
return used;
}
inventory map_inv;
map_inv.form_from_map( crafter->pos(), PICKUP_RANGE );
if( !check_item_components_missing( map_inv ).empty() ) {
debugmsg( "Aborting crafting: couldn't find cached components" );
return used;
}
for( const auto &it : item_selections ) {
std::list<item> tmp = crafter->consume_items( it, batch_size );
used.splice( used.end(), tmp );
}
for( const auto &it : tool_selections ) {
crafter->consume_tools( it, batch_size );
}
return used;
}
std::vector<comp_selection<item_comp>> craft_command::check_item_components_missing(
const inventory &map_inv ) const
{
std::vector<comp_selection<item_comp>> missing;
for( const auto &item_sel : item_selections ) {
itype_id type = item_sel.comp.type;
item_comp component = item_sel.comp;
long count = ( component.count > 0 ) ? component.count * batch_size : abs( component.count );
if( item::count_by_charges( type ) && count > 0 ) {
switch( item_sel.use_from ) {
case use_from_player:
if( !crafter->has_charges( type, count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
if( !map_inv.has_charges( type, count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
if( !( crafter->charges_of( type ) + map_inv.charges_of( type ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
break;
}
} else {
// Counting by units, not charges.
switch( item_sel.use_from ) {
case use_from_player:
if( !crafter->has_amount( type, count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
if( !map_inv.has_components( type, count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
if( !( crafter->amount_of( type ) + map_inv.amount_of( type ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
break;
}
}
}
return missing;
}
std::vector<comp_selection<tool_comp>> craft_command::check_tool_components_missing(
const inventory &map_inv ) const
{
std::vector<comp_selection<tool_comp>> missing;
for( const auto &tool_sel : tool_selections ) {
itype_id type = tool_sel.comp.type;
if( tool_sel.comp.count > 0 ) {
long count = tool_sel.comp.count * batch_size;
switch( tool_sel.use_from ) {
case use_from_player:
if( !crafter->has_charges( type, count ) ) {
missing.push_back( tool_sel );
}
break;
case use_from_map:
if( !map_inv.has_charges( type, count ) ) {
missing.push_back( tool_sel );
}
break;
case use_from_both:
case use_from_none:
case cancel:
break;
}
} else if( !crafter->has_amount( type, 1 ) && !map_inv.has_tools( type, 1 ) ) {
missing.push_back( tool_sel );
}
}
return missing;
}