forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_item_handling.cpp
3329 lines (3123 loc) · 147 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 <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <iosfwd>
#include <list>
#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "activity_type.h"
#include "avatar.h"
#include "calendar.h"
#include "character.h"
#include "clzones.h"
#include "colony.h"
#include "construction.h"
#include "contents_change_handler.h"
#include "creature.h"
#include "creature_tracker.h"
#include "debug.h"
#include "enums.h"
#include "field.h"
#include "field_type.h"
#include "fire.h"
#include "flag.h"
#include "game.h"
#include "game_constants.h"
#include "iexamine.h"
#include "inventory.h"
#include "item.h"
#include "item_location.h"
#include "itype.h"
#include "iuse.h"
#include "line.h"
#include "make_static.h"
#include "map.h"
#include "map_iterator.h"
#include "map_selector.h"
#include "mapdata.h"
#include "messages.h"
#include "mtype.h"
#include "npc.h"
#include "optional.h"
#include "pickup.h"
#include "player_activity.h"
#include "point.h"
#include "requirements.h"
#include "ret_val.h"
#include "rng.h"
#include "stomach.h"
#include "temp_crafting_inventory.h"
#include "translations.h"
#include "trap.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
#include "weather.h"
#include "recipe_dictionary.h"
#include "activity_actor_definitions.h"
static const activity_id ACT_BUILD( "ACT_BUILD" );
static const activity_id ACT_BUTCHER_FULL( "ACT_BUTCHER_FULL" );
static const activity_id ACT_FETCH_REQUIRED( "ACT_FETCH_REQUIRED" );
static const activity_id ACT_FISH( "ACT_FISH" );
static const activity_id ACT_JACKHAMMER( "ACT_JACKHAMMER" );
static const activity_id ACT_MOVE_LOOT( "ACT_MOVE_LOOT" );
static const activity_id ACT_MULTIPLE_BUTCHER( "ACT_MULTIPLE_BUTCHER" );
static const activity_id ACT_MULTIPLE_CHOP_PLANKS( "ACT_MULTIPLE_CHOP_PLANKS" );
static const activity_id ACT_MULTIPLE_CHOP_TREES( "ACT_MULTIPLE_CHOP_TREES" );
static const activity_id ACT_MULTIPLE_CONSTRUCTION( "ACT_MULTIPLE_CONSTRUCTION" );
static const activity_id ACT_MULTIPLE_DIS( "ACT_MULTIPLE_DIS" );
static const activity_id ACT_MULTIPLE_FARM( "ACT_MULTIPLE_FARM" );
static const activity_id ACT_MULTIPLE_FISH( "ACT_MULTIPLE_FISH" );
static const activity_id ACT_MULTIPLE_MINE( "ACT_MULTIPLE_MINE" );
static const activity_id ACT_MULTIPLE_MOP( "ACT_MULTIPLE_MOP" );
static const activity_id ACT_PICKAXE( "ACT_PICKAXE" );
static const activity_id ACT_TIDY_UP( "ACT_TIDY_UP" );
static const activity_id ACT_VEHICLE( "ACT_VEHICLE" );
static const activity_id ACT_VEHICLE_DECONSTRUCTION( "ACT_VEHICLE_DECONSTRUCTION" );
static const activity_id ACT_VEHICLE_REPAIR( "ACT_VEHICLE_REPAIR" );
static const efftype_id effect_incorporeal( "incorporeal" );
static const flag_id json_flag_MOP( "MOP" );
static const flag_id json_flag_NO_AUTO_CONSUME( "NO_AUTO_CONSUME" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_detergent( "detergent" );
static const itype_id itype_disassembly( "disassembly" );
static const itype_id itype_liquid_soap( "liquid_soap" );
static const itype_id itype_log( "log" );
static const itype_id itype_soap( "soap" );
static const itype_id itype_soldering_iron( "soldering_iron" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_clean( "water_clean" );
static const itype_id itype_welder( "welder" );
static const quality_id qual_AXE( "AXE" );
static const quality_id qual_BUTCHER( "BUTCHER" );
static const quality_id qual_DIG( "DIG" );
static const quality_id qual_FISHING( "FISHING" );
static const quality_id qual_SAW_M( "SAW_M" );
static const quality_id qual_SAW_W( "SAW_W" );
static const quality_id qual_WELD( "WELD" );
static const requirement_id requirement_data_mining_standard( "mining_standard" );
static const trap_str_id tr_firewood_source( "tr_firewood_source" );
static const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
static const zone_type_id zone_type_( "" );
static const zone_type_id zone_type_AUTO_DRINK( "AUTO_DRINK" );
static const zone_type_id zone_type_AUTO_EAT( "AUTO_EAT" );
static const zone_type_id zone_type_CAMP_STORAGE( "CAMP_STORAGE" );
static const zone_type_id zone_type_CHOP_TREES( "CHOP_TREES" );
static const zone_type_id zone_type_CONSTRUCTION_BLUEPRINT( "CONSTRUCTION_BLUEPRINT" );
static const zone_type_id zone_type_FARM_PLOT( "FARM_PLOT" );
static const zone_type_id zone_type_FISHING_SPOT( "FISHING_SPOT" );
static const zone_type_id zone_type_LOOT_CORPSE( "LOOT_CORPSE" );
static const zone_type_id zone_type_LOOT_CUSTOM( "LOOT_CUSTOM" );
static const zone_type_id zone_type_LOOT_IGNORE( "LOOT_IGNORE" );
static const zone_type_id zone_type_LOOT_IGNORE_FAVORITES( "LOOT_IGNORE_FAVORITES" );
static const zone_type_id zone_type_LOOT_UNSORTED( "LOOT_UNSORTED" );
static const zone_type_id zone_type_LOOT_WOOD( "LOOT_WOOD" );
static const zone_type_id zone_type_MINING( "MINING" );
static const zone_type_id zone_type_MOPPING( "MOPPING" );
static const zone_type_id zone_type_SOURCE_FIREWOOD( "SOURCE_FIREWOOD" );
static const zone_type_id zone_type_VEHICLE_DECONSTRUCT( "VEHICLE_DECONSTRUCT" );
static const zone_type_id zone_type_VEHICLE_REPAIR( "VEHICLE_REPAIR" );
static const zone_type_id zone_type_zone_disassemble( "zone_disassemble" );
static const zone_type_id zone_type_zone_strip( "zone_strip" );
static const zone_type_id zone_type_zone_unload_all( "zone_unload_all" );
namespace
{
faction_id _fac_id( Character &you )
{
faction const *fac = you.get_faction();
return fac == nullptr ? faction_id() : fac->id;
}
} // namespace
/** Activity-associated item */
struct act_item {
/// inventory item
item_location loc;
/// How many items need to be processed
int count;
/// Amount of moves that processing will consume
int consumed_moves;
act_item( const item_location &loc, int count, int consumed_moves )
: loc( loc ),
count( count ),
consumed_moves( consumed_moves ) {}
};
// TODO: Deliberately unified with multidrop. Unify further.
using drop_location = std::pair<item_location, int>;
using drop_locations = std::list<std::pair<item_location, 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;
} );
}
// Deprecated. See `contents_change_handler` and `Character::handle_contents_changed`
// for contents handling with item_location.
static bool handle_spillable_contents( Character &c, item &it, map &m )
{
if( it.is_bucket_nonempty() ) {
it.spill_open_pockets( c, /*avoid=*/&it );
// If bucket is still not empty then player opted not to handle the
// rest of the contents
if( !it.empty() ) {
c.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(), m.name( c.pos() )
);
m.add_item_or_charges( c.pos(), it );
return true;
}
}
return false;
}
static void put_into_vehicle( Character &c, item_drop_reason reason, const std::list<item> &items,
vehicle &veh, int part )
{
c.invalidate_weight_carried_cache();
if( items.empty() ) {
return;
}
const tripoint where = veh.global_part_pos3( part );
map &here = get_map();
const std::string ter_name = here.name( where );
int fallen_count = 0;
int into_vehicle_count = 0;
// can't use constant reference here because of the spill_contents()
for( item it : items ) {
if( handle_spillable_contents( c, it, here ) ) {
continue;
}
if( it.made_of( phase_id::LIQUID ) ) {
here.add_item_or_charges( c.pos(), it );
it.charges = 0;
}
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.
int charges_added = veh.add_charges( part, it );
it.mod_charges( -charges_added );
into_vehicle_count += charges_added;
}
here.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(
n_gettext( "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 ),
n_gettext( "<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(
n_gettext(
"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(
n_gettext( "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,
n_gettext( "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,
n_gettext( "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,
n_gettext( "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
);
}
}
}
void drop_on_map( Character &you, item_drop_reason reason, const std::list<item> &items,
const tripoint &where )
{
you.invalidate_weight_carried_cache();
if( items.empty() ) {
return;
}
map &here = get_map();
const std::string ter_name = here.name( where );
const bool can_move_there = here.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 ) {
you.add_msg_player_or_npc(
n_gettext( "You drop your %1$s on the %2$s.",
"You drop your %1$s on the %2$s.", dropcount ),
n_gettext( "<npcname> drops their %1$s on the %2$s.",
"<npcname> drops their %1$s on the %2$s.", dropcount ),
it_name, ter_name
);
} else {
you.add_msg_player_or_npc(
n_gettext( "You put your %1$s in the %2$s.",
"You put your %1$s in the %2$s.", dropcount ),
n_gettext( "<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:
you.add_msg_if_player(
n_gettext( "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:
you.add_msg_if_player(
n_gettext( "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:
you.add_msg_if_player(
m_bad,
n_gettext( "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 ) {
you.add_msg_player_or_npc(
_( "You drop several items on the %s." ),
_( "<npcname> drops several items on the %s." ),
ter_name
);
} else {
you.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:
you.add_msg_if_player( m_bad, _( "Some items tumble to the %s." ), ter_name );
break;
}
}
for( const item &it : items ) {
here.add_item_or_charges( where, it );
item( it ).handle_pickup_ownership( you );
}
you.recoil = MAX_RECOIL;
}
void put_into_vehicle_or_drop( Character &you, item_drop_reason reason,
const std::list<item> &items )
{
return put_into_vehicle_or_drop( you, reason, items, you.pos() );
}
void put_into_vehicle_or_drop( Character &you, item_drop_reason reason,
const std::list<item> &items,
const tripoint &where, bool force_ground )
{
map &here = get_map();
const cata::optional<vpart_reference> vp = here.veh_at( where ).part_with_feature( "CARGO", false );
if( vp && !force_ground ) {
put_into_vehicle( you, reason, items, vp->vehicle(), vp->part_index() );
return;
}
drop_on_map( you, reason, items, where );
}
static std::list<act_item> convert_to_act_item( const player_activity &act, Character &guy )
{
std::list<act_item> res;
if( act.values.size() != act.targets.size() ) {
debugmsg( "Drop/stash activity contains an odd number of values." );
return res;
}
for( size_t i = 0; i < act.values.size(); i++ ) {
// locations may have become invalid as items are forcefully dropped
// when they exceed the storage volume of the character
if( !act.targets[i] || !act.targets[i].get_item() ) {
continue;
}
res.emplace_back( act.targets[i], act.values[i], act.targets[i].obtain_cost( guy, act.values[i] ) );
}
return res;
}
void activity_handlers::washing_finish( player_activity *act, Character *you )
{
std::list<act_item> items = convert_to_act_item( *act, *you );
// 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 ) {
int count = filthy_item.loc->count_by_charges() ? filthy_item.count : -1;
total_volume += filthy_item.loc->volume( false, true, count );
}
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( phase_id::LIQUID ) );
};
const inventory &crafting_inv = you->crafting_inventory();
if( !crafting_inv.has_charges( itype_water, required.water, is_liquid_crafting_component ) &&
!crafting_inv.has_charges( itype_water_clean, required.water, is_liquid_crafting_component ) ) {
you->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( itype_soap, required.cleanser ) &&
!crafting_inv.has_charges( itype_detergent, required.cleanser ) &&
!crafting_inv.has_charges( itype_liquid_soap, required.cleanser,
is_liquid_crafting_component ) ) {
you->add_msg_if_player( _( "You need %1$i charges of cleansing agent to wash these items." ),
required.cleanser );
act->set_to_null();
return;
}
for( act_item &ait : items ) {
item *filthy_item = const_cast<item *>( &*ait.loc );
if( filthy_item->count_by_charges() ) {
item copy( *filthy_item );
copy.charges = ait.count;
copy.unset_flag( flag_FILTHY );
filthy_item->charges -= ait.count;
if( filthy_item->charges <= 0 ) {
ait.loc.remove_item();
}
you->i_add_or_drop( copy );
} else {
filthy_item->unset_flag( flag_FILTHY );
you->on_worn_item_washed( *filthy_item );
}
}
std::vector<item_comp> comps;
comps.emplace_back( itype_water, required.water );
comps.emplace_back( itype_water_clean, required.water );
you->consume_items( comps, 1, is_liquid_crafting_component );
std::vector<item_comp> comps1;
comps1.emplace_back( itype_soap, required.cleanser );
comps1.emplace_back( itype_detergent, required.cleanser );
comps1.emplace_back( itype_liquid_soap, required.cleanser );
you->consume_items( comps1 );
you->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
you->invalidate_crafting_inventory();
act->set_to_null();
}
static double get_capacity_fraction( int capacity, int volume )
{
// fraction 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, variable 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;
Character &player_character = get_player_character();
// only free inventory capacity
const int inventory_capacity = units::to_milliliter( player_character.volume_capacity() -
player_character.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( get_player_character(), 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 )
{
avatar &player_character = get_avatar();
if( player_character.get_grab_type() == object_type::VEHICLE ) {
tripoint cart_position = player_character.pos() + player_character.grab_point;
if( const cata::optional<vpart_reference> vp = get_map().veh_at(
cart_position ).part_with_feature( "CARGO", false ) ) {
const vehicle &veh = vp->vehicle();
size_t vstor = vp->part_index();
units::volume capacity = veh.free_volume( vstor );
return move_cost_cart( it, src, dest, capacity );
}
}
return move_cost_inv( it, src, dest );
}
// return true if activity was assigned.
// return false if it was not possible.
static bool vehicle_activity( Character &you, const tripoint &src_loc, int vpindex, char type )
{
map &here = get_map();
vehicle *veh = veh_pointer_or_null( here.veh_at( src_loc ) );
if( !veh ) {
return false;
}
int time_to_take = 0;
if( vpindex >= veh->part_count() ) {
// if parts got removed during our work, we can't just carry on removing, we want to repair parts!
// so just bail out, as we don't know if the next shifted part is suitable for repair.
if( type == 'r' ) {
return false;
} else if( type == 'o' ) {
vpindex = veh->get_next_shifted_index( vpindex, you );
if( vpindex == -1 ) {
return false;
}
}
}
const vpart_info &vp = veh->part_info( vpindex );
if( type == 'r' ) {
const vehicle_part part = veh->part( vpindex );
time_to_take = vp.repair_time( you ) * ( part.damage() - part.degradation() ) /
( part.max_damage() - part.degradation() );
} else if( type == 'o' ) {
time_to_take = vp.removal_time( you );
}
you.assign_activity( ACT_VEHICLE, time_to_take, static_cast<int>( type ) );
// so , NPCs can remove the last part on a position, then there is no vehicle there anymore,
// for someone else who stored that position at the start of their activity.
// so we may need to go looking a bit further afield to find it , at activities end.
for( const tripoint &pt : veh->get_points( true ) ) {
you.activity.coord_set.insert( here.getabs( pt ) );
}
// values[0]
you.activity.values.push_back( here.getabs( src_loc ).x );
// values[1]
you.activity.values.push_back( here.getabs( src_loc ).y );
// values[2]
you.activity.values.push_back( point_zero.x );
// values[3]
you.activity.values.push_back( point_zero.y );
// values[4]
you.activity.values.push_back( -point_zero.x );
// values[5]
you.activity.values.push_back( -point_zero.y );
// values[6]
you.activity.values.push_back( veh->index_of_part( &veh->part( vpindex ) ) );
you.activity.str_values.push_back( vp.get_id().str() );
// this would only be used for refilling tasks
item_location target;
you.activity.targets.emplace_back( std::move( target ) );
you.activity.placement = here.getabs( src_loc );
you.activity_vehicle_part_index = -1;
return true;
}
static void move_item( Character &you, item &it, const int quantity, const tripoint &src,
const tripoint &dest, vehicle *src_veh, int src_part,
const 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;
}
map &here = get_map();
// Check that we can pick it up.
if( !it.made_of_from_type( phase_id::LIQUID ) ) {
you.mod_moves( -move_cost( it, src, dest ) );
if( activity_to_restore == ACT_TIDY_UP ) {
it.erase_var( "activity_var" );
} else if( activity_to_restore == ACT_FETCH_REQUIRED ) {
it.set_var( "activity_var", you.name );
}
put_into_vehicle_or_drop( you, item_drop_reason::deliberate, { it }, dest );
// Remove from map or vehicle.
if( src_veh ) {
src_veh->remove_item( src_part, &it );
} else {
here.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 {
here.add_item_or_charges( src, leftovers );
}
}
}
std::vector<tripoint> route_adjacent( const Character &you, const tripoint &dest )
{
std::unordered_set<tripoint> passable_tiles = std::unordered_set<tripoint>();
map &here = get_map();
for( const tripoint &tp : here.points_in_radius( dest, 1 ) ) {
if( tp != you.pos() && here.passable( tp ) ) {
passable_tiles.emplace( tp );
}
}
const std::vector<tripoint> &sorted = get_sorted_tiles_by_distance( you.pos(), passable_tiles );
const std::set<tripoint> &avoid = you.get_path_avoid();
for( const tripoint &tp : sorted ) {
std::vector<tripoint> route = here.route( you.pos(), tp, you.get_pathfinding_settings(), avoid );
if( !route.empty() ) {
return route;
}
}
return std::vector<tripoint>();
}
std::vector<tripoint> route_best_workbench( const Character &you, const tripoint &dest )
{
std::unordered_set<tripoint> passable_tiles = std::unordered_set<tripoint>();
map &here = get_map();
creature_tracker &creatures = get_creature_tracker();
for( const tripoint &tp : here.points_in_radius( dest, 1 ) ) {
if( tp == you.pos() || ( here.passable( tp ) && !creatures.creature_at( tp ) ) ) {
passable_tiles.emplace( tp );
}
}
// Make sure current tile is at first
// so that the "best" tile doesn't change on reaching our destination
// if we are near the best workbench
std::vector<tripoint> sorted = get_sorted_tiles_by_distance( you.pos(), passable_tiles );
const auto cmp = [&]( tripoint a, tripoint b ) {
float best_bench_multi_a = 0.0f;
float best_bench_multi_b = 0.0f;
for( const tripoint &adj : here.points_in_radius( a, 1 ) ) {
if( here.dangerous_field_at( adj ) ) {
continue;
}
if( const cata::value_ptr<furn_workbench_info> &wb = here.furn( adj ).obj().workbench ) {
if( wb->multiplier > best_bench_multi_a ) {
best_bench_multi_a = wb->multiplier;
}
} else if( const cata::optional<vpart_reference> vp = here.veh_at(
adj ).part_with_feature( "WORKBENCH", true ) ) {
if( const cata::optional<vpslot_workbench> &wb_info = vp->part().info().get_workbench_info() ) {
if( wb_info->multiplier > best_bench_multi_a ) {
best_bench_multi_a = wb_info->multiplier;
}
} else {
debugmsg( "part '%s' with WORKBENCH flag has no workbench info", vp->part().name() );
}
}
}
for( const tripoint &adj : here.points_in_radius( b, 1 ) ) {
if( here.dangerous_field_at( adj ) ) {
continue;
}
if( const cata::value_ptr<furn_workbench_info> &wb = here.furn( adj ).obj().workbench ) {
if( wb->multiplier > best_bench_multi_b ) {
best_bench_multi_b = wb->multiplier;
}
} else if( const cata::optional<vpart_reference> vp = here.veh_at(
adj ).part_with_feature( "WORKBENCH", true ) ) {
if( const cata::optional<vpslot_workbench> &wb_info = vp->part().info().get_workbench_info() ) {
if( wb_info->multiplier > best_bench_multi_b ) {
best_bench_multi_b = wb_info->multiplier;
}
} else {
debugmsg( "part '%s' with WORKBENCH flag has no workbench info", vp->part().name() );
}
}
}
return best_bench_multi_a > best_bench_multi_b;
};
std::stable_sort( sorted.begin(), sorted.end(), cmp );
const std::set<tripoint> &avoid = you.get_path_avoid();
if( sorted.front() == you.pos() ) {
// We are on the best tile
return std::vector<tripoint>();
}
for( const tripoint &tp : sorted ) {
std::vector<tripoint> route = here.route( you.pos(), tp, you.get_pathfinding_settings(), avoid );
if( !route.empty() ) {
return route;
}
}
return std::vector<tripoint>();
}
namespace
{
bool _can_construct(
tripoint_bub_ms const &loc, construction_id const &idx, construction const &check,
cata::optional<construction_id> const &part_con_idx )
{
return ( part_con_idx && *part_con_idx == check.id ) ||
( check.pre_terrain != idx->post_terrain && can_construct( check, loc ) );
}
construction const *
_find_alt_construction( tripoint_bub_ms const &loc, construction_id const &idx,
cata::optional<construction_id> const &part_con_idx,
std::function<bool( construction const & )> const &filter )
{
std::vector<construction *> cons = constructions_by_filter( filter );
for( construction const *el : cons ) {
if( _can_construct( loc, idx, *el, part_con_idx ) ) {
return el;
}
}
return nullptr;
}
template <class ID>
ID _get_id( construction_id const &idx )
{
return idx->post_terrain.empty() ? ID() : ID( idx->post_terrain );
}
using checked_cache_t = std::vector<construction_id>;
construction const *_find_prereq( tripoint_bub_ms const &loc, construction_id const &idx,
construction_id const &top_idx,
cata::optional<construction_id> const &part_con_idx, checked_cache_t &checked_cache )
{
construction const *con = nullptr;
std::vector<construction *> cons = constructions_by_filter( [&idx, &top_idx](
construction const & it ) {
furn_id const f = top_idx->post_is_furniture ? _get_id<furn_id>( top_idx ) : furn_id();
ter_id const t = top_idx->post_is_furniture ? ter_id() : _get_id<ter_id>( top_idx );
return it.group != idx->group && !it.post_terrain.empty() &&
it.post_terrain == idx->pre_terrain &&
// don't get stuck building and deconstructing the top level post_terrain
it.pre_terrain != top_idx->post_terrain &&
( it.pre_flags.empty() || !can_construct_furn_ter( it, f, t ) );
} );
for( construction const *gcon : cons ) {
if( std::find( checked_cache.begin(), checked_cache.end(), gcon->id ) !=
checked_cache.end() ) {
continue;
}
checked_cache.emplace_back( gcon->id );
if( _can_construct( loc, idx, *gcon, part_con_idx ) ) {
return gcon;
}
// try to find a prerequisite of this prerequisite
if( !gcon->pre_terrain.empty() || !gcon->pre_flags.empty() ) {
con = _find_prereq( loc, gcon->id, top_idx, part_con_idx, checked_cache );
}
if( con != nullptr ) {
return con;
}
}
return nullptr;
}
} // namespace
static activity_reason_info find_base_construction(
Character &you,
const inventory &inv,
const tripoint_bub_ms &loc,
const cata::optional<construction_id> &part_con_idx,
const construction_id &idx )
{
bool cc = can_construct( idx.obj(), loc );
construction const *con = nullptr;
if( !cc ) {
// try to build a variant from the same group
con = _find_alt_construction( loc, idx, part_con_idx, [&idx]( construction const & it ) {
return it.group == idx->group;
} );
if( con == nullptr ) {
// try to build a pre-requisite from a different group, recursively
checked_cache_t checked_cache;
for( construction const *vcon : constructions_by_group( idx->group ) ) {
con = _find_prereq( loc, vcon->id, vcon->id, part_con_idx, checked_cache );
if( con != nullptr ) {
break;
}
}
}
cc = con != nullptr;
}
const construction &build = con == nullptr ? idx.obj() : *con;
bool pcb = player_can_build( you, inv, build, true );
//already done?
map &here = get_map();
const furn_id furn = here.furn( loc );
const ter_id ter = here.ter( loc );
if( !build.post_terrain.empty() &&
( ( !build.post_is_furniture && ter_id( build.post_terrain ) == ter ) ||
( build.post_is_furniture && furn_id( build.post_terrain ) == furn ) ) ) {
return activity_reason_info::build( do_activity_reason::ALREADY_DONE, false, build.id );
}
const bool has_skill = you.meets_skill_requirements( build );
if( !has_skill ) {
return activity_reason_info::build( do_activity_reason::DONT_HAVE_SKILL, false, build.id );
}
//if there's an appropriate partial construction on the tile, then we can work on it, no need to check inventories.
if( part_con_idx ) {
if( *part_con_idx != build.id ) {
//have a partial construction which is not leading to the required construction
return activity_reason_info::build( do_activity_reason::BLOCKING_TILE, false, idx );
}
return activity_reason_info::build( do_activity_reason::CAN_DO_CONSTRUCTION, true, build.id );
}
//can build?
if( cc ) {
if( pcb ) {
return activity_reason_info::build( do_activity_reason::CAN_DO_CONSTRUCTION, true, build.id );
}
//can't build with current inventory, do not look for pre-req
return activity_reason_info::build( do_activity_reason::NO_COMPONENTS, false, build.id );
}
return activity_reason_info::build( do_activity_reason::BLOCKING_TILE, false, idx );
}
static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
const requirement_id &needed_things, Character &you, const activity_id &activity_to_restore,
const bool in_loot_zones, const tripoint &src_loc )
{
zone_manager &mgr = zone_manager::get_manager();
temp_crafting_inventory temp_inv;
units::volume volume_allowed;
units::mass weight_allowed;
static const auto check_weight_if = []( const activity_id & id ) {
return id == ACT_MULTIPLE_FARM ||
id == ACT_MULTIPLE_CHOP_PLANKS ||
id == ACT_MULTIPLE_BUTCHER ||
id == ACT_VEHICLE_DECONSTRUCTION ||
id == ACT_VEHICLE_REPAIR ||
id == ACT_MULTIPLE_CHOP_TREES ||
id == ACT_MULTIPLE_FISH ||
id == ACT_MULTIPLE_MINE ||
id == ACT_MULTIPLE_MOP;
};
const bool check_weight = check_weight_if( activity_to_restore ) || ( !you.backlog.empty() &&
check_weight_if( you.backlog.front().id() ) );
if( check_weight ) {
volume_allowed = you.volume_capacity() - you.volume_carried();
weight_allowed = you.weight_capacity() - you.weight_carried();
}
bool found_welder = false;
for( item *elem : you.inv_dump() ) {
if( elem->has_quality( qual_WELD ) ) {
found_welder = true;
}
temp_inv.add_item_ref( *elem );
}
map &here = get_map();
for( const tripoint &elem : loot_spots ) {
// if we are searching for things to fetch, we can skip certain things.
// if, however they are already near the work spot, then the crafting / inventory functions will have their own method to use or discount them.
if( in_loot_zones ) {
// skip tiles in IGNORE zone and tiles on fire
// (to prevent taking out wood off the lit brazier)
// and inaccessible furniture, like filled charcoal kiln
if( mgr.has( zone_type_LOOT_IGNORE, here.getglobal( elem ), _fac_id( you ) ) ||
here.dangerous_field_at( elem ) ||
!here.can_put_items_ter_furn( elem ) ) {