forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_item_handling.cpp
589 lines (512 loc) · 20 KB
/
activity_item_handling.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
#include "activity_handlers.h"
#include "game.h"
#include "map.h"
#include "item.h"
#include "player_activity.h"
#include "action.h"
#include "enums.h"
#include "creature.h"
#include "pickup.h"
#include "translations.h"
#include "messages.h"
#include "monster.h"
#include "vehicle.h"
#include "veh_type.h"
#include "player.h"
#include "debug.h"
#include "pickup.h"
#include <list>
#include <vector>
#include <cassert>
#include <algorithm>
void cancel_aim_processing();
const efftype_id effect_controlled( "controlled" );
const efftype_id effect_pet( "pet" );
/** Activity-associated item */
struct act_item {
const item *it; /// Pointer to the inventory item
int count; /// How many items need to be processed
int consumed_moves; /// Amount of moves that processing will consume
act_item( const item *it, int count, int consumed_moves )
: it( it ),
count( count ),
consumed_moves( consumed_moves ) {};
};
// @todo Deliberately unified with multidrop. Unify further.
typedef std::list<std::pair<int, int>> drop_indexes;
bool same_type( const std::list<item> &items )
{
return std::all_of( items.begin(), items.end(), [ &items ]( const item & it ) {
return it.type == items.begin()->type;
} );
}
void put_into_vehicle( player &p, const std::list<item> &items, vehicle &veh, int part )
{
if( items.empty() ) {
return;
}
const tripoint where = veh.global_part_pos3( part );
const std::string ter_name = g->m.name( where );
int fallen_count = 0;
for( auto it : items ) { // cant use constant reference here because of the spill_contents()
if( it.is_bucket_nonempty() && !it.spill_contents( p ) ) {
p.add_msg_player_or_npc(
_( "To avoid spilling its contents, you set your %1$s on the %2$s." ),
_( "To avoid spilling its contents, <npcname> sets their %1$s on the %2$s." ),
it.display_name().c_str(), ter_name.c_str()
);
g->m.add_item_or_charges( where, it );
continue;
}
if( !veh.add_item( part, it ) ) {
if( it.count_by_charges() ) {
// Maybe we can add a few charges in the trunk and the rest on the ground.
it.mod_charges( -veh.add_charges( part, it ) );
}
g->m.add_item_or_charges( where, it );
++fallen_count;
}
}
const std::string part_name = veh.part_info( part ).name();
if( same_type( items ) ) {
const item &it = items.front();
const int dropcount = items.size() * ( it.count_by_charges() ? it.charges : 1 );
p.add_msg_player_or_npc(
ngettext( "You put your %1$s in the %2$s's %3$s.",
"You put your %1$s in the %2$s's %3$s.", dropcount ),
ngettext( "<npcname> puts their %1$s in the %2$s's %3$s.",
"<npcname> puts their %1$s in the %2$s's %3$s.", dropcount ),
it.tname( dropcount ).c_str(), veh.name.c_str(), part_name.c_str()
);
} else {
p.add_msg_player_or_npc(
_( "You put several items in the %1$s's %2$s." ),
_( "<npcname> puts several items in the %1$s's %2$s." ),
veh.name.c_str(), part_name.c_str()
);
}
if( fallen_count > 0 ) {
add_msg( m_warning, _( "The trunk is full, so some items fell to the %s." ), ter_name.c_str() );
}
}
void stash_on_pet( const std::list<item> &items, monster &pet )
{
units::volume remaining_volume = pet.inv.empty() ? units::volume( 0 ) :
pet.inv.front().get_storage();
int remaining_weight = pet.weight_capacity();
for( const auto &it : pet.inv ) {
remaining_volume -= it.volume();
remaining_weight -= it.weight();
}
for( auto &it : items ) {
pet.add_effect( effect_controlled, 5 );
if( it.volume() > remaining_volume ) {
add_msg( m_bad, _( "%1$s did not fit and fell to the %2$s." ),
it.display_name().c_str(), g->m.name( pet.pos() ).c_str() );
g->m.add_item_or_charges( pet.pos(), it );
} else if( it.weight() > remaining_weight ) {
add_msg( m_bad, _( "%1$s is too heavy and fell to the %2$s." ),
it.display_name().c_str(), g->m.name( pet.pos() ).c_str() );
g->m.add_item_or_charges( pet.pos(), it );
} else {
pet.add_item( it );
remaining_volume -= it.volume();
remaining_weight -= it.weight();
}
}
}
void drop_on_map( const player &p, const std::list<item> &items, const tripoint &where )
{
if( items.empty() ) {
return;
}
const std::string ter_name = g->m.name( where );
const bool can_move_there = g->m.passable( where );
if( same_type( items ) ) {
const item &it = items.front();
const int dropcount = items.size() * ( it.count_by_charges() ? it.charges : 1 );
const std::string it_name = it.tname( dropcount );
if( can_move_there ) {
p.add_msg_player_or_npc(
ngettext( "You drop your %1$s on the %2$s.",
"You drop your %1$s on the %2$s.", dropcount ),
ngettext( "<npcname> drops their %1$s on the %2$s.",
"<npcname> drops their %1$s on the %2$s.", dropcount ),
it_name.c_str(), ter_name.c_str()
);
} else {
p.add_msg_player_or_npc(
ngettext( "You put your %1$s in the %2$s.",
"You put your %1$s in the %2$s.", dropcount ),
ngettext( "<npcname> puts their %1$s in the %2$s.",
"<npcname> puts their %1$s in the %2$s.", dropcount ),
it_name.c_str(), ter_name.c_str()
);
}
} else {
if( can_move_there ) {
p.add_msg_player_or_npc(
_( "You drop several items on the %s." ),
_( "<npcname> drops several items on the %s." ),
ter_name.c_str()
);
} else {
p.add_msg_player_or_npc(
_( "You put several items in the %s." ),
_( "<npcname> puts several items in the %s." ),
ter_name.c_str()
);
}
}
for( const auto &it : items ) {
g->m.add_item_or_charges( where, it );
}
}
void put_into_vehicle_or_drop( player &p, const std::list<item> &items,
const tripoint &where )
{
int veh_part = 0;
vehicle *veh = g->m.veh_at( where, veh_part );
if( veh != nullptr ) {
veh_part = veh->part_with_feature( veh_part, "CARGO" );
if( veh_part >= 0 ) {
put_into_vehicle( p, items, *veh, veh_part );
return;
}
}
drop_on_map( p, items, where );
}
drop_indexes convert_to_indexes( const player_activity &act )
{
drop_indexes res;
if( act.values.size() % 2 != 0 ) {
debugmsg( "Drop/stash activity contains an odd number of values." );
return res;
}
for( auto iter = act.values.begin(); iter != act.values.end(); iter += 2 ) {
res.emplace_back( *iter, *std::next( iter ) );
}
return res;
}
drop_indexes convert_to_indexes( const player &p, const std::list<act_item> &items )
{
drop_indexes res;
for( const auto &ait : items ) {
const int pos = p.get_item_position( ait.it );
if( pos != INT_MIN && ait.count > 0 ) {
if( res.empty() || res.back().first != pos ) {
res.emplace_back( pos, ait.count );
} else {
res.back().second += ait.count;
}
}
}
return res;
}
std::list<act_item> convert_to_items( const player &p, const drop_indexes &drop,
int min_pos, int max_pos )
{
std::list<act_item> res;
for( const auto &rec : drop ) {
const auto pos = rec.first;
const auto count = rec.second;
if( pos < min_pos || pos > max_pos ) {
continue;
} else if( pos >= 0 ) {
int obtained = 0;
for( const auto &it : p.inv.const_stack( pos ) ) {
if( obtained >= count ) {
break;
}
const int qty = it.count_by_charges() ? std::min<int>( it.charges, count - obtained ) : 1;
obtained += qty;
res.emplace_back( &it, qty, 100 ); // @todo Use a calculated cost
}
} else {
res.emplace_back( &p.i_at( pos ), count, ( pos == -1 ) ? 0 : 100 ); // @todo Use a calculated cost
}
}
return res;
}
// Prepares items for dropping by reordering them so that the drop
// cost is minimal and "dependent" items get taken off first.
// Implements the "backpack" logic.
std::list<act_item> reorder_for_dropping( const player &p, const drop_indexes &drop )
{
auto res = convert_to_items( p, drop, -1, -1 );
auto inv = convert_to_items( p, drop, 0, INT_MAX );
auto worn = convert_to_items( p, drop, INT_MIN, -2 );
// Sort inventory items by volume in ascending order
inv.sort( []( const act_item & first, const act_item & second ) {
return first.it->volume() < second.it->volume();
} );
// Add missing dependent worn items (if any).
for( const auto &wait : worn ) {
for( const auto dit : p.get_dependent_worn_items( *wait.it ) ) {
const auto iter = std::find_if( worn.begin(), worn.end(),
[ dit ]( const act_item & ait ) {
return ait.it == dit;
} );
if( iter == worn.end() ) {
worn.emplace_front( dit, dit->count_by_charges() ? dit->charges : 1,
100 ); // @todo Use a calculated cost
}
}
}
// Sort worn items by storage in descending order, but dependent items always go first.
worn.sort( []( const act_item & first, const act_item & second ) {
return first.it->is_worn_only_with( *second.it )
|| ( ( first.it->get_storage() > second.it->get_storage() )
&& !second.it->is_worn_only_with( *first.it ) );
} );
units::volume storage_loss = 0; // Cumulatively increases
units::volume remaining_storage = p.volume_capacity(); // Cumulatively decreases
while( !worn.empty() && !inv.empty() ) {
storage_loss += worn.front().it->get_storage();
remaining_storage -= p.volume_capacity_reduced_by( storage_loss );
if( remaining_storage < inv.front().it->volume() ) {
break; // Does not fit
}
while( !inv.empty() && remaining_storage >= inv.front().it->volume() ) {
remaining_storage -= inv.front().it->volume();
res.push_back( inv.front() );
res.back().consumed_moves = 0; // Free of charge
inv.pop_front();
}
res.push_back( worn.front() );
worn.pop_front();
}
// Now insert everything that remains
std::copy( inv.begin(), inv.end(), std::back_inserter( res ) );
std::copy( worn.begin(), worn.end(), std::back_inserter( res ) );
return res;
}
//@todo Display costs in the multidrop menu
void debug_drop_list( const std::list<act_item> &list )
{
if( !debug_mode ) {
return;
}
std::string res( "Items ordered to drop:\n" );
for( const auto &ait : list ) {
res += string_format( "Drop %d %s for %d moves\n",
ait.count, ait.it->display_name( ait.count ).c_str(), ait.consumed_moves );
}
popup( res, PF_GET_KEY );
}
std::list<item> obtain_activity_items( player_activity &act, player &p )
{
std::list<item> res;
auto items = reorder_for_dropping( p, convert_to_indexes( act ) );
debug_drop_list( items );
while( !items.empty() && ( p.is_npc() || p.moves > 0 || items.front().consumed_moves == 0 ) ) {
const auto &ait = items.front();
p.mod_moves( -ait.consumed_moves );
if( p.is_worn( *ait.it ) ) {
p.takeoff( *ait.it, &res );
} else if( ait.it->count_by_charges() ) {
res.push_back( p.reduce_charges( const_cast<item *>( ait.it ), ait.count ) );
} else {
res.push_back( p.i_rem( ait.it ) );
}
items.pop_front();
}
// Avoid tumbling to the ground. Unload cleanly.
const units::volume excessive_volume = p.volume_carried() - p.volume_capacity();
if( excessive_volume > 0 ) {
const auto excess = p.inv.remove_randomly_by_volume( excessive_volume );
res.insert( res.begin(), excess.begin(), excess.end() );
}
// Load anything that remains (if any) into the activity
act.values.clear();
if( !items.empty() ) {
for( const auto &drop : convert_to_indexes( p, items ) ) {
act.values.push_back( drop.first );
act.values.push_back( drop.second );
}
}
// And either cancel if it's empty, or restart if it's not.
if( act.values.empty() ) {
p.cancel_activity();
} else {
p.assign_activity( act );
}
return res;
}
void activity_handlers::drop_do_turn( player_activity *act, player *p )
{
const tripoint pos = act->placement + p->pos();
put_into_vehicle_or_drop( *p, obtain_activity_items( *act, *p ), pos );
}
void activity_handlers::stash_do_turn( player_activity *act, player *p )
{
const tripoint pos = act->placement + p->pos();
monster *pet = g->critter_at<monster>( pos );
if( pet != nullptr && pet->has_effect( effect_pet ) ) {
stash_on_pet( obtain_activity_items( *act, *p ), *pet );
} else {
p->add_msg_if_player( _( "The pet has moved somewhere else." ) );
p->cancel_activity();
}
}
void activity_on_turn_pickup()
{
// Pickup activity has source square, bool indicating source type,
// indices of items on map, and quantities of same.
bool from_vehicle = g->u.activity.values.front();
tripoint pickup_target = g->u.activity.placement;
tripoint true_target = g->u.pos();
true_target += pickup_target;
// Auto_resume implies autopickup.
bool autopickup = g->u.activity.auto_resume;
std::list<int> indices;
std::list<int> quantities;
auto map_stack = g->m.i_at( true_target );
if( !from_vehicle && map_stack.empty() ) {
g->u.cancel_activity();
return;
}
// Note i = 1, skipping first element.
for( size_t i = 1; i < g->u.activity.values.size(); i += 2 ) {
indices.push_back( g->u.activity.values[i] );
quantities.push_back( g->u.activity.values[ i + 1 ] );
}
g->u.cancel_activity();
bool keep_going = Pickup::do_pickup( pickup_target, from_vehicle, indices, quantities, autopickup );
// If there are items left, we ran out of moves, so make a new activity with the remainder.
if( keep_going && !indices.empty() ) {
g->u.assign_activity( activity_id( "ACT_PICKUP" ) );
g->u.activity.placement = pickup_target;
g->u.activity.auto_resume = autopickup;
g->u.activity.values.push_back( from_vehicle );
while( !indices.empty() ) {
g->u.activity.values.push_back( indices.front() );
indices.pop_front();
g->u.activity.values.push_back( quantities.front() );
quantities.pop_front();
}
}
// @todo Move this to advanced inventory instead of hacking it in here
if( !keep_going ) {
cancel_aim_processing();
}
}
// I'd love to have this not duplicate so much code from Pickup::pick_one_up(),
// but I don't see a clean way to do that.
static void move_items( const tripoint &src, bool from_vehicle,
const tripoint &dest, bool to_vehicle,
std::list<int> &indices, std::list<int> &quantities )
{
tripoint source = src + g->u.pos();
tripoint destination = dest + g->u.pos();
int s_cargo, d_cargo; // oui oui, mon frere
s_cargo = d_cargo = -1;
vehicle *s_veh, *d_veh; // 2diva4me
s_veh = d_veh = nullptr;
// load vehicle information if requested
if( from_vehicle == true ) {
s_veh = g->m.veh_at( source, s_cargo );
assert( s_veh != nullptr );
s_cargo = s_veh->part_with_feature( s_cargo, "CARGO", false );
assert( s_cargo >= 0 );
}
if( to_vehicle == true ) {
d_veh = g->m.veh_at( destination, d_cargo );
assert( d_veh != nullptr );
d_cargo = d_veh->part_with_feature( d_cargo, "CARGO", false );
assert( d_cargo >= 0 );
}
while( g->u.moves > 0 && !indices.empty() ) {
int index = indices.back();
int quantity = quantities.back();
indices.pop_back();
quantities.pop_back();
item *temp_item = nullptr;
temp_item = ( from_vehicle == true ) ?
g->m.item_from( s_veh, s_cargo, index ) :
g->m.item_from( source, index );
if( temp_item == nullptr ) {
continue; // No such item.
}
item leftovers = *temp_item;
if( quantity != 0 && temp_item->count_by_charges() ) {
// Reinserting leftovers happens after item removal to avoid stacking issues.
leftovers.charges = temp_item->charges - quantity;
if( leftovers.charges > 0 ) {
temp_item->charges = quantity;
}
} else {
leftovers.charges = 0;
}
// Check that we can pick it up.
if( !temp_item->made_of( LIQUID ) ) {
g->u.mod_moves( -Pickup::cost_to_move_item( g->u, *temp_item ) );
if( to_vehicle ) {
put_into_vehicle_or_drop( g->u, { *temp_item }, destination );
} else {
drop_on_map( g->u, { *temp_item }, destination );
}
// Remove from map or vehicle.
if( from_vehicle == true ) {
s_veh->remove_item( s_cargo, index );
} else {
g->m.i_rem( source, index );
}
}
// If we didn't pick up a whole stack, put the remainder back where it came from.
if( leftovers.charges > 0 ) {
bool to_map = !from_vehicle;
if( !to_map ) {
to_map = !s_veh->add_item( s_cargo, leftovers );
}
if( to_map ) {
g->m.add_item_or_charges( source, leftovers );
}
}
}
}
/* values explanation
* 0: items from vehicle?
* 1: items to a vehicle?
* 2: index <-+
* 3: amount |
* n: ^-------+
*/
void activity_on_turn_move_items()
{
// Drop activity if we don't know destination coords.
if( g->u.activity.coords.empty() ) {
g->u.activity = player_activity();
return;
}
// Move activity has source square, target square,
// indices of items on map, and quantities of same.
const tripoint destination = g->u.activity.coords[0];
const tripoint source = g->u.activity.placement;
bool from_vehicle = g->u.activity.values[0];
bool to_vehicle = g->u.activity.values[1];
std::list<int> indices;
std::list<int> quantities;
// Note i = 4, skipping first few elements.
for( size_t i = 2; i < g->u.activity.values.size(); i += 2 ) {
indices.push_back( g->u.activity.values[i] );
quantities.push_back( g->u.activity.values[i + 1] );
}
// Nuke the current activity, leaving the backlog alone.
g->u.activity = player_activity();
// *puts on 3d glasses from 90s cereal box*
move_items( source, from_vehicle, destination, to_vehicle, indices, quantities );
if( !indices.empty() ) {
g->u.assign_activity( activity_id( "ACT_MOVE_ITEMS" ) );
g->u.activity.placement = source;
g->u.activity.coords.push_back( destination );
g->u.activity.values.push_back( from_vehicle );
g->u.activity.values.push_back( to_vehicle );
while( !indices.empty() ) {
g->u.activity.values.push_back( indices.front() );
indices.pop_front();
g->u.activity.values.push_back( quantities.front() );
quantities.pop_front();
}
}
}