forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.cpp
1535 lines (1404 loc) · 42.2 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 <sstream>
#include "inventory.h"
#include "game.h"
#include "keypress.h"
#include "mapdata.h"
#include "item_factory.h"
const std::string inv_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#&()*+./:;=?@[\\]^_{|}";
// TODO: make the two slice methods complain (debugmsg?) if length == size()
// after all, if we're asking for the *entire* inventory,
// that's probably a sign of a partial legacy conversion
invslice inventory::slice(int start, int length)
{
invslice stacks;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (start <= 0)
{
stacks.push_back(&*iter);
--length;
}
if (length <= 0)
{
break;
}
--start;
}
return stacks;
}
invslice inventory::slice(const std::list<item>* start, int length)
{
invslice stacks;
bool found_start = false;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (!found_start && start == &(*iter))
{
found_start = true;
}
if (found_start)
{
stacks.push_back(&*iter);
--length;
}
if (length <= 0)
{
break;
}
}
return stacks;
}
inventory inventory::subset(std::map<char, int> chosen) const
{
inventory ret;
for (invstack::const_iterator iter = items.begin(); iter != items.end(); ++iter)
{
// don't need to worry about auto-creation of entries, as long as default == 0
int count = chosen[iter->front().invlet];
if (count != 0)
{
if (iter->front().count_by_charges())
{
item tmp = iter->front();
if (count <= tmp.charges && count >= 0) // -1 is used to mean "all" sometimes
{
tmp.charges = count;
}
ret.add_item(tmp);
}
else
{
for (std::list<item>::const_iterator stack_iter = iter->begin(); stack_iter != iter->end(); ++stack_iter)
{
ret.add_item(*stack_iter);
--count;
if (count <= 0)
{
break;
}
}
}
}
}
return ret;
}
std::list<item>& inventory::stack_by_letter(char ch)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->begin()->invlet == ch)
{
return *iter;
}
}
return nullstack;
}
std::list<item> inventory::const_stack(int i) const
{
if (i < 0 || i > items.size())
{
debugmsg("Attempted to access stack %d in an inventory (size %d)",
i, items.size());
return nullstack;
}
invstack::const_iterator iter = items.begin();
for (int j = 0; j < i; ++j)
{
++iter;
}
return *iter;
}
int inventory::size() const
{
return items.size();
}
int inventory::num_items() const
{
int ret = 0;
for (invstack::const_iterator iter = items.begin(); iter != items.end(); ++iter)
{
ret += iter->size();
}
return ret;
}
bool inventory::is_sorted() const
{
return sorted;
}
inventory& inventory::operator= (inventory &rhs)
{
if (this == &rhs)
return *this; // No self-assignment
clear();
for (int i = 0; i < rhs.size(); i++)
items.push_back(rhs.const_stack(i));
return *this;
}
inventory& inventory::operator= (const inventory &rhs)
{
if (this == &rhs)
return *this; // No self-assignment
clear();
for (int i = 0; i < rhs.size(); i++)
items.push_back(rhs.const_stack(i));
return *this;
}
inventory& inventory::operator+= (const inventory &rhs)
{
for (int i = 0; i < rhs.size(); i++)
add_stack(rhs.const_stack(i));
return *this;
}
inventory& inventory::operator+= (const std::list<item> &rhs)
{
for (std::list<item>::const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
add_item(*iter, true);
}
return *this;
}
inventory& inventory::operator+= (const item &rhs)
{
add_item(rhs);
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;
}
inventory inventory::filter_by_category(item_cat cat, const player& u) const
{
inventory reduced_inv;
for (invstack::const_iterator iter = items.begin(); iter != items.end(); ++iter)
{
const item& it = iter->front();
switch (cat)
{
case IC_COMESTIBLE: // food
if (it.is_food(&u) || it.is_food_container(&u))
{
reduced_inv += *iter;
}
break;
case IC_AMMO: // ammo
if (it.is_ammo() || it.is_ammo_container())
{
reduced_inv += *iter;
}
break;
case IC_ARMOR: // armour
if (it.is_armor())
{
reduced_inv += *iter;
}
break;
case IC_BOOK: // books
if (it.is_book())
{
reduced_inv += *iter;
}
break;
case IC_TOOL: // tools
if (it.is_tool())
{
reduced_inv += *iter;
}
break;
case IC_CONTAINER: // containers for liquid handling
if (it.is_tool() || it.is_gun())
{
if (it.ammo_type() == "gasoline")
{
reduced_inv += *iter;
}
}
else
{
if (it.is_container())
{
reduced_inv += *iter;
}
}
break;
}
}
return reduced_inv;
}
void inventory::unsort()
{
sorted = false;
}
bool stack_compare(const std::list<item>& lhs, const std::list<item>& rhs)
{
return lhs.front() < rhs.front();
}
void inventory::sort()
{
items.sort(stack_compare);
sorted = true;
}
void inventory::clear()
{
items.clear();
}
void inventory::add_stack(const std::list<item> newits)
{
for (std::list<item>::const_iterator iter = newits.begin(); iter != newits.end(); ++iter)
{
add_item(*iter, true);
}
}
void inventory::push_back(std::list<item> newits)
{
add_stack(newits);
}
// 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.
// Iterator over all the keys of the map.
std::map<std::string, std::vector<char> >::iterator i;
for(i=invlet_cache.begin(); i!=invlet_cache.end(); i++) {
std::string type = i->first;
std::vector<char>& preferred_invlets = i->second;
// Erase the used invlet from all caches.
for(int ind=0; ind < preferred_invlets.size(); ind++) {
if(preferred_invlets[ind] == newit.invlet) {
preferred_invlets.erase(preferred_invlets.begin()+ind);
ind--;
}
}
}
// Append the selected invlet to the list of preferred invlets of this item type.
std::vector<char>& preferred_invlets = invlet_cache[newit.typeId()];
preferred_invlets.push_back(newit.invlet);
}
char inventory::get_invlet_for_item( std::string item_type ) {
char candidate_invlet = 0;
if( invlet_cache.count( item_type ) ) {
std::vector<char>& preferred_invlets = invlet_cache[ item_type ];
// Some of our preferred letters might already be used.
int first_free_invlet = -1;
for(int invlets_index = 0; invlets_index < preferred_invlets.size(); invlets_index++) {
bool invlet_is_used = false; // Check if anything is using this invlet.
if( g->u.weapon.invlet == preferred_invlets[ invlets_index ] ) {
continue;
}
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter) {
if( iter->front().invlet == preferred_invlets[ invlets_index ] ) {
invlet_is_used = true;
break;
}
}
// If we found one that isn't used, we're done iterating.
if( !invlet_is_used ) {
first_free_invlet = invlets_index;
break;
}
}
if( first_free_invlet != -1 ) {
candidate_invlet = preferred_invlets[first_free_invlet];
}
}
return candidate_invlet;
}
item& inventory::add_item(item newit, bool keep_invlet)
{
if (newit.is_style())
{
return nullitem; // Styles never belong in our inventory.
}
bool reuse_cached_letter = false;
// Check how many stacks of this type already are in our inventory.
if(!keep_invlet) {
// Do we have this item in our inventory favourites cache?
char temp_invlet = get_invlet_for_item( newit.typeId() );
if( temp_invlet != 0 ) {
newit.invlet = temp_invlet;
reuse_cached_letter = true;
}
// If it's not in our cache and not a lowercase letter, try to give it a low letter.
if(!reuse_cached_letter && (newit.invlet < 'a' || newit.invlet > 'z')) {
assign_empty_invlet(newit);
}
// Make sure the assigned invlet doesn't exist already.
if(g->u.has_item(newit.invlet)) {
assign_empty_invlet(newit);
}
}
// See if we can't stack this item.
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
std::list<item>::iterator it_ref = iter->begin();
if (it_ref->type->id == newit.type->id)
{
if (newit.charges != -1 && (newit.is_food() || newit.is_ammo()))
{
it_ref->charges += newit.charges;
return *it_ref;
}
else if (it_ref->stacks_with(newit))
{
if (it_ref->is_food() && it_ref->has_flag("HOT"))
{
int tmpcounter = (it_ref->item_counter + newit.item_counter) / 2;
it_ref->item_counter = tmpcounter;
newit.item_counter = tmpcounter;
}
newit.invlet = it_ref->invlet;
iter->push_back(newit);
return iter->back();
}
}
// If keep_invlet is true, we'll be forcing other items out of their current invlet.
else if (keep_invlet && it_ref->invlet == newit.invlet)
{
assign_empty_invlet(*it_ref);
update_cache_with_item(newit);
}
}
// Couldn't stack the item, proceed.
if(!reuse_cached_letter) {
update_cache_with_item(newit);
}
std::list<item> newstack;
newstack.push_back(newit);
items.push_back(newstack);
return items.back().back();
}
void inventory::add_item_by_type(itype_id type, int count, int charges)
{
// TODO add proper birthday
while (count > 0)
{
item tmp = item_controller->create(type, 0);
if (charges != -1)
{
tmp.charges = charges;
}
add_item(tmp);
count--;
}
}
void inventory::add_item_keep_invlet(item newit)
{
add_item(newit, true);
}
void inventory::push_back(item newit)
{
add_item(newit);
}
void inventory::restack(player *p)
{
// tasks that the old restack seemed to do:
// 1. reassign inventory letters
// 2. remove items from non-matching stacks
// 3. combine matching stacks
if (!p)
{
return;
}
std::list<item> to_restack;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (!iter->front().invlet_is_okay() || p->has_weapon_or_armor(iter->front().invlet))
{
assign_empty_invlet(iter->front());
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end();
++stack_iter)
{
stack_iter->invlet = iter->front().invlet;
}
}
// remove non-matching items
while (iter->size() > 0 && !iter->front().stacks_with(iter->back()))
{
to_restack.splice(to_restack.begin(), *iter, iter->begin());
}
if (iter->size() <= 0)
{
iter = items.erase(iter);
--iter;
continue;
}
}
// 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().type->id == other->front().type->id)
{
if (other->front().charges != -1 && (other->front().is_food() || other->front().is_ammo()))
{
iter->front().charges += other->front().charges;
}
else if (iter->front().stacks_with(other->front()))
{
iter->splice(iter->begin(), *other);
}
else
{
continue;
}
other = items.erase(other);
--other;
}
}
}
//re-add non-matching items
for (std::list<item>::iterator iter = to_restack.begin(); iter != to_restack.end(); ++iter)
{
add_item(*iter);
}
}
void inventory::form_from_map(game *g, point origin, int range)
{
items.clear();
for (int x = origin.x - range; x <= origin.x + range; x++) {
for (int y = origin.y - range; y <= origin.y + range; y++) {
if (g->m.has_flag(sealed, x, y))
continue;
for (int i = 0; i < g->m.i_at(x, y).size(); i++)
if (!g->m.i_at(x, y)[i].made_of(LIQUID))
add_item(g->m.i_at(x, y)[i]);
// Kludges for now!
ter_id terrain_id = g->m.ter(x, y);
if ((g->m.field_at(x, y).findField(fd_fire)) || (terrain_id == t_lava)) {
item fire(g->itypes["fire"], 0);
fire.charges = 1;
add_item(fire);
}
if (terrain_id == t_water_sh || terrain_id == t_water_dp){
item water(g->itypes["water"], 0);
water.charges = 50;
add_item(water);
}
int vpart = -1;
vehicle *veh = g->m.veh_at(x, y, vpart);
if (veh) {
const int kpart = veh->part_with_feature(vpart, "KITCHEN");
const int weldpart = veh->part_with_feature(vpart, "WELDRIG");
if (kpart >= 0) {
item hotplate(g->itypes["hotplate"], 0);
hotplate.charges = veh->fuel_left("battery", true);
add_item(hotplate);
item water(g->itypes["water_clean"], 0);
water.charges = veh->fuel_left("water");
add_item(water);
item pot(g->itypes["pot"], 0);
add_item(pot);
item pan(g->itypes["pan"], 0);
add_item(pan);
}
if (weldpart >= 0) {
item welder(g->itypes["welder"], 0);
welder.charges = veh->fuel_left("battery", true);
add_item(welder);
item soldering_iron(g->itypes["soldering_iron"], 0);
soldering_iron.charges = veh->fuel_left("battery", true);
add_item(soldering_iron);
}
}
}
}
}
std::list<item> inventory::remove_stack_by_letter(char ch)
{
return remove_partial_stack(ch, -1);
}
std::list<item> inventory::remove_partial_stack(char ch, int amount)
{
std::list<item> ret;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->front().invlet == ch)
{
if(amount >= iter->size() || amount < 0)
{
ret = *iter;
items.erase(iter);
}
else
{
for(int i = 0 ; i < amount ; i++)
{
ret.push_back(remove_item(&iter->front()));
}
}
break;
}
}
return ret;
}
item inventory::remove_item(item* it)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin(); stack_iter != iter->end(); ++stack_iter)
{
if (it == &*stack_iter)
{
item tmp = *stack_iter;
iter->erase(stack_iter);
if (iter->size() <= 0)
{
items.erase(iter);
}
return tmp;
}
}
}
debugmsg("Tried to remove a item not in inventory (name: %s)", it->type->name.c_str());
return nullitem;
}
item inventory::remove_item(invstack::iterator iter)
{
item ret = iter->front();
iter->erase(iter->begin());
if (iter->empty())
{
items.erase(iter);
}
return ret;
}
item inventory::remove_item_by_type(itype_id type)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->front().type->id == type)
{
return remove_item(iter);
}
}
return nullitem;
}
item inventory::remove_item_by_letter(char ch)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->begin()->invlet == ch)
{
if (iter->size() > 1)
{
std::list<item>::iterator stack_member = iter->begin();
++stack_member;
stack_member->invlet = ch;
}
return remove_item(iter);
}
}
return nullitem;
}
// using this assumes the item has charges
item inventory::remove_item_by_charges(char ch, int quantity)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->begin()->invlet == ch)
{
if (!iter->front().count_by_charges())
{
debugmsg("Tried to remove %s by charges, but item is not counted by charges", iter->front().type->name.c_str());
}
item ret = iter->front();
if (quantity > iter->front().charges)
{
debugmsg("Charges: Tried to remove charges that does not exist, \
removing maximum available charges instead");
quantity = iter->front().charges;
}
ret.charges = quantity;
iter->front().charges -= quantity;
if (iter->front().charges <= 0)
{
items.erase(iter);
}
return ret;
}
}
debugmsg("Tried to remove item with invlet %c by quantity, no such item", ch);
return nullitem;
}
std::vector<item> inventory::remove_mission_items(int mission_id)
{
std::vector<item> ret;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end(); ++stack_iter)
{
if (stack_iter->mission_id == mission_id)
{
ret.push_back(remove_item(&*stack_iter));
stack_iter = iter->begin();
}
else
{
for (int k = 0; k < stack_iter->contents.size() && stack_iter != iter->end(); ++k)
{
if (stack_iter->contents[k].mission_id == mission_id)
{
ret.push_back(remove_item(&*stack_iter));
stack_iter = iter->begin();
}
}
}
}
if (ret.size())
{
return ret;
}
}
return ret;
}
void inventory::dump(std::vector<item *>& dest)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end(); ++stack_iter)
{
dest.push_back(&(*stack_iter));
}
}
}
item& inventory::item_by_letter(char ch)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->begin()->invlet == ch)
{
return *iter->begin();
}
}
return nullitem;
}
item& inventory::item_by_type(itype_id type)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
if (iter->front().type->id == type)
{
return iter->front();
}
}
return nullitem;
}
item& inventory::item_or_container(itype_id type)
{
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end(); ++stack_iter)
{
if (stack_iter->type->id == type)
{
return *stack_iter;
}
else if (stack_iter->is_container() && stack_iter->contents.size() > 0)
{
if (stack_iter->contents[0].type->id == type)
{
return *stack_iter;
}
}
}
}
return nullitem;
}
std::vector<item*> inventory::all_items_by_type(itype_id type)
{
std::vector<item*> ret;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end();
++stack_iter)
{
if (stack_iter->type->id == type)
{
ret.push_back(&*stack_iter);
}
}
}
return ret;
}
std::vector<item*> inventory::all_items_with_flag( const std::string flag ) {
std::vector<item*> ret;
for (invstack::iterator istack = items.begin(); istack != items.end(); ++istack) {
for (std::list<item>::iterator iitem = istack->begin(); iitem != istack->end(); ++iitem) {
if (iitem->has_flag(flag)) {
ret.push_back(&*iitem);
}
}
}
return ret;
}
std::vector<item*> inventory::all_ammo(ammotype type)
{
std::vector<item*> ret;
for (invstack::iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end();
++stack_iter)
{
if (stack_iter->is_ammo() && dynamic_cast<it_ammo*>(stack_iter->type)->type == type)
{
ret.push_back(&*stack_iter);
}
// Handle gasoline nested in containers
else if (type == "gasoline" && stack_iter->is_container() &&
!stack_iter->contents.empty() && stack_iter->contents[0].is_ammo() &&
dynamic_cast<it_ammo*>(stack_iter->contents[0].type)->type == type)
{
ret.push_back(&*stack_iter);
return ret;
}
}
}
return ret;
}
int inventory::amount_of(itype_id it) const
{
int count = 0;
for (invstack::const_iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::const_iterator stack_iter = iter->begin();
stack_iter != iter->end();
++stack_iter)
{
if (stack_iter->type->id == it)
{
// check if it's a container, if so, it should be empty
if (stack_iter->type->is_container())
{
if (stack_iter->contents.empty())
{
count++;
}
}
else
{
count++;
}
}
for (int k = 0; k < stack_iter->contents.size(); k++)
{
if (stack_iter->contents[k].type->id == it)
{
count++;
}
}
}
}
return count;
}
int inventory::charges_of(itype_id it) const
{
int count = 0;
for (invstack::const_iterator iter = items.begin(); iter != items.end(); ++iter)
{
for (std::list<item>::const_iterator stack_iter = iter->begin();
stack_iter != iter->end();
++stack_iter)
{
if (stack_iter->type->id == it)
{
if (stack_iter->charges < 0)
{
count++;
}
else
{
count += stack_iter->charges;
}
}
for (int k = 0; k < stack_iter->contents.size(); k++)
{
if (stack_iter->contents[k].type->id == it)
{
if (stack_iter->contents[k].charges < 0)
{
count++;
}
else
{
count += stack_iter->contents[k].charges;
}
}
}
}
}
return count;
}
std::list<item> inventory::use_amount(itype_id it, int quantity, bool use_container)
{
sort();
std::list<item> ret;
for (invstack::iterator iter = items.begin(); iter != items.end() && quantity > 0; ++iter)
{
for (std::list<item>::iterator stack_iter = iter->begin();
stack_iter != iter->end() && quantity > 0;
++stack_iter)
{
// First, check contents
bool used_item_contents = false;
for (int k = 0; k < stack_iter->contents.size() && quantity > 0; k++)
{
if (stack_iter->contents[k].type->id == it)
{
ret.push_back(stack_iter->contents[k]);
quantity--;
stack_iter->contents.erase(stack_iter->contents.begin() + k);
k--;
used_item_contents = true;
}
}
// Now check the item itself
if (use_container && used_item_contents)
{
stack_iter = iter->erase(stack_iter);
if (iter->empty())
{
iter = items.erase(iter);
--iter;
break;
}
else
{
--stack_iter;
}
}
else if (stack_iter->type->id == it && quantity > 0)
{
ret.push_back(*stack_iter);
quantity--;
stack_iter = iter->erase(stack_iter);
if (iter->empty())
{
iter = items.erase(iter);
--iter;
break;
}
else
{
--stack_iter;
}
}