-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
mapgen.cpp
7896 lines (7432 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 <assert.h>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <memory>
#include <sstream>
#include <array>
#include <functional>
#include <iterator>
#include <set>
#include <stdexcept>
#include <unordered_map>
#include <cmath>
#include "clzones.h"
#include "computer.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "faction.h"
#include "game.h"
#include "item_group.h"
#include "itype.h"
#include "json.h"
#include "line.h"
#include "mapgendata.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 "mapgenformat.h"
#include "mission.h"
#include "mongroup.h"
#include "npc.h"
#include "omdata.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "rng.h"
#include "string_formatter.h"
#include "submap.h"
#include "text_snippets.h"
#include "translations.h"
#include "trap.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "calendar.h"
#include "common_types.h"
#include "field.h"
#include "game_constants.h"
#include "item.h"
#include "string_id.h"
#include "tileray.h"
#include "weighted_list.h"
#include "material.h"
#include "int_id.h"
#include "colony.h"
#include "pimpl.h"
#include "point.h"
#define dbg(x) DebugLog((x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
#define MON_RADIUS 3
static const mongroup_id GROUP_DARK_WYRM( "GROUP_DARK_WYRM" );
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_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_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_LAB_FEMA( "GROUP_LAB_FEMA" );
static const mongroup_id GROUP_MIL_WEAK( "GROUP_MIL_WEAK" );
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_HEART( "GROUP_TRIFFID_HEART" );
static const mongroup_id GROUP_TRIFFID( "GROUP_TRIFFID" );
static const mongroup_id GROUP_TRIFFID_OUTER( "GROUP_TRIFFID_OUTER" );
static const mongroup_id GROUP_TURRET( "GROUP_TURRET" );
static const mongroup_id GROUP_VANILLA( "GROUP_VANILLA" );
static const mongroup_id GROUP_ZOMBIE( "GROUP_ZOMBIE" );
static const mongroup_id GROUP_ZOMBIE_COP( "GROUP_ZOMBIE_COP" );
void science_room( map *m, int x1, int y1, int x2, int y2, int z, int rotate );
void set_science_room( map *m, int x1, int y1, bool faces_right, const time_point &when );
void build_mine_room( room_type type, int x1, int y1, int x2, int y2, mapgendata &dat );
// (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( D_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
tripoint 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 auto p = random_point( *this, [this]( const tripoint & n ) {
return passable( n );
} ) ) {
add_spawn( spawn_details.name, spawn_details.pack_size, p->xy() );
}
}
}
// 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( D_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
/*
* ptr storage.
*/
std::map<std::string, std::vector<std::shared_ptr<mapgen_function>> > oter_mapgen;
std::map<std::string, std::vector<std::unique_ptr<mapgen_function_json_nested>> > nested_mapgen;
std::map<std::string, std::vector<std::unique_ptr<update_mapgen_function_json>> > update_mapgen;
/*
* index to the above, adjusted to allow for rarity
*/
std::map<std::string, std::map<int, int> > oter_mapgen_weights;
/*
* setup oter_mapgen_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_weights.clear();
for( auto &omw : oter_mapgen ) {
int funcnum = 0;
int wtotal = 0;
oter_mapgen_weights[ omw.first ] = std::map<int, int>();
for( auto fit = omw.second.begin(); fit != omw.second.end(); ++fit ) {
//
int weight = ( *fit )->weight;
if( weight < 1 ) {
dbg( D_INFO ) << "wcalc " << omw.first << "(" << funcnum << "): (rej(1), " << weight << ") = " <<
wtotal;
++funcnum;
continue; // rejected!
}
( *fit )->setup();
wtotal += weight;
oter_mapgen_weights[ omw.first ][ wtotal ] = funcnum;
dbg( D_INFO ) << "wcalc " << omw.first << "(" << funcnum << "): +" << weight << " = " << wtotal;
++funcnum;
}
}
// Not really calculate weights, but let's keep it here for now
for( auto &pr : nested_mapgen ) {
for( std::unique_ptr<mapgen_function_json_nested> &ptr : pr.second ) {
ptr->setup();
}
}
for( auto &pr : update_mapgen ) {
for( auto &ptr : pr.second ) {
ptr->setup();
}
}
}
void check_mapgen_definitions()
{
for( auto &oter_definition : oter_mapgen ) {
for( auto &mapgen_function_ptr : oter_definition.second ) {
mapgen_function_ptr->check( oter_definition.first );
}
}
for( auto &oter_definition : nested_mapgen ) {
for( auto &mapgen_function_ptr : oter_definition.second ) {
mapgen_function_ptr->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, const std::string &id_base,
int default_idx, const point &offset )
{
int mgweight = jio.get_int( "weight", 1000 );
std::shared_ptr<mapgen_function> ret;
if( mgweight <= 0 || jio.get_bool( "disabled", false ) ) {
const std::string mgtype = jio.get_string( "method" );
if( default_idx != -1 && mgtype == "builtin" ) {
if( jio.has_string( "name" ) ) {
const std::string mgname = jio.get_string( "name" );
if( mgname == id_base ) {
oter_mapgen[id_base][ default_idx ]->weight = 0;
}
}
}
jio.allow_omitted_members();
return nullptr; // nothing
} else if( jio.has_string( "method" ) ) {
const std::string mgtype = jio.get_string( "method" );
if( mgtype == "builtin" ) { // c-function
if( jio.has_string( "name" ) ) {
const std::string mgname = jio.get_string( "name" );
if( const auto ptr = get_mapgen_cfunction( mgname ) ) {
ret = std::make_shared<mapgen_function_builtin>( ptr, mgweight );
oter_mapgen[id_base].push_back( ret );
} else {
debugmsg( "oter_t[%s]: builtin mapgen function \"%s\" does not exist.", id_base.c_str(),
mgname );
}
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function (missing \"name\" value).", id_base.c_str() );
}
} else if( mgtype == "json" ) {
if( jio.has_object( "object" ) ) {
JsonObject jo = jio.get_object( "object" );
std::string jstr = jo.str();
ret = std::make_shared<mapgen_function_json>( jstr, mgweight, offset );
oter_mapgen[id_base].push_back( ret );
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function (missing \"object\" object)", id_base.c_str() );
}
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function type: %s", id_base.c_str(), mgtype.c_str() );
}
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function (missing \"method\" value, must be \"builtin\" or \"json\").",
id_base.c_str() );
}
return ret;
}
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" ) ) {
JsonObject jo = jio.get_object( "object" );
std::string jstr = jo.str();
nested_mapgen[id_base].push_back(
std::make_unique<mapgen_function_json_nested>( jstr ) );
} 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" );
std::string jstr = jo.str();
update_mapgen[id_base].push_back(
std::make_unique<update_mapgen_function_json>( jstr ) );
} 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 )
{
if( jo.has_array( "om_terrain" ) ) {
JsonArray ja = jo.get_array( "om_terrain" );
if( ja.test_array() ) {
point offset;
for( JsonArray row_items : ja ) {
for( const std::string &mapgenid : row_items ) {
const auto mgfunc = load_mapgen_function( jo, mapgenid, -1, offset );
if( mgfunc ) {
oter_mapgen[ mapgenid ].push_back( mgfunc );
}
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, -1 );
if( mgfunc ) {
for( auto &i : mapgenid_list ) {
oter_mapgen[ i ].push_back( mgfunc );
}
}
}
}
} else if( jo.has_string( "om_terrain" ) ) {
load_mapgen_function( jo, jo.get_string( "om_terrain" ), -1 );
} 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.clear();
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( 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 point &mapgensize, const JsonObject &jso )
{
rectangle bounds( point_zero, mapgensize );
if( !bounds.contains_half_open( 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 std::string &s )
: jdata( s )
, do_format( false )
, is_ready( false )
, mapgensize( SEEX * 2, SEEY * 2 )
, objects( m_offset, mapgensize )
{
}
mapgen_function_json_base::~mapgen_function_json_base() = default;
mapgen_function_json::mapgen_function_json( const std::string &s, const int w,
const point &grid_offset )
: mapgen_function( w )
, mapgen_function_json_base( s )
, fill_ter( t_null )
, rotation( 0 )
{
m_offset.x = grid_offset.x * mapgensize.x;
m_offset.y = grid_offset.y * mapgensize.y;
objects = jmapgen_objects( m_offset, mapgensize );
}
mapgen_function_json_nested::mapgen_function_json_nested( const std::string &s )
: mapgen_function_json_base( s )
, 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, const short def_val,
const short 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 ) ) {
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();
}
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();
}
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( const point &offset )
{
x.val -= offset.x;
x.valmax -= offset.x;
y.val -= offset.y;
y.valmax -= offset.y;
}
/**
* 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, int x, int y ) const override {
return dat.m.veh_at( tripoint( x, y, 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 int rx = x.get();
const int ry = y.get();
dat.m.furn_set( point( rx, ry ), f_null );
dat.m.furn_set( point( rx, ry ), 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();
const city *c = overmap_buffer.closest_city( abs_sub ).city;
if( c != nullptr ) {
cityname = c->name;
}
signtext = apply_all_tags( signtext, cityname );
}
dat.m.set_signage( tripoint( rx, ry, 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, int x, int y ) const override {
return dat.m.veh_at( tripoint( x, y, 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 int rx = x.get();
const int ry = y.get();
std::string graffiti;
if( !snippet.empty() ) {
// select a snippet from the category
graffiti = SNIPPET.random_from_category( snippet ).value_or( translation() ).translated();
} else if( !text.empty() ) {
graffiti = text;
}
if( !graffiti.empty() ) {
// replace tags
graffiti = _( graffiti );
std::string cityname = "illegible city name";
tripoint abs_sub = dat.m.get_abs_sub();
const city *c = overmap_buffer.closest_city( abs_sub ).city;
if( c != nullptr ) {
cityname = c->name;
}
graffiti = apply_all_tags( graffiti, cityname );
}
dat.m.set_graffiti( tripoint( rx, ry, dat.m.get_abs_sub().z ), graffiti );
}
std::string apply_all_tags( std::string graffiti, const std::string &cityname ) const {
replace_city_tag( graffiti, cityname );
replace_name_tags( graffiti );
return graffiti;
}
};
/**
* Place a vending machine with content.
* "item_group": the item group that is used to generate the content of the vending machine.
*/
class jmapgen_vending_machine : public jmapgen_piece
{
public:
bool reinforced;
std::string item_group_id;
jmapgen_vending_machine( const JsonObject &jsi ) :
reinforced( jsi.get_bool( "reinforced", false ) )
, item_group_id( jsi.get_string( "item_group", "default_vending_machine" ) ) {
if( !item_group::group_is_defined( item_group_id ) ) {
set_mapgen_defer( jsi, "item_group", "no such item group" );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
const int rx = x.get();
const int ry = y.get();
dat.m.furn_set( point( rx, ry ), f_null );
dat.m.place_vending( point( rx, ry ), item_group_id, reinforced );
}
bool has_vehicle_collision( mapgendata &dat, int x, int y ) const override {
return dat.m.veh_at( tripoint( x, y, dat.zlevel() ) ).has_value();
}
};
/**
* Place a toilet with (dirty) water in it.
* "amount": number of water charges to place.
*/
class jmapgen_toilet : public jmapgen_piece
{
public:
jmapgen_int amount;
jmapgen_toilet( const JsonObject &jsi ) :
amount( jsi, "amount", 0, 0 ) {
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
const int rx = x.get();
const int ry = y.get();
const int charges = amount.get();
dat.m.furn_set( point( rx, ry ), f_null );
if( charges == 0 ) {
dat.m.place_toilet( point( rx, ry ) ); // Use the default charges supplied as default values
} else {
dat.m.place_toilet( point( rx, ry ), charges );
}
}
bool has_vehicle_collision( mapgendata &dat, int x, int y ) const override {
return dat.m.veh_at( tripoint( x, y, dat.zlevel() ) ).has_value();
}
};
/**
* Place a gas pump with fuel in it.
* "amount": number of fuel charges to place.
*/
class jmapgen_gaspump : public jmapgen_piece
{
public:
jmapgen_int amount;
std::string fuel;
jmapgen_gaspump( const JsonObject &jsi ) :
amount( jsi, "amount", 0, 0 ) {
if( jsi.has_string( "fuel" ) ) {
fuel = jsi.get_string( "fuel" );
// may want to not force this, if we want to support other fuels for some reason
if( fuel != "gasoline" && fuel != "diesel" ) {
jsi.throw_error( "invalid fuel", "fuel" );
}
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
const int rx = x.get();
const int ry = y.get();
int charges = amount.get();
dat.m.furn_set( point( rx, ry ), f_null );
if( charges == 0 ) {
charges = rng( 10000, 50000 );
}
if( !fuel.empty() ) {
dat.m.place_gas_pump( point( rx, ry ), charges, fuel );
} else {
dat.m.place_gas_pump( point( rx, ry ), charges );
}
}
bool has_vehicle_collision( mapgendata &dat, int x, int y ) const override {
return dat.m.veh_at( tripoint( x, y, dat.zlevel() ) ).has_value();
}