-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathveh_type.cpp
1683 lines (1489 loc) · 60.7 KB
/
veh_type.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 "veh_type.h"
#include <algorithm>
#include <cstddef>
#include <limits>
#include <memory>
#include <numeric>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include "ammo.h"
#include "assign.h"
#include "cata_assert.h"
#include "character.h"
#include "color.h"
#include "debug.h"
#include "flag.h"
#include "game_constants.h"
#include "init.h"
#include "item.h"
#include "item_factory.h"
#include "item_group.h"
#include "item_pocket.h"
#include "itype.h"
#include "json.h"
#include "output.h"
#include "requirements.h"
#include "ret_val.h"
#include "string_formatter.h"
#include "translations.h"
#include "units.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vehicle_group.h"
#include "wcwidth.h"
class npc;
static const ammotype ammo_battery( "battery" );
static const itype_id itype_null( "null" );
static const quality_id qual_JACK( "JACK" );
static const quality_id qual_LIFT( "LIFT" );
static const skill_id skill_launcher( "launcher" );
static const vpart_id vpart_turret_generic( "turret_generic" );
static std::unordered_map<vproto_id, vehicle_prototype> vtypes;
// GENERAL GUIDELINES
// To determine mount position for parts (dx, dy), check this scheme:
// orthogonal dir left: (Y-)
// ^
// back: X- -------> forward dir: X+
// v
// orthogonal dir right (Y+)
//
// i.e, if you want to add a part to the back from the center of vehicle,
// use dx = -1, dy = 0;
// for the part 1 tile forward and two tiles left from the center of vehicle,
// use dx = 1, dy = -2.
//
// Internal parts should be added after external on the same mount point, i.e:
// part {"x": 0, "y": 1, "part": "seat"}, // put a seat (it's external)
// part {"x": 0, "y": 1, "part": "controls"}, // put controls for driver here
// part {"x": 0, "y": 1, "seatbelt"} // also, put a seatbelt here
// To determine, what parts can be external, and what can not, check
// vehicle_parts.json
// If you use wrong config, installation of part will fail
static const std::unordered_map<std::string, vpart_bitflags> vpart_bitflag_map = {
{ "ARMOR", VPFLAG_ARMOR },
{ "APPLIANCE", VPFLAG_APPLIANCE },
{ "EVENTURN", VPFLAG_EVENTURN },
{ "ODDTURN", VPFLAG_ODDTURN },
{ "CONE_LIGHT", VPFLAG_CONE_LIGHT },
{ "WIDE_CONE_LIGHT", VPFLAG_WIDE_CONE_LIGHT },
{ "HALF_CIRCLE_LIGHT", VPFLAG_HALF_CIRCLE_LIGHT },
{ "CIRCLE_LIGHT", VPFLAG_CIRCLE_LIGHT },
{ "BOARDABLE", VPFLAG_BOARDABLE },
{ "AISLE", VPFLAG_AISLE },
{ "CONTROLS", VPFLAG_CONTROLS },
{ "OBSTACLE", VPFLAG_OBSTACLE },
{ "OPAQUE", VPFLAG_OPAQUE },
{ "OPENABLE", VPFLAG_OPENABLE },
{ "SEATBELT", VPFLAG_SEATBELT },
{ "SIMPLE_PART", VPFLAG_SIMPLE_PART },
{ "WHEEL", VPFLAG_WHEEL },
{ "ROTOR", VPFLAG_ROTOR },
{ "ROTOR_SIMPLE", VPFLAG_ROTOR_SIMPLE },
{ "FLOATS", VPFLAG_FLOATS },
{ "DOME_LIGHT", VPFLAG_DOME_LIGHT },
{ "AISLE_LIGHT", VPFLAG_AISLE_LIGHT },
{ "ATOMIC_LIGHT", VPFLAG_ATOMIC_LIGHT },
{ "ALTERNATOR", VPFLAG_ALTERNATOR },
{ "ENGINE", VPFLAG_ENGINE },
{ "FRIDGE", VPFLAG_FRIDGE },
{ "FREEZER", VPFLAG_FREEZER },
{ "ARCADE", VPFLAG_ARCADE },
{ "LIGHT", VPFLAG_LIGHT },
{ "WINDOW", VPFLAG_WINDOW },
{ "CURTAIN", VPFLAG_CURTAIN },
{ "CARGO", VPFLAG_CARGO },
{ "INTERNAL", VPFLAG_INTERNAL },
{ "SOLAR_PANEL", VPFLAG_SOLAR_PANEL },
{ "WIND_TURBINE", VPFLAG_WIND_TURBINE },
{ "SPACE_HEATER", VPFLAG_SPACE_HEATER, },
{ "HEATED_TANK", VPFLAG_HEATED_TANK, },
{ "COOLER", VPFLAG_COOLER, },
{ "WATER_WHEEL", VPFLAG_WATER_WHEEL },
{ "RECHARGE", VPFLAG_RECHARGE },
{ "VISION", VPFLAG_EXTENDS_VISION },
{ "ENABLED_DRAINS_EPOWER", VPFLAG_ENABLED_DRAINS_EPOWER },
{ "AUTOCLAVE", VPFLAG_AUTOCLAVE },
{ "WASHING_MACHINE", VPFLAG_WASHING_MACHINE },
{ "DISHWASHER", VPFLAG_DISHWASHER },
{ "FLUIDTANK", VPFLAG_FLUIDTANK },
{ "REACTOR", VPFLAG_REACTOR },
{ "RAIL", VPFLAG_RAIL },
{ "TURRET_CONTROLS", VPFLAG_TURRET_CONTROLS },
{ "ROOF", VPFLAG_ROOF },
};
static const std::vector<std::pair<std::string, veh_ter_mod>> standard_terrain_mod = {{
{ "FLAT", { 0, 4 } }, { "ROAD", { 0, 2 } }
}
};
static const std::vector<std::pair<std::string, veh_ter_mod>> rigid_terrain_mod = {{
{ "FLAT", { 0, 6 } }, { "ROAD", { 0, 3 } }
}
};
static const std::vector<std::pair<std::string, veh_ter_mod>> racing_terrain_mod = {{
{ "FLAT", { 0, 5 } }, { "ROAD", { 0, 2 } }
}
};
static const std::vector<std::pair<std::string, veh_ter_mod>> off_road_terrain_mod = {{
{ "FLAT", { 0, 3 } }, { "ROAD", { 0, 1 } }
}
};
static const std::vector<std::pair<std::string, veh_ter_mod>> treads_terrain_mod = {{
{ "FLAT", { 0, 3 } }
}
};
static const std::vector<std::pair<std::string, veh_ter_mod>> rail_terrain_mod = {{
{ "RAIL", { 2, 8 } }
}
};
static std::map<vpart_id, vpart_info> vpart_info_all;
static std::map<vpart_id, vpart_info> abstract_parts;
static std::map<vpart_id, vpart_migration> vpart_migrations;
static DynamicDataLoader::deferred_json deferred;
std::pair<std::string, std::string> get_vpart_str_variant( const std::string &vpid )
{
for( const auto &vp_variant_pair : vpart_variants ) {
const std::string &vp_variant = vp_variant_pair.first;
const size_t loc = vpid.rfind( "_" + vp_variant );
if( loc != std::string::npos && ( ( loc + vp_variant.size() + 1 ) == vpid.size() ) ) {
return std::make_pair( vpid.substr( 0, loc ), vp_variant );
}
}
return std::make_pair( vpid, "" );
}
std::pair<vpart_id, std::string> get_vpart_id_variant( const vpart_id &vpid )
{
std::string final_vpid;
std::string variant_id;
std::tie( final_vpid, variant_id ) = get_vpart_str_variant( vpid.str() );
return std::make_pair( vpart_id( final_vpid ), variant_id );
}
std::pair<vpart_id, std::string> get_vpart_id_variant( const std::string &vpid )
{
std::string final_vpid;
std::string variant_id;
std::tie( final_vpid, variant_id ) = get_vpart_str_variant( vpid );
return std::make_pair( vpart_id( final_vpid ), variant_id );
}
/** @relates string_id */
template<>
bool string_id<vpart_info>::is_valid() const
{
return vpart_info_all.count( *this );
}
/** @relates string_id */
template<>
const vpart_info &string_id<vpart_info>::obj() const
{
const auto found = vpart_info_all.find( *this );
if( found == vpart_info_all.end() ) {
vpart_id base_id;
std::string variant;
std::tie( base_id, variant ) = get_vpart_id_variant( *this );
const auto var_found = vpart_info_all.find( base_id );
if( var_found == vpart_info_all.end() ) {
debugmsg( "Tried to get invalid vehicle part: %s", c_str() );
static const vpart_info null_part{};
return null_part;
}
return var_found->second;
}
return found->second;
}
static void parse_vp_reqs( const JsonObject &obj, const std::string &id, const std::string &key,
std::vector<std::pair<requirement_id, int>> &reqs,
std::map<skill_id, int> &skills, int &moves )
{
if( !obj.has_object( key ) ) {
return;
}
JsonObject src = obj.get_object( key );
JsonArray sk = src.get_array( "skills" );
if( !sk.empty() ) {
skills.clear();
}
for( JsonArray cur : sk ) {
skills.emplace( skill_id( cur.get_string( 0 ) ), cur.size() >= 2 ? cur.get_int( 1 ) : 1 );
}
if( src.has_int( "time" ) ) {
moves = src.get_int( "time" );
} else if( src.has_string( "time" ) ) {
moves = to_moves<int>( read_from_json_string<time_duration>( src.get_member( "time" ),
time_duration::units ) );
}
if( src.has_string( "using" ) ) {
reqs = { { requirement_id( src.get_string( "using" ) ), 1 } };
} else if( src.has_array( "using" ) ) {
reqs.clear();
for( JsonArray cur : src.get_array( "using" ) ) {
reqs.emplace_back( requirement_id( cur.get_string( 0 ) ), cur.get_int( 1 ) );
}
} else {
reqs.clear();
// Construct a requirement to capture "components", "qualities", and
// "tools" that might be listed.
// NOLINTNEXTLINE(cata-translate-string-literal)
const requirement_id req_id( string_format( "inline_%s_%s", key.c_str(), id.c_str() ) );
requirement_data::load_requirement( src, req_id );
reqs.emplace_back( req_id, 1 );
}
}
/**
* Reads engine info from a JsonObject.
*/
void vpart_info::load_engine( std::optional<vpslot_engine> &eptr, const JsonObject &jo,
const itype_id &fuel_type )
{
vpslot_engine e_info{};
if( eptr ) {
e_info = *eptr;
}
assign( jo, "backfire_threshold", e_info.backfire_threshold );
assign( jo, "backfire_freq", e_info.backfire_freq );
assign( jo, "noise_factor", e_info.noise_factor );
assign( jo, "damaged_power_factor", e_info.damaged_power_factor );
assign( jo, "m2c", e_info.m2c );
assign( jo, "muscle_power_factor", e_info.muscle_power_factor );
JsonArray excludes = jo.get_array( "exclusions" );
if( !excludes.empty() ) {
e_info.exclusions.clear();
for( const std::string line : excludes ) {
e_info.exclusions.push_back( line );
}
}
JsonArray fuel_opts = jo.get_array( "fuel_options" );
if( !fuel_opts.empty() ) {
e_info.fuel_opts.clear();
for( const std::string line : fuel_opts ) {
e_info.fuel_opts.emplace_back( line );
}
} else if( e_info.fuel_opts.empty() && fuel_type != itype_null ) {
e_info.fuel_opts.push_back( fuel_type );
}
eptr = e_info;
cata_assert( eptr );
}
void vpart_info::load_rotor( std::optional<vpslot_rotor> &roptr, const JsonObject &jo )
{
vpslot_rotor rotor_info{};
if( roptr ) {
rotor_info = *roptr;
}
assign( jo, "rotor_diameter", rotor_info.rotor_diameter );
roptr = rotor_info;
cata_assert( roptr );
}
void vpart_info::load_toolkit( std::optional<vpslot_toolkit> &tkptr, const JsonObject &jo )
{
vpslot_toolkit toolkit_info{};
if( tkptr ) {
toolkit_info = *tkptr;
}
assign( jo, "allowed_tools", toolkit_info.allowed_types );
tkptr = toolkit_info;
cata_assert( tkptr );
}
void vpart_info::load_wheel( std::optional<vpslot_wheel> &whptr, const JsonObject &jo )
{
vpslot_wheel wh_info{};
if( whptr ) {
wh_info = *whptr;
}
assign( jo, "rolling_resistance", wh_info.rolling_resistance );
assign( jo, "contact_area", wh_info.contact_area );
if( !jo.has_member( "copy-from" ) ) {
// if flag presented, it is already set
wh_info.terrain_mod = standard_terrain_mod;
wh_info.or_rating = 0.5f;
}
if( jo.has_string( "wheel_type" ) ) {
const std::string wheel_type = jo.get_string( "wheel_type" );
if( wheel_type == "rigid" ) {
wh_info.terrain_mod = rigid_terrain_mod;
wh_info.or_rating = 0.1;
} else if( wheel_type == "off-road" ) {
wh_info.terrain_mod = off_road_terrain_mod;
wh_info.or_rating = 0.7;
} else if( wheel_type == "racing" ) {
wh_info.terrain_mod = racing_terrain_mod;
wh_info.or_rating = 0.3;
} else if( wheel_type == "treads" ) {
wh_info.terrain_mod = treads_terrain_mod;
wh_info.or_rating = 0.9;
} else if( wheel_type == "rail" ) {
wh_info.terrain_mod = rail_terrain_mod;
wh_info.or_rating = 0.05;
}
}
whptr = wh_info;
cata_assert( whptr );
}
void vpart_info::load_workbench( std::optional<vpslot_workbench> &wbptr, const JsonObject &jo )
{
vpslot_workbench wb_info{};
if( wbptr ) {
wb_info = *wbptr;
}
JsonObject wb_jo = jo.get_object( "workbench" );
assign( wb_jo, "multiplier", wb_info.multiplier );
assign( wb_jo, "mass", wb_info.allowed_mass );
assign( wb_jo, "volume", wb_info.allowed_volume );
wbptr = wb_info;
cata_assert( wbptr );
}
/**
* Reads in a vehicle part from a JsonObject.
*/
void vpart_info::load( const JsonObject &jo, const std::string &src )
{
vpart_info def;
if( jo.has_string( "copy-from" ) ) {
const auto base = vpart_info_all.find( vpart_id( jo.get_string( "copy-from" ) ) );
const auto ab = abstract_parts.find( vpart_id( jo.get_string( "copy-from" ) ) );
if( base != vpart_info_all.end() ) {
def = base->second;
def.looks_like = base->second.id.str();
def.categories = base->second.categories;
} else if( ab != abstract_parts.end() ) {
def = ab->second;
if( def.looks_like.empty() ) {
def.looks_like = ab->second.id.str();
}
def.categories = ab->second.categories;
} else {
deferred.emplace_back( jo, src );
jo.allow_omitted_members();
return;
}
}
std::string temp_id;
std::string variant_id;
if( jo.has_string( "abstract" ) ) {
def.id = vpart_id( jo.get_string( "abstract" ) );
} else {
temp_id = jo.get_string( "id" );
std::tie( def.id, variant_id ) = get_vpart_id_variant( temp_id );
if( !variant_id.empty() ) {
const auto base = vpart_info_all.find( def.id );
if( base != vpart_info_all.end() ) {
def = base->second;
}
}
}
assign( jo, "name", def.name_ );
assign( jo, "item", def.base_item );
assign( jo, "location", def.location );
assign( jo, "durability", def.durability );
assign( jo, "damage_modifier", def.dmg_mod );
assign( jo, "energy_consumption", def.energy_consumption );
assign( jo, "power", def.power );
assign( jo, "epower", def.epower );
assign( jo, "emissions", def.emissions );
assign( jo, "exhaust", def.exhaust );
assign( jo, "fuel_type", def.fuel_type );
assign( jo, "default_ammo", def.default_ammo );
assign( jo, "folded_volume", def.folded_volume );
assign( jo, "size", def.size );
assign( jo, "bonus", def.bonus );
assign( jo, "cargo_weight_modifier", def.cargo_weight_modifier );
assign( jo, "categories", def.categories );
assign( jo, "flags", def.flags );
assign( jo, "description", def.description );
assign( jo, "comfort", def.comfort );
assign( jo, "floor_bedding_warmth", def.floor_bedding_warmth );
assign( jo, "bonus_fire_warmth_feet", def.bonus_fire_warmth_feet );
if( jo.has_member( "transform_terrain" ) ) {
JsonObject jttd = jo.get_object( "transform_terrain" );
for( const std::string pre_flag : jttd.get_array( "pre_flags" ) ) {
def.transform_terrain.pre_flags.emplace( pre_flag );
}
def.transform_terrain.post_terrain = jttd.get_string( "post_terrain", "t_null" );
def.transform_terrain.post_furniture = jttd.get_string( "post_furniture", "f_null" );
def.transform_terrain.post_field = jttd.get_string( "post_field", "fd_null" );
def.transform_terrain.post_field_intensity = jttd.get_int( "post_field_intensity", 0 );
if( jttd.has_int( "post_field_age" ) ) {
def.transform_terrain.post_field_age = time_duration::from_turns(
jttd.get_int( "post_field_age" ) );
} else if( jttd.has_string( "post_field_age" ) ) {
def.transform_terrain.post_field_age = read_from_json_string<time_duration>(
jttd.get_member( "post_field_age" ), time_duration::units );
} else {
def.transform_terrain.post_field_age = 0_turns;
}
}
if( jo.has_member( "requirements" ) ) {
JsonObject reqs = jo.get_object( "requirements" );
parse_vp_reqs( reqs, def.id.str(), "install", def.install_reqs, def.install_skills,
def.install_moves );
parse_vp_reqs( reqs, def.id.str(), "removal", def.removal_reqs, def.removal_skills,
def.removal_moves );
parse_vp_reqs( reqs, def.id.str(), "repair", def.repair_reqs, def.repair_skills,
def.repair_moves );
}
if( jo.has_string( "symbol" ) ) {
int symbol = jo.get_string( "symbol" )[ 0 ];
if( variant_id.empty() ) {
def.sym = symbol;
} else {
def.symbols[ variant_id ] = symbol;
}
}
if( jo.has_bool( "standard_symbols" ) && jo.get_bool( "standard_symbols" ) ) {
// Fallback symbol for unknown variant
def.sym = '=';
for( const auto &variant : vpart_variants_standard ) {
def.symbols[ variant.first ] = variant.second;
}
}
if( jo.has_object( "symbols" ) ) {
JsonObject jo_variants = jo.get_object( "symbols" );
for( const auto &vp_variant_pair : vpart_variants ) {
const std::string &vp_variant = vp_variant_pair.first;
if( jo_variants.has_string( vp_variant ) ) {
def.symbols[ vp_variant ] = static_cast<uint8_t>( jo_variants.get_string( vp_variant )[ 0 ] );
}
}
}
if( jo.has_string( "broken_symbol" ) ) {
def.sym_broken = static_cast<uint8_t>( jo.get_string( "broken_symbol" )[ 0 ] );
}
jo.read( "looks_like", def.looks_like );
if( jo.has_member( "color" ) ) {
def.color = color_from_string( jo.get_string( "color" ) );
}
if( jo.has_member( "broken_color" ) ) {
def.color_broken = color_from_string( jo.get_string( "broken_color" ) );
}
if( jo.has_member( "breaks_into" ) ) {
def.breaks_into_group = item_group::load_item_group(
jo.get_member( "breaks_into" ), "collection", "breaks_into for vpart " + def.id.str() );
}
JsonArray qual = jo.get_array( "qualities" );
if( !qual.empty() ) {
def.qualities.clear();
for( JsonArray pair : qual ) {
def.qualities[ quality_id( pair.get_string( 0 ) ) ] = pair.get_int( 1 );
}
}
JsonArray tools = jo.get_array( "pseudo_tools" );
if( !tools.empty() ) {
def.pseudo_tools.clear();
while( tools.has_more() ) {
const JsonObject tooldef = tools.next_object();
const itype_id tool_id( tooldef.get_string( "id" ) );
const std::string hotkey_str = tooldef.has_string( "hotkey" ) ? tooldef.get_string( "hotkey" ) : "";
const int hotkey = hotkey_str.empty() ? -1 : static_cast<int>( hotkey_str[0] );
if( !def.pseudo_tools.insert( { tool_id, hotkey } ).second ) {
debugmsg( "Insert failed on %s to %s's tools, duplicate?", tool_id.str(), def.id.str() );
}
}
}
assign( jo, "folding_tools", def.folding_tools );
assign( jo, "unfolding_tools", def.unfolding_tools );
assign( jo, "folding_time", def.folding_time );
assign( jo, "unfolding_time", def.unfolding_time );
if( jo.has_member( "damage_reduction" ) ) {
JsonObject dred = jo.get_object( "damage_reduction" );
def.damage_reduction = load_damage_map( dred );
}
if( def.has_flag( "ENGINE" ) ) {
load_engine( def.engine_info, jo, def.fuel_type );
}
if( def.has_flag( "WHEEL" ) ) {
load_wheel( def.wheel_info, jo );
}
if( def.has_flag( "ROTOR" ) || def.has_flag( "ROTOR_SIMPLE" ) ) {
load_rotor( def.rotor_info, jo );
}
if( def.has_flag( "WORKBENCH" ) ) {
load_workbench( def.workbench_info, jo );
}
if( def.has_flag( "VEH_TOOLS" ) ) {
load_toolkit( def.toolkit_info, jo );
}
if( jo.has_string( "abstract" ) ) {
abstract_parts[def.id] = def;
} else {
vpart_info_all[def.id] = def;
}
}
void vpart_info::set_flag( const std::string &flag )
{
flags.insert( flag );
const auto iter = vpart_bitflag_map.find( flag );
if( iter != vpart_bitflag_map.end() ) {
bitflags.set( iter->second );
}
}
const std::set<std::string> &vpart_info::get_categories() const
{
return this->categories;
}
// @returns true for "valid" gun items which can be mounted as vehicle turret
static bool mountable_gun_filter( const itype &guntype )
{
static const std::vector<flag_id> bad_flags {
flag_BIONIC_WEAPON,
flag_NO_TURRET,
flag_NO_UNWIELD,
flag_PSEUDO,
flag_RELOAD_AND_SHOOT,
flag_STR_DRAW,
};
if( !guntype.gun || guntype.gunmod ) {
return false;
}
return std::none_of( bad_flags.cbegin(), bad_flags.cend(), [&guntype]( const flag_id & flag ) {
return guntype.has_flag( flag );
} );
}
// @returns true if itype uses liquid ammo directly or has a magwell + magazine pocket that accepts liquid ammo
static bool gun_uses_liquid_ammo( const itype &guntype )
{
for( const ammotype &at : guntype.gun->ammo ) {
if( at->default_ammotype()->phase == phase_id::LIQUID ) {
return true;
}
}
for( const pocket_data &maybe_magwell : guntype.pockets ) {
for( const itype_id &restricted_types : maybe_magwell.item_id_restriction ) {
for( const pocket_data &maybe_mag : restricted_types.obj().pockets ) {
for( const std::pair<const ammotype, int> &res : maybe_mag.ammo_restriction ) {
if( res.first->default_ammotype()->phase == phase_id::LIQUID ) {
return true;
}
}
}
}
}
return false;
}
// @returns Amount of battery drained on default gunmode if itype uses magwell+mag with battery ammo
static int gun_battery_mags_drain( const itype &guntype )
{
int charges_used = 0;
for( const pocket_data &maybe_mag_well : guntype.pockets ) {
for( const itype_id &restricted_type : maybe_mag_well.item_id_restriction ) {
for( const pocket_data &maybe_mag : restricted_type.obj().pockets ) {
for( const std::pair<const ammotype, int> &res : maybe_mag.ammo_restriction ) {
if( res.first == ammo_battery ) {
charges_used = std::max( charges_used, guntype.gun->ammo_to_fire );
}
}
}
}
}
if( guntype.gun->energy_drain > 0_kJ && charges_used > 0 ) {
debugmsg( "%s uses both energy and battery magazines", guntype.nname( 1 ) );
}
return charges_used;
}
static std::string get_looks_like( const vpart_info &vpi, const itype &it )
{
static const auto has_light_ammo = []( const std::set<ammotype> &ats ) {
for( const ammotype &at : ats ) {
if( !at->default_ammotype() ) {
continue;
}
const auto at_weight = at->default_ammotype()->weight;
if( 15_gram > at_weight && at_weight > 3_gram ) {
return true; // detects g80 and coil gun
}
}
return false;
};
if( it.gun->energy_drain > 0_kJ && has_light_ammo( it.gun->ammo ) ) {
return "mounted_hk_g80"; // railguns
} else if( vpi.has_flag( "USE_BATTERIES" ) || it.gun->energy_drain > 0_kJ ) {
return "laser_rifle"; // generic energy weapons
} else if( vpi.has_flag( "USE_TANKS" ) ) {
return "watercannon"; // liquid sprayers (flamethrower, foam gun etc)
} else if( it.gun->skill_used == skill_launcher ) {
return "tow_launcher"; // launchers
} else {
return "m249"; // machine guns and also default for any unknown
}
}
void vpart_info::finalize()
{
DynamicDataLoader::get_instance().load_deferred( deferred );
for( const itype *const item : item_controller->find( mountable_gun_filter ) ) {
vpart_info new_part = vpart_info_all[vpart_turret_generic]; // copy from generic
const itype_id item_id = item->get_id();
new_part.id = vpart_id( "turret_" + item_id.str() );
new_part.base_item = item_id;
new_part.description = item->description;
new_part.color = item->color;
// calculate requirements for install/removal by gross estimate
int primary_req = 3; // 3 in whatever gun's skill is set to
int mechanics_req = 3 + static_cast<int>( item->weight / 10_kilogram );
int electronics_req = 0; // calculated below
// mark turrets with migrated-from or blacklisted items obsolete
if( item_id != item_controller->migrate_id( item_id ) ||
item_is_blacklisted( item->get_id() ) ) {
new_part.set_flag( "OBSOLETE" );
}
if( gun_uses_liquid_ammo( *item ) ) {
new_part.set_flag( "USE_TANKS" );
// +1 mechanics level for liquid plumbing
mechanics_req++;
}
const int battery_mags_drain = gun_battery_mags_drain( *item );
if( battery_mags_drain ) {
// USE_BATTERIES for afs cartridge-like batteries
new_part.set_flag( "USE_BATTERIES" );
// +1 electronics level per 15 battery charges used for electric plumbing
electronics_req += static_cast<int>( std::ceil( battery_mags_drain / 15.0 ) );
}
// cap all skills at 8
primary_req = std::min( 8, primary_req );
mechanics_req = std::min( 8, mechanics_req );
electronics_req = std::min( 8, electronics_req );
new_part.install_skills.emplace( item->gun->skill_used, primary_req );
new_part.removal_skills.emplace( item->gun->skill_used, primary_req / 2 );
new_part.install_skills.emplace( "mechanics", mechanics_req );
new_part.removal_skills.emplace( "mechanics", mechanics_req / 2 );
if( electronics_req > 0 ) {
new_part.install_skills.emplace( "electronics", electronics_req );
new_part.removal_skills.emplace( "electronics", electronics_req / 2 );
}
int difficulty = primary_req + mechanics_req + electronics_req * 2;
time_duration install_time = 10_minutes * difficulty;
time_duration removal_time = 10_minutes * difficulty / 2;
new_part.install_moves = to_moves<int>( install_time );
new_part.removal_moves = to_moves<int>( removal_time );
new_part.looks_like = get_looks_like( new_part, *item );
vpart_info_all.emplace( new_part.id, new_part );
}
for( auto &e : vpart_info_all ) {
for( const auto &f : e.second.flags ) {
auto b = vpart_bitflag_map.find( f );
if( b != vpart_bitflag_map.end() ) {
e.second.bitflags.set( b->second );
}
}
if( e.second.has_flag( VPFLAG_APPLIANCE ) ) {
// force all appliances' location field to "structure"
// dragging code currently checks this for considering collisions
e.second.location = "structure";
}
finalize_damage_map( e.second.damage_reduction );
// Calculate and cache z-ordering based off of location
// list_order is used when inspecting the vehicle
if( e.second.location == "on_roof" ) {
e.second.z_order = 9;
e.second.list_order = 3;
} else if( e.second.location == "on_cargo" ) {
e.second.z_order = 8;
e.second.list_order = 6;
} else if( e.second.location == "center" ) {
e.second.z_order = 7;
e.second.list_order = 7;
} else if( e.second.location == "under" ) {
// Have wheels show up over frames
e.second.z_order = 6;
e.second.list_order = 10;
} else if( e.second.location == "structure" ) {
e.second.z_order = 5;
e.second.list_order = 1;
} else if( e.second.location == "engine_block" ) {
// Should be hidden by frames
e.second.z_order = 4;
e.second.list_order = 8;
} else if( e.second.location == "on_battery_mount" ) {
// Should be hidden by frames
e.second.z_order = 3;
e.second.list_order = 10;
} else if( e.second.location == "fuel_source" ) {
// Should be hidden by frames
e.second.z_order = 3;
e.second.list_order = 9;
} else if( e.second.location == "roof" ) {
// Shouldn't be displayed
e.second.z_order = -1;
e.second.list_order = 4;
} else if( e.second.location == "armor" ) {
// Shouldn't be displayed (the color is used, but not the symbol)
e.second.z_order = -2;
e.second.list_order = 2;
} else {
// Everything else
e.second.z_order = 0;
e.second.list_order = 5;
}
std::set<std::pair<itype_id, int>> &pt = e.second.pseudo_tools;
for( auto it = pt.begin(); it != pt.end(); /* blank */ ) {
const itype_id &tool = it->first;
if( !tool.is_valid() ) {
debugmsg( "invalid pseudo tool itype_id '%s' on '%s'", tool.str(), e.first.str() );
pt.erase( it++ );
} else {
++it;
}
}
if( e.second.toolkit_info.has_value() ) {
std::set<itype_id> &pt = e.second.toolkit_info->allowed_types;
for( auto it = pt.begin(); it != pt.end(); /* blank */ ) {
if( !it->is_valid() ) {
debugmsg( "invalid itype_id '%s' on 'allowed_tools' of vpart '%s'", it->str(), e.first.str() );
pt.erase( it++ );
} else {
++it;
}
}
}
}
}
static bool type_can_contain( const itype &container, const itype_id &containee )
{
return item( &container ).can_contain( item( containee ) ).success();
}
void vpart_info::check()
{
for( auto &vp : vpart_info_all ) {
vpart_info &part = vp.second;
// add the base item to the installation requirements
// TODO: support multiple/alternative base items
requirement_data ins;
ins.components.push_back( { { { part.base_item, 1 } } } );
const requirement_id ins_id( std::string( "inline_vehins_base_" ) + part.id.str() );
requirement_data::save_requirement( ins, ins_id );
part.install_reqs.emplace_back( ins_id, 1 );
if( part.removal_moves < 0 ) {
part.removal_moves = part.install_moves / 2;
}
for( auto &e : part.install_skills ) {
if( !e.first.is_valid() ) {
debugmsg( "vehicle part %s has unknown install skill %s", part.id.c_str(), e.first.c_str() );
}
}
for( auto &e : part.removal_skills ) {
if( !e.first.is_valid() ) {
debugmsg( "vehicle part %s has unknown removal skill %s", part.id.c_str(), e.first.c_str() );
}
}
for( auto &e : part.repair_skills ) {
if( !e.first.is_valid() ) {
debugmsg( "vehicle part %s has unknown repair skill %s", part.id.c_str(), e.first.c_str() );
}
}
for( const auto &e : part.install_reqs ) {
if( !e.first.is_valid() || e.second <= 0 ) {
debugmsg( "vehicle part %s has unknown or incorrectly specified install requirements %s",
part.id.c_str(), e.first.c_str() );
}
}
for( const auto &e : part.install_reqs ) {
if( !( e.first.is_null() || e.first.is_valid() ) || e.second < 0 ) {
debugmsg( "vehicle part %s has unknown or incorrectly specified removal requirements %s",
part.id.c_str(), e.first.c_str() );
}
}
for( const auto &e : part.repair_reqs ) {
if( !( e.first.is_null() || e.first.is_valid() ) || e.second < 0 ) {
debugmsg( "vehicle part %s has unknown or incorrectly specified repair requirements %s",
part.id.c_str(), e.first.c_str() );
}
}
if( part.install_moves < 0 ) {
debugmsg( "vehicle part %s has negative installation time", part.id.c_str() );
}
if( part.removal_moves < 0 ) {
debugmsg( "vehicle part %s has negative removal time", part.id.c_str() );
}
if( !item_group::group_is_defined( part.breaks_into_group ) ) {
debugmsg( "Vehicle part %s breaks into non-existent item group %s.",
part.id.c_str(), part.breaks_into_group.c_str() );
}
for( const auto &f : part.get_flags() ) {
if( !json_flag::get( f ) ) {
debugmsg( "vehicle part %s has unknown flag %s", part.id.c_str(), f.c_str() );
}
}
// Default symbol is always needed in case an unknown variant is encountered
if( part.sym == 0 ) {
debugmsg( "vehicle part %s does not define a symbol", part.id.c_str() );
} else if( mk_wcwidth( part.sym ) != 1 ) {
debugmsg( "vehicle part %s defined a symbol that is not 1 console cell wide.",
part.id.str() );
}
if( part.sym_broken == 0 ) {
debugmsg( "vehicle part %s does not define a broken symbol", part.id.c_str() );
} else if( mk_wcwidth( part.sym_broken ) != 1 ) {
debugmsg( "vehicle part %s defined a broken symbol that is not 1 console cell wide.",
part.id.str() );
}
for( const std::pair<const std::string, int> &sym : part.symbols ) {
if( mk_wcwidth( sym.second ) != 1 ) {
debugmsg( "vehicle part %s defined a variant symbol that is not 1 console cell wide.",
part.id.str() );
}
}
if( part.durability <= 0 ) {
debugmsg( "vehicle part %s has zero or negative durability", part.id.c_str() );
}
if( part.dmg_mod < 0 ) {
debugmsg( "vehicle part %s has negative damage modifier", part.id.c_str() );
}
if( part.folded_volume && part.folded_volume.value() <= 0_ml ) {
debugmsg( "vehicle part %s has folding part with zero or negative folded volume", part.id.c_str() );
}
if( !item::type_is_defined( part.default_ammo ) ) {
debugmsg( "vehicle part %s has undefined default ammo %s", part.id.c_str(),
part.base_item.c_str() );
}
if( part.size != 0_ml ) {
if( part.size < 0_ml ) {
debugmsg( "vehicle part %s has negative size", part.id.c_str() );
}
if( !part.has_flag( "CARGO" ) ) {
debugmsg( "vehicle part %s is not CARGO and cannot define size", part.id.c_str() );
} else if( part.has_flag( "FLUIDTANK" ) ) {
debugmsg( "vehicle part %s is FLUIDTANK and cannot define size; it's defined on part's base item",
part.id.c_str() );
}
}
if( part.has_flag( "CARGO" ) && part.has_flag( "FLUIDTANK" ) ) {
debugmsg( "vehicle part %s can't have both CARGO and FLUIDTANK flags at the same time",
part.id.c_str() );
}
if( !item::type_is_defined( part.base_item ) ) {
debugmsg( "vehicle part %s uses undefined item %s", part.id.c_str(), part.base_item.c_str() );
}
const itype &base_item_type = *item::find_type( part.base_item );
// Fuel type errors are serious and need fixing now
if( !item::type_is_defined( part.fuel_type ) ) {
debugmsg( "vehicle part %s uses undefined fuel %s", part.id.c_str(), part.base_item.c_str() );
part.fuel_type = itype_id::NULL_ID();
} else if( !part.fuel_type.is_null() && !item( part.fuel_type ).is_fuel() &&
!type_can_contain( base_item_type, part.fuel_type ) ) {
// HACK: Tanks are allowed to specify non-fuel "fuel",
// because currently legacy blazemod uses it as a hack to restrict content types
debugmsg( "non-tank vehicle part %s uses non-fuel item %s as fuel, setting to null",
part.id.c_str(), part.fuel_type.c_str() );
part.fuel_type = itype_id::NULL_ID();
}
if( part.has_flag( "TURRET" ) && !base_item_type.gun ) {
debugmsg( "vehicle part %s has the TURRET flag, but is not made from a gun item", part.id.c_str() );
}
for( const emit_id &e : part.emissions ) {
if( !e.is_valid() ) {
debugmsg( "vehicle part %s has invalid emission %s was set",
part.id.c_str(), e.str().c_str() );
}
}
for( const emit_id &e : part.exhaust ) {
if( !e.is_valid() ) {
debugmsg( "vehicle part %s has invalid exhaust %s was set",
part.id.c_str(), e.str().c_str() );
}
}
if( part.has_flag( "WHEEL" ) && !base_item_type.wheel ) {
debugmsg( "vehicle part %s has the WHEEL flag, but base item %s is not a wheel. "
"THIS WILL CRASH!", part.id.str(), part.base_item.str() );
}
if( part.has_flag( "WHEEL" ) && !part.has_flag( "UNSTABLE_WHEEL" ) && !part.has_flag( "STABLE" ) ) {
debugmsg( "Wheel '%s' lacks either 'UNSTABLE_WHEEL' or 'STABLE' flag.", vp.first.str() );
}
if( part.has_flag( "UNSTABLE_WHEEL" ) && part.has_flag( "STABLE" ) ) {
debugmsg( "Wheel '%s' cannot be both an 'UNSTABLE_WHEEL' and 'STABLE'.", vp.first.str() );
}
for( auto &q : part.qualities ) {
if( !q.first.is_valid() ) {
debugmsg( "vehicle part %s has undefined tool quality %s", part.id.c_str(), q.first.c_str() );
}
}
if( part.has_flag( VPFLAG_ENABLED_DRAINS_EPOWER ) && part.epower == 0_W ) {