-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
inventory.cpp
1157 lines (1037 loc) · 34 KB
/
inventory.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 "inventory.h"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <type_traits>
#include "avatar.h"
#include "calendar.h"
#include "character.h"
#include "colony.h"
#include "damage.h"
#include "debug.h"
#include "enums.h"
#include "flag.h"
#include "iexamine.h"
#include "inventory_ui.h" // auto inventory blocking
#include "item_pocket.h"
#include "item_stack.h"
#include "itype.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "messages.h" //for rust message
#include "npc.h"
#include "optional.h"
#include "options.h"
#include "point.h"
#include "proficiency.h"
#include "ret_val.h"
#include "rng.h"
#include "translations.h"
#include "type_id.h"
#include "units.h"
#include "vpart_position.h"
static const itype_id itype_aspirin( "aspirin" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_butchery_tree_pseudo( "butchery_tree_pseudo" );
static const itype_id itype_codeine( "codeine" );
static const itype_id itype_fire( "fire" );
static const itype_id itype_heroin( "heroin" );
static const itype_id itype_oxycodone( "oxycodone" );
static const itype_id itype_salt_water( "salt_water" );
static const itype_id itype_tramadol( "tramadol" );
static const material_id material_iron( "iron" );
struct itype;
const invlet_wrapper
inv_chars( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#&()+.:;=@[\\]^_{|}" );
bool invlet_wrapper::valid( const int invlet ) const
{
if( invlet > std::numeric_limits<char>::max() || invlet < std::numeric_limits<char>::min() ) {
return false;
}
return find( static_cast<char>( invlet ) ) != std::string::npos;
}
invlet_favorites::invlet_favorites( const std::unordered_map<itype_id, std::string> &map )
{
for( const auto &p : map ) {
if( p.second.empty() ) {
// The map gradually accumulates empty lists; remove those here
continue;
}
invlets_by_id.insert( p );
for( char invlet : p.second ) {
uint8_t invlet_u = invlet;
if( !ids_by_invlet[invlet_u].is_empty() ) {
debugmsg( "Duplicate invlet: %s and %s both mapped to %c",
ids_by_invlet[invlet_u].str(), p.first.str(), invlet );
}
ids_by_invlet[invlet_u] = p.first;
}
}
}
void invlet_favorites::set( char invlet, const itype_id &id )
{
if( contains( invlet, id ) ) {
return;
}
erase( invlet );
uint8_t invlet_u = invlet;
ids_by_invlet[invlet_u] = id;
invlets_by_id[id].push_back( invlet );
}
void invlet_favorites::erase( char invlet )
{
uint8_t invlet_u = invlet;
const itype_id &id = ids_by_invlet[invlet_u];
if( id.is_empty() ) {
return;
}
std::string &invlets = invlets_by_id[id];
std::string::iterator it = std::find( invlets.begin(), invlets.end(), invlet );
invlets.erase( it );
ids_by_invlet[invlet_u] = itype_id();
}
bool invlet_favorites::contains( char invlet, const itype_id &id ) const
{
uint8_t invlet_u = invlet;
return ids_by_invlet[invlet_u] == id;
}
std::string invlet_favorites::invlets_for( const itype_id &id ) const
{
auto map_iterator = invlets_by_id.find( id );
if( map_iterator == invlets_by_id.end() ) {
return {};
}
return map_iterator->second;
}
const std::unordered_map<itype_id, std::string> &
invlet_favorites::get_invlets_by_id() const
{
return invlets_by_id;
}
inventory::inventory() = default;
invslice inventory::slice()
{
invslice stacks;
for( auto &elem : items ) {
stacks.push_back( &elem );
}
return stacks;
}
const_invslice inventory::const_slice() const
{
const_invslice stacks;
for( const auto &item : items ) {
stacks.push_back( &item );
}
return stacks;
}
const std::list<item> &inventory::const_stack( int i ) const
{
if( i < 0 || i >= static_cast<int>( items.size() ) ) {
debugmsg( "Attempted to access stack %d in an inventory (size %d)", i, items.size() );
static const std::list<item> nullstack{};
return nullstack;
}
invstack::const_iterator iter = items.begin();
for( int j = 0; j < i; ++j ) {
++iter;
}
return *iter;
}
size_t inventory::size() const
{
return items.size();
}
inventory &inventory::operator+= ( const inventory &rhs )
{
for( size_t i = 0; i < rhs.size(); i++ ) {
push_back( rhs.const_stack( i ) );
}
return *this;
}
inventory &inventory::operator+= ( const std::list<item> &rhs )
{
for( const item &rh : rhs ) {
add_item( rh, false, false );
}
return *this;
}
inventory &inventory::operator+= ( const std::vector<item> &rhs )
{
for( const item &rh : rhs ) {
add_item( rh, true );
}
return *this;
}
inventory &inventory::operator+= ( const item &rhs )
{
add_item( rhs );
return *this;
}
inventory &inventory::operator+= ( const item_stack &rhs )
{
for( const item &p : rhs ) {
if( !p.made_of( phase_id::LIQUID ) ) {
add_item( p, true );
}
}
return *this;
}
inventory inventory::operator+ ( const inventory &rhs )
{
return inventory( *this ) += rhs;
}
inventory inventory::operator+ ( const std::list<item> &rhs )
{
return inventory( *this ) += rhs;
}
inventory inventory::operator+ ( const item &rhs )
{
return inventory( *this ) += rhs;
}
void inventory::unsort()
{
binned = false;
}
static bool stack_compare( const std::list<item> &lhs, const std::list<item> &rhs )
{
return lhs.front() < rhs.front();
}
void inventory::clear()
{
items.clear();
max_empty_liq_cont.clear();
binned = false;
qualities_cache.clear();
}
void inventory::push_back( const std::list<item> &newits )
{
for( const item &newit : newits ) {
add_item( newit, true );
}
}
// This function keeps the invlet cache updated when a new item is added.
void inventory::update_cache_with_item( item &newit )
{
// This function does two things:
// 1. It adds newit's invlet to the list of favorite letters for newit's item type.
// 2. It removes newit's invlet from the list of favorite letters for all other item types.
// no invlet item, just return.
// TODO: Should we instead remember that the invlet was cleared?
if( newit.invlet == 0 ) {
return;
}
invlet_cache.set( newit.invlet, newit.typeId() );
}
char inventory::find_usable_cached_invlet( const itype_id &item_type )
{
Character &player_character = get_player_character();
// Some of our preferred letters might already be used.
for( char invlet : invlet_cache.invlets_for( item_type ) ) {
// Don't overwrite user assignments.
if( assigned_invlet.count( invlet ) ) {
continue;
}
// Check if anything is using this invlet.
if( player_character.invlet_to_item( invlet ) != nullptr ) {
continue;
}
return invlet;
}
return 0;
}
item &inventory::add_item( item newit, bool keep_invlet, bool assign_invlet, bool should_stack )
{
binned = false;
Character &player_character = get_player_character();
if( should_stack ) {
// See if we can't stack this item.
for( auto &elem : items ) {
std::list<item>::iterator it_ref = elem.begin();
if( it_ref->stacks_with( newit ) ) {
if( it_ref->merge_charges( newit ) ) {
return *it_ref;
}
if( it_ref->invlet == '\0' ) {
if( !keep_invlet ) {
update_invlet( newit, assign_invlet );
}
update_cache_with_item( newit );
it_ref->invlet = newit.invlet;
} else {
newit.invlet = it_ref->invlet;
}
elem.emplace_back( std::move( newit ) );
return elem.back();
} else if( keep_invlet && assign_invlet && it_ref->invlet == newit.invlet ) {
// If keep_invlet is true, we'll be forcing other items out of their current invlet.
assign_empty_invlet( *it_ref, player_character );
}
}
}
// Couldn't stack the item, proceed.
if( !keep_invlet ) {
update_invlet( newit, assign_invlet );
}
update_cache_with_item( newit );
items.emplace_back( std::list<item> { std::move( newit ) } );
return items.back().back();
}
void inventory::add_item_keep_invlet( const item &newit )
{
add_item( newit, true );
}
void inventory::push_back( const item &newit )
{
add_item( newit );
}
#if defined(__ANDROID__)
extern void remove_stale_inventory_quick_shortcuts();
#endif
item *inventory::provide_pseudo_item( const itype_id &id, int battery )
{
if( id.is_empty() || !provisioned_pseudo_tools.insert( id ).second ) {
return nullptr; // empty itype_id or already provided tool -> bail out
}
item &it = add_item( item( id, calendar::turn, 0 ) );
it.set_flag( flag_PSEUDO );
// if tool doesn't need battery bail out early
if( battery <= 0 || it.magazine_default().is_null() ) {
return ⁢
}
item it_batt( it.magazine_default() );
item it_ammo = item( it_batt.ammo_default(), calendar::turn_zero );
if( it_ammo.is_null() || it_ammo.typeId() != itype_battery ) {
return ⁢
}
it_batt.ammo_set( it_batt.ammo_default(), battery );
it.put_in( it_batt, item_pocket::pocket_type::MAGAZINE_WELL );
return ⁢
}
book_proficiency_bonuses inventory::get_book_proficiency_bonuses() const
{
book_proficiency_bonuses ret;
for( const std::list<item> &it : this->items ) {
ret += it.front().get_book_proficiency_bonuses();
}
return ret;
}
void inventory::restack( Character &p )
{
// tasks that the old restack seemed to do:
// 1. reassign inventory letters
// 2. remove items from non-matching stacks
// 3. combine matching stacks
binned = false;
std::list<item> to_restack;
int idx = 0;
for( invstack::iterator iter = items.begin(); iter != items.end(); ++iter, ++idx ) {
std::list<item> &stack = *iter;
item &topmost = stack.front();
const item *invlet_item = p.invlet_to_item( topmost.invlet );
if( !inv_chars.valid( topmost.invlet ) || ( invlet_item != nullptr &&
position_by_item( invlet_item ) != idx ) ) {
assign_empty_invlet( topmost, p );
for( item &stack_iter : stack ) {
stack_iter.invlet = topmost.invlet;
}
}
// remove non-matching items, stripping off end of stack so the first item keeps the invlet.
while( stack.size() > 1 && !topmost.stacks_with( stack.back() ) ) {
to_restack.splice( to_restack.begin(), *iter, --stack.end() );
}
}
// combine matching stacks
// separate loop to ensure that ALL stacks are homogeneous
for( invstack::iterator iter = items.begin(); iter != items.end(); ++iter ) {
for( invstack::iterator other = iter; other != items.end(); ++other ) {
if( iter != other && iter->front().stacks_with( other->front() ) ) {
if( other->front().count_by_charges() ) {
iter->front().charges += other->front().charges;
} else {
iter->splice( iter->begin(), *other );
}
other = items.erase( other );
--other;
}
}
}
//re-add non-matching items
for( item &elem : to_restack ) {
add_item( elem );
}
//Ensure that all items in the same stack have the same invlet.
for( std::list< item > &outer : items ) {
for( item &inner : outer ) {
inner.invlet = outer.front().invlet;
}
}
items.sort( stack_compare );
#if defined(__ANDROID__)
remove_stale_inventory_quick_shortcuts();
#endif
}
static int count_charges_in_list( const itype *type, const map_stack &items )
{
for( const item &candidate : items ) {
if( candidate.type == type ) {
return candidate.charges;
}
}
return 0;
}
/**
* Finds the number of charges of the first item that matches ammotype.
*
* @param ammotype Search target.
* @param items Stack of items. Search stops at first match.
* @param [out] item_type Matching type.
*
* @return Number of charges.
* */
static int count_charges_in_list( const ammotype *ammotype, const map_stack &items,
itype_id &item_type )
{
for( const item &candidate : items ) {
if( candidate.is_ammo() && candidate.type->ammo->type == *ammotype ) {
item_type = candidate.typeId();
return candidate.charges;
}
}
return 0;
}
void inventory::form_from_map( const tripoint &origin, int range, const Character *pl,
bool assign_invlet,
bool clear_path )
{
form_from_map( get_map(), origin, range, pl, assign_invlet, clear_path );
}
void inventory::form_from_zone( map &m, std::unordered_set<tripoint_abs_ms> &zone_pts,
const Character *pl, bool assign_invlet )
{
std::vector<tripoint> pts;
pts.reserve( zone_pts.size() );
for( const tripoint_abs_ms &elem : zone_pts ) {
pts.push_back( m.getlocal( elem ) );
}
form_from_map( m, pts, pl, assign_invlet );
}
void inventory::form_from_map( map &m, const tripoint &origin, int range, const Character *pl,
bool assign_invlet,
bool clear_path )
{
// populate a grid of spots that can be reached
std::vector<tripoint> reachable_pts = {};
// If we need a clear path we care about the reachability of points
if( clear_path ) {
m.reachable_flood_steps( reachable_pts, origin, range, 1, 100 );
} else {
// Fill reachable points with points_in_radius
tripoint_range<tripoint> in_radius = m.points_in_radius( origin, range );
for( const tripoint &p : in_radius ) {
reachable_pts.emplace_back( p );
}
}
form_from_map( m, reachable_pts, pl, assign_invlet );
}
void inventory::form_from_map( map &m, std::vector<tripoint> pts, const Character *pl,
bool assign_invlet )
{
items.clear();
provisioned_pseudo_tools.clear();
for( const tripoint &p : pts ) {
// a temporary hack while trees are terrain
if( m.ter( p )->has_flag( ter_furn_flag::TFLAG_TREE ) ) {
provide_pseudo_item( itype_butchery_tree_pseudo, 0 );
}
const furn_t &f = m.furn( p ).obj();
if( item *furn_item = provide_pseudo_item( f.crafting_pseudo_item, 0 ) ) {
const itype *ammo = f.crafting_ammo_item_type();
if( furn_item->has_pocket_type( item_pocket::pocket_type::MAGAZINE ) ) {
// NOTE: This only works if the pseudo item has a MAGAZINE pocket, not a MAGAZINE_WELL!
const bool using_ammotype = f.has_flag( ter_furn_flag::TFLAG_AMMOTYPE_RELOAD );
int amount = 0;
itype_id ammo_id = ammo->get_id();
// Some furniture can consume more than one item type.
if( using_ammotype ) {
amount = count_charges_in_list( &ammo->ammo->type, m.i_at( p ), ammo_id );
} else {
amount = count_charges_in_list( ammo, m.i_at( p ) );
}
item furn_ammo( ammo_id, calendar::turn, amount );
furn_item->put_in( furn_ammo, item_pocket::pocket_type::MAGAZINE );
}
}
if( m.accessible_items( p ) ) {
for( item &i : m.i_at( p ) ) {
// if it's *the* player requesting this from from map inventory
// then don't allow items owned by another faction to be factored into recipe components etc.
if( pl && !i.is_owned_by( *pl, true ) ) {
continue;
}
if( !i.made_of( phase_id::LIQUID ) ) {
if( i.empty_container() && i.is_watertight_container() ) {
const int count = i.count_by_charges() ? i.charges : 1;
update_liq_container_count( i.typeId(), count );
}
add_item( i, false, assign_invlet );
}
}
}
// Kludges for now!
if( m.has_nearby_fire( p, 0 ) ) {
if( item *fire = provide_pseudo_item( itype_fire, 0 ) ) {
fire->charges = 1;
}
}
// Handle any water from map sources.
item water = m.water_from( p );
if( !water.is_null() ) {
add_item( water );
}
// keg-kludge
if( m.furn( p )->has_examine( iexamine::keg ) ) {
map_stack liq_contained = m.i_at( p );
for( item &i : liq_contained ) {
if( i.made_of( phase_id::LIQUID ) ) {
add_item( i );
}
}
}
// form from vehicle
if( optional_vpart_position vp = m.veh_at( p ) ) {
vp->form_inventory( *this );
}
}
pts.clear();
}
std::list<item> inventory::reduce_stack( const int position, const int quantity )
{
int pos = 0;
std::list<item> ret;
for( invstack::iterator iter = items.begin(); iter != items.end(); ++iter ) {
if( position == pos ) {
binned = false;
if( quantity >= static_cast<int>( iter->size() ) || quantity < 0 ) {
ret = *iter;
items.erase( iter );
} else {
for( int i = 0 ; i < quantity ; i++ ) {
ret.push_back( remove_item( &iter->front() ) );
}
}
break;
}
++pos;
}
return ret;
}
item inventory::remove_item( const item *it )
{
auto tmp = remove_items_with( [&it]( const item & i ) {
return &i == it;
}, 1 );
if( !tmp.empty() ) {
binned = false;
return tmp.front();
}
debugmsg( "Tried to remove a item not in inventory." );
return item();
}
item inventory::remove_item( const int position )
{
int pos = 0;
for( invstack::iterator iter = items.begin(); iter != items.end(); ++iter ) {
if( position == pos ) {
binned = false;
if( iter->size() > 1 ) {
std::list<item>::iterator stack_member = iter->begin();
char invlet = stack_member->invlet;
++stack_member;
stack_member->invlet = invlet;
}
item ret = iter->front();
iter->erase( iter->begin() );
if( iter->empty() ) {
items.erase( iter );
}
return ret;
}
++pos;
}
return item();
}
std::list<item> inventory::remove_randomly_by_volume( const units::volume &volume )
{
std::list<item> result;
units::volume volume_dropped = 0_ml;
while( volume_dropped < volume ) {
units::volume cumulative_volume = 0_ml;
auto chosen_stack = items.begin();
auto chosen_item = chosen_stack->begin();
for( auto stack = items.begin(); stack != items.end(); ++stack ) {
for( auto stack_it = stack->begin(); stack_it != stack->end(); ++stack_it ) {
cumulative_volume += stack_it->volume();
if( x_in_y( stack_it->volume().value(), cumulative_volume.value() ) ) {
chosen_item = stack_it;
chosen_stack = stack;
}
}
}
volume_dropped += chosen_item->volume();
result.push_back( std::move( *chosen_item ) );
chosen_item = chosen_stack->erase( chosen_item );
if( chosen_item == chosen_stack->begin() && !chosen_stack->empty() ) {
// preserve the invlet when removing the first item of a stack
chosen_item->invlet = result.back().invlet;
}
if( chosen_stack->empty() ) {
binned = false;
items.erase( chosen_stack );
}
}
return result;
}
void inventory::dump( std::vector<item *> &dest )
{
for( auto &elem : items ) {
for( item &elem_stack_iter : elem ) {
dest.push_back( &elem_stack_iter );
}
}
}
void inventory::dump( std::vector<const item *> &dest ) const
{
for( const auto &elem : items ) {
for( const item &elem_stack_iter : elem ) {
dest.push_back( &elem_stack_iter );
}
}
}
const item &inventory::find_item( int position ) const
{
if( position < 0 || position >= static_cast<int>( items.size() ) ) {
return null_item_reference();
}
invstack::const_iterator iter = items.begin();
for( int j = 0; j < position; ++j ) {
++iter;
}
return iter->front();
}
item &inventory::find_item( int position )
{
return const_cast<item &>( const_cast<const inventory *>( this )->find_item( position ) );
}
int inventory::invlet_to_position( char invlet ) const
{
int i = 0;
for( const auto &elem : items ) {
if( elem.begin()->invlet == invlet ) {
return i;
}
++i;
}
return INT_MIN;
}
int inventory::position_by_item( const item *it ) const
{
int p = 0;
for( const auto &stack : items ) {
for( const item &e : stack ) {
if( e.has_item( *it ) ) {
return p;
}
}
p++;
}
return INT_MIN;
}
int inventory::position_by_type( const itype_id &type ) const
{
int i = 0;
for( const auto &elem : items ) {
if( elem.front().typeId() == type ) {
return i;
}
++i;
}
return INT_MIN;
}
std::list<item> inventory::use_amount( const itype_id &it, int quantity,
const std::function<bool( const item & )> &filter )
{
items.sort( stack_compare );
std::list<item> ret;
for( invstack::iterator iter = items.begin(); iter != items.end() && quantity > 0; /* noop */ ) {
for( std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end() && quantity > 0;
/* noop */ ) {
if( stack_iter->use_amount( it, quantity, ret, filter ) ) {
stack_iter = iter->erase( stack_iter );
} else {
++stack_iter;
}
}
if( iter->empty() ) {
binned = false;
iter = items.erase( iter );
} else if( iter != items.end() ) {
++iter;
}
}
return ret;
}
int inventory::worst_item_value( npc *p ) const
{
int worst = 99999;
for( const auto &elem : items ) {
const item &it = elem.front();
int val = p->value( it );
if( val < worst ) {
worst = val;
}
}
return worst;
}
bool inventory::has_enough_painkiller( int pain ) const
{
for( const auto &elem : items ) {
const item &it = elem.front();
if( ( pain <= 35 && it.typeId() == itype_aspirin ) ||
( pain >= 50 && it.typeId() == itype_oxycodone ) ||
it.typeId() == itype_tramadol || it.typeId() == itype_codeine ) {
return true;
}
}
return false;
}
item *inventory::most_appropriate_painkiller( int pain )
{
int difference = 9999;
item *ret = &null_item_reference();
for( auto &elem : items ) {
int diff = 9999;
itype_id type = elem.front().typeId();
if( type == itype_aspirin ) {
diff = std::abs( pain - 15 );
} else if( type == itype_codeine ) {
diff = std::abs( pain - 30 );
} else if( type == itype_oxycodone ) {
diff = std::abs( pain - 60 );
} else if( type == itype_heroin ) {
diff = std::abs( pain - 100 );
} else if( type == itype_tramadol ) {
diff = std::abs( pain - 40 ) / 2; // Bonus since it's long-acting
}
if( diff < difference ) {
difference = diff;
ret = &elem.front();
}
}
return ret;
}
void inventory::rust_iron_items()
{
Character &player_character = get_player_character();
map &here = get_map();
for( auto &elem : items ) {
for( item &elem_stack_iter : elem ) {
if( elem_stack_iter.made_of( material_iron ) &&
!elem_stack_iter.has_flag( flag_WATERPROOF_GUN ) &&
!elem_stack_iter.has_flag( flag_WATERPROOF ) &&
elem_stack_iter.damage() < elem_stack_iter.max_damage() / 2 &&
//Passivation layer prevents further rusting
one_in( 500 ) &&
//Scale with volume, bigger = slower (see #24204)
one_in(
static_cast<int>(
14 * std::cbrt(
0.5 * std::max(
0.05, static_cast<double>(
elem_stack_iter.base_volume().value() ) / 250 ) ) ) ) &&
// ^season length ^14/5*0.75/pi (from volume of sphere)
//Freshwater without oxygen rusts slower than air
here.water_from( player_character.pos() ).typeId() == itype_salt_water ) {
elem_stack_iter.inc_damage( damage_type::ACID ); // rusting never completely destroys an item
add_msg( m_bad, _( "Your %s is damaged by rust." ), elem_stack_iter.tname() );
}
}
}
}
units::mass inventory::weight() const
{
units::mass ret = 0_gram;
for( const auto &elem : items ) {
for( const item &elem_stack_iter : elem ) {
ret += elem_stack_iter.weight();
}
}
return ret;
}
// Helper function to iterate over the intersection of the inventory and a list
// of items given
template<typename F>
void for_each_item_in_both(
const invstack &items, const std::map<const item *, int> &other, const F &f )
{
// Shortcut the logic in the common case where other is empty
if( other.empty() ) {
return;
}
for( const auto &elem : items ) {
const item &representative = elem.front();
auto other_it = other.find( &representative );
if( other_it == other.end() ) {
continue;
}
int num_to_count = other_it->second;
if( representative.count_by_charges() ) {
item copy = representative;
copy.charges = std::min( copy.charges, num_to_count );
f( copy );
} else {
for( const item &elem_stack_iter : elem ) {
f( elem_stack_iter );
if( --num_to_count <= 0 ) {
break;
}
}
}
}
}
units::mass inventory::weight_without( const std::map<const item *, int> &without ) const
{
units::mass ret = weight();
for_each_item_in_both( items, without,
[&]( const item & i ) {
ret -= i.weight();
}
);
if( ret < 0_gram ) {
debugmsg( "Negative mass after removing some of inventory" );
ret = {};
}
return ret;
}
units::volume inventory::volume() const
{
units::volume ret = 0_ml;
for( const auto &elem : items ) {
for( const item &elem_stack_iter : elem ) {
ret += elem_stack_iter.volume();
}
}
return ret;
}
units::volume inventory::volume_without( const std::map<const item *, int> &without ) const
{
units::volume ret = volume();
for_each_item_in_both( items, without,
[&]( const item & i ) {
ret -= i.volume();
}
);
if( ret < 0_ml ) {
debugmsg( "Negative volume after removing some of inventory" );
ret = 0_ml;
}
return ret;
}
std::vector<item *> inventory::active_items()
{
std::vector<item *> ret;
for( std::list<item> &elem : items ) {
for( item &elem_stack_iter : elem ) {
if( elem_stack_iter.needs_processing() ) {
ret.push_back( &elem_stack_iter );
}
}
}
return ret;
}
enchant_cache inventory::get_active_enchantment_cache( const Character &owner ) const
{
enchant_cache temp_cache;
for( const std::list<item> &elem : items ) {
for( const item &check_item : elem ) {
for( const enchant_cache &ench : check_item.get_proc_enchantments() ) {
if( ench.is_active( owner, check_item ) ) {
temp_cache.force_add( ench );
}
}
for( const enchantment &ench : check_item.get_defined_enchantments() ) {
if( ench.is_active( owner, check_item ) ) {
temp_cache.force_add( ench, owner );
}
}
}
}
return temp_cache;
}
int inventory::count_item( const itype_id &item_type ) const
{
int num = 0;
const itype_bin bin = get_binned_items();
if( bin.find( item_type ) == bin.end() ) {
return num;
}
const std::list<const item *> items = get_binned_items().find( item_type )->second;
for( const item *it : items ) {
num += it->count();
}
return num;
}
void inventory::assign_empty_invlet( item &it, const Character &p, const bool force )
{
const std::string auto_setting = get_option<std::string>( "AUTO_INV_ASSIGN" );
if( auto_setting == "disabled" || ( ( auto_setting == "favorites" ) && !it.is_favorite ) ) {
return;
}
invlets_bitset cur_inv = p.allocated_invlets();
itype_id target_type = it.typeId();
for( const auto &iter : assigned_invlet ) {
if( iter.second == target_type && !cur_inv[iter.first] ) {
it.invlet = iter.first;