forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle.cpp
7170 lines (6447 loc) · 253 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" // IWYU pragma: associated
#include "vpart_position.h" // IWYU pragma: associated
#include "vpart_range.h" // IWYU pragma: associated
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <list>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include "active_tile_data_def.h"
#include "avatar.h"
#include "avatar_functions.h"
#include "bionics.h"
#include "cata_utility.h"
#include "character.h"
#include "clzones.h"
#include "colony.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "creature.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "distribution_grid.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "explosion.h"
#include "faction.h"
#include "field_type.h"
#include "flag.h"
#include "game.h"
#include "item.h"
#include "item_contents.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 "math_defines.h"
#include "messages.h"
#include "monster.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "player.h"
#include "player_activity.h"
#include "point_float.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "submap.h"
#include "translations.h"
#include "units_utility.h"
#include "veh_type.h"
#include "vehicle_move.h"
#include "vehicle_selector.h"
#include "weather.h"
#include "weather_gen.h"
/*
* Speed up all those if ( blarg == "structure" ) statements that are used everywhere;
* assemble "structure" once here instead of repeatedly later.
*/
static const std::string part_location_structure( "structure" );
static const std::string part_location_center( "center" );
static const std::string part_location_onroof( "on_roof" );
static const itype_id fuel_type_animal( "animal" );
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 itype_id fuel_type_wind( "wind" );
static const fault_id fault_belt( "fault_engine_belt_drive" );
static const fault_id fault_filter_air( "fault_engine_filter_air" );
static const fault_id fault_filter_fuel( "fault_engine_filter_fuel" );
static const fault_id fault_immobiliser( "fault_engine_immobiliser" );
static const activity_id ACT_VEHICLE( "ACT_VEHICLE" );
static const bionic_id bio_jointservo( "bio_jointservo" );
static const efftype_id effect_harnessed( "harnessed" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_plut_cell( "plut_cell" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_clean( "water_clean" );
static const itype_id itype_water_purifier( "water_purifier" );
static const std::string flag_PERPETUAL( "PERPETUAL" );
static bool is_sm_tile_outside( const tripoint &real_global_pos );
static bool is_sm_tile_over_water( const tripoint &real_global_pos );
static const itype_id fuel_type_mana( "mana" );
// 1 kJ per battery charge
const int bat_energy_j = 1000;
// For reference what each function is supposed to do, see their implementation in
// @ref DefaultRemovePartHandler. Add compatible code for it into @ref MapgenRemovePartHandler,
// if needed.
class RemovePartHandler
{
public:
virtual ~RemovePartHandler() = default;
virtual void unboard( const tripoint &loc ) = 0;
virtual void add_item_or_charges( const tripoint &loc, item it, bool permit_oob ) = 0;
virtual void set_transparency_cache_dirty( int z ) = 0;
virtual void set_floor_cache_dirty( int z ) = 0;
virtual void removed( vehicle &veh, int part ) = 0;
virtual void spawn_animal_from_part( item &base, const tripoint &loc ) = 0;
};
class DefaultRemovePartHandler : public RemovePartHandler
{
public:
~DefaultRemovePartHandler() override = default;
void unboard( const tripoint &loc ) override {
g->m.unboard_vehicle( loc );
}
void add_item_or_charges( const tripoint &loc, item it, bool /*permit_oob*/ ) override {
g->m.add_item_or_charges( loc, std::move( it ) );
}
void set_transparency_cache_dirty( const int z ) override {
map &here = get_map();
here.set_transparency_cache_dirty( z );
here.set_seen_cache_dirty( tripoint_zero );
}
void set_floor_cache_dirty( const int z ) override {
get_map().set_floor_cache_dirty( z );
}
void removed( vehicle &veh, const int part ) override {
avatar &player_character = get_avatar();
// If the player is currently working on the removed part, stop them as it's futile now.
const player_activity &act = player_character.activity;
map &here = get_map();
if( act.id() == ACT_VEHICLE && act.moves_left > 0 && act.values.size() > 6 ) {
if( veh_pointer_or_null( here.veh_at( tripoint( act.values[0], act.values[1],
player_character.posz() ) ) ) == &veh ) {
if( act.values[6] >= part ) {
player_character.cancel_activity();
add_msg( m_info, _( "The vehicle part you were working on has gone!" ) );
}
}
}
// TODO: maybe do this for all the nearby NPCs as well?
if( g->u.get_grab_type() == OBJECT_VEHICLE && g->u.grab_point == veh.global_part_pos3( part ) ) {
if( veh.parts_at_relative( veh.part( part ).mount, false ).empty() ) {
add_msg( m_info, _( "The vehicle part you were holding has been destroyed!" ) );
g->u.grab( OBJECT_NONE );
}
}
here.dirty_vehicle_list.insert( &veh );
}
void spawn_animal_from_part( item &base, const tripoint &loc ) override {
base.release_monster( loc, 1 );
}
};
class MapgenRemovePartHandler : public RemovePartHandler
{
private:
map &m;
public:
MapgenRemovePartHandler( map &m ) : m( m ) { }
~MapgenRemovePartHandler() override = default;
void unboard( const tripoint &/*loc*/ ) override {
debugmsg( "Tried to unboard during mapgen!" );
// Ignored. Will almost certainly not be called anyway, because
// there are no creatures that could have been mounted during mapgen.
}
void add_item_or_charges( const tripoint &loc, item it, bool permit_oob ) override {
if( !m.inbounds( loc ) ) {
if( !permit_oob ) {
debugmsg( "Tried to put item %s on invalid tile %s during mapgen!",
it.tname(), loc.to_string() );
}
tripoint copy = loc;
m.clip_to_bounds( copy );
assert( m.inbounds( copy ) ); // prevent infinite recursion
add_item_or_charges( copy, std::move( it ), false );
return;
}
m.add_item_or_charges( loc, std::move( it ) );
}
void set_transparency_cache_dirty( const int /*z*/ ) override {
// Ignored for now. We don't initialize the transparency cache in mapgen anyway.
}
void set_floor_cache_dirty( const int /*z*/ ) override {
// Ignored for now. We don't initialize the floor cache in mapgen anyway.
}
void removed( vehicle &veh, const int /*part*/ ) override {
// TODO: check if this is necessary, it probably isn't during mapgen
m.dirty_vehicle_list.insert( &veh );
}
void spawn_animal_from_part( item &/*base*/, const tripoint &/*loc*/ ) override {
debugmsg( "Tried to spawn animal from vehicle part during mapgen!" );
// Ignored. The base item will not be changed and will spawn as is:
// still containing the animal.
// This should not happend during mapgen anyway.
// TODO: *if* this actually happens: create a spawn point for the animal instead.
}
};
// Vehicle stack methods.
vehicle_stack::iterator vehicle_stack::erase( vehicle_stack::const_iterator it )
{
return myorigin->remove_item( part_num, it );
}
void vehicle_stack::insert( const item &newitem )
{
myorigin->add_item( part_num, newitem );
}
units::volume vehicle_stack::max_volume() const
{
if( myorigin->part_flag( part_num, "CARGO" ) && !myorigin->part( part_num ).is_broken() ) {
// Set max volume for vehicle cargo to prevent integer overflow
return std::min( myorigin->part( part_num ).info().size, 10000_liter );
}
return 0_ml;
}
// Vehicle class methods.
vehicle::vehicle( const vproto_id &type_id, int init_veh_fuel,
int init_veh_status ): type( type_id )
{
turn_dir = 0_degrees;
face.init( 0_degrees );
move.init( 0_degrees );
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() )
{
sm_pos = tripoint_zero;
}
vehicle::~vehicle() = default;
bool vehicle::player_in_control( const Character &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(), "CONTROL_ANIMAL", true ) >= 0 &&
has_engine_type( fuel_type_animal, false ) && has_harnessed_animal() ) ||
( part_with_feature( vp->part_index(), VPFLAG_CONTROLS, false ) >= 0 ) ) &&
p.controlling_vehicle ) {
return true;
}
return remote_controlled( p );
}
bool vehicle::remote_controlled( const Character &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 = calendar::start_of_cataclysm;
// 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
//chance decays from 1 in 4 vehicles on day 0 to 1 in (day + 4) in the future.
int current_day = std::max( to_days<int>( calendar::turn - calendar::turn_zero ), 0 );
if( veh_fuel_mult > 0 && !empty( get_avail_parts( "ENGINE" ) ) &&
one_in( current_day + 4 ) && !destroyEngine && !has_no_key &&
has_engine_type_not( fuel_type_muscle, true ) ) {
engine_on = true;
}
auto light_head = one_in( 20 );
auto light_whead = one_in( 20 ); // wide-angle headlight
auto light_dome = one_in( 16 );
auto light_aisle = one_in( 8 );
auto light_hoverh = one_in( 4 ); // half circle overhead light
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_WIDE_CONE_LIGHT ) ) {
pt.enabled = light_whead;
} 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_HALF_CIRCLE_LIGHT ) ) {
pt.enabled = light_hoverh;
} 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;
}
}
std::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( VPFLAG_REACTOR ) ) {
// De-hardcoded reactors. Should always start active
pt.enabled = true;
}
if( pt.is_reactor() ) {
if( veh_fuel_mult == 100 ) { // Mint condition vehicle
pt.ammo_set( itype_plut_cell, pt.ammo_capacity() );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) { // Randomize charge a bit
pt.ammo_set( itype_plut_cell, pt.ammo_capacity() * ( veh_fuel_mult + rng( 0, 10 ) ) / 100 );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) {
pt.ammo_set( itype_plut_cell, pt.ammo_capacity() * ( veh_fuel_mult - rng( 0, 10 ) ) / 100 );
} else {
pt.ammo_set( itype_plut_cell, pt.ammo_capacity() * veh_fuel_mult / 100 );
}
}
if( pt.is_battery() ) {
if( veh_fuel_mult == 100 ) { // Mint condition vehicle
pt.ammo_set( itype_battery, pt.ammo_capacity() );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) { // Randomize battery ammo a bit
pt.ammo_set( itype_battery, pt.ammo_capacity() * ( veh_fuel_mult + rng( 0, 10 ) ) / 100 );
} else if( one_in( 2 ) && veh_fuel_mult > 0 ) {
pt.ammo_set( itype_battery, pt.ammo_capacity() * ( veh_fuel_mult - rng( 0, 10 ) ) / 100 );
} else {
pt.ammo_set( itype_battery, pt.ammo_capacity() * veh_fuel_mult / 100 );
}
}
if( pt.is_tank() && !type->parts[p].fuel.is_null() ) {
int qty = pt.ammo_capacity() * veh_fuel_mult / 100;
qty *= std::max( 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.is_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 ) / static_cast<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() && tries < 100 ) {
// wheel config is still valid, destroy the tire.
set_hp( parts[random_entry( wheelcache )], 0 );
tries++;
}
}
invalidate_mass();
}
void vehicle::activate_magical_follow()
{
for( vehicle_part &vp : parts ) {
if( vp.info().fuel_type == fuel_type_mana ) {
vp.enabled = true;
is_following = true;
engine_on = true;
} else {
vp.enabled = true;
}
}
refresh();
}
void vehicle::activate_animal_follow()
{
for( size_t e = 0; e < parts.size(); e++ ) {
vehicle_part &vp = parts[ e ];
if( vp.info().fuel_type == fuel_type_animal ) {
monster *mon = get_pet( e );
if( mon && mon->has_effect( effect_harnessed ) ) {
vp.enabled = true;
is_following = true;
engine_on = true;
}
} else {
vp.enabled = true;
}
}
refresh();
}
void vehicle::autopilot_patrol()
{
/** choose one single zone ( multiple zones too complex for now )
* choose a point at the far edge of the zone
* the edge chosen is the edge that is smaller, therefore the longer side
* of the rectangle is the one the vehicle drives mostly parallel too.
* if its perfect square then choose a point that is on any edge that the
* vehicle is not currently at
* drive to that point.
* then once arrived, choose a random opposite point of the zone.
* this should ( in a simple fashion ) cause a patrolling behavior
* in a criss-cross fashion.
* in an auto-tractor, this would eventually cover the entire rectangle.
*/
// if we are close to a waypoint, then return to come back to this function next turn.
if( autodrive_local_target != tripoint_zero ) {
if( rl_dist( global_square_location().raw(), autodrive_local_target ) <= 3 ) {
autodrive_local_target = tripoint_zero;
return;
}
if( !g->m.inbounds( g->m.getlocal( autodrive_local_target ) ) ) {
autodrive_local_target = tripoint_zero;
is_patrolling = false;
return;
}
drive_to_local_target( autodrive_local_target, false );
return;
}
zone_manager &mgr = zone_manager::get_manager();
const auto &zone_src_set = mgr.get_near( zone_type_id( "VEHICLE_PATROL" ),
global_square_location().raw(), 60 );
if( zone_src_set.empty() ) {
is_patrolling = false;
return;
}
// get corners.
tripoint min;
tripoint max;
for( const tripoint &box : zone_src_set ) {
if( min == tripoint_zero ) {
min = box;
max = box;
continue;
}
min.x = std::min( box.x, min.x );
min.y = std::min( box.y, min.y );
min.z = std::min( box.z, min.z );
max.x = std::max( box.x, max.x );
max.y = std::max( box.y, max.y );
max.z = std::max( box.z, max.z );
}
const bool x_side = ( max.x - min.x ) < ( max.y - min.y );
const int point_along = x_side ? rng( min.x, max.x ) : rng( min.y, max.y );
const tripoint max_tri = x_side ? tripoint( point_along, max.y, min.z ) : tripoint( max.x,
point_along,
min.z );
const tripoint min_tri = x_side ? tripoint( point_along, min.y, min.z ) : tripoint( min.x,
point_along,
min.z );
tripoint chosen_tri = min_tri;
if( rl_dist( max_tri, global_square_location().raw() ) >= rl_dist( min_tri,
global_square_location().raw() ) ) {
chosen_tri = max_tri;
}
autodrive_local_target = chosen_tri;
drive_to_local_target( autodrive_local_target, false );
}
std::set<point> vehicle::immediate_path( units::angle rotate )
{
std::set<point> points_to_check;
const int distance_to_check = 10 + ( velocity / 800 );
units::angle adjusted_angle = normalize( face.dir() + rotate );
// clamp to multiples of 15.
adjusted_angle = round_to_multiple_of( adjusted_angle, 15_degrees );
tileray collision_vector;
collision_vector.init( adjusted_angle );
point top_left_actual = global_pos3().xy() + coord_translate( front_left );
point top_right_actual = global_pos3().xy() + coord_translate( front_right );
std::vector<point> front_row = line_to( g->m.getabs( top_left_actual ),
g->m.getabs( top_right_actual ) );
for( point elem : front_row ) {
for( int i = 0; i < distance_to_check; ++i ) {
collision_vector.advance( i );
point point_to_add = elem + point( collision_vector.dx(), collision_vector.dy() );
points_to_check.emplace( point_to_add );
}
}
collision_check_points = points_to_check;
return points_to_check;
}
static int get_turn_from_angle( const units::angle angle, const tripoint &vehpos,
const tripoint &target, bool reverse = false )
{
if( angle > 10.0_degrees && angle <= 45.0_degrees ) {
return reverse ? 4 : 1;
} else if( angle > 45.0_degrees && angle <= 90.0_degrees ) {
return 3;
} else if( angle > 90.0_degrees && angle < 180.0_degrees ) {
return reverse ? 1 : 4;
} else if( angle < -10.0_degrees && angle >= -45.0_degrees ) {
return reverse ? -4 : -1;
} else if( angle < -45.0_degrees && angle >= -90.0_degrees ) {
return -3;
} else if( angle < -90.0_degrees && angle > -180.0_degrees ) {
return reverse ? -1 : -4;
// edge case of being exactly on the button for the target.
// just keep driving, the next path point will be picked up.
} else if( ( angle == 180_degrees || angle == -180_degrees ) && vehpos == target ) {
return 0;
}
return 0;
}
void vehicle::drive_to_local_target( const tripoint &target, bool follow_protocol )
{
if( follow_protocol && g->u.in_vehicle ) {
stop_autodriving();
return;
}
refresh();
tripoint vehpos = global_square_location().raw();
units::angle angle = get_angle_from_targ( target );
// now we got the angle to the target, we can work out when we are heading towards disaster.
// Check the tileray in the direction we need to head towards.
std::set<point> points_to_check = immediate_path( angle );
bool stop = false;
for( point pt_elem : points_to_check ) {
point elem = g->m.getlocal( pt_elem );
if( stop ) {
break;
}
const optional_vpart_position ovp = g->m.veh_at( tripoint( elem, sm_pos.z ) );
if( g->m.impassable_ter_furn( tripoint( elem, sm_pos.z ) ) || ( ovp &&
&ovp->vehicle() != this ) ) {
stop = true;
break;
}
if( elem == g->u.pos().xy() ) {
if( follow_protocol || g->u.in_vehicle ) {
continue;
} else {
stop = true;
break;
}
}
bool its_a_pet = false;
if( g->critter_at( tripoint( elem, sm_pos.z ) ) ) {
npc *guy = g->critter_at<npc>( tripoint( elem, sm_pos.z ) );
if( guy && !guy->in_vehicle ) {
stop = true;
break;
}
for( const auto &p : parts ) {
monster *mon = get_pet( index_of_part( &p ) );
if( mon && mon->pos().xy() == elem ) {
its_a_pet = true;
break;
}
}
if( !its_a_pet ) {
stop = true;
break;
}
}
}
if( stop ) {
if( autopilot_on ) {
sounds::sound( global_pos3(), 30, sounds::sound_t::alert,
string_format( _( "the %s emitting a beep and saying \"Obstacle detected!\"" ),
name ) );
}
stop_autodriving();
return;
}
int turn_x = get_turn_from_angle( angle, vehpos, target );
int accel_y = 0;
// best to cruise around at a safe velocity or 40mph, whichever is lowest
// accelerate when it dosnt need to turn.
// when following player, take distance to player into account.
// we really want to avoid running the player over.
// If its a helicopter, we dont need to worry about airborne obstacles so much
// And fuel efficiency is terrible at low speeds.
int safe_player_follow_speed = 400;
if( g->u.movement_mode_is( CMM_RUN ) ) {
safe_player_follow_speed = 800;
} else if( g->u.movement_mode_is( CMM_CROUCH ) ) {
safe_player_follow_speed = 200;
}
if( follow_protocol ) {
if( ( ( turn_x > 0 || turn_x < 0 ) && velocity > safe_player_follow_speed ) ||
rl_dist( vehpos, g->m.getabs( g->u.pos() ) ) < 7 + ( ( mount_max.y * 3 ) + 4 ) ) {
accel_y = 1;
}
if( ( velocity < std::min( safe_velocity(), safe_player_follow_speed ) && turn_x == 0 &&
rl_dist( vehpos, g->m.getabs( g->u.pos() ) ) > 8 + ( ( mount_max.y * 3 ) + 4 ) ) ||
velocity < 100 ) {
accel_y = -1;
}
} else {
if( ( turn_x > 0 || turn_x < 0 ) && velocity > 1000 ) {
accel_y = 1;
}
if( ( velocity < std::min( safe_velocity(), is_rotorcraft() &&
is_flying_in_air() ? 12000 : 32 * 100 ) && turn_x == 0 ) || velocity < 500 ) {
accel_y = -1;
}
if( is_patrolling && velocity > 400 ) {
accel_y = 1;
}
}
selfdrive( point( turn_x, accel_y ) );
}
units::angle vehicle::get_angle_from_targ( const tripoint &targ )
{
tripoint vehpos = global_square_location().raw();
rl_vec2d facevec = face_vec();
point rel_pos_target = targ.xy() - vehpos.xy();
rl_vec2d targetvec = rl_vec2d( rel_pos_target.x, rel_pos_target.y );
// cross product
double crossy = ( facevec.x * targetvec.y ) - ( targetvec.x * facevec.y );
// dot product.
double dotx = ( facevec.x * targetvec.x ) + ( facevec.y * targetvec.y );
return units::atan2( crossy, dotx );
}
/**
* Smashes up a vehicle that has already been placed; used for generating
* very damaged vehicles. Additionally, any spot where two vehicles overlapped
* (i.e., any spot with multiple frames) will be completely destroyed, as that
* was the collision point.
*/
void vehicle::smash( map &m, 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 ) {
double dist = damage_size == 0.0f ? 1.0f :
clamp( 1.0f - trig_dist( damage_origin, part.precalc[0].xy() ) /
damage_size, 0.0f, 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();
}
}
}
std::unique_ptr<RemovePartHandler> handler_ptr;
// 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;
}
const vpart_info &p_info = part_info( p );
const vpart_info &other_p_info = part_info( other_p );
if( p_info.get_id() == other_p_info.get_id() ||
( !p_info.location.empty() && p_info.location == other_p_info.location ) ) {
// Deferred creation of the handler to here so it is only created when actually needed.
if( !handler_ptr ) {
// This is a heuristic: we just assume the default handler is good enough when called
// on the main game map. And assume that we run from some mapgen code if called on
// another instance.