forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.cpp
4826 lines (4300 loc) · 163 KB
/
vehicle.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 "vehicle.h"
#include "ammo.h"
#include "cata_utility.h"
#include "coordinate_conversions.h"
#include "debug.h"
#include "game.h"
#include "item.h"
#include "item_group.h"
#include "itype.h"
#include "json.h"
#include "map.h"
#include "map_iterator.h"
#include "mapbuffer.h"
#include "mapdata.h"
#include "messages.h"
#include "output.h"
#include "overmapbuffer.h"
#include "sounds.h"
#include "string_formatter.h"
#include "submap.h"
#include "translations.h"
#include "veh_interact.h"
#include "veh_type.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "vpart_reference.h"
#include "weather.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
/*
* Speed up all those if ( blarg == "structure" ) statements that are used everywhere;
* assemble "structure" once here instead of repeatedly later.
*/
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_muscle( "muscle" );
static const itype_id fuel_type_plutonium_cell( "plut_cell" );
static const std::string part_location_structure( "structure" );
static const fault_id fault_belt( "fault_engine_belt_drive" );
static const fault_id fault_immobiliser( "fault_engine_immobiliser" );
static const fault_id fault_filter_air( "fault_engine_filter_air" );
static const fault_id fault_filter_fuel( "fault_engine_filter_fuel" );
const skill_id skill_mechanics( "mechanics" );
// 1 kJ per battery charge
const int bat_energy_j = 1000;
inline int modulo( int v, int m );
//
// Point dxs for the adjacent cardinal tiles.
point vehicles::cardinal_d[5] = { point( -1, 0 ), point( 1, 0 ), point( 0, -1 ), point( 0, 1 ), point( 0, 0 ) };
// Vehicle stack methods.
std::list<item>::iterator vehicle_stack::erase( std::list<item>::iterator it )
{
return myorigin->remove_item( part_num, it );
}
void vehicle_stack::push_back( const item &newitem )
{
myorigin->add_item( part_num, newitem );
}
void vehicle_stack::insert_at( std::list<item>::iterator index,
const item &newitem )
{
myorigin->add_item_at( part_num, index, newitem );
}
units::volume vehicle_stack::max_volume() const
{
if( myorigin->part_flag( part_num, "CARGO" ) && myorigin->parts[part_num].is_available() ) {
// Set max volume for vehicle cargo to prevent integer overflow
return std::min( myorigin->parts[part_num].info().size, 10000000_ml );
}
return 0;
}
// Vehicle class methods.
vehicle::vehicle( const vproto_id &type_id, int init_veh_fuel,
int init_veh_status ): type( type_id )
{
turn_dir = 0;
face.init( 0 );
move.init( 0 );
of_turn_carry = 0;
if( !type.str().empty() && type.is_valid() ) {
const vehicle_prototype &proto = type.obj();
// Copy the already made vehicle. The blueprint is created when the json data is loaded
// and is guaranteed to be valid (has valid parts etc.).
*this = *proto.blueprint;
init_state( init_veh_fuel, init_veh_status );
}
precalc_mounts( 0, pivot_rotation[0], pivot_anchor[0] );
refresh();
}
vehicle::vehicle() : vehicle( vproto_id() )
{
smx = 0;
smy = 0;
smz = 0;
}
vehicle::~vehicle() = default;
bool vehicle::player_in_control( player const &p ) const
{
// Debug switch to prevent vehicles from skidding
// without having to place the player in them.
if( tags.count( "IN_CONTROL_OVERRIDE" ) ) {
return true;
}
const optional_vpart_position vp = g->m.veh_at( p.pos() );
if( vp && &vp->vehicle() == this &&
part_with_feature( vp->part_index(), VPFLAG_CONTROLS, false ) >= 0 && p.controlling_vehicle ) {
return true;
}
return remote_controlled( p );
}
bool vehicle::remote_controlled( player const &p ) const
{
vehicle *veh = g->remoteveh();
if( veh != this ) {
return false;
}
for( const vpart_reference &vp : get_avail_parts( "REMOTE_CONTROLS" ) ) {
if( rl_dist( p.pos(), vp.pos() ) <= 40 ) {
return true;
}
}
add_msg( m_bad, _( "Lost connection with the vehicle due to distance!" ) );
g->setremoteveh( nullptr );
return false;
}
/** Checks all parts to see if frames are missing (as they might be when
* loading from a game saved before the vehicle construction rules overhaul). */
void vehicle::add_missing_frames()
{
static const vpart_id frame_id( "frame_vertical" );
//No need to check the same spot more than once
std::set<point> locations_checked;
for( auto &i : parts ) {
if( locations_checked.count( i.mount ) != 0 ) {
continue;
}
locations_checked.insert( i.mount );
bool found = false;
for( auto &elem : parts_at_relative( i.mount, false ) ) {
if( part_info( elem ).location == part_location_structure ) {
found = true;
break;
}
}
if( !found ) {
// Install missing frame
parts.emplace_back( frame_id, i.mount, item( frame_id->item ) );
}
}
}
// Called when loading a vehicle that predates steerable wheels.
// Tries to convert some wheels to steerable versions on the front axle.
void vehicle::add_steerable_wheels()
{
int axle = INT_MIN;
std::vector< std::pair<int, vpart_id> > wheels;
// Find wheels that have steerable versions.
// Convert the wheel(s) with the largest x value.
for( const vpart_reference &vp : get_all_parts() ) {
if( vp.has_feature( "STEERABLE" ) || vp.has_feature( "TRACKED" ) ) {
// Has a wheel that is inherently steerable
// (e.g. unicycle, casters), this vehicle doesn't
// need conversion.
return;
}
if( vp.mount().x < axle ) {
// there is another axle in front of this
continue;
}
if( vp.has_feature( VPFLAG_WHEEL ) ) {
vpart_id steerable_id( vp.info().get_id().str() + "_steerable" );
if( steerable_id.is_valid() ) {
// We can convert this.
if( vp.mount().x != axle ) {
// Found a new axle further forward than the
// existing one.
wheels.clear();
axle = vp.mount().x;
}
wheels.push_back( std::make_pair( static_cast<int>( vp.part_index() ), steerable_id ) );
}
}
}
// Now convert the wheels to their new types.
for( auto &wheel : wheels ) {
parts[ wheel.first ].id = wheel.second;
}
}
void vehicle::init_state( int init_veh_fuel, int init_veh_status )
{
// vehicle parts excluding engines are by default turned off
for( auto &pt : parts ) {
pt.enabled = pt.base.is_engine();
}
bool destroySeats = false;
bool destroyControls = false;
bool destroyTank = false;
bool destroyEngine = false;
bool destroyTires = false;
bool blood_covered = false;
bool blood_inside = false;
bool has_no_key = false;
bool destroyAlarm = false;
// More realistically it should be -5 days old
last_update = 0;
// veh_fuel_multiplier is percentage of fuel
// 0 is empty, 100 is full tank, -1 is random 7% to 35%
int veh_fuel_mult = init_veh_fuel;
if( init_veh_fuel == - 1 ) {
veh_fuel_mult = rng( 1, 7 );
}
if( init_veh_fuel > 100 ) {
veh_fuel_mult = 100;
}
// veh_status is initial vehicle damage
// -1 = light damage (DEFAULT)
// 0 = undamaged
// 1 = disabled, destroyed tires OR engine
int veh_status = -1;
if( init_veh_status == 0 ) {
veh_status = 0;
}
if( init_veh_status == 1 ) {
int rand = rng( 1, 100 );
veh_status = 1;
if( rand <= 5 ) { // seats are destroyed 5%
destroySeats = true;
} else if( rand <= 15 ) { // controls are destroyed 10%
destroyControls = true;
veh_fuel_mult += rng( 0, 7 ); // add 0-7% more fuel if controls are destroyed
} else if( rand <= 23 ) { // battery, minireactor or gasoline tank are destroyed 8%
destroyTank = true;
} else if( rand <= 29 ) { // engine are destroyed 6%
destroyEngine = true;
veh_fuel_mult += rng( 3, 12 ); // add 3-12% more fuel if engine is destroyed
} else if( rand <= 66 ) { // tires are destroyed 37%
destroyTires = true;
veh_fuel_mult += rng( 0, 18 ); // add 0-18% more fuel if tires are destroyed
} else { // vehicle locked 34%
has_no_key = true;
}
}
// if locked, 16% chance something damaged
if( one_in( 6 ) && has_no_key ) {
if( one_in( 3 ) ) {
destroyTank = true;
} else if( one_in( 2 ) ) {
destroyEngine = true;
} else {
destroyTires = true;
}
} else if( !one_in( 3 ) ) {
//most cars should have a destroyed alarm
destroyAlarm = true;
}
//Provide some variety to non-mint vehicles
if( veh_status != 0 ) {
//Leave engine running in some vehicles, if the engine has not been destroyed
if( veh_fuel_mult > 0 && !empty( get_avail_parts( "ENGINE" ) ) &&
one_in( 8 ) && !destroyEngine && !has_no_key && has_engine_type_not( fuel_type_muscle, true ) ) {
engine_on = true;
}
auto light_head = one_in( 20 );
auto light_dome = one_in( 16 );
auto light_aisle = one_in( 8 );
auto light_overh = one_in( 4 );
auto light_atom = one_in( 2 );
for( auto &pt : parts ) {
if( pt.has_flag( VPFLAG_CONE_LIGHT ) ) {
pt.enabled = light_head;
} else if( pt.has_flag( VPFLAG_DOME_LIGHT ) ) {
pt.enabled = light_dome;
} else if( pt.has_flag( VPFLAG_AISLE_LIGHT ) ) {
pt.enabled = light_aisle;
} else if( pt.has_flag( VPFLAG_CIRCLE_LIGHT ) ) {
pt.enabled = light_overh;
} else if( pt.has_flag( VPFLAG_ATOMIC_LIGHT ) ) {
pt.enabled = light_atom;
}
}
if( one_in( 10 ) ) {
blood_covered = true;
}
if( one_in( 8 ) ) {
blood_inside = true;
}
for( const vpart_reference &vp : get_parts_including_carried( "FRIDGE" ) ) {
vp.part().enabled = true;
}
for( const vpart_reference &vp : get_parts_including_carried( "FREEZER" ) ) {
vp.part().enabled = true;
}
for( const vpart_reference &vp : get_parts_including_carried( "WATER_PURIFIER" ) ) {
vp.part().enabled = true;
}
}
cata::optional<point> blood_inside_pos;
for( const vpart_reference &vp : get_all_parts() ) {
const size_t p = vp.part_index();
vehicle_part &pt = vp.part();
if( vp.has_feature( "REACTOR" ) ) {
// De-hardcoded reactors. Should always start active
pt.enabled = true;
}
if( pt.is_battery() ) {
if( veh_fuel_mult == 100 ) { // Mint condition vehicle
pt.ammo_set( "battery", pt.ammo_capacity() );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) { // Randomize battery ammo a bit
pt.ammo_set( "battery", pt.ammo_capacity() * ( veh_fuel_mult + rng( 0, 10 ) ) / 100 );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) {
pt.ammo_set( "battery", pt.ammo_capacity() * ( veh_fuel_mult - rng( 0, 10 ) ) / 100 );
} else {
pt.ammo_set( "battery", pt.ammo_capacity() * veh_fuel_mult / 100 );
}
}
if( pt.is_tank() && type->parts[p].fuel != "null" ) {
int qty = pt.ammo_capacity() * veh_fuel_mult / 100;
qty *= std::max( item::find_type( type->parts[p].fuel )->stack_size, 1 );
qty /= to_milliliter( units::legacy_volume_factor );
pt.ammo_set( type->parts[ p ].fuel, qty );
} else if( pt.is_fuel_store() && type->parts[p].fuel != "null" ) {
int qty = pt.ammo_capacity() * veh_fuel_mult / 100;
pt.ammo_set( type->parts[ p ].fuel, qty );
}
if( vp.has_feature( "OPENABLE" ) ) { // doors are closed
if( !pt.open && one_in( 4 ) ) {
open( p );
}
}
if( vp.has_feature( "BOARDABLE" ) ) { // no passengers
pt.remove_flag( vehicle_part::passenger_flag );
}
// initial vehicle damage
if( veh_status == 0 ) {
// Completely mint condition vehicle
set_hp( pt, vp.info().durability );
} else {
//a bit of initial damage :)
//clamp 4d8 to the range of [8,20]. 8=broken, 20=undamaged.
int broken = 8;
int unhurt = 20;
int roll = dice( 4, 8 );
if( roll < unhurt ) {
if( roll <= broken ) {
set_hp( pt, 0 );
pt.ammo_unset(); //empty broken batteries and fuel tanks
} else {
set_hp( pt, ( roll - broken ) / double( unhurt - broken ) * vp.info().durability );
}
} else {
set_hp( pt, vp.info().durability );
}
if( vp.has_feature( VPFLAG_ENGINE ) ) {
// If possible set an engine fault rather than destroying the engine outright
if( destroyEngine && pt.faults_potential().empty() ) {
set_hp( pt, 0 );
} else if( destroyEngine || one_in( 3 ) ) {
do {
pt.fault_set( random_entry( pt.faults_potential() ) );
} while( one_in( 3 ) );
}
} else if( ( destroySeats && ( vp.has_feature( "SEAT" ) || vp.has_feature( "SEATBELT" ) ) ) ||
( destroyControls && ( vp.has_feature( "CONTROLS" ) || vp.has_feature( "SECURITY" ) ) ) ||
( destroyAlarm && vp.has_feature( "SECURITY" ) ) ) {
set_hp( pt, 0 );
}
// Fuel tanks should be emptied as well
if( destroyTank && pt.is_fuel_store() ) {
set_hp( pt, 0 );
pt.ammo_unset();
}
//Solar panels have 25% of being destroyed
if( vp.has_feature( "SOLAR_PANEL" ) && one_in( 4 ) ) {
set_hp( pt, 0 );
}
/* Bloodsplatter the front-end parts. Assume anything with x > 0 is
* the "front" of the vehicle (since the driver's seat is at (0, 0).
* We'll be generous with the blood, since some may disappear before
* the player gets a chance to see the vehicle. */
if( blood_covered && vp.mount().x > 0 ) {
if( one_in( 3 ) ) {
//Loads of blood. (200 = completely red vehicle part)
pt.blood = rng( 200, 600 );
} else {
//Some blood
pt.blood = rng( 50, 200 );
}
}
if( blood_inside ) {
// blood is splattered around (blood_inside_pos),
// coordinates relative to mount point; the center is always a seat
if( blood_inside_pos ) {
const int distSq = std::pow( blood_inside_pos->x - vp.mount().x, 2 ) +
std::pow( blood_inside_pos->y - vp.mount().y, 2 );
if( distSq <= 1 ) {
pt.blood = rng( 200, 400 ) - distSq * 100;
}
} else if( vp.has_feature( "SEAT" ) ) {
// Set the center of the bloody mess inside
blood_inside_pos.emplace( vp.mount() );
}
}
}
//sets the vehicle to locked, if there is no key and an alarm part exists
if( vp.has_feature( "SECURITY" ) && has_no_key && pt.is_available() ) {
is_locked = true;
if( one_in( 2 ) ) {
// if vehicle has immobilizer 50% chance to add additional fault
pt.fault_set( fault_immobiliser );
}
}
}
// destroy tires until the vehicle is not drivable
if( destroyTires && !wheelcache.empty() ) {
int tries = 0;
while( valid_wheel_config( false ) && tries < 100 ) {
// wheel config is still valid, destroy the tire.
set_hp( parts[random_entry( wheelcache )], 0 );
tries++;
}
}
invalidate_mass();
}
/**
* Smashes up a vehicle that has already been placed; used for generating
* very damaged vehicles. Additionally, any spot where two vehicles overlapped
* (ie, any spot with multiple frames) will be completely destroyed, as that
* was the collision point.
*/
void vehicle::smash( float hp_percent_loss_min, float hp_percent_loss_max,
float percent_of_parts_to_affect, point damage_origin, float damage_size )
{
for( auto &part : parts ) {
//Skip any parts already mashed up or removed.
if( part.is_broken() || part.removed ) {
continue;
}
std::vector<int> parts_in_square = parts_at_relative( part.mount, true );
int structures_found = 0;
for( auto &square_part_index : parts_in_square ) {
if( part_info( square_part_index ).location == part_location_structure ) {
structures_found++;
}
}
if( structures_found > 1 ) {
//Destroy everything in the square
for( int idx : parts_in_square ) {
mod_hp( parts[ idx ], 0 - parts[ idx ].hp(), DT_BASH );
parts[ idx ].ammo_unset();
}
continue;
}
int roll = dice( 1, 1000 );
int pct_af = ( percent_of_parts_to_affect * 1000.0f );
if( roll < pct_af ) {
float dist = 1.0f - trig_dist( damage_origin.x, damage_origin.y, part.precalc[0].x,
part.precalc[0].y ) / damage_size;
dist = clamp( dist, 0.0f, 1.0f );
if( damage_size == 0 ) {
dist = 1.0f;
}
//Everywhere else, drop by 10-120% of max HP (anything over 100 = broken)
if( mod_hp( part, 0 - ( rng_float( hp_percent_loss_min * dist,
hp_percent_loss_max * dist ) * part.info().durability ), DT_BASH ) ) {
part.ammo_unset();
}
}
}
// clear out any duplicated locations
for( int p = static_cast<int>( parts.size() ) - 1; p >= 0; p-- ) {
vehicle_part &part = parts[ p ];
if( part.removed ) {
continue;
}
std::vector<int> parts_here = parts_at_relative( part.mount, true );
for( int other_i = static_cast<int>( parts_here.size() ) - 1; other_i >= 0; other_i -- ) {
int other_p = parts_here[ other_i ];
if( p == other_p ) {
continue;
}
if( ( part_info( p ).location.empty() &&
part_info( p ).get_id() == part_info( other_p ).get_id() ) ||
( part_info( p ).location == part_info( other_p ).location ) ) {
remove_part( other_p );
}
}
}
}
int vehicle::lift_strength() const
{
units::mass mass = total_mass();
return std::max( mass / 10000_gram, 1 );
}
void vehicle::toggle_specific_engine( int e, bool on )
{
toggle_specific_part( engines[e], on );
}
void vehicle::toggle_specific_part( int p, bool on )
{
parts[p].enabled = on;
}
bool vehicle::is_engine_type_on( int e, const itype_id &ft ) const
{
return is_engine_on( e ) && is_engine_type( e, ft );
}
bool vehicle::has_engine_type( const itype_id &ft, bool const enabled ) const
{
for( size_t e = 0; e < engines.size(); ++e ) {
if( is_engine_type( e, ft ) && ( !enabled || is_engine_on( e ) ) ) {
return true;
}
}
return false;
}
bool vehicle::has_engine_type_not( const itype_id &ft, bool const enabled ) const
{
for( size_t e = 0; e < engines.size(); ++e ) {
if( !is_engine_type( e, ft ) && ( !enabled || is_engine_on( e ) ) ) {
return true;
}
}
return false;
}
bool vehicle::has_engine_conflict( const vpart_info *possible_conflict,
std::string &conflict_type ) const
{
std::vector<std::string> new_excludes = possible_conflict->engine_excludes();
// skip expensive string comparisons if there are no exclusions
if( new_excludes.empty() ) {
return false;
}
bool has_conflict = false;
for( size_t e = 0; e < engines.size(); ++e ) {
std::vector<std::string> install_excludes = part_info( engines[e] ).engine_excludes();
std::vector<std::string> conflicts;
std::set_intersection( new_excludes.begin(), new_excludes.end(), install_excludes.begin(),
install_excludes.end(), back_inserter( conflicts ) );
if( !conflicts.empty() ) {
has_conflict = true;
conflict_type = conflicts.front();
break;
}
}
return has_conflict;
}
bool vehicle::is_engine_type( const int e, const itype_id &ft ) const
{
return part_info( engines[e] ).fuel_type == ft;
}
bool vehicle::is_perpetual_type( const int e ) const
{
const itype_id &ft = part_info( engines[e] ).fuel_type;
return item( ft ).has_flag( "PERPETUAL" );
}
bool vehicle::is_engine_on( int const e ) const
{
return parts[ engines[ e ] ].is_available() && is_part_on( engines[ e ] );
}
bool vehicle::is_part_on( int const p ) const
{
return parts[p].enabled;
}
bool vehicle::is_alternator_on( int const a ) const
{
auto alt = parts[ alternators [ a ] ];
if( alt.is_unavailable() ) {
return false;
}
return std::any_of( engines.begin(), engines.end(), [this, &alt]( int idx ) {
auto &eng = parts [ idx ];
return eng.enabled && eng.is_available() && eng.mount == alt.mount &&
!eng.faults().count( fault_belt );
} );
}
bool vehicle::has_security_working() const
{
bool found_security = false;
for( size_t s = 0; s < speciality.size(); s++ ) {
if( part_flag( speciality[ s ], "SECURITY" ) && parts[ speciality[ s ] ].is_available() ) {
found_security = true;
break;
}
}
return found_security;
}
void vehicle::backfire( const int e ) const
{
const int power = part_vpower_w( engines[e], true );
const tripoint pos = global_part_pos3( engines[e] );
//~ backfire sound
sounds::ambient_sound( pos, 40 + power / 12, _( "BANG!" ) );
}
const vpart_info &vehicle::part_info( int index, bool include_removed ) const
{
if( index < static_cast<int>( parts.size() ) ) {
if( !parts[index].removed || include_removed ) {
return parts[index].info();
}
}
return vpart_id::NULL_ID().obj();
}
// engines & alternators all have power.
// engines provide, whilst alternators consume.
int vehicle::part_vpower_w( int const index, bool const at_full_hp ) const
{
const vehicle_part &vp = parts[ index ];
int pwr = vp.info().power;
if( part_flag( index, VPFLAG_ENGINE ) ) {
if( pwr == 0 ) {
pwr = vhp_to_watts( vp.base.engine_displacement() );
}
///\EFFECT_STR increases power produced for MUSCLE_* vehicles
pwr += ( g->u.str_cur - 8 ) * part_info( index ).engine_muscle_power_factor();
}
if( pwr < 0 ) {
return pwr; // Consumers always draw full power, even if broken
}
if( at_full_hp ) {
return pwr; // Assume full hp
}
// Damaged engines give less power, but some engines handle it better
double health = parts[index].health_percent();
// dpf is 0 for engines that scale power linearly with damage and
// provides a floor otherwise
float dpf = part_info( index ).engine_damaged_power_factor();
double effective_percent = dpf + ( ( 1 - dpf ) * health );
return static_cast<int>( pwr * effective_percent );
}
// alternators, solar panels, reactors, and accessories all have epower.
// alternators, solar panels, and reactors provide, whilst accessories consume.
// for motor consumption see @ref vpart_info::energy_consumption instead
int vehicle::part_epower_w( int const index ) const
{
int e = part_info( index ).epower;
if( e < 0 ) {
return e; // Consumers always draw full power, even if broken
}
return e * parts[ index ].health_percent();
}
int vehicle::power_to_energy_bat( const int power_w, const time_duration t ) const
{
// Integrate constant epower (watts) over time to get units of battery energy
int seconds = static_cast<int>( to_minutes<double>( t ) * 60 );
int energy_j = power_w * seconds;
int energy_bat = energy_j / bat_energy_j;
int sign = power_w >= 0 ? 1 : -1;
// energy_bat remainder results in chance at additional charge/discharge
energy_bat += x_in_y( abs( energy_j % bat_energy_j ), bat_energy_j ) ? sign : 0;
return energy_bat;
}
int vehicle::vhp_to_watts( int const power_vhp )
{
// Convert vhp units (0.5 HP ) to watts
// Used primarily for calculating battery charge/discharge
// TODO: convert batteries to use energy units based on watts (watt-ticks?)
constexpr int conversion_factor = 373; // 373 watts == 1 power_vhp == 0.5 HP
return power_vhp * conversion_factor;
}
bool vehicle::has_structural_part( const point dp ) const
{
for( const int elem : parts_at_relative( dp, false ) ) {
if( part_info( elem ).location == part_location_structure &&
!part_info( elem ).has_flag( "PROTRUSION" ) ) {
return true;
}
}
return false;
}
/**
* Returns whether or not the vehicle has a structural part queued for removal,
* @return true if a structural is queue for removal, false if not.
* */
bool vehicle::is_structural_part_removed() const
{
for( const vpart_reference &vp : get_all_parts() ) {
if( vp.part().removed && vp.info().location == part_location_structure ) {
return true;
}
}
return false;
}
/**
* Returns whether or not the vehicle part with the given id can be mounted in
* the specified square.
* @param dp The local coordinate to mount in.
* @param id The id of the part to install.
* @return true if the part can be mounted, false if not.
*/
bool vehicle::can_mount( const point dp, const vpart_id &id ) const
{
//The part has to actually exist.
if( !id.is_valid() ) {
return false;
}
//It also has to be a real part, not the null part
const vpart_info &part = id.obj();
if( part.has_flag( "NOINSTALL" ) ) {
return false;
}
const std::vector<int> parts_in_square = parts_at_relative( dp, false );
//First part in an empty square MUST be a structural part
if( parts_in_square.empty() && part.location != part_location_structure ) {
return false;
}
//No other part can be placed on a protrusion
if( !parts_in_square.empty() && part_info( parts_in_square[0] ).has_flag( "PROTRUSION" ) ) {
return false;
}
//No part type can stack with itself, or any other part in the same slot
for( const auto &elem : parts_in_square ) {
const vpart_info &other_part = parts[elem].info();
//Parts with no location can stack with each other (but not themselves)
if( part.get_id() == other_part.get_id() ||
( !part.location.empty() && part.location == other_part.location ) ) {
return false;
}
// Until we have an interface for handling multiple components with CARGO space,
// exclude them from being mounted in the same tile.
if( part.has_flag( "CARGO" ) && other_part.has_flag( "CARGO" ) ) {
return false;
}
}
// All parts after the first must be installed on or next to an existing part
// the exception is when a single tile only structural object is being repaired
if( !parts.empty() ) {
if( !is_structural_part_removed() &&
!has_structural_part( dp ) &&
!has_structural_part( dp + point( +1, 0 ) ) &&
!has_structural_part( dp + point( 0, +1 ) ) &&
!has_structural_part( dp + point( -1, 0 ) ) &&
!has_structural_part( dp + point( 0, -1 ) ) ) {
return false;
}
}
// only one exclusive engine allowed
std::string empty;
if( has_engine_conflict( &part, empty ) ) {
return false;
}
// Alternators must be installed on a gas engine
if( part.has_flag( VPFLAG_ALTERNATOR ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "E_ALTERNATOR" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
//Seatbelts must be installed on a seat
if( part.has_flag( "SEATBELT" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "BELTABLE" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
//Internal must be installed into a cargo area.
if( part.has_flag( "INTERNAL" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "CARGO" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
// curtains must be installed on (reinforced)windshields
// TODO: do this automatically using "location":"on_mountpoint"
if( part.has_flag( "WINDOW_CURTAIN" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "WINDOW" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
// Security system must be installed on controls
if( part.has_flag( "ON_CONTROLS" ) ) {
bool anchor_found = false;
for( std::vector<int>::const_iterator it = parts_in_square.begin();
it != parts_in_square.end(); ++it ) {
if( part_info( *it ).has_flag( "CONTROLS" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
// Cargo locks must go on lockable cargo containers
// TODO: do this automatically using "location":"on_mountpoint"
if( part.has_flag( "CARGO_LOCKING" ) ) {
bool anchor_found = false;
for( std::vector<int>::const_iterator it = parts_in_square.begin();
it != parts_in_square.end(); ++it ) {
if( part_info( *it ).has_flag( "LOCKABLE_CARGO" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
//Swappable storage battery must be installed on a BATTERY_MOUNT
if( part.has_flag( "NEEDS_BATTERY_MOUNT" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "BATTERY_MOUNT" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
//Door motors need OPENABLE
if( part.has_flag( "DOOR_MOTOR" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "OPENABLE" ) ) {
anchor_found = true;
}
}
if( !anchor_found ) {
return false;
}
}
//Mirrors cannot be mounted on OPAQUE parts
if( part.has_flag( "VISION" ) && !part.has_flag( "CAMERA" ) ) {
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "OPAQUE" ) ) {
return false;
}
}
}
//and vice versa
if( part.has_flag( "OPAQUE" ) ) {
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "VISION" ) &&
!part_info( elem ).has_flag( "CAMERA" ) ) {
return false;
}
}
}
//Turrets must be installed on a turret mount
if( part.has_flag( "TURRET" ) ) {
bool anchor_found = false;
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "TURRET_MOUNT" ) ) {
anchor_found = true;
break;
}
}
if( !anchor_found ) {
return false;
}
}
//Turret mounts must NOT be installed on other (moded) turret mounts
if( part.has_flag( "TURRET_MOUNT" ) ) {
for( const auto &elem : parts_in_square ) {
if( part_info( elem ).has_flag( "TURRET_MOUNT" ) ) {
return false;
}
}
}
//Anything not explicitly denied is permitted
return true;
}
bool vehicle::can_unmount( int const p ) const
{
std::string no_reason;
return can_unmount( p, no_reason );
}