forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle_move.cpp
1970 lines (1778 loc) · 71.8 KB
/
vehicle_move.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 "vehicle_move.h" // IWYU pragma: associated
#include <cassert>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include "avatar.h"
#include "bodypart.h"
#include "creature.h"
#include "debug.h"
#include "enums.h"
#include "explosion.h"
#include "game.h"
#include "int_id.h"
#include "item.h"
#include "itype.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "material.h"
#include "math_defines.h"
#include "messages.h"
#include "monster.h"
#include "options.h"
#include "player.h"
#include "point_float.h"
#include "rng.h"
#include "sounds.h"
#include "string_id.h"
#include "translations.h"
#include "trap.h"
#include "units_angle.h"
#include "units_utility.h"
#include "veh_type.h"
#include "vpart_position.h"
#include "vpart_range.h"
#define dbg(x) DebugLogFL((x),DC::Map)
static const itype_id fuel_type_muscle( "muscle" );
static const itype_id fuel_type_animal( "animal" );
static const itype_id fuel_type_battery( "battery" );
static const skill_id skill_driving( "driving" );
static const efftype_id effect_harnessed( "harnessed" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_stunned( "stunned" );
static const std::string part_location_structure( "structure" );
// tile height in meters
static const float tile_height = 4;
// miles per hour to vehicle 100ths of miles per hour
static const int mi_to_vmi = 100;
// meters per second to miles per hour
static const float mps_to_miph = 2.23694f;
// convert m/s to vehicle 100ths of a mile per hour
int mps_to_vmiph( double mps )
{
return mps * mps_to_miph * mi_to_vmi;
}
// convert vehicle 100ths of a mile per hour to m/s
double vmiph_to_mps( int vmiph )
{
return vmiph / mps_to_miph / mi_to_vmi;
}
int cmps_to_vmiph( int cmps )
{
return cmps * mps_to_miph;
}
int vmiph_to_cmps( int vmiph )
{
return vmiph / mps_to_miph;
}
int vehicle::slowdown( int at_velocity ) const
{
double mps = vmiph_to_mps( std::abs( at_velocity ) );
// slowdown due to air resistance is proportional to square of speed
double f_total_drag = coeff_air_drag() * mps * mps;
if( is_watercraft() ) {
// same with water resistance
f_total_drag += coeff_water_drag() * mps * mps;
} else if( !is_falling && !is_flying ) {
double f_rolling_drag = coeff_rolling_drag() * ( vehicles::rolling_constant_to_variable + mps );
if( vehicle_movement::is_on_rails( get_map(), *this ) ) {
// vehicles on rails don't skid
f_total_drag += f_rolling_drag;
} else {
// increase rolling resistance by up to 25x if the vehicle is skidding at right angle to facing
const double skid_factor = 1.0 + 24.0 * std::abs( units::sin( face.dir() - move.dir() ) );
f_total_drag += f_rolling_drag * skid_factor;
}
}
double accel_slowdown = f_total_drag / to_kilogram( total_mass() );
// converting m/s^2 to vmiph/s
int slowdown = mps_to_vmiph( accel_slowdown );
if( is_towing() ) {
vehicle *other_veh = tow_data.get_towed();
if( other_veh ) {
slowdown += other_veh->slowdown( at_velocity );
}
}
if( slowdown < 0 ) {
debugmsg( "vehicle %s has negative drag slowdown %d\n", name, slowdown );
}
add_msg( m_debug, "%s at %d vimph, f_drag %3.2f, drag accel %d vmiph - extra drag %d",
name, at_velocity, f_total_drag, slowdown, static_drag() );
// plows slow rolling vehicles, but not falling or floating vehicles
if( !( is_falling || is_floating || is_flying ) ) {
slowdown -= static_drag();
}
return std::max( 1, slowdown );
}
void vehicle::thrust( int thd, int z )
{
//if vehicle is stopped, set target direction to forward.
//ensure it is not skidding. Set turns used to 0.
if( !is_moving() && z == 0 ) {
turn_dir = face.dir();
stop();
}
bool pl_ctrl = player_in_control( get_player_character() );
// No need to change velocity if there are no wheels
if( ( in_water && can_float() ) || ( is_rotorcraft() && ( z != 0 || is_flying ) ) ) {
// we're good
} else if( is_floating && !can_float() ) {
stop();
if( pl_ctrl ) {
add_msg( _( "The %s is too leaky!" ), name );
}
return;
} else if( !valid_wheel_config() && z == 0 ) {
stop();
if( pl_ctrl ) {
add_msg( _( "The %s doesn't have enough wheels to move!" ), name );
}
return;
}
// Accelerate (true) or brake (false)
bool thrusting = true;
if( velocity ) {
int sgn = ( velocity < 0 ) ? -1 : 1;
thrusting = ( sgn == thd );
}
// TODO: Pass this as an argument to avoid recalculating
float traction = k_traction( get_map().vehicle_wheel_traction( *this ) );
int accel = current_acceleration() * traction;
if( accel < 200 && velocity > 0 && is_towing() ) {
if( pl_ctrl ) {
add_msg( _( "The %s struggles to pull the %s on this surface!" ), name,
tow_data.get_towed()->name );
}
return;
}
if( thrusting && accel == 0 ) {
if( pl_ctrl ) {
add_msg( _( "The %s is too heavy for its engine(s)!" ), name );
}
return;
}
const int max_vel = traction * max_velocity();
// maximum braking is 20 mph/s, assumes high friction tires
const int max_brake = 20 * 100;
//pos or neg if accelerator or brake
int vel_inc = ( accel + ( thrusting ? 0 : max_brake ) ) * thd;
// Reverse is only 60% acceleration, unless an electric motor is in use
if( thd == -1 && thrusting && !has_engine_type( fuel_type_battery, true ) ) {
vel_inc = .6 * vel_inc;
}
//find ratio of used acceleration to maximum available, returned in tenths of a percent
//so 1000 = 100% and 453 = 45.3%
int load;
// Keep exact cruise control speed
if( cruise_on && accel != 0 ) {
int effective_cruise = std::min( cruise_velocity, max_vel );
if( thd > 0 ) {
vel_inc = std::min( vel_inc, effective_cruise - velocity );
} else {
vel_inc = std::max( vel_inc, effective_cruise - velocity );
}
if( thrusting ) {
load = 1000 * std::abs( vel_inc ) / accel;
} else {
// brakes provide 20 mph/s of slowdown and the rest is engine braking
// TODO: braking depends on wheels, traction, driver skill
load = 1000 * std::max( 0, std::abs( vel_inc ) - max_brake ) / accel;
}
} else {
load = ( thrusting ? 1000 : 0 );
}
// rotorcraft need to spend +5% (in addition to idle) of load to fly, +20% (in addition to idle) to ascend
if( is_rotorcraft() && ( z > 0 || is_flying_in_air() ) ) {
load = std::max( load, z > 0 ? 200 : 50 );
thrusting = true;
}
// only consume resources if engine accelerating
if( load >= 1 && thrusting ) {
//abort if engines not operational
if( total_power_w() <= 0 || !engine_on || ( z == 0 && accel == 0 ) ) {
if( pl_ctrl ) {
if( total_power_w( false ) <= 0 ) {
add_msg( m_info, _( "The %s doesn't have an engine!" ), name );
} else if( has_engine_type( fuel_type_muscle, true ) ) {
add_msg( m_info, _( "The %s's mechanism is out of reach!" ), name );
} else if( !engine_on ) {
add_msg( _( "The %s's engine isn't on!" ), name );
} else if( traction < 0.01f ) {
add_msg( _( "The %s is stuck." ), name );
} else {
add_msg( _( "The %s's engine emits a sneezing sound." ), name );
}
}
cruise_velocity = 0;
return;
}
//make noise and consume fuel
noise_and_smoke( load );
consume_fuel( load, 1 );
if( z != 0 && is_rotorcraft() ) {
requested_z_change = z;
}
//break the engines a bit, if going too fast.
int strn = static_cast<int>( strain() * strain() * 100 );
for( size_t e = 0; e < engines.size(); e++ ) {
do_engine_damage( e, strn );
}
}
//wheels aren't facing the right way to change velocity properly
//lower down, since engines should be getting damaged anyway
if( skidding ) {
return;
}
//change vehicles velocity
if( ( velocity > 0 && velocity + vel_inc < 0 ) || ( velocity < 0 && velocity + vel_inc > 0 ) ) {
//velocity within braking distance of 0
stop();
} else {
// Increase velocity up to max_vel or min_vel, but not above.
const int min_vel = max_reverse_velocity();
if( vel_inc > 0 ) {
// Don't allow braking by accelerating (could happen with damaged engines)
velocity = std::max( velocity, std::min( velocity + vel_inc, max_vel ) );
} else {
velocity = std::min( velocity, std::max( velocity + vel_inc, min_vel ) );
}
}
// If you are going faster than the animal can handle, harness is damaged
// Animal may come free ( and possibly hit by vehicle )
for( size_t e = 0; e < parts.size(); e++ ) {
const vehicle_part &vp = parts[ e ];
if( vp.info().fuel_type == fuel_type_animal && engines.size() != 1 ) {
monster *mon = get_pet( e );
if( mon != nullptr && mon->has_effect( effect_harnessed ) ) {
if( velocity > mon->get_speed() * 12 ) {
add_msg( m_bad, _( "Your %s is not fast enough to keep up with the %s" ), mon->get_name(), name );
int dmg = rng( 0, 10 );
damage_direct( e, dmg );
}
}
}
}
}
void vehicle::cruise_thrust( int amount )
{
if( amount == 0 ) {
return;
}
int safe_vel = safe_velocity();
int max_vel = autopilot_on ? safe_velocity() : max_velocity();
int max_rev_vel = max_reverse_velocity();
//if the safe velocity is between the cruise velocity and its next value, set to safe velocity
if( ( cruise_velocity < safe_vel && safe_vel < ( cruise_velocity + amount ) ) ||
( cruise_velocity > safe_vel && safe_vel > ( cruise_velocity + amount ) ) ) {
cruise_velocity = safe_vel;
} else {
if( amount < 0 && ( cruise_velocity == safe_vel || cruise_velocity == max_vel ) ) {
// If coming down from safe_velocity or max_velocity decrease by one so
// the rounding below will drop velocity to a multiple of amount.
cruise_velocity += -1;
} else if( amount > 0 && cruise_velocity == max_rev_vel ) {
// If increasing from max_rev_vel, do the opposite.
cruise_velocity += 1;
} else {
// Otherwise just add the amount.
cruise_velocity += amount;
}
// Integer round to lowest multiple of amount.
// The result is always equal to the original or closer to zero,
// even if negative
cruise_velocity = ( cruise_velocity / std::abs( amount ) ) * std::abs( amount );
}
// Can't have a cruise speed faster than max speed
// or reverse speed faster than max reverse speed.
if( cruise_velocity > max_vel ) {
cruise_velocity = max_vel;
} else if( cruise_velocity < max_rev_vel ) {
cruise_velocity = max_rev_vel;
}
}
void vehicle::turn( units::angle deg )
{
if( deg == 0_degrees ) {
return;
}
if( velocity < 0 && !::get_option<bool>( "REVERSE_STEERING" ) ) {
deg = -deg;
}
last_turn = deg;
turn_dir = normalize( turn_dir + deg );
// quick rounding the turn dir to a multiple of 15
turn_dir = round_to_multiple_of( turn_dir, 15_degrees );
}
void vehicle::stop( bool update_cache )
{
velocity = 0;
skidding = false;
move = face;
last_turn = 0_degrees;
of_turn_carry = 0;
if( !update_cache ) {
return;
}
map &here = get_map();
for( const tripoint &p : get_points() ) {
here.set_memory_seen_cache_dirty( p );
}
}
bool vehicle::collision( std::vector<veh_collision> &colls,
const tripoint &dp,
bool just_detect, bool bash_floor )
{
/*
* Big TODO:
* Rewrite this function so that it has "pre-collision" phase (detection)
* and "post-collision" phase (applying damage).
* Then invoke the functions cyclically (pre-post-pre-post-...) until
* velocity == 0 or no collision happens.
* Make all post-collisions in a given phase use the same momentum.
*
* How it works right now: find the first obstacle, then ram it over and over
* until either the obstacle is removed or the vehicle stops.
* Bug: when ramming a critter without enough force to send it flying,
* the vehicle will phase into it.
*/
if( dp.z != 0 && ( dp.x != 0 || dp.y != 0 ) ) {
// Split into horizontal + vertical
return collision( colls, tripoint( dp.xy(), 0 ), just_detect, bash_floor ) ||
collision( colls, tripoint( 0, 0, dp.z ), just_detect, bash_floor );
}
if( dp.z == -1 && !bash_floor ) {
// First check current level, then the one below if current had no collisions
// Bash floors on the current one, but not on the one below.
if( collision( colls, tripoint_zero, just_detect, true ) ) {
return true;
}
}
const bool vertical = bash_floor || dp.z != 0;
const int &coll_velocity = vertical ? vertical_velocity : velocity;
// Skip collisions when there is no apparent movement, except verticially moving rotorcraft.
if( coll_velocity == 0 && !is_rotorcraft() ) {
just_detect = true;
}
const int velocity_before = coll_velocity;
int lowest_velocity = coll_velocity;
const int sign_before = sgn( velocity_before );
bool empty = true;
for( int p = 0; static_cast<size_t>( p ) < parts.size(); p++ ) {
const vpart_info &info = part_info( p );
if( ( info.location != part_location_structure && info.rotor_diameter() == 0 ) ||
parts[ p ].removed ) {
continue;
}
empty = false;
// Coordinates of where part will go due to movement (dx/dy/dz)
// and turning (precalc[1])
const tripoint dsp = global_pos3() + dp + parts[p].precalc[1];
veh_collision coll = part_collision( p, dsp, just_detect, bash_floor );
if( coll.type == veh_coll_nothing ) {
continue;
}
colls.push_back( coll );
if( just_detect ) {
// DO insert the first collision so we can tell what was it
return true;
}
const int velocity_after = coll_velocity;
// A hack for falling vehicles: restore the velocity so that it hits at full force everywhere
// TODO: Make this more elegant
if( vertical ) {
if( velocity_before < 0 ) {
lowest_velocity = std::max( lowest_velocity, coll_velocity );
} else {
lowest_velocity = std::min( lowest_velocity, coll_velocity );
}
vertical_velocity = velocity_before;
} else if( sgn( velocity_after ) != sign_before ) {
// Sign of velocity inverted, collisions would be in wrong direction
break;
}
}
if( vertical ) {
vertical_velocity = lowest_velocity;
if( vertical_velocity == 0 ) {
is_falling = false;
}
}
if( empty ) {
// HACK: Hack for dirty vehicles that didn't yet get properly removed
veh_collision fake_coll;
fake_coll.type = veh_coll_other;
colls.push_back( fake_coll );
velocity = 0;
vertical_velocity = 0;
add_msg( m_debug, "Collision check on a dirty vehicle %s", name );
return true;
}
return !colls.empty();
}
// A helper to make sure mass and density is always calculated the same way
static void terrain_collision_data( const tripoint &p, bool bash_floor,
float &mass, float &density, float &elastic )
{
elastic = 0.30;
map &here = get_map();
// Just a rough rescale for now to obtain approximately equal numbers
const int bash_min = here.bash_resistance( p, bash_floor );
const int bash_max = here.bash_strength( p, bash_floor );
mass = ( bash_min + bash_max ) / 2.0;
density = bash_min;
}
veh_collision vehicle::part_collision( int part, const tripoint &p,
bool just_detect, bool bash_floor )
{
// Vertical collisions need to be handled differently
// All collisions have to be either fully vertical or fully horizontal for now
const bool vert_coll = bash_floor || p.z != sm_pos.z;
Character &player_character = get_player_character();
const bool pl_ctrl = player_in_control( player_character );
Creature *critter = g->critter_at( p, true );
player *ph = dynamic_cast<player *>( critter );
Creature *driver = pl_ctrl ? &player_character : nullptr;
// If in a vehicle assume it's this one
if( ph != nullptr && ph->in_vehicle ) {
critter = nullptr;
ph = nullptr;
}
map &here = get_map();
const optional_vpart_position ovp = here.veh_at( p );
// Disable vehicle/critter collisions when bashing floor
// TODO: More elegant code
const bool is_veh_collision = !bash_floor && ovp && &ovp->vehicle() != this;
const bool is_body_collision = !bash_floor && critter != nullptr;
veh_collision ret;
ret.type = veh_coll_nothing;
ret.part = part;
// Vehicle collisions are a special case. just return the collision.
// The map takes care of the dynamic stuff.
if( is_veh_collision ) {
ret.type = veh_coll_veh;
//"imp" is too simplistic for vehicle-vehicle collisions
ret.target = &ovp->vehicle();
ret.target_part = ovp->part_index();
ret.target_name = ovp->vehicle().disp_name();
return ret;
}
// Typical rotor tip speed in MPH * 100.
int rotor_velocity = 45600;
// Non-vehicle collisions can't happen when the vehicle is not moving
int &coll_velocity = ( part_info( part ).rotor_diameter() == 0 ) ?
( vert_coll ? vertical_velocity : velocity ) :
rotor_velocity;
if( !just_detect && coll_velocity == 0 ) {
return ret;
}
if( is_body_collision ) {
// critters on a BOARDABLE part in this vehicle aren't colliding
if( ovp && ( &ovp->vehicle() == this ) && get_pet( ovp->part_index() ) ) {
return ret;
}
// we just ran into a fish, so move it out of the way
if( here.has_flag( "SWIMMABLE", critter->pos() ) ) {
tripoint end_pos = critter->pos();
tripoint start_pos;
const units::angle angle =
move.dir() + 45_degrees * ( parts[part].mount.x > pivot_point().x ? -1 : 1 );
std::set<tripoint> &cur_points = get_points( true );
// push the animal out of way until it's no longer in our vehicle and not in
// anyone else's position
while( g->critter_at( end_pos, true ) ||
cur_points.find( end_pos ) != cur_points.end() ) {
start_pos = end_pos;
calc_ray_end( angle, 2, start_pos, end_pos );
}
critter->setpos( end_pos );
return ret;
}
}
// Damage armor before damaging any other parts
// Actually target, not just damage - spiked plating will "hit back", for example
const int armor_part = part_with_feature( ret.part, VPFLAG_ARMOR, true );
if( armor_part >= 0 ) {
ret.part = armor_part;
}
int dmg_mod = part_info( ret.part ).dmg_mod;
// Let's calculate type of collision & mass of object we hit
float mass2 = 0;
// e = 0 -> plastic collision
float e = 0.3;
// e = 1 -> inelastic collision
//part density
float part_dens = 0;
if( is_body_collision ) {
// Check any monster/NPC/player on the way
// body
ret.type = veh_coll_body;
ret.target = critter;
e = 0.30;
part_dens = 15;
mass2 = units::to_kilogram( critter->get_weight() );
ret.target_name = critter->disp_name();
} else if( ( bash_floor && here.is_bashable_ter_furn( p, true ) ) ||
( here.is_bashable_ter_furn( p, false ) && here.move_cost_ter_furn( p ) != 2 &&
// Don't collide with tiny things, like flowers, unless we have a wheel in our space.
( part_with_feature( ret.part, VPFLAG_WHEEL, true ) >= 0 ||
!here.has_flag_ter_or_furn( "TINY", p ) ) &&
// Protrusions don't collide with short terrain.
// Tiny also doesn't, but it's already excluded unless there's a wheel present.
!( part_with_feature( ret.part, "PROTRUSION", true ) >= 0 &&
here.has_flag_ter_or_furn( "SHORT", p ) ) &&
// These are bashable, but don't interact with vehicles.
!here.has_flag_ter_or_furn( "NOCOLLIDE", p ) &&
// Do not collide with track tiles if we can use rails
!( here.has_flag_ter_or_furn( TFLAG_RAIL, p ) && this->can_use_rails() ) ) ) {
// Movecost 2 indicates flat terrain like a floor, no collision there.
ret.type = veh_coll_bashable;
terrain_collision_data( p, bash_floor, mass2, part_dens, e );
ret.target_name = here.disp_name( p );
} else if( here.impassable_ter_furn( p ) ||
( bash_floor && !here.has_flag( TFLAG_NO_FLOOR, p ) ) ) {
// not destructible
ret.type = veh_coll_other;
mass2 = 1000;
e = 0.10;
part_dens = 80;
ret.target_name = here.disp_name( p );
}
if( ret.type == veh_coll_nothing || just_detect ) {
// Hit nothing or we aren't actually hitting
return ret;
}
stop_autodriving();
// Calculate mass AFTER checking for collision
// because it involves iterating over all cargo
// Rotors only use rotor mass in calculation.
const float mass = ( part_info( part ).rotor_diameter() > 0 ) ?
to_kilogram( parts[ part ].base.weight() ) : to_kilogram( total_mass() );
//Calculate damage resulting from d_E
const material_id_list &mats = part_info( ret.part ).item->materials;
float vpart_dens = 0;
if( !mats.empty() ) {
for( auto &mat_id : mats ) {
vpart_dens += mat_id.obj().density();
}
// average
vpart_dens /= mats.size();
}
//k=100 -> 100% damage on part
//k=0 -> 100% damage on obj
float material_factor = ( part_dens - vpart_dens ) * 0.5;
material_factor = std::max( -25.0f, std::min( 25.0f, material_factor ) );
// factor = -25 if mass is much greater than mass2
// factor = +25 if mass2 is much greater than mass
const float weight_factor = mass >= mass2 ?
-25 * ( std::log( mass ) - std::log( mass2 ) ) / std::log( mass ) :
25 * ( std::log( mass2 ) - std::log( mass ) ) / std::log( mass2 );
float k = 50 + material_factor + weight_factor;
k = std::max( 10.0f, std::min( 90.0f, k ) );
bool smashed = true;
const std::string snd = _( "smash!" );
float dmg = 0.0f;
float part_dmg = 0.0f;
// Calculate Impulse of car
time_duration time_stunned = 0_turns;
const int prev_velocity = coll_velocity;
const int vel_sign = sgn( coll_velocity );
// Velocity of the object we're hitting
// Assuming it starts at 0, but we'll probably hit it many times
// in one collision, so accumulate the velocity gain from each hit.
float vel2 = 0.0f;
do {
smashed = false;
// Impulse of vehicle
const float vel1 = coll_velocity / 100.0f;
// Velocity of car after collision
const float vel1_a = ( mass * vel1 + mass2 * vel2 + e * mass2 * ( vel2 - vel1 ) ) /
( mass + mass2 );
// Velocity of object after collision
const float vel2_a = ( mass * vel1 + mass2 * vel2 + e * mass * ( vel1 - vel2 ) ) / ( mass + mass2 );
// Lost energy at collision -> deformation energy -> damage
const float E_before = 0.5f * ( mass * vel1 * vel1 ) + 0.5f * ( mass2 * vel2 * vel2 );
const float E_after = 0.5f * ( mass * vel1_a * vel1_a ) + 0.5f * ( mass2 * vel2_a * vel2_a );
const float d_E = E_before - E_after;
if( d_E <= 0 ) {
// Deformation energy is signed
// If it's negative, it means something went wrong
// But it still does happen sometimes...
if( std::fabs( vel1_a ) < std::fabs( vel1 ) ) {
// Lower vehicle's speed to prevent infinite loops
coll_velocity = vel1_a * 90;
}
if( std::fabs( vel2_a ) > std::fabs( vel2 ) ) {
vel2 = vel2_a;
}
// this causes infinite loop
if( mass2 == 0 ) {
mass2 = 1;
}
continue;
}
add_msg( m_debug, "Deformation energy: %.2f", d_E );
// Damage calculation
// Damage dealt overall
dmg += d_E / 400;
// Damage for vehicle-part
// Always if no critters, otherwise if critter is real
if( critter == nullptr || !critter->is_hallucination() ) {
part_dmg = dmg * k / 100;
add_msg( m_debug, "Part collision damage: %.2f", part_dmg );
}
// Damage for object
const float obj_dmg = dmg * ( 100 - k ) / 100;
if( ret.type == veh_coll_bashable ) {
// Something bashable -- use map::bash to determine outcome
// NOTE: Floor bashing disabled for balance reasons
// Floor values are still used to set damage dealt to vehicle
smashed = here.is_bashable_ter_furn( p, false ) &&
here.bash_resistance( p, bash_floor ) <= obj_dmg &&
here.bash( p, obj_dmg, false, false, false, this ).success;
if( smashed ) {
if( here.is_bashable_ter_furn( p, bash_floor ) ) {
// There's new terrain there to smash
smashed = false;
terrain_collision_data( p, bash_floor, mass2, part_dens, e );
ret.target_name = here.disp_name( p );
} else if( here.impassable_ter_furn( p ) ) {
// There's new terrain there, but we can't smash it!
smashed = false;
ret.type = veh_coll_other;
mass2 = 1000;
e = 0.10;
part_dens = 80;
ret.target_name = here.disp_name( p );
}
}
} else if( ret.type == veh_coll_body ) {
int dam = obj_dmg * dmg_mod / 100;
// We know critter is set for this type. Assert to inform static
// analysis.
assert( critter );
// No blood from hallucinations
if( !critter->is_hallucination() ) {
if( part_flag( ret.part, "SHARP" ) ) {
parts[ret.part].blood += ( 20 + dam ) * 5;
} else if( dam > rng( 10, 30 ) ) {
parts[ret.part].blood += ( 10 + dam / 2 ) * 5;
}
check_environmental_effects = true;
}
time_stunned = time_duration::from_turns( ( rng( 0, dam ) > 10 ) + ( rng( 0, dam ) > 40 ) );
if( time_stunned > 0_turns ) {
critter->add_effect( effect_stunned, time_stunned );
}
if( ph != nullptr ) {
ph->hitall( dam, 40, driver );
} else {
const int armor = part_flag( ret.part, "SHARP" ) ?
critter->get_armor_cut( bodypart_id( "torso" ) ) :
critter->get_armor_bash( bodypart_id( "torso" ) );
dam = std::max( 0, dam - armor );
critter->apply_damage( driver, bodypart_id( "torso" ), dam );
add_msg( m_debug, "Critter collision damage: %d", dam );
}
// Don't fling if vertical - critter got smashed into the ground
if( !vert_coll ) {
if( std::fabs( vel2_a ) > 10.0f ||
std::fabs( e * mass * vel1_a ) > std::fabs( mass2 * ( 10.0f - vel2_a ) ) ) {
const units::angle angle = rng_float( -60_degrees, 60_degrees );
// Also handle the weird case when we don't have enough force
// but still have to push (in such case compare momentum)
const float push_force = std::max<float>( std::fabs( vel2_a ), 10.1f );
// move.dir is where the vehicle is facing. If velocity is negative,
// we're moving backwards and have to adjust the angle accordingly.
const units::angle angle_sum =
angle + move.dir() + ( vel2_a > 0 ? 0_degrees : 180_degrees );
g->fling_creature( critter, angle_sum, push_force );
} else if( std::fabs( vel2_a ) > std::fabs( vel2 ) ) {
vel2 = vel2_a;
} else {
// Vehicle's momentum isn't big enough to push the critter
velocity = 0;
break;
}
if( critter->is_dead_state() ) {
smashed = true;
} else if( critter != nullptr ) {
// Only count critter as pushed away if it actually changed position
smashed = critter->pos() != p;
}
}
}
if( critter == nullptr || !critter->is_hallucination() ) {
coll_velocity = vel1_a * ( smashed ? 100 : 90 );
}
// Stop processing when sign inverts, not when we reach 0
} while( !smashed && sgn( coll_velocity ) == vel_sign );
// Apply special effects from collision.
if( critter != nullptr ) {
if( !critter->is_hallucination() ) {
if( pl_ctrl ) {
if( time_stunned > 0_turns ) {
//~ 1$s - vehicle name, 2$s - part name, 3$s - NPC or monster
add_msg( m_warning, _( "Your %1$s's %2$s rams into %3$s and stuns it!" ),
name, parts[ ret.part ].name(), ret.target_name );
} else {
//~ 1$s - vehicle name, 2$s - part name, 3$s - NPC or monster
add_msg( m_warning, _( "Your %1$s's %2$s rams into %3$s!" ),
name, parts[ ret.part ].name(), ret.target_name );
}
}
if( part_flag( ret.part, "SHARP" ) ) {
critter->bleed();
} else {
sounds::sound( p, 20, sounds::sound_t::combat, snd, false, "smash_success", "hit_vehicle" );
}
}
} else {
if( pl_ctrl ) {
if( !snd.empty() ) {
//~ 1$s - vehicle name, 2$s - part name, 3$s - collision object name, 4$s - sound message
add_msg( m_warning, _( "Your %1$s's %2$s rams into %3$s with a %4$s" ),
name, parts[ ret.part ].name(), ret.target_name, snd );
} else {
//~ 1$s - vehicle name, 2$s - part name, 3$s - collision object name
add_msg( m_warning, _( "Your %1$s's %2$s rams into %3$s." ),
name, parts[ ret.part ].name(), ret.target_name );
}
}
sounds::sound( p, smashed ? 80 : 50, sounds::sound_t::combat, snd, false, "smash_success",
"hit_vehicle" );
}
if( smashed && !vert_coll ) {
int turn_amount = rng( 1, 3 ) * std::sqrt( static_cast<double>( part_dmg ) );
turn_amount /= 15;
if( turn_amount < 1 ) {
turn_amount = 1;
}
turn_amount *= 15;
if( turn_amount > 120 ) {
turn_amount = 120;
}
int turn_roll = rng( 0, 100 );
// Probability of skidding increases with higher delta_v
if( turn_roll < std::abs( ( prev_velocity - coll_velocity ) / 100.0f * 2.0f ) ) {
//delta_v = vel1 - vel1_a
//delta_v = 50 mph -> 100% probability of skidding
//delta_v = 25 mph -> 50% probability of skidding
skidding = true;
turn( units::from_degrees( one_in( 2 ) ? turn_amount : -turn_amount ) );
}
}
ret.imp = part_dmg;
return ret;
}
void vehicle::handle_trap( const tripoint &p, int part )
{
int pwh = part_with_feature( part, VPFLAG_WHEEL, true );
if( pwh < 0 ) {
return;
}
map &here = get_map();
Character &player_character = get_player_character();
const trap &tr = here.tr_at( p );
const trap_id t = tr.loadid;
if( t == tr_null ) {
// If the trap doesn't exist, we can't interact with it, so just return
return;
}
vehicle_handle_trap_data veh_data = tr.vehicle_data;
if( veh_data.is_falling ) {
return;
}
const bool seen = player_character.sees( p );
const bool known = player_character.knows_trap( p );
if( seen ) {
if( known ) {
//~ %1$s: name of the vehicle; %2$s: name of the related vehicle part; %3$s: trap name
add_msg( m_bad, _( "The %1$s's %2$s runs over %3$s." ), name, parts[ part ].name(), tr.name() );
} else {
add_msg( m_bad, _( "The %1$s's %2$s runs over something." ), name, parts[ part ].name() );
}
}
if( veh_data.chance >= rng( 1, 100 ) ) {
if( veh_data.sound_volume > 0 ) {
sounds::sound( p, veh_data.sound_volume, sounds::sound_t::combat, veh_data.sound, false,
veh_data.sound_type, veh_data.sound_variant );
}
if( veh_data.do_explosion ) {
explosion_handler::explosion( p, nullptr, veh_data.damage, 0.5f, false, veh_data.shrapnel );
} else {
// Hit the wheel directly since it ran right over the trap.
damage_direct( pwh, veh_data.damage );
}
bool still_has_trap = true;
if( veh_data.remove_trap || veh_data.do_explosion ) {
here.remove_trap( p );
still_has_trap = false;
}
for( const auto &it : veh_data.spawn_items ) {
int cnt = roll_remainder( it.second );
if( cnt > 0 ) {
here.spawn_item( p, it.first, cnt );
}
}
if( veh_data.set_trap ) {
here.trap_set( p, veh_data.set_trap.id() );
still_has_trap = true;
}
if( still_has_trap ) {
const trap &tr = here.tr_at( p );
if( seen || known ) {
// known status has been reset by map::trap_set()
player_character.add_known_trap( p, tr );
}
if( seen && !known ) {
// hard to miss!
const std::string direction = direction_name( direction_from( player_character.pos(), p ) );
add_msg( _( "You've spotted a %1$s to the %2$s!" ), tr.name(), direction );
}
}
}
}
bool vehicle::has_harnessed_animal() const
{
for( size_t e = 0; e < parts.size(); e++ ) {
const 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 ) && mon->has_effect( effect_pet ) ) {
return true;
}
}
}
return false;
}
void vehicle::selfdrive( point p )
{
if( !is_towed() && !magic ) {
for( size_t e = 0; e < parts.size(); e++ ) {
const 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 ) || !mon->has_effect( effect_pet ) ) {
is_following = false;
return;
}
}
}
}
units::angle turn_delta = 15_degrees * p.x;
const float handling_diff = handling_difficulty();
if( turn_delta != 0_degrees ) {
float eff = steering_effectiveness();
if( eff == -2 ) {
return;
}
if( eff < 0 ) {
return;
}
if( eff == 0 ) {
return;
}
turn( turn_delta );
}
if( p.y != 0 ) {
int thr_amount = 100 * ( std::abs( velocity ) < 2000 ? 4 : 5 );
if( cruise_on ) {
cruise_thrust( -p.y * thr_amount );
} else {
thrust( -p.y );
}
}
// TODO: Actually check if we're on land on water (or disable water-skidding)
if( skidding && valid_wheel_config() ) {
///\EFFECT_DEX increases chance of regaining control of a vehicle
///\EFFECT_DRIVING increases chance of regaining control of a vehicle
if( handling_diff * rng( 1, 10 ) < 15 ) {
velocity = static_cast<int>( forward_velocity() );
skidding = false;
move.init( turn_dir );
}
}
}
bool vehicle::check_is_heli_landed()
{
// @TODO - when there are chasms that extend below z-level 0 - perhaps the heli
// will be able to descend into them but for now, assume z-level-0 == the ground.
if( global_pos3().z == 0 || !get_map().has_flag_ter_or_furn( TFLAG_NO_FLOOR, global_pos3() ) ) {
is_flying = false;
return true;
}
return false;
}
bool vehicle::check_heli_descend( player &p )
{
if( !is_rotorcraft() ) {
debugmsg( "A vehicle is somehow flying without being an aircraft" );