forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.cpp
9160 lines (8006 loc) · 305 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 <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <optional>
#include <ostream>
#include <queue>
#include <type_traits>
#include <unordered_map>
#include "active_item_cache.h"
#include "ammo.h"
#include "ammo_effect.h"
#include "artifact.h"
#include "avatar.h"
#include "basecamp.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "character_id.h"
#include "clzones.h"
#include "colony.h"
#include "color.h"
#include "construction.h"
#include "coordinate_conversions.h"
#include "creature.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "distribution_grid.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "explosion.h"
#include "field.h"
#include "field_type.h"
#include "flat_set.h"
#include "fragment_cloud.h"
#include "fungal_effects.h"
#include "game.h"
#include "harvest.h"
#include "iexamine.h"
#include "input.h"
#include "int_id.h"
#include "item.h"
#include "item_contents.h"
#include "item_factory.h"
#include "item_group.h"
#include "item_location.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "lightmap.h"
#include "line.h"
#include "map_functions.h"
#include "map_iterator.h"
#include "map_memory.h"
#include "map_selector.h"
#include "mapbuffer.h"
#include "math_defines.h"
#include "memory_fast.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "morale_types.h"
#include "mtype.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pathfinding.h"
#include "player.h"
#include "point_float.h"
#include "projectile.h"
#include "rng.h"
#include "safe_reference.h"
#include "scent_map.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "submap.h"
#include "tileray.h"
#include "timed_event.h"
#include "translations.h"
#include "trap.h"
#include "ui_manager.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "visitable.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
#include "weighted_list.h"
struct ammo_effect;
using ammo_effect_str_id = string_id<ammo_effect>;
static const ammo_effect_str_id ammo_effect_INCENDIARY( "INCENDIARY" );
static const ammo_effect_str_id ammo_effect_LASER( "LASER" );
static const ammo_effect_str_id ammo_effect_LIGHTNING( "LIGHTNING" );
static const ammo_effect_str_id ammo_effect_PLASMA( "PLASMA" );
static const itype_id itype_autoclave( "autoclave" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_chemistry_set( "chemistry_set" );
static const itype_id itype_dehydrator( "dehydrator" );
static const itype_id itype_electrolysis_kit( "electrolysis_kit" );
static const itype_id itype_food_processor( "food_processor" );
static const itype_id itype_forge( "forge" );
static const itype_id itype_hotplate( "hotplate" );
static const itype_id itype_kiln( "kiln" );
static const itype_id itype_nail( "nail" );
static const itype_id itype_press( "press" );
static const itype_id itype_soldering_iron( "soldering_iron" );
static const itype_id itype_vac_sealer( "vac_sealer" );
static const itype_id itype_welder( "welder" );
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" );
static const std::string flag_RECHARGE( "RECHARGE" );
static const std::string flag_USE_UPS( "USE_UPS" );
static const std::string flag_USES_GRID_POWER( "USES_GRID_POWER" );
static const ter_str_id t_rock_floor_no_roof( "t_rock_floor_no_roof" );
#define dbg(x) DebugLog((x),DC::Map)
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
bool disable_mapgen = false;
map &get_map()
{
return g->m;
}
// 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( static_cast<size_t>( my_MAPSIZE * my_MAPSIZE * OVERMAP_LAYERS ), nullptr );
} else {
grid.resize( static_cast<size_t>( 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( DL::Info ) << "map::map(): my_MAPSIZE: " << my_MAPSIZE << " z-levels enabled:" << zlevels;
traplocs.resize( trap::count() );
}
map::~map() = default;
map &map::operator=( map && ) = default;
void map::set_transparency_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).transparency_cache_dirty.set();
}
}
void map::set_seen_cache_dirty( const tripoint change_location )
{
if( inbounds( change_location ) ) {
level_cache &cache = get_cache( change_location.z );
if( cache.seen_cache_dirty ) {
return;
}
if( cache.seen_cache[change_location.x][change_location.y] != 0.0 ||
cache.camera_cache[change_location.x][change_location.y] != 0.0 ) {
cache.seen_cache_dirty = true;
}
}
}
void map::set_outside_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).outside_cache_dirty = true;
}
}
void map::set_suspension_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).suspension_cache_dirty = true;
}
}
void map::set_floor_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).floor_cache_dirty = true;
}
}
void map::set_seen_cache_dirty( const int zlevel )
{
if( inbounds_z( zlevel ) ) {
level_cache &cache = get_cache( zlevel );
cache.seen_cache_dirty = true;
}
}
void map::set_transparency_cache_dirty( const tripoint &p )
{
if( inbounds( p ) ) {
const tripoint smp = ms_to_sm_copy( p );
get_cache( smp.z ).transparency_cache_dirty.set( smp.x * MAPSIZE + smp.y );
}
}
static submap null_submap;
maptile map::maptile_at( const tripoint &p ) const
{
if( !inbounds( p ) ) {
return maptile( &null_submap, point_zero );
}
return maptile_at_internal( p );
}
maptile map::maptile_at( const tripoint &p )
{
if( !inbounds( p ) ) {
return maptile( &null_submap, point_zero );
}
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( last_full_vehicle_list_dirty ) {
if( !zlevels ) {
last_full_vehicle_list = get_vehicles( tripoint( 0, 0, abs_sub.z ),
tripoint( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, abs_sub.z ) );
} else {
last_full_vehicle_list = get_vehicles( tripoint( 0, 0, -OVERMAP_DEPTH ),
tripoint( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, OVERMAP_HEIGHT ) );
}
last_full_vehicle_list_dirty = false;
}
return last_full_vehicle_list;
}
void map::reset_vehicle_cache( )
{
last_full_vehicle_list_dirty = true;
clear_vehicle_cache();
// Cache all vehicles
const int zmin = zlevels ? -OVERMAP_DEPTH : abs_sub.z;
const int zmax = zlevels ? OVERMAP_HEIGHT : abs_sub.z;
for( int zlev = zmin; zlev <= zmax; zlev++ ) {
auto &ch = get_cache( zlev );
for( const auto &elem : ch.vehicle_list ) {
elem->adjust_zlevel( 0, tripoint_zero );
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;
}
// Get parts
for( const vpart_reference &vpr : veh->get_all_parts() ) {
if( vpr.part().removed ) {
continue;
}
const tripoint p = veh->global_part_pos3( vpr.part() );
level_cache &ch = get_cache( p.z );
ch.veh_in_active_range = true;
ch.veh_cached_parts[p] = std::make_pair( veh, static_cast<int>( vpr.part_index() ) );
if( inbounds( p ) ) {
ch.veh_exists_at[p.x][p.y] = true;
}
}
last_full_vehicle_list_dirty = true;
}
void map::clear_vehicle_point_from_cache( vehicle *veh, const tripoint &pt )
{
if( veh == nullptr ) {
debugmsg( "Tried to clear null vehicle from cache" );
return;
}
level_cache &ch = get_cache( pt.z );
if( inbounds( pt ) ) {
ch.veh_exists_at[pt.x][pt.y] = false;
}
auto it = ch.veh_cached_parts.find( pt );
if( it != ch.veh_cached_parts.end() && it->second.first == veh ) {
ch.veh_cached_parts.erase( it );
}
}
void map::clear_vehicle_cache( )
{
const int zmin = zlevels ? -OVERMAP_DEPTH : abs_sub.z;
const int zmax = zlevels ? OVERMAP_HEIGHT : abs_sub.z;
for( int zlev = zmin; zlev <= zmax; zlev++ ) {
level_cache &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 );
}
ch.veh_in_active_range = false;
}
}
void map::clear_vehicle_list( const int zlev )
{
auto &ch = get_cache( zlev );
ch.vehicle_list.clear();
ch.zone_vehicles.clear();
last_full_vehicle_list_dirty = true;
}
void map::update_vehicle_list( const submap *const to, const int zlev )
{
// Update vehicle data
level_cache &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() );
}
}
last_full_vehicle_list_dirty = true;
}
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 );
}
}
veh->invalidate_towing( true );
submap *const current_submap = get_submap_at_grid( veh->sm_pos );
level_cache &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( );
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 );
veh->detach();
veh->refresh_position();
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_floor_cache_dirty( smz + 1 );
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();
// The bool tracks whether the vehicles is on the map or not.
std::map<vehicle *, bool> connected_vehicles;
for( int zlev = minz; zlev <= maxz; ++zlev ) {
level_cache &cache = get_cache( zlev );
vehicle::enumerate_vehicles( connected_vehicles, cache.vehicle_list );
}
for( std::pair<vehicle *const, bool> &veh_pair : connected_vehicles ) {
veh_pair.first->idle( veh_pair.second );
}
}
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 || ( vehs_v.v->is_rotorcraft() && vehs_v.v->get_z_change() != 0 ) ) {
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();
}
// confirm that veh_in_active_range is still correct for each z-level
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 );
// Check if any vehicles exist in the active range for this z-level
cache.veh_in_active_range = cache.veh_in_active_range &&
std::any_of( std::begin( cache.veh_exists_at ),
std::end( cache.veh_exists_at ), []( const auto & row ) {
return std::any_of( std::begin( row ), std::end( row ), []( bool veh_exists ) {
return veh_exists;
} );
} );
}
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( std::abs( dp.x ) > 1 || std::abs( dp.y ) > 1 || std::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();
if( !vertical ) {
veh.adjust_zlevel( 1, dp1 );
}
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 && !veh.is_rotorcraft() && !veh.is_flying_in_air() ) {
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( coll.part > veh.part_count() ||
veh.part( coll.part ).removed ) {
continue;
}
point collision_point = veh.part( coll.part ).mount;
const int coll_dmg = coll.imp;
// Shock damage, if the target part is a rotor treat as an aimed hit.
if( veh.part_info( coll.part ).rotor_diameter() > 0 ) {
veh.damage( coll.part, coll_dmg, DT_BASH, true );
} else {
impulse += coll_dmg;
veh.damage( coll.part, coll_dmg, DT_BASH );
veh.damage_all( coll_dmg / 2, coll_dmg, DT_BASH, collision_point );
}
}
// prevent vehicle bouncing after the first collision
if( vertical && velocity_before < 0 && coll_velocity > 0 ) {
veh.vertical_velocity = 0; // also affects `coll_velocity` and thus exits the loop
}
} while( collision_attempts-- > 0 && coll_velocity != 0 &&
sgn( coll_velocity ) == sgn( velocity_before ) &&
!collisions.empty() && !veh_veh_coll_flag );
const int velocity_after = coll_velocity;
bool can_move = velocity_after != 0 && sgn( velocity_after ) == sgn( velocity_before );
if( dp.z != 0 && veh.is_rotorcraft() ) {
can_move = true;
}
units::angle coll_turn = 0_degrees;
if( impulse > 0 ) {
coll_turn = shake_vehicle( veh, velocity_before, facing.dir() );
veh.stop_autodriving();
const int volume = std::min<int>( 100, std::sqrt( 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.is_flying_in_air() &&
dp.z == 0 ) {
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 units::angle last_turn_dec = 1_degrees;
if( veh.last_turn < 0_degrees ) {
veh.last_turn += last_turn_dec;
if( veh.last_turn > -last_turn_dec ) {
veh.last_turn = 0_degrees;
}
} else if( veh.last_turn > 0_degrees ) {
veh.last_turn -= last_turn_dec;
if( veh.last_turn < last_turn_dec ) {
veh.last_turn = 0_degrees;
}
}
Character &player_character = get_player_character();
const bool seen = sees_veh( player_character, veh, false );
if( can_move || ( vertical && veh.is_falling ) ) {
// Accept new direction
if( veh.skidding ) {
veh.face.init( veh.turn_dir );
} else {
veh.face = facing;
}
veh.move = facing;
if( coll_turn != 0_degrees ) {
veh.skidding = true;
veh.turn( coll_turn );
}
veh.on_move();
// Actually change position
displace_vehicle( veh, dp1 );
veh.shift_zlevel();
} else if( !vertical ) {
veh.stop();
}
veh.check_falling_or_floating();
// 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.part( 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() ),
false );
}
}
}
if( veh.is_towing() ) {
veh.do_towing_move();
// veh.do_towing_move() may cancel towing, so we need to recheck is_towing here
if( veh.is_towing() && veh.tow_data.get_towed()->tow_cable_too_far() ) {
add_msg( m_info, _( "A towing cable snaps off of %s." ),
veh.tow_data.get_towed()->disp_name() );
veh.tow_data.get_towed()->invalidate_towing( true );
}
}
// Redraw scene, but only if the player is not engaged in an activity and
// the vehicle was seen before or after the move.
if( !player_character.activity && ( seen || sees_veh( player_character, veh, true ) ) ) {
g->invalidate_main_ui_adaptor();
inp_mngr.pump_events();
ui_manager::redraw_invalidated();
refresh_display();
}
return &veh;
}
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 = std::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.part( parm1 ).mount;
veh.damage( parm1, dmg1_part, DT_BASH );
epicenter2 += veh2.part( 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;