-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
item.cpp
15656 lines (13883 loc) · 571 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 <cctype>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iterator>
#include <limits>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include "ammo.h"
#include "ascii_art.h"
#include "avatar.h"
#include "bionics.h"
#include "bodygraph.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_assert.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "character_id.h"
#include "character_martial_arts.h"
#include "city.h"
#include "clothing_mod.h"
#include "clzones.h"
#include "color.h"
#include "coordinates.h"
#include "craft_command.h"
#include "creature.h"
#include "damage.h"
#include "debug.h"
#include "dispersion.h"
#include "display.h"
#include "effect.h" // for weed_msg
#include "effect_source.h"
#include "enum_traits.h"
#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 "inventory.h"
#include "item_category.h"
#include "item_factory.h"
#include "item_group.h"
#include "item_tname.h"
#include "iteminfo_query.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "line.h"
#include "localized_comparator.h"
#include "magic.h"
#include "magic_enchantment.h"
#include "make_static.h"
#include "map.h"
#include "martialarts.h"
#include "material.h"
#include "messages.h"
#include "mod_manager.h"
#include "monster.h"
#include "mtype.h"
#include "mutation.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "pocket_type.h"
#include "point.h"
#include "proficiency.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.h"
#include "string_id_utils.h"
#include "text_snippets.h"
#include "translations.h"
#include "trap.h"
#include "try_parse_integer.h"
#include "units.h"
#include "units_fwd.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vitamin.h"
#include "veh_type.h"
#include "vpart_position.h"
#include "weather.h"
#include "weather_gen.h"
#include "weather_type.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_IGNITE( "IGNITE" );
static const ammo_effect_str_id ammo_effect_INCENDIARY( "INCENDIARY" );
static const ammo_effect_str_id ammo_effect_MATCHHEAD( "MATCHHEAD" );
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_bolt( "bolt" );
static const ammotype ammo_money( "money" );
static const ammotype ammo_plutonium( "plutonium" );
static const attack_vector_id attack_vector_vector_null( "vector_null" );
static const bionic_id bio_digestion( "bio_digestion" );
static const bodygraph_id bodygraph_full_body_iteminfo( "full_body_iteminfo" );
static const damage_type_id damage_acid( "acid" );
static const damage_type_id damage_bash( "bash" );
static const damage_type_id damage_bullet( "bullet" );
static const damage_type_id damage_cut( "cut" );
static const damage_type_id damage_heat( "heat" );
static const efftype_id effect_bleed( "bleed" );
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_overheat_safety( "fault_overheat_safety" );
static const furn_str_id furn_f_metal_smoking_rack_active( "f_metal_smoking_rack_active" );
static const furn_str_id furn_f_smoking_rack_active( "f_smoking_rack_active" );
static const furn_str_id furn_f_water_mill_active( "f_water_mill_active" );
static const furn_str_id furn_f_wind_mill_active( "f_wind_mill_active" );
static const gun_mode_id gun_mode_REACH( "REACH" );
static const item_category_id item_category_container( "container" );
static const item_category_id item_category_drugs( "drugs" );
static const item_category_id item_category_food( "food" );
static const item_category_id item_category_maps( "maps" );
static const item_category_id item_category_spare_parts( "spare_parts" );
static const item_category_id item_category_tools( "tools" );
static const item_category_id item_category_weapons( "weapons" );
static const itype_id itype_arredondo_chute( "arredondo_chute" );
static const itype_id itype_barrel_small( "barrel_small" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_blood( "blood" );
static const itype_id itype_brass_catcher( "brass_catcher" );
static const itype_id itype_bullet_crossbow( "bullet_crossbow" );
static const itype_id itype_cash_card( "cash_card" );
static const itype_id itype_disassembly( "disassembly" );
static const itype_id itype_hand_crossbow( "hand_crossbow" );
static const itype_id itype_joint_lit( "joint_lit" );
static const itype_id itype_null( "null" );
static const itype_id itype_power_cord( "power_cord" );
static const itype_id itype_rad_badge( "rad_badge" );
static const itype_id itype_rm13_armor( "rm13_armor" );
static const itype_id itype_stock_none( "stock_none" );
static const itype_id itype_tuned_mechanism( "tuned_mechanism" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_clean( "water_clean" );
static const itype_id itype_waterproof_gunmod( "waterproof_gunmod" );
static const json_character_flag json_flag_CANNIBAL( "CANNIBAL" );
static const json_character_flag json_flag_CARNIVORE_DIET( "CARNIVORE_DIET" );
static const json_character_flag json_flag_IMMUNE_SPOIL( "IMMUNE_SPOIL" );
static const json_character_flag json_flag_PSYCHOPATH( "PSYCHOPATH" );
static const json_character_flag json_flag_SAPIOVORE( "SAPIOVORE" );
static const matec_id RAPID( "RAPID" );
static const material_id material_wool( "wool" );
static const mtype_id mon_human( "mon_human" );
static const mtype_id mon_zombie_smoker( "mon_zombie_smoker" );
static const mtype_id mon_zombie_soldier_no_weakpoints( "mon_zombie_soldier_no_weakpoints" );
static const mtype_id mon_zombie_survivor_no_weakpoints( "mon_zombie_survivor_no_weakpoints" );
static const mtype_id pseudo_debug_mon( "pseudo_debug_mon" );
static const quality_id qual_BOIL( "BOIL" );
static const quality_id qual_JACK( "JACK" );
static const quality_id qual_LIFT( "LIFT" );
static const skill_id skill_archery( "archery" );
static const skill_id skill_cooking( "cooking" );
static const skill_id skill_melee( "melee" );
static const skill_id skill_survival( "survival" );
static const skill_id skill_weapon( "weapon" );
static const species_id species_ROBOT( "ROBOT" );
static const sub_bodypart_str_id sub_body_part_torso_lower( "torso_lower" );
static const sub_bodypart_str_id sub_body_part_torso_upper( "torso_upper" );
static const trait_id trait_JITTERY( "JITTERY" );
static const trait_id trait_LIGHTWEIGHT( "LIGHTWEIGHT" );
static const trait_id trait_TOLERANCE( "TOLERANCE" );
static const trait_id trait_WOOLALLERGY( "WOOLALLERGY" );
static const vitamin_id vitamin_human_flesh_vitamin( "human_flesh_vitamin" );
// vitamin flags
static const std::string flag_NO_DISPLAY( "NO_DISPLAY" );
// fault flags
static const std::string flag_BLACKPOWDER_FOULING_DAMAGE( "BLACKPOWDER_FOULING_DAMAGE" );
// item pricing
static const int PRICE_FILTHY_MALUS = 100; // cents
static constexpr float MIN_LINK_EFFICIENCY = 0.001f;
class npc_class;
using npc_class_id = string_id<npc_class>;
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
{
static bool goes_bad_temp_cache = false;
static const item *goes_bad_temp_cache_for = nullptr;
static bool goes_bad_cache_fetch()
{
return goes_bad_temp_cache;
}
static void goes_bad_cache_set( const item *i )
{
goes_bad_temp_cache = i->goes_bad();
goes_bad_temp_cache_for = i;
}
static void goes_bad_cache_unset()
{
goes_bad_temp_cache = false;
goes_bad_temp_cache_for = nullptr;
}
static bool goes_bad_cache_is_for( const item *i )
{
return goes_bad_temp_cache_for == i;
}
struct scoped_goes_bad_cache {
explicit 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;
contents = item_contents( type->pockets );
select_itype_variant();
}
item::item( const itype *type, time_point turn, int qty ) : type( type ), bday( turn )
{
contents = item_contents( type->pockets );
if( type->countdown_interval > 0_seconds ) {
countdown_point = calendar::turn + type->countdown_interval;
}
item_vars = type->item_variables;
update_prefix_suffix_flags();
if( has_flag( flag_CORPSE ) ) {
corpse = &type->source_monster.obj();
if( !type->source_monster.is_null() && !type->source_monster->zombify_into.is_empty() &&
get_option<bool>( "ZOMBIFY_INTO_ENABLED" ) ) {
set_var( "zombie_form", type->source_monster->zombify_into.c_str() );
}
}
if( qty >= 0 ) {
if( type->can_have_charges() ) {
charges = qty;
} else if( qty > 1 ) {
debugmsg( "Tried to set charges for item %s that could not have them!", tname() );
}
} else {
if( type->tool && type->tool->rand_charges.size() > 1 ) {
const int charge_roll = rng( 1, type->tool->rand_charges.size() - 1 );
const int charge_count = rng( type->tool->rand_charges[charge_roll - 1],
type->tool->rand_charges[charge_roll] );
ammo_set( ammo_default(), charge_count );
} else {
charges = type->charges_default();
}
}
if( has_flag( flag_SPAWN_ACTIVE ) ) {
activate();
}
if( has_flag( flag_ENERGY_SHIELD ) ) {
const islot_armor *sh = find_armor_data();
set_var( "MAX_ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
set_var( "ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
}
if( has_flag( flag_COLLAPSE_CONTENTS ) ) {
for( item_pocket *pocket : contents.get_all_standard_pockets() ) {
pocket->settings.set_collapse( true );
}
} else {
auto const mag_filter = []( item_pocket const & pck ) {
return pck.is_type( pocket_type::MAGAZINE ) ||
pck.is_type( pocket_type::MAGAZINE_WELL );
};
for( item_pocket *pocket : contents.get_pockets( mag_filter ) ) {
pocket->settings.set_collapse( true );
}
}
if( has_flag( flag_NANOFAB_TEMPLATE ) ) {
itype_id nanofab_recipe =
item_group::item_from( type->nanofab_template_group ).typeId();
set_var( "NANOFAB_ITEM_ID", nanofab_recipe.str() );
}
select_itype_variant();
if( type->gun ) {
for( const itype_id &mod : type->gun->built_in_mods ) {
item it( mod, turn, qty );
it.set_flag( flag_IRREMOVABLE );
put_in( it, pocket_type::MOD );
}
for( const itype_id &mod : type->gun->default_mods ) {
put_in( item( mod, turn, qty ), pocket_type::MOD );
}
} else if( type->magazine ) {
if( type->magazine->count > 0 ) {
put_in( item( type->magazine->default_ammo, calendar::turn, type->magazine->count ),
pocket_type::MAGAZINE );
}
} else if( has_temperature() ) {
active = true;
last_temp_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 );
}
if( type->expand_snippets ) {
set_var( "description", SNIPPET.expand( type->description.translated() ) );
}
if( has_itype_variant() && itype_variant().expand_snippets ) {
set_var( "description", SNIPPET.expand( variant_description() ) );
}
if( current_phase == phase_id::PNULL ) {
current_phase = type->phase;
}
// 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( find_type( 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( find_type( 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( find_type( 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_components::type_vector_pair &tvp : craft.components ) {
if( !tvp.second.front().goes_bad() ) {
// they're all the same type, so this should be the same for all
continue;
}
for( const item &it : tvp.second ) {
if( !most_rotten || it.get_relative_rot() > most_rotten->get_relative_rot() ) {
most_rotten = ⁢
}
}
}
return most_rotten;
}
// sorts with localized_compare, and enumerates entries, if more than \p max entries
// the rest are abbreviated into " and %d more"
static std::string enumerate_lcsorted_with_limit( const std::vector<std::string> &v,
const int max = 5 )
{
if( max < 1 ) {
debugmsg( "Expected max to be 1 or more" );
return std::string();
}
std::vector<std::string> c( std::min<size_t>( max, v.size() ) );
std::partial_sort_copy( v.begin(), v.end(), c.begin(), c.end(), localized_compare );
const int more = static_cast<int>( v.size() - c.size() );
if( more == 0 ) {
return enumerate_as_string( c );
}
const std::string res = enumerate_as_string( c, enumeration_conjunction::none );
return string_format( max == 1 ? _( "%s and %d more" ) : _( "%s, and %d more" ), res, more );
}
static time_duration get_shortest_lifespan_from_components( const item &craft )
{
const item *shortest_lifespan_component = nullptr;
time_duration shortest_lifespan = 0_turns;
for( const item_components::type_vector_pair &tvp : craft.components ) {
if( !tvp.second.front().goes_bad() ) {
// they're all the same type, so this should be the same for all
continue;
}
for( const item &it : tvp.second ) {
time_duration lifespan = it.get_shelf_life() - it.get_rot();
if( !shortest_lifespan_component || ( lifespan < shortest_lifespan ) ) {
shortest_lifespan_component = ⁢
shortest_lifespan = shortest_lifespan_component->get_shelf_life() -
shortest_lifespan_component->get_rot();
}
}
}
return shortest_lifespan;
}
static bool shelf_life_less_than_each_component( const item &craft )
{
for( const item_components::type_vector_pair &tvp : craft.components ) {
if( !tvp.second.front().goes_bad() || !tvp.second.front().is_comestible() ) {
// they're all the same type, so this should be the same for all
continue;
}
for( const item &it : tvp.second ) {
if( it.get_shelf_life() < craft.get_shelf_life() ) {
return false;
}
}
}
return true;
}
// There are two ways rot is inherited:
// 1) Inherit the remaining lifespan of the component with the lowest remaining lifespan
// 2) Inherit the relative rot of the component with the highest relative rot
//
// Method 1 is used when the result of the recipe has a lower maximum shelf life than all of
// its component's maximum shelf lives. Relative rot is not good to inherit in this case because
// it can make an extremely short resultant remaining lifespan on the product.
//
// Method 2 is used when any component has a longer maximum shelf life than the result does.
// Inheriting the lowest remaining lifespan can not be used in this case because it would break
// food preservation recipes.
static void inherit_rot_from_components( item &it )
{
if( shelf_life_less_than_each_component( it ) ) {
const time_duration shortest_lifespan = get_shortest_lifespan_from_components( it );
if( shortest_lifespan > 0_turns && shortest_lifespan < it.get_shelf_life() ) {
it.set_rot( it.get_shelf_life() - shortest_lifespan );
return;
}
// Fallthrough: shortest_lifespan <= 0_turns (all components are rotten)
}
const item *most_rotten = get_most_rotten_component( it );
if( most_rotten ) {
it.set_relative_rot( most_rotten->get_relative_rot() );
}
}
item::item( const recipe *rec, int qty, item_components items, std::vector<item_comp> selections )
: item( "craft", calendar::turn )
{
craft_data_ = cata::make_value<craft_data>();
craft_data_->making = rec;
craft_data_->disassembly = false;
craft_data_->batch_size = qty;
components = std::move( items );
craft_data_->comps_used = std::move( selections );
if( has_temperature() ) {
active = true;
last_temp_check = bday;
if( goes_bad() ) {
inherit_rot_from_components( *this );
}
}
for( item_components::type_vector_pair &tvp : components ) {
for( item &component : tvp.second ) {
for( const flag_id &f : component.get_flags() ) {
if( f->craft_inherit() ) {
set_flag( f );
}
}
for( const flag_id &f : component.type->get_flags() ) {
if( f->craft_inherit() ) {
set_flag( f );
}
}
}
}
}
item::item( const recipe *rec, int qty, item &component )
: item( "disassembly", calendar::turn )
{
craft_data_ = cata::make_value<craft_data>();
craft_data_->making = rec;
craft_data_->disassembly = true;
craft_data_->batch_size = qty;
item_components items;
items.add( component );
components = items;
if( has_temperature() ) {
active = true;
last_temp_check = bday;
if( goes_bad() ) {
inherit_rot_from_components( *this );
}
}
for( item_components::type_vector_pair &tvp : components ) {
for( item &comp : tvp.second ) {
for( const flag_id &f : comp.get_flags() ) {
if( f->craft_inherit() ) {
set_flag( f );
}
}
for( const flag_id &f : comp.type->get_flags() ) {
if( f->craft_inherit() ) {
set_flag( f );
}
}
}
}
}
item::item( const item & ) = default;
item::item( item && ) noexcept = default;
item::~item() = default;
item &item::operator=( const item & ) = default;
item &item::operator=( item && ) noexcept = 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( mon_flag_REVIVES ) ) {
if( one_in( 20 ) ) {
result.set_flag( flag_REVIVE_SPECIAL );
}
result.set_var( "upgrade_time", std::to_string( upgrade_time ) );
}
if( !mt->zombify_into.is_empty() && get_option<bool>( "ZOMBIFY_INTO_ENABLED" ) ) {
result.set_var( "zombie_form", mt->zombify_into.c_str() );
}
// 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, Character *carrier )
{
// Carry over relative rot similar to crafting
const double rel_rot = get_relative_rot();
type = find_type( new_type );
set_relative_rot( rel_rot );
requires_tags_processing = true; // new type may have "active" flags
item temp( *this );
temp.contents = item_contents( type->pockets );
for( const item *it : contents.mods() ) {
if( !temp.put_in( *it, pocket_type::MOD ).success() ) {
debugmsg( "failed to insert mod" );
}
}
temp.update_modified_pockets();
temp.contents.combine( contents, true );
contents = temp.contents;
current_phase = new_type->phase;
if( count_by_charges() != new_type->count_by_charges() ) {
charges = new_type->charges_default();
}
if( temp.type->countdown_interval > 0_seconds ) {
countdown_point = calendar::turn + type->countdown_interval;
active = true;
}
if( carrier && carrier->has_item( *this ) ) {
carrier->on_item_acquire( *this );
}
item_counter = 0;
update_link_traits();
update_prefix_suffix_flags();
return *this;
}
item &item::deactivate( Character *ch, bool alert )
{
if( !active ) {
return *this; // no-op
}
if( type->revert_to ) {
if( ch && alert && !type->tool->revert_msg.empty() ) {
ch->add_msg_if_player( m_info, type->tool->revert_msg.translated(), tname() );
}
convert( *type->revert_to );
active = false;
if( ch ) {
ch->clear_inventory_search_cache();
}
}
return *this;
}
item &item::activate()
{
if( active ) {
return *this; // no-op
}
active = true;
return *this;
}
bool item::activate_thrown( const tripoint_bub_ms &pos )
{
return type->invoke( nullptr, *this, pos.raw() ).value_or( 0 );
}
units::energy item::mod_energy( const units::energy &qty )
{
if( !is_vehicle_battery() ) {
debugmsg( "Tried to set energy of non-battery item" );
return 0_J;
}
units::energy val = energy_remaining( nullptr ) + 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( !ammo->ammo ) {
if( !has_flag( flag_USES_BIONIC_POWER ) ) {
debugmsg( "can't set ammo %s in %s as it is not an ammo", ammo.c_str(), type_name() );
}
return *this;
}
const ammotype &ammo_type = ammo->ammo->type;
if( qty < 0 ) {
// completely fill an integral or existing magazine
//if( magazine_current() ) then we need capacity of the magazine instead of the item maybe?
if( magazine_integral() || magazine_current() ) {
qty = ammo_capacity( ammo_type );
// else try to add a magazine using default ammo count property if set
} else if( !magazine_default( true ).is_null() ) {
item mag( magazine_default( true ) );
if( mag.type->magazine->count > 0 ) {
qty = mag.type->magazine->count;
} else {
qty = mag.ammo_capacity( ammo_type );
}
}
}
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() ) {
if( magazine_integral() ) {
if( is_tool() ) {
charges = std::min( qty, ammo_capacity( ammo_type ) );
} else if( is_gun() ) {
const item temp_ammo( ammo_default(), calendar::turn, std::min( qty, ammo_capacity( ammo_type ) ) );
put_in( temp_ammo, pocket_type::MAGAZINE );
}
}
return *this;
}
// check ammo is valid for the item
std::set<itype_id> mags = magazine_compatible();
if( ammo_types().count( ammo_type ) == 0 && !( magazine_current() &&
magazine_current()->ammo_types().count( ammo_type ) ) &&
!std::any_of( mags.begin(), mags.end(), [&ammo_type]( const itype_id & mag ) {
return mag->magazine->type.count( ammo_type );
} ) ) {
debugmsg( "Tried to set invalid ammo of %s for %s", ammo.c_str(), typeId().c_str() );
return *this;
}
if( is_magazine() ) {
ammo_unset();
item set_ammo( ammo, calendar::turn, std::min( qty, ammo_capacity( ammo_type ) ) );
if( has_flag( flag_NO_UNLOAD ) ) {
set_ammo.set_flag( flag_NO_DROP );
set_ammo.set_flag( flag_IRREMOVABLE );
}
put_in( set_ammo, pocket_type::MAGAZINE );
} else {
if( !magazine_current() ) {
itype_id mag = magazine_default();
if( !mag->magazine ) {
debugmsg( "Tried to set ammo of %s without suitable magazine for %s",
ammo.c_str(), typeId().c_str() );
return *this;
}
item mag_item( mag );
// if default magazine too small fetch instead closest available match
if( mag_item.ammo_capacity( ammo_type ) < qty ) {
std::vector<item> opts;
for( const itype_id &mag_type : mags ) {
if( mag_type->magazine->type.count( ammo_type ) ) {
opts.emplace_back( mag_type );
}
}
if( opts.empty() ) {
const std::string magazines_str = enumerate_as_string( mags,
[]( const itype_id & mag ) {
const std::string ammotype_str = enumerate_as_string( mag->magazine->type,
[]( const ammotype & a ) {
return a.str();
} );
return string_format( _( "%s (taking %s)" ), mag.str(), ammotype_str );
} );
debugmsg( "Cannot find magazine fitting %s with any capacity for ammo %s "
"(ammotype %s). Magazines considered were %s",
typeId().str(), ammo.str(), ammo_type.str(), magazines_str );
return *this;
}
std::sort( opts.begin(), opts.end(), [&ammo_type]( const item & lhs, const item & rhs ) {
return lhs.ammo_capacity( ammo_type ) < rhs.ammo_capacity( ammo_type );
} );
auto iter = std::find_if( opts.begin(), opts.end(), [&qty, &ammo_type]( const item & mag ) {
return mag.ammo_capacity( ammo_type ) >= qty;
} );
if( iter != opts.end() ) {
mag = iter->typeId();
} else {
mag = opts.back().typeId();
}
}
put_in( item( mag ), pocket_type::MAGAZINE_WELL );
}
item *mag_cur = magazine_current();
if( mag_cur != nullptr ) {
mag_cur->ammo_set( ammo, qty );
}
}
return *this;
}
item &item::ammo_unset()
{
if( !is_tool() && !is_gun() && !is_magazine() ) {
// do nothing
} else if( is_magazine() ) {
if( is_money() ) { // charges are set wrong on cash cards.
charges = 0;
}
contents.clear_magazines();
} else if( magazine_integral() ) {
charges = 0;
if( is_gun() ) {
contents.clear_magazines();
}
} else if( magazine_current() ) {
magazine_current()->ammo_unset();
}
return *this;
}
int item::damage() const
{
return damage_;
}
int item::degradation() const
{
return degradation_;
}
void item::rand_degradation()
{
if( type->degrade_increments() == 0 ) {
return; // likely count_by_charges
}
set_degradation( rng( 0, damage() ) * 50.0f / type->degrade_increments() );
}
int item::damage_level( bool precise ) const
{
return precise ? damage_ : type->damage_level( damage_ );
}
float item::damage_adjusted_melee_weapon_damage( float value ) const
{
if( type->count_by_charges() ) {
return value; // count by charges items don't have partial damage
}
return value * ( 1.0f - 0.1f * std::max( 0, damage_level() - 1 ) );
}
float item::damage_adjusted_gun_damage( float value ) const
{
if( type->count_by_charges() ) {
return value; // count by charges items don't have partial damage
}
return value - 2 * std::max( 0, damage_level() - 1 );
}
float item::damage_adjusted_armor_resist( float value ) const
{
if( type->count_by_charges() ) {
return value; // count by charges items don't have partial damage
}
return value * ( 1.0f - std::max( 0, damage_level() - 1 ) * 0.125f );
}
void item::set_damage( int qty )
{
damage_ = std::clamp( qty, degradation_, max_damage() );
}
void item::set_degradation( int qty )
{
if( type->degrade_increments() > 0 ) {
degradation_ = std::clamp( qty, 0, max_damage() );
} else {
degradation_ = 0;
}
set_damage( damage_ );
}
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_frozen_liquid() const
{
return made_of( phase_id::SOLID ) && made_of_from_type( phase_id::LIQUID );
}
bool item::covers( const sub_bodypart_id &bp, bool check_ablative_armor ) const
{
// if the item has no armor data it doesn't cover that part
const islot_armor *armor = find_armor_data();
if( armor == nullptr ) {
return false;
}
bool has_sub_data = false;
// If the item has no sub location info and covers the part's parent,
// then assume that the item covers that sub body part.
for( const armor_portion_data &data : armor->sub_data ) {
if( !data.sub_coverage.empty() ) {
has_sub_data = true;
}
}
if( !has_sub_data ) {
return covers( bp->parent );
}
bool does_cover = false;
bool subpart_cover = false;
iterate_covered_sub_body_parts_internal( get_side(), [&]( const sub_bodypart_str_id & covered ) {
does_cover = does_cover || bp == covered;
}, check_ablative_armor );
return does_cover || subpart_cover;
}
bool item::covers( const bodypart_id &bp, bool check_ablative_armor ) const
{
bool does_cover = false;
bool subpart_cover = false;
iterate_covered_body_parts_internal( get_side(), [&]( const bodypart_str_id & covered ) {
does_cover = does_cover || bp == covered;
}, check_ablative_armor );
return does_cover || subpart_cover;
}