-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
vehicle.cpp
8185 lines (7437 loc) · 301 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 <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <list>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include "activity_handlers.h"
#include "activity_type.h"
#include "avatar.h"
#include "bionics.h"
#include "cata_assert.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 "creature_tracker.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "enum_traits.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_group.h"
#include "item_pocket.h"
#include "itype.h"
#include "json.h"
#include "json_loader.h"
#include "make_static.h"
#include "map.h"
#include "map_iterator.h"
#include "mapbuffer.h"
#include "mapdata.h"
#include "material.h"
#include "math_defines.h"
#include "messages.h"
#include "monster.h"
#include "move_mode.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "player_activity.h"
#include "ret_val.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "submap.h"
#include "translations.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle_selector.h"
#include "weather.h"
#include "weather_gen.h"
#include "weather_type.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 activity_id ACT_VEHICLE( "ACT_VEHICLE" );
static const ammotype ammo_battery( "battery" );
static const bionic_id bio_jointservo( "bio_jointservo" );
static const damage_type_id damage_heat( "heat" );
static const damage_type_id damage_pure( "pure" );
static const efftype_id effect_harnessed( "harnessed" );
static const efftype_id effect_winded( "winded" );
static const fault_id fault_engine_immobiliser( "fault_engine_immobiliser" );
static const itype_id fuel_type_animal( "animal" );
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_mana( "mana" );
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 itype_id itype_battery( "battery" );
static const itype_id itype_plut_cell( "plut_cell" );
static const itype_id itype_wall_wiring( "wall_wiring" );
static const itype_id itype_water( "water" );
static const itype_id itype_water_clean( "water_clean" );
static const itype_id itype_water_faucet( "water_faucet" );
static const itype_id itype_water_purifier( "water_purifier" );
static const proficiency_id proficiency_prof_aircraft_mechanic( "prof_aircraft_mechanic" );
static const proficiency_id proficiency_prof_athlete_basic( "prof_athlete_basic" );
static const proficiency_id proficiency_prof_athlete_expert( "prof_athlete_expert" );
static const proficiency_id proficiency_prof_athlete_master( "prof_athlete_master" );
static const proficiency_id proficiency_prof_boat_pilot( "prof_boat_pilot" );
static const proficiency_id proficiency_prof_driver( "prof_driver" );
static const skill_id skill_swimming( "swimming" );
static const vproto_id vehicle_prototype_none( "none" );
static const zone_type_id zone_type_VEHICLE_PATROL( "VEHICLE_PATROL" );
static const std::string flag_E_COMBUSTION( "E_COMBUSTION" );
static const std::string flag_APPLIANCE( "APPLIANCE" );
static const std::string flag_CANT_DRAG( "CANT_DRAG" );
static const std::string flag_WIRING( "WIRING" );
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 int MAX_WIRE_VEHICLE_SIZE = 24;
void DefaultRemovePartHandler::removed( vehicle &veh, const int part )
{
avatar &player_character = get_avatar();
const vehicle_part &vp = veh.part( part );
const tripoint part_pos = veh.global_part_pos3( vp );
// 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( player_character.get_grab_type() == object_type::VEHICLE &&
player_character.pos() + player_character.grab_point == part_pos ) {
if( veh.parts_at_relative( vp.mount, false ).empty() ) {
add_msg( m_info, _( "The vehicle part you were holding has been destroyed!" ) );
player_character.grab( object_type::NONE );
}
}
here.dirty_vehicle_list.insert( &veh );
here.clear_vehicle_point_from_cache( &veh, part_pos );
here.add_vehicle_to_cache( &veh );
here.memory_cache_dec_set_dirty( part_pos, true );
player_character.memorize_clear_decoration(
here.getglobal( part_pos ), "vp_" + vp.info().id.str() );
}
// Vehicle stack methods.
vehicle_stack::vehicle_stack( vehicle &veh, vehicle_part &vp ) :
item_stack( &vp.items ), veh( veh ), vp( vp ) {}
vehicle_stack::iterator vehicle_stack::erase( vehicle_stack::const_iterator it )
{
return veh.remove_item( vp, it );
}
void vehicle_stack::insert( const item &newitem )
{
veh.add_item( vp, newitem );
}
int vehicle_stack::count_limit() const
{
return MAX_ITEM_IN_VEHICLE_STORAGE;
}
units::volume vehicle_stack::max_volume() const
{
const vpart_info &vpi = vp.info();
if( !vpi.has_flag( VPFLAG_CARGO ) || vp.is_broken() ) {
return 0_ml;
}
// Set max volume for vehicle cargo to prevent integer overflow
return std::min( vpi.size, 10000_liter );
}
// Vehicle class methods.
vehicle::vehicle( const vproto_id &proto_id )
{
face.init( 0_degrees );
move.init( 0_degrees );
if( proto_id.is_empty() ) {
refresh(); // we're not using any blueprint, just return an empty initialized vehicle
} else if( !proto_id.is_valid() ) {
debugmsg( "trying to construct vehicle from invalid prototype '%s'", proto_id.str() );
} else {
type = proto_id;
const vehicle_prototype &proto = *type;
// 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;
// The game language may have changed after the blueprint was created,
// so translated the prototype name again.
name = proto.name.translated();
refresh();
}
}
vehicle::~vehicle() = default;
turret_cpu::~turret_cpu() = default;
turret_cpu &turret_cpu::operator=( const turret_cpu & )
{
brain.reset();
return *this;
}
turret_cpu::turret_cpu( const turret_cpu & ) {}
safe_reference<vehicle> vehicle::get_safe_reference()
{
return anchor.reference_to( this );
}
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 = get_map().veh_at( p.pos() );
if( vp && &vp->vehicle() == this &&
p.controlling_vehicle &&
( ( part_with_feature( vp->mount(), "CONTROL_ANIMAL", true ) >= 0 &&
has_engine_type( fuel_type_animal, false ) && get_harnessed_animal() ) ||
( part_with_feature( vp->part_index(), VPFLAG_CONTROLS, false ) >= 0 ) )
) {
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;
}
void vehicle::init_state( map &placed_on, int init_veh_fuel, int init_veh_status )
{
// vehicle parts excluding engines in non-owned vehicles are by default turned off
for( vehicle_part &pt : parts ) {
pt.enabled = !has_owner() && pt.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::turn_zero;
if( get_option<bool>( "OVERRIDE_VEHICLE_INIT_STATE" ) ) {
init_veh_status = get_option<int>( "VEHICLE_STATUS_AT_SPAWN" );
init_veh_fuel = get_option<int>( "VEHICLE_FUEL_AT_SPAWN" );
}
std::map<itype_id, double> fuels; // lets tanks of same fuel type have even contents
const auto rng_fuel_amount = [&fuels, init_veh_fuel]( vehicle_part & vp, const itype_id & fuel ) {
if( !fuel || !fuel->ammo || !fuel->ammo->type ) {
vp.ammo_unset(); // clear if no valid fuel
return;
}
const int max = vp.ammo_capacity( fuel->ammo->type );
if( init_veh_fuel < 0 ) {
// map.emplace(...).first returns iterator to the new or existing element
const double roll = fuels.emplace( fuel, normal_roll( 0.3, 0.15 ) ).first->second;
vp.ammo_set( fuel, max * std::clamp( roll, 0.05, 0.95 ) );
} else if( init_veh_fuel == 0 ) {
vp.ammo_unset();
} else if( init_veh_fuel > 0 && init_veh_fuel < 100 ) {
vp.ammo_set( fuel, max * init_veh_fuel / 100 );
} else { // init_veh_fuel >= 100
vp.ammo_set( fuel, max );
}
};
// veh_status is initial vehicle damage
// -1 = light damage (DEFAULT)
// 0 = undamaged
// 1 = disabled: destroyed seats, controls, tanks, tires, OR engine
// 2 = undamaged with no faults or security
int veh_status = -1;
if( init_veh_status == 0 ) {
veh_status = 0;
}
if( init_veh_status == 1 ) {
veh_status = 1;
const int rand = rng( 1, 5 );
switch( rand ) {
case 1:
destroySeats = true;
break;
case 2:
destroyControls = true;
break;
case 3:
destroyTank = true;
break;
case 4:
destroyEngine = true;
break;
case 5:
destroyTires = true;
break;
}
}
if( one_in( 3 ) ) {
//33% chance for a locked vehicle
has_no_key = true;
}
if( !one_in( 3 ) ) {
//most cars should have a destroyed alarm
destroyAlarm = true;
}
// Make engine faults more likely
destroyEngine = destroyEngine || one_in( 3 );
if( init_veh_status == 2 ) {
veh_status = 0;
has_no_key = false;
destroyAlarm = false;
destroyEngine = false;
}
//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( init_veh_fuel != 0 && !engines.empty() &&
one_in( current_day + 4 ) && !destroyEngine && !has_no_key &&
has_engine_type_not( fuel_type_muscle, true ) ) {
engine_on = true;
}
bool light_head = one_in( 20 );
bool light_whead = one_in( 20 ); // wide-angle headlight
bool light_dome = one_in( 16 );
bool light_aisle = one_in( 8 );
bool light_hoverh = one_in( 4 ); // half circle overhead light
bool light_overh = one_in( 4 );
bool light_atom = one_in( 2 );
for( vehicle_part &vp : parts ) {
const vpart_info vpi = vp.info();
if( vpi.has_flag( VPFLAG_CONE_LIGHT ) ) {
vp.enabled = light_head;
} else if( vpi.has_flag( VPFLAG_WIDE_CONE_LIGHT ) ) {
vp.enabled = light_whead;
} else if( vpi.has_flag( VPFLAG_DOME_LIGHT ) ) {
vp.enabled = light_dome;
} else if( vpi.has_flag( VPFLAG_AISLE_LIGHT ) ) {
vp.enabled = light_aisle;
} else if( vpi.has_flag( VPFLAG_HALF_CIRCLE_LIGHT ) ) {
vp.enabled = light_hoverh;
} else if( vpi.has_flag( VPFLAG_CIRCLE_LIGHT ) ) {
vp.enabled = light_overh;
} else if( vpi.has_flag( VPFLAG_ATOMIC_LIGHT ) ) {
vp.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() ) {
rng_fuel_amount( pt, itype_plut_cell );
} else if( pt.is_battery() ) {
rng_fuel_amount( pt, itype_battery );
} else if( pt.is_tank() || pt.is_fuel_store() ) {
rng_fuel_amount( pt, pt.ammo_current() );
}
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( vp_flag::passenger_flag );
}
// initial vehicle damage
if( veh_status == 0 ) {
// Completely mint condition vehicle
set_hp( pt, vp.info().durability, false );
} 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, false );
pt.ammo_unset(); //empty broken batteries and fuel tanks
} else {
set_hp( pt, ( roll - broken ) / static_cast<double>( unhurt - broken ) * vp.info().durability,
false );
}
} else {
set_hp( pt, vp.info().durability, false );
}
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, false );
} else if( destroyEngine ) {
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, false );
}
// Fuel tanks should be emptied as well
if( destroyTank && pt.is_fuel_store() ) {
set_hp( pt, 0, false );
pt.ammo_unset();
}
//Solar panels have 25% of being destroyed
if( vp.has_feature( "SOLAR_PANEL" ) && one_in( 4 ) && init_veh_status != 2 ) {
set_hp( pt, 0, false );
}
/* 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_engine_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, false );
tries++;
}
}
// Additional 50% chance for heavy damage to disabled vehicles
if( veh_status == 1 && one_in( 2 ) ) {
smash( placed_on, 0.5 );
}
for( const int p : engines ) {
auto_select_fuel( parts[p] );
}
refresh();
}
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_monster( 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.
*/
map &here = get_map();
// 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( !here.inbounds( here.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_VEHICLE_PATROL, global_square_location(), 60 );
if( zone_src_set.empty() ) {
is_patrolling = false;
return;
}
// get corners.
tripoint_abs_ms min;
tripoint_abs_ms max;
for( const tripoint_abs_ms &box : zone_src_set ) {
if( min == tripoint_abs_ms() ) {
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_abs_ms max_tri = x_side ? tripoint_abs_ms( point_along, max.y(), min.z() ) :
tripoint_abs_ms( max.x(), point_along, min.z() );
const tripoint_abs_ms min_tri = x_side ? tripoint_abs_ms( point_along, min.y(), min.z() ) :
tripoint_abs_ms( min.x(), point_along, min.z() );
tripoint_abs_ms chosen_tri = min_tri;
if( rl_dist( max_tri, global_square_location() ) >=
rl_dist( min_tri, global_square_location() ) ) {
chosen_tri = max_tri;
}
// TODO: fix point types
autodrive_local_target = chosen_tri.raw();
drive_to_local_target( autodrive_local_target, false );
}
std::set<point> vehicle::immediate_path( const 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, vehicles::steer_increment );
tileray collision_vector;
collision_vector.init( adjusted_angle );
map &here = get_map();
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( here.getabs( top_left_actual ),
here.getabs( top_right_actual ) );
for( const 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 )
{
Character &player_character = get_player_character();
if( follow_protocol && player_character.in_vehicle ) {
sounds::sound( global_pos3(), 30, sounds::sound_t::alert,
string_format( _( "the %s emitting a beep and saying \"Autonomous driving protocols suspended!\"" ),
name ) );
stop_autodriving();
return;
}
refresh();
map &here = get_map();
tripoint vehpos = global_square_location().raw();
units::angle angle = get_angle_from_targ( target );
bool stop = precollision_check( angle, here, follow_protocol );
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 doesn't 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.
const int safe_player_follow_speed = 400 *
player_character.current_movement_mode()->move_speed_mult();
if( follow_protocol ) {
if( ( ( turn_x > 0 || turn_x < 0 ) && velocity > safe_player_follow_speed ) ||
rl_dist( vehpos, here.getabs( player_character.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, here.getabs( player_character.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 ) );
}
bool vehicle::precollision_check( units::angle &angle, map &here, bool follow_protocol )
{
if( !precollision_on ) {
return false;
}
Character &player_character = get_player_character();
// 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;
creature_tracker &creatures = get_creature_tracker();
for( const point &pt_elem : points_to_check ) {
point elem = here.getlocal( pt_elem );
if( stop ) {
break;
}
const optional_vpart_position ovp = here.veh_at( tripoint( elem, sm_pos.z ) );
if( here.impassable_ter_furn( tripoint( elem, sm_pos.z ) ) || ( ovp &&
&ovp->vehicle() != this ) ) {
stop = true;
break;
}
if( elem == player_character.pos().xy() ) {
if( follow_protocol || player_character.in_vehicle ) {
continue;
} else {
stop = true;
break;
}
}
bool its_a_pet = false;
if( creatures.creature_at( tripoint( elem, sm_pos.z ) ) ) {
npc *guy = creatures.creature_at<npc>( tripoint( elem, sm_pos.z ) );
if( guy && !guy->in_vehicle ) {
stop = true;
break;
}
for( const vehicle_part &p : parts ) {
monster *mon = get_monster( index_of_part( &p ) );
if( mon && mon->pos().xy() == elem ) {
its_a_pet = true;
break;
}
}
if( !its_a_pet ) {
stop = true;
break;
}
}
}
return stop;
}
units::angle vehicle::get_angle_from_targ( const tripoint &targ ) const
{
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( vehicle_part &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( const int square_part_index : parts_in_square ) {
const vpart_info &vpi = parts[square_part_index].info();
if( vpi.location == part_location_structure ) {
structures_found++;
}
}
if( structures_found > 1 ) {
//Destroy everything in the square
for( int idx : parts_in_square ) {
vehicle_part &vp = parts[idx];
vp.ammo_unset();
mod_hp( vp, -vp.hp() );
}
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)
const float roll = rng_float( hp_percent_loss_min * dist, hp_percent_loss_max * dist );
if( mod_hp( part, -part.info().durability * roll ) ) {
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;
}
vehicle_part &vp1 = parts[p];
vehicle_part &vp2 = parts[other_p];
const vpart_info &vpi1 = vp1.info();
const vpart_info &vpi2 = vp2.info();
if( vpi1.id == vpi2.id ||
( !vpi1.location.empty() && vpi1.location == vpi2.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.
if( g && &get_map() == &m ) {
handler_ptr = std::make_unique<DefaultRemovePartHandler>();
} else {
handler_ptr = std::make_unique<MapgenRemovePartHandler>( m );
}
}
if( part.is_fake ) {
remove_part( vp1, *handler_ptr );
} else {
remove_part( vp2, *handler_ptr );
}
}
}
}
}
int vehicle::lift_strength() const
{
units::mass mass = total_mass();
return std::max<std::int64_t>( mass / 10000_gram, 1 );
}
bool vehicle::is_engine_type_on( const vehicle_part &vp, const itype_id &ft ) const
{
return is_engine_on( vp ) && is_engine_type( vp, ft );
}
bool vehicle::has_engine_type( const itype_id &ft, const bool enabled ) const
{
for( const int p : engines ) {
const vehicle_part &vp = parts[p];
if( is_engine_type( vp, ft ) && ( !enabled || is_engine_on( vp ) ) ) {
return true;
}
}
return false;
}
bool vehicle::has_engine_type_not( const itype_id &ft, const bool enabled ) const
{
for( const int p : engines ) {
const vehicle_part &vp = parts[p];
if( !is_engine_type( vp, ft ) && ( !enabled || is_engine_on( vp ) ) ) {
return true;
}
}
return false;
}
std::optional<std::string> vehicle::has_engine_conflict( const vpart_info &possible_conflict ) const
{
if( !possible_conflict.engine_info ) {
return std::nullopt; // not an engine
}
const std::vector<std::string> &new_excludes = possible_conflict.engine_info->exclusions;
if( new_excludes.empty() ) {
return std::nullopt;
}
for( const int p : engines ) {
const vehicle_part &vp = parts[p];
std::vector<std::string> install_excludes = vp.info().engine_info->exclusions;
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() ) {
return conflicts.front();
}
}
return std::nullopt;
}
bool vehicle::is_engine_type( const vehicle_part &vp, const itype_id &ft ) const
{
return vp.ammo_current().is_null() ? vp.fuel_current() == ft : vp.ammo_current() == ft;
}
bool vehicle::is_engine_type_combustion( const vehicle_part &vp ) const
{