forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
veh_type.cpp
1165 lines (1042 loc) · 41.4 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 <cassert>
#include <cstddef>
#include <memory>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include "ammo.h"
#include "assign.h"
#include "cata_utility.h"
#include "character_functions.h"
#include "color.h"
#include "debug.h"
#include "flag.h"
#include "game_constants.h"
#include "init.h"
#include "item.h"
#include "item_group.h"
#include "itype.h"
#include "json.h"
#include "mapdata.h"
#include "output.h"
#include "player.h"
#include "requirements.h"
#include "string_formatter.h"
#include "string_id.h"
#include "translations.h"
#include "units.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vehicle_group.h"
class npc;
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 },
{ "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 },
{ "WHEEL", VPFLAG_WHEEL },
{ "ROTOR", VPFLAG_ROTOR },
{ "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 },
{ "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, },
{ "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, int>> standard_terrain_mod = {{
{ "FLAT", 4 }, { "ROAD", 2 }
}
};
static const std::vector<std::pair<std::string, int>> rigid_terrain_mod = {{
{ "FLAT", 6 }, { "ROAD", 3 }
}
};
static const std::vector<std::pair<std::string, int>> racing_terrain_mod = {{
{ "FLAT", 5 }, { "ROAD", 2 }
}
};
static const std::vector<std::pair<std::string, int>> off_road_terrain_mod = {{
{ "FLAT", 3 }, { "ROAD", 1 }
}
};
static const std::vector<std::pair<std::string, int>> treads_terrain_mod = {{
{ "FLAT", 3 }
}
};
static const std::vector<std::pair<std::string, int>> rail_terrain_mod = {{
{ "RAIL", 8 }
}
};
static std::map<vpart_id, vpart_info> vpart_info_all;
static std::map<vpart_id, vpart_info> abstract_parts;
static DynamicDataLoader::deferred_json deferred;
/** @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() ) {
debugmsg( "Tried to get invalid vehicle part: %s", c_str() );
static const vpart_info null_part{};
return null_part;
}
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 );
auto 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_raw( "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.
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 );
auto excludes = jo.get_array( "exclusions" );
if( !excludes.empty() ) {
e_info.exclusions.clear();
for( const std::string line : excludes ) {
e_info.exclusions.push_back( line );
}
}
auto 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.push_back( itype_id( line ) );
}
} else if( e_info.fuel_opts.empty() && fuel_type != itype_id( "null" ) ) {
e_info.fuel_opts.push_back( fuel_type );
}
eptr = e_info;
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;
assert( roptr );
}
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;
} else if( wheel_type == "standard" ) {
wh_info.terrain_mod = standard_terrain_mod;
wh_info.or_rating = 0.5;
} else {
jo.throw_error( string_format( "Unknown wheel type '%s'", wheel_type ), "wheel_type" );
}
}
whptr = wh_info;
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;
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();
} else if( ab != abstract_parts.end() ) {
def = ab->second;
if( def.looks_like.empty() ) {
def.looks_like = ab->second.id.str();
}
} else {
deferred.emplace_back( jo.get_source_location(), src );
jo.allow_omitted_members();
return;
}
}
if( jo.has_string( "abstract" ) ) {
def.id = vpart_id( jo.get_string( "abstract" ) );
} else {
def.id = vpart_id( jo.get_string( "id" ) );
}
assign( jo, "name", def.name_ );
assign( jo, "item", def.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, "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, "difficulty", def.difficulty );
assign( jo, "bonus", def.bonus );
assign( jo, "cargo_weight_modifier", def.cargo_weight_modifier );
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_raw( "post_field_age" ), time_duration::units );
} else {
def.transform_terrain.post_field_age = 0_turns;
}
}
if( jo.has_member( "requirements" ) ) {
auto 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_member( "symbol" ) ) {
def.sym = jo.get_string( "symbol" )[ 0 ];
}
if( jo.has_member( "broken_symbol" ) ) {
def.sym_broken = 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" );
}
auto 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 );
}
}
if( jo.has_member( "damage_reduction" ) ) {
JsonObject dred = jo.get_object( "damage_reduction" );
def.damage_reduction = load_damage_array( 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" ) ) {
load_rotor( def.rotor_info, jo );
}
if( def.has_flag( "WORKBENCH" ) ) {
load_workbench( def.workbench_info, jo );
}
// Dummy
// TODO: Implement
jo.get_string_array( "categories" );
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 );
}
}
void vpart_info::finalize()
{
DynamicDataLoader::get_instance().load_deferred( deferred );
for( auto &e : vpart_info_all ) {
if( e.second.folded_volume > 0_ml ) {
e.second.set_flag( "FOLDABLE" );
}
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 );
}
}
// 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;
}
}
}
void vpart_info::check()
{
for( auto &vp : vpart_info_all ) {
auto &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.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() );
}
if( part.sym == 0 ) {
debugmsg( "vehicle part %s does not define a symbol", part.id.c_str() );
}
if( part.sym_broken == 0 ) {
debugmsg( "vehicle part %s does not define a broken symbol", part.id.c_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 < 0_ml ) {
debugmsg( "vehicle part %s has negative folded volume", part.id.c_str() );
}
if( part.has_flag( "FOLDABLE" ) && part.folded_volume == 0_ml ) {
debugmsg( "vehicle part %s has folding part with zero folded volume", part.name() );
}
if( !part.default_ammo.is_valid() ) {
debugmsg( "vehicle part %s has undefined default ammo %s", part.id.c_str(), part.item.c_str() );
}
if( part.size < 0_ml ) {
debugmsg( "vehicle part %s has negative size", part.id.c_str() );
}
if( !part.item.is_valid() ) {
debugmsg( "vehicle part %s uses undefined item %s", part.id.c_str(), part.item.c_str() );
}
const itype &base_item_type = *part.item;
// Fuel type errors are serious and need fixing now
if( !part.fuel_type.is_valid() ) {
debugmsg( "vehicle part %s uses undefined fuel %s", part.id.c_str(), part.item.c_str() );
part.fuel_type = itype_id::NULL_ID();
} else if( part.fuel_type && !part.fuel_type->fuel &&
( !base_item_type.container || !base_item_type.container->watertight ) ) {
// 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() );
}
if( !part.emissions.empty() && !part.has_flag( "EMITTER" ) ) {
debugmsg( "vehicle part %s has emissions set, but the EMITTER flag is not set", part.id.c_str() );
}
if( part.has_flag( "EMITTER" ) ) {
if( part.emissions.empty() ) {
debugmsg( "vehicle part %s has the EMITTER flag, but no emissions were set", part.id.c_str() );
} else {
for( const emit_id &e : part.emissions ) {
if( !e.is_valid() ) {
debugmsg( "vehicle part %s has the EMITTER flag, but invalid emission %s was set",
part.id.c_str(), e.str().c_str() );
}
}
}
}
if( part.has_flag( "ADVANCED_PLANTER" ) && !part.has_flag( "PLANTER" ) ) {
debugmsg( "vehicle part %s has ADVANCED_PLANTER flag, but is missing PLANTER flag.",
part.id.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.item.str() );
}
if( part.has_flag( "RAIL" ) && !part.has_flag( "WHEEL" ) ) {
debugmsg( "vehicle part %s has RAIL flag, but is missing WHEEL flag.", part.id.c_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 ) {
debugmsg( "%s is set to drain epower, but has epower == 0", part.id.c_str() );
}
// Parts with non-zero epower must have a flag that affects epower usage
static const std::vector<std::string> handled = {{
"ENABLED_DRAINS_EPOWER", "SECURITY", "ENGINE",
"ALTERNATOR", "SOLAR_PANEL", "POWER_TRANSFER",
"REACTOR", "WIND_TURBINE", "WATER_WHEEL"
}
};
if( part.epower != 0 &&
std::none_of( handled.begin(), handled.end(), [&part]( const std::string & flag ) {
return part.has_flag( flag );
} ) ) {
std::string warnings_are_good_docs = enumerate_as_string( handled );
debugmsg( "%s has non-zero epower, but lacks a flag that would make it affect epower (one of %s)",
part.id.c_str(), warnings_are_good_docs.c_str() );
}
}
}
void vpart_info::reset()
{
deferred.clear();
vpart_info_all.clear();
abstract_parts.clear();
}
const std::map<vpart_id, vpart_info> &vpart_info::all()
{
return vpart_info_all;
}
std::string vpart_info::name() const
{
if( name_.empty() ) {
return item::nname( item );
} else {
return name_.translated();
}
}
int vpart_info::format_description( std::string &msg, const nc_color &format_color,
int width ) const
{
int lines = 1;
msg += _( "<color_white>Description</color>\n" );
msg += "> <color_" + string_from_color( format_color ) + ">";
std::string long_descrip;
if( !description.empty() ) {
long_descrip += description.translated();
}
for( const auto &flagid : flags ) {
if( flagid == "ALARMCLOCK" || flagid == "WATCH" ) {
continue;
}
json_flag flag = json_flag::get( flagid );
if( !flag.info().empty() ) {
if( !long_descrip.empty() ) {
long_descrip += " ";
}
long_descrip += _( flag.info() );
}
}
if( ( has_flag( "SEAT" ) || has_flag( "BED" ) ) && !has_flag( "BELTABLE" ) ) {
json_flag nobelt = json_flag::get( "NONBELTABLE" );
long_descrip += " " + _( nobelt.info() );
}
if( has_flag( "BOARDABLE" ) && has_flag( "OPENABLE" ) ) {
json_flag nobelt = json_flag::get( "DOOR" );
long_descrip += " " + _( nobelt.info() );
}
if( has_flag( "TURRET" ) ) {
class::item base( item );
if( base.ammo_required() && !base.ammo_remaining() ) {
itype_id default_ammo = base.magazine_current() ? base.common_ammo_default() : base.ammo_default();
base.ammo_set( default_ammo );
}
long_descrip += string_format( _( "\nRange: %1$5d Damage: %2$5.0f" ),
base.gun_range( true ),
base.gun_damage().total_damage() );
}
if( !long_descrip.empty() ) {
const auto wrap_descrip = foldstring( long_descrip, width );
msg += wrap_descrip[0];
for( size_t i = 1; i < wrap_descrip.size(); i++ ) {
msg += "\n " + wrap_descrip[i];
}
msg += "</color>\n";
lines += wrap_descrip.size();
}
// borrowed from item.cpp and adjusted
const quality_id quality_jack( "JACK" );
const quality_id quality_lift( "LIFT" );
for( const auto &qual : qualities ) {
msg += string_format(
_( "Has level <color_cyan>%1$d %2$s</color> quality" ), qual.second, qual.first.obj().name );
if( qual.first == quality_jack || qual.first == quality_lift ) {
msg += string_format( _( " and is rated at <color_cyan>%1$d %2$s</color>" ),
static_cast<int>( convert_weight( qual.second * TOOL_LIFT_FACTOR ) ),
weight_units() );
}
msg += ".\n";
lines += 1;
}
return lines;
}
requirement_data vpart_info::install_requirements() const
{
return std::accumulate( install_reqs.begin(), install_reqs.end(), requirement_data(),
[]( const requirement_data & lhs, const std::pair<requirement_id, int> &rhs ) {
return lhs + ( *rhs.first * rhs.second );
} );
}
requirement_data vpart_info::removal_requirements() const
{
return std::accumulate( removal_reqs.begin(), removal_reqs.end(), requirement_data(),
[]( const requirement_data & lhs, const std::pair<requirement_id, int> &rhs ) {
return lhs + ( *rhs.first * rhs.second );
} );
}
requirement_data vpart_info::repair_requirements() const
{
return std::accumulate( repair_reqs.begin(), repair_reqs.end(), requirement_data(),
[]( const requirement_data & lhs, const std::pair<requirement_id, int> &rhs ) {
return lhs + ( *rhs.first * rhs.second );
} );
}
bool vpart_info::is_repairable() const
{
return !repair_requirements().is_empty();
}
static int scale_time( const std::map<skill_id, int> &sk, int mv, const player &p )
{
if( sk.empty() ) {
return mv;
}
const int lvl = std::accumulate( sk.begin(), sk.end(), 0, [&p]( int lhs,
const std::pair<skill_id, int> &rhs ) {
return lhs + std::max( std::min( p.get_skill_level( rhs.first ), MAX_SKILL ) - rhs.second,
0 );
} );
// 10% per excess level (reduced proportionally if >1 skill required) with max 50% reduction
// 10% reduction per assisting NPC
return mv * ( 1.0 - std::min( static_cast<double>( lvl ) / sk.size() / 10.0, 0.5 ) )
* ( 10 - character_funcs::get_crafting_helpers( p, 3 ).size() ) / 10;
}
int vpart_info::install_time( const player &p ) const
{
return scale_time( install_skills, install_moves, p );
}
int vpart_info::removal_time( const player &p ) const
{
return scale_time( removal_skills, removal_moves, p );
}
int vpart_info::repair_time( const player &p ) const
{
return scale_time( repair_skills, repair_moves, p );
}
/**
* @name Engine specific functions
*
*/
float vpart_info::engine_backfire_threshold() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->backfire_threshold : false;
}
int vpart_info::engine_backfire_freq() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->backfire_freq : false;
}
int vpart_info::engine_muscle_power_factor() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->muscle_power_factor : false;
}
float vpart_info::engine_damaged_power_factor() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->damaged_power_factor : false;
}
int vpart_info::engine_noise_factor() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->noise_factor : false;
}
int vpart_info::engine_m2c() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->m2c : 0;
}
std::vector<std::string> vpart_info::engine_excludes() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->exclusions : std::vector<std::string>();
}
std::vector<itype_id> vpart_info::engine_fuel_opts() const
{
return has_flag( VPFLAG_ENGINE ) ? engine_info->fuel_opts : std::vector<itype_id>();
}
/**
* @name Wheel specific functions
*
*/
float vpart_info::wheel_rolling_resistance() const
{
// caster wheels return 29, so if a part rolls worse than a caster wheel...
return has_flag( VPFLAG_WHEEL ) ? wheel_info->rolling_resistance : 50;
}
int vpart_info::wheel_area() const
{
return has_flag( VPFLAG_WHEEL ) ? wheel_info->contact_area : 0;
}
std::vector<std::pair<std::string, int>> vpart_info::wheel_terrain_mod() const
{
const std::vector<std::pair<std::string, int>> null_map;
return has_flag( VPFLAG_WHEEL ) ? wheel_info->terrain_mod : null_map;
}
float vpart_info::wheel_or_rating() const
{
return has_flag( VPFLAG_WHEEL ) ? wheel_info->or_rating : 0.0f;
}
int vpart_info::rotor_diameter() const
{
return has_flag( VPFLAG_ROTOR ) ? rotor_info->rotor_diameter : 0;
}
const std::optional<vpslot_workbench> &vpart_info::get_workbench_info() const
{
return workbench_info;
}
/** @relates string_id */
template<>
const vehicle_prototype &string_id<vehicle_prototype>::obj() const
{
const auto iter = vtypes.find( *this );
if( iter == vtypes.end() ) {
debugmsg( "invalid vehicle prototype id %s", c_str() );
static const vehicle_prototype dummy = {
"",
std::vector<vehicle_prototype::part_def>{},
std::vector<vehicle_item_spawn>{},
nullptr
};
return dummy;
}
return iter->second;
}
/** @relates string_id */
template<>
bool string_id<vehicle_prototype>::is_valid() const
{
return vtypes.count( *this ) > 0;
}
vehicle_prototype::vehicle_prototype() = default;
vehicle_prototype::vehicle_prototype( const std::string &name,
const std::vector<part_def> &parts,
const std::vector<vehicle_item_spawn> &item_spawns,
std::unique_ptr<vehicle> &&blueprint )
: name( name ), parts( parts ), item_spawns( item_spawns ),
blueprint( std::move( blueprint ) )
{
}
vehicle_prototype::vehicle_prototype( vehicle_prototype && ) = default;
vehicle_prototype::~vehicle_prototype() = default;
vehicle_prototype &vehicle_prototype::operator=( vehicle_prototype && ) = default;
/**
*Caches a vehicle definition from a JsonObject to be loaded after itypes is initialized.
*/
void vehicle_prototype::load( const JsonObject &jo )
{
vehicle_prototype &vproto = vtypes[ vproto_id( jo.get_string( "id" ) ) ];
// If there are already parts defined, this vehicle prototype overrides an existing one.
// If the json contains a name, it means a completely new prototype (replacing the
// original one), therefore the old data has to be cleared.
// If the json does not contain a name (the prototype would have no name), it means appending
// to the existing prototype (the parts are not cleared).
if( !vproto.parts.empty() && jo.has_string( "name" ) ) {
vproto = vehicle_prototype();
}
if( vproto.parts.empty() ) {
vproto.name = jo.get_string( "name" );
}
vgroups[vgroup_id( jo.get_string( "id" ) )].add_vehicle( vproto_id( jo.get_string( "id" ) ), 100 );
const auto add_part_obj = [&]( const JsonObject & part, point pos ) {
part_def pt;
pt.pos = pos;
pt.part = vpart_id( part.get_string( "part" ) );
assign( part, "ammo", pt.with_ammo, true, 0, 100 );
assign( part, "ammo_types", pt.ammo_types, true );
assign( part, "ammo_qty", pt.ammo_qty, true, 0 );
assign( part, "fuel", pt.fuel, true );
vproto.parts.push_back( pt );
};
const auto add_part_string = [&]( const std::string & part, point pos ) {
part_def pt;
pt.pos = pos;
pt.part = vpart_id( part );
vproto.parts.push_back( pt );
};
if( jo.has_member( "blueprint" ) ) {