forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_contents.cpp
2320 lines (2085 loc) · 77.1 KB
/
item_contents.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_contents.h"
#include <algorithm>
#include <iterator>
#include <map>
#include <string>
#include <type_traits>
#include "avatar.h"
#include "character.h"
#include "color.h"
#include "cursesdef.h"
#include "debug.h"
#include "enum_conversions.h"
#include "enums.h"
#include "flat_set.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "item_category.h"
#include "item_location.h"
#include "item_pocket.h"
#include "iteminfo_query.h"
#include "itype.h"
#include "localized_comparator.h"
#include "make_static.h"
#include "map.h"
#include "output.h"
#include "point.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "ui.h"
#include "units.h"
static const flag_id json_flag_CASING( "CASING" );
void pocket_favorite_callback::refresh( uilist *menu )
{
if( needs_to_refresh ) {
refresh_columns( menu );
needs_to_refresh = false;
}
item_pocket *selected_pocket = nullptr;
int i = 0;
int pocket_num = 0;
for( std::tuple<item_pocket *, int, uilist_entry *> &pocket_val : saved_pockets ) {
item_pocket *pocket = std::get<0>( pocket_val );
if( pocket == nullptr || ( pocket->get_pocket_data() &&
!pocket->is_type( item_pocket::pocket_type::CONTAINER ) ) ) {
++i;
continue;
}
if( i == menu->selected ) {
selected_pocket = pocket;
pocket_num = std::get<1>( pocket_val ) + 1;
break;
}
++i;
}
if( selected_pocket != nullptr && selected_pocket->is_allowed() ) {
std::vector<iteminfo> info;
int starty = 5;
const int startx = menu->w_width - menu->pad_right;
const int width = menu->pad_right - 1;
fold_and_print( menu->window, point( 2, 2 ), width,
c_light_gray, string_format( _( "Currently modifying %s" ),
colorize( whitelist ? _( "whitelist" ) : _( "blacklist" ), c_light_blue ) ) );
selected_pocket->general_info( info, pocket_num, true );
starty += fold_and_print( menu->window, point( startx, starty ), width,
c_light_gray, format_item_info( info, {} ) ) + 1;
info.clear();
selected_pocket->favorite_info( info );
starty += fold_and_print( menu->window, point( startx, starty ), width,
c_light_gray, format_item_info( info, {} ) ) + 1;
info.clear();
selected_pocket->contents_info( info, pocket_num, true );
fold_and_print( menu->window, point( startx, starty ), width,
c_light_gray, format_item_info( info, {} ) );
}
wnoutrefresh( menu->window );
}
pocket_favorite_callback::pocket_favorite_callback( std::vector<item *> to_organize,
uilist &pocket_selector )
{
this->to_organize = to_organize;
for( item *i : to_organize ) {
add_pockets( *i, pocket_selector, "" );
}
}
void pocket_favorite_callback::add_pockets( item &i, uilist &pocket_selector,
std::string depth )
{
if( i.get_all_contained_pockets().empty() ) {
// if it doesn't have pockets skip it
return;
}
pocket_selector.addentry( -1, false, '\0', string_format( "%s%s", depth, i.display_name() ) );
// pad list with empty entries for the items themselves
saved_pockets.emplace_back( nullptr, 0, nullptr );
if( ( i.is_collapsed() && to_organize.size() != 1 ) || i.all_pockets_sealed() ) {
//is collapsed, or all pockets are sealed skip its nested pockets
// only skip collapsed items if doing multi menu, for a single item this wouldn't make sense
return;
}
uilist_entry *item_entry = &pocket_selector.entries.back();
int pocket_num = 1;
for( item_pocket *it_pocket : i.get_all_contained_pockets() ) {
std::string temp = string_format( "%d -", pocket_num );
pocket_selector.addentry( 0, true, '\0', string_format( "%s%s %s/%s",
depth,
temp,
vol_to_info( "", "", it_pocket->contains_volume() ).sValue,
vol_to_info( "", "", it_pocket->max_contains_volume() ).sValue ) );
// pocket number is displayed from 1 stored from 0
saved_pockets.emplace_back( it_pocket, pocket_num - 1, item_entry );
pocket_num++;
// display the items
for( item *it : it_pocket->all_items_top() ) {
// check for pockets in that pocket
add_pockets( *it, pocket_selector, depth + " " );
}
}
}
void pocket_favorite_callback::refresh_columns( uilist *menu )
{
// rebuild the list of rows can't fully clear or the menu will close
// clear all but one entry repopulate then delete that initial
menu->entries.clear();
saved_pockets.clear();
for( item *i : to_organize ) {
add_pockets( *i, *menu, "" );
}
// there might be a better way to refresh this
menu->setup();
}
void pocket_favorite_callback::move_item( uilist *menu, item_pocket *selected_pocket )
{
uilist selector_menu;
if( item_to_move.second == nullptr ) {
selector_menu.title = _( "Select an item from the pocket" );
std::vector<item *> item_list;
for( item *it_in : selected_pocket->all_items_top() ) {
item_list.emplace_back( it_in );
}
std::sort( item_list.begin(), item_list.end() );
for( const item *it : item_list ) {
selector_menu.addentry( it->display_name() );
}
if( selector_menu.entries.empty() ) {
popup( std::string( _( "No items in the pocket." ) ), PF_GET_KEY );
} else {
selector_menu.query();
}
if( selector_menu.ret >= 0 ) {
item_to_move = { item_list[selector_menu.ret], selected_pocket };
}
if( item_to_move.first != nullptr ) {
menu->settext( string_format( "%s: %s", _( "Moving" ), item_to_move.first->display_name() ) );
refresh_columns( menu );
// if we have an item already selected for moving update some info
auto itt = saved_pockets.begin();
for( uilist_entry &entry : menu->entries ) {
if( entry.enabled && !std::get<0>( *itt )->can_contain( *item_to_move.first ).success() ) {
entry.enabled = false;
}
// make sure we dont try to put anything in itself
if( entry.enabled ) {
for( item_pocket *pocket : item_to_move.first->get_all_standard_pockets() ) {
if( std::get<0>( *itt ) == pocket ) {
entry.enabled = false;
}
}
}
// move through the pockets as you process entries
++itt;
}
}
} else {
// storage should mimick character inserting
get_avatar().as_character()->store( selected_pocket, *item_to_move.first );
// reset the moved item
item_to_move = { nullptr, nullptr };
menu->settext( title );
refresh_columns( menu );
}
}
bool pocket_favorite_callback::key( const input_context &ctxt, const input_event &event, int,
uilist *menu )
{
item_pocket *selected_pocket = nullptr;
int i = 0;
int pocket_num = 0;
for( std::tuple<item_pocket *, int, uilist_entry *> &pocket_val : saved_pockets ) {
item_pocket *pocket = std::get<0>( pocket_val );
if( pocket == nullptr || ( pocket->get_pocket_data() &&
!pocket->is_type( item_pocket::pocket_type::CONTAINER ) ) ) {
++i;
continue;
}
if( i == menu->selected ) {
selected_pocket = pocket;
pocket_num = std::get<1>( pocket_val ) + 1;
break;
}
++i;
}
if( selected_pocket == nullptr ) {
return false;
}
const std::string &action = ctxt.input_to_action( event );
//popup( string_format( "%s, %s, %s.", event.long_description(),
// event.short_description(), action ) );
if( action == "FAV_WHITELIST" ) {
whitelist = true;
return true;
} else if( action == "FAV_BLACKLIST" ) {
whitelist = false;
return true;
} else if( action == "FAV_PRIORITY" ) {
string_input_popup popup;
popup.title( string_format( _( "Enter Priority (current priority %d)" ),
selected_pocket->settings.priority() ) );
selected_pocket->settings.set_priority( popup.query_int() );
return true;
} else if( action == "FAV_AUTO_PICKUP" ) {
selected_pocket->settings.set_disabled( !selected_pocket->settings.is_disabled() );
return true;
} else if( action == "FAV_AUTO_UNLOAD" ) {
selected_pocket->settings.set_unloadable( !selected_pocket->settings.is_unloadable() );
return true;
}
uilist selector_menu;
const std::string remove_prefix = "<color_light_red>-</color> ";
const std::string add_prefix = "<color_green>+</color> ";
if( action == "FAV_MOVE_ITEM" ) {
move_item( menu, selected_pocket );
return true;
}
if( action == "FAV_ITEM" ) {
const cata::flat_set<itype_id> &listed_itypes = whitelist
? selected_pocket->settings.get_item_whitelist()
: selected_pocket->settings.get_item_blacklist();
cata::flat_set<itype_id> nearby_itypes;
selector_menu.title = _( "Select an item from nearby" );
for( const std::list<item> *it_list : get_player_character().crafting_inventory().const_slice() ) {
nearby_itypes.insert( it_list->front().typeId() );
}
std::vector<std::pair<itype_id, std::string>> listed_names;
std::vector<std::pair<itype_id, std::string>> nearby_names;
listed_names.reserve( listed_itypes.size() );
nearby_names.reserve( nearby_itypes.size() );
for( const itype_id &id : listed_itypes ) {
listed_names.emplace_back( id, id->nname( 1 ) );
}
for( const itype_id &id : nearby_itypes ) {
if( !listed_itypes.count( id ) && selected_pocket->is_compatible( item( id ) ).success() ) {
nearby_names.emplace_back( id, id->nname( 1 ) );
}
}
const auto &compare_names = []( const std::pair<itype_id, std::string> &lhs,
const std::pair<itype_id, std::string> &rhs ) {
return localized_compare( lhs.second, rhs.second );
};
std::sort( listed_names.begin(), listed_names.end(), compare_names );
std::sort( nearby_names.begin(), nearby_names.end(), compare_names );
for( const std::pair<itype_id, std::string> &it : listed_names ) {
selector_menu.addentry( remove_prefix + it.second );
}
for( const std::pair<itype_id, std::string> &it : nearby_names ) {
selector_menu.addentry( add_prefix + it.second );
}
if( selector_menu.entries.empty() ) {
popup( std::string( _( "No nearby items would fit here." ) ), PF_GET_KEY );
} else {
selector_menu.query();
}
const int selected = selector_menu.ret;
itype_id selected_id = itype_id::NULL_ID();
if( selected >= 0 ) {
size_t idx = selected;
const std::vector<std::pair<itype_id, std::string>> *names = nullptr;
if( idx < listed_names.size() ) {
names = &listed_names;
} else {
idx -= listed_names.size();
}
if( !names && idx < nearby_names.size() ) {
names = &nearby_names;
}
if( names ) {
selected_id = ( *names )[idx].first;
}
}
if( !selected_id.is_null() ) {
if( whitelist ) {
selected_pocket->settings.whitelist_item( selected_id );
} else {
selected_pocket->settings.blacklist_item( selected_id );
}
}
return true;
} else if( action == "FAV_CATEGORY" ) {
// Get all categories and sort by name
std::vector<item_category> all_cat = item_category::get_all();
const cata::flat_set<item_category_id> &listed_cat = whitelist
? selected_pocket->settings.get_category_whitelist()
: selected_pocket->settings.get_category_blacklist();
std::sort( all_cat.begin(), all_cat.end(), [&]( const item_category & lhs,
const item_category & rhs ) {
const bool lhs_in_list = listed_cat.count( lhs.get_id() );
const bool rhs_in_list = listed_cat.count( rhs.get_id() );
if( lhs_in_list && !rhs_in_list ) {
return true;
} else if( !lhs_in_list && rhs_in_list ) {
return false;
}
return localized_compare( lhs.name(), rhs.name() );
} );
for( const item_category &cat : all_cat ) {
const bool in_list = listed_cat.count( cat.get_id() );
const std::string &prefix = in_list ? remove_prefix : add_prefix;
selector_menu.addentry( prefix + cat.name() );
}
selector_menu.query();
if( selector_menu.ret >= 0 ) {
const item_category_id id( all_cat.at( selector_menu.ret ).get_id() );
if( whitelist ) {
selected_pocket->settings.whitelist_category( id );
} else {
selected_pocket->settings.blacklist_category( id );
}
}
return true;
} else if( action == "FAV_CLEAR" ) {
if( query_yn( _( "Are you sure you want to clear settings for pocket %d?" ), pocket_num ) ) {
selected_pocket->settings.clear();
}
} else if( action == "FAV_CONTEXT_MENU" ) {
uilist cmenu( _( "Action to take on this pocket" ), {} );
cmenu.addentry( 0, true, inp_mngr.get_first_char_for_action( "FAV_MOVE_ITEM", "INVENTORY" ),
ctxt.get_action_name( "FAV_MOVE_ITEM" ) );
cmenu.addentry( 1, true, inp_mngr.get_first_char_for_action( "FAV_ITEM", "INVENTORY" ),
ctxt.get_action_name( "FAV_ITEM" ) );
cmenu.addentry( 2, true, inp_mngr.get_first_char_for_action( "FAV_CATEGORY", "INVENTORY" ),
ctxt.get_action_name( "FAV_CATEGORY" ) );
cmenu.addentry( 3, true, inp_mngr.get_first_char_for_action( "FAV_WHITELIST", "INVENTORY" ),
ctxt.get_action_name( "FAV_WHITELIST" ) );
cmenu.addentry( 4, true, inp_mngr.get_first_char_for_action( "FAV_BLACKLIST", "INVENTORY" ),
ctxt.get_action_name( "FAV_BLACKLIST" ) );
cmenu.addentry( 5, true, inp_mngr.get_first_char_for_action( "FAV_PRIORITY", "INVENTORY" ),
ctxt.get_action_name( "FAV_PRIORITY" ) );
cmenu.addentry( 6, true, inp_mngr.get_first_char_for_action( "FAV_AUTO_PICKUP", "INVENTORY" ),
ctxt.get_action_name( "FAV_AUTO_PICKUP" ) );
cmenu.addentry( 7, true, inp_mngr.get_first_char_for_action( "FAV_AUTO_UNLOAD", "INVENTORY" ),
ctxt.get_action_name( "FAV_AUTO_UNLOAD" ) );
cmenu.addentry( 8, true, inp_mngr.get_first_char_for_action( "FAV_CLEAR", "INVENTORY" ),
ctxt.get_action_name( "FAV_CLEAR" ) );
cmenu.query();
std::string ev;
switch( cmenu.ret ) {
case 0:
ev = "FAV_MOVE_ITEM";
break;
case 1:
ev = "FAV_ITEM";
break;
case 2:
ev = "FAV_CATEGORY";
break;
case 3:
ev = "FAV_WHITELIST";
break;
case 4:
ev = "FAV_BLACKLIST";
break;
case 5:
ev = "FAV_PRIORITY";
break;
case 6:
ev = "FAV_AUTO_PICKUP";
break;
case 7:
ev = "FAV_AUTO_UNLOAD";
break;
case 8:
ev = "FAV_CLEAR";
break;
default:
break;
}
const std::vector<input_event> evlist = inp_mngr.get_input_for_action( ev, "INVENTORY" );
if( cmenu.ret >= 0 && cmenu.ret <= 8 && !evlist.empty() ) {
return key( ctxt, evlist.front(), -1, menu );
}
}
return false;
}
item_contents::item_contents( const std::vector<pocket_data> &pockets )
{
for( const pocket_data &data : pockets ) {
contents.emplace_back( &data );
}
}
bool item_contents::empty_with_no_mods() const
{
return contents.empty() ||
std::all_of( contents.begin(), contents.end(), []( const item_pocket & p ) {
return p.is_default_state();
} );
}
bool item_contents::empty() const
{
if( contents.empty() ) {
return true;
}
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::MOD ) ) {
// item mods aren't really contents per se
continue;
}
if( !pocket.empty() ) {
return false;
}
}
return true;
}
bool item_contents::empty_container() const
{
if( contents.empty() ) {
return true;
}
for( const item_pocket &pocket : contents ) {
if( !pocket.is_type( item_pocket::pocket_type::CONTAINER ) ) {
continue;
}
if( !pocket.empty() ) {
return false;
}
}
return true;
}
bool item_contents::full( bool allow_bucket ) const
{
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER )
&& !pocket.full( allow_bucket ) ) {
return false;
}
}
return true;
}
bool item_contents::is_magazine_full() const
{
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::MAGAZINE )
&& pocket.full( false ) ) {
return true;
}
}
return false;
}
bool item_contents::bigger_on_the_inside( const units::volume &container_volume ) const
{
units::volume min_logical_volume = 0_ml;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) ) {
if( pocket.rigid() ) {
min_logical_volume += pocket.max_contains_volume();
} else {
min_logical_volume += pocket.magazine_well();
}
}
}
return container_volume <= min_logical_volume;
}
size_t item_contents::size() const
{
return contents.size();
}
void item_contents::read_mods( const item_contents &read_input )
{
for( const item_pocket &pocket : read_input.contents ) {
if( pocket.saved_type() == item_pocket::pocket_type::MOD ) {
for( const item *it : pocket.all_items_top() ) {
insert_item( *it, item_pocket::pocket_type::MOD );
}
}
}
}
void item_contents::combine( const item_contents &read_input, const bool convert )
{
std::vector<item> uninserted_items;
size_t pocket_index = 0;
for( const item &pocket : read_input.additional_pockets ) {
add_pocket( pocket );
}
for( const item_pocket &pocket : read_input.contents ) {
if( pocket_index < contents.size() ) {
if( convert ) {
if( pocket.is_type( item_pocket::pocket_type::MIGRATION ) ||
pocket.is_type( item_pocket::pocket_type::CORPSE ) ||
pocket.is_type( item_pocket::pocket_type::MAGAZINE ) ||
pocket.is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) {
++pocket_index;
for( const item *it : pocket.all_items_top() ) {
insert_item( *it, pocket.get_pocket_data()->type );
}
continue;
} else if( pocket.is_type( item_pocket::pocket_type::MOD ) ) {
// skipping mod type pocket because using combine this way requires mods to come first
// and to call update_mod_pockets
++pocket_index;
continue;
}
} else {
if( pocket.saved_type() == item_pocket::pocket_type::MOD ) {
// this is already handled in item_contents::read_mods
++pocket_index;
continue;
} else if( pocket.saved_type() == item_pocket::pocket_type::MIGRATION ||
pocket.saved_type() == item_pocket::pocket_type::CORPSE ) {
for( const item *it : pocket.all_items_top() ) {
insert_item( *it, pocket.saved_type() );
}
++pocket_index;
continue;
}
}
auto current_pocket_iter = contents.begin();
std::advance( current_pocket_iter, pocket_index );
for( const item *it : pocket.all_items_top() ) {
const ret_val<item_pocket::contain_code> inserted = current_pocket_iter->insert_item( *it );
if( !inserted.success() ) {
uninserted_items.push_back( *it );
debugmsg( "error: item %s cannot fit into pocket while loading: %s",
it->typeId().str(), inserted.str() );
}
}
if( pocket.saved_sealed() ) {
current_pocket_iter->seal();
}
current_pocket_iter->settings = pocket.settings;
} else {
for( const item *it : pocket.all_items_top() ) {
uninserted_items.push_back( *it );
}
}
++pocket_index;
}
for( const item &uninserted_item : uninserted_items ) {
insert_item( uninserted_item, item_pocket::pocket_type::MIGRATION );
}
}
struct item_contents::item_contents_helper {
// Static helper function to implement the const and non-const versions of
// find_pocket_for with less code duplication
template<typename ItemContents>
using pocket_type = std::conditional_t <
std::is_const<ItemContents>::value,
const item_pocket,
item_pocket
>;
template<typename ItemContents>
static ret_val<pocket_type<ItemContents>*> find_pocket_for(
ItemContents &contents, const item &it, item_pocket::pocket_type pk_type ) {
using my_pocket_type = pocket_type<ItemContents>;
static constexpr item_pocket *null_pocket = nullptr;
std::vector<std::string> failure_messages;
int num_pockets_of_type = 0;
for( my_pocket_type &pocket : contents.contents ) {
if( !pocket.is_type( pk_type ) ) {
continue;
}
if( pk_type != item_pocket::pocket_type::CONTAINER &&
pk_type != item_pocket::pocket_type::MAGAZINE &&
pk_type != item_pocket::pocket_type::MAGAZINE_WELL &&
pocket.is_type( pk_type ) ) {
return ret_val<my_pocket_type *>::make_success(
&pocket, "special pocket type override" );
}
++num_pockets_of_type;
ret_val<item_pocket::contain_code> ret_contain = pocket.can_contain( it );
if( ret_contain.success() ) {
return ret_val<my_pocket_type *>::make_success( &pocket, ret_contain.str() );
} else {
failure_messages.push_back( ret_contain.str() );
}
}
if( failure_messages.empty() ) {
return ret_val<my_pocket_type *>::make_failure( null_pocket, _( "pocket with type (%s) not found" ),
io::enum_to_string( pk_type ) );
}
std::sort( failure_messages.begin(), failure_messages.end(), localized_compare );
failure_messages.erase(
std::unique( failure_messages.begin(), failure_messages.end() ),
failure_messages.end() );
return ret_val<my_pocket_type *>::make_failure(
null_pocket,
n_gettext( "pocket unacceptable because %s", "pockets unacceptable because %s",
num_pockets_of_type ),
enumerate_as_string( failure_messages, enumeration_conjunction::or_ ) );
}
};
ret_val<item_pocket *> item_contents::find_pocket_for( const item &it,
item_pocket::pocket_type pk_type )
{
return item_contents_helper::find_pocket_for( *this, it, pk_type );
}
ret_val<const item_pocket *> item_contents::find_pocket_for( const item &it,
item_pocket::pocket_type pk_type ) const
{
return item_contents_helper::find_pocket_for( *this, it, pk_type );
}
int item_contents::obtain_cost( const item &it ) const
{
for( const item_pocket &pocket : contents ) {
const int mv = pocket.obtain_cost( it );
if( mv != 0 ) {
return mv;
}
}
return 0;
}
int item_contents::insert_cost( const item &it ) const
{
ret_val<const item_pocket *> pocket = find_pocket_for( it, item_pocket::pocket_type::CONTAINER );
if( pocket.success() ) {
return pocket.value()->moves();
} else {
return -1;
}
}
ret_val<item_pocket *> item_contents::insert_item( const item &it,
item_pocket::pocket_type pk_type )
{
if( pk_type == item_pocket::pocket_type::LAST ) {
// LAST is invalid, so we assume it will be a regular container
pk_type = item_pocket::pocket_type::CONTAINER;
}
ret_val<item_pocket *> pocket = find_pocket_for( it, pk_type );
if( !pocket.success() ) {
return pocket;
}
if( !pocket.value()->is_allowed() ) {
return ret_val<item_pocket *>::make_failure( nullptr, _( "Can't store anything in this." ) );
}
ret_val<item_pocket::contain_code> pocket_contain_code = pocket.value()->insert_item( it );
if( pocket_contain_code.success() ) {
return pocket;
}
return ret_val<item_pocket *>::make_failure( nullptr, pocket_contain_code.str() );
}
void item_contents::force_insert_item( const item &it, item_pocket::pocket_type pk_type )
{
for( item_pocket &pocket : contents ) {
if( pocket.is_type( pk_type ) ) {
pocket.add( it );
return;
}
}
debugmsg( "ERROR: Could not insert item %s as contents does not have pocket type", it.tname() );
}
std::pair<item_location, item_pocket *> item_contents::best_pocket( const item &it,
item_location &this_loc, const item *avoid, const bool allow_sealed, const bool ignore_settings,
const bool nested, bool ignore_rigidity )
{
// @TODO: this could be made better by doing a plain preliminary volume check.
// if the total volume of the parent is not sufficient, a child won't have enough either.
std::pair<item_location, item_pocket *> ret = { this_loc, nullptr };
std::vector<item_pocket *> valid_pockets;
for( item_pocket &pocket : contents ) {
if( !pocket.is_type( item_pocket::pocket_type::CONTAINER ) ) {
// best pocket is for picking stuff up.
// containers are the only pockets that are available for such
continue;
}
if( !allow_sealed && pocket.sealed() ) {
// we don't want to unseal a pocket to put something in it automatically
// that needs to be something a player explicitly does
continue;
}
valid_pockets.emplace_back( &pocket );
if( !ignore_settings && !pocket.settings.accepts_item( it ) ) {
// Item forbidden by whitelist / blacklist
continue;
}
if( !pocket.rigid() && (
!pocket.settings.get_item_whitelist().empty() ||
!pocket.settings.get_category_whitelist().empty() ||
pocket.settings.priority() > 0 ) ) {
ignore_rigidity = true;
}
if( !pocket.can_contain( it ).success() || ( !ignore_rigidity && nested && !pocket.rigid() ) ) {
// non-rigid nested pocket makes no sense, item should also be able to fit in parent.
continue;
}
if( ret.second == nullptr || ret.second->better_pocket( pocket, it, /*nested=*/nested ) ) {
ret.second = &pocket;
}
}
const bool parent_pkt_selected = !!ret.second;
for( item_pocket *pocket : valid_pockets ) {
std::pair<item_location, item_pocket *const> nested_content_pocket =
pocket->best_pocket_in_contents( this_loc, it, avoid, allow_sealed, ignore_settings );
if( !nested_content_pocket.second ||
( !nested_content_pocket.second->rigid() && pocket->remaining_volume() < it.volume() ) ) {
// no nested pocket found, or the nested pocket is soft and the parent is full
continue;
}
if( parent_pkt_selected ) {
if( ret.second->better_pocket( *nested_content_pocket.second, it, true ) ) {
// item is whitelisted in nested pocket, prefer that over parent pocket
ret = nested_content_pocket;
}
continue;
}
ret = nested_content_pocket;
}
return ret;
}
item_pocket *item_contents::contained_where( const item &contained )
{
for( item_pocket &pocket : contents ) {
if( pocket.has_item( contained ) ) {
return &pocket;
}
}
return nullptr;
}
units::length item_contents::max_containable_length( const bool unrestricted_pockets_only ) const
{
units::length ret = 0_mm;
for( const item_pocket &pocket : contents ) {
bool restriction_condition = !pocket.is_type( item_pocket::pocket_type::CONTAINER ) ||
pocket.is_ablative() || pocket.holster_full();
if( unrestricted_pockets_only ) {
restriction_condition = restriction_condition && pocket.is_restricted();
}
if( restriction_condition ) {
continue;
}
units::length candidate = pocket.max_containable_length();
if( candidate > ret ) {
ret = candidate;
}
}
return ret;
}
units::length item_contents::min_containable_length() const
{
units::length ret = 0_mm;
for( const item_pocket &pocket : contents ) {
if( !pocket.is_type( item_pocket::pocket_type::CONTAINER ) || pocket.is_ablative() ||
pocket.holster_full() ) {
continue;
}
units::length candidate = pocket.min_containable_length();
if( candidate > ret ) {
ret = candidate;
}
}
return ret;
}
std::set<flag_id> item_contents::magazine_flag_restrictions() const
{
std::set<flag_id> ret;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) {
ret = pocket.get_pocket_data()->get_flag_restrictions();
}
}
return ret;
}
units::volume item_contents::max_containable_volume( const bool unrestricted_pockets_only ) const
{
units::volume ret = 0_ml;
for( const item_pocket &pocket : contents ) {
bool restriction_condition = !pocket.is_type( item_pocket::pocket_type::CONTAINER ) ||
pocket.is_ablative() || pocket.holster_full() ||
pocket.volume_capacity() >= pocket_data::max_volume_for_container;
if( unrestricted_pockets_only ) {
restriction_condition = restriction_condition && pocket.is_restricted();
}
if( restriction_condition ) {
continue;
}
units::volume candidate = pocket.remaining_volume();
if( candidate > ret ) {
ret = candidate;
}
}
return ret;
}
ret_val<bool> item_contents::is_compatible( const item &it ) const
{
ret_val<bool> ret = ret_val<bool>::make_failure( _( "is not a container" ) );
for( const item_pocket &pocket : contents ) {
// mod, migration, corpse, and software aren't regular pockets.
if( !pocket.is_standard_type() ) {
continue;
}
const ret_val<item_pocket::contain_code> pocket_contain_code = pocket.is_compatible( it );
if( pocket_contain_code.success() ) {
return ret_val<bool>::make_success();
}
ret = ret_val<bool>::make_failure( pocket_contain_code.str() );
}
return ret;
}
ret_val<bool> item_contents::can_contain_rigid( const item &it,
const bool ignore_pkt_settings ) const
{
ret_val<bool> ret = ret_val<bool>::make_failure( _( "is not a container" ) );
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::MOD ) ||
pocket.is_type( item_pocket::pocket_type::CORPSE ) ||
pocket.is_type( item_pocket::pocket_type::MIGRATION ) ) {
continue;
}
if( !pocket.rigid() ) {
ret = ret_val<bool>::make_failure( _( "is not rigid" ) );
continue;
}
if( !ignore_pkt_settings && !pocket.settings.accepts_item( it ) ) {
ret = ret_val<bool>::make_failure( _( "denied by pocket auto insert settings" ) );
continue;
}
const ret_val<item_pocket::contain_code> pocket_contain_code = pocket.can_contain( it );
if( pocket_contain_code.success() ) {
return ret_val<bool>::make_success();
}
ret = ret_val<bool>::make_failure( pocket_contain_code.str() );
}
return ret;
}
ret_val<bool> item_contents::can_contain( const item &it, const bool ignore_pkt_settings ) const
{
ret_val<bool> ret = ret_val<bool>::make_failure( _( "is not a container" ) );
for( const item_pocket &pocket : contents ) {
// mod, migration, corpse, and software aren't regular pockets.
if( !pocket.is_standard_type() ) {
continue;
}
if( !ignore_pkt_settings && !pocket.settings.accepts_item( it ) ) {
ret = ret_val<bool>::make_failure( _( "denied by pocket auto insert settings" ) );
continue;
}
const ret_val<item_pocket::contain_code> pocket_contain_code = pocket.can_contain( it );
if( pocket_contain_code.success() ) {
return ret_val<bool>::make_success();
}
ret = ret_val<bool>::make_failure( pocket_contain_code.str() );
}
return ret;
}
bool item_contents::can_contain_liquid( bool held_or_ground ) const
{
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) &&
pocket.can_contain_liquid( held_or_ground ) ) {
return true;
}
}
return false;
}
bool item_contents::contains_no_solids() const
{
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) &&
pocket.contains_phase( phase_id::SOLID ) ) {
return false;
}
}
return true;
}
bool item_contents::can_reload_with( const item &ammo, const bool now ) const
{
for( const item_pocket *pocket : get_all_reloadable_pockets() ) {
if( pocket->can_reload_with( ammo, now ) ) {
return true;
}
}
return false;
}
bool item_contents::can_unload_liquid() const
{
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::CONTAINER ) && pocket.can_unload_liquid() ) {
return true;
}
}
return false;
}
size_t item_contents::num_item_stacks() const
{
size_t num = 0;
for( const item_pocket &pocket : contents ) {
if( pocket.is_type( item_pocket::pocket_type::MOD ) ||
pocket.is_type( item_pocket::pocket_type::MAGAZINE_WELL ) ) {
// mods and magazine wells aren't really a contained item, which this function gets
continue;
}
num += pocket.size();