forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_item_handling.cpp
2563 lines (2357 loc) · 109 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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "activity_handlers.h" // IWYU pragma: associated
#include <climits>
#include <algorithm>
#include <list>
#include <vector>
#include <iterator>
#include <memory>
#include <string>
#include <utility>
#include "avatar.h"
#include "construction.h"
#include "clzones.h"
#include "debug.h"
#include "enums.h"
#include "field.h"
#include "fire.h"
#include "game.h"
#include "iuse.h"
#include "iexamine.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "messages.h"
#include "monster.h"
#include "npc.h"
#include "optional.h"
#include "output.h"
#include "pickup.h"
#include "player.h"
#include "player_activity.h"
#include "requirements.h"
#include "string_formatter.h"
#include "translations.h"
#include "trap.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "calendar.h"
#include "character.h"
#include "game_constants.h"
#include "inventory.h"
#include "line.h"
#include "units.h"
#include "flat_set.h"
#include "int_id.h"
#include "item_location.h"
#include "point.h"
#include "string_id.h"
struct construction_category;
void cancel_aim_processing();
const efftype_id effect_controlled( "controlled" );
const efftype_id effect_pet( "pet" );
const zone_type_id zone_source_firewood( "SOURCE_FIREWOOD" );
const zone_type_id z_loot_unsorted( "LOOT_UNSORTED" );
const trap_str_id tr_firewood_source( "tr_firewood_source" );
const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
/** 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.
using drop_indexes = std::list<std::pair<int, int>>;
static 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;
} );
}
static void put_into_vehicle( Character &c, item_drop_reason reason, 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;
int into_vehicle_count = 0;
for( auto it : items ) { // cant use constant reference here because of the spill_contents()
if( Pickup::handle_spillable_contents( c, it, g->m ) ) {
continue;
}
if( veh.add_item( part, it ) ) {
into_vehicle_count += it.count();
} else {
if( it.count_by_charges() ) {
// Maybe we can add a few charges in the trunk and the rest on the ground.
auto charges_added = veh.add_charges( part, it );
it.mod_charges( -charges_added );
into_vehicle_count += charges_added;
}
g->m.add_item_or_charges( where, it );
fallen_count += it.count();
}
it.handle_pickup_ownership( c );
}
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();
const std::string it_name = it.tname( dropcount );
switch( reason ) {
case item_drop_reason::deliberate:
c.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_name, veh.name, part_name
);
break;
case item_drop_reason::too_large:
c.add_msg_if_player(
ngettext(
"There's no room in your inventory for the %s, so you drop it into the %s's %s.",
"There's no room in your inventory for the %s, so you drop them into the %s's %s.",
dropcount ),
it_name, veh.name, part_name
);
break;
case item_drop_reason::too_heavy:
c.add_msg_if_player(
ngettext( "The %s is too heavy to carry, so you drop it into the %s's %s.",
"The %s are too heavy to carry, so you drop them into the %s's %s.", dropcount ),
it_name, veh.name, part_name
);
break;
case item_drop_reason::tumbling:
c.add_msg_if_player(
m_bad,
ngettext( "Your %s tumbles into the %s's %s.",
"Your %s tumble into the %s's %s.", dropcount ),
it_name, veh.name, part_name
);
break;
}
} else {
switch( reason ) {
case item_drop_reason::deliberate:
c.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, part_name
);
break;
case item_drop_reason::too_large:
case item_drop_reason::too_heavy:
case item_drop_reason::tumbling:
c.add_msg_if_player(
m_bad, _( "Some items tumble into the %1$s's %2$s." ),
veh.name, part_name
);
break;
}
}
if( fallen_count > 0 ) {
if( into_vehicle_count > 0 ) {
c.add_msg_if_player(
m_warning,
ngettext( "The %s is full, so something fell to the %s.",
"The %s is full, so some items fell to the %s.", fallen_count ),
part_name, ter_name
);
} else {
c.add_msg_if_player(
m_warning,
ngettext( "The %s is full, so it fell to the %s.",
"The %s is full, so they fell to the %s.", fallen_count ),
part_name, ter_name
);
}
}
}
static void pass_to_ownership_handling( item obj, Character &c )
{
obj.handle_pickup_ownership( c );
}
static void pass_to_ownership_handling( item obj, player *p )
{
obj.handle_pickup_ownership( *p );
}
static void stash_on_pet( const std::list<item> &items, monster &pet, player *p )
{
units::volume remaining_volume = pet.inv.empty() ? 0_ml : pet.inv.front().get_storage();
units::mass 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_turns );
if( it.volume() > remaining_volume ) {
add_msg( m_bad, _( "%1$s did not fit and fell to the %2$s." ),
it.display_name(), g->m.name( pet.pos() ) );
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(), g->m.name( pet.pos() ) );
g->m.add_item_or_charges( pet.pos(), it );
} else {
pet.add_item( it );
remaining_volume -= it.volume();
remaining_weight -= it.weight();
}
// TODO: if NPCs can have pets or move items onto pets
pass_to_ownership_handling( it, p );
}
}
void drop_on_map( Character &c, item_drop_reason reason, 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();
const std::string it_name = it.tname( dropcount );
switch( reason ) {
case item_drop_reason::deliberate:
if( can_move_there ) {
c.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, ter_name
);
} else {
c.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, ter_name
);
}
break;
case item_drop_reason::too_large:
c.add_msg_if_player(
ngettext( "There's no room in your inventory for the %s, so you drop it.",
"There's no room in your inventory for the %s, so you drop them.", dropcount ),
it_name
);
break;
case item_drop_reason::too_heavy:
c.add_msg_if_player(
ngettext( "The %s is too heavy to carry, so you drop it.",
"The %s is too heavy to carry, so you drop them.", dropcount ),
it_name
);
break;
case item_drop_reason::tumbling:
c.add_msg_if_player(
m_bad,
ngettext( "Your %1$s tumbles to the %2$s.",
"Your %1$s tumble to the %2$s.", dropcount ),
it_name, ter_name
);
break;
}
} else {
switch( reason ) {
case item_drop_reason::deliberate:
if( can_move_there ) {
c.add_msg_player_or_npc(
_( "You drop several items on the %s." ),
_( "<npcname> drops several items on the %s." ),
ter_name
);
} else {
c.add_msg_player_or_npc(
_( "You put several items in the %s." ),
_( "<npcname> puts several items in the %s." ),
ter_name
);
}
break;
case item_drop_reason::too_large:
case item_drop_reason::too_heavy:
case item_drop_reason::tumbling:
c.add_msg_if_player( m_bad, _( "Some items tumble to the %s." ), ter_name );
break;
}
}
for( auto &it : items ) {
g->m.add_item_or_charges( where, it );
pass_to_ownership_handling( it, c );
}
}
void put_into_vehicle_or_drop( Character &c, item_drop_reason reason, const std::list<item> &items )
{
return put_into_vehicle_or_drop( c, reason, items, c.pos() );
}
void put_into_vehicle_or_drop( Character &c, item_drop_reason reason, const std::list<item> &items,
const tripoint &where, bool force_ground )
{
const cata::optional<vpart_reference> vp = g->m.veh_at( where ).part_with_feature( "CARGO", false );
if( vp && !force_ground ) {
put_into_vehicle( c, reason, items, vp->vehicle(), vp->part_index() );
return;
}
drop_on_map( c, reason, items, where );
}
static 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;
}
static 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;
}
static 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.
static 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(), 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_ml; // 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 );
units::volume inventory_item_volume = inv.front().it->volume();
if( remaining_storage < inventory_item_volume ) {
break; // Does not fit
}
while( !inv.empty() && remaining_storage >= inventory_item_volume ) {
remaining_storage -= inventory_item_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
static 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 ), ait.consumed_moves );
}
popup( res, PF_GET_KEY );
}
static 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_ml ) {
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 cancel if its empty. If its not, we modified in place and we will continue
// to resolve the drop next turn. This is different from the pickup logic which
// creates a brand new activity every turn and cancels the old activity
if( act.values.empty() ) {
p.cancel_activity();
}
return res;
}
void activity_handlers::drop_do_turn( player_activity *act, player *p )
{
const tripoint pos = act->placement + p->pos();
bool force_ground = false;
for( auto &it : act->str_values ) {
if( it == "force_ground" ) {
force_ground = true;
break;
}
}
put_into_vehicle_or_drop( *p, item_drop_reason::deliberate, obtain_activity_items( *act, *p ),
pos, force_ground );
}
void activity_on_turn_wear( player_activity &act, player &p )
{
// ACT_WEAR has item_location targets, and int quatities
while( p.moves > 0 && !act.targets.empty() ) {
item_location target = std::move( act.targets.back() );
int quantity = act.values.back();
act.targets.pop_back();
act.values.pop_back();
if( !target ) {
debugmsg( "Lost target item of ACT_WEAR" );
continue;
}
// Make copies so the original remains untouched if wearing fails
item newit = *target;
item leftovers = newit;
// Handle charges, quantity == 0 means move all
if( quantity != 0 && newit.count_by_charges() ) {
leftovers.charges = newit.charges - quantity;
if( leftovers.charges > 0 ) {
newit.charges = quantity;
}
} else {
leftovers.charges = 0;
}
if( p.wear_item( newit ) ) {
// If we wore up a whole stack, remove the original item
// Otherwise, replace the item with the leftovers
if( leftovers.charges > 0 ) {
*target = std::move( leftovers );
} else {
target.remove_item();
}
}
}
// If there are no items left we are done
if( act.targets.empty() ) {
p.cancel_activity();
}
}
void activity_handlers::washing_finish( player_activity *act, player *p )
{
auto items = reorder_for_dropping( *p, convert_to_indexes( *act ) );
// Check again that we have enough water and soap incase the amount in our inventory changed somehow
// Consume the water and soap
units::volume total_volume = 0_ml;
for( const act_item &filthy_item : items ) {
total_volume += filthy_item.it->volume();
}
washing_requirements required = washing_requirements_for_volume( total_volume );
const auto is_liquid_crafting_component = []( const item & it ) {
return is_crafting_component( it ) && ( !it.count_by_charges() || it.made_of( LIQUID ) ||
it.contents_made_of( LIQUID ) );
};
const inventory &crafting_inv = p->crafting_inventory();
if( !crafting_inv.has_charges( "water", required.water, is_liquid_crafting_component ) &&
!crafting_inv.has_charges( "water_clean", required.water, is_liquid_crafting_component ) ) {
p->add_msg_if_player( _( "You need %1$i charges of water or clean water to wash these items." ),
required.water );
act->set_to_null();
return;
} else if( !crafting_inv.has_charges( "soap", required.cleanser ) &&
!crafting_inv.has_charges( "detergent", required.cleanser ) ) {
p->add_msg_if_player( _( "You need %1$i charges of cleansing agent to wash these items." ),
required.cleanser );
act->set_to_null();
return;
}
for( const auto &ait : items ) {
item *filthy_item = const_cast<item *>( ait.it );
filthy_item->item_tags.erase( "FILTHY" );
p->on_worn_item_washed( *filthy_item );
}
std::vector<item_comp> comps;
comps.push_back( item_comp( "water", required.water ) );
comps.push_back( item_comp( "water_clean", required.water ) );
p->consume_items( comps, 1, is_liquid_crafting_component );
std::vector<item_comp> comps1;
comps1.push_back( item_comp( "soap", required.cleanser ) );
comps1.push_back( item_comp( "detergent", required.cleanser ) );
p->consume_items( comps1 );
p->add_msg_if_player( m_good, _( "You washed your items." ) );
// Make sure newly washed components show up as available if player attempts to craft immediately
p->invalidate_crafting_inventory();
act->set_to_null();
}
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, p );
} else {
p->add_msg_if_player( _( "The pet has moved somewhere else." ) );
p->cancel_activity();
}
}
void activity_on_turn_pickup()
{
// ACT_PICKUP has item_locations of target items and quantities of the same.
// If we don't have target items bail out
if( g->u.activity.targets.empty() ) {
g->u.cancel_activity();
return;
}
// Auto_resume implies autopickup.
const bool autopickup = g->u.activity.auto_resume;
// False indicates that the player canceled pickup when met with some prompt
const bool keep_going = Pickup::do_pickup( g->u.activity.targets, g->u.activity.values,
autopickup );
// If there are items left we ran out of moves, so continue the activity
// Otherwise, we are done.
if( !keep_going || g->u.activity.targets.empty() ) {
g->u.cancel_activity();
if( g->u.get_value( "THIEF_MODE_KEEP" ) != "YES" ) {
g->u.set_value( "THIEF_MODE", "THIEF_ASK" );
}
}
// TODO: Move this to advanced inventory instead of hacking it in here
if( !keep_going ) {
// The user canceled the activity, so we're done
g->u.cancel_activity();
// AIM might have more pickup activities pending, also cancel them.
// TODO: Move this to advanced inventory instead of hacking it in here
cancel_aim_processing();
} else if( g->u.activity.targets.empty() ) {
// The user did not cancel, but there's no item left
g->u.cancel_activity();
// But do not cancel AIM processing as it might have more pickup activities
// pending for other locations.
}
}
// 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( player &p, const tripoint &relative_dest, bool to_vehicle,
std::vector<item_location> &targets, std::vector<int> &quantities )
{
const tripoint dest = relative_dest + p.pos();
while( p.moves > 0 && !targets.empty() ) {
item_location target = std::move( targets.back() );
int quantity = quantities.back();
targets.pop_back();
quantities.pop_back();
if( !target ) {
debugmsg( "Lost target item of ACT_MOVE_ITEMS" );
continue;
}
// Don't need to make a copy here since movement can't be canceled
item &leftovers = *target;
// Make a copy to be put in the destination location
item newit = leftovers;
// Handle charges, quantity == 0 means move all
if( quantity != 0 && newit.count_by_charges() ) {
newit.charges = std::min( newit.charges, quantity );
leftovers.charges -= quantity;
} else {
leftovers.charges = 0;
}
// Check that we can pick it up.
if( !newit.made_of_from_type( LIQUID ) ) {
// This is for hauling across zlevels, remove when going up and down stairs
// is no longer teleportation
if( !newit.has_owner() && p.is_player() ) {
newit.set_owner( p.get_faction() );
}
const tripoint src = target.position();
int distance = src.z == dest.z ? std::max( rl_dist( src, dest ), 1 ) : 1;
p.mod_moves( -Pickup::cost_to_move_item( p, newit ) * distance );
if( to_vehicle ) {
put_into_vehicle_or_drop( p, item_drop_reason::deliberate, { newit }, dest );
} else {
drop_on_map( p, item_drop_reason::deliberate, { newit }, dest );
}
// If we picked up a whole stack, remove the leftover item
if( leftovers.charges <= 0 ) {
target.remove_item();
}
}
}
}
/* values explanation
* 0: items to a vehicle?
* 1: amount 0 <-+
* 2: amount 1 |
* n: ^----------+
*
* targets correspond to amounts
*/
void activity_on_turn_move_items( player_activity &act, player &p )
{
// Drop activity if we don't know destination coordinates or have target items.
if( act.coords.empty() || act.targets.empty() ) {
act.set_to_null();
return;
}
// Move activity has source square, target square,
// item_locations of targets, and quantities of same.
const tripoint relative_dest = act.coords.front();
const bool to_vehicle = act.values.front();
// *puts on 3d glasses from 90s cereal box*
move_items( p, relative_dest, to_vehicle, act.targets, act.values );
if( act.targets.empty() ) {
// Nuke the current activity, leaving the backlog alone.
act.set_to_null();
}
}
static double get_capacity_fraction( int capacity, int volume )
{
// fration of capacity the item would occupy
// fr = 1 is for capacity smaller than is size of item
// in such case, let's assume player does the trip for full cost with item in hands
double fr = 1;
if( capacity > volume ) {
fr = static_cast<double>( volume ) / capacity;
}
return fr;
}
static int move_cost_inv( const item &it, const tripoint &src, const tripoint &dest )
{
// to prevent potentially ridiculous number
const int MAX_COST = 500;
// it seems that pickup cost is flat 100
// in function pick_one_up, varible moves_taken has initial value of 100
// and never changes until it is finally used in function
// remove_from_map_or_vehicle
const int pickup_cost = 100;
// drop cost for non-tumbling items (from inventory overload) is also flat 100
// according to convert_to_items (it does contain todo to use calculated costs)
const int drop_cost = 100;
// typical flat ground move cost
const int mc_per_tile = 100;
// only free inventory capacity
const int inventory_capacity = units::to_milliliter( g->u.volume_capacity() -
g->u.volume_carried() );
const int item_volume = units::to_milliliter( it.volume() );
const double fr = get_capacity_fraction( inventory_capacity, item_volume );
// approximation of movement cost between source and destination
const int move_cost = mc_per_tile * rl_dist( src, dest ) * fr;
return std::min( pickup_cost + drop_cost + move_cost, MAX_COST );
}
static int move_cost_cart( const item &it, const tripoint &src, const tripoint &dest,
const units::volume &capacity )
{
// to prevent potentially ridiculous number
const int MAX_COST = 500;
// cost to move item into the cart
const int pickup_cost = Pickup::cost_to_move_item( g->u, it );
// cost to move item out of the cart
const int drop_cost = pickup_cost;
// typical flat ground move cost
const int mc_per_tile = 100;
// only free cart capacity
const int cart_capacity = units::to_milliliter( capacity );
const int item_volume = units::to_milliliter( it.volume() );
const double fr = get_capacity_fraction( cart_capacity, item_volume );
// approximation of movement cost between source and destination
const int move_cost = mc_per_tile * rl_dist( src, dest ) * fr;
return std::min( pickup_cost + drop_cost + move_cost, MAX_COST );
}
static int move_cost( const item &it, const tripoint &src, const tripoint &dest )
{
if( g->u.get_grab_type() == OBJECT_VEHICLE ) {
tripoint cart_position = g->u.pos() + g->u.grab_point;
if( const cata::optional<vpart_reference> vp = g->m.veh_at(
cart_position ).part_with_feature( "CARGO", false ) ) {
auto veh = vp->vehicle();
auto vstor = vp->part_index();
auto capacity = veh.free_volume( vstor );
return move_cost_cart( it, src, dest, capacity );
}
}
return move_cost_inv( it, src, dest );
}
static void move_item( player &p, item &it, int quantity, const tripoint &src,
const tripoint &dest, vehicle *src_veh, int src_part,
activity_id activity_to_restore = activity_id::NULL_ID() )
{
item leftovers = it;
if( quantity != 0 && it.count_by_charges() ) {
// Reinserting leftovers happens after item removal to avoid stacking issues.
leftovers.charges = it.charges - quantity;
if( leftovers.charges > 0 ) {
it.charges = quantity;
}
} else {
leftovers.charges = 0;
}
// Check that we can pick it up.
if( !it.made_of_from_type( LIQUID ) ) {
p.mod_moves( -move_cost( it, src, dest ) );
if( activity_to_restore == activity_id( "ACT_TIDY_UP" ) ) {
it.erase_var( "activity_var" );
} else if( activity_to_restore == activity_id( "ACT_FETCH_REQUIRED" ) ) {
it.set_var( "activity_var", p.name );
}
put_into_vehicle_or_drop( p, item_drop_reason::deliberate, { it }, dest );
// Remove from map or vehicle.
if( src_veh ) {
src_veh->remove_item( src_part, &it );
} else {
g->m.i_rem( src, &it );
}
}
// If we didn't pick up a whole stack, put the remainder back where it came from.
if( leftovers.charges > 0 ) {
if( src_veh ) {
if( !src_veh->add_item( src_part, leftovers ) ) {
debugmsg( "SortLoot: Source vehicle failed to receive leftover charges." );
}
} else {
g->m.add_item_or_charges( src, leftovers );
}
}
}
std::vector<tripoint> route_adjacent( const player &p, const tripoint &dest )
{
auto passable_tiles = std::unordered_set<tripoint>();
for( const tripoint &tp : g->m.points_in_radius( dest, 1 ) ) {
if( tp != p.pos() && g->m.passable( tp ) ) {
passable_tiles.emplace( tp );
}
}
const auto &sorted = get_sorted_tiles_by_distance( p.pos(), passable_tiles );
const auto &avoid = p.get_path_avoid();
for( const tripoint &tp : sorted ) {
auto route = g->m.route( p.pos(), tp, p.get_pathfinding_settings(), avoid );
if( !route.empty() ) {
return route;
}
}
return std::vector<tripoint>();
}
static activity_reason_info find_base_construction(
const std::vector<construction> &list_constructions,
player &p,
const inventory &inv,
const tripoint &loc,
const cata::optional<size_t> part_con_idx,
const size_t idx,
std::set<size_t> &used )
{
const construction &build = list_constructions[idx];
//already done?
const furn_id furn = g->m.furn( loc );
const ter_id ter = g->m.ter( loc );
if( !build.post_terrain.empty() ) {
if( build.post_is_furniture ) {
if( furn_id( build.post_terrain ) == furn ) {
return activity_reason_info::build( ALREADY_DONE, false, idx );
}
} else {
if( ter_id( build.post_terrain ) == ter ) {
return activity_reason_info::build( ALREADY_DONE, false, idx );
}
}
}
//if theres an apropriate partial construction on the tile, then we can work on it, no need to check inventories.
const bool has_skill = p.meets_skill_requirements( build );
if( part_con_idx && *part_con_idx == idx ) {
if( !has_skill ) {
return activity_reason_info::build( DONT_HAVE_SKILL, false, idx );
}
return activity_reason_info::build( CAN_DO_CONSTRUCTION, true, idx );
}
//can build?
const bool cc = can_construct( build, loc );
const bool pcb = player_can_build( p, inv, build );
if( !has_skill ) {
return activity_reason_info::build( DONT_HAVE_SKILL, false, idx );
}
if( cc ) {
if( pcb ) {
return activity_reason_info::build( CAN_DO_CONSTRUCTION, true, idx );
}
//can't build with current inventory, do not look for pre-req
return activity_reason_info::build( NO_COMPONENTS, false, idx );
}
// there are no pre-requisites.
// so we need to potentially fetch components
if( build.pre_terrain.empty() && build.pre_special( loc ) ) {
return activity_reason_info::build( NO_COMPONENTS, false, idx );
} else if( !build.pre_special( loc ) ) {
return activity_reason_info::build( BLOCKING_TILE, false, idx );
}
// cant build it
// maybe we can build the pre-requisite instead