forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovermapbuffer.cpp
1777 lines (1574 loc) · 61.6 KB
/
overmapbuffer.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 "overmapbuffer.h"
#include <algorithm>
#include <climits>
#include <iterator>
#include <list>
#include <map>
#include <string>
#include <tuple>
#include "basecamp.h"
#include "calendar.h"
#include "cata_assert.h"
#include "cata_utility.h"
#include "character.h"
#include "character_id.h"
#include "color.h"
#include "common_types.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "filesystem.h"
#include "game.h"
#include "game_constants.h"
#include "line.h"
#include "map.h"
#include "memory_fast.h"
#include "mongroup.h"
#include "monster.h"
#include "npc.h"
#include "optional.h"
#include "overmap.h"
#include "overmap_connection.h"
#include "overmap_types.h"
#include "path_info.h"
#include "point.h"
#include "rng.h"
#include "simple_pathfinding.h"
#include "string_formatter.h"
#include "translations.h"
#include "vehicle.h"
class map_extra;
static const oter_type_str_id oter_type_bridge( "bridge" );
static const oter_type_str_id oter_type_bridge_road( "bridge_road" );
static const oter_type_str_id oter_type_bridgehead_ground( "bridgehead_ground" );
static const oter_type_str_id oter_type_bridgehead_ramp( "bridgehead_ramp" );
static const oter_type_str_id oter_type_deep_rock( "deep_rock" );
static const oter_type_str_id oter_type_empty_rock( "empty_rock" );
static const oter_type_str_id oter_type_field( "field" );
static const oter_type_str_id oter_type_forest( "forest" );
static const oter_type_str_id oter_type_forest_trail( "forest_trail" );
static const oter_type_str_id oter_type_forest_water( "forest_water" );
static const oter_type_str_id oter_type_lab_subway( "lab_subway" );
static const oter_type_str_id oter_type_lake_surface( "lake_surface" );
static const oter_type_str_id oter_type_microlab_rock_border( "microlab_rock_border" );
static const oter_type_str_id oter_type_open_air( "open_air" );
static const oter_type_str_id oter_type_river_center( "river_center" );
static const oter_type_str_id oter_type_road( "road" );
static const oter_type_str_id oter_type_road_nesw_manhole( "road_nesw_manhole" );
static const oter_type_str_id oter_type_solid_earth( "solid_earth" );
static const oter_type_str_id oter_type_subway( "subway" );
overmapbuffer overmap_buffer;
overmapbuffer::overmapbuffer()
: last_requested_overmap( nullptr )
{
}
const city_reference city_reference::invalid{ nullptr, tripoint_abs_sm(), -1 };
int city_reference::get_distance_from_bounds() const
{
cata_assert( city != nullptr );
return distance - omt_to_sm_copy( city->size );
}
int camp_reference::get_distance_from_bounds() const
{
cata_assert( camp != nullptr );
return distance - omt_to_sm_copy( 4 );
}
std::string overmapbuffer::terrain_filename( const point_abs_om &p )
{
return string_format( "%s/o.%d.%d", PATH_INFO::world_base_save_path(), p.x(), p.y() );
}
std::string overmapbuffer::player_filename( const point_abs_om &p )
{
return string_format( "%s.seen.%d.%d", PATH_INFO::player_base_save_path(), p.x(), p.y() );
}
overmap &overmapbuffer::get( const point_abs_om &p )
{
if( last_requested_overmap != nullptr && last_requested_overmap->pos() == p ) {
return *last_requested_overmap;
}
const auto it = overmaps.find( p );
if( it != overmaps.end() ) {
return *( last_requested_overmap = it->second.get() );
}
// That constructor loads an existing overmap or creates a new one.
overmap &new_om = *( overmaps[ p ] = std::make_unique<overmap>( p ) );
new_om.populate();
// Note: fix_mongroups might load other overmaps, so overmaps.back() is not
// necessarily the overmap at (x,y)
fix_mongroups( new_om );
fix_npcs( new_om );
last_requested_overmap = &new_om;
return new_om;
}
void overmapbuffer::create_custom_overmap( const point_abs_om &p, overmap_special_batch &specials )
{
if( last_requested_overmap != nullptr ) {
auto om_iter = overmaps.find( p );
if( om_iter != overmaps.end() && om_iter->second.get() == last_requested_overmap ) {
last_requested_overmap = nullptr;
}
}
overmap &new_om = *( overmaps[ p ] = std::make_unique<overmap>( p ) );
new_om.populate( specials );
}
void overmapbuffer::fix_mongroups( overmap &new_overmap )
{
for( auto it = new_overmap.zg.begin(); it != new_overmap.zg.end(); ) {
mongroup &mg = it->second;
// spawn related code simply sets population to 0 when they have been
// transformed into spawn points on a submap, the group can then be removed
if( mg.empty() ) {
new_overmap.zg.erase( it++ );
continue;
}
// Inside the bounds of the overmap?
if( mg.pos.x() >= 0 && mg.pos.y() >= 0 && mg.pos.x() < OMAPX * 2 &&
mg.pos.y() < OMAPY * 2 ) {
++it;
continue;
}
point_abs_sm smabs = project_combine( new_overmap.pos(), mg.pos.xy() );
point_abs_om omp;
point_om_sm sm_rem;
std::tie( omp, sm_rem ) = project_remain<coords::om>( smabs );
if( !has( omp ) ) {
// Don't generate new overmaps, as this can be called from the
// overmap-generating code.
++it;
continue;
}
overmap &om = get( omp );
mg.pos = tripoint_om_sm( sm_rem, mg.pos.z() );
om.spawn_mon_group( mg );
new_overmap.zg.erase( it++ );
}
}
void overmapbuffer::fix_nemesis( overmap &new_overmap )
{
for( std::multimap<tripoint_om_sm, mongroup>::iterator it = new_overmap.zg.begin();
it != new_overmap.zg.end(); ) {
mongroup &mg = it->second;
//if it's not the nemesis, continue
if( mg.horde_behaviour != "nemesis" ) {
++it;
continue;
}
//if the nemesis's abs coordinates put it in this overmap, it belongs here
if( project_to<coords::om>( mg.abs_pos.xy() ) == new_overmap.pos() ) {
++it;
continue;
}
//otherwise, place it in the overmap that corresponds to its abs_sm coords
point_abs_om omp;
point_om_sm sm_rem;
std::tie( omp, sm_rem ) = project_remain<coords::om>( mg.abs_pos.xy() );
overmap &om = get( omp );
mg.pos = tripoint_om_sm( sm_rem, mg.pos.z() );
om.spawn_mon_group( mg );
new_overmap.zg.erase( it++ );
//there should only be one nemesis, so we can break after finding it
break;
}
}
void overmapbuffer::fix_npcs( overmap &new_overmap )
{
// First step: move all npcs that are located outside of the given overmap
// into a separate container. After that loop, new_overmap.npcs is no
// accessed anymore!
decltype( overmap::npcs ) to_relocate;
for( auto it = new_overmap.npcs.begin(); it != new_overmap.npcs.end(); ) {
npc &np = **it;
const tripoint_abs_omt npc_omt_pos = np.global_omt_location();
const point_abs_om npc_om_pos = project_to<coords::om>( npc_omt_pos.xy() );
if( npc_om_pos == new_overmap.pos() ) {
// NPC is still in the same overmap (common case), nothing to do
++it;
continue;
}
to_relocate.push_back( *it );
it = new_overmap.npcs.erase( it );
}
// Second step: put them back where they belong. This step involves loading
// new overmaps (via `get`), which does in turn call this function for the
// newly loaded overmaps. This in turn may move NPCs from the second overmap
// back into the first overmap. This messes up the iteration of it. The
// iteration is therefore done in a separate step above (which does *not*
// involve loading new overmaps).
for( auto &ptr : to_relocate ) {
npc &np = *ptr;
const tripoint_abs_omt npc_omt_pos = np.global_omt_location();
const point_abs_om npc_om_pos = project_to<coords::om>( npc_omt_pos.xy() );
const point_abs_om loc = new_overmap.pos();
if( !has( npc_om_pos ) ) {
// This can't really happen without save editing
// Just move the NPC back into the bounds of new_overmap, as close
// as possible to where they were supposed to be.
debugmsg( "NPC %s is out of bounds at %s, on non-generated overmap %s",
np.get_name(), npc_omt_pos.to_string(), loc.to_string() );
// bounding box for new_overmap in omt coords
const half_open_rectangle<point_abs_omt> om_bounds( project_to<coords::omt>( loc ),
project_to<coords::omt>( loc + point( 1, 1 ) ) ); // NOLINT(cata-use-named-point-constants)
const tripoint_abs_omt adjusted_omt_pos( clamp( npc_omt_pos.xy(), om_bounds ), npc_omt_pos.z() );
np.spawn_at_omt( adjusted_omt_pos );
new_overmap.npcs.push_back( ptr );
continue;
}
// Simplest case: just move the pointer
get( npc_om_pos ).insert_npc( ptr );
}
}
void overmapbuffer::save()
{
for( auto &omp : overmaps ) {
// Note: this may throw io errors from std::ofstream
omp.second->save();
}
}
void overmapbuffer::clear()
{
overmaps.clear();
known_non_existing.clear();
last_requested_overmap = nullptr;
}
const regional_settings &overmapbuffer::get_settings( const tripoint_abs_omt &p )
{
overmap *om = get_om_global( p ).om;
return om->get_settings();
}
void overmapbuffer::add_note( const tripoint_abs_omt &p, const std::string &message )
{
overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->add_note( om_loc.local, message );
}
void overmapbuffer::delete_note( const tripoint_abs_omt &p )
{
if( has_note( p ) ) {
overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->delete_note( om_loc.local );
}
}
void overmapbuffer::mark_note_dangerous( const tripoint_abs_omt &p, int radius, bool is_dangerous )
{
if( has_note( p ) ) {
overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->mark_note_dangerous( om_loc.local, radius, is_dangerous );
}
}
void overmapbuffer::add_extra( const tripoint_abs_omt &p, const map_extra_id &id )
{
overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->add_extra( om_loc.local, id );
}
void overmapbuffer::delete_extra( const tripoint_abs_omt &p )
{
if( has_extra( p ) ) {
overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->delete_extra( om_loc.local );
}
}
overmap *overmapbuffer::get_existing( const point_abs_om &p )
{
if( last_requested_overmap && last_requested_overmap->pos() == p ) {
return last_requested_overmap;
}
const auto it = overmaps.find( p );
if( it != overmaps.end() ) {
return last_requested_overmap = it->second.get();
}
if( known_non_existing.count( p ) > 0 ) {
// This overmap does not exist on disk (this has already been
// checked in a previous call of this function).
return nullptr;
}
if( file_exist( terrain_filename( p ) ) ) {
// File exists, load it normally (the get function
// indirectly call overmap::open to do so).
return &get( p );
}
// File does not exist (or not readable which is essentially
// the same for our usage). A second call of this function with
// the same coordinates will not check the file system, and
// return early.
// If the overmap had been created in the mean time, the previous
// loop would have found and returned it.
known_non_existing.insert( p );
return nullptr;
}
bool overmapbuffer::has( const point_abs_om &p )
{
return get_existing( p ) != nullptr;
}
overmap_with_local_coords
overmapbuffer::get_om_global( const point_abs_omt &p )
{
return get_om_global( tripoint_abs_omt( p, 0 ) );
}
overmap_with_local_coords
overmapbuffer::get_om_global( const tripoint_abs_omt &p )
{
point_abs_om om_pos;
point_om_omt local;
std::tie( om_pos, local ) = project_remain<coords::om>( p.xy() );
overmap *om = &get( om_pos );
return { om, tripoint_om_omt( local, p.z() ) };
}
overmap_with_local_coords
overmapbuffer::get_existing_om_global( const point_abs_omt &p )
{
return get_existing_om_global( tripoint_abs_omt( p, 0 ) );
}
overmap_with_local_coords
overmapbuffer::get_existing_om_global( const tripoint_abs_omt &p )
{
point_abs_om om_pos;
point_om_omt local;
std::tie( om_pos, local ) = project_remain<coords::om>( p.xy() );
overmap *om = get_existing( om_pos );
if( om == nullptr ) {
return overmap_with_local_coords{ nullptr, tripoint_om_omt() };
}
return overmap_with_local_coords{ om, tripoint_om_omt( local, p.z() ) };
}
bool overmapbuffer::is_omt_generated( const tripoint_abs_omt &loc )
{
if( overmap_with_local_coords om_loc = get_existing_om_global( loc ) ) {
return om_loc.om->is_omt_generated( om_loc.local );
}
// If the overmap doesn't exist, then for sure the local mapgen
// hasn't happened.
return false;
}
bool overmapbuffer::has_note( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->has_note( om_loc.local );
}
return false;
}
bool overmapbuffer::is_marked_dangerous( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->is_marked_dangerous( om_loc.local );
}
return false;
}
const std::string &overmapbuffer::note( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->note( om_loc.local );
}
static const std::string empty_string;
return empty_string;
}
bool overmapbuffer::has_extra( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->has_extra( om_loc.local );
}
return false;
}
const map_extra_id &overmapbuffer::extra( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->extra( om_loc.local );
}
static const map_extra_id id;
return id;
}
bool overmapbuffer::is_explored( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->is_explored( om_loc.local );
}
return false;
}
void overmapbuffer::toggle_explored( const tripoint_abs_omt &p )
{
const overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->explored( om_loc.local ) = !om_loc.om->explored( om_loc.local );
}
bool overmapbuffer::has_horde( const tripoint_abs_omt &p )
{
for( mongroup * const &m : overmap_buffer.monsters_at( p ) ) {
if( m->horde ) {
return true;
}
}
return false;
}
int overmapbuffer::get_horde_size( const tripoint_abs_omt &p )
{
int horde_size = 0;
for( mongroup * const &m : overmap_buffer.monsters_at( p ) ) {
if( m->horde ) {
if( !m->monsters.empty() ) {
horde_size += m->monsters.size();
} else {
// We don't know how large this will actually be, because
// population "1" can still result in a zombie pack.
// So we double the population as an estimate to make
// hordes more likely to be visible on the overmap.
horde_size += m->population * 2;
}
}
}
return horde_size;
}
bool overmapbuffer::has_camp( const tripoint_abs_omt &p )
{
if( p.z() ) {
return false;
}
const overmap_with_local_coords om_loc = get_existing_om_global( p );
if( !om_loc ) {
return false;
}
for( const basecamp &v : om_loc.om->camps ) {
if( v.camp_omt_pos().xy() == p.xy() ) {
return true;
}
}
return false;
}
bool overmapbuffer::has_vehicle( const tripoint_abs_omt &p )
{
const overmap_with_local_coords om_loc = get_existing_om_global( p );
if( !om_loc ) {
return false;
}
for( const auto &v : om_loc.om->vehicles ) {
if( v.second.p.xy() == om_loc.local.xy() ) {
return true;
}
}
return false;
}
std::vector<om_vehicle> overmapbuffer::get_vehicle( const tripoint_abs_omt &p )
{
std::vector<om_vehicle> result;
const overmap_with_local_coords om_loc = get_existing_om_global( p );
if( !om_loc ) {
return result;
}
for( const auto &ov : om_loc.om->vehicles ) {
if( ov.second.p.xy() == om_loc.local.xy() ) {
result.push_back( ov.second );
}
}
return result;
}
std::string overmapbuffer::get_vehicle_ter_sym( const tripoint_abs_omt &omt )
{
std::string ter_sym;
std::vector<om_vehicle> vehicles = overmap_buffer.get_vehicle( omt );
int distance = std::max( OVERMAP_DEPTH, OVERMAP_HEIGHT ) + 1;
for( om_vehicle vehicle : vehicles ) {
int temp_distance = std::abs( vehicle.p.z() - omt.z() );
if( temp_distance < distance ) {
distance = temp_distance;
if( vehicle.p.z() == omt.z() ) {
return "c"; // Break to always show vehicles on current level first
} else if( vehicle.p.z() > omt.z() ) {
ter_sym = "^";
} else {
ter_sym = "v";
}
}
}
return ter_sym;
}
std::string overmapbuffer::get_vehicle_tile_id( const tripoint_abs_omt &omt )
{
std::string tile_id;
std::vector<om_vehicle> vehicles = overmap_buffer.get_vehicle( omt );
int distance = std::max( OVERMAP_DEPTH, OVERMAP_HEIGHT ) + 1;
for( om_vehicle vehicle : vehicles ) {
int temp_distance = std::abs( vehicle.p.z() - omt.z() );
if( temp_distance < distance ) {
distance = temp_distance;
if( vehicle.p.z() == omt.z() ) {
return "overmap_remembered_vehicle"; // Break to always show vehicles on current level first
} else if( vehicle.p.z() > omt.z() ) {
tile_id = "overmap_remembered_vehicle_above";
} else {
tile_id = "overmap_remembered_vehicle_below";
}
}
}
return tile_id;
}
void overmapbuffer::signal_hordes( const tripoint_abs_sm ¢er, const int sig_power )
{
const int radius = sig_power;
for( overmap *&om : get_overmaps_near( center, radius ) ) {
const point_abs_sm abs_pos_om = project_to<coords::sm>( om->pos() );
const tripoint_rel_sm rel_pos = center - abs_pos_om;
// overmap::signal_hordes expects a coordinate relative to the overmap, this is easier
// for processing as the monster group stores is location as relative coordinates, too.
om->signal_hordes( rel_pos, sig_power );
}
}
void overmapbuffer::signal_nemesis( const tripoint_abs_sm &p )
{
for( std::pair<const point_abs_om, std::unique_ptr<overmap>> &omp : overmaps ) {
omp.second->signal_nemesis( p );
}
}
void overmapbuffer::add_nemesis( const tripoint_abs_omt &p )
{
//takes location from kill_nemesis mission and adds a nemesis monstergroup into the overmap
const tripoint_abs_om loc = project_to<coords::om>( p );
overmap *om = get_existing( loc.xy() );
om->place_nemesis( p );
}
void overmapbuffer::process_mongroups()
{
// arbitrary radius to include nearby overmaps (aside from the current one)
const int radius = MAPSIZE * 2;
const tripoint_abs_sm center = get_player_character().global_sm_location();
for( overmap *&om : get_overmaps_near( center, radius ) ) {
om->process_mongroups();
}
}
void overmapbuffer::move_hordes()
{
// arbitrary radius to include nearby overmaps (aside from the current one)
const int radius = MAPSIZE * 2;
const tripoint_abs_sm center = get_player_character().global_sm_location();
for( overmap *&om : get_overmaps_near( center, radius ) ) {
om->move_hordes();
}
}
void overmapbuffer::move_nemesis()
{
for( std::pair<const point_abs_om, std::unique_ptr<overmap>> &omp : overmaps ) {
omp.second->move_nemesis();
fix_nemesis( *omp.second );
}
}
void overmapbuffer::remove_nemesis()
{
for( std::pair<const point_abs_om, std::unique_ptr<overmap>> &omp : overmaps ) {
bool nemesis_removed = omp.second->remove_nemesis();
if( nemesis_removed ) {
break;
}
}
}
std::vector<mongroup *> overmapbuffer::monsters_at( const tripoint_abs_omt &p )
{
// (x,y) are overmap terrain coordinates, they spawn 2x2 submaps,
// but monster groups are defined with submap coordinates.
tripoint_abs_sm p_sm = project_to<coords::sm>( p );
std::vector<mongroup *> result;
for( const point &offset : std::array<point, 4> { { { point_zero }, { point_south }, { point_east }, { point_south_east } } } ) {
std::vector<mongroup *> tmp = groups_at( p_sm + offset );
result.insert( result.end(), tmp.begin(), tmp.end() );
}
return result;
}
std::vector<mongroup *> overmapbuffer::groups_at( const tripoint_abs_sm &p )
{
std::vector<mongroup *> result;
point_om_sm sm_within_om;
point_abs_om omp;
std::tie( omp, sm_within_om ) = project_remain<coords::om>( p.xy() );
if( !has( omp ) ) {
return result;
}
overmap &om = get( omp );
auto groups_range = om.zg.equal_range( tripoint_om_sm( sm_within_om, p.z() ) );
for( auto it = groups_range.first; it != groups_range.second; ++it ) {
mongroup &mg = it->second;
if( mg.empty() ) {
continue;
}
result.push_back( &mg );
}
return result;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
std::array<std::array<scent_trace, 3>, 3> overmapbuffer::scents_near( const tripoint_abs_omt
&origin )
{
std::array<std::array<scent_trace, 3>, 3> found_traces;
for( int x = -1; x <= 1 ; ++x ) {
for( int y = -1; y <= 1; ++y ) {
tripoint_abs_omt iter = origin + point( x, y );
found_traces[x + 1][y + 1] = scent_at( iter );
}
}
return found_traces;
}
#pragma GCC diagnostic pop
scent_trace overmapbuffer::scent_at( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->scent_at( p );
}
return scent_trace();
}
void overmapbuffer::set_scent( const tripoint_abs_omt &loc, int strength )
{
const overmap_with_local_coords om_loc = get_om_global( loc );
scent_trace new_scent( calendar::turn, strength );
om_loc.om->set_scent( loc, new_scent );
}
void overmapbuffer::move_vehicle( vehicle *veh, const point_abs_ms &old_msp )
{
const point_abs_ms new_msp = veh->global_square_location().xy();
const point_abs_omt old_omt = project_to<coords::omt>( old_msp );
const point_abs_omt new_omt = project_to<coords::omt>( new_msp );
const overmap_with_local_coords old_om_loc = get_om_global( old_omt );
const overmap_with_local_coords new_om_loc = get_om_global( new_omt );
if( old_om_loc.om == new_om_loc.om ) {
new_om_loc.om->vehicles[veh->om_id].p = new_om_loc.local;
} else {
old_om_loc.om->vehicles.erase( veh->om_id );
add_vehicle( veh );
}
}
void overmapbuffer::remove_camp( const basecamp &camp )
{
const point_abs_omt omt = camp.camp_omt_pos().xy();
const overmap_with_local_coords om_loc = get_om_global( omt );
std::vector<basecamp> &camps = om_loc.om->camps;
for( auto it = camps.begin(); it != camps.end(); ++it ) {
if( it->camp_omt_pos().xy() == omt ) {
camps.erase( it );
return;
}
}
}
void overmapbuffer::remove_vehicle( const vehicle *veh )
{
const tripoint_abs_omt omt = veh->global_omt_location();
const overmap_with_local_coords om_loc = get_existing_om_global( omt );
if( !om_loc.om ) {
debugmsg( "Can't find overmap for vehicle at %s", omt.to_string_writable() );
return;
}
om_loc.om->vehicles.erase( veh->om_id );
}
void overmapbuffer::add_vehicle( vehicle *veh )
{
const tripoint_abs_omt omt = veh->global_omt_location();
const overmap_with_local_coords om_loc = get_existing_om_global( omt );
if( !om_loc.om ) {
debugmsg( "Can't find overmap for vehicle at %s", omt.to_string_writable() );
return;
}
int id = om_loc.om->vehicles.size() + 1;
// this *should* be unique but just in case
while( om_loc.om->vehicles.count( id ) > 0 ) {
id++;
}
om_vehicle &tracked_veh = om_loc.om->vehicles[id];
tracked_veh.p = om_loc.local;
tracked_veh.name = veh->name;
veh->om_id = id;
}
void overmapbuffer::add_camp( const basecamp &camp )
{
const point_abs_omt omt = camp.camp_omt_pos().xy();
const overmap_with_local_coords om_loc = get_om_global( omt );
om_loc.om->camps.push_back( camp );
}
bool overmapbuffer::seen( const tripoint_abs_omt &p )
{
if( const overmap_with_local_coords om_loc = get_existing_om_global( p ) ) {
return om_loc.om->seen( om_loc.local );
}
return false;
}
void overmapbuffer::set_seen( const tripoint_abs_omt &p, bool seen )
{
const overmap_with_local_coords om_loc = get_om_global( p );
om_loc.om->set_seen( om_loc.local, seen );
}
const oter_id &overmapbuffer::ter( const tripoint_abs_omt &p )
{
const overmap_with_local_coords om_loc = get_om_global( p );
return om_loc.om->ter( om_loc.local );
}
const oter_id &overmapbuffer::ter_existing( const tripoint_abs_omt &p )
{
static const oter_id ot_null;
const overmap_with_local_coords om_loc = get_existing_om_global( p );
if( !om_loc.om ) {
return ot_null;
}
return om_loc.om->ter( om_loc.local );
}
void overmapbuffer::ter_set( const tripoint_abs_omt &p, const oter_id &id )
{
const overmap_with_local_coords om_loc = get_om_global( p );
return om_loc.om->ter_set( om_loc.local, id );
}
cata::optional<mapgen_arguments> *overmapbuffer::mapgen_args( const tripoint_abs_omt &p )
{
const overmap_with_local_coords om_loc = get_om_global( p );
return om_loc.om->mapgen_args( om_loc.local );
}
std::string *overmapbuffer::join_used_at( const std::pair<tripoint_abs_omt, cube_direction> &p )
{
const overmap_with_local_coords om_loc = get_om_global( p.first );
return om_loc.om->join_used_at( { om_loc.local, p.second } );
}
std::vector<oter_id> overmapbuffer::predecessors( const tripoint_abs_omt &p )
{
const overmap_with_local_coords om_loc = get_om_global( p );
return om_loc.om->predecessors( om_loc.local );
}
bool overmapbuffer::reveal( const point_abs_omt ¢er, int radius, int z )
{
return reveal( tripoint_abs_omt( center, z ), radius );
}
bool overmapbuffer::reveal( const tripoint_abs_omt ¢er, int radius )
{
return reveal( center, radius, []( const oter_id & ) {
return true;
} );
}
bool overmapbuffer::reveal( const tripoint_abs_omt ¢er, int radius,
const std::function<bool( const oter_id & )> &filter )
{
int radius_squared = radius * radius;
bool result = false;
for( int i = -radius; i <= radius; i++ ) {
for( int j = -radius; j <= radius; j++ ) {
const tripoint_abs_omt p = center + point( i, j );
if( seen( p ) ) {
continue;
}
if( trigdist && i * i + j * j > radius_squared ) {
continue;
}
if( !filter( ter( p ) ) ) {
continue;
}
result = true;
set_seen( p, true );
}
}
return result;
}
overmap_path_params overmap_path_params::for_player()
{
overmap_path_params ret;
ret.road_cost = 10;
ret.dirt_road_cost = 10;
ret.field_cost = 15;
ret.trail_cost = 18;
ret.shore_cost = 20;
ret.small_building_cost = 20;
ret.forest_cost = 30;
ret.swamp_cost = 100;
ret.other_cost = 30;
return ret;
}
overmap_path_params overmap_path_params::for_npc()
{
overmap_path_params ret = overmap_path_params::for_player();
ret.only_known_by_player = false;
ret.avoid_danger = false;
return ret;
}
overmap_path_params overmap_path_params::for_land_vehicle( float offroad_coeff, bool tiny,
bool amphibious )
{
const bool can_offroad = offroad_coeff >= 0.05;
overmap_path_params ret;
ret.road_cost = 10;
ret.field_cost = can_offroad ? std::lround( 15 / std::min( 1.0f, offroad_coeff ) ) : -1;
ret.dirt_road_cost = ret.field_cost;
ret.forest_cost = -1;
ret.small_building_cost = ( can_offroad && tiny ) ? ret.field_cost + 30 : -1;
ret.swamp_cost = -1;
ret.trail_cost = ( can_offroad && tiny ) ? ret.field_cost + 10 : -1;
if( amphibious ) {
const overmap_path_params boat_params = overmap_path_params::for_watercraft();
ret.water_cost = boat_params.water_cost;
ret.shore_cost = boat_params.shore_cost;
}
return ret;
}
overmap_path_params overmap_path_params::for_watercraft()
{
overmap_path_params ret;
ret.water_cost = 10;
ret.shore_cost = 20;
return ret;
}
overmap_path_params overmap_path_params::for_aircraft()
{
overmap_path_params ret;
ret.air_cost = 10;
return ret;
}
static int get_terrain_cost( const tripoint_abs_omt &omt_pos, const overmap_path_params ¶ms )
{
if( params.only_known_by_player && !overmap_buffer.seen( omt_pos ) ) {
return -1;
}
if( params.avoid_danger && overmap_buffer.is_marked_dangerous( omt_pos ) ) {
return -1;
}
const oter_id &oter = overmap_buffer.ter_existing( omt_pos );
if( ( oter->get_type_id() == oter_type_road ) ||
( oter->get_type_id() == oter_type_bridge_road ) ||
( oter->get_type_id() == oter_type_bridgehead_ground ) ||
( oter->get_type_id() == oter_type_bridgehead_ramp ) ||
( oter->get_type_id() == oter_type_road_nesw_manhole ) ) {
return params.road_cost;
} else if( oter->get_type_id() == oter_type_field ) {
return params.field_cost;
} else if( is_ot_match( "rural_road", oter, ot_match_type::prefix ) ||
is_ot_match( "dirt_road", oter, ot_match_type::prefix ) ||
( oter->get_type_id() == oter_type_subway ) ||
( oter->get_type_id() == oter_type_lab_subway ) ) {
return params.dirt_road_cost;
} else if( oter->get_type_id() == oter_type_forest_trail ) {
return params.trail_cost;
} else if( oter->get_type_id() == oter_type_forest_water ) {
return params.swamp_cost;
} else if( is_ot_match( "river", oter, ot_match_type::prefix ) ||
is_ot_match( "lake", oter, ot_match_type::prefix ) ) {
if( ( oter->get_type_id() == oter_type_river_center ) ||
( oter->get_type_id() == oter_type_lake_surface ) ) {
return params.water_cost;
} else {
return params.shore_cost;
}
} else if( oter->get_type_id() == oter_type_bridge ) {
return params.water_cost;
} else if( oter->get_type_id() == oter_type_open_air ) {
return params.air_cost;
} else if( oter->get_type_id() == oter_type_forest ) {
return params.forest_cost;
} else if( ( oter->get_type_id() == oter_type_empty_rock ) ||
( oter->get_type_id() == oter_type_deep_rock ) ||
( oter->get_type_id() == oter_type_solid_earth ) ||
( oter->get_type_id() == oter_type_microlab_rock_border ) ) {
return -1;
} else {
return params.other_cost;
}
}
static bool is_ramp( const tripoint_abs_omt &omt_pos )
{
const oter_id &oter = overmap_buffer.ter_existing( omt_pos );
return ( oter->get_type_id() == oter_type_bridgehead_ground ) ||
( oter->get_type_id() == oter_type_bridgehead_ramp );
}
std::vector<tripoint_abs_omt> overmapbuffer::get_travel_path(
const tripoint_abs_omt &src, const tripoint_abs_omt &dest, overmap_path_params params )
{
if( src == overmap::invalid_tripoint || dest == overmap::invalid_tripoint ) {
return {};
}
const pf::omt_scoring_fn estimate = [&]( tripoint_abs_omt pos ) {
const int cur_cost = pos == src ? 0 : get_terrain_cost( pos, params );
if( cur_cost < 0 ) {
return pf::omt_score::rejected;
}
return pf::omt_score( cur_cost, is_ramp( pos ) );
};
constexpr int radius = 4 * OMAPX; // radius of search in OMTs = 4 overmaps
const pf::simple_path<tripoint_abs_omt> path = pf::find_overmap_path( src, dest, radius, estimate );
return path.points;
}
bool overmapbuffer::reveal_route( const tripoint_abs_omt &source, const tripoint_abs_omt &dest,
int radius, bool road_only )
{
static const int RADIUS = 4; // Maximal radius of search (in overmaps)
static const point_rel_omt O( RADIUS * OMAPX,
RADIUS * OMAPY ); // half-height of the area to search in
if( source == overmap::invalid_tripoint || dest == overmap::invalid_tripoint ) {
return false;