-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmap.cpp
10643 lines (9379 loc) · 377 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 <climits>
#include <cmath>
#include <cstdlib>
#include <optional>
#include <ostream>
#include <queue>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include "active_item_cache.h"
#include "ammo.h"
#include "ammo_effect.h"
#include "avatar.h"
#include "basecamp.h"
#include "bodypart.h"
#include "cached_options.h"
#include "calendar.h"
#include "cata_assert.h"
#include "cata_type_traits.h"
#include "character.h"
#include "character_id.h"
#include "clzones.h"
#include "colony.h"
#include "color.h"
#include "construction.h"
#include "coordinates.h"
#include "coords_fwd.h"
#include "creature.h"
#include "creature_tracker.h"
#include "cuboid_rectangle.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "do_turn.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "explosion.h"
#include "field.h"
#include "field_type.h"
#include "flag.h"
#include "fragment_cloud.h"
#include "fungal_effects.h"
#include "game.h"
#include "harvest.h"
#include "iexamine.h"
#include "input.h"
#include "item.h"
#include "item_category.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 "magic_ter_furn_transform.h"
#include "map_iterator.h"
#include "map_memory.h"
#include "map_selector.h"
#include "mapbuffer.h"
#include "mapdata.h"
#include "mapgen.h"
#include "material.h"
#include "math_defines.h"
#include "mission.h"
#include "memory_fast.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "mtype.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pathfinding.h"
#include "pocket_type.h"
#include "projectile.h"
#include "ranged.h"
#include "relic.h"
#include "ret_val.h"
#include "rng.h"
#include "safe_reference.h"
#include "scent_map.h"
#include "shadowcasting.h"
#include "sounds.h"
#include "string_formatter.h"
#include "submap.h"
#include "tileray.h"
#include "translations.h"
#include "trap.h"
#include "ui_manager.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "viewer.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
#include "weighted_list.h"
#if defined(TILES)
#include "cata_tiles.h" // all animation functions will be pushed out to a cata_tiles function in some manner
#include "sdltiles.h"
#endif
static const ammo_effect_str_id ammo_effect_IGNITE( "IGNITE" );
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 ammotype ammo_battery( "battery" );
static const bionic_id bio_shock_absorber( "bio_shock_absorber" );
static const damage_type_id damage_bash( "bash" );
static const efftype_id effect_boomered( "boomered" );
static const efftype_id effect_crushed( "crushed" );
static const efftype_id effect_fake_common_cold( "fake_common_cold" );
static const efftype_id effect_fake_flu( "fake_flu" );
static const efftype_id effect_gliding( "gliding" );
static const efftype_id effect_incorporeal( "incorporeal" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_slow_descent( "slow_descent" );
static const efftype_id effect_strengthened_gravity( "strengthened_gravity" );
static const efftype_id effect_weakened_gravity( "weakened_gravity" );
static const field_type_str_id field_fd_clairvoyant( "fd_clairvoyant" );
static const flag_id json_flag_AVATAR_ONLY( "AVATAR_ONLY" );
static const flag_id json_flag_JETPACK( "JETPACK" );
static const flag_id json_flag_LEVITATION( "LEVITATION" );
static const flag_id json_flag_PRESERVE_SPAWN_LOC( "PRESERVE_SPAWN_LOC" );
static const flag_id json_flag_PROXIMITY( "PROXIMITY" );
static const flag_id json_flag_UNDODGEABLE( "UNDODGEABLE" );
static const furn_str_id furn_f_clear( "f_clear" );
static const furn_str_id furn_f_rubble( "f_rubble" );
static const furn_str_id furn_f_rubble_rock( "f_rubble_rock" );
static const furn_str_id furn_f_wreckage( "f_wreckage" );
static const item_group_id Item_spawn_data_default_zombie_clothes( "default_zombie_clothes" );
static const item_group_id Item_spawn_data_default_zombie_items( "default_zombie_items" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_maple_sap( "maple_sap" );
static const itype_id itype_nail( "nail" );
static const itype_id itype_pipe( "pipe" );
static const itype_id itype_rock( "rock" );
static const itype_id itype_scrap( "scrap" );
static const itype_id itype_splinter( "splinter" );
static const itype_id itype_steel_chunk( "steel_chunk" );
static const itype_id itype_wire( "wire" );
static const json_character_flag json_flag_WALL_CLING( "WALL_CLING" );
static const material_id material_glass( "glass" );
static const mtype_id mon_zombie( "mon_zombie" );
static const species_id species_FERAL( "FERAL" );
static const ter_str_id ter_t_bars( "t_bars" );
static const ter_str_id ter_t_card_industrial( "t_card_industrial" );
static const ter_str_id ter_t_card_military( "t_card_military" );
static const ter_str_id ter_t_card_reader_broken( "t_card_reader_broken" );
static const ter_str_id ter_t_card_science( "t_card_science" );
static const ter_str_id ter_t_dirt( "t_dirt" );
static const ter_str_id ter_t_dirtmound( "t_dirtmound" );
static const ter_str_id ter_t_door_b( "t_door_b" );
static const ter_str_id ter_t_door_bar_c( "t_door_bar_c" );
static const ter_str_id ter_t_door_bar_locked( "t_door_bar_locked" );
static const ter_str_id ter_t_door_bar_o( "t_door_bar_o" );
static const ter_str_id ter_t_door_c( "t_door_c" );
static const ter_str_id ter_t_door_frame( "t_door_frame" );
static const ter_str_id ter_t_door_locked( "t_door_locked" );
static const ter_str_id ter_t_door_locked_alarm( "t_door_locked_alarm" );
static const ter_str_id ter_t_door_locked_peep( "t_door_locked_peep" );
static const ter_str_id ter_t_floor( "t_floor" );
static const ter_str_id ter_t_floor_wax( "t_floor_wax" );
static const ter_str_id ter_t_gas_pump( "t_gas_pump" );
static const ter_str_id ter_t_gas_pump_smashed( "t_gas_pump_smashed" );
static const ter_str_id ter_t_grass( "t_grass" );
static const ter_str_id ter_t_open_air( "t_open_air" );
static const ter_str_id ter_t_reb_cage( "t_reb_cage" );
static const ter_str_id ter_t_rock_floor( "t_rock_floor" );
static const ter_str_id ter_t_rootcellar( "t_rootcellar" );
static const ter_str_id ter_t_stump( "t_stump" );
static const ter_str_id ter_t_tree_birch( "t_tree_birch" );
static const ter_str_id ter_t_tree_birch_harvested( "t_tree_birch_harvested" );
static const ter_str_id ter_t_tree_dead( "t_tree_dead" );
static const ter_str_id ter_t_tree_deadpine( "t_tree_deadpine" );
static const ter_str_id ter_t_tree_hickory( "t_tree_hickory" );
static const ter_str_id ter_t_tree_hickory_dead( "t_tree_hickory_dead" );
static const ter_str_id ter_t_tree_hickory_harvested( "t_tree_hickory_harvested" );
static const ter_str_id ter_t_tree_maple_tapped( "t_tree_maple_tapped" );
static const ter_str_id ter_t_tree_pine( "t_tree_pine" );
static const ter_str_id ter_t_tree_willow( "t_tree_willow" );
static const ter_str_id ter_t_tree_willow_harvested( "t_tree_willow_harvested" );
static const ter_str_id ter_t_tree_young( "t_tree_young" );
static const ter_str_id ter_t_trunk( "t_trunk" );
static const ter_str_id ter_t_vat( "t_vat" );
static const ter_str_id ter_t_wall_glass( "t_wall_glass" );
static const ter_str_id ter_t_wall_glass_alarm( "t_wall_glass_alarm" );
static const ter_str_id ter_t_water_sh( "t_water_sh" );
static const ter_str_id ter_t_wax( "t_wax" );
static const ter_str_id ter_t_window( "t_window" );
static const ter_str_id ter_t_window_alarm( "t_window_alarm" );
static const ter_str_id ter_t_window_empty( "t_window_empty" );
static const ter_str_id ter_t_window_no_curtains( "t_window_no_curtains" );
static const trait_id trait_SCHIZOPHRENIC( "SCHIZOPHRENIC" );
static const trap_str_id tr_unfinished_construction( "tr_unfinished_construction" );
#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( map &, const item &newitem )
{
myorigin->add_item_or_charges( location, newitem );
}
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 ), my_HALF_MAPSIZE( mapsize / 2 ),
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 : 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()
{
if( ( _main_requires_cleanup && !_main_cleanup_override ) ||
( _main_cleanup_override && *_main_cleanup_override ) ) {
get_map().reset_vehicles_sm_pos();
get_map().rebuild_vehicle_level_caches();
g->load_npcs();
}
}
// NOLINTNEXTLINE(performance-noexcept-move-constructor)
map &map::operator=( map && ) = default;
static submap null_submap;
void map::set_transparency_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).transparency_cache_dirty.set();
}
}
void map::set_transparency_cache_dirty( const tripoint_bub_ms &p, bool field )
{
if( inbounds( p ) ) {
const tripoint_bub_sm smp = coords::project_to<coords::sm>( p );
get_cache( smp.z() ).transparency_cache_dirty.set( smp.x() * MAPSIZE + smp.y() );
if( !field ) {
get_creature_tracker().invalidate_reachability_cache();
}
}
}
void map::set_seen_cache_dirty( const tripoint_bub_ms &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_seen_cache_dirty( const int zlevel )
{
if( inbounds_z( zlevel ) ) {
level_cache &cache = get_cache( zlevel );
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_floor_cache_dirty( const int zlev )
{
if( inbounds_z( zlev ) ) {
get_cache( zlev ).floor_cache_dirty = true;
}
}
bool map::memory_cache_dec_is_dirty( const tripoint_bub_ms &p ) const
{
if( !inbounds( p ) ) {
debugmsg( "memory_cache_dec_is_dirty called on out of bounds position" );
return true;
}
return !get_cache( p.z() ).map_memory_cache_dec[p.x() + p.y() * MAPSIZE_Y];
}
void map::memory_cache_dec_set_dirty( const tripoint_bub_ms &p, bool value ) const
{
if( !inbounds( p ) ) {
debugmsg( "memory_cache_dec_set_dirty called on out of bounds position" );
return;
}
get_cache( p.z() ).map_memory_cache_dec[p.x() + p.y() * MAPSIZE_Y] = !value;
}
bool map::memory_cache_ter_is_dirty( const tripoint_bub_ms &p ) const
{
if( !inbounds( p ) ) {
debugmsg( "memory_cache_ter_is_dirty called on out of bounds position" );
return true;
}
return !get_cache( p.z() ).map_memory_cache_ter[p.x() + p.y() * MAPSIZE_Y];
}
void map::memory_cache_ter_set_dirty( const tripoint_bub_ms &p, bool value ) const
{
if( !inbounds( p ) ) {
debugmsg( "memory_cache_ter_set_dirty called on out of bounds position" );
return;
}
get_cache( p.z() ).map_memory_cache_ter[p.x() + p.y() * MAPSIZE_Y] = !value;
}
void map::memory_clear_vehicle_points( const vehicle &veh ) const
{
avatar &player_character = get_avatar();
for( const tripoint_abs_ms &p : veh.get_points() ) {
if( !inbounds( p ) ) {
continue;
}
memory_cache_dec_set_dirty( get_bub( p ), true );
player_character.memorize_clear_decoration( p, "vp_" );
}
}
void map::invalidate_map_cache( const int zlev )
{
if( inbounds_z( zlev ) ) {
level_cache &ch = get_cache( zlev );
ch.floor_cache_dirty = true;
ch.seen_cache_dirty = true;
ch.outside_cache_dirty = true;
set_transparency_cache_dirty( zlev );
}
}
const_maptile map::maptile_at( const tripoint_bub_ms &p ) const
{
if( !inbounds( p ) ) {
return const_maptile( &null_submap, point_sm_ms::zero );
}
return maptile_at_internal( p );
}
maptile map::maptile_at( const tripoint_bub_ms &p )
{
if( !inbounds( p ) ) {
return maptile( &null_submap, point_sm_ms::zero );
}
return maptile_at_internal( p );
}
const_maptile map::maptile_at_internal( const tripoint_bub_ms &p ) const
{
point_sm_ms l;
const submap *const sm = get_submap_at( p, l );
return const_maptile( sm, l );
}
maptile map::maptile_at_internal( const tripoint_bub_ms &p )
{
point_sm_ms 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_bub_ms( 0, 0, abs_sub.z() ),
tripoint_bub_ms( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, abs_sub.z() ) );
}
return get_vehicles( tripoint_bub_ms( 0, 0, -OVERMAP_DEPTH ),
tripoint_bub_ms( SEEX * my_MAPSIZE, SEEY * my_MAPSIZE, OVERMAP_HEIGHT ) );
}
void map::rebuild_vehicle_level_caches()
{
clear_vehicle_level_caches();
for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) {
// Cache all vehicles
level_cache *ch = get_cache_lazy( gridz );
if( ch ) {
for( vehicle * const &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;
}
// Get parts
for( const vpart_reference &vpr : veh->get_all_parts_with_fakes() ) {
if( vpr.part().removed ) {
continue;
}
const tripoint_bub_ms p = veh->bub_part_pos( *this, vpr.part() );
level_cache &ch = get_cache( p.z() );
ch.set_veh_cached_parts( p, *veh, static_cast<int>( vpr.part_index() ) );
if( inbounds( p ) ) {
ch.set_veh_exists_at( p, true );
set_transparency_cache_dirty( p );
}
}
}
void map::clear_vehicle_point_from_cache( vehicle *veh, const tripoint_bub_ms &pt )
{
if( veh == nullptr ) {
debugmsg( "Tried to add null vehicle to cache" );
return;
}
level_cache *ch = get_cache_lazy( pt.z() );
if( ch ) {
if( inbounds( pt ) ) {
ch->set_veh_exists_at( pt, false );
}
ch->clear_veh_from_veh_cached_parts( pt, veh );
}
}
void map::clear_vehicle_level_caches( )
{
for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) {
level_cache *ch = get_cache_lazy( gridz );
if( ch ) {
ch->clear_vehicle_cache();
}
}
}
void map::remove_vehicle_from_cache( vehicle *veh, int zmin, int zmax )
{
for( int gridz = zmin; gridz <= zmax; gridz++ ) {
level_cache *const ch = get_cache_lazy( gridz );
if( ch != nullptr ) {
ch->vehicle_list.erase( veh );
ch->zone_vehicles.erase( veh );
}
dirty_vehicle_list.erase( veh );
}
}
namespace
{
void _add_vehicle_to_list( level_cache &ch, vehicle *veh )
{
ch.vehicle_list.insert( veh );
if( !veh->loot_zones.empty() ) {
ch.zone_vehicles.insert( veh );
}
}
} // namespace
void map::reset_vehicles_sm_pos()
{
int const zmin = zlevels ? -OVERMAP_DEPTH : abs_sub.z();
int const zmax = zlevels ? OVERMAP_HEIGHT : abs_sub.z();
for( int z = zmin; z <= zmax; z++ ) {
level_cache &ch = get_cache( z );
for( int x = 0; x < getmapsize(); x++ ) {
for( int y = 0; y < getmapsize(); y++ ) {
tripoint_rel_sm const grid( x, y, z );
submap *const sm = get_submap_at_grid( grid );
if( sm != nullptr ) {
for( auto const &elem : sm->vehicles ) {
// This should be redundant.
elem->sm_pos = abs_sub.xy() + grid;
add_vehicle_to_cache( &*elem );
_add_vehicle_to_list( ch, &*elem );
}
}
}
}
}
}
void map::clear_vehicle_list( const int zlev )
{
auto *ch = get_cache_lazy( zlev );
if( ch ) {
ch->vehicle_list.clear();
ch->zone_vehicles.clear();
}
}
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 ) {
_add_vehicle_to_list( ch, 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:%s",
veh->name, veh->sm_pos.to_string() );
// Try to fix by moving the vehicle here
z = veh->sm_pos.z() = abs_sub.z();
}
// Unboard all passengers before detaching
for( const vpart_reference &part : veh->get_avail_parts( VPFLAG_BOARDABLE ) ) {
Character *passenger = part.get_passenger();
if( passenger ) {
unboard_vehicle( part, passenger );
}
}
veh->invalidate_towing( *this, true );
submap *const current_submap = get_submap_at_grid( veh->sm_pos - abs_sub.xy() );
if( current_submap == nullptr ) {
debugmsg( "Tried to detach vehicle at %s but the submap is not loaded",
veh->sm_pos.to_string() );
return std::unique_ptr<vehicle>();
}
level_cache &ch = get_cache( z );
for( size_t i = 0; i < current_submap->vehicles.size(); i++ ) {
if( current_submap->vehicles[i].get() == veh ) {
for( const tripoint_abs_ms &pt : veh->get_points() ) {
if( inbounds( pt ) ) {
memory_cache_dec_set_dirty( get_bub( pt ), true );
}
get_avatar().memorize_clear_decoration( pt, "vp_" );
}
ch.vehicle_list.erase( veh );
ch.zone_vehicles.erase( veh );
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 );
rebuild_vehicle_level_caches();
set_pathfinding_cache_dirty( z );
return result;
}
}
debugmsg( "detach_vehicle can't find it! name=%s, submap:%s", veh->name,
veh->sm_pos.to_string() );
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();
const tripoint_abs_ms player_pos = get_player_character().pos_abs();
for( int zlev = minz; zlev <= maxz; ++zlev ) {
level_cache *cache_lazy = get_cache_lazy( zlev );
if( !cache_lazy ) {
continue;
}
level_cache &cache = *cache_lazy;
for( vehicle *veh : cache.vehicle_list ) {
if( veh->is_following ) {
veh->drive_to_local_target( this, player_pos, true );
} else if( veh->is_patrolling ) {
veh->autopilot_patrol( this );
}
veh->gain_moves( *this );
veh->slow_leak( *this );
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( vehicle * const &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( *this );
}
}
dirty_vehicle_list.clear();
std::map<vehicle *, bool> vehs; // value true means in on map
std::unordered_set<vehicle *> connected_vehs;
for( int zlev = minz; zlev <= maxz; ++zlev ) {
const level_cache *cache = get_cache_lazy( zlev );
if( !cache ) {
continue;
}
for( vehicle *veh : cache->vehicle_list ) {
vehs[veh] = true; // force on map vehicles to true
veh->get_connected_vehicles( *this, connected_vehs );
}
}
for( vehicle *connected_veh : connected_vehs ) {
vehs.emplace( connected_veh, false ); // add with 'false' if does not exist (off map)
}
for( const std::pair<vehicle *const, bool> &veh_pair : vehs ) {
veh_pair.first->idle( *this, /* on_map = */ veh_pair.second );
}
// refresh vehicle zones for moved vehicles
zone_manager::get_manager().cache_vzones( this );
}
bool map::vehproceed( VehicleList &vehicle_list )
{
wrapped_vehicle *cur_veh = nullptr;
float max_of_turn = 0.0f;
// 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( *this ) &&
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( *this );
if( cur_veh->v == nullptr ) {
vehicle_list = get_vehicles();
}
return true;
}
// TODO: Make reality bubble independent.
static bool sees_veh( const Creature &c, vehicle &veh, bool force_recalc )
{
const std::set<tripoint_abs_ms> &veh_points = veh.get_points( force_recalc );
return std::any_of( veh_points.begin(), veh_points.end(), [&c]( const tripoint_abs_ms & pt ) {
return c.sees( get_map().get_bub( pt ) );
} );
}
vehicle *map::move_vehicle( vehicle &veh, const tripoint_rel_ms &dp, const tileray &facing )
{
if( dp == tripoint_rel_ms::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_rel_ms::zero && dp.z() != 0 ) {
vehicle *const new_pointer = move_vehicle( veh, tripoint_rel_ms( dp.xy(), 0 ), facing );
if( !new_pointer ) {
return nullptr;
}
vehicle *const result = move_vehicle( *new_pointer, tripoint_rel_ms( 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
cata_assert( vertical == ( dp.xy() == point_rel_ms::zero ) );
const int target_z = dp.z() + veh.sm_pos.z();
// limit vehicles to at most OVERMAP_HEIGHT - 1; this mitigates getting to zlevel 10 easily
// and causing `get_cache_ref( zlev + 1 );` call in map::build_sunlight_cache overflowing
if( target_z < -OVERMAP_DEPTH || target_z > OVERMAP_HEIGHT - 1 ) {
return &veh;
}
veh.precalc_mounts( 1, veh.skidding ? veh.turn_dir : facing.dir(), veh.pivot_point( *this ) );
// cancel out any movement of the vehicle due only to a change in pivot
tripoint_rel_ms 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 && !veh.is_rotorcraft( *this ) && !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( *this, collisions, dp1, false );
// Vehicle collisions
std::map<vehicle *, std::vector<veh_collision> > veh_collisions;
for( veh_collision &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 veh_collision &coll : collisions ) {
if( coll.type == veh_coll_veh ) {
continue;
}
int part_num = veh.get_non_fake_part( coll.part );
const point_rel_ms &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.
// don't try to deal damage to invalid part (probably removed or destroyed)
if( part_num != -1 ) {
if( veh.part( part_num ).info().has_flag( VPFLAG_ROTOR ) ) {
veh.damage( *this, part_num, coll_dmg, damage_bash, true );
} else {
impulse += coll_dmg;
veh.damage( *this, part_num, coll_dmg, damage_bash );
veh.damage_all( *this, coll_dmg / 2, coll_dmg, damage_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( *this ) ) {
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.pos_bub( *this ), 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( *this ) && !( veh.is_watercraft() &&
veh.can_float( *this ) ) &&
!veh.is_flying_in_air() && dp.z() == 0 ) {
veh.velocity -= std::clamp( veh.velocity, -2000, 2000 ); // extra drag
for( const tripoint_abs_ms &p : veh.get_points() ) {
const tripoint_bub_ms pos = get_bub( p );
const ter_id &pter = ter( pos );
if( pter == ter_t_dirt || pter == ter_t_grass ) {
ter_set( pos, ter_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( *this );
// Actually change position
displace_vehicle( veh, dp1 );
level_vehicle( veh );
} else if( !vertical ) {
veh.stop( *this );
}
veh.check_falling_or_floating();
// If the PC is in the currently moved vehicle, adjust the
// view offset.
if( player_character.controlling_vehicle &&
veh_pointer_or_null( veh_at( player_character.pos_bub() ) ) == &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.05f; // Nobody likes a magic number.
const float vehicle_mass_kg = to_kilogram( veh.total_mass( *this ) );
for( const int vp_wheel_idx : wheel_indices ) {
vehicle_part &vp_wheel = veh.part( vp_wheel_idx );
const vpart_info &vpi_wheel = vp_wheel.info();
const tripoint_bub_ms wheel_p = veh.bub_part_pos( *this, vp_wheel );
if( one_in( 2 ) && displace_water( wheel_p ) ) {
sounds::sound( wheel_p, 4, sounds::sound_t::movement, _( "splash!" ), false,
"environment", "splash" );
}
veh.handle_trap( this, wheel_p, vp_wheel );
// dont use vp_wheel or vp_wheel_idx below this - handle_trap might've removed it from parts
if( !has_flag( ter_furn_flag::TFLAG_SEALED, wheel_p ) ) {
// 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 = vpi_wheel.wheel_info->contact_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() ) );
}
}
}
if( veh.is_towing() ) {
veh.do_towing_move( *this );
// 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( *this, 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();