-
Notifications
You must be signed in to change notification settings - Fork 272
/
item.cpp
10158 lines (8998 loc) · 358 KB
/
item.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.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iterator>
#include <limits>
#include <locale>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_set>
#include "advanced_inv.h"
#include "ammo.h"
#include "ascii_art.h"
#include "avatar.h"
#include "bionics.h"
#include "bodypart.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "character_id.h"
#include "character_encumbrance.h"
#include "character_functions.h"
#include "character_martial_arts.h"
#include "character_stat.h"
#include "clothing_mod.h"
#include "clzones.h"
#include "color.h"
#include "coordinate_conversions.h"
#include "craft_command.h"
#include "damage.h"
#include "debug.h"
#include "dispersion.h"
#include "drop_token.h"
#include "effect.h" // for weed_msg
#include "enums.h"
#include "explosion.h"
#include "faction.h"
#include "fault.h"
#include "field_type.h"
#include "fire.h"
#include "flag.h"
#include "game.h"
#include "game_constants.h"
#include "gun_mode.h"
#include "iexamine.h"
#include "int_id.h"
#include "inventory.h"
#include "item_category.h"
#include "item_factory.h"
#include "item_group.h"
#include "iteminfo_query.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "line.h"
#include "magic.h"
#include "map.h"
#include "martialarts.h"
#include "material.h"
#include "messages.h"
#include "mod_manager.h"
#include "monster.h"
#include "mtype.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "player.h"
#include "pldata.h"
#include "point.h"
#include "projectile.h"
#include "ranged.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "relic.h"
#include "requirements.h"
#include "ret_val.h"
#include "rng.h"
#include "skill.h"
#include "stomach.h"
#include "string_formatter.h"
#include "string_id_utils.h"
#include "string_utils.h"
#include "text_snippets.h"
#include "translations.h"
#include "units.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vitamin.h"
#include "vpart_position.h"
#include "weather.h"
#include "weather_gen.h"
static const std::string GUN_MODE_VAR_NAME( "item::mode" );
static const std::string CLOTHING_MOD_VAR_PREFIX( "clothing_mod_" );
static const ammo_effect_str_id ammo_effect_BLACKPOWDER( "BLACKPOWDER" );
static const ammo_effect_str_id ammo_effect_INCENDIARY( "INCENDIARY" );
static const ammo_effect_str_id ammo_effect_NEVER_MISFIRES( "NEVER_MISFIRES" );
static const ammo_effect_str_id ammo_effect_RECYCLED( "RECYCLED" );
static const ammotype ammo_battery( "battery" );
static const ammotype ammo_plutonium( "plutonium" );
static const item_category_id itemcat_drugs( "drugs" );
static const item_category_id itemcat_food( "food" );
static const item_category_id itemcat_maps( "maps" );
static const efftype_id effect_cig( "cig" );
static const efftype_id effect_shakes( "shakes" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_weed_high( "weed_high" );
static const fault_id fault_gun_blackpowder( "fault_gun_blackpowder" );
static const fault_id fault_bionic_nonsterile( "fault_bionic_nonsterile" );
static const gun_mode_id gun_mode_REACH( "REACH" );
static const itype_id itype_barrel_small( "barrel_small" );
static const itype_id itype_blood( "blood" );
static const itype_id itype_brass_catcher( "brass_catcher" );
static const itype_id itype_cig_butt( "cig_butt" );
static const itype_id itype_cig_lit( "cig_lit" );
static const itype_id itype_cigar_butt( "cigar_butt" );
static const itype_id itype_cigar_lit( "cigar_lit" );
static const itype_id itype_hand_crossbow( "hand_crossbow" );
static const itype_id itype_joint_roach( "joint_roach" );
static const itype_id itype_plut_cell( "plut_cell" );
static const itype_id itype_rad_badge( "rad_badge" );
static const itype_id itype_tuned_mechanism( "tuned_mechanism" );
static const itype_id itype_stock_small( "stock_small" );
static const itype_id itype_UPS( "UPS" );
static const itype_id itype_bio_armor( "bio_armor" );
static const itype_id itype_waterproof_gunmod( "waterproof_gunmod" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_acid( "water_acid" );
static const itype_id itype_water_acid_weak( "water_acid_weak" );
static const skill_id skill_survival( "survival" );
static const skill_id skill_unarmed( "unarmed" );
static const skill_id skill_weapon( "weapon" );
static const quality_id qual_JACK( "JACK" );
static const quality_id qual_LIFT( "LIFT" );
static const species_id ROBOT( "ROBOT" );
static const std::string trait_flag_CANNIBAL( "CANNIBAL" );
static const bionic_id bio_digestion( "bio_digestion" );
static const trait_id trait_CARNIVORE( "CARNIVORE" );
static const trait_id trait_LIGHTWEIGHT( "LIGHTWEIGHT" );
static const trait_id trait_SAPROVORE( "SAPROVORE" );
static const trait_id trait_SQUEAMISH( "SQUEAMISH" );
static const trait_id trait_TOLERANCE( "TOLERANCE" );
static const trait_id trait_WOOLALLERGY( "WOOLALLERGY" );
static const std::string flag_ALWAYS_TWOHAND( "ALWAYS_TWOHAND" );
static const std::string flag_AURA( "AURA" );
static const std::string flag_BELTED( "BELTED" );
static const std::string flag_BIPOD( "BIPOD" );
static const std::string flag_BYPRODUCT( "BYPRODUCT" );
static const std::string flag_CABLE_SPOOL( "CABLE_SPOOL" );
static const std::string flag_CANNIBALISM( "CANNIBALISM" );
static const std::string flag_CBM_SCANNED( "CBM_SCANNED" );
static const std::string flag_CHARGEDIM( "CHARGEDIM" );
static const std::string flag_COLLAPSIBLE_STOCK( "COLLAPSIBLE_STOCK" );
static const std::string flag_CONDUCTIVE( "CONDUCTIVE" );
static const std::string flag_CONSUMABLE( "CONSUMABLE" );
static const std::string flag_CORPSE( "CORPSE" );
static const std::string flag_CROSSBOW( "CROSSBOW" );
static const std::string flag_DANGEROUS( "DANGEROUS" );
static const std::string flag_DEEP_WATER( "DEEP_WATER" );
static const std::string flag_DIAMOND( "DIAMOND" );
static const std::string flag_DISABLE_SIGHTS( "DISABLE_SIGHTS" );
static const std::string flag_ETHEREAL_ITEM( "ETHEREAL_ITEM" );
static const std::string flag_FAKE_MILL( "FAKE_MILL" );
static const std::string flag_FAKE_SMOKE( "FAKE_SMOKE" );
static const std::string flag_FIELD_DRESS( "FIELD_DRESS" );
static const std::string flag_FIELD_DRESS_FAILED( "FIELD_DRESS_FAILED" );
static const std::string flag_FILTHY( "FILTHY" );
static const std::string flag_FIRE_100( "FIRE_100" );
static const std::string flag_FIRE_20( "FIRE_20" );
static const std::string flag_FIRE_50( "FIRE_50" );
static const std::string flag_FIRE_TWOHAND( "FIRE_TWOHAND" );
static const std::string flag_FIT( "FIT" );
static const std::string flag_FLAMMABLE( "FLAMMABLE" );
static const std::string flag_FLAMMABLE_ASH( "FLAMMABLE_ASH" );
static const std::string flag_GIBBED( "GIBBED" );
static const std::string flag_HEATS_FOOD( "HEATS_FOOD" );
static const std::string flag_HELMET_COMPAT( "HELMET_COMPAT" );
static const std::string flag_HIDDEN_HALLU( "HIDDEN_HALLU" );
static const std::string flag_HIDDEN_POISON( "HIDDEN_POISON" );
static const std::string flag_IRREMOVABLE( "IRREMOVABLE" );
static const std::string flag_IS_ARMOR( "IS_ARMOR" );
static const std::string flag_IS_PET_ARMOR( "IS_PET_ARMOR" );
static const std::string flag_IS_UPS( "IS_UPS" );
static const std::string flag_LEAK_ALWAYS( "LEAK_ALWAYS" );
static const std::string flag_LEAK_DAM( "LEAK_DAM" );
static const std::string flag_LIQUID( "LIQUID" );
static const std::string flag_LIQUIDCONT( "LIQUIDCONT" );
static const std::string flag_LITCIG( "LITCIG" );
static const std::string flag_MAG_BELT( "MAG_BELT" );
static const std::string flag_MAG_DESTROY( "MAG_DESTROY" );
static const std::string flag_MAG_EJECT( "MAG_EJECT" );
static const flag_str_id flag_MELEE_GUNMOD( "MELEE_GUNMOD" );
static const std::string flag_NANOFAB_TEMPLATE( "NANOFAB_TEMPLATE" );
static const std::string flag_NEEDS_UNFOLD( "NEEDS_UNFOLD" );
static const std::string flag_NEVER_JAMS( "NEVER_JAMS" );
static const std::string flag_NONCONDUCTIVE( "NONCONDUCTIVE" );
static const std::string flag_NO_DISPLAY( "NO_DISPLAY" );
static const std::string flag_NO_DROP( "NO_DROP" );
static const std::string flag_NO_PARASITES( "NO_PARASITES" );
static const std::string flag_NO_RELOAD( "NO_RELOAD" );
static const std::string flag_NO_REPAIR( "NO_REPAIR" );
static const std::string flag_NO_SALVAGE( "NO_SALVAGE" );
static const std::string flag_NO_UNLOAD( "NO_UNLOAD" );
static const std::string flag_OUTER( "OUTER" );
static const std::string flag_OVERSIZE( "OVERSIZE" );
static const std::string flag_PERSONAL( "PERSONAL" );
static const std::string flag_POWERARMOR_EXO( "POWERARMOR_EXO" );
static const std::string flag_POWERARMOR_EXTERNAL( "POWERARMOR_EXTERNAL" );
static const std::string flag_POWERARMOR_MOD( "POWERARMOR_MOD" );
static const std::string flag_PROCESSING( "PROCESSING" );
static const std::string flag_PROCESSING_RESULT( "PROCESSING_RESULT" );
static const std::string flag_PULPED( "PULPED" );
static const std::string flag_PUMP_ACTION( "PUMP_ACTION" );
static const std::string flag_PUMP_RAIL_COMPATIBLE( "PUMP_RAIL_COMPATIBLE" );
static const std::string flag_QUARTERED( "QUARTERED" );
static const std::string flag_RADIOACTIVE( "RADIOACTIVE" );
static const std::string flag_RADIOSIGNAL_1( "RADIOSIGNAL_1" );
static const std::string flag_RADIOSIGNAL_2( "RADIOSIGNAL_2" );
static const std::string flag_RADIOSIGNAL_3( "RADIOSIGNAL_3" );
static const std::string flag_RADIO_ACTIVATION( "RADIO_ACTIVATION" );
static const std::string flag_RADIO_INVOKE_PROC( "RADIO_INVOKE_PROC" );
static const std::string flag_RADIO_MOD( "RADIO_MOD" );
static const std::string flag_RAIN_PROTECT( "RAIN_PROTECT" );
static const std::string flag_REACH3( "REACH3" );
static const std::string flag_REACH_ATTACK( "REACH_ATTACK" );
static const std::string flag_RECHARGE( "RECHARGE" );
static const std::string flag_REDUCED_BASHING( "REDUCED_BASHING" );
static const std::string flag_REDUCED_WEIGHT( "REDUCED_WEIGHT" );
static const std::string flag_RELOAD_AND_SHOOT( "RELOAD_AND_SHOOT" );
static const std::string flag_RELOAD_EJECT( "RELOAD_EJECT" );
static const std::string flag_RELOAD_ONE( "RELOAD_ONE" );
static const std::string flag_REVIVE_SPECIAL( "REVIVE_SPECIAL" );
static const std::string flag_SILENT( "SILENT" );
static const std::string flag_SKINNED( "SKINNED" );
static const std::string flag_SKINTIGHT( "SKINTIGHT" );
static const std::string flag_SLOW_WIELD( "SLOW_WIELD" );
static const std::string flag_SPEEDLOADER( "SPEEDLOADER" );
static const std::string flag_SPLINT( "SPLINT" );
static const std::string flag_STR_DRAW( "STR_DRAW" );
static const std::string flag_TOBACCO( "TOBACCO" );
static const std::string flag_UNARMED_WEAPON( "UNARMED_WEAPON" );
static const std::string flag_UNDERSIZE( "UNDERSIZE" );
static const std::string flag_USES_BIONIC_POWER( "USES_BIONIC_POWER" );
static const std::string flag_USE_UPS( "USE_UPS" );
static const std::string flag_NAT_UPS( "NAT_UPS" );
static const std::string flag_VARSIZE( "VARSIZE" );
static const std::string flag_VEHICLE( "VEHICLE" );
static const std::string flag_WAIST( "WAIST" );
static const std::string flag_WATERPROOF_GUN( "WATERPROOF_GUN" );
static const std::string flag_WATER_EXTINGUISH( "WATER_EXTINGUISH" );
static const std::string flag_WET( "WET" );
static const std::string flag_WIND_EXTINGUISH( "WIND_EXTINGUISH" );
static const std::string has_thievery_witness( "has_thievery_witness" );
static const activity_id ACT_PICKUP( "ACT_PICKUP" );
static const matec_id rapid_strike( "RAPID" );
class npc_class;
using npc_class_id = string_id<npc_class>;
std::string rad_badge_color( const int rad )
{
using pair_t = std::pair<const int, const translation>;
static const std::array<pair_t, 6> values = {{
pair_t { 0, to_translation( "color", "green" ) },
pair_t { 30, to_translation( "color", "blue" ) },
pair_t { 60, to_translation( "color", "yellow" )},
pair_t {120, to_translation( "color", "orange" )},
pair_t {240, to_translation( "color", "red" ) },
pair_t {500, to_translation( "color", "black" ) },
}
};
for( const auto &i : values ) {
if( rad <= i.first ) {
return i.second.translated();
}
}
return values.back().second.translated();
}
light_emission nolight = {0, 0, 0};
// Returns the default item type, used for the null item (default constructed),
// the returned pointer is always valid, it's never cleared by the @ref Item_factory.
static const itype *nullitem()
{
static itype nullitem_m;
return &nullitem_m;
}
item &null_item_reference()
{
static item result{};
// reset it, in case a previous caller has changed it
result = item();
return result;
}
namespace item_internal
{
bool goes_bad_temp_cache = false;
const item *goes_bad_temp_cache_for = nullptr;
inline bool goes_bad_cache_fetch()
{
return goes_bad_temp_cache;
}
inline void goes_bad_cache_set( const item *i )
{
goes_bad_temp_cache = i->goes_bad();
goes_bad_temp_cache_for = i;
}
inline void goes_bad_cache_unset()
{
goes_bad_temp_cache = false;
goes_bad_temp_cache_for = nullptr;
}
inline bool goes_bad_cache_is_for( const item *i )
{
return goes_bad_temp_cache_for == i;
}
struct scoped_goes_bad_cache {
scoped_goes_bad_cache( item *i ) {
goes_bad_cache_set( i );
}
~scoped_goes_bad_cache() {
goes_bad_cache_unset();
}
};
} // namespace item_internal
const int item::INFINITE_CHARGES = INT_MAX;
item::item() : bday( calendar::start_of_cataclysm )
{
type = nullitem();
charges = 0;
}
item::item( const itype *type, time_point turn, int qty ) : type( type ), bday( turn )
{
corpse = has_flag( flag_CORPSE ) ? &mtype_id::NULL_ID().obj() : nullptr;
item_counter = type->countdown_interval;
if( qty >= 0 ) {
charges = qty;
} else {
if( type->tool && type->tool->rand_charges.size() > 1 ) {
const int charge_roll = rng( 1, type->tool->rand_charges.size() - 1 );
charges = rng( type->tool->rand_charges[charge_roll - 1], type->tool->rand_charges[charge_roll] );
} else {
charges = type->charges_default();
}
}
if( has_flag( flag_NANOFAB_TEMPLATE ) ) {
itype_id nanofab_recipe = item_group::item_from( item_group_id( "nanofab_recipes" ) ).typeId();
set_var( "NANOFAB_ITEM_ID", nanofab_recipe.str() );
}
if( type->gun ) {
for( const itype_id &mod : type->gun->built_in_mods ) {
item it( mod, turn, qty );
it.set_flag( "IRREMOVABLE" );
put_in( it );
}
for( const itype_id &mod : type->gun->default_mods ) {
put_in( item( mod, turn, qty ) );
}
} else if( type->magazine ) {
if( type->magazine->count > 0 ) {
put_in( item( type->magazine->default_ammo, calendar::turn, type->magazine->count ) );
}
} else if( goes_bad() ) {
active = true;
last_rot_check = bday;
} else if( type->tool ) {
if( ammo_remaining() && !ammo_types().empty() ) {
ammo_set( ammo_default(), ammo_remaining() );
}
}
if( ( type->gun || type->tool ) && !magazine_integral() ) {
set_var( "magazine_converted", 1 );
}
if( !type->snippet_category.empty() ) {
snip_id = SNIPPET.random_id_from_category( type->snippet_category );
}
// item always has any relic properties from itype.
if( type->relic_data ) {
relic_data = type->relic_data;
}
}
item::item( const itype_id &id, time_point turn, int qty )
: item( & * id, turn, qty ) {}
item::item( const itype *type, time_point turn, default_charges_tag )
: item( type, turn, type->charges_default() ) {}
item::item( const itype_id &id, time_point turn, default_charges_tag tag )
: item( & * id, turn, tag ) {}
item::item( const itype *type, time_point turn, solitary_tag )
: item( type, turn, type->count_by_charges() ? 1 : -1 ) {}
item::item( const itype_id &id, time_point turn, solitary_tag tag )
: item( & * id, turn, tag ) {}
safe_reference<item> item::get_safe_reference()
{
return anchor.reference_to( this );
}
static const item *get_most_rotten_component( const item &craft )
{
const item *most_rotten = nullptr;
for( const item &it : craft.components ) {
if( it.goes_bad() ) {
if( !most_rotten || it.get_relative_rot() > most_rotten->get_relative_rot() ) {
most_rotten = ⁢
}
}
}
return most_rotten;
}
item::item( const recipe *rec, int qty, std::list<item> items, std::vector<item_comp> selections )
: item( "craft", calendar::turn, qty )
{
craft_data_ = cata::make_value<craft_data>();
craft_data_->making = rec;
components = items;
craft_data_->comps_used = selections;
if( is_food() ) {
active = true;
last_rot_check = bday;
if( goes_bad() ) {
const item *most_rotten = get_most_rotten_component( *this );
if( most_rotten ) {
set_relative_rot( most_rotten->get_relative_rot() );
}
}
}
for( item &component : components ) {
for( const std::string &f : component.item_tags ) {
if( json_flag::get( f ).craft_inherit() ) {
set_flag( f );
}
}
for( const std::string &f : component.type->get_flags() ) {
if( json_flag::get( f ).craft_inherit() ) {
set_flag( f );
}
}
}
// this extra section is so that in-progress crafts will correctly display expected flags.
for( const std::string &flag : rec->flags_to_delete ) {
unset_flag( flag );
}
}
item::item( const item & ) = default;
item::item( item && ) = default;
item::~item() = default;
item &item::operator=( const item & ) = default;
item &item::operator=( item && ) = default;
item item::make_corpse( const mtype_id &mt, time_point turn, const std::string &name,
const int upgrade_time )
{
if( !mt.is_valid() ) {
debugmsg( "tried to make a corpse with an invalid mtype id" );
}
std::string corpse_type = mt == mtype_id::NULL_ID() ? "corpse_generic_human" : "corpse";
item result( corpse_type, turn );
result.corpse = &mt.obj();
if( result.corpse->has_flag( MF_REVIVES ) ) {
if( one_in( 20 ) ) {
result.set_flag( "REVIVE_SPECIAL" );
}
result.set_var( "upgrade_time", std::to_string( upgrade_time ) );
}
// This is unconditional because the const itemructor above sets result.name to
// "human corpse".
result.corpse_name = name;
return result;
}
item &item::convert( const itype_id &new_type )
{
type = &*new_type;
relic_data = type->relic_data;
return *this;
}
item &item::deactivate( const Character *ch, bool alert )
{
if( !active ) {
return *this; // no-op
}
if( is_tool() && type->tool->revert_to ) {
if( ch && alert && !type->tool->revert_msg.empty() ) {
ch->add_msg_if_player( m_info, _( type->tool->revert_msg ), tname() );
}
convert( *type->tool->revert_to );
active = false;
}
return *this;
}
item &item::activate()
{
if( active ) {
return *this; // no-op
}
if( type->countdown_interval > 0 ) {
item_counter = type->countdown_interval;
}
active = true;
return *this;
}
units::energy item::mod_energy( const units::energy &qty )
{
if( !is_battery() ) {
debugmsg( "Tried to set energy of non-battery item" );
return 0_J;
}
units::energy val = energy_remaining() + qty;
if( val < 0_J ) {
return val;
} else if( val > type->battery->max_capacity ) {
energy = type->battery->max_capacity;
} else {
energy = val;
}
return 0_J;
}
item &item::ammo_set( const itype_id &ammo, int qty )
{
if( qty < 0 ) {
// completely fill an integral or existing magazine
if( magazine_integral() || magazine_current() ) {
qty = ammo_capacity();
// else try to add a magazine using default ammo count property if set
} else if( !magazine_default().is_null() ) {
item mag( magazine_default() );
if( mag.type->magazine->count > 0 ) {
qty = mag.type->magazine->count;
} else {
qty = item( magazine_default() ).ammo_capacity();
}
}
}
if( qty <= 0 ) {
ammo_unset();
return *this;
}
// handle reloadable tools and guns with no specific ammo type as special case
if( ( ammo.is_null() && ammo_types().empty() ) || is_money() ) {
if( ( is_tool() || is_gun() ) && magazine_integral() ) {
curammo = nullptr;
charges = std::min( qty, ammo_capacity() );
}
return *this;
}
// check ammo is valid for the item
const itype *atype = &*ammo;
if( !atype->ammo || !ammo_types().count( atype->ammo->type ) ) {
debugmsg( "Tried to set invalid ammo %s[%d] for %s", atype->get_id(), qty, typeId() );
return *this;
}
if( is_magazine() ) {
ammo_unset();
item set_ammo( ammo, calendar::turn, std::min( qty, ammo_capacity() ) );
if( has_flag( flag_NO_UNLOAD ) ) {
set_ammo.set_flag( "NO_DROP" );
set_ammo.set_flag( "IRREMOVABLE" );
}
put_in( set_ammo );
} else if( magazine_integral() ) {
curammo = atype;
charges = std::min( qty, ammo_capacity() );
} else {
if( !magazine_current() ) {
itype_id mag = magazine_default();
if( !mag->magazine ) {
debugmsg( "Tried to set ammo %s[%d] without suitable magazine for %s",
atype->get_id(), qty, typeId() );
return *this;
}
// if default magazine too small fetch instead closest available match
if( mag->magazine->capacity < qty ) {
// as above call to magazine_default successful can infer minimum one option exists
auto iter = type->magazines.find( atype->ammo->type );
if( iter == type->magazines.end() ) {
debugmsg( "%s doesn't have a magazine for %s",
typeId(), ammo );
return *this;
}
std::vector<itype_id> opts( iter->second.begin(), iter->second.end() );
std::sort( opts.begin(), opts.end(), []( const itype_id & lhs, const itype_id & rhs ) {
return lhs->magazine->capacity < rhs->magazine->capacity;
} );
mag = opts.back();
for( const itype_id &e : opts ) {
if( e->magazine->capacity >= qty ) {
mag = e;
break;
}
}
}
put_in( item( mag ) );
}
magazine_current()->ammo_set( ammo, qty );
}
return *this;
}
item &item::ammo_unset()
{
if( !is_tool() && !is_gun() && !is_magazine() ) {
// do nothing
} else if( is_magazine() ) {
contents.clear_items();
} else if( magazine_integral() ) {
curammo = nullptr;
charges = 0;
} else if( magazine_current() ) {
magazine_current()->ammo_unset();
}
return *this;
}
int item::damage() const
{
return damage_;
}
int item::damage_level( int max ) const
{
if( damage_ == 0 || max <= 0 ) {
return 0;
} else if( max_damage() <= 1 ) {
return damage_ > 0 ? max : damage_;
} else if( damage_ < 0 ) {
return -( ( max - 1 ) * ( -damage_ - 1 ) / ( max_damage() - 1 ) + 1 );
} else {
return ( max - 1 ) * ( damage_ - 1 ) / ( max_damage() - 1 ) + 1;
}
}
item &item::set_damage( int qty )
{
on_damage( qty - damage_, DT_TRUE );
damage_ = std::max( std::min( qty, max_damage() ), min_damage() );
return *this;
}
item item::split( int qty )
{
if( !count_by_charges() || qty <= 0 || qty >= charges ) {
return item();
}
item res = *this;
res.charges = qty;
charges -= qty;
return res;
}
bool item::is_null() const
{
// Actually, type should never by null at all.
return ( type == nullptr || type == nullitem() || typeId().is_null() );
}
bool item::is_unarmed_weapon() const
{
return has_flag( flag_UNARMED_WEAPON ) || is_null();
}
bool item::covers( const body_part bp ) const
{
return covers( convert_bp( bp ) );
}
bool item::covers( const bodypart_id &bp ) const
{
return get_covered_body_parts().test( bp->token );
}
body_part_set item::get_covered_body_parts() const
{
return get_covered_body_parts( get_side() );
}
body_part_set item::get_covered_body_parts( const side s ) const
{
body_part_set res;
if( is_gun() ) {
// Currently only used for guns with the should strap mod, other guns might
// go on another bodypart.
res.set( bp_torso );
}
const islot_armor *armor = find_armor_data();
if( armor == nullptr ) {
return res;
}
res |= armor->covers;
if( !armor->sided ) {
return res; // Just ignore the side.
}
switch( s ) {
case side::BOTH:
case side::num_sides:
break;
case side::LEFT:
res.reset( bp_arm_r );
res.reset( bp_hand_r );
res.reset( bp_leg_r );
res.reset( bp_foot_r );
break;
case side::RIGHT:
res.reset( bp_arm_l );
res.reset( bp_hand_l );
res.reset( bp_leg_l );
res.reset( bp_foot_l );
break;
}
return res;
}
bool item::is_sided() const
{
const islot_armor *t = find_armor_data();
return t ? t->sided : false;
}
side item::get_side() const
{
// MSVC complains if directly cast double to enum
return static_cast<side>( static_cast<int>( get_var( "lateral",
static_cast<int>( side::BOTH ) ) ) );
}
bool item::set_side( side s )
{
if( !is_sided() ) {
return false;
}
if( s == side::BOTH ) {
erase_var( "lateral" );
} else {
set_var( "lateral", static_cast<int>( s ) );
}
return true;
}
bool item::swap_side()
{
return set_side( opposite_side( get_side() ) );
}
bool item::is_worn_only_with( const item &it ) const
{
return( ( has_flag( flag_POWERARMOR_EXTERNAL ) || has_flag( flag_POWERARMOR_MOD ) ) &&
it.has_flag( flag_POWERARMOR_EXO ) );
}
item item::in_its_container() const
{
return in_container( type->default_container.value_or( itype_id::NULL_ID() ) );
}
item item::in_container( const itype_id &cont ) const
{
if( !cont.is_null() ) {
item ret( cont, birthday() );
ret.put_in( *this );
if( made_of( LIQUID ) && ret.is_container() ) {
// Note: we can't use any of the normal container functions as they check the
// container being suitable (seals, watertight etc.)
ret.contents.back().charges = charges_per_volume( ret.get_container_capacity() );
}
ret.invlet = invlet;
return ret;
} else {
return *this;
}
}
int item::charges_per_volume( const units::volume &vol ) const
{
if( count_by_charges() ) {
if( type->volume == 0_ml ) {
debugmsg( "Item '%s' with zero volume", tname() );
return INFINITE_CHARGES;
}
// Type cast to prevent integer overflow with large volume containers like the cargo
// dimension
return vol * static_cast<int64_t>( type->stack_size ) / type->volume;
} else {
units::volume my_volume = volume();
if( my_volume == 0_ml ) {
debugmsg( "Item '%s' with zero volume", tname() );
return INFINITE_CHARGES;
}
return vol / my_volume;
}
}
bool item::display_stacked_with( const item &rhs, bool check_components ) const
{
return !count_by_charges() && stacks_with( rhs, check_components );
}
bool item::stacks_with( const item &rhs, bool check_components, bool skip_type_check ) const
{
if( !skip_type_check && type != rhs.type ) {
return false;
}
if( is_relic() && rhs.is_relic() && !( *relic_data == *rhs.relic_data ) ) {
return false;
}
if( charges != 0 && rhs.charges != 0 && is_money() ) {
// Dealing with nonempty cash cards
return true;
}
// This function is also used to test whether items counted by charges should be merged, for that
// check the, the charges must be ignored. In all other cases (tools/guns), the charges are important.
if( !count_by_charges() && charges != rhs.charges ) {
return false;
}
if( is_favorite != rhs.is_favorite ) {
return false;
}
if( damage_ != rhs.damage_ ) {
return false;
}
if( burnt != rhs.burnt ) {
return false;
}
if( active != rhs.active ) {
return false;
}
if( item_tags != rhs.item_tags ) {
return false;
}
if( faults != rhs.faults ) {
return false;
}
if( techniques != rhs.techniques ) {
return false;
}
if( item_vars != rhs.item_vars ) {
return false;
}
if( goes_bad() && rhs.goes_bad() ) {
// Stack items that fall into the same "bucket" of freshness.
// Distant buckets are larger than near ones.
std::pair<int, clipped_unit> my_clipped_time_to_rot =
clipped_time( get_shelf_life() - rot );
std::pair<int, clipped_unit> other_clipped_time_to_rot =
clipped_time( rhs.get_shelf_life() - rhs.rot );
if( my_clipped_time_to_rot != other_clipped_time_to_rot ) {
return false;
}
if( rotten() != rhs.rotten() ) {
// just to be safe that rotten and unrotten food is *never* stacked.
return false;
}
}
if( ( corpse == nullptr && rhs.corpse != nullptr ) ||
( corpse != nullptr && rhs.corpse == nullptr ) ) {
return false;
}
if( corpse != nullptr && rhs.corpse != nullptr && corpse->id != rhs.corpse->id ) {
return false;
}
if( craft_data_ || rhs.craft_data_ ) {
// In-progress crafts are always distinct items. Easier to handle for the player,
// and there shouldn't be that many items of this type around anyway.
return false;
}
if( check_components || is_comestible() || is_craft() ) {
//Only check if at least one item isn't using the default recipe or is comestible
if( !components.empty() || !rhs.components.empty() ) {
if( get_uncraft_components() != rhs.get_uncraft_components() ) {
return false;
}
}
}
if( contents.num_item_stacks() != rhs.contents.num_item_stacks() ) {
return false;
}
if( ammo_current() != rhs.ammo_current() ) {
return false;
}
return contents.stacks_with( rhs.contents );
}
bool item::merge_charges( const item &rhs )
{
if( !count_by_charges() || !stacks_with( rhs ) ) {
return false;
}
// Prevent overflow when either item has "near infinite" charges.
if( charges >= INFINITE_CHARGES / 2 || rhs.charges >= INFINITE_CHARGES / 2 ) {
charges = INFINITE_CHARGES;
return true;
}
// We'll just hope that the item counter represents the same thing for both items
if( item_counter > 0 || rhs.item_counter > 0 ) {
item_counter = ( static_cast<double>( item_counter ) * charges + static_cast<double>
( rhs.item_counter ) * rhs.charges ) / ( charges + rhs.charges );
}
charges += rhs.charges;
return true;
}
void item::put_in( const item &payload )
{
contents.insert_item( payload );
}
void item::set_var( const std::string &name, const int value )