-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathmapgen.cpp
8267 lines (7618 loc) · 363 KB
/
mapgen.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 "mapgen.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include <stdexcept>
#include <type_traits>
#include <unordered_map>
#include "all_enum_values.h"
#include "avatar.h"
#include "calendar.h"
#include "cata_assert.h"
#include "catacharset.h"
#include "character_id.h"
#include "city.h"
#include "clzones.h"
#include "colony.h"
#include "common_types.h"
#include "computer.h"
#include "condition.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "enum_conversions.h"
#include "enums.h"
#include "field.h"
#include "field_type.h"
#include "game.h"
#include "game_constants.h"
#include "generic_factory.h"
#include "global_vars.h"
#include "input.h"
#include "item.h"
#include "item_factory.h"
#include "item_group.h"
#include "itype.h"
#include "level_cache.h"
#include "line.h"
#include "magic_ter_furn_transform.h"
#include "map.h"
#include "map_extras.h"
#include "map_iterator.h"
#include "mapbuffer.h"
#include "mapdata.h"
#include "mapgen_functions.h"
#include "mapgendata.h"
#include "mapgenformat.h"
#include "memory_fast.h"
#include "mission.h"
#include "mongroup.h"
#include "npc.h"
#include "omdata.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "pocket_type.h"
#include "point.h"
#include "ret_val.h"
#include "rng.h"
#include "string_formatter.h"
#include "submap.h"
#include "text_snippets.h"
#include "tileray.h"
#include "to_string_id.h"
#include "translations.h"
#include "trap.h"
#include "units.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_group.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weighted_list.h"
#include "creature_tracker.h"
static const furn_str_id furn_f_bed( "f_bed" );
static const furn_str_id furn_f_console( "f_console" );
static const furn_str_id furn_f_counter( "f_counter" );
static const furn_str_id furn_f_dresser( "f_dresser" );
static const furn_str_id furn_f_flower_fungal( "f_flower_fungal" );
static const furn_str_id furn_f_fridge( "f_fridge" );
static const furn_str_id furn_f_fungal_clump( "f_fungal_clump" );
static const furn_str_id furn_f_rack( "f_rack" );
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_sign( "f_sign" );
static const furn_str_id furn_f_table( "f_table" );
static const furn_str_id furn_f_toilet( "f_toilet" );
static const furn_str_id furn_f_vending_c( "f_vending_c" );
static const furn_str_id furn_f_vending_c_off( "f_vending_c_off" );
static const furn_str_id furn_f_vending_reinforced( "f_vending_reinforced" );
static const furn_str_id furn_f_vending_reinforced_off( "f_vending_reinforced_off" );
static const item_group_id Item_spawn_data_ammo_rare( "ammo_rare" );
static const item_group_id Item_spawn_data_bed( "bed" );
static const item_group_id Item_spawn_data_bionics( "bionics" );
static const item_group_id Item_spawn_data_bionics_common( "bionics_common" );
static const item_group_id Item_spawn_data_chem_lab( "chem_lab" );
static const item_group_id Item_spawn_data_cleaning( "cleaning" );
static const item_group_id Item_spawn_data_cloning_vat( "cloning_vat" );
static const item_group_id Item_spawn_data_dissection( "dissection" );
static const item_group_id Item_spawn_data_dresser( "dresser" );
static const item_group_id Item_spawn_data_goo( "goo" );
static const item_group_id Item_spawn_data_guns_rare( "guns_rare" );
static const item_group_id Item_spawn_data_lab_dorm( "lab_dorm" );
static const item_group_id Item_spawn_data_mut_lab( "mut_lab" );
static const item_group_id Item_spawn_data_sewer( "sewer" );
static const item_group_id Item_spawn_data_teleport( "teleport" );
static const itype_id itype_avgas( "avgas" );
static const itype_id itype_diesel( "diesel" );
static const itype_id itype_gasoline( "gasoline" );
static const itype_id itype_jp8( "jp8" );
static const mongroup_id GROUP_BREATHER( "GROUP_BREATHER" );
static const mongroup_id GROUP_BREATHER_HUB( "GROUP_BREATHER_HUB" );
static const mongroup_id GROUP_FUNGI_FUNGALOID( "GROUP_FUNGI_FUNGALOID" );
static const mongroup_id GROUP_LAB( "GROUP_LAB" );
static const mongroup_id GROUP_LAB_CYBORG( "GROUP_LAB_CYBORG" );
static const mongroup_id GROUP_LAB_SECURITY( "GROUP_LAB_SECURITY" );
static const mongroup_id GROUP_NETHER( "GROUP_NETHER" );
static const mongroup_id GROUP_ROBOT_SECUBOT( "GROUP_ROBOT_SECUBOT" );
static const mongroup_id GROUP_SLIME( "GROUP_SLIME" );
static const mongroup_id GROUP_TURRET( "GROUP_TURRET" );
static const oter_str_id oter_ants_es( "ants_es" );
static const oter_str_id oter_ants_esw( "ants_esw" );
static const oter_str_id oter_ants_ew( "ants_ew" );
static const oter_str_id oter_ants_lab( "ants_lab" );
static const oter_str_id oter_ants_lab_stairs( "ants_lab_stairs" );
static const oter_str_id oter_ants_ne( "ants_ne" );
static const oter_str_id oter_ants_nes( "ants_nes" );
static const oter_str_id oter_ants_nesw( "ants_nesw" );
static const oter_str_id oter_ants_new( "ants_new" );
static const oter_str_id oter_ants_ns( "ants_ns" );
static const oter_str_id oter_ants_nsw( "ants_nsw" );
static const oter_str_id oter_ants_sw( "ants_sw" );
static const oter_str_id oter_ants_wn( "ants_wn" );
static const oter_str_id oter_central_lab( "central_lab" );
static const oter_str_id oter_central_lab_core( "central_lab_core" );
static const oter_str_id oter_central_lab_finale( "central_lab_finale" );
static const oter_str_id oter_central_lab_stairs( "central_lab_stairs" );
static const oter_str_id oter_ice_lab( "ice_lab" );
static const oter_str_id oter_ice_lab_core( "ice_lab_core" );
static const oter_str_id oter_ice_lab_finale( "ice_lab_finale" );
static const oter_str_id oter_ice_lab_stairs( "ice_lab_stairs" );
static const oter_str_id oter_lab( "lab" );
static const oter_str_id oter_lab_core( "lab_core" );
static const oter_str_id oter_lab_finale( "lab_finale" );
static const oter_str_id oter_lab_stairs( "lab_stairs" );
static const oter_str_id oter_slimepit( "slimepit" );
static const oter_str_id oter_slimepit_bottom( "slimepit_bottom" );
static const oter_str_id oter_slimepit_down( "slimepit_down" );
static const oter_str_id oter_tower_lab( "tower_lab" );
static const oter_str_id oter_tower_lab_finale( "tower_lab_finale" );
static const oter_str_id oter_tower_lab_stairs( "tower_lab_stairs" );
static const oter_type_str_id oter_type_road( "road" );
static const oter_type_str_id oter_type_sewer( "sewer" );
static const ter_str_id ter_t_bars( "t_bars" );
static const ter_str_id ter_t_card_science( "t_card_science" );
static const ter_str_id ter_t_concrete_wall( "t_concrete_wall" );
static const ter_str_id ter_t_cvdbody( "t_cvdbody" );
static const ter_str_id ter_t_cvdmachine( "t_cvdmachine" );
static const ter_str_id ter_t_dirt( "t_dirt" );
static const ter_str_id ter_t_door_glass_frosted_c( "t_door_glass_frosted_c" );
static const ter_str_id ter_t_door_metal_c( "t_door_metal_c" );
static const ter_str_id ter_t_door_metal_locked( "t_door_metal_locked" );
static const ter_str_id ter_t_floor( "t_floor" );
static const ter_str_id ter_t_fungus_floor_in( "t_fungus_floor_in" );
static const ter_str_id ter_t_fungus_wall( "t_fungus_wall" );
static const ter_str_id ter_t_grass( "t_grass" );
static const ter_str_id ter_t_marloss( "t_marloss" );
static const ter_str_id ter_t_radio_tower( "t_radio_tower" );
static const ter_str_id ter_t_reinforced_door_glass_c( "t_reinforced_door_glass_c" );
static const ter_str_id ter_t_reinforced_glass( "t_reinforced_glass" );
static const ter_str_id ter_t_rock_floor( "t_rock_floor" );
static const ter_str_id ter_t_sewage( "t_sewage" );
static const ter_str_id ter_t_slime( "t_slime" );
static const ter_str_id ter_t_slope_down( "t_slope_down" );
static const ter_str_id ter_t_slope_up( "t_slope_up" );
static const ter_str_id ter_t_stairs_down( "t_stairs_down" );
static const ter_str_id ter_t_stairs_up( "t_stairs_up" );
static const ter_str_id ter_t_strconc_floor( "t_strconc_floor" );
static const ter_str_id ter_t_thconc_floor( "t_thconc_floor" );
static const ter_str_id ter_t_thconc_floor_olight( "t_thconc_floor_olight" );
static const ter_str_id ter_t_vat( "t_vat" );
static const ter_str_id ter_t_water_sh( "t_water_sh" );
static const trait_id trait_NPC_STATIC_NPC( "NPC_STATIC_NPC" );
static const trap_str_id tr_dissector( "tr_dissector" );
static const trap_str_id tr_drain( "tr_drain" );
static const trap_str_id tr_glow( "tr_glow" );
static const trap_str_id tr_goo( "tr_goo" );
static const trap_str_id tr_hum( "tr_hum" );
static const trap_str_id tr_portal( "tr_portal" );
static const trap_str_id tr_shadow( "tr_shadow" );
static const trap_str_id tr_snake( "tr_snake" );
static const trap_str_id tr_telepad( "tr_telepad" );
static const vproto_id vehicle_prototype_shopping_cart( "shopping_cart" );
#define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
static constexpr int MON_RADIUS = 3;
static void science_room( map *m, const point &p1, const point &p2, int z, int rotate );
// Assumptions:
// - The map supplied is empty, i.e. no grid entries are in use
// - The map supports Z levels.
void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save_results )
{
dbg( D_INFO ) << "map::generate( g[" << g.get() << "], p[" << p << "], "
"when[" << to_string( when ) << "] )";
const tripoint_abs_sm p_sm_base = project_to<coords::sm>( p );
std::vector<bool> generated;
generated.resize( my_MAPSIZE * my_MAPSIZE * OVERMAP_LAYERS );
// Prepare the canvas...
for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) {
const tripoint_rel_sm pos( gridx, gridy, gridz );
const size_t grid_pos = get_nonant( pos );
// For some reason 'emplace' doesn't work. emplacing data later overwrote data...
generated[grid_pos] = MAPBUFFER.submap_exists( p_sm_base.xy() + pos );
if( !generated.at( grid_pos ) || !save_results ) {
setsubmap( grid_pos, new submap() );
// Generate uniform submaps immediately and cheaply.
// This causes them to be available for "proper" overlays even if on a lower Z level.
const ter_str_id ter = uniform_terrain( overmap_buffer.ter( { p.xy(), gridz } ) );
if( ter != t_null.id() ) {
getsubmap( grid_pos )->set_all_ter( ter, true );
getsubmap( grid_pos )->last_touched = calendar::turn;
}
} else {
setsubmap( grid_pos, MAPBUFFER.lookup_submap( p_sm_base.xy() + pos ) );
}
}
}
}
std::vector<submap *> saved_overlay;
saved_overlay.reserve( 4 );
for( size_t index = 0; index <= 3; index++ ) {
saved_overlay.emplace_back( nullptr );
}
// We're generating all Z levels in one go to be able to account for dependencies
// between levels. We iterate from the top down based on the assumption it is
// more common to add overlays on other Z levels upwards than downwards, so
// going downwards we can immediately apply overlays onto the already generated
// map, while overlays further down will have to be reapplied when the basic
// map exists.
for( int gridz = OVERMAP_HEIGHT; gridz >= -OVERMAP_DEPTH; gridz-- ) {
const tripoint_abs_sm p_sm = { p_sm_base.xy(), gridz };
set_abs_sub( p_sm );
for( int gridx = 0; gridx <= 1; gridx++ ) {
for( int gridy = 0; gridy <= 1; gridy++ ) {
const tripoint_rel_sm pos( gridx, gridy, gridz );
const size_t grid_pos = get_nonant( pos );
if( ( !generated.at( grid_pos ) || !save_results ) &&
!getsubmap( grid_pos )->is_uniform() &&
uniform_terrain( overmap_buffer.ter( { p.xy(), gridz } ) ) == t_null.id() ) {
saved_overlay[gridx + gridy * 2] = getsubmap( grid_pos );
setsubmap( grid_pos, new submap() );
}
}
}
oter_id terrain_type = overmap_buffer.ter( tripoint_abs_omt( p.xy(), gridz ) );
// This attempts to scale density of zombies inversely with distance from the nearest city.
// In other words, make city centers dense and perimeters sparse.
float density = 0.0f;
for( int i = -MON_RADIUS; i <= MON_RADIUS; i++ ) {
for( int j = -MON_RADIUS; j <= MON_RADIUS; j++ ) {
density += overmap_buffer.ter( { p.x() + i, p.y() + j, gridz } )->get_mondensity();
}
}
density = density / 100;
// Not sure if we actually have to check all submaps.
const bool any_missing = !generated.at( get_nonant( { point_rel_sm_zero, p_sm.z() } ) ) ||
!generated.at( get_nonant( { point_rel_sm_east, p_sm.z() } ) ) ||
!generated.at( get_nonant( { point_rel_sm_south_east, p_sm.z() } ) ) ||
!generated.at( get_nonant( { point_rel_sm_south, p_sm.z() } ) );
mapgendata dat( { p.xy(), gridz}, *this, density, when, nullptr );
if( ( any_missing || !save_results ) &&
uniform_terrain( overmap_buffer.ter( { p.xy(), gridz } ) ) == t_null.id() ) {
draw_map( dat );
}
// Merge the overlays generated earlier into the current Z level now we have the base map on it.
for( int gridx = 0; gridx <= 1; gridx++ ) {
for( int gridy = 0; gridy <= 1; gridy++ ) {
const tripoint_rel_sm pos( gridx, gridy, gridz );
const size_t index = gridx + gridy * 2;
if( saved_overlay.at( index ) != nullptr ) {
const size_t grid_pos = get_nonant( pos );
getsubmap( grid_pos )->merge_submaps( saved_overlay.at( index ), true );
delete saved_overlay.at( index );
saved_overlay[index] = nullptr;
}
}
}
if( any_missing || !save_results ) {
// At some point, we should add region information so we can grab the appropriate extras
map_extras &this_ex = region_settings_map["default"].region_extras[terrain_type->get_extras()];
map_extras ex = this_ex.filtered_by( dat );
if( this_ex.chance > 0 && ex.values.empty() && !this_ex.values.empty() ) {
DebugLog( D_WARNING, D_MAP_GEN ) << "Overmap terrain " << terrain_type->get_type_id().str() <<
" (extra type \"" << terrain_type->get_extras() <<
"\") zlevel = " << p.z() <<
" is out of range of all assigned map extras. Skipping map extra generation.";
} else if( ex.chance > 0 && one_in( ex.chance ) ) {
map_extra_id *extra = ex.values.pick();
if( extra == nullptr ) {
debugmsg( "failed to pick extra for type %s (ter = %s)", terrain_type->get_extras(),
terrain_type->get_type_id().str() );
} else {
MapExtras::apply_function( *ex.values.pick(), *this, tripoint_abs_sm( abs_sub ) );
}
}
const overmap_static_spawns &spawns = terrain_type->get_static_spawns();
float spawn_density = 1.0f;
if( MonsterGroupManager::is_animal( spawns.group ) ) {
spawn_density = get_option< float >( "SPAWN_ANIMAL_DENSITY" );
} else {
spawn_density = get_option< float >( "SPAWN_DENSITY" );
}
// Apply a multiplier to the number of monsters for really high densities.
float odds_after_density = spawns.chance * spawn_density;
const float max_odds = 100 - ( 100 - spawns.chance ) / 2.0f;
float density_multiplier = 1.0f;
if( odds_after_density > max_odds ) {
density_multiplier = 1.0f * odds_after_density / max_odds;
odds_after_density = max_odds;
}
const int spawn_count = roll_remainder( density_multiplier );
if( spawns.group && x_in_y( odds_after_density, 100 ) ) {
int pop = spawn_count * rng( spawns.population.min, spawns.population.max );
for( ; pop > 0; pop-- ) {
std::vector<MonsterGroupResult> spawn_details =
MonsterGroupManager::GetResultFromGroup( spawns.group, &pop );
for( const MonsterGroupResult &mgr : spawn_details ) {
if( !mgr.name ) {
continue;
}
if( const std::optional<tripoint_bub_ms> pt =
random_point_on_level( *this, gridz, [this]( const tripoint_bub_ms & n ) {
return passable( n );
} ) ) {
const tripoint_bub_ms pnt = pt.value();
add_spawn( mgr, pnt );
}
}
}
}
}
}
if( save_results ) {
for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) {
const tripoint_rel_sm pos( gridx, gridy, gridz );
const size_t grid_pos = get_nonant( pos );
if( !generated.at( grid_pos ) ) {
if( gridx <= 1 && gridy <= 1 ) {
saven( { gridx, gridy, gridz } );
} else {
delete getsubmap( grid_pos );
}
}
}
}
}
}
set_abs_sub( p_sm_base );
}
void map::delete_unmerged_submaps()
{
tripoint_abs_sm sm_base = get_abs_sub();
for( size_t index = 0; index < grid.size(); index++ ) {
tripoint offset;
const int ix = static_cast<int>( index );
// This is the inverse of get_nonant.
if( zlevels ) {
offset = { ( ix / OVERMAP_LAYERS ) % my_MAPSIZE, ix / OVERMAP_LAYERS / my_MAPSIZE, ix % OVERMAP_LAYERS - OVERMAP_DEPTH };
} else {
offset = { ix % my_MAPSIZE, ix / my_MAPSIZE, sm_base.z()};
}
if( grid[index] != nullptr && MAPBUFFER.lookup_submap( sm_base.xy() + offset ) != grid[index] ) {
delete grid[index];
grid[index] = nullptr;
}
}
}
void mapgen_function_builtin::generate( mapgendata &mgd )
{
( *fptr )( mgd );
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
///// mapgen_function class.
///// all sorts of ways to apply our hellish reality to a grid-o-squares
class mapgen_basic_container
{
private:
std::vector<std::shared_ptr<mapgen_function>> mapgens_;
//mapgens that need to be recalculated with a function when spawned
std::vector<std::shared_ptr<mapgen_function>> mapgens_to_recalc_;
weighted_int_list<std::shared_ptr<mapgen_function>> weights_;
public:
int add( const std::shared_ptr<mapgen_function> &ptr ) {
cata_assert( ptr );
if( std::find( mapgens_.begin(), mapgens_.end(), ptr ) != mapgens_.end() ) {
debugmsg( "Adding duplicate mapgen to container!" );
}
mapgens_.push_back( ptr );
return mapgens_.size() - 1;
}
/**
* Pick a mapgen function randomly and call its generate function.
* This basically runs the mapgen functions with the given @ref mapgendata
* as argument.
* @return Whether the mapgen function has been run. It may not get run if
* the list of mapgen functions is effectively empty.
* @p hardcoded_weight Weight for an additional entry. If that entry is chosen,
* false is returned. If unsure, just use 0 for it.
*/
bool generate( mapgendata &dat, const int hardcoded_weight ) {
for( const std::shared_ptr<mapgen_function> &ptr : mapgens_to_recalc_ ) {
dialogue d( get_talker_for( get_avatar() ), std::make_unique<talker>() );
int const weight = ptr->weight.evaluate( d );
if( weight >= 1 ) {
weights_.add_or_replace( ptr, weight );
} else {
weights_.remove( ptr );
}
}
if( hardcoded_weight > 0 &&
rng( 1, weights_.get_weight() + hardcoded_weight ) > weights_.get_weight() ) {
return false;
}
const std::shared_ptr<mapgen_function> *const ptr = weights_.pick();
if( !ptr ) {
return false;
}
cata_assert( *ptr );
( *ptr )->generate( dat );
return true;
}
/**
* Calls @ref mapgen_function::setup and sets up the internal weighted list using
* the **current** value of @ref mapgen_function::weight. This value may have
* changed since it was first added, so this is needed to recalculate the weighted list.
*/
void setup() {
for( const std::shared_ptr<mapgen_function> &ptr : mapgens_ ) {
cata_assert( ptr->weight );
if( ptr->weight.is_constant() ) {
int const weight = ptr->weight.constant();
if( weight < 1 ) {
continue; // rejected!
}
weights_.add( ptr, weight );
} else {
mapgens_to_recalc_.push_back( ptr );
}
ptr->setup();
}
// Not needed anymore, pointers are now stored in weights_ (or not used at all)
mapgens_.clear();
}
void finalize_parameters() {
for( auto &mapgen_function_ptr : weights_ ) {
mapgen_function_ptr.obj->finalize_parameters();
}
}
void check_consistency() const {
for( const auto &mapgen_function_ptr : weights_ ) {
mapgen_function_ptr.obj->check();
}
}
void check_consistency_with( const oter_t &ter ) const {
for( const auto &mapgen_function_ptr : weights_ ) {
mapgen_function_ptr.obj->check_consistent_with( ter );
}
}
mapgen_parameters get_mapgen_params( mapgen_parameter_scope scope,
const std::string &context ) const {
mapgen_parameters result;
for( const weighted_object<int, std::shared_ptr<mapgen_function>> &p : weights_ ) {
result.check_and_merge( p.obj->get_mapgen_params( scope ), context );
}
return result;
}
};
class mapgen_factory
{
private:
std::map<std::string, mapgen_basic_container> mapgens_;
/// Collect all the possible and expected keys that may get used with @ref pick.
static std::set<std::string> get_usages() {
std::set<std::string> result;
for( const oter_t &elem : overmap_terrains::get_all() ) {
result.insert( elem.get_mapgen_id() );
result.insert( elem.id.str() );
}
// Why do I have to repeat the MapExtras here? Wouldn't "MapExtras::factory" be enough?
for( const map_extra &elem : MapExtras::mapExtraFactory().get_all() ) {
if( elem.generator_method == map_extra_method::mapgen ) {
result.insert( elem.generator_id );
}
}
// Used in C++ code only, see calls to `oter_mapgen.generate()` below
result.insert( "lab_1side" );
result.insert( "lab_4side" );
result.insert( "lab_finale_1level" );
return result;
}
public:
void reset() {
mapgens_.clear();
}
/// @see mapgen_basic_container::setup
void setup() {
for( std::pair<const std::string, mapgen_basic_container> &omw : mapgens_ ) {
omw.second.setup();
inp_mngr.pump_events();
}
// Dummy entry, overmap terrain null should never appear and is
// therefore never generated.
mapgens_.erase( "null" );
}
void finalize_parameters() {
for( std::pair<const std::string, mapgen_basic_container> &omw : mapgens_ ) {
omw.second.finalize_parameters();
}
}
void check_consistency() const {
// Cache all strings that may get looked up here so we don't have to go through
// all the sources for them upon each loop.
const std::set<std::string> usages = get_usages();
for( const std::pair<const std::string, mapgen_basic_container> &omw : mapgens_ ) {
omw.second.check_consistency();
if( usages.count( omw.first ) == 0 ) {
debugmsg( "Mapgen %s is not used by anything!", omw.first );
}
}
}
/**
* Checks whether we have an entry for the given key.
* Note that the entry itself may not contain any valid mapgen instance
* (could all have been removed via @ref erase).
*/
bool has( const std::string &key ) const {
return mapgens_.count( key ) != 0;
}
const mapgen_basic_container *find( const std::string &key ) const {
auto it = mapgens_.find( key );
if( it == mapgens_.end() ) {
return nullptr;
} else {
return &it->second;
}
}
/// @see mapgen_basic_container::add
int add( const std::string &key, const std::shared_ptr<mapgen_function> &ptr ) {
return mapgens_[key].add( ptr );
}
/// @see mapgen_basic_container::generate
bool generate( mapgendata &dat, const std::string &key, const int hardcoded_weight = 0 ) {
const auto iter = mapgens_.find( key );
if( iter == mapgens_.end() ) {
return false;
}
return iter->second.generate( dat, hardcoded_weight );
}
mapgen_parameters get_map_special_params( const std::string &key ) const {
const auto iter = mapgens_.find( key );
if( iter == mapgens_.end() ) {
return mapgen_parameters();
}
return iter->second.get_mapgen_params( mapgen_parameter_scope::overmap_special,
// NOLINTNEXTLINE(cata-translate-string-literal)
string_format( "map special %s", key ) );
}
};
static mapgen_factory oter_mapgen;
std::map<nested_mapgen_id, nested_mapgen> nested_mapgens;
std::map<update_mapgen_id, update_mapgen> update_mapgens;
static std::unordered_map<std::string, tripoint_abs_ms> queued_points;
template<>
bool string_id<nested_mapgen>::is_valid() const
{
return str() == "null" || nested_mapgens.find( *this ) != nested_mapgens.end();
}
template<>
const nested_mapgen &string_id<nested_mapgen>::obj() const
{
auto it = nested_mapgens.find( *this );
if( it == nested_mapgens.end() ) {
debugmsg( "Using invalid nested_mapgen_id %s", str() );
static const nested_mapgen null_mapgen;
return null_mapgen;
}
return it->second;
}
template<>
bool string_id<update_mapgen>::is_valid() const
{
return str() == "null" || update_mapgens.find( *this ) != update_mapgens.end();
}
template<>
const update_mapgen &string_id<update_mapgen>::obj() const
{
auto it = update_mapgens.find( *this );
if( it == update_mapgens.end() ) {
debugmsg( "Using invalid nested_mapgen_id %s", str() );
static const update_mapgen null_mapgen;
return null_mapgen;
}
return it->second;
}
/*
* setup mapgen_basic_container::weights_ which mapgen uses to diceroll. Also setup mapgen_function_json
*/
void calculate_mapgen_weights() // TODO: rename as it runs jsonfunction setup too
{
oter_mapgen.setup();
// Not really calculate weights, but let's keep it here for now
for( auto &pr : nested_mapgens ) {
for( const weighted_object<int, std::shared_ptr<mapgen_function_json_nested>> &ptr :
pr.second.funcs() ) {
ptr.obj->setup();
inp_mngr.pump_events();
}
}
for( auto &pr : update_mapgens ) {
for( const auto &ptr : pr.second.funcs() ) {
ptr->setup();
inp_mngr.pump_events();
}
}
// Having set up all the mapgens we can now perform a second
// pass of finalizing their parameters
oter_mapgen.finalize_parameters();
for( auto &pr : nested_mapgens ) {
for( const weighted_object<int, std::shared_ptr<mapgen_function_json_nested>> &ptr :
pr.second.funcs() ) {
ptr.obj->finalize_parameters();
inp_mngr.pump_events();
}
}
for( auto &pr : update_mapgens ) {
for( const auto &ptr : pr.second.funcs() ) {
ptr->finalize_parameters();
inp_mngr.pump_events();
}
}
}
void check_mapgen_definitions()
{
oter_mapgen.check_consistency();
for( const auto &oter_definition : nested_mapgens ) {
for( const auto &mapgen_function_ptr : oter_definition.second.funcs() ) {
mapgen_function_ptr.obj->check();
}
}
for( const auto &oter_definition : update_mapgens ) {
for( const auto &mapgen_function_ptr : oter_definition.second.funcs() ) {
mapgen_function_ptr->check();
}
}
}
/////////////////////////////////////////////////////////////////////////////////
///// json mapgen functions
///// 1 - init():
/**
* Tiny little namespace to hold error messages
*/
namespace mapgen_defer
{
static std::string member;
static std::string message;
static bool defer;
static JsonObject jsi;
} // namespace mapgen_defer
static void set_mapgen_defer( const JsonObject &jsi, const std::string &member,
const std::string &message )
{
mapgen_defer::defer = true;
mapgen_defer::jsi = jsi;
mapgen_defer::member = member;
mapgen_defer::message = message;
}
/*
* load a single mapgen json structure; this can be inside an overmap_terrain, or on it's own.
*/
std::shared_ptr<mapgen_function>
load_mapgen_function( const JsonObject &jio, const std::string &id_base, const point &offset,
const point &total )
{
dbl_or_var weight = get_dbl_or_var( jio, "weight", false, 1000 );
if( weight.min.is_constant() && ( weight.min.constant() < 0 ||
weight.min.constant() >= INT_MAX ) ) {
jio.throw_error_at( "weight", "min value out of bounds (0 - max int)" );
}
if( weight.pair && weight.max.is_constant() && ( weight.max.constant() < 0 ||
weight.max.constant() >= INT_MAX ) ) {
jio.throw_error_at( "weight", "max value out of bounds (0 - max int)" );
}
if( jio.get_bool( "disabled", false ) ) {
jio.allow_omitted_members();
return nullptr; // nothing
}
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "builtin" ) {
if( const building_gen_pointer ptr = get_mapgen_cfunction( jio.get_string( "name" ) ) ) {
return std::make_shared<mapgen_function_builtin>( ptr, std::move( weight ) );
} else {
jio.throw_error_at( "name", "function does not exist" );
}
} else if( mgtype == "json" ) {
if( !jio.has_object( "object" ) ) {
jio.throw_error( R"(mapgen with method "json" must define key "object")" );
}
JsonObject jo = jio.get_object( "object" );
jo.allow_omitted_members();
return std::make_shared<mapgen_function_json>(
jo, std::move( weight ), "mapgen " + id_base, offset, total );
} else {
jio.throw_error_at( "method", R"(invalid value: must be "builtin" or "json")" );
}
}
void load_and_add_mapgen_function( const JsonObject &jio, const std::string &id_base,
const point &offset, const point &total )
{
std::shared_ptr<mapgen_function> f = load_mapgen_function( jio, id_base, offset, total );
if( f ) {
oter_mapgen.add( id_base, f );
}
}
static void load_nested_mapgen( const JsonObject &jio, const nested_mapgen_id &id_base )
{
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "json" ) {
if( jio.has_object( "object" ) ) {
int weight = jio.get_int( "weight", 1000 );
JsonObject jo = jio.get_object( "object" );
jo.allow_omitted_members();
nested_mapgens[id_base].add(
std::make_shared<mapgen_function_json_nested>(
jo, "nested mapgen " + id_base.str() ),
weight );
} else {
debugmsg( "Nested mapgen: Invalid mapgen function (missing \"object\" object)", id_base.c_str() );
}
} else {
debugmsg( "Nested mapgen: type for id %s was %s, but nested mapgen only supports \"json\"",
id_base.c_str(), mgtype.c_str() );
}
}
static void load_update_mapgen( const JsonObject &jio, const update_mapgen_id &id_base )
{
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "json" ) {
if( jio.has_object( "object" ) ) {
JsonObject jo = jio.get_object( "object" );
jo.allow_omitted_members();
update_mapgens[id_base].add(
std::make_unique<update_mapgen_function_json>(
jo, "update mapgen " + id_base.str() ) );
} else {
debugmsg( "Update mapgen: Invalid mapgen function (missing \"object\" object)",
id_base.c_str() );
}
} else {
debugmsg( "Update mapgen: type for id %s was %s, but update mapgen only supports \"json\"",
id_base.c_str(), mgtype.c_str() );
}
}
/*
* feed bits `o json from standalone file to load_mapgen_function. (standalone json "type": "mapgen")
*/
void load_mapgen( const JsonObject &jo )
{
// NOLINTNEXTLINE(cata-use-named-point-constants)
static constexpr point point_one( 1, 1 );
if( jo.has_array( "om_terrain" ) ) {
JsonArray ja = jo.get_array( "om_terrain" );
if( ja.test_array() ) {
point offset;
point total( ja.get_array( 0 ).size(), ja.size() );
for( JsonArray row_items : ja ) {
for( const std::string mapgenid : row_items ) {
load_and_add_mapgen_function( jo, mapgenid, offset, total );
offset.x++;
}
offset.y++;
offset.x = 0;
}
} else {
std::vector<std::string> mapgenid_list;
for( const std::string line : ja ) {
mapgenid_list.push_back( line );
}
if( !mapgenid_list.empty() ) {
const std::string mapgenid = mapgenid_list[0];
const auto mgfunc = load_mapgen_function( jo, mapgenid, point_zero, point_one );
if( mgfunc ) {
for( auto &i : mapgenid_list ) {
oter_mapgen.add( i, mgfunc );
}
}
}
}
} else if( jo.has_string( "om_terrain" ) ) {
load_and_add_mapgen_function( jo, jo.get_string( "om_terrain" ), point_zero, point_one );
} else if( jo.has_string( "nested_mapgen_id" ) ) {
load_nested_mapgen( jo, nested_mapgen_id( jo.get_string( "nested_mapgen_id" ) ) );
} else if( jo.has_string( "update_mapgen_id" ) ) {
load_update_mapgen( jo, update_mapgen_id( jo.get_string( "update_mapgen_id" ) ) );
} else {
debugmsg( "mapgen entry requires \"om_terrain\" or \"nested_mapgen_id\"(string, array of strings, or array of array of strings)\n%s\n",
jo.str() );
}
}
void reset_mapgens()
{
oter_mapgen.reset();
nested_mapgens.clear();
update_mapgens.clear();
}
/////////////////////////////////////////////////////////////////////////////////
///// 2 - right after init() finishes parsing all game json and terrain info/etc is set..
///// ...parse more json! (mapgen_function_json)
size_t mapgen_function_json_base::calc_index( const point &p ) const
{
if( p.x >= mapgensize.x ) {
debugmsg( "invalid value %zu for x in calc_index", p.x );
}
if( p.y >= mapgensize.y ) {
debugmsg( "invalid value %zu for y in calc_index", p.y );
}
return p.y * mapgensize.y + p.x;
}
static bool common_check_bounds( const jmapgen_int &x, const jmapgen_int &y, const jmapgen_int &z,
const point &mapgensize, const JsonObject &jso )
{
half_open_rectangle<point> bounds( point_zero, mapgensize );
if( !bounds.contains( point( x.val, y.val ) ) ) {
return false;
}
if( x.valmax < x.val ) {
jso.throw_error( "x maximum is less than x minimum" );
}
if( y.valmax < y.val ) {
jso.throw_error( "y maximum is less than y minimum" );
}
if( z.valmax != z.val ) {
jso.throw_error( "z maximum has to be identical to z minimum" );
}
if( x.valmax > mapgensize.x - 1 ) {
jso.throw_error_at( "x", "coordinate range cannot cross grid boundaries" );
}
if( y.valmax > mapgensize.y - 1 ) {
jso.throw_error_at( "y", "coordinate range cannot cross grid boundaries" );
}
return true;
}
void mapgen_function_json_base::merge_non_nest_parameters_into(
mapgen_parameters ¶ms, const std::string &outer_context ) const
{
// NOLINTNEXTLINE(cata-translate-string-literal)
const std::string context = string_format( "%s within %s", context_, outer_context );
params.check_and_merge( parameters, context, mapgen_parameter_scope::nest );
}
bool mapgen_function_json_base::check_inbounds( const jmapgen_int &x, const jmapgen_int &y,
const jmapgen_int &z,
const JsonObject &jso ) const
{
return common_check_bounds( x, y, z, mapgensize, jso );
}
mapgen_function_json_base::mapgen_function_json_base(
const JsonObject &jsobj, const std::string &context )
: jsobj( jsobj )
, context_( context )
, is_ready( false )
, mapgensize( SEEX * 2, SEEY * 2 )
, total_size( mapgensize )
, objects( m_offset, mapgensize, total_size )
{
this->jsobj.allow_omitted_members();
}
mapgen_function_json_base::~mapgen_function_json_base() = default;
mapgen_function_json::mapgen_function_json( const JsonObject &jsobj,
dbl_or_var w, const std::string &context, const point &grid_offset, const point &grid_total )
: mapgen_function( std::move( w ) )
, mapgen_function_json_base( jsobj, context )
, fill_ter( t_null )
, rotation( 0 )
, fallback_predecessor_mapgen_( oter_str_id::NULL_ID() )
{
m_offset.x() = grid_offset.x * mapgensize.x;
m_offset.y() = grid_offset.y * mapgensize.y;
m_offset.z() = 0;
total_size.x = grid_total.x * mapgensize.x;
total_size.y = grid_total.y * mapgensize.y;
objects = jmapgen_objects( m_offset, mapgensize, total_size );
}
mapgen_function_json_nested::mapgen_function_json_nested(
const JsonObject &jsobj, const std::string &context )
: mapgen_function_json_base( jsobj, context )
, rotation( 0 )
{
}
jmapgen_int::jmapgen_int( point p ) : val( p.x ), valmax( p.y )