forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapgen.cpp
6539 lines (6101 loc) · 287 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 <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <stdexcept>
#include <unordered_map>
#include "basecamp.h"
#include "calendar.h"
#include "catacharset.h"
#include "character_id.h"
#include "clzones.h"
#include "numeric_interval.h"
#include "computer.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "field_type.h"
#include "game.h"
#include "game_constants.h"
#include "generic_factory.h"
#include "input.h"
#include "int_id.h"
#include "item.h"
#include "item_factory.h"
#include "item_group.h"
#include "itype.h"
#include "json.h"
#include "line.h"
#include "magic_ter_furn_transform.h"
#include "map.h"
#include "map_extras.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "mapgen_functions.h"
#include "mapgendata.h"
#include "mapgenformat.h"
#include "memory_fast.h"
#include "mission.h"
#include "mod_manager.h"
#include "mongroup.h"
#include "npc.h"
#include "omdata.h"
#include "options.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "player.h"
#include "point.h"
#include "point_float.h"
#include "rng.h"
#include "string_formatter.h"
#include "string_id.h"
#include "string_utils.h"
#include "submap.h"
#include "text_snippets.h"
#include "tileray.h"
#include "translations.h"
#include "trap.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vehicle_group.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weighted_list.h"
static const mongroup_id GROUP_BLOB( "GROUP_BLOB" );
static const mongroup_id GROUP_BREATHER( "GROUP_BREATHER" );
static const mongroup_id GROUP_BREATHER_HUB( "GROUP_BREATHER_HUB" );
static const mongroup_id GROUP_DOG_THING( "GROUP_DOG_THING" );
static const mongroup_id GROUP_FUNGI_FUNGALOID( "GROUP_FUNGI_FUNGALOID" );
static const mongroup_id GROUP_HAZMATBOT( "GROUP_HAZMATBOT" );
static const mongroup_id GROUP_LAB( "GROUP_LAB" );
static const mongroup_id GROUP_LAB_CYBORG( "GROUP_LAB_CYBORG" );
static const mongroup_id GROUP_NETHER( "GROUP_NETHER" );
static const mongroup_id GROUP_PLAIN( "GROUP_PLAIN" );
static const mongroup_id GROUP_ROBOT_SECUBOT( "GROUP_ROBOT_SECUBOT" );
static const mongroup_id GROUP_SEWER( "GROUP_SEWER" );
static const mongroup_id GROUP_SPIDER( "GROUP_SPIDER" );
static const mongroup_id GROUP_TRIFFID( "GROUP_TRIFFID" );
static const mongroup_id GROUP_TRIFFID_HEART( "GROUP_TRIFFID_HEART" );
static const mongroup_id GROUP_TRIFFID_OUTER( "GROUP_TRIFFID_OUTER" );
static const mongroup_id GROUP_TURRET( "GROUP_TURRET" );
static const mongroup_id GROUP_ZOMBIE( "GROUP_ZOMBIE" );
static const mongroup_id GROUP_ZOMBIE_COP( "GROUP_ZOMBIE_COP" );
static const trait_id trait_NPC_STATIC_NPC( "NPC_STATIC_NPC" );
#define dbg(x) DebugLogFL((x),DC::MapGen)
static constexpr int MON_RADIUS = 3;
static void science_room( map *m, point p1, point p2, int z, int rotate );
// (x,y,z) are absolute coordinates of a submap
// x%2 and y%2 must be 0!
void map::generate( const tripoint &p, const time_point &when )
{
dbg( DL::Info ) << "map::generate( g[" << g.get() << "], p[" << p <<
"], when[" << to_string( when ) << "] )";
set_abs_sub( p );
// First we have to create new submaps and initialize them to 0 all over
// We create all the submaps, even if we're not a tinymap, so that map
// generation which overflows won't cause a crash. At the bottom of this
// function, we save the upper-left 4 submaps, and delete the rest.
// Mapgen is not z-level aware yet. Only actually initialize current z-level
// because other submaps won't be touched.
for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
const size_t grid_pos = get_nonant( { gridx, gridy, p.z } );
if( getsubmap( grid_pos ) ) {
debugmsg( "Submap already exists at (%d, %d, %d)", gridx, gridy, p.z );
continue;
}
setsubmap( grid_pos, new submap() );
// TODO: memory leak if the code below throws before the submaps get stored/deleted!
}
}
// x, and y are submap coordinates, convert to overmap terrain coordinates
// TODO: fix point types
tripoint_abs_omt abs_omt( sm_to_omt_copy( p ) );
oter_id terrain_type = overmap_buffer.ter( abs_omt );
// 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.0;
for( int i = -MON_RADIUS; i <= MON_RADIUS; i++ ) {
for( int j = -MON_RADIUS; j <= MON_RADIUS; j++ ) {
density += overmap_buffer.ter( abs_omt + point( i, j ) )->get_mondensity();
}
}
density = density / 100;
mapgendata dat( abs_omt, *this, density, when, nullptr );
draw_map( dat );
// At some point, we should add region information so we can grab the appropriate extras
map_extras ex = region_settings_map["default"].region_extras[terrain_type->get_extras()];
if( ex.chance > 0 && one_in( ex.chance ) ) {
std::string *extra = ex.values.pick();
if( extra == nullptr ) {
debugmsg( "failed to pick extra for type %s", terrain_type->get_extras() );
} else {
MapExtras::apply_function( *( ex.values.pick() ), *this, abs_sub );
}
}
const auto &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.0;
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-- ) {
MonsterGroupResult spawn_details = MonsterGroupManager::GetResultFromGroup( spawns.group, &pop );
if( !spawn_details.name ) {
continue;
}
if( const std::optional<tripoint> pt =
random_point( *this, [this]( const tripoint & n ) {
return passable( n );
} ) ) {
add_spawn( spawn_details.name, spawn_details.pack_size, *pt );
}
}
}
// Okay, we know who are neighbors are. Let's draw!
// And finally save used submaps and delete the rest.
for( int i = 0; i < my_MAPSIZE; i++ ) {
for( int j = 0; j < my_MAPSIZE; j++ ) {
dbg( DL::Info ) << "map::generate: submap (" << i << "," << j << ")";
const tripoint pos( i, j, p.z );
if( i <= 1 && j <= 1 ) {
saven( pos );
} else {
const size_t grid_pos = get_nonant( pos );
delete getsubmap( grid_pos );
setsubmap( grid_pos, 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_;
weighted_int_list<std::shared_ptr<mapgen_function>> weights_;
public:
int add( const std::shared_ptr<mapgen_function> &ptr ) {
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 ) const {
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;
}
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_ ) {
const int weight = ptr->weight;
if( weight < 1 ) {
continue; // rejected!
}
weights_.add( ptr, weight );
ptr->setup();
}
// Not needed anymore, pointers are now stored in weights_ (or not used at all)
mapgens_.clear();
}
void check_consistency( const std::string &key ) {
for( auto &mapgen_function_ptr : weights_ ) {
mapgen_function_ptr.obj->check( key );
}
}
};
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 therefor never generated.
mapgens_.erase( "null" );
}
void check_consistency() {
// 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( std::pair<const std::string, mapgen_basic_container> &omw : mapgens_ ) {
omw.second.check_consistency( omw.first );
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;
}
/// @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 {
const auto iter = mapgens_.find( disable_mapgen ? "test" : key );
if( iter == mapgens_.end() ) {
return false;
}
return iter->second.generate( dat, hardcoded_weight );
}
};
static mapgen_factory oter_mapgen;
/*
* stores function ref and/or required data
*/
std::map<std::string, weighted_int_list<std::shared_ptr<mapgen_function_json_nested>> >
nested_mapgen;
std::map<std::string, std::vector<std::unique_ptr<update_mapgen_function_json>> > update_mapgen;
/*
* 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_mapgen ) {
for( weighted_object<int, std::shared_ptr<mapgen_function_json_nested>> &ptr : pr.second ) {
ptr.obj->setup();
inp_mngr.pump_events();
}
}
for( auto &pr : update_mapgen ) {
for( auto &ptr : pr.second ) {
ptr->setup();
inp_mngr.pump_events();
}
}
}
void check_mapgen_definitions()
{
oter_mapgen.check_consistency();
for( auto &oter_definition : nested_mapgen ) {
for( auto &mapgen_function_ptr : oter_definition.second ) {
mapgen_function_ptr.obj->check( oter_definition.first );
}
}
for( auto &oter_definition : update_mapgen ) {
for( auto &mapgen_function_ptr : oter_definition.second ) {
mapgen_function_ptr->check( oter_definition.first );
}
}
}
/////////////////////////////////////////////////////////////////////////////////
///// json mapgen functions
///// 1 - init():
/**
* Tiny little namespace to hold error messages
*/
namespace mapgen_defer
{
std::string member;
std::string message;
bool defer;
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, point offset, point total )
{
int mgweight = jio.get_int( "weight", 1000 );
if( mgweight <= 0 || 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, mgweight );
} else {
jio.throw_error( "function does not exist", "name" );
}
} else if( mgtype == "json" ) {
JsonObject jo = jio.get_object( "object" );
const json_source_location jsrc = jo.get_source_location();
jo.allow_omitted_members();
return std::make_shared<mapgen_function_json>(
jsrc, mgweight, offset, total );
} else {
jio.throw_error( R"(invalid value: must be "builtin" or "json")", "method" );
}
}
void load_and_add_mapgen_function( const JsonObject &jio, const std::string &id_base,
point offset, point total )
{
std::shared_ptr<mapgen_function> f = load_mapgen_function( jio, offset, total );
if( f ) {
oter_mapgen.add( id_base, f );
}
}
static void load_nested_mapgen( const JsonObject &jio, const std::string &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" );
const json_source_location jsrc = jo.get_source_location();
jo.allow_omitted_members();
nested_mapgen[id_base].add( std::make_shared<mapgen_function_json_nested>( jsrc ), 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 std::string &id_base )
{
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "json" ) {
if( jio.has_object( "object" ) ) {
JsonObject jo = jio.get_object( "object" );
const json_source_location jsrc = jo.get_source_location();
jo.allow_omitted_members();
update_mapgen[id_base].push_back(
std::make_unique<update_mapgen_function_json>( jsrc ) );
} 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 auto mgfunc = load_mapgen_function( jo, 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, jo.get_string( "nested_mapgen_id" ) );
} else if( jo.has_string( "update_mapgen_id" ) ) {
load_update_mapgen( jo, 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_mapgen.clear();
update_mapgen.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( 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,
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 > mapgensize.x - 1 ) {
jso.throw_error( "coordinate range cannot cross grid boundaries", "x" );
return false;
}
if( y.valmax > mapgensize.y - 1 ) {
jso.throw_error( "coordinate range cannot cross grid boundaries", "y" );
return false;
}
return true;
}
bool mapgen_function_json_base::check_inbounds( const jmapgen_int &x, const jmapgen_int &y,
const JsonObject &jso ) const
{
return common_check_bounds( x, y, mapgensize, jso );
}
mapgen_function_json_base::mapgen_function_json_base( const json_source_location &jsrcloc )
: jsrcloc( jsrcloc )
, do_format( false )
, is_ready( false )
, mapgensize( SEEX * 2, SEEY * 2 )
, total_size( mapgensize )
, objects( m_offset, mapgensize, total_size )
{
}
mapgen_function_json_base::~mapgen_function_json_base() = default;
mapgen_function_json::mapgen_function_json( const json_source_location &jsrcloc, const int w,
point grid_offset, point grid_total )
: mapgen_function( w )
, mapgen_function_json_base( jsrcloc )
, fill_ter( t_null )
, rotation( 0 )
{
m_offset.x = grid_offset.x * mapgensize.x;
m_offset.y = grid_offset.y * mapgensize.y;
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 json_source_location &jsrcloc )
: mapgen_function_json_base( jsrcloc )
, rotation( 0 )
{
}
jmapgen_int::jmapgen_int( point p ) : val( p.x ), valmax( p.y ) {}
jmapgen_int::jmapgen_int( const JsonObject &jo, const std::string &tag )
{
if( jo.has_array( tag ) ) {
JsonArray sparray = jo.get_array( tag );
if( sparray.size() < 1 || sparray.size() > 2 ) {
jo.throw_error( "invalid data: must be an array of 1 or 2 values", tag );
}
val = sparray.get_int( 0 );
if( sparray.size() == 2 ) {
valmax = sparray.get_int( 1 );
} else {
valmax = val;
}
} else {
val = valmax = jo.get_int( tag );
}
}
jmapgen_int::jmapgen_int( const JsonObject &jo, const std::string &tag, int def_val,
int def_valmax )
: val( def_val )
, valmax( def_valmax )
{
if( jo.has_array( tag ) ) {
JsonArray sparray = jo.get_array( tag );
if( sparray.size() > 2 ) {
jo.throw_error( "invalid data: must be an array of 1 or 2 values", tag );
}
if( sparray.size() >= 1 ) {
val = sparray.get_int( 0 );
}
if( sparray.size() >= 2 ) {
valmax = sparray.get_int( 1 );
}
} else if( jo.has_member( tag ) ) {
val = valmax = jo.get_int( tag );
}
}
int jmapgen_int::get() const
{
return val == valmax ? val : rng( val, valmax );
}
/*
* Turn json gobbldigook into machine friendly gobbldigook, for applying
* basic map 'set' functions, optionally based on one_in(chance) or repeat value
*/
void mapgen_function_json_base::setup_setmap( const JsonArray &parray )
{
std::string tmpval;
std::map<std::string, jmapgen_setmap_op> setmap_opmap;
setmap_opmap[ "terrain" ] = JMAPGEN_SETMAP_TER;
setmap_opmap[ "furniture" ] = JMAPGEN_SETMAP_FURN;
setmap_opmap[ "trap" ] = JMAPGEN_SETMAP_TRAP;
setmap_opmap[ "radiation" ] = JMAPGEN_SETMAP_RADIATION;
setmap_opmap[ "bash" ] = JMAPGEN_SETMAP_BASH;
std::map<std::string, jmapgen_setmap_op>::iterator sm_it;
jmapgen_setmap_op tmpop;
int setmap_optype = 0;
for( const JsonObject pjo : parray ) {
if( pjo.read( "point", tmpval ) ) {
setmap_optype = JMAPGEN_SETMAP_OPTYPE_POINT;
} else if( pjo.read( "set", tmpval ) ) {
setmap_optype = JMAPGEN_SETMAP_OPTYPE_POINT;
debugmsg( "Warning, set: [ { \"set\": … } is deprecated, use set: [ { \"point\": … " );
} else if( pjo.read( "line", tmpval ) ) {
setmap_optype = JMAPGEN_SETMAP_OPTYPE_LINE;
} else if( pjo.read( "square", tmpval ) ) {
setmap_optype = JMAPGEN_SETMAP_OPTYPE_SQUARE;
} else {
pjo.throw_error( R"(invalid data: must contain "point", "set", "line" or "square" member)" );
}
sm_it = setmap_opmap.find( tmpval );
if( sm_it == setmap_opmap.end() ) {
pjo.throw_error( string_format( "invalid subfunction %s", tmpval.c_str() ) );
}
tmpop = sm_it->second;
jmapgen_int tmp_x2( 0, 0 );
jmapgen_int tmp_y2( 0, 0 );
jmapgen_int tmp_i( 0, 0 );
int tmp_chance = 1;
int tmp_rotation = 0;
int tmp_fuel = -1;
int tmp_status = -1;
const jmapgen_int tmp_x( pjo, "x" );
const jmapgen_int tmp_y( pjo, "y" );
if( !check_inbounds( tmp_x, tmp_y, pjo ) ) {
pjo.allow_omitted_members();
continue;
}
if( setmap_optype != JMAPGEN_SETMAP_OPTYPE_POINT ) {
tmp_x2 = jmapgen_int( pjo, "x2" );
tmp_y2 = jmapgen_int( pjo, "y2" );
if( !check_inbounds( tmp_x2, tmp_y2, pjo ) ) {
pjo.allow_omitted_members();
continue;
}
}
if( tmpop == JMAPGEN_SETMAP_RADIATION ) {
tmp_i = jmapgen_int( pjo, "amount" );
} else if( tmpop == JMAPGEN_SETMAP_BASH ) {
//suppress warning
} else {
std::string tmpid = pjo.get_string( "id" );
switch( tmpop ) {
case JMAPGEN_SETMAP_TER: {
const ter_str_id tid( tmpid );
if( !tid.is_valid() ) {
set_mapgen_defer( pjo, "id", "no such terrain" );
return;
}
tmp_i.val = tid.id().to_i();
}
break;
case JMAPGEN_SETMAP_FURN: {
const furn_str_id fid( tmpid );
if( !fid.is_valid() ) {
set_mapgen_defer( pjo, "id", "no such furniture" );
return;
}
tmp_i.val = fid.id().to_i();
}
break;
case JMAPGEN_SETMAP_TRAP: {
const trap_str_id sid( tmpid );
if( !sid.is_valid() ) {
set_mapgen_defer( pjo, "id", "no such trap" );
return;
}
tmp_i.val = sid.id().to_i();
}
break;
default:
//Suppress warnings
break;
}
// TODO: ... support for random furniture? or not.
tmp_i.valmax = tmp_i.val;
}
// TODO: sanity check?
const jmapgen_int tmp_repeat = jmapgen_int( pjo, "repeat", 1, 1 );
pjo.read( "chance", tmp_chance );
pjo.read( "rotation", tmp_rotation );
pjo.read( "fuel", tmp_fuel );
pjo.read( "status", tmp_status );
jmapgen_setmap tmp( tmp_x, tmp_y, tmp_x2, tmp_y2,
static_cast<jmapgen_setmap_op>( tmpop + setmap_optype ), tmp_i,
tmp_chance, tmp_repeat, tmp_rotation, tmp_fuel, tmp_status );
setmap_points.push_back( tmp );
tmpval.clear();
}
}
jmapgen_place::jmapgen_place( const JsonObject &jsi )
: x( jsi, "x" )
, y( jsi, "y" )
, repeat( jsi, "repeat", 1, 1 )
{
}
void jmapgen_place::offset( point offset )
{
x.val -= offset.x;
x.valmax -= offset.x;
y.val -= offset.y;
y.valmax -= offset.y;
}
map_key::map_key( const std::string &s ) : str( s )
{
if( utf8_width( str ) != 1 ) {
debugmsg( "map key '%s' must be 1 column", str );
}
}
map_key::map_key( const JsonMember &member ) : str( member.name() )
{
if( utf8_width( str ) != 1 ) {
member.throw_error( "format map key must be 1 column" );
}
}
/**
* This is a generic mapgen piece, the template parameter PieceType should be another specific
* type of jmapgen_piece. This class contains a vector of those objects and will chose one of
* it at random.
*/
template<typename PieceType>
class jmapgen_alternativly : public jmapgen_piece
{
public:
// Note: this bypasses virtual function system, all items in this vector are of type
// PieceType, they *can not* be of any other type.
std::vector<PieceType> alternatives;
jmapgen_alternativly() = default;
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
if( const auto chosen = random_entry_opt( alternatives ) ) {
chosen->get().apply( dat, x, y );
}
}
bool has_vehicle_collision( mapgendata &dat, point p ) const override {
return dat.m.veh_at( tripoint( p, dat.zlevel() ) ).has_value();
}
};
/**
* Places fields on the map.
* "field": field type ident.
* "intensity": initial field intensity.
* "age": initial field age.
*/
class jmapgen_field : public jmapgen_piece
{
public:
field_type_id ftype;
int intensity;
time_duration age;
jmapgen_field( const JsonObject &jsi ) :
ftype( field_type_id( jsi.get_string( "field" ) ) )
, intensity( jsi.get_int( "intensity", 1 ) )
, age( time_duration::from_turns( jsi.get_int( "age", 0 ) ) ) {
if( !ftype.id() ) {
set_mapgen_defer( jsi, "field", "invalid field type" );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
dat.m.add_field( tripoint( x.get(), y.get(), dat.m.get_abs_sub().z ), ftype, intensity, age );
}
};
/**
* Place an NPC.
* "class": the npc class, see @ref map::place_npc
*/
class jmapgen_npc : public jmapgen_piece
{
public:
string_id<npc_template> npc_class;
bool target;
std::vector<std::string> traits;
jmapgen_npc( const JsonObject &jsi ) :
npc_class( jsi.get_string( "class" ) )
, target( jsi.get_bool( "target", false ) ) {
if( !npc_class.is_valid() ) {
set_mapgen_defer( jsi, "class", "unknown npc class" );
}
if( jsi.has_string( "add_trait" ) ) {
std::string new_trait = jsi.get_string( "add_trait" );
traits.emplace_back( new_trait );
} else if( jsi.has_array( "add_trait" ) ) {
for( const std::string new_trait : jsi.get_array( "add_trait" ) ) {
traits.emplace_back( new_trait );
}
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
character_id npc_id = dat.m.place_npc( point( x.get(), y.get() ), npc_class );
if( dat.mission() && target ) {
dat.mission()->set_target_npc_id( npc_id );
}
npc *p = g->find_npc( npc_id );
if( p != nullptr ) {
for( const std::string &new_trait : traits ) {
p->set_mutation( trait_id( new_trait ) );
}
}
}
};
/**
* Place ownership area
*/
class jmapgen_faction : public jmapgen_piece
{
public:
faction_id id;
jmapgen_faction( const JsonObject &jsi ) {
if( jsi.has_string( "id" ) ) {
id = faction_id( jsi.get_string( "id" ) );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
dat.m.apply_faction_ownership( point( x.val, y.val ), point( x.valmax, y.valmax ), id );
}
};
/**
* Place a sign with some text.
* "signage": the text on the sign.
*/
class jmapgen_sign : public jmapgen_piece
{
public:
std::string signage;
std::string snippet;
jmapgen_sign( const JsonObject &jsi ) :
signage( jsi.get_string( "signage", "" ) )
, snippet( jsi.get_string( "snippet", "" ) ) {
if( signage.empty() && snippet.empty() ) {
jsi.throw_error( "jmapgen_sign: needs either signage or snippet" );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
const point r{ x.get(), y.get() };
dat.m.furn_set( r, f_null );
dat.m.furn_set( r, furn_str_id( "f_sign" ) );
std::string signtext;
if( !snippet.empty() ) {
// select a snippet from the category
signtext = SNIPPET.random_from_category( snippet ).value_or( translation() ).translated();
} else if( !signage.empty() ) {
signtext = signage;
}
if( !signtext.empty() ) {
// replace tags
signtext = _( signtext );
std::string cityname = "illegible city name";
tripoint abs_sub = dat.m.get_abs_sub();
// TODO: fix point types
const city *c = overmap_buffer.closest_city( tripoint_abs_sm( abs_sub ) ).city;
if( c != nullptr ) {
cityname = c->name;
}
signtext = apply_all_tags( signtext, cityname );
}
dat.m.set_signage( tripoint( r, dat.m.get_abs_sub().z ), signtext );
}
std::string apply_all_tags( std::string signtext, const std::string &cityname ) const {
replace_city_tag( signtext, cityname );
replace_name_tags( signtext );
return signtext;
}
bool has_vehicle_collision( mapgendata &dat, point p ) const override {
return dat.m.veh_at( tripoint( p, dat.zlevel() ) ).has_value();
}
};
/**
* Place graffiti with some text or a snippet.
* "text": the text of the graffiti.
* "snippet": snippet category to pull from for text instead.
*/
class jmapgen_graffiti : public jmapgen_piece
{
public:
std::string text;
std::string snippet;
jmapgen_graffiti( const JsonObject &jsi ) :
text( jsi.get_string( "text", "" ) )
, snippet( jsi.get_string( "snippet", "" ) ) {
if( text.empty() && snippet.empty() ) {
jsi.throw_error( "jmapgen_graffiti: needs either text or snippet" );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
const point r{ x.get(), y.get() };
std::string graffiti;
if( !snippet.empty() ) {