-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
map.cpp
8460 lines (7430 loc) · 281 KB
/
map.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 "map.h"
#include <climits>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <queue>
#include <unordered_map>
#include "ammo.h"
#include "artifact.h"
#include "avatar.h"
#include "calendar.h"
#include "colony.h"
#include "coordinate_conversions.h"
#include "clzones.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "emit.h"
#include "event_bus.h"
#include "explosion.h"
#include "fragment_cloud.h"
#include "fungal_effects.h"
#include "game.h"
#include "harvest.h"
#include "iexamine.h"
#include "item.h"
#include "item_factory.h"
#include "item_group.h"
#include "iuse_actor.h"
#include "lightmap.h"
#include "line.h"
#include "map_iterator.h"
#include "map_selector.h"
#include "mapbuffer.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "morale_types.h"
#include "mtype.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pathfinding.h"
#include "projectile.h"
#include "rng.h"
#include "safe_reference.h"
#include "scent_map.h"
#include "sounds.h"
#include "string_formatter.h"
#include "submap.h"
#include "timed_event.h"
#include "translations.h"
#include "trap.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
#include "active_item_cache.h"
#include "basecamp.h"
#include "bodypart.h"
#include "character.h"
#include "color.h"
#include "creature.h"
#include "cursesdef.h"
#include "damage.h"
#include "field.h"
#include "item_location.h"
#include "itype.h"
#include "iuse.h"
#include "map_memory.h"
#include "math_defines.h"
#include "optional.h"
#include "tileray.h"
#include "weighted_list.h"
#include "enums.h"
#include "int_id.h"
#include "string_id.h"
#include "construction.h"
#include "flat_set.h"
static const mtype_id mon_zombie( "mon_zombie" );
static const skill_id skill_traps( "traps" );
static const efftype_id effect_boomered( "boomered" );
static const efftype_id effect_crushed( "crushed" );
#define dbg(x) DebugLog((x),D_MAP) << __FILE__ << ":" << __LINE__ << ": "
static cata::colony<item> nulitems; // Returned when &i_at() is asked for an OOB value
static field nulfield; // Returned when &field_at() is asked for an OOB value
static level_cache nullcache; // Dummy cache for z-levels outside bounds
// Map stack methods.
map_stack::iterator map_stack::erase( map_stack::const_iterator it )
{
return myorigin->i_rem( location, it );
}
void map_stack::insert( const item &newitem )
{
myorigin->add_item_or_charges( location, newitem );
}
units::volume map_stack::max_volume() const
{
if( !myorigin->inbounds( location ) ) {
return 0_ml;
} else if( myorigin->has_furn( location ) ) {
return myorigin->furn( location ).obj().max_volume;
}
return myorigin->ter( location ).obj().max_volume;
}
// Map class methods.
map::map( int mapsize, bool zlev )
{
my_MAPSIZE = mapsize;
zlevels = zlev;
if( zlevels ) {
grid.resize( my_MAPSIZE * my_MAPSIZE * OVERMAP_LAYERS, nullptr );
} else {
grid.resize( my_MAPSIZE * my_MAPSIZE, nullptr );
}
for( auto &ptr : caches ) {
ptr = std::make_unique<level_cache>();
}
for( auto &ptr : pathfinding_caches ) {
ptr = std::make_unique<pathfinding_cache>();
}
dbg( D_INFO ) << "map::map(): my_MAPSIZE: " << my_MAPSIZE << " z-levels enabled:" << zlevels;
traplocs.resize( trap::count() );
}
map::~map() = default;
static submap null_submap;
maptile map::maptile_at( const tripoint &p ) const
{
if( !inbounds( p ) ) {
return maptile( &null_submap, 0, 0 );
}
return maptile_at_internal( p );
}
maptile map::maptile_at( const tripoint &p )
{
if( !inbounds( p ) ) {
return maptile( &null_submap, 0, 0 );
}
return maptile_at_internal( p );
}
maptile map::maptile_at_internal( const tripoint &p ) const
{
point l;
submap *const sm = get_submap_at( p, l );
return maptile( sm, l );
}
maptile map::maptile_at_internal( const tripoint &p )
{
point l;
submap *const sm = get_submap_at( p, l );
return maptile( sm, l );
}
// Vehicle functions
VehicleList map::get_vehicles()
{
if( !zlevels ) {
return get_vehicles( tripoint( 0, 0, abs_sub.z ),
tripoint( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, abs_sub.z ) );
}
return get_vehicles( tripoint( 0, 0, -OVERMAP_DEPTH ),
tripoint( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, OVERMAP_HEIGHT ) );
}
void map::reset_vehicle_cache( const int zlev )
{
clear_vehicle_cache( zlev );
// Cache all vehicles
auto &ch = get_cache( zlev );
ch.veh_in_active_range = false;
for( const auto &elem : ch.vehicle_list ) {
add_vehicle_to_cache( elem );
}
}
void map::add_vehicle_to_cache( vehicle *veh )
{
if( veh == nullptr ) {
debugmsg( "Tried to add null vehicle to cache" );
return;
}
auto &ch = get_cache( veh->sm_pos.z );
ch.veh_in_active_range = true;
// Get parts
std::vector<vehicle_part> &parts = veh->parts;
int partid = 0;
for( std::vector<vehicle_part>::iterator it = parts.begin(),
end = parts.end(); it != end; ++it, ++partid ) {
if( it->removed ) {
continue;
}
const tripoint p = veh->global_part_pos3( *it );
ch.veh_cached_parts.insert( std::make_pair( p,
std::make_pair( veh, partid ) ) );
if( inbounds( p ) ) {
ch.veh_exists_at[p.x][p.y] = true;
}
}
}
void map::update_vehicle_cache( vehicle *veh, const int old_zlevel )
{
if( veh == nullptr ) {
debugmsg( "Tried to add null vehicle to cache" );
return;
}
// Existing must be cleared
auto &ch = get_cache( old_zlevel );
auto it = ch.veh_cached_parts.begin();
const auto end = ch.veh_cached_parts.end();
while( it != end ) {
if( it->second.first == veh ) {
const tripoint p = it->first;
if( inbounds( p ) ) {
ch.veh_exists_at[p.x][p.y] = false;
}
ch.veh_cached_parts.erase( it++ );
// If something was resting on vehicle, drop it
support_dirty( tripoint( p.xy(), old_zlevel + 1 ) );
} else {
++it;
}
}
add_vehicle_to_cache( veh );
}
void map::clear_vehicle_cache( const int zlev )
{
auto &ch = get_cache( zlev );
while( !ch.veh_cached_parts.empty() ) {
const auto part = ch.veh_cached_parts.begin();
const auto &p = part->first;
if( inbounds( p ) ) {
ch.veh_exists_at[p.x][p.y] = false;
}
ch.veh_cached_parts.erase( part );
}
}
void map::clear_vehicle_list( const int zlev )
{
auto &ch = get_cache( zlev );
ch.vehicle_list.clear();
ch.zone_vehicles.clear();
}
void map::update_vehicle_list( const submap *const to, const int zlev )
{
// Update vehicle data
auto &ch = get_cache( zlev );
for( const auto &elem : to->vehicles ) {
ch.vehicle_list.insert( elem.get() );
if( !elem->loot_zones.empty() ) {
ch.zone_vehicles.insert( elem.get() );
}
}
}
std::unique_ptr<vehicle> map::detach_vehicle( vehicle *veh )
{
if( veh == nullptr ) {
debugmsg( "map::detach_vehicle was passed nullptr" );
return std::unique_ptr<vehicle>();
}
int z = veh->sm_pos.z;
if( z < -OVERMAP_DEPTH || z > OVERMAP_HEIGHT ) {
debugmsg( "detach_vehicle got a vehicle outside allowed z-level range! name=%s, submap:%d,%d,%d",
veh->name, veh->sm_pos.x, veh->sm_pos.y, veh->sm_pos.z );
// Try to fix by moving the vehicle here
z = veh->sm_pos.z = abs_sub.z;
}
// Unboard all passengers before detaching
for( auto const &part : veh->get_avail_parts( VPFLAG_BOARDABLE ) ) {
player *passenger = part.get_passenger();
if( passenger ) {
unboard_vehicle( part, passenger );
}
}
submap *const current_submap = get_submap_at_grid( veh->sm_pos );
auto &ch = get_cache( z );
for( size_t i = 0; i < current_submap->vehicles.size(); i++ ) {
if( current_submap->vehicles[i].get() == veh ) {
ch.vehicle_list.erase( veh );
ch.zone_vehicles.erase( veh );
reset_vehicle_cache( z );
std::unique_ptr<vehicle> result = std::move( current_submap->vehicles[i] );
current_submap->vehicles.erase( current_submap->vehicles.begin() + i );
if( veh->tracking_on ) {
overmap_buffer.remove_vehicle( veh );
}
dirty_vehicle_list.erase( veh );
return result;
}
}
debugmsg( "detach_vehicle can't find it! name=%s, submap:%d,%d,%d", veh->name, veh->sm_pos.x,
veh->sm_pos.y, veh->sm_pos.z );
return std::unique_ptr<vehicle>();
}
void map::destroy_vehicle( vehicle *veh )
{
detach_vehicle( veh );
}
void map::on_vehicle_moved( const int smz )
{
set_outside_cache_dirty( smz );
set_transparency_cache_dirty( smz );
set_floor_cache_dirty( smz );
set_pathfinding_cache_dirty( smz );
}
void map::vehmove()
{
// give vehicles movement points
VehicleList vehicle_list;
int minz = zlevels ? -OVERMAP_DEPTH : abs_sub.z;
int maxz = zlevels ? OVERMAP_HEIGHT : abs_sub.z;
for( int zlev = minz; zlev <= maxz; ++zlev ) {
level_cache &cache = get_cache( zlev );
for( vehicle *veh : cache.vehicle_list ) {
veh->gain_moves();
veh->slow_leak();
wrapped_vehicle w;
w.v = veh;
vehicle_list.push_back( w );
}
}
// 15 equals 3 >50mph vehicles, or up to 15 slow (1 square move) ones
// But 15 is too low for V12 death-bikes, let's put 100 here
for( int count = 0; count < 100; count++ ) {
if( !vehproceed( vehicle_list ) ) {
break;
}
}
// Process item removal on the vehicles that were modified this turn.
// Use a copy because part_removal_cleanup can modify the container.
auto temp = dirty_vehicle_list;
for( const auto &elem : temp ) {
auto same_ptr = [ elem ]( const struct wrapped_vehicle & tgt ) {
return elem == tgt.v;
};
if( std::find_if( vehicle_list.begin(), vehicle_list.end(), same_ptr ) !=
vehicle_list.end() ) {
elem->part_removal_cleanup();
}
}
dirty_vehicle_list.clear();
}
bool map::vehproceed( VehicleList &vehicle_list )
{
wrapped_vehicle *cur_veh = nullptr;
float max_of_turn = 0;
// First horizontal movement
for( wrapped_vehicle &vehs_v : vehicle_list ) {
if( vehs_v.v->of_turn > max_of_turn ) {
cur_veh = &vehs_v;
max_of_turn = cur_veh->v->of_turn;
}
}
// Then vertical-only movement
if( cur_veh == nullptr ) {
for( wrapped_vehicle &vehs_v : vehicle_list ) {
if( vehs_v.v->is_falling ) {
cur_veh = &vehs_v;
break;
}
}
}
if( cur_veh == nullptr ) {
return false;
}
cur_veh->v = cur_veh->v->act_on_map();
if( cur_veh->v == nullptr ) {
vehicle_list = get_vehicles();
}
return true;
}
static bool sees_veh( const Creature &c, vehicle &veh, bool force_recalc )
{
const auto &veh_points = veh.get_points( force_recalc );
return std::any_of( veh_points.begin(), veh_points.end(), [&c]( const tripoint & pt ) {
return c.sees( pt );
} );
}
vehicle *map::move_vehicle( vehicle &veh, const tripoint &dp, const tileray &facing )
{
if( dp == tripoint_zero ) {
debugmsg( "Empty displacement vector" );
return &veh;
} else if( abs( dp.x ) > 1 || abs( dp.y ) > 1 || abs( dp.z ) > 1 ) {
debugmsg( "Invalid displacement vector: %d, %d, %d", dp.x, dp.y, dp.z );
return &veh;
}
// Split the movement into horizontal and vertical for easier processing
if( dp.xy() != point_zero && dp.z != 0 ) {
vehicle *const new_pointer = move_vehicle( veh, tripoint( dp.xy(), 0 ), facing );
if( !new_pointer ) {
return nullptr;
}
vehicle *const result = move_vehicle( *new_pointer, tripoint( 0, 0, dp.z ), facing );
if( !result ) {
return nullptr;
}
result->is_falling = false;
return result;
}
const bool vertical = dp.z != 0;
// Ensured by the splitting above
assert( vertical == ( dp.xy() == point_zero ) );
const int target_z = dp.z + veh.sm_pos.z;
if( target_z < -OVERMAP_DEPTH || target_z > OVERMAP_HEIGHT ) {
return &veh;
}
veh.precalc_mounts( 1, veh.skidding ? veh.turn_dir : facing.dir(), veh.pivot_point() );
// cancel out any movement of the vehicle due only to a change in pivot
tripoint dp1 = dp - veh.pivot_displacement();
int impulse = 0;
std::vector<veh_collision> collisions;
// Find collisions
// Velocity of car before collision
// Split into vertical and horizontal movement
const int &coll_velocity = vertical ? veh.vertical_velocity : veh.velocity;
const int velocity_before = coll_velocity;
if( velocity_before == 0 ) {
debugmsg( "%s tried to move %s with no velocity",
veh.name, vertical ? "vertically" : "horizontally" );
return &veh;
}
bool veh_veh_coll_flag = false;
// Try to collide multiple times
size_t collision_attempts = 10;
do {
collisions.clear();
veh.collision( collisions, dp1, false );
// Vehicle collisions
std::map<vehicle *, std::vector<veh_collision> > veh_collisions;
for( auto &coll : collisions ) {
if( coll.type != veh_coll_veh ) {
continue;
}
veh_veh_coll_flag = true;
// Only collide with each vehicle once
veh_collisions[ static_cast<vehicle *>( coll.target ) ].push_back( coll );
}
for( auto &pair : veh_collisions ) {
impulse += vehicle_vehicle_collision( veh, *pair.first, pair.second );
}
// Non-vehicle collisions
for( const auto &coll : collisions ) {
if( coll.type == veh_coll_veh ) {
continue;
}
if( static_cast<size_t>( coll.part ) > veh.parts.size() ) {
continue;
}
const point &collision_point = veh.parts[coll.part].mount;
const int coll_dmg = coll.imp;
impulse += coll_dmg;
// Shock damage
veh.damage( coll.part, coll_dmg, DT_BASH );
veh.damage_all( coll_dmg / 2, coll_dmg, DT_BASH, collision_point );
}
} while( collision_attempts-- > 0 &&
sgn( coll_velocity ) == sgn( velocity_before ) &&
!collisions.empty() && !veh_veh_coll_flag );
if( vertical && !collisions.empty() ) {
// A big hack, should be removed when possible
veh.vertical_velocity = 0;
}
const int velocity_after = coll_velocity;
const bool can_move = velocity_after != 0 && sgn( velocity_after ) == sgn( velocity_before );
int coll_turn = 0;
if( impulse > 0 ) {
coll_turn = shake_vehicle( veh, velocity_before, facing.dir() );
const int volume = std::min<int>( 100, sqrtf( impulse ) );
// TODO: Center the sound at weighted (by impulse) average of collisions
sounds::sound( veh.global_pos3(), volume, sounds::sound_t::combat, _( "crash!" ),
false, "smash_success", "hit_vehicle" );
}
if( veh_veh_coll_flag ) {
// Break here to let the hit vehicle move away
return nullptr;
}
// If not enough wheels, mess up the ground a bit.
if( !vertical && !veh.valid_wheel_config() && !veh.is_in_water() ) {
veh.velocity += veh.velocity < 0 ? 2000 : -2000;
for( const auto &p : veh.get_points() ) {
const ter_id &pter = ter( p );
if( pter == t_dirt || pter == t_grass ) {
ter_set( p, t_dirtmound );
}
}
}
const int last_turn_dec = 1;
if( veh.last_turn < 0 ) {
veh.last_turn += last_turn_dec;
if( veh.last_turn > -last_turn_dec ) {
veh.last_turn = 0;
}
} else if( veh.last_turn > 0 ) {
veh.last_turn -= last_turn_dec;
if( veh.last_turn < last_turn_dec ) {
veh.last_turn = 0;
}
}
const bool seen = sees_veh( g->u, veh, false );
vehicle *new_vehicle = &veh;
if( can_move ) {
// Accept new direction
if( veh.skidding ) {
veh.face.init( veh.turn_dir );
} else {
veh.face = facing;
}
veh.move = facing;
if( coll_turn != 0 ) {
veh.skidding = true;
veh.turn( coll_turn );
}
veh.on_move();
// Actually change position
displace_vehicle( *new_vehicle, dp1 );
} else if( !vertical ) {
veh.stop();
}
// If the PC is in the currently moved vehicle, adjust the
// view offset.
if( g->u.controlling_vehicle && veh_pointer_or_null( veh_at( g->u.pos() ) ) == &veh ) {
g->calc_driving_offset( &veh );
if( veh.skidding && can_move ) {
// TODO: Make skid recovery in air hard
veh.possibly_recover_from_skid();
}
}
// Now we're gonna handle traps we're standing on (if we're still moving).
if( !vertical && can_move ) {
const auto wheel_indices = veh.wheelcache; // Don't use a reference here, it causes a crash.
// Values to deal with crushing items.
// The math needs to be floating-point to work, so the values might as well be.
const float vehicle_grounded_wheel_area = static_cast<int>( vehicle_wheel_traction( veh, true ) );
const float weight_to_damage_factor = 0.05; // Nobody likes a magic number.
const float vehicle_mass_kg = to_kilogram( veh.total_mass() );
for( auto &w : wheel_indices ) {
const tripoint wheel_p = veh.global_part_pos3( w );
if( one_in( 2 ) && displace_water( wheel_p ) ) {
sounds::sound( wheel_p, 4, sounds::sound_t::movement, _( "splash!" ), false,
"environment", "splash" );
}
veh.handle_trap( wheel_p, w );
if( !has_flag( "SEALED", wheel_p ) ) {
const float wheel_area = veh.parts[ w ].wheel_area();
// Damage is calculated based on the weight of the vehicle,
// The area of it's wheels, and the area of the wheel running over the items.
// This number is multiplied by weight_to_damage_factor to get reasonable results, damage-wise.
const int wheel_damage = static_cast<int>( ( ( wheel_area / vehicle_grounded_wheel_area ) *
vehicle_mass_kg ) * weight_to_damage_factor );
//~ %1$s: vehicle name
smash_items( wheel_p, wheel_damage, string_format( _( "weight of %1$s" ), veh.disp_name() ) );
}
}
}
// Redraw scene
// But only if the vehicle was seen before or after the move
if( seen || sees_veh( g->u, veh, true ) ) {
g->draw();
refresh_display();
}
return new_vehicle;
}
float map::vehicle_vehicle_collision( vehicle &veh, vehicle &veh2,
const std::vector<veh_collision> &collisions )
{
if( &veh == &veh2 ) {
debugmsg( "Vehicle %s collided with itself", veh.name );
return 0.0f;
}
// Effects of colliding with another vehicle:
// transfers of momentum, skidding,
// parts are damaged/broken on both sides,
// remaining times are normalized
const veh_collision &c = collisions[0];
add_msg( m_bad, _( "The %1$s's %2$s collides with %3$s's %4$s." ),
veh.name, veh.part_info( c.part ).name(),
veh2.name, veh2.part_info( c.target_part ).name() );
const bool vertical = veh.sm_pos.z != veh2.sm_pos.z;
// Used to calculate the epicenter of the collision.
point epicenter1;
point epicenter2;
float dmg;
// Vertical collisions will be simpler for a while (1D)
if( !vertical ) {
// For reference, a cargo truck weighs ~25300, a bicycle 690,
// and 38mph is 3800 'velocity'
rl_vec2d velo_veh1 = veh.velo_vec();
rl_vec2d velo_veh2 = veh2.velo_vec();
const float m1 = to_kilogram( veh.total_mass() );
const float m2 = to_kilogram( veh2.total_mass() );
//Energy of vehicle1 and vehicle2 before collision
float E = 0.5 * m1 * velo_veh1.magnitude() * velo_veh1.magnitude() +
0.5 * m2 * velo_veh2.magnitude() * velo_veh2.magnitude();
// Collision_axis
point cof1 = veh .rotated_center_of_mass();
point cof2 = veh2.rotated_center_of_mass();
int &x_cof1 = cof1.x;
int &y_cof1 = cof1.y;
int &x_cof2 = cof2.x;
int &y_cof2 = cof2.y;
rl_vec2d collision_axis_y;
collision_axis_y.x = ( veh.global_pos3().x + x_cof1 ) - ( veh2.global_pos3().x + x_cof2 );
collision_axis_y.y = ( veh.global_pos3().y + y_cof1 ) - ( veh2.global_pos3().y + y_cof2 );
collision_axis_y = collision_axis_y.normalized();
rl_vec2d collision_axis_x = collision_axis_y.rotated( M_PI / 2 );
// imp? & delta? & final? reworked:
// newvel1 =( vel1 * ( mass1 - mass2 ) + ( 2 * mass2 * vel2 ) ) / ( mass1 + mass2 )
// as per http://en.wikipedia.org/wiki/Elastic_collision
//velocity of veh1 before collision in the direction of collision_axis_y
float vel1_y = collision_axis_y.dot_product( velo_veh1 );
float vel1_x = collision_axis_x.dot_product( velo_veh1 );
//velocity of veh2 before collision in the direction of collision_axis_y
float vel2_y = collision_axis_y.dot_product( velo_veh2 );
float vel2_x = collision_axis_x.dot_product( velo_veh2 );
// e = 0 -> inelastic collision
// e = 1 -> elastic collision
float e = get_collision_factor( vel1_y / 100 - vel2_y / 100 );
// Velocity after collision
// vel1_x_a = vel1_x, because in x-direction we have no transmission of force
float vel1_x_a = vel1_x;
float vel2_x_a = vel2_x;
// Transmission of force only in direction of collision_axix_y
// Equation: partially elastic collision
float vel1_y_a = ( m2 * vel2_y * ( 1 + e ) + vel1_y * ( m1 - m2 * e ) ) / ( m1 + m2 );
float vel2_y_a = ( m1 * vel1_y * ( 1 + e ) + vel2_y * ( m2 - m1 * e ) ) / ( m1 + m2 );
// Add both components; Note: collision_axis is normalized
rl_vec2d final1 = collision_axis_y * vel1_y_a + collision_axis_x * vel1_x_a;
rl_vec2d final2 = collision_axis_y * vel2_y_a + collision_axis_x * vel2_x_a;
veh.move.init( final1.as_point() );
if( final1.dot_product( veh.face_vec() ) < 0 ) {
// Car is being pushed backwards. Make it move backwards
veh.velocity = -final1.magnitude();
} else {
veh.velocity = final1.magnitude();
}
veh2.move.init( final2.as_point() );
if( final2.dot_product( veh2.face_vec() ) < 0 ) {
// Car is being pushed backwards. Make it move backwards
veh2.velocity = -final2.magnitude();
} else {
veh2.velocity = final2.magnitude();
}
//give veh2 the initiative to proceed next before veh1
float avg_of_turn = ( veh2.of_turn + veh.of_turn ) / 2;
if( avg_of_turn < .1f ) {
avg_of_turn = .1f;
}
veh.of_turn = avg_of_turn * .9;
veh2.of_turn = avg_of_turn * 1.1;
//Energy after collision
float E_a = 0.5 * m1 * final1.magnitude() * final1.magnitude() +
0.5 * m2 * final2.magnitude() * final2.magnitude();
float d_E = E - E_a; //Lost energy at collision -> deformation energy
dmg = std::abs( d_E / 1000 / 2000 ); //adjust to balance damage
} else {
const float m1 = to_kilogram( veh.total_mass() );
// Collision is perfectly inelastic for simplicity
// Assume veh2 is standing still
dmg = abs( veh.vertical_velocity / 100 ) * m1 / 10;
veh.vertical_velocity = 0;
}
float dmg_veh1 = dmg * 0.5;
float dmg_veh2 = dmg * 0.5;
int coll_parts_cnt = 0; //quantity of colliding parts between veh1 and veh2
for( const auto &veh_veh_coll : collisions ) {
if( &veh2 == static_cast<vehicle *>( veh_veh_coll.target ) ) {
coll_parts_cnt++;
}
}
const float dmg1_part = dmg_veh1 / coll_parts_cnt;
const float dmg2_part = dmg_veh2 / coll_parts_cnt;
//damage colliding parts (only veh1 and veh2 parts)
for( const auto &veh_veh_coll : collisions ) {
if( &veh2 != static_cast<vehicle *>( veh_veh_coll.target ) ) {
continue;
}
int parm1 = veh.part_with_feature( veh_veh_coll.part, VPFLAG_ARMOR, true );
if( parm1 < 0 ) {
parm1 = veh_veh_coll.part;
}
int parm2 = veh2.part_with_feature( veh_veh_coll.target_part, VPFLAG_ARMOR, true );
if( parm2 < 0 ) {
parm2 = veh_veh_coll.target_part;
}
epicenter1 += veh.parts[parm1].mount;
veh.damage( parm1, dmg1_part, DT_BASH );
epicenter2 += veh2.parts[parm2].mount;
veh2.damage( parm2, dmg2_part, DT_BASH );
}
epicenter2.x /= coll_parts_cnt;
epicenter2.y /= coll_parts_cnt;
if( dmg2_part > 100 ) {
// Shake vehicle because of collision
veh2.damage_all( dmg2_part / 2, dmg2_part, DT_BASH, epicenter2 );
}
if( dmg_veh1 > 800 ) {
veh.skidding = true;
}
if( dmg_veh2 > 800 ) {
veh2.skidding = true;
}
// Return the impulse of the collision
return dmg_veh1;
}
bool map::check_vehicle_zones( const int zlev )
{
for( auto veh : get_cache( zlev ).zone_vehicles ) {
if( veh->zones_dirty ) {
return true;
}
}
return false;
}
std::vector<zone_data *> map::get_vehicle_zones( const int zlev )
{
std::vector<zone_data *> veh_zones;
bool rebuild = false;
for( auto veh : get_cache( zlev ).zone_vehicles ) {
if( veh->refresh_zones() ) {
rebuild = true;
}
for( auto &zone : veh->loot_zones ) {
veh_zones.emplace_back( &zone.second );
}
}
if( rebuild ) {
zone_manager::get_manager().cache_vzones();
}
return veh_zones;
}
void map::register_vehicle_zone( vehicle *veh, const int zlev )
{
auto &ch = get_cache( zlev );
ch.zone_vehicles.insert( veh );
}
bool map::deregister_vehicle_zone( zone_data &zone )
{
if( const cata::optional<vpart_reference> vp = veh_at( getlocal(
zone.get_start_point() ) ).part_with_feature( "CARGO", false ) ) {
auto bounds = vp->vehicle().loot_zones.equal_range( vp->mount() );
for( auto it = bounds.first; it != bounds.second; it++ ) {
if( &zone == &( it->second ) ) {
vp->vehicle().loot_zones.erase( it );
return true;
}
}
}
return false;
}
// 3D vehicle functions
VehicleList map::get_vehicles( const tripoint &start, const tripoint &end )
{
const int chunk_sx = std::max( 0, ( start.x / SEEX ) - 1 );
const int chunk_ex = std::min( my_MAPSIZE - 1, ( end.x / SEEX ) + 1 );
const int chunk_sy = std::max( 0, ( start.y / SEEY ) - 1 );
const int chunk_ey = std::min( my_MAPSIZE - 1, ( end.y / SEEY ) + 1 );
const int chunk_sz = start.z;
const int chunk_ez = end.z;
VehicleList vehs;
for( int cx = chunk_sx; cx <= chunk_ex; ++cx ) {
for( int cy = chunk_sy; cy <= chunk_ey; ++cy ) {
for( int cz = chunk_sz; cz <= chunk_ez; ++cz ) {
submap *current_submap = get_submap_at_grid( { cx, cy, cz } );
for( const auto &elem : current_submap->vehicles ) {
// Ensure the vehicle z-position is correct
elem->sm_pos.z = cz;
wrapped_vehicle w;
w.v = elem.get();
w.pos = w.v->global_pos3();
vehs.push_back( w );
}
}
}
}
return vehs;
}
optional_vpart_position map::veh_at( const tripoint &p ) const
{
if( !const_cast<map *>( this )->get_cache( p.z ).veh_in_active_range || !inbounds( p ) ) {
return optional_vpart_position( cata::nullopt );
}
int part_num = 1;
vehicle *const veh = const_cast<map *>( this )->veh_at_internal( p, part_num );
if( !veh ) {
return optional_vpart_position( cata::nullopt );
}
return optional_vpart_position( vpart_position( *veh, part_num ) );
}
const vehicle *map::veh_at_internal( const tripoint &p, int &part_num ) const
{
// This function is called A LOT. Move as much out of here as possible.
const auto &ch = get_cache_ref( p.z );
if( !ch.veh_in_active_range || !ch.veh_exists_at[p.x][p.y] ) {
part_num = -1;
return nullptr; // Clear cache indicates no vehicle. This should optimize a great deal.
}
const auto it = ch.veh_cached_parts.find( p );
if( it != ch.veh_cached_parts.end() ) {
part_num = it->second.second;
return it->second.first;
}
debugmsg( "vehicle part cache indicated vehicle not found: %d %d %d", p.x, p.y, p.z );
part_num = -1;
return nullptr;
}
vehicle *map::veh_at_internal( const tripoint &p, int &part_num )
{
return const_cast<vehicle *>( const_cast<const map *>( this )->veh_at_internal( p, part_num ) );
}
void map::board_vehicle( const tripoint &pos, player *p )
{
if( p == nullptr ) {
debugmsg( "map::board_vehicle: null player" );
return;
}
const cata::optional<vpart_reference> vp = veh_at( pos ).part_with_feature( VPFLAG_BOARDABLE,
true );
if( !vp ) {
if( p->grab_point.x == 0 && p->grab_point.y == 0 ) {
debugmsg( "map::board_vehicle: vehicle not found" );
}
return;
}
if( vp->part().has_flag( vehicle_part::passenger_flag ) ) {
player *psg = vp->vehicle().get_passenger( vp->part_index() );
debugmsg( "map::board_vehicle: passenger (%s) is already there",
psg ? psg->name : "<null>" );
unboard_vehicle( pos );
}
vp->part().set_flag( vehicle_part::passenger_flag );
vp->part().passenger_id = p->getID();
vp->vehicle().invalidate_mass();
p->setpos( pos );
p->in_vehicle = true;
if( p == &g->u ) {
g->update_map( g->u );
}
}
void map::unboard_vehicle( const vpart_reference &vp, player *passenger, bool dead_passenger )
{
// Mark the part as un-occupied regardless of whether there's a live passenger here.
vp.part().remove_flag( vehicle_part::passenger_flag );
vp.vehicle().invalidate_mass();
if( !passenger ) {
if( !dead_passenger ) {
debugmsg( "map::unboard_vehicle: passenger not found" );
}
return;
}
passenger->in_vehicle = false;
// Only make vehicle go out of control if the driver is the one unboarding.
if( passenger->controlling_vehicle ) {
vp.vehicle().skidding = true;
}
passenger->controlling_vehicle = false;
}
void map::unboard_vehicle( const tripoint &p, bool dead_passenger )
{
const cata::optional<vpart_reference> vp = veh_at( p ).part_with_feature( VPFLAG_BOARDABLE, false );
player *passenger = nullptr;
if( !vp ) {
debugmsg( "map::unboard_vehicle: vehicle not found" );
// Try and force unboard the player anyway.
passenger = g->critter_at<player>( p );
if( passenger ) {
passenger->in_vehicle = false;
passenger->controlling_vehicle = false;
}
return;
}
passenger = vp->get_passenger();
unboard_vehicle( *vp, passenger, dead_passenger );
}