forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitable.cpp
1123 lines (978 loc) · 33.1 KB
/
visitable.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 "visitable.h"
#include <algorithm>
#include <climits>
#include <limits>
#include <map>
#include <memory>
#include <unordered_map>
#include <utility>
#include "active_item_cache.h"
#include "bionics.h"
#include "character.h"
#include "colony.h"
#include "debug.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "map.h"
#include "map_selector.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "pimpl.h"
#include "player.h"
#include "point.h"
#include "submap.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_selector.h"
static const itype_id itype_apparatus( "apparatus" );
static const itype_id itype_adv_UPS_off( "adv_UPS_off" );
static const itype_id itype_toolset( "toolset" );
static const itype_id itype_UPS( "UPS" );
static const itype_id itype_UPS_off( "UPS_off" );
static const itype_id itype_bio_armor( "bio_armor" );
static const quality_id qual_BUTCHER( "BUTCHER" );
static const bionic_id bio_tools( "bio_tools" );
static const bionic_id bio_ups( "bio_ups" );
static const flag_str_id flag_BIONIC_ARMOR_INTERFACE( "BIONIC_ARMOR_INTERFACE" );
/** @relates visitable */
template <typename T>
item *visitable<T>::find_parent( const item &it )
{
item *res = nullptr;
if( visit_items( [&]( item * node, item * parent ) {
if( node == &it ) {
res = parent;
return VisitResponse::ABORT;
}
return VisitResponse::NEXT;
} ) != VisitResponse::ABORT ) {
debugmsg( "Tried to find item parent using an object that doesn't contain it" );
}
return res;
}
/** @relates visitable */
template <typename T>
const item *visitable<T>::find_parent( const item &it ) const
{
return const_cast<visitable<T> *>( this )->find_parent( it );
}
/** @relates visitable */
template <typename T>
std::vector<item *> visitable<T>::parents( const item &it )
{
std::vector<item *> res;
for( item *obj = find_parent( it ); obj; obj = find_parent( *obj ) ) {
res.push_back( obj );
}
return res;
}
/** @relates visitable */
template <typename T>
std::vector<const item *> visitable<T>::parents( const item &it ) const
{
std::vector<const item *> res;
for( const item *obj = find_parent( it ); obj; obj = find_parent( *obj ) ) {
res.push_back( obj );
}
return res;
}
/** @relates visitable */
template <typename T>
bool visitable<T>::has_item( const item &it ) const
{
return visit_items( [&it]( const item * node ) {
return node == &it ? VisitResponse::ABORT : VisitResponse::NEXT;
} ) == VisitResponse::ABORT;
}
/** @relates visitable */
template <typename T>
bool visitable<T>::has_item_with( const std::function<bool( const item & )> &filter ) const
{
return visit_items( [&filter]( const item * node ) {
return filter( *node ) ? VisitResponse::ABORT : VisitResponse::NEXT;
} ) == VisitResponse::ABORT;
}
/** Sums the two terms, being careful to not trigger overflow.
* Doesn't handle underflow.
*
* @param a The first addend.
* @param b The second addend.
* @return the sum of the addends, but truncated to std::numeric_limits<int>::max().
*/
template <typename T>
static T sum_no_wrap( T a, T b )
{
if( a > std::numeric_limits<T>::max() - b ||
b > std::numeric_limits<T>::max() - a ) {
return std::numeric_limits<T>::max();
}
return a + b;
}
template <typename T>
static int has_quality_internal( const T &self, const quality_id &qual, int level, int limit )
{
int qty = 0;
self.visit_items( [&qual, level, &limit, &qty]( const item * e ) {
if( e->get_quality( qual ) >= level ) {
qty = sum_no_wrap( qty, static_cast<int>( e->count() ) );
if( qty >= limit ) {
// found sufficient items
return VisitResponse::ABORT;
}
}
return VisitResponse::NEXT;
} );
return std::min( qty, limit );
}
static int has_quality_from_vpart( const vehicle &veh, int part, const quality_id &qual, int level,
int limit )
{
int qty = 0;
auto pos = veh.cpart( part ).mount;
for( const auto &n : veh.parts_at_relative( pos, true ) ) {
// only unbroken parts can provide tool qualities
if( !veh.cpart( n ).is_broken() ) {
auto tq = veh.part_info( n ).qualities;
auto iter = tq.find( qual );
// does the part provide this quality?
if( iter != tq.end() && iter->second >= level ) {
qty = sum_no_wrap( qty, 1 );
}
}
}
return std::min( qty, limit );
}
template <typename T>
bool visitable<T>::has_quality( const quality_id &qual, int level, int qty ) const
{
return has_quality_internal( *this, qual, level, qty ) == qty;
}
/** @relates visitable */
template <>
bool visitable<inventory>::has_quality( const quality_id &qual, int level, int qty ) const
{
const inventory *inv = static_cast<const inventory *>( this );
const std::map<quality_id, std::map<int, int>> &inv_qual_cache = inv->get_quality_cache();
int res = 0;
if( !inv_qual_cache.empty() ) {
auto iter = inv_qual_cache.find( qual );
if( iter != inv_qual_cache.end() ) {
for( const auto &q : iter->second ) {
if( q.first >= level ) {
res = sum_no_wrap( res, q.second );
}
}
}
return res >= qty;
}
for( const auto &stack : inv->items ) {
res += stack.size() * has_quality_internal( stack.front(), qual, level, qty );
if( res >= qty ) {
return true;
}
}
return false;
}
/** @relates visitable */
template <>
bool visitable<vehicle_selector>::has_quality( const quality_id &qual, int level, int qty ) const
{
for( const auto &cursor : static_cast<const vehicle_selector &>( *this ) ) {
qty -= has_quality_from_vpart( cursor.veh, cursor.part, qual, level, qty );
if( qty <= 0 ) {
return true;
}
}
return has_quality_internal( *this, qual, level, qty ) == qty;
}
/** @relates visitable */
template <>
bool visitable<vehicle_cursor>::has_quality( const quality_id &qual, int level, int qty ) const
{
auto self = static_cast<const vehicle_cursor *>( this );
qty -= has_quality_from_vpart( self->veh, self->part, qual, level, qty );
return qty <= 0 ? true : has_quality_internal( *this, qual, level, qty ) == qty;
}
/** @relates visitable */
template <>
bool visitable<Character>::has_quality( const quality_id &qual, int level, int qty ) const
{
auto self = static_cast<const Character *>( this );
for( const auto &bio : *self->my_bionics ) {
if( bio.get_quality( qual ) >= level ) {
if( qty <= 1 ) {
return true;
}
qty--;
}
}
return qty <= 0 ? true : has_quality_internal( *this, qual, level, qty ) == qty;
}
template <typename T>
static int max_quality_internal( const T &self, const quality_id &qual )
{
int res = INT_MIN;
self.visit_items( [&res, &qual]( const item * e ) {
res = std::max( res, e->get_quality( qual ) );
return VisitResponse::NEXT;
} );
return res;
}
static int max_quality_from_vpart( const vehicle &veh, int part, const quality_id &qual )
{
int res = INT_MIN;
auto pos = veh.cpart( part ).mount;
for( const auto &n : veh.parts_at_relative( pos, true ) ) {
// only unbroken parts can provide tool qualities
if( !veh.cpart( n ).is_broken() ) {
auto tq = veh.part_info( n ).qualities;
auto iter = tq.find( qual );
// does the part provide this quality?
if( iter != tq.end() ) {
res = std::max( res, iter->second );
}
}
}
return res;
}
template <typename T>
int visitable<T>::max_quality( const quality_id &qual ) const
{
return max_quality_internal( *this, qual );
}
/** @relates visitable */
template<>
int visitable<Character>::max_quality( const quality_id &qual ) const
{
int res = INT_MIN;
auto self = static_cast<const Character *>( this );
for( const auto &bio : *self->my_bionics ) {
res = std::max( res, bio.get_quality( qual ) );
}
if( qual == qual_BUTCHER ) {
for( const trait_id &mut : self->get_mutations() ) {
res = std::max( res, mut->butchering_quality );
}
}
return std::max( res, max_quality_internal( *this, qual ) );
}
/** @relates visitable */
template <>
int visitable<vehicle_cursor>::max_quality( const quality_id &qual ) const
{
auto self = static_cast<const vehicle_cursor *>( this );
return std::max( max_quality_from_vpart( self->veh, self->part, qual ),
max_quality_internal( *this, qual ) );
}
/** @relates visitable */
template <>
int visitable<vehicle_selector>::max_quality( const quality_id &qual ) const
{
int res = INT_MIN;
for( const auto &e : static_cast<const vehicle_selector &>( *this ) ) {
res = std::max( res, e.max_quality( qual ) );
}
return res;
}
/** @relates visitable */
template <typename T>
std::vector<item *> visitable<T>::items_with( const std::function<bool( const item & )> &filter )
{
std::vector<item *> res;
visit_items( [&res, &filter]( item * node, item * ) {
if( filter( *node ) ) {
res.push_back( node );
}
return VisitResponse::NEXT;
} );
return res;
}
/** @relates visitable */
template <typename T>
std::vector<const item *>
visitable<T>::items_with( const std::function<bool( const item & )> &filter ) const
{
std::vector<const item *> res;
visit_items( [&res, &filter]( const item * node, const item * ) {
if( filter( *node ) ) {
res.push_back( node );
}
return VisitResponse::NEXT;
} );
return res;
}
/** @relates visitable */
template <typename T>
VisitResponse
visitable<T>::visit_items( const std::function<VisitResponse( const item *,
const item * )> &func ) const
{
return const_cast<visitable<T> *>( this )->visit_items(
static_cast<const std::function<VisitResponse( item *, item * )>&>( func ) );
}
/** @relates visitable */
template <typename T> VisitResponse
visitable<T>::visit_items( const std::function<VisitResponse( const item * )> &func ) const
{
return const_cast<visitable<T> *>( this )->visit_items(
static_cast<const std::function<VisitResponse( item * )>&>( func ) );
}
/** @relates visitable */
template <typename T>
VisitResponse visitable<T>::visit_items( const std::function<VisitResponse( item * )> &func )
{
return visit_items( [&func]( item * it, item * ) {
return func( it );
} );
}
// Specialize visitable<T>::visit_items() for each class that will implement the visitable interface
static VisitResponse visit_internal( const std::function<VisitResponse( item *, item * )> &func,
item *node, item *parent = nullptr )
{
switch( func( node, parent ) ) {
case VisitResponse::ABORT:
return VisitResponse::ABORT;
case VisitResponse::NEXT:
if( node->is_gun() || node->is_magazine() ) {
// Content of guns and magazines are accessible only via their specific accessors
return VisitResponse::NEXT;
}
if( node->contents.visit_contents( func, node ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
/* intentional fallthrough */
case VisitResponse::SKIP:
return VisitResponse::NEXT;
}
/* never reached but suppresses GCC warning */
return VisitResponse::ABORT;
}
VisitResponse item_contents::visit_contents( const std::function<VisitResponse( item *, item * )>
&func, item *parent )
{
for( item &e : items ) {
switch( visit_internal( func, &e, parent ) ) {
case VisitResponse::ABORT:
return VisitResponse::ABORT;
default:
break;
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<item>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
auto it = static_cast<item *>( this );
return visit_internal( func, it );
}
/** @relates visitable */
template <>
VisitResponse visitable<inventory>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
auto inv = static_cast<inventory *>( this );
for( auto &stack : inv->items ) {
for( auto &it : stack ) {
if( visit_internal( func, &it ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<Character>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
auto ch = static_cast<Character *>( this );
for( item *weapon : ch->wielded_items() ) {
if( visit_internal( func, weapon ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
for( auto &e : ch->worn ) {
if( visit_internal( func, &e ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
return ch->inv.visit_items( func );
}
/** @relates visitable */
template <>
VisitResponse visitable<map_cursor>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
auto cur = static_cast<map_cursor *>( this );
map &here = get_map();
// skip inaccessible items
if( here.has_flag( "SEALED", *cur ) && !here.has_flag( "LIQUIDCONT", *cur ) ) {
return VisitResponse::NEXT;
}
for( item &e : here.i_at( *cur ) ) {
if( visit_internal( func, &e ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<map_selector>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
for( auto &cursor : static_cast<map_selector &>( *this ) ) {
if( cursor.visit_items( func ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<vehicle_cursor>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
auto self = static_cast<vehicle_cursor *>( this );
int idx = self->veh.part_with_feature( self->part, "CARGO", true );
if( idx >= 0 ) {
for( auto &e : self->veh.get_items( idx ) ) {
if( visit_internal( func, &e ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<vehicle_selector>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
for( auto &cursor : static_cast<vehicle_selector &>( *this ) ) {
if( cursor.visit_items( func ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
return VisitResponse::NEXT;
}
/** @relates visitable */
template <>
VisitResponse visitable<monster>::visit_items(
const std::function<VisitResponse( item *, item * )> &func )
{
monster *mon = static_cast<monster *>( this );
for( item &it : mon->inv ) {
if( visit_internal( func, &it ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
}
if( mon->storage_item && visit_internal( func, &*mon->storage_item ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
if( mon->armor_item && visit_internal( func, &*mon->armor_item ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
if( mon->tack_item && visit_internal( func, &*mon->tack_item ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
if( mon->tied_item && visit_internal( func, &*mon->tied_item ) == VisitResponse::ABORT ) {
return VisitResponse::ABORT;
}
return VisitResponse::NEXT;
}
// Specialize visitable<T>::remove_items_with() for each class that will implement the visitable interface
/** @relates visitable */
template <typename T>
item visitable<T>::remove_item( item &it )
{
auto obj = remove_items_with( [&it]( const item & e ) {
return &e == ⁢
}, 1 );
if( !obj.empty() ) {
return obj.front();
} else {
debugmsg( "Tried removing item from object which did not contain it" );
return item();
}
}
bool item_contents::remove_internal( const std::function<bool( item & )> &filter,
int &count, std::list<item> &res )
{
for( auto it = items.begin(); it != items.end(); ) {
if( filter( *it ) ) {
res.splice( res.end(), items, it++ );
if( --count == 0 ) {
return true;
}
} else {
it->contents.remove_internal( filter, count, res );
++it;
}
}
return false;
}
/** @relates visitable */
template <>
std::list<item> visitable<item>::remove_items_with( const std::function<bool( const item &e )>
&filter, int count )
{
item *it = static_cast<item *>( this );
std::list<item> res;
if( count <= 0 ) {
// nothing to do
return res;
}
it->contents.remove_internal( filter, count, res );
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<inventory>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
auto inv = static_cast<inventory *>( this );
std::list<item> res;
if( count <= 0 ) {
// nothing to do
return res;
}
for( auto stack = inv->items.begin(); stack != inv->items.end() && count > 0; ) {
std::list<item> &istack = *stack;
const auto original_invlet = istack.front().invlet;
for( auto istack_iter = istack.begin(); istack_iter != istack.end() && count > 0; ) {
if( filter( *istack_iter ) ) {
count--;
res.splice( res.end(), istack, istack_iter++ );
// The non-first items of a stack may have different invlets, the code
// in inventory only ever checks the invlet of the first item. This
// ensures that the first item of a stack always has the same invlet, even
// after the original first item was removed.
if( istack_iter == istack.begin() && istack_iter != istack.end() ) {
istack_iter->invlet = original_invlet;
}
} else {
istack_iter->contents.remove_internal( filter, count, res );
++istack_iter;
}
}
if( istack.empty() ) {
stack = inv->items.erase( stack );
} else {
++stack;
}
}
// Invalidate binning cache
inv->binned = false;
inv->items_type_cached = false;
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<Character>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
auto ch = static_cast<Character *>( this );
std::list<item> res;
if( count <= 0 ) {
// nothing to do
return res;
}
// first try and remove items from the inventory
res = ch->inv.remove_items_with( filter, count );
count -= res.size();
if( count == 0 ) {
return res;
}
// then try any worn items
for( auto iter = ch->worn.begin(); iter != ch->worn.end(); ) {
if( filter( *iter ) ) {
iter->on_takeoff( *ch );
res.splice( res.end(), ch->worn, iter++ );
if( --count == 0 ) {
return res;
}
} else {
iter->contents.remove_internal( filter, count, res );
if( count == 0 ) {
return res;
}
++iter;
}
}
// finally try the currently wielded item (if any)
if( filter( ch->primary_weapon() ) ) {
res.push_back( ch->remove_weapon() );
count--;
} else {
ch->primary_weapon().contents.remove_internal( filter, count, res );
}
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<map_cursor>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
auto cur = static_cast<map_cursor *>( this );
std::list<item> res;
if( count <= 0 ) {
// nothing to do
return res;
}
map &here = get_map();
if( !here.inbounds( *cur ) ) {
debugmsg( "cannot remove items from map: cursor out-of-bounds" );
return res;
}
// fetch the appropriate item stack
point offset;
submap *sub = here.get_submap_at( *cur, offset );
cata::colony<item> &stack = sub->get_items( offset );
for( auto iter = stack.begin(); iter != stack.end(); ) {
if( filter( *iter ) ) {
// remove from the active items cache (if it isn't there does nothing)
sub->active_items.remove( &*iter );
// if necessary remove item from the luminosity map
sub->update_lum_rem( offset, *iter );
// finally remove the item
res.push_back( *iter );
iter = stack.erase( iter );
if( --count == 0 ) {
return res;
}
} else {
iter->contents.remove_internal( filter, count, res );
if( count == 0 ) {
return res;
}
++iter;
}
}
here.update_submap_active_item_status( *cur );
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<map_selector>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
std::list<item> res;
for( auto &cursor : static_cast<map_selector &>( *this ) ) {
std::list<item> out = cursor.remove_items_with( filter, count );
count -= out.size();
res.splice( res.end(), out );
}
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<vehicle_cursor>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
auto cur = static_cast<vehicle_cursor *>( this );
std::list<item> res;
if( count <= 0 ) {
// nothing to do
return res;
}
int idx = cur->veh.part_with_feature( cur->part, "CARGO", false );
if( idx < 0 ) {
return res;
}
vehicle_part &part = cur->veh.part( idx );
for( auto iter = part.items.begin(); iter != part.items.end(); ) {
if( filter( *iter ) ) {
// remove from the active items cache (if it isn't there does nothing)
cur->veh.active_items.remove( &*iter );
res.push_back( *iter );
iter = part.items.erase( iter );
if( --count == 0 ) {
return res;
}
} else {
iter->contents.remove_internal( filter, count, res );
if( count == 0 ) {
return res;
}
++iter;
}
}
if( !res.empty() ) {
// if we removed any items then invalidate the cached mass
cur->veh.invalidate_mass();
}
return res;
}
/** @relates visitable */
template <>
std::list<item> visitable<vehicle_selector>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
std::list<item> res;
for( auto &cursor : static_cast<vehicle_selector &>( *this ) ) {
std::list<item> out = cursor.remove_items_with( filter, count );
count -= out.size();
res.splice( res.end(), out );
}
return res;
}
static void remove_from_item_valptr(
cata::value_ptr<item> &ptr,
const std::function<bool( const item &e )> &filter,
int &count, std::list<item> &res )
{
if( ptr ) {
if( filter( *ptr ) ) {
res.push_back( *ptr );
ptr.reset();
count -= 1;
} else {
ptr->contents.remove_internal( filter, count, res );
}
}
}
/** @relates visitable */
template <>
std::list<item> visitable<monster>::remove_items_with( const
std::function<bool( const item &e )> &filter, int count )
{
std::list<item> res;
monster *mon = static_cast<monster *>( this );
for( auto iter = mon->inv.begin(); iter != mon->inv.end(); ) {
if( filter( *iter ) ) {
res.push_back( *iter );
iter = mon->inv.erase( iter );
count -= 1;
if( count == 0 ) {
return res;
}
} else {
iter->contents.remove_internal( filter, count, res );
if( count == 0 ) {
return res;
}
iter++;
}
}
remove_from_item_valptr( mon->storage_item, filter, count, res );
if( count == 0 ) {
return res;
}
remove_from_item_valptr( mon->armor_item, filter, count, res );
if( count == 0 ) {
return res;
}
remove_from_item_valptr( mon->tack_item, filter, count, res );
if( count == 0 ) {
return res;
}
remove_from_item_valptr( mon->tied_item, filter, count, res );
return res;
}
template <typename T, typename M>
static int charges_of_internal( const T &self, const M &main, const itype_id &id, int limit,
const std::function<bool( const item & )> &filter,
std::function<void( int )> visitor )
{
int qty = 0;
bool found_tool_with_UPS = false;
self.visit_items( [&]( const item * e ) {
if( filter( *e ) ) {
if( e->is_tool() ) {
if( e->typeId() == id ) {
// includes charges from any included magazine.
qty = sum_no_wrap( qty, e->ammo_remaining() );
if( e->has_flag( "USE_UPS" ) ) {
found_tool_with_UPS = true;
}
}
return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT;
} else if( e->count_by_charges() ) {
if( e->typeId() == id ) {
qty = sum_no_wrap( qty, e->charges );
}
// items counted by charges are not themselves expected to be containers
return qty < limit ? VisitResponse::SKIP : VisitResponse::ABORT;
}
}
// recurse through any nested containers
return qty < limit ? VisitResponse::NEXT : VisitResponse::ABORT;
} );
if( qty < limit && found_tool_with_UPS ) {
qty += main.charges_of( itype_UPS, limit - qty );
if( visitor ) {
visitor( qty );
}
}
return std::min( qty, limit );
}
/** @relates visitable */
template <typename T>
int visitable<T>::charges_of( const itype_id &what, int limit,
const std::function<bool( const item & )> &filter,
std::function<void( int )> visitor ) const
{
return charges_of_internal( *this, *this, what, limit, filter, visitor );
}
/** @relates visitable */
template <>
int visitable<inventory>::charges_of( const itype_id &what, int limit,
const std::function<bool( const item & )> &filter,
std::function<void( int )> visitor ) const
{
if( what == itype_UPS ) {
int qty = 0;
qty = sum_no_wrap( qty, charges_of( itype_UPS_off ) );
qty = sum_no_wrap( qty, static_cast<int>( charges_of( itype_adv_UPS_off ) / 0.6 ) );
return std::min( qty, limit );
}
const auto &binned = static_cast<const inventory *>( this )->get_binned_items();
const auto iter = binned.find( what );
if( iter == binned.end() ) {
return 0;
}
int res = 0;
for( const item *it : iter->second ) {
res = sum_no_wrap( res, charges_of_internal( *it, *this, what, limit, filter, visitor ) );
if( res >= limit ) {
break;
}
}
return std::min( limit, res );
}
/** @relates visitable */
template <>
int visitable<Character>::charges_of( const itype_id &what, int limit,
const std::function<bool( const item & )> &filter,
std::function<void( int )> visitor ) const
{
auto self = static_cast<const Character *>( this );
auto p = dynamic_cast<const player *>( self );
if( what == itype_toolset ) {
if( p && p->has_active_bionic( bio_tools ) ) {
return std::min( units::to_kilojoule( p->get_power_level() ), limit );
} else {
return 0;
}
}
if( what == itype_bio_armor ) {
float efficiency = 1;
int power_charges = 0;
for( const bionic &bio : *self->my_bionics ) {