forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_pocket.cpp
2262 lines (2030 loc) · 74.4 KB
/
item_pocket.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 "item_pocket.h"
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <string>
#include <utility>
#include "ammo.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "color.h"
#include "crafting.h"
#include "debug.h"
#include "enums.h"
#include "flag.h"
#include "generic_factory.h"
#include "handle_liquid.h"
#include "item.h"
#include "item_category.h"
#include "item_factory.h"
#include "item_location.h"
#include "itype.h"
#include "json.h"
#include "localized_comparator.h"
#include "map.h"
#include "math_defines.h"
#include "messages.h"
#include "output.h"
#include "string_formatter.h"
#include "translations.h"
#include "units.h"
#include "units_utility.h"
namespace io
{
// *INDENT-OFF*
template<>
std::string enum_to_string<item_pocket::pocket_type>( item_pocket::pocket_type data )
{
switch ( data ) {
case item_pocket::pocket_type::CONTAINER: return "CONTAINER";
case item_pocket::pocket_type::MAGAZINE: return "MAGAZINE";
case item_pocket::pocket_type::MAGAZINE_WELL: return "MAGAZINE_WELL";
case item_pocket::pocket_type::MOD: return "MOD";
case item_pocket::pocket_type::CORPSE: return "CORPSE";
case item_pocket::pocket_type::SOFTWARE: return "SOFTWARE";
case item_pocket::pocket_type::EBOOK: return "EBOOK";
case item_pocket::pocket_type::MIGRATION: return "MIGRATION";
case item_pocket::pocket_type::LAST: break;
}
cata_fatal( "Invalid valid_target" );
}
// *INDENT-ON*
} // namespace io
constexpr units::volume pocket_data::max_volume_for_container;
constexpr units::mass pocket_data::max_weight_for_container;
std::string pocket_data::check_definition() const
{
if( type == item_pocket::pocket_type::MOD ||
type == item_pocket::pocket_type::CORPSE ||
type == item_pocket::pocket_type::MIGRATION ) {
return "";
}
for( const itype_id &it : item_id_restriction ) {
if( !it.is_valid() ) {
return string_format( "item_id %s used in restriction invalid\n", it.str() );
}
if( it->phase == phase_id::LIQUID && !watertight ) {
return string_format( "restricted to liquid item %s but not watertight\n",
it->get_id().str() );
}
if( it->phase == phase_id::GAS && !airtight ) {
return string_format( "restricted to gas item %s but not airtight\n",
it->get_id().str() );
}
}
for( const std::pair<const ammotype, int> &ammo_res : ammo_restriction ) {
const ammotype &at = ammo_res.first;
if( !at.is_valid() ) {
return string_format( "invalid ammotype %s", at.str() );
}
const itype_id &it = at->default_ammotype();
if( it->phase == phase_id::LIQUID && !watertight ) {
return string_format( "restricted to liquid item %s but not watertight\n",
it->get_id().str() );
}
if( it->phase == phase_id::GAS && !airtight ) {
return string_format( "restricted to gas item %s but not airtight\n",
it->get_id().str() );
}
}
if( max_contains_volume() == 0_ml ) {
return "has zero max volume\n";
}
if( magazine_well > 0_ml && rigid ) {
return "rigid overrides magazine_well\n";
}
if( magazine_well >= max_contains_volume() ) {
return string_format(
"magazine well (%s) larger than pocket volume (%s); "
"consider using rigid instead.\n",
quantity_to_string( magazine_well ),
quantity_to_string( max_contains_volume() ) );
}
if( max_item_volume && *max_item_volume < min_item_volume ) {
return "max_item_volume is greater than min_item_volume. no item can fit.\n";
}
if( ( watertight || airtight ) && min_item_volume > 0_ml ) {
return "watertight or gastight is incompatible with min_item_volume\n";
}
return "";
}
void pocket_data::load( const JsonObject &jo )
{
optional( jo, was_loaded, "pocket_type", type, item_pocket::pocket_type::CONTAINER );
optional( jo, was_loaded, "ammo_restriction", ammo_restriction );
optional( jo, was_loaded, "flag_restriction", flag_restrictions );
optional( jo, was_loaded, "item_restriction", item_id_restriction );
optional( jo, was_loaded, "allowed_speedloaders", allowed_speedloaders );
optional( jo, was_loaded, "default_magazine", default_magazine );
optional( jo, was_loaded, "description", description );
optional( jo, was_loaded, "name", pocket_name );
if( jo.has_member( "ammo_restriction" ) && ammo_restriction.empty() ) {
jo.throw_error( "pocket defines empty ammo_restriction" );
}
if( jo.has_member( "flag_restrictions" ) && flag_restrictions.empty() ) {
jo.throw_error( "pocket defines empty flag_restrictions" );
}
if( jo.has_member( "item_restriction" ) && item_id_restriction.empty() ) {
jo.throw_error( "pocket defines empty item_restriction" );
}
if( !item_id_restriction.empty() ) {
std::vector<itype_id> item_restriction;
mandatory( jo, was_loaded, "item_restriction", item_restriction );
default_magazine = item_restriction.front();
}
// ammo_restriction is a type of override, making the mandatory members not mandatory and superfluous
// putting it in an if statement like this should allow for report_unvisited_member to work here
if( ammo_restriction.empty() ) {
optional( jo, was_loaded, "min_item_volume", min_item_volume, volume_reader(), 0_ml );
units::volume temp = -1_ml;
optional( jo, was_loaded, "max_item_volume", temp, volume_reader(), temp );
if( temp != -1_ml ) {
max_item_volume = temp;
}
optional( jo, was_loaded, "max_contains_volume", volume_capacity, volume_reader(),
max_volume_for_container );
optional( jo, was_loaded, "max_contains_weight", max_contains_weight, mass_reader(),
max_weight_for_container );
optional( jo, was_loaded, "max_item_length", max_item_length,
units::default_length_from_volume( volume_capacity ) * M_SQRT2 );
optional( jo, was_loaded, "min_item_length", min_item_length );
optional( jo, was_loaded, "extra_encumbrance", extra_encumbrance, 0 );
optional( jo, was_loaded, "volume_encumber_modifier", volume_encumber_modifier, 1 );
optional( jo, was_loaded, "ripoff", ripoff, 0 );
optional( jo, was_loaded, "activity_noise", activity_noise );
}
optional( jo, was_loaded, "spoil_multiplier", spoil_multiplier, 1.0f );
optional( jo, was_loaded, "weight_multiplier", weight_multiplier, 1.0f );
optional( jo, was_loaded, "volume_multiplier", volume_multiplier, 1.0f );
optional( jo, was_loaded, "magazine_well", magazine_well, volume_reader(), 0_ml );
optional( jo, was_loaded, "moves", moves, 100 );
optional( jo, was_loaded, "fire_protection", fire_protection, false );
optional( jo, was_loaded, "watertight", watertight, false );
optional( jo, was_loaded, "airtight", airtight, false );
optional( jo, was_loaded, "open_container", open_container, false );
optional( jo, was_loaded, "transparent", transparent, false );
optional( jo, was_loaded, "rigid", rigid, false );
optional( jo, was_loaded, "holster", holster );
optional( jo, was_loaded, "ablative", ablative );
optional( jo, was_loaded, "inherits_flags", inherits_flags );
// if ablative also flag as a holster so it only holds 1 item
if( ablative ) {
holster = true;
}
optional( jo, was_loaded, "sealed_data", sealed_data );
}
void sealable_data::load( const JsonObject &jo )
{
optional( jo, was_loaded, "spoil_multiplier", spoil_multiplier, 1.0f );
}
void pocket_noise::load( const JsonObject &jo )
{
optional( jo, was_loaded, "volume", volume, 0 );
optional( jo, was_loaded, "chance", chance, 0 );
}
bool item_pocket::operator==( const item_pocket &rhs ) const
{
return *data == *rhs.data;
}
bool pocket_data::operator==( const pocket_data &rhs ) const
{
return rigid == rhs.rigid &&
watertight == rhs.watertight &&
airtight == rhs.airtight &&
fire_protection == rhs.fire_protection &&
get_flag_restrictions() == rhs.get_flag_restrictions() &&
type == rhs.type &&
volume_capacity == rhs.volume_capacity &&
min_item_volume == rhs.min_item_volume &&
max_contains_weight == rhs.max_contains_weight &&
spoil_multiplier == rhs.spoil_multiplier &&
weight_multiplier == rhs.weight_multiplier &&
moves == rhs.moves;
}
const pocket_data::FlagsSetType &pocket_data::get_flag_restrictions() const
{
return flag_restrictions;
}
void pocket_data::add_flag_restriction( const flag_id &flag )
{
flag_restrictions.insert( flag );
}
bool item_pocket::same_contents( const item_pocket &rhs ) const
{
if( contents.size() != rhs.contents.size() ) {
return false;
}
return std::equal( contents.begin(), contents.end(),
rhs.contents.begin(), rhs.contents.end(),
[]( const item & a, const item & b ) {
return a.typeId() == b.typeId() &&
a.charges == b.charges &&
a.same_contents( b );
} );
}
void item_pocket::restack()
{
if( contents.size() <= 1 ) {
return;
}
if( is_type( item_pocket::pocket_type::MAGAZINE ) ) {
// Restack magazine contents in a way that preserves order of items
for( auto iter = contents.begin(); iter != contents.end(); ) {
if( !iter->count_by_charges() ) {
continue;
}
auto next = std::next( iter, 1 );
if( next == contents.end() ) {
break;
}
if( iter->combine( *next ) ) {
iter = contents.erase( next );
} else {
++iter;
}
}
} else {
for( auto outer_iter = contents.begin(); outer_iter != contents.end(); ++outer_iter ) {
if( !outer_iter->count_by_charges() ) {
continue;
}
for( auto inner_iter = contents.begin(); inner_iter != contents.end(); ) {
if( outer_iter == inner_iter || !inner_iter->count_by_charges() ) {
++inner_iter;
continue;
}
if( outer_iter->combine( *inner_iter ) ) {
inner_iter = contents.erase( inner_iter );
outer_iter = contents.begin();
} else {
++inner_iter;
}
}
}
}
}
item *item_pocket::restack( /*const*/ item *it )
{
item *ret = it;
if( contents.size() <= 1 ) {
return ret;
}
for( auto outer_iter = contents.begin(); outer_iter != contents.end(); ++outer_iter ) {
if( !outer_iter->count_by_charges() ) {
continue;
}
for( auto inner_iter = contents.begin(); inner_iter != contents.end(); ) {
if( outer_iter == inner_iter || !inner_iter->count_by_charges() ) {
++inner_iter;
continue;
}
if( outer_iter->combine( *inner_iter ) ) {
// inner was placed in outer, check if inner was the item that we track
if( &( *inner_iter ) == ret ) {
ret = &( *outer_iter );
}
inner_iter = contents.erase( inner_iter );
outer_iter = contents.begin();
} else {
++inner_iter;
}
}
}
return ret;
}
bool item_pocket::has_item_stacks_with( const item &it ) const
{
for( const item &inside : contents ) {
if( it.stacks_with( inside ) ) {
return true;
}
}
return false;
}
bool item_pocket::better_pocket( const item_pocket &rhs, const item &it, bool nested ) const
{
if( this->settings.priority() != rhs.settings.priority() ) {
// Priority overrides all other factors.
return rhs.settings.priority() > this->settings.priority();
}
// if we've somehow bypassed the pocket autoinsert rules, check them here
if( !rhs.settings.accepts_item( it ) ) {
return false;
} else if( !this->settings.accepts_item( it ) ) {
return true;
}
const bool rhs_whitelisted = !rhs.settings.get_item_whitelist().empty() ||
!rhs.settings.get_category_whitelist().empty();
const bool lhs_whitelisted = !this->settings.get_item_whitelist().empty() ||
!this->settings.get_category_whitelist().empty();
if( rhs_whitelisted != lhs_whitelisted ) {
return rhs_whitelisted;
}
const bool rhs_it_stack = rhs.has_item_stacks_with( it );
if( has_item_stacks_with( it ) != rhs_it_stack ) {
return rhs_it_stack;
}
if( data->ammo_restriction.empty() != rhs.data->ammo_restriction.empty() ) {
// pockets restricted by ammo should try to get filled first
return !rhs.data->ammo_restriction.empty();
}
if( data->get_flag_restrictions().empty() != rhs.data->get_flag_restrictions().empty() ) {
// pockets restricted by flag should try to get filled first
return !rhs.data->get_flag_restrictions().empty();
}
if( it.is_comestible() && it.get_comestible()->spoils != 0_seconds ) {
// a lower spoil multiplier is better
return rhs.spoil_multiplier() < spoil_multiplier();
}
if( it.made_of( phase_id::SOLID ) ) {
if( data->watertight != rhs.data->watertight ) {
return !rhs.data->watertight;
}
}
//Skip irrelevant properties of nested containers
if( nested ) {
return false;
}
if( rhs.data->extra_encumbrance < data->extra_encumbrance ) {
// pockets with less extra encumbrance should be prioritized
return true;
}
if( data->ripoff > rhs.data->ripoff ) {
// pockets without ripoff chance should be prioritized
return true;
}
if( data->rigid != rhs.data->rigid ) {
// Prefer rigid containers, as their fixed volume is wasted unless filled
return rhs.data->rigid;
}
if( remaining_volume() != rhs.remaining_volume() ) {
// we want the least amount of remaining volume
return rhs.remaining_volume() < remaining_volume();
}
return rhs.obtain_cost( it ) < obtain_cost( it );
}
bool item_pocket::stacks_with( const item_pocket &rhs ) const
{
if( _sealed != rhs._sealed ) {
return false;
}
return ( empty() && rhs.empty() ) || std::equal( contents.begin(), contents.end(),
rhs.contents.begin(), rhs.contents.end(),
[]( const item & a, const item & b ) {
return a.charges == b.charges && a.stacks_with( b );
} );
}
bool item_pocket::is_funnel_container( units::volume &bigger_than ) const
{
// should not be static, since after reloading some members of the item objects,
// such as item types, may be invalidated.
const std::vector<item> allowed_liquids{
item( "water", calendar::turn_zero, 1 ),
};
if( !data->watertight ) {
return false;
}
if( sealable() && _sealed ) {
return false;
}
for( const item &liquid : allowed_liquids ) {
if( can_contain( liquid ).success() ) {
bigger_than = remaining_volume();
return true;
}
}
return false;
}
bool item_pocket::is_restricted() const
{
return !data->get_flag_restrictions().empty() || !data->ammo_restriction.empty();
}
std::list<item *> item_pocket::all_items_top()
{
std::list<item *> items;
for( item &it : contents ) {
items.push_back( &it );
}
return items;
}
std::list<const item *> item_pocket::all_items_top() const
{
std::list<const item *> items;
for( const item &it : contents ) {
items.push_back( &it );
}
return items;
}
std::list<item *> item_pocket::all_items_ptr( item_pocket::pocket_type pk_type )
{
if( !is_type( pk_type ) ) {
return std::list<item *>();
}
std::list<item *> all_items_top_level{ all_items_top() };
for( item *it : all_items_top_level ) {
std::list<item *> all_items_internal{ it->all_items_ptr( pk_type ) };
all_items_top_level.insert( all_items_top_level.end(), all_items_internal.begin(),
all_items_internal.end() );
}
return all_items_top_level;
}
std::list<const item *> item_pocket::all_items_ptr( item_pocket::pocket_type pk_type ) const
{
if( !is_type( pk_type ) ) {
return std::list<const item *>();
}
std::list<const item *> all_items_top_level{ all_items_top() };
for( const item *it : all_items_top_level ) {
std::list<const item *> all_items_internal{ it->all_items_ptr( pk_type ) };
all_items_top_level.insert( all_items_top_level.end(), all_items_internal.begin(),
all_items_internal.end() );
}
return all_items_top_level;
}
bool item_pocket::has_any_with( const std::function<bool( const item &it )> &filter ) const
{
return std::any_of( contents.begin(), contents.end(), filter );
}
item &item_pocket::back()
{
return contents.back();
}
const item &item_pocket::back() const
{
return contents.back();
}
item &item_pocket::front()
{
return contents.front();
}
const item &item_pocket::front() const
{
return contents.front();
}
void item_pocket::pop_back()
{
contents.pop_back();
}
size_t item_pocket::size() const
{
return contents.size();
}
units::volume item_pocket::volume_capacity() const
{
return data->volume_capacity;
}
units::volume item_pocket::magazine_well() const
{
return data->magazine_well;
}
units::mass item_pocket::weight_capacity() const
{
return data->max_contains_weight;
}
units::volume item_pocket::max_contains_volume() const
{
return data->max_contains_volume();
}
units::volume item_pocket::remaining_volume() const
{
return volume_capacity() - contains_volume();
}
int item_pocket::remaining_capacity_for_item( const item &it ) const
{
if( it.count_by_charges() ) {
item it_copy = it;
it_copy.charges = 1;
if( can_contain( it_copy ).success() ) {
return std::min( {
it.charges,
charges_per_remaining_volume( it ),
charges_per_remaining_weight( it )
} );
} else {
return 0;
}
} else if( can_contain( it ).success() ) {
return 1;
} else {
return 0;
}
}
units::volume item_pocket::item_size_modifier() const
{
if( data->rigid ) {
return 0_ml;
}
units::volume total_vol = 0_ml;
for( const item &it : contents ) {
total_vol += it.volume( is_type( item_pocket::pocket_type::MOD ) );
}
total_vol -= data->magazine_well;
total_vol *= data->volume_multiplier;
return std::max( 0_ml, total_vol );
}
units::mass item_pocket::item_weight_modifier() const
{
units::mass total_mass = 0_gram;
for( const item &it : contents ) {
if( is_type( item_pocket::pocket_type::MOD ) ) {
total_mass += it.weight( true, true ) * data->weight_multiplier;
} else {
total_mass += it.weight() * data->weight_multiplier;
}
}
return total_mass;
}
units::length item_pocket::item_length_modifier() const
{
if( is_type( item_pocket::pocket_type::EBOOK ) ) {
return 0_mm;
}
units::length total_length = 0_mm;
for( const item &it : contents ) {
total_length = std::max( static_cast<units::length>( it.length() * std::cbrt(
data->volume_multiplier ) ),
total_length );
}
return total_length;
}
float item_pocket::spoil_multiplier() const
{
if( sealed() ) {
return data->sealed_data->spoil_multiplier;
} else {
return data->spoil_multiplier;
}
}
int item_pocket::moves() const
{
if( data ) {
return data->moves;
} else {
return -1;
}
}
std::vector<item *> item_pocket::gunmods()
{
std::vector<item *> mods;
for( item &it : contents ) {
if( it.is_gunmod() ) {
mods.push_back( &it );
}
}
return mods;
}
std::vector<const item *> item_pocket::gunmods() const
{
std::vector<const item *> mods;
for( const item &it : contents ) {
if( it.is_gunmod() ) {
mods.push_back( &it );
}
}
return mods;
}
cata::flat_set<itype_id> item_pocket::item_type_restrictions() const
{
return data->item_id_restriction;
}
item *item_pocket::magazine_current()
{
auto iter = std::find_if( contents.begin(), contents.end(), []( const item & it ) {
return !it.is_null();
} );
return iter != contents.end() ? &*iter : nullptr;
}
itype_id item_pocket::magazine_default() const
{
if( is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) {
return data->default_magazine;
}
return itype_id::NULL_ID();
}
int item_pocket::ammo_consume( int qty )
{
int need = qty;
int used = 0;
std::list<item>::iterator it;
for( it = contents.begin(); it != contents.end(); ) {
if( it->has_flag( flag_CASING ) ) {
it++;
continue;
}
if( need >= it->charges ) {
need -= it->charges;
used += it->charges;
it = contents.erase( it );
} else {
it->charges -= need;
used += need;
break;
}
}
return used;
}
int item_pocket::ammo_capacity( const ammotype &ammo ) const
{
const auto found_ammo = data->ammo_restriction.find( ammo );
if( found_ammo == data->ammo_restriction.end() ) {
return 0;
} else {
return found_ammo->second;
}
}
int item_pocket::remaining_ammo_capacity( const ammotype &ammo ) const
{
int total_capacity = ammo_capacity( ammo );
if( total_capacity == 0 ) {
return 0;
}
int ammo_count = 0;
for( const item &it : contents ) {
if( it.has_flag( flag_CASING ) ) {
continue;
} else if( ammo != it.ammo_type() ) {
return 0;
}
ammo_count += it.count();
}
return total_capacity - ammo_count;
}
std::set<ammotype> item_pocket::ammo_types() const
{
std::set<ammotype> ret;
for( const std::pair<const ammotype, int> &type_pair : data->ammo_restriction ) {
ret.emplace( type_pair.first );
}
return ret;
}
void item_pocket::casings_handle( const std::function<bool( item & )> &func )
{
for( auto it = contents.begin(); it != contents.end(); ) {
if( it->has_flag( flag_CASING ) ) {
it->unset_flag( flag_CASING );
if( func( *it ) ) {
it = contents.erase( it );
continue;
}
// didn't handle the casing so reset the flag ready for next call
it->set_flag( flag_CASING );
}
++it;
}
}
void item_pocket::handle_liquid_or_spill( Character &guy, const item *avoid )
{
if( guy.is_npc() ) {
spill_contents( guy.pos() );
return;
}
for( auto iter = contents.begin(); iter != contents.end(); ) {
if( iter->made_of( phase_id::LIQUID ) ) {
while( iter->charges > 0 && liquid_handler::handle_liquid( *iter, avoid, 1 ) ) {
// query until completely handled or explicitly canceled
}
if( iter->charges == 0 ) {
iter = contents.erase( iter );
} else {
return;
}
} else {
item i_copy( *iter );
guy.i_add_or_drop( i_copy, 1, avoid, &*iter );
iter = contents.erase( iter );
}
}
}
bool item_pocket::use_amount( const itype_id &it, int &quantity, std::list<item> &used )
{
bool used_item = false;
for( auto a = contents.begin(); a != contents.end() && quantity > 0; ) {
if( a->use_amount( it, quantity, used ) ) {
used_item = true;
a = contents.erase( a );
} else {
++a;
}
}
restack();
return used_item;
}
bool item_pocket::will_explode_in_a_fire() const
{
if( data->fire_protection ) {
return false;
}
return std::any_of( contents.begin(), contents.end(), []( const item & it ) {
return it.will_explode_in_fire();
} );
}
bool item_pocket::will_spill() const
{
if( sealed() ) {
return false;
} else {
return data->open_container;
}
}
bool item_pocket::will_spill_if_unsealed() const
{
return data->open_container;
}
bool item_pocket::seal()
{
if( !sealable() || empty() ) {
return false;
}
_sealed = true;
return true;
}
void item_pocket::unseal()
{
_sealed = false;
}
bool item_pocket::sealed() const
{
if( !sealable() ) {
return false;
} else {
return _sealed;
}
}
bool item_pocket::sealable() const
{
// if it has sealed data it is understood that the
// data is different when sealed than when not
// In other words, a pocket is sealable IFF it has sealed_data.
return !!data->sealed_data;
}
std::string item_pocket::translated_sealed_prefix() const
{
if( sealed() ) {
return _( "sealed" );
} else {
return _( "open" );
}
}
bool item_pocket::detonate( const tripoint &pos, std::vector<item> &drops )
{
const auto new_end = std::remove_if( contents.begin(), contents.end(), [&pos, &drops]( item & it ) {
return it.detonate( pos, drops );
} );
if( new_end != contents.end() ) {
contents.erase( new_end, contents.end() );
// If any of the contents explodes, so does the container
return true;
}
return false;
}
bool item_pocket::process( const itype &type, map &here, Character *carrier, const tripoint &pos,
float insulation, const temperature_flag flag )
{
bool processed = false;
float spoil_multiplier = 1.0f;
for( auto it = contents.begin(); it != contents.end(); ) {
if( _sealed ) {
spoil_multiplier = 0.0f;
}
if( it->process( here, carrier, pos, type.insulation_factor * insulation, flag,
spoil_multiplier ) ) {
it->spill_contents( pos );
it = contents.erase( it );
processed = true;
} else {
++it;
}
}
return processed;
}
void item_pocket::remove_all_ammo( Character &guy )
{
for( auto iter = contents.begin(); iter != contents.end(); ) {
if( iter->is_irremovable() ) {
iter++;
continue;
}
drop_or_handle( *iter, guy );
iter = contents.erase( iter );
}
}
void item_pocket::remove_all_mods( Character &guy )
{
for( auto iter = contents.begin(); iter != contents.end(); ) {
if( iter->is_toolmod() ) {
guy.i_add_or_drop( *iter );
iter = contents.erase( iter );
} else {
++iter;
}
}
}
void item_pocket::set_item_defaults()
{
for( item &contained_item : contents ) {
/* for guns and other items defined to have a magazine but don't use "ammo" */
if( contained_item.is_magazine() ) {
contained_item.ammo_set(
contained_item.ammo_default(),
contained_item.ammo_capacity( item_controller->find_template(
contained_item.ammo_default() )->ammo->type ) / 2
);
} else { //Contents are batteries or food
contained_item.charges =
item::find_type( contained_item.typeId() )->charges_default();
}
}
}
static void insert_separation_line( std::vector<iteminfo> &info )
{
if( info.empty() || info.back().sName != "--" ) {
info.emplace_back( "DESCRIPTION", "--" );
}
}
void item_pocket::general_info( std::vector<iteminfo> &info, int pocket_number,
bool disp_pocket_number ) const
{
if( disp_pocket_number ) {
const std::string pocket_num = string_format( _( "Pocket %d:" ), pocket_number );
info.emplace_back( "DESCRIPTION", pocket_num );
}
if( !get_description().empty() ) {
info.emplace_back( "DESCRIPTION", string_format( "<info>%s</info>",
get_description().translated() ) );
} else if( name_as_description ) {
// fallback to the original items name as a description
info.emplace_back( "DESCRIPTION", string_format( "<info>%s</info>",
get_name().translated() ) );
}
// NOLINTNEXTLINE(cata-translate-string-literal)
const std::string cont_type_str = string_format( "{%d}CONTAINER", pocket_number );
// NOLINTNEXTLINE(cata-translate-string-literal)
const std::string base_type_str = string_format( "{%d}BASE", pocket_number );
// Show volume/weight for normal containers, or ammo capacity if ammo_restriction is defined if its an ablative pocket don't display any info
if( is_ablative() ) {
// its an ablative pocket volume and weight maxes don't matter
info.emplace_back( "DESCRIPTION",
_( "Holds a <info>single armored plate</info>." ) );
} else if( data->ammo_restriction.empty() ) {
info.push_back( vol_to_info( cont_type_str, _( "Volume: " ),
volume_capacity(), 2, false ) );
info.push_back( weight_to_info( cont_type_str, _( " Weight: " ),
weight_capacity(), 2, false ) );
info.back().bNewLine = true;
} else {
std::vector<std::pair<std::string, int>> fmts;
for( const ammotype &at : ammo_types() ) {
int capacity = ammo_capacity( at );
fmts.emplace_back( string_format( n_gettext( "<num> round of %s",
"<num> rounds of %s", capacity ),
at->name() ),
capacity );
}
std::sort( fmts.begin(), fmts.end(), localized_compare );
for( const std::pair<std::string, int> &fmt : fmts ) {
// NOLINTNEXTLINE(cata-translate-string-literal)
info.emplace_back( string_format( "{%d}MAGAZINE", pocket_number ), _( "Holds: " ), fmt.first,
iteminfo::no_flags,
fmt.second );
}
}
if( data->max_item_length != 0_mm && !is_ablative() ) {
info.back().bNewLine = true;
// if the item has no minimum length then keep it in the same units as max
if( data->min_item_length == 0_mm ) {
info.emplace_back( base_type_str, _( "Item length: " ),
string_format( "<num> %s", length_units( data->max_item_length ) ),
iteminfo::no_newline,
0 );
} else {
info.emplace_back( base_type_str, _( "Item length: " ),
string_format( "<num> %s", length_units( data->min_item_length ) ),
iteminfo::no_newline,
convert_length( data->min_item_length ), data->min_item_length.value() );
}
info.emplace_back( base_type_str, _( " to " ),
string_format( "<num> %s", length_units( data->max_item_length ) ),
iteminfo::no_flags,
convert_length( data->max_item_length ), data->max_item_length.value() );
} else if( data->min_item_length > 0_mm && !is_ablative() ) {
info.back().bNewLine = true;