forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_factory.cpp
4939 lines (4450 loc) · 190 KB
/
item_factory.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_factory.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iterator>
#include <limits>
#include <memory>
#include <new>
#include <stdexcept>
#include <type_traits>
#include <unordered_set>
#include "addiction.h"
#include "ammo.h"
#include "assign.h"
#include "bodypart.h"
#include "cached_options.h"
#include "calendar.h"
#include "cata_assert.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "color.h"
#include "damage.h"
#include "debug.h"
#include "effect_on_condition.h"
#include "enum_conversions.h"
#include "enums.h"
#include "explosion.h"
#include "flag.h"
#include "flat_set.h"
#include "game_constants.h"
#include "generic_factory.h"
#include "init.h"
#include "item.h"
#include "item_contents.h"
#include "item_group.h"
#include "item_pocket.h"
#include "iuse_actor.h"
#include "json.h"
#include "material.h"
#include "optional.h"
#include "options.h"
#include "proficiency.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "relic.h"
#include "requirements.h"
#include "ret_val.h"
#include "stomach.h"
#include "string_formatter.h"
#include "text_snippets.h"
#include "translations.h"
#include "try_parse_integer.h"
#include "ui.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vitamin.h"
struct tripoint;
template <typename T> struct enum_traits;
static const ammotype ammo_NULL( "NULL" );
static const gun_mode_id gun_mode_DEFAULT( "DEFAULT" );
static const gun_mode_id gun_mode_MELEE( "MELEE" );
static const item_category_id item_category_ammo( "ammo" );
static const item_category_id item_category_bionics( "bionics" );
static const item_category_id item_category_books( "books" );
static const item_category_id item_category_clothing( "clothing" );
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_guns( "guns" );
static const item_category_id item_category_magazines( "magazines" );
static const item_category_id item_category_mods( "mods" );
static const item_category_id item_category_other( "other" );
static const item_category_id item_category_tools( "tools" );
static const item_category_id item_category_veh_parts( "veh_parts" );
static const item_category_id item_category_weapons( "weapons" );
static const item_group_id Item_spawn_data_EMPTY_GROUP( "EMPTY_GROUP" );
static const material_id material_bean( "bean" );
static const material_id material_egg( "egg" );
static const material_id material_flesh( "flesh" );
static const material_id material_fruit( "fruit" );
static const material_id material_garlic( "garlic" );
static const material_id material_hflesh( "hflesh" );
static const material_id material_honey( "honey" );
static const material_id material_hydrocarbons( "hydrocarbons" );
static const material_id material_iflesh( "iflesh" );
static const material_id material_junk( "junk" );
static const material_id material_milk( "milk" );
static const material_id material_mushroom( "mushroom" );
static const material_id material_nut( "nut" );
static const material_id material_oil( "oil" );
static const material_id material_tomato( "tomato" );
static const material_id material_veggy( "veggy" );
static const material_id material_wheat( "wheat" );
static const material_id material_wool( "wool" );
static const skill_id skill_pistol( "pistol" );
static const skill_id skill_rifle( "rifle" );
static const skill_id skill_shotgun( "shotgun" );
static const skill_id skill_smg( "smg" );
static item_blacklist_t item_blacklist;
static DynamicDataLoader::deferred_json deferred;
std::unique_ptr<Item_factory> item_controller = std::make_unique<Item_factory>();
/** @relates string_id */
template<>
const itype &string_id<itype>::obj() const
{
const itype *result = item_controller->find_template( *this );
static const itype dummy{};
return result ? *result : dummy;
}
/** @relates string_id */
template<>
bool string_id<itype>::is_valid() const
{
return item_controller->has_template( *this );
}
/** @relates string_id */
template<>
bool string_id<Item_spawn_data>::is_valid() const
{
return item_controller->get_group( *this ) != nullptr;
}
static item_category_id calc_category( const itype &obj );
static void hflesh_to_flesh( itype &item_template );
bool item_is_blacklisted( const itype_id &id )
{
return item_blacklist.blacklist.count( id );
}
static void assign( const JsonObject &jo, const std::string &name,
std::map<gun_mode_id, gun_modifier_data> &mods )
{
if( !jo.has_array( name ) ) {
return;
}
mods.clear();
for( JsonArray curr : jo.get_array( name ) ) {
translation text;
curr.read( 1, text );
mods.emplace( gun_mode_id( curr.get_string( 0 ) ), gun_modifier_data( text,
curr.get_int( 2 ), curr.size() >= 4 ? curr.get_tags( 3 ) : std::set<std::string>() ) );
}
}
static bool assign_coverage_from_json( const JsonObject &jo, const std::string &key,
body_part_set &parts )
{
auto parse = [&parts]( const std::string & val ) {
parts.set( bodypart_str_id( val ) );
};
if( jo.has_array( key ) ) {
for( const std::string line : jo.get_array( key ) ) {
parse( line );
}
return true;
} else if( jo.has_string( key ) ) {
parse( jo.get_string( key ) );
return true;
} else {
return false;
}
}
static bool assign_coverage_from_json( const JsonObject &jo, const std::string &key,
cata::optional<body_part_set> &parts )
{
body_part_set temp;
if( assign_coverage_from_json( jo, key, temp ) ) {
parts = temp;
return true;
}
return false;
}
static bool is_physical( const itype &type )
{
return !type.has_flag( flag_AURA ) &&
!type.has_flag( flag_CORPSE ) &&
!type.has_flag( flag_IRREMOVABLE ) &&
!type.has_flag( flag_NO_DROP ) &&
!type.has_flag( flag_NO_UNWIELD ) &&
!type.has_flag( flag_PERSONAL ) &&
!type.has_flag( flag_PSEUDO ) &&
!type.has_flag( flag_ZERO_WEIGHT );
}
void Item_factory::finalize_pre( itype &obj )
{
// TODO: separate repairing from reinforcing/enhancement
if( obj.damage_max() == obj.damage_min() ) {
obj.item_tags.insert( flag_NO_REPAIR );
}
if( obj.has_flag( flag_STAB ) || obj.has_flag( flag_SPEAR ) ) {
std::swap( obj.melee[static_cast<int>( damage_type::CUT )],
obj.melee[static_cast<int>( damage_type::STAB )] );
}
// add usage methods (with default values) based upon qualities
// if a method was already set the specific values remain unchanged
for( const auto &q : obj.qualities ) {
for( const auto &u : q.first.obj().usages ) {
if( q.second >= u.first ) {
emplace_usage( obj.use_methods, u.second );
// As far as I know all the actions provided by quality level do not consume ammo
// So it is safe to set all to 0
// To do: read the json file of this item again and get for each quality a scale number
obj.ammo_scale.emplace( u.second, 0.0f );
}
}
}
for( const auto &q : obj.charged_qualities ) {
for( const auto &u : q.first.obj().usages ) {
if( q.second >= u.first ) {
emplace_usage( obj.use_methods, u.second );
// I do not know how to get the ammo scale, so hopefully it naturally comes with the item's scale?
}
}
}
if( obj.mod ) {
std::string func = obj.gunmod ? "GUNMOD_ATTACH" : "TOOLMOD_ATTACH";
emplace_usage( obj.use_methods, func );
obj.ammo_scale.emplace( func, 0.0f );
} else if( obj.gun ) {
const std::string func = "detach_gunmods";
emplace_usage( obj.use_methods, func );
obj.ammo_scale.emplace( func, 0.0f );
const std::string func2 = "modify_gunmods";
emplace_usage( obj.use_methods, func2 );
obj.ammo_scale.emplace( func2, 0.0f );
}
if( get_option<bool>( "NO_FAULTS" ) ) {
obj.faults.clear();
}
// If no category was forced via JSON automatically calculate one now
if( !obj.category_force.is_valid() || obj.category_force.is_empty() ) {
obj.category_force = calc_category( obj );
}
// use pre-Cataclysm price as default if post-cataclysm price unspecified
if( obj.price_post < 0_cent ) {
obj.price_post = obj.price;
}
// use base volume if integral volume unspecified
if( obj.integral_volume < 0_ml ) {
obj.integral_volume = obj.volume;
}
// use base weight if integral weight unspecified
if( obj.integral_weight < 0_gram ) {
obj.integral_weight = obj.weight;
}
// for ammo and comestibles stack size defaults to count of initial charges
// Set max stack size to 200 to prevent integer overflow
if( obj.count_by_charges() ) {
if( obj.stack_size == 0 ) {
obj.stack_size = obj.charges_default();
} else if( obj.stack_size > 200 ) {
debugmsg( obj.id.str() + " stack size is too large, reducing to 200" );
obj.stack_size = 200;
}
}
// Items always should have some volume.
// TODO: handle possible exception software?
if( obj.volume <= 0_ml ) {
if( is_physical( obj ) ) {
debugmsg( "item %s has zero volume (if zero volume is intentional "
"you can suppress this error with the ZERO_WEIGHT "
"flag)\n", obj.id.str() );
}
obj.volume = units::from_milliliter( 1 );
}
// set light_emission based on LIGHT_[X] flag
for( const auto &f : obj.item_tags ) {
if( string_starts_with( f.str(), "LIGHT_" ) ) {
ret_val<int> ll = try_parse_integer<int>( f.str().substr( 6 ), false );
if( ll.success() ) {
if( ll.value() > 0 ) {
obj.light_emission = ll.value();
} else {
debugmsg( "item %s specifies light emission of zero, which is redundant",
obj.id.str() );
}
} else {
debugmsg( "error parsing integer light emission suffic for item %s: %s",
obj.id.str(), ll.str() );
}
}
}
// remove LIGHT_[X] flags
erase_if( obj.item_tags, []( const flag_id & f ) {
return string_starts_with( f.str(), "LIGHT_" );
} );
// for ammo not specifying loudness (or an explicit zero) derive value from other properties
if( obj.ammo ) {
if( obj.ammo->loudness < 0 ) {
obj.ammo->loudness = obj.ammo->range * 2;
for( const damage_unit &du : obj.ammo->damage ) {
obj.ammo->loudness += ( du.amount + du.res_pen ) * 2;
}
}
const auto &mats = obj.materials;
if( mats.find( material_hydrocarbons ) == mats.end() &&
mats.find( material_oil ) == mats.end() ) {
const auto &ammo_effects = obj.ammo->ammo_effects;
obj.ammo->cookoff = ammo_effects.count( "INCENDIARY" ) > 0 ||
ammo_effects.count( "COOKOFF" ) > 0;
static const std::set<std::string> special_cookoff_tags = {{
"NAPALM", "NAPALM_BIG",
"EXPLOSIVE_SMALL", "EXPLOSIVE", "EXPLOSIVE_BIG", "EXPLOSIVE_HUGE",
"TOXICGAS", "SMOKE", "SMOKE_BIG",
"FRAG", "FLASHBANG"
}
};
obj.ammo->special_cookoff = std::any_of( ammo_effects.begin(), ammo_effects.end(),
[]( const std::string & s ) {
return special_cookoff_tags.count( s ) > 0;
} );
} else {
obj.ammo->cookoff = false;
obj.ammo->special_cookoff = false;
}
// Special casing for shot, since the damage per pellet can be tiny.
// Instead of handling fractional damage values, we scale the effective number
// of projectiles based on the damage so that they end up at 1.
if( obj.ammo->count > 1 && obj.ammo->shot_damage.total_damage() < 1.0f ) {
// Patch to fixup shot without shot_damage until I get all the definitions consistent.
if( obj.ammo->shot_damage.damage_units.empty() ) {
obj.ammo->shot_damage.damage_units.emplace_back( damage_type::BULLET, 0.1f );
}
obj.ammo->count = obj.ammo->count * obj.ammo->shot_damage.total_damage();
obj.ammo->shot_damage.damage_units.front().amount = 1.0f;
}
}
// Helper for ammo migration in following sections
auto migrate_ammo_set = [this]( std::set<ammotype> &ammoset ) {
for( auto ammo_type_it = ammoset.begin(); ammo_type_it != ammoset.end(); ) {
const itype_id default_ammo_type = ammo_type_it->obj().default_ammotype();
auto maybe_migrated = migrated_ammo.find( default_ammo_type );
if( maybe_migrated != migrated_ammo.end() ) {
ammo_type_it = ammoset.erase( ammo_type_it );
ammoset.insert( maybe_migrated->second );
} else {
++ammo_type_it;
}
}
};
auto migrate_ammo_map = [this]( std::map<ammotype, int> &ammomap ) {
for( auto ammo_type_it = ammomap.begin(); ammo_type_it != ammomap.end(); ) {
const itype_id default_ammo_type = ammo_type_it->first.obj().default_ammotype();
auto maybe_migrated = migrated_ammo.find( default_ammo_type );
if( maybe_migrated != migrated_ammo.end() ) {
int capacity = ammo_type_it->second;
ammo_type_it = ammomap.erase( ammo_type_it );
ammomap.emplace( maybe_migrated->second, capacity );
} else {
++ammo_type_it;
}
}
};
if( obj.magazine ) {
// ensure default_ammo is set
if( obj.magazine->default_ammo.is_null() ) {
obj.magazine->default_ammo = ammotype( *obj.magazine->type.begin() )->default_ammotype();
}
// If the magazine has ammo types for which the default ammo has been migrated, we need to
// replace those ammo types with that of the migrated ammo
migrate_ammo_set( obj.magazine->type );
// ensure default_ammo is migrated if need be
auto maybe_migrated = migrated_ammo.find( obj.magazine->default_ammo );
if( maybe_migrated != migrated_ammo.end() ) {
obj.magazine->default_ammo = maybe_migrated->second.obj().default_ammotype();
}
for( pocket_data &magazine : obj.pockets ) {
if( magazine.type != item_pocket::pocket_type::MAGAZINE ) {
continue;
}
migrate_ammo_map( magazine.ammo_restriction );
}
}
// Migrate compatible magazines
for( auto kv : obj.magazines ) {
for( auto mag_it = kv.second.begin(); mag_it != kv.second.end(); ) {
auto maybe_migrated = migrated_magazines.find( *mag_it );
if( maybe_migrated != migrated_magazines.end() ) {
mag_it = kv.second.erase( mag_it );
kv.second.insert( kv.second.begin(), maybe_migrated->second );
} else {
++mag_it;
}
}
}
// Migrate default magazines
for( auto kv : obj.magazine_default ) {
auto maybe_migrated = migrated_magazines.find( kv.second );
if( maybe_migrated != migrated_magazines.end() ) {
kv.second = maybe_migrated->second;
}
}
if( obj.mod ) {
// Migrate acceptable ammo and ammo modifiers
migrate_ammo_set( obj.mod->acceptable_ammo );
migrate_ammo_set( obj.mod->ammo_modifier );
for( auto kv = obj.mod->magazine_adaptor.begin(); kv != obj.mod->magazine_adaptor.end(); ) {
auto maybe_migrated = migrated_ammo.find( kv->first.obj().default_ammotype() );
if( maybe_migrated != migrated_ammo.end() ) {
for( const itype_id &compatible_mag : kv->second ) {
obj.mod->magazine_adaptor[maybe_migrated->second].insert( compatible_mag );
}
kv = obj.mod->magazine_adaptor.erase( kv );
} else {
++kv;
}
}
}
if( obj.gun ) {
// If the gun has ammo types for which the default ammo has been migrated, we need to
// replace those ammo types with that of the migrated ammo
for( auto ammo_type_it = obj.gun->ammo.begin(); ammo_type_it != obj.gun->ammo.end(); ) {
auto maybe_migrated = migrated_ammo.find( ammo_type_it->obj().default_ammotype() );
if( maybe_migrated != migrated_ammo.end() ) {
const ammotype old_ammo = *ammo_type_it;
// Remove the old ammotype add the migrated version
ammo_type_it = obj.gun->ammo.erase( ammo_type_it );
const ammotype &new_ammo = maybe_migrated->second;
obj.gun->ammo.insert( obj.gun->ammo.begin(), new_ammo );
// Migrate the compatible magazines
auto old_mag_it = obj.magazines.find( old_ammo );
if( old_mag_it != obj.magazines.end() ) {
for( const itype_id &old_mag : old_mag_it->second ) {
obj.magazines[new_ammo].insert( old_mag );
}
obj.magazines.erase( old_ammo );
}
// And the default magazines for each magazine type
auto old_default_mag_it = obj.magazine_default.find( old_ammo );
if( old_default_mag_it != obj.magazine_default.end() ) {
const itype_id &old_default_mag = old_default_mag_it->second;
obj.magazine_default[new_ammo] = old_default_mag;
obj.magazine_default.erase( old_ammo );
}
} else {
++ammo_type_it;
}
}
for( pocket_data &magazine : obj.pockets ) {
if( magazine.type != item_pocket::pocket_type::MAGAZINE ) {
continue;
}
migrate_ammo_map( magazine.ammo_restriction );
}
// TODO: add explicit action field to gun definitions
const auto defmode_name = [&]() {
if( obj.gun->clip == 1 ) {
return to_translation( "manual" ); // break-type actions
} else if( obj.gun->skill_used == skill_pistol && obj.has_flag( flag_RELOAD_ONE ) ) {
return to_translation( "revolver" );
} else {
return to_translation( "semi-auto" );
}
};
// if the gun doesn't have a DEFAULT mode then add one now
obj.gun->modes.emplace( gun_mode_DEFAULT,
gun_modifier_data( defmode_name(), 1, std::set<std::string>() ) );
// If a "gun" has a reach attack, give it an additional melee mode.
if( obj.has_flag( flag_REACH_ATTACK ) ) {
obj.gun->modes.emplace( gun_mode_MELEE,
gun_modifier_data( to_translation( "melee" ), 1,
{ "MELEE" } ) );
}
if( obj.gun->handling < 0 ) {
// TODO: specify in JSON via classes
if( obj.gun->skill_used == skill_rifle ||
obj.gun->skill_used == skill_smg ||
obj.gun->skill_used == skill_shotgun ) {
obj.gun->handling = 20;
} else {
obj.gun->handling = 10;
}
}
}
set_allergy_flags( obj );
hflesh_to_flesh( obj );
npc_implied_flags( obj );
if( obj.comestible ) {
std::map<vitamin_id, int> &vitamins = obj.comestible->default_nutrition.vitamins;
if( get_option<bool>( "NO_VITAMINS" ) ) {
for( auto &vit : vitamins ) {
if( vit.first->type() == vitamin_type::VITAMIN ) {
vit.second = 0;
}
}
} else if( vitamins.empty() && obj.comestible->healthy >= 0 ) {
// Default vitamins of healthy comestibles to their edible base materials if none explicitly specified.
int healthy = std::max( obj.comestible->healthy, 1 ) * 10;
auto mat = obj.materials;
// TODO: migrate inedible comestibles to appropriate alternative types.
for( auto m = mat.begin(); m != mat.end(); ) {
if( !m->first->edible() ) {
m = mat.erase( m );
} else {
m++;
}
}
// For comestibles composed of multiple edible materials we calculate the average.
for( const auto &v : vitamin::all() ) {
if( !vitamins.count( v.first ) ) {
for( const auto &m : mat ) {
double amount = m.first->vitamin( v.first ) * healthy / mat.size();
vitamins[v.first] += std::ceil( amount );
}
}
}
}
}
if( obj.tool ) {
if( !obj.tool->subtype.is_empty() && has_template( obj.tool->subtype ) ) {
tool_subtypes[ obj.tool->subtype ].insert( obj.id );
}
}
for( auto &e : obj.use_methods ) {
e.second.get_actor_ptr()->finalize( obj.id );
}
if( obj.drop_action.get_actor_ptr() != nullptr ) {
obj.drop_action.get_actor_ptr()->finalize( obj.id );
}
if( obj.can_use( "MA_MANUAL" ) && obj.book && obj.book->martial_art.is_null() &&
string_starts_with( obj.get_id().str(), "manual_" ) ) {
// HACK: Legacy martial arts books rely on a hack whereby the name of the
// martial art is derived from the item id
obj.book->martial_art = matype_id( "style_" + obj.get_id().str().substr( 7 ) );
}
if( obj.longest_side == -1_mm ) {
units::volume effective_volume = obj.count_by_charges() ?
( obj.volume / obj.stack_size ) : obj.volume;
obj.longest_side = units::default_length_from_volume<int>( effective_volume );
}
}
void Item_factory::register_cached_uses( const itype &obj )
{
for( const auto &e : obj.use_methods ) {
// can this item function as a repair tool?
if( repair_actions.count( e.first ) ) {
repair_tools.insert( obj.id );
}
// can this item be used to repair complex firearms?
if( e.first == "GUN_REPAIR" ) {
gun_tools.insert( obj.id );
}
}
}
void Item_factory::finalize_post( itype &obj )
{
erase_if( obj.item_tags, [&]( const flag_id & f ) {
if( !f.is_valid() ) {
debugmsg( "itype '%s' uses undefined flag '%s'. Please add corresponding 'json_flag' entry to json.",
obj.id.str(), f.str() );
return true;
}
return false;
} );
// handle complex firearms as a special case
if( obj.gun && !obj.has_flag( flag_PRIMITIVE_RANGED_WEAPON ) ) {
std::copy( gun_tools.begin(), gun_tools.end(), std::inserter( obj.repair, obj.repair.begin() ) );
return;
}
// for each item iterate through potential repair tools
for( const auto &tool : repair_tools ) {
// check if item can be repaired with any of the actions?
for( const auto &act : repair_actions ) {
const use_function *func = m_templates[tool].get_use( act );
if( func == nullptr ) {
continue;
}
// tool has a possible repair action, check if the materials are compatible
const auto &opts = dynamic_cast<const repair_item_actor *>( func->get_actor_ptr() )->materials;
if( std::any_of( obj.materials.begin(),
obj.materials.end(), [&opts]( const std::pair<material_id, int> &m ) {
return opts.count( m.first ) > 0;
} ) ) {
obj.repair.insert( tool );
}
}
}
// go through each pocket and assign the name as a fallback definition
for( pocket_data &pocket : obj.pockets ) {
pocket.name = obj.name;
}
if( obj.armor ) {
// if this armor doesn't have material info should try to populate it with base item materials
for( armor_portion_data &data : obj.armor->sub_data ) {
if( data.materials.empty() ) {
// if no portion info defined skip scaling for portions
bool skip_scale = obj.mat_portion_total == 0;
for( const auto &m : obj.materials ) {
if( skip_scale ) {
part_material pm( m.first, 100, obj.materials.size() * data.avg_thickness );
// need to ignore sheet thickness since inferred thicknesses are not gonna be perfect
pm.ignore_sheet_thickness = true;
data.materials.push_back( pm );
} else {
part_material pm( m.first, 100,
static_cast<float>( m.second ) / static_cast<float>( obj.mat_portion_total ) * data.avg_thickness );
// need to ignore sheet thickness since inferred thicknesses are not gonna be perfect
pm.ignore_sheet_thickness = true;
data.materials.push_back( pm );
}
}
}
}
// cache some values and entries before consolidating info per limb
for( armor_portion_data &data : obj.armor->sub_data ) {
// Setting max_encumber must be in finalize_post because it relies on
// stack_size being set for all ammo, which happens in finalize_pre.
if( data.max_encumber == -1 ) {
units::volume total_nonrigid_volume = 0_ml;
for( const pocket_data &pocket : obj.pockets ) {
if( !pocket.rigid ) {
// include the modifier for each individual pocket
total_nonrigid_volume += pocket.max_contains_volume() * pocket.volume_encumber_modifier;
}
}
data.max_encumber = data.encumber + total_nonrigid_volume * data.volume_encumber_modifier /
armor_portion_data::volume_per_encumbrance;
}
// Precalc average thickness per portion
int data_count = 0;
float thic_acc = 0.0f;
for( part_material &m : data.materials ) {
thic_acc += m.thickness * m.cover / 100.0f;
data_count++;
}
if( data_count > 0 && thic_acc > std::numeric_limits<float>::epsilon() ) {
data.avg_thickness = thic_acc;
}
// Precalc hardness and comfort for these parts of the armor
for( const part_material &m : data.materials ) {
if( m.cover > islot_armor::test_threshold ) {
if( m.id->soft() ) {
data.comfortable = true;
} else {
data.rigid = true;
}
}
}
}
for( auto itt = obj.armor->sub_data.begin(); itt != obj.armor->sub_data.end(); ++itt ) {
// using empty to signify it has already been consolidated
if( !itt->sub_coverage.empty() ) {
//check if any further entries should be combined with this one
for( auto comp_itt = std::next( itt ); comp_itt != obj.armor->sub_data.end(); ++comp_itt ) {
if( armor_portion_data::should_consolidate( *itt, *comp_itt ) ) {
// they are the same so add the covers and sub covers to the original and then clear them from the other
itt->covers->unify_set( comp_itt->covers.value() );
itt->sub_coverage.insert( comp_itt->sub_coverage.begin(), comp_itt->sub_coverage.end() );
comp_itt->covers->clear();
comp_itt->sub_coverage.clear();
}
}
}
}
//remove any now empty entries
auto remove_itt = std::remove_if( obj.armor->sub_data.begin(),
obj.armor->sub_data.end(), [&]( const armor_portion_data & data ) {
return data.sub_coverage.empty() && data.covers.value().none();
} );
obj.armor->sub_data.erase( remove_itt, obj.armor->sub_data.end() );
// now consolidate all the loaded sub_data to one entry per body part
for( const armor_portion_data &sub_armor : obj.armor->sub_data ) {
// for each body part this covers we need to add to the overall data for that bp
if( sub_armor.covers.has_value() ) {
for( const bodypart_str_id &bp : sub_armor.covers.value() ) {
bool found = false;
// go through and find if the body part already exists
for( armor_portion_data &it : obj.armor->data ) {
// if it contains the body part update the values with data from this
//body_part_set set = it.covers.value();
if( it.covers->test( bp ) ) {
found = true;
// modify the values with additional info
it.encumber += sub_armor.encumber;
it.max_encumber += sub_armor.max_encumber;
for( const encumbrance_modifier &en : sub_armor.encumber_modifiers ) {
it.encumber_modifiers.push_back( en );
}
// get the amount of the limb that is covered with sublocations
// for overall coverage we need to scale coverage by that
float scale = sub_armor.max_coverage( bp ) / 100.0;
float it_scale = it.max_coverage( bp ) / 100.0;
it.coverage += sub_armor.coverage * scale;
it.cover_melee += sub_armor.cover_melee * scale;
it.cover_ranged += sub_armor.cover_ranged * scale;
it.cover_vitals += sub_armor.cover_vitals;
// these values need to be averaged based on proportion covered
it.avg_thickness = ( sub_armor.avg_thickness * scale + it.avg_thickness * it_scale ) /
( scale + it_scale );
it.env_resist = ( sub_armor.env_resist * scale + it.env_resist * it_scale ) /
( scale + it_scale );
it.env_resist_w_filter = ( sub_armor.env_resist_w_filter * scale + it.env_resist_w_filter *
it_scale ) / ( scale + it_scale );
// add layers that are covered by sublimbs
for( const layer_level &ll : sub_armor.layers ) {
it.layers.insert( ll );
}
// if you are trying to add a new data entry and either the original data
// or the new data has an empty sublocations list then say that you are
// redefining a limb
if( it.sub_coverage.empty() || sub_armor.sub_coverage.empty() ) {
debugmsg( "item %s has multiple entries for %s.",
obj.id.str(), bp.str() );
}
// go through the materials list and update data
for( const part_material &new_mat : sub_armor.materials ) {
bool mat_found = false;
for( part_material &old_mat : it.materials ) {
if( old_mat.id == new_mat.id ) {
mat_found = true;
// values should be averaged
float max_coverage_new = sub_armor.max_coverage( bp );
float max_coverage_mats = it.max_coverage( bp );
// the percent of the coverable bits that this armor does cover
float coverage_multiplier = sub_armor.coverage * max_coverage_new / 100.0f;
// portion should be handled as the portion scaled by relative coverage
old_mat.cover = old_mat.cover + static_cast<float>( new_mat.cover ) * coverage_multiplier / 100.0f;
// with the max values we can get the weight that each should have
old_mat.thickness = ( max_coverage_new * new_mat.thickness + max_coverage_mats *
old_mat.thickness ) / ( max_coverage_mats + max_coverage_new );
}
}
// if we didn't find an entry for this material
// create new entry with a scaled material coverage
if( !mat_found ) {
float max_coverage_new = sub_armor.max_coverage( bp );
// the percent of the coverable bits that this armor does cover
float coverage_multiplier = sub_armor.coverage * max_coverage_new / 100.0f;
part_material modified_mat = new_mat;
// if for example your elbow was covered in plastic but none of the rest of the arm
// this should be represented correctly in the UI with the covers for plastic being 5%
// of the arm. Similarily 50% covered in plastic covering only 30% of the arm should lead to
// 15% covered for the arm overall
modified_mat.cover = static_cast<float>( new_mat.cover ) * coverage_multiplier / 100.0f;
it.materials.push_back( modified_mat );
}
}
// add additional sub coverage locations to the original list
for( const sub_bodypart_str_id &sbp : sub_armor.sub_coverage ) {
it.sub_coverage.insert( sbp );
}
}
}
// if not found create a new bp entry
if( !found ) {
// copy values to data but only have one limb
armor_portion_data new_limb = sub_armor;
new_limb.covers->clear();
new_limb.covers->set( bp );
// get the amount of the limb that is covered with sublocations
// for overall coverage we need to scale coverage by that
float scale = new_limb.max_coverage( bp ) / 100.0;
new_limb.coverage = new_limb.coverage * scale;
new_limb.cover_melee = new_limb.cover_melee * scale;
new_limb.cover_ranged = new_limb.cover_ranged * scale;
// need to scale each material coverage the same way since they will after this be
// scaled back up at the end of the amalgamation
for( part_material &mat : new_limb.materials ) {
mat.cover = static_cast<float>( mat.cover ) * new_limb.coverage / 100.0f;
}
obj.armor->data.push_back( new_limb );
}
}
}
}
// calculate encumbrance data per limb if done by description
for( armor_portion_data &data : obj.armor->data ) {
if( !data.encumber_modifiers.empty() ) {
// we know that the data entry covers a single bp
data.encumber = data.calc_encumbrance( obj.weight, *data.covers.value().begin() );
// need to account for varsize stuff here and double encumbrance if so
if( obj.has_flag( flag_VARSIZE ) ) {
data.encumber = std::min( data.encumber * 2, data.encumber + 10 );
}
// Recalc max encumber as well
units::volume total_nonrigid_volume = 0_ml;
for( const pocket_data &pocket : obj.pockets ) {
if( !pocket.rigid ) {
// include the modifier for each individual pocket
total_nonrigid_volume += pocket.max_contains_volume() * pocket.volume_encumber_modifier;
}
}
data.max_encumber = data.encumber + total_nonrigid_volume * data.volume_encumber_modifier /
armor_portion_data::volume_per_encumbrance;
}
}
// need to scale amalgamized portion data based on total coverage.
// 3% of 48% needs to be scaled to 6% of 100%
for( armor_portion_data &it : obj.armor->data ) {
for( part_material &mat : it.materials ) {
// scale the value of portion covered based on how much total is covered
// if you proportionally only cover 5% of the arm but overall cover 50%
// you actually proportionally cover 10% of the armor
// in case of 0 coverage just say the mats cover it all
if( it.coverage == 0 ) {
mat.cover = 100;
} else {
mat.cover = std::round( static_cast<float>( mat.cover ) / ( static_cast<float>
( it.coverage ) / 100.0f ) );
}
}
}
// store a shorthand var for if the item has notable sub coverage data
for( const armor_portion_data &armor_data : obj.armor->data ) {
if( obj.armor->has_sub_coverage ) {
// if we already know it has subcoverage break from the loop
break;
}
// if the item covers everything it doesn't have specific sub coverage
// so don't need to display it in the UI and do tests for it
if( !armor_data.sub_coverage.empty() ) {
// go through and see if we are missing any sublocations
// if we are missing some then the item does cover specific locations
// this flag is mostly used to skip itterating and testing
// if UI should be displayed
// each armor data entry covers exactly 1 body part
for( const sub_bodypart_str_id &compare_sbp : armor_data.covers->begin()->obj().sub_parts ) {
if( compare_sbp->secondary ) {
// don't care about secondary locations
continue;
}
bool found = false;
for( const sub_bodypart_str_id &sbp : armor_data.sub_coverage ) {
if( compare_sbp == sbp ) {
found = true;
}
}
// if an entry is not found we cover specific parts so this item has sub_coverage
if( !found ) {
obj.armor->has_sub_coverage = true;
}
}
}
}
// calculate worst case and best case protection %
for( armor_portion_data &armor_data : obj.armor->data ) {
// go through each material and contribute its values
float tempbest = 1.0f;
float tempworst = 1.0f;
for( const part_material &mat : armor_data.materials ) {
// the percent chance the material is not hit
float cover = mat.cover * .01f;
float cover_invert = 1.0f - cover;
tempbest *= cover;
// just interested in cover values that can fail.
// multiplying by 0 would make this value pointless
if( cover_invert > 0 ) {
tempworst *= cover_invert;
}
}
// if tempworst is 1 then the item is homogenous so it only has a single option for damage
if( tempworst == 1 ) {
tempworst = 0;
}
// if not exactly 0 it should display as at least 1
if( tempworst > 0 ) {
armor_data.worst_protection_chance = std::max( 1.0f, tempworst * 100.0f );
} else {
armor_data.worst_protection_chance = 0;
}
if( tempbest > 0 ) {
armor_data.best_protection_chance = std::max( 1.0f, tempbest * 100.0f );
} else {
armor_data.best_protection_chance = 0;
}
}
// calculate worst case and best case protection %
for( armor_portion_data &armor_data : obj.armor->sub_data ) {
// go through each material and contribute its values
float tempbest = 1.0f;
float tempworst = 1.0f;
for( const part_material &mat : armor_data.materials ) {
// the percent chance the material is not hit
float cover = mat.cover * .01f;
float cover_invert = 1.0f - cover;
tempbest *= cover;
// just interested in cover values that can fail.
// multiplying by 0 would make this value pointless
if( cover_invert > 0 ) {
tempworst *= cover_invert;
}
}
// if tempworst is 1 then the item is homogenous so it only has a single option for damage
if( tempworst == 1 ) {
tempworst = 0;
}