forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
craft_command.cpp
384 lines (330 loc) · 12.5 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
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
#include "craft_command.h"
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <limits>
#include <list>
#include "crafting.h"
#include "debug.h"
#include "enum_conversions.h"
#include "game_constants.h"
#include "inventory.h"
#include "item.h"
#include "json.h"
#include "output.h"
#include "player.h"
#include "recipe.h"
#include "requirements.h"
#include "translations.h"
#include "type_id.h"
#include "uistate.h"
static const trait_id trait_DEBUG_HS( "DEBUG_HS" );
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:
case num_usages:
break;
}
return item::nname( comp.type, comp.count );
}
namespace io
{
template<>
std::string enum_to_string<usage>( usage data )
{
switch( data ) {
// *INDENT-OFF*
case usage::use_from_map: return "map";
case usage::use_from_player: return "player";
case usage::use_from_both: return "both";
case usage::use_from_none: return "none";
case usage::cancel: return "cancel";
// *INDENT-ON*
case usage::num_usages:
break;
}
debugmsg( "Invalid usage" );
abort();
}
} // namespace io
template<typename CompType>
void comp_selection<CompType>::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "use_from", io::enum_to_string( use_from ) );
jsout.member( "type", comp.type );
jsout.member( "count", comp.count );
jsout.end_object();
}
template<typename CompType>
void comp_selection<CompType>::deserialize( JsonIn &jsin )
{
JsonObject data = jsin.get_object();
std::string use_from_str;
data.read( "use_from", use_from_str );
use_from = io::string_to_enum<usage>( use_from_str );
data.read( "type", comp.type );
data.read( "count", comp.count );
}
template void comp_selection<tool_comp>::serialize( JsonOut &jsout ) const;
template void comp_selection<item_comp>::serialize( JsonOut &jsout ) const;
template void comp_selection<tool_comp>::deserialize( JsonIn &jsin );
template void comp_selection<item_comp>::deserialize( JsonIn &jsin );
void craft_command::execute( const tripoint &new_loc )
{
if( empty() ) {
return;
}
if( new_loc != tripoint_zero ) {
loc = new_loc;
}
bool need_selections = true;
inventory map_inv;
map_inv.form_from_map( crafter->pos(), PICKUP_RANGE, crafter );
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;
}
}
if( need_selections ) {
if( !crafter->can_make( rec, batch_size ) ) {
if( crafter->can_start_craft( rec, recipe_filter_flags::none, batch_size ) ) {
if( !query_yn( _( "You don't have enough charges to complete the %s.\n"
"Start crafting anyway?" ), rec->result_name() ) ) {
return;
}
} else {
debugmsg( "Tried to start craft without sufficient charges" );
return;
}
}
flags = recipe_filter_flags::no_rotten;
if( !crafter->can_start_craft( rec, flags, batch_size ) ) {
if( !query_yn( _( "This craft will use rotten components.\n"
"Start crafting anyway?" ) ) ) {
return;
}
flags = recipe_filter_flags::none;
}
item_selections.clear();
const auto filter = rec->get_component_filter( flags );
const requirement_data *needs = rec->deduped_requirements().select_alternative(
*crafter, filter, batch_size, cost_adjustment::start_only );
if( !needs ) {
return;
}
for( const auto &it : needs->get_components() ) {
comp_selection<item_comp> is =
crafter->select_item_component( it, batch_size, map_inv, true, filter );
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 = crafting::select_tool_component(
it, batch_size, map_inv, crafter, true,
DEFAULT_HOTKEYS,
cost_adjustment::start_only );
if( ts.use_from == cancel ) {
return;
}
tool_selections.push_back( ts );
}
}
crafter->start_craft( *this, loc );
crafter->last_batch = batch_size;
crafter->lastrecipe = rec->ident();
const auto iter = std::find( uistate.recent_recipes.begin(), uistate.recent_recipes.end(),
rec->ident() );
if( iter != uistate.recent_recipes.end() ) {
uistate.recent_recipes.erase( iter );
}
uistate.recent_recipes.push_back( rec->ident() );
if( uistate.recent_recipes.size() > 20 ) {
uistate.recent_recipes.erase( uistate.recent_recipes.begin() );
}
}
/** Does a string join with ', ' of the components in the passed vector and inserts into 'str' */
template<typename T>
static std::string component_list_string( const std::vector<comp_selection<T>> &components )
{
return 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::string ss = _( "Some components used previously are missing. Continue?" );
if( !missing_items.empty() ) {
ss += "\n";
ss += _( "Item(s): " );
ss += component_list_string( missing_items );
}
if( !missing_tools.empty() ) {
ss += "\n";
ss += _( "Tool(s): " );
ss += component_list_string( missing_tools );
}
return query_yn( ss );
}
item craft_command::create_in_progress_craft()
{
// Use up the components and tools
std::list<item> used;
std::vector<item_comp> comps_used;
if( crafter->has_trait( trait_DEBUG_HS ) ) {
item new_craft( rec, batch_size, used, comps_used );
new_craft.set_tools_to_continue( true );
return new_craft;
}
if( empty() ) {
debugmsg( "Warning: attempted to consume items from an empty craft_command" );
return item();
}
inventory map_inv;
map_inv.form_from_map( crafter->pos(), PICKUP_RANGE, crafter );
if( !check_item_components_missing( map_inv ).empty() ) {
debugmsg( "Aborting crafting: couldn't find cached components" );
return item();
}
const auto filter = rec->get_component_filter( flags );
for( const auto &it : item_selections ) {
std::list<item> tmp = crafter->consume_items( it, batch_size, filter );
used.splice( used.end(), tmp );
}
for( const comp_selection<item_comp> &selection : item_selections ) {
item_comp comp_used = selection.comp;
comp_used.count *= batch_size;
//Handle duplicate component requirement
auto found_it = std::find_if( comps_used.begin(),
comps_used.end(), [&comp_used]( const item_comp & c ) {
return c.type == comp_used.type;
} );
if( found_it != comps_used.end() ) {
item_comp &found_comp = *found_it;
found_comp.count += comp_used.count;
} else {
comps_used.emplace_back( comp_used );
}
}
item new_craft( rec, batch_size, used, comps_used );
new_craft.set_cached_tool_selections( tool_selections );
new_craft.set_tools_to_continue( true );
// Pass true to indicate that we are starting the craft and the remainder should be consumed as well
crafter->craft_consume_tools( new_craft, 1, true );
new_craft.set_next_failure_point( *crafter );
return new_craft;
}
skill_id craft_command::get_skill_id()
{
return rec->skill_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;
const auto filter = rec->get_component_filter( flags );
for( const auto &item_sel : item_selections ) {
itype_id type = item_sel.comp.type;
const item_comp component = item_sel.comp;
const int count = component.count > 0 ? component.count * batch_size : std::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, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
if( !map_inv.has_charges( type, count, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
if( !( crafter->charges_of( type, INT_MAX, filter ) +
map_inv.charges_of( type, INT_MAX, filter ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
case num_usages:
break;
}
} else {
// Counting by units, not charges.
switch( item_sel.use_from ) {
case use_from_player:
if( !crafter->has_amount( type, count, false, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_map:
if( !map_inv.has_components( type, count, filter ) ) {
missing.push_back( item_sel );
}
break;
case use_from_both:
if( !( crafter->amount_of( type, false, std::numeric_limits<int>::max(), filter ) +
map_inv.amount_of( type, false, std::numeric_limits<int>::max(), filter ) >= count ) ) {
missing.push_back( item_sel );
}
break;
case use_from_none:
case cancel:
case num_usages:
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 ) {
const int 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:
case num_usages:
break;
}
} else if( !crafter->has_amount( type, 1 ) && !map_inv.has_tools( type, 1 ) ) {
missing.push_back( tool_sel );
}
}
return missing;
}