/
mapgen.cpp
8313 lines (7833 loc) · 370 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 <cassert>
#include <chrono>
#include <list>
#include <random>
#include <sstream>
#include "ammo.h"
#include "catalua.h"
#include "computer.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "game.h"
#include "item_group.h"
#include "json.h"
#include "line.h"
#include "map.h"
#include "map_extras.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "mapgen_functions.h"
#include "mapgenformat.h"
#include "mongroup.h"
#include "mtype.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 "vehicle_group.h"
#include "vpart_position.h"
#include "vpart_range.h"
#define dbg(x) DebugLog((DebugLevel)(x),D_MAP_GEN) << __FILE__ << ":" << __LINE__ << ": "
#define MON_RADIUS 3
const mtype_id mon_biollante( "mon_biollante" );
const mtype_id mon_blank( "mon_blank" );
const mtype_id mon_blob( "mon_blob" );
const mtype_id mon_boomer( "mon_boomer" );
const mtype_id mon_breather( "mon_breather" );
const mtype_id mon_breather_hub( "mon_breather_hub" );
const mtype_id mon_broken_cyborg( "mon_broken_cyborg" );
const mtype_id mon_chickenbot( "mon_chickenbot" );
const mtype_id mon_crawler( "mon_crawler" );
const mtype_id mon_creeper_hub( "mon_creeper_hub" );
const mtype_id mon_dark_wyrm( "mon_dark_wyrm" );
const mtype_id mon_dog_thing( "mon_dog_thing" );
const mtype_id mon_eyebot( "mon_eyebot" );
const mtype_id mon_flaming_eye( "mon_flaming_eye" );
const mtype_id mon_flying_polyp( "mon_flying_polyp" );
const mtype_id mon_fungaloid( "mon_fungaloid" );
const mtype_id mon_fungal_fighter( "mon_fungal_fighter" );
const mtype_id mon_gelatin( "mon_gelatin" );
const mtype_id mon_gozu( "mon_gozu" );
const mtype_id mon_gracke( "mon_gracke" );
const mtype_id mon_hazmatbot( "mon_hazmatbot" );
const mtype_id mon_hunting_horror( "mon_hunting_horror" );
const mtype_id mon_kreck( "mon_kreck" );
const mtype_id mon_mi_go( "mon_mi_go" );
const mtype_id mon_secubot( "mon_secubot" );
const mtype_id mon_sewer_snake( "mon_sewer_snake" );
const mtype_id mon_shoggoth( "mon_shoggoth" );
const mtype_id mon_spider_web( "mon_spider_web" );
const mtype_id mon_tankbot( "mon_tankbot" );
const mtype_id mon_triffid( "mon_triffid" );
const mtype_id mon_triffid_heart( "mon_triffid_heart" );
const mtype_id mon_turret( "mon_turret" );
const mtype_id mon_turret_bmg( "mon_turret_bmg" );
const mtype_id mon_turret_rifle( "mon_turret_rifle" );
const mtype_id mon_turret_searchlight( "mon_turret_searchlight" );
const mtype_id mon_yugg( "mon_yugg" );
const mtype_id mon_zombie( "mon_zombie" );
const mtype_id mon_zombie_bio_op( "mon_zombie_bio_op" );
const mtype_id mon_zombie_brute( "mon_zombie_brute" );
const mtype_id mon_zombie_child( "mon_zombie_child" );
const mtype_id mon_zombie_cop( "mon_zombie_cop" );
const mtype_id mon_zombie_dog( "mon_zombie_dog" );
const mtype_id mon_zombie_electric( "mon_zombie_electric" );
const mtype_id mon_zombie_flamer( "mon_zombie_flamer" );
const mtype_id mon_zombie_grabber( "mon_zombie_grabber" );
const mtype_id mon_zombie_grenadier( "mon_zombie_grenadier" );
const mtype_id mon_zombie_grenadier_elite( "mon_zombie_grenadier_elite" );
const mtype_id mon_zombie_scientist( "mon_zombie_scientist" );
const mtype_id mon_zombie_shrieker( "mon_zombie_shrieker" );
const mtype_id mon_zombie_smoker( "mon_zombie_smoker" );
const mtype_id mon_zombie_soldier( "mon_zombie_soldier" );
const mtype_id mon_zombie_spitter( "mon_zombie_spitter" );
const mtype_id mon_zombie_tough( "mon_zombie_tough" );
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 silo_rooms( map *m );
void build_mine_room( map *m, 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 int x, const int y, const int z, const time_point &when )
{
dbg( D_INFO ) << "map::generate( g[" << g.get() << "], x[" << x << "], "
<< "y[" << y << "], z[" << z << "], when[" << to_string( when ) << "] )";
set_abs_sub( x, y, z );
// 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++ ) {
setsubmap( get_nonant( { gridx, gridy } ), 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
int overx = x;
int overy = y;
sm_to_omt( overx, overy );
const regional_settings *rsettings = &overmap_buffer.get_settings( overx, overy, z );
oter_id terrain_type = overmap_buffer.ter( overx, overy, z );
oter_id t_above = overmap_buffer.ter( overx, overy, z + 1 );
oter_id t_below = overmap_buffer.ter( overx, overy, z - 1 );
oter_id t_north = overmap_buffer.ter( overx, overy - 1, z );
oter_id t_neast = overmap_buffer.ter( overx + 1, overy - 1, z );
oter_id t_east = overmap_buffer.ter( overx + 1, overy, z );
oter_id t_seast = overmap_buffer.ter( overx + 1, overy + 1, z );
oter_id t_south = overmap_buffer.ter( overx, overy + 1, z );
oter_id t_swest = overmap_buffer.ter( overx - 1, overy + 1, z );
oter_id t_west = overmap_buffer.ter( overx - 1, overy, z );
oter_id t_nwest = overmap_buffer.ter( overx - 1, overy - 1, z );
// 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 = overx - MON_RADIUS; i <= overx + MON_RADIUS; i++ ) {
for( int j = overy - MON_RADIUS; j <= overy + MON_RADIUS; j++ ) {
density += overmap_buffer.ter( i, j, z )->get_mondensity();
}
}
density = density / 100;
draw_map( terrain_type, t_north, t_east, t_south, t_west, t_neast, t_seast, t_swest, t_nwest,
t_above, t_below, when, density, z, rsettings );
// 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().c_str() );
} else {
auto func = MapExtras::get_function( *( ex.values.pick() ) );
if( func != nullptr ) {
func( *this, abs_sub );
}
}
}
const auto &spawns = terrain_type->get_static_spawns();
if( spawns.group && x_in_y( spawns.chance, 100 ) ) {
int pop = 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->x, p->y );
}
}
}
// 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 << ")";
if( i <= 1 && j <= 1 ) {
saven( i, j, z );
} else {
delete get_submap_at_grid( { i, j, z } );
}
}
}
}
void mapgen_function_builtin::generate( map *m, const oter_id &terrain_type, const mapgendata &mgd,
const time_point &t, float d )
{
( *fptr )( m, terrain_type, mgd, t, d );
const std::string mapgen_generator_type = "builtin";
const tripoint terrain_tripoint = sm_to_omt_copy( m->get_abs_sub() );
CallbackArgumentContainer lua_callback_args_info;
lua_callback_args_info.emplace_back( mapgen_generator_type );
lua_callback_args_info.emplace_back( terrain_type.id().str() );
lua_callback_args_info.emplace_back( terrain_tripoint );
lua_callback( "on_mapgen_finished", lua_callback_args_info );
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
///// 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::shared_ptr<mapgen_function_json_nested>> > nested_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( auto &ptr : pr.second ) {
ptr->setup();
}
}
}
/////////////////////////////////////////////////////////////////////////////////
///// json mapgen functions
///// 1 - init():
/*
* 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( JsonObject &jio, const std::string &id_base,
int default_idx, const int x_offset, const int y_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;
}
}
}
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.c_str() );
}
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function (missing \"name\" value).", id_base.c_str() );
}
} else if( mgtype == "lua" ) { // lua script
if( jio.has_string( "script" ) ) { // minified into one\nline
const std::string mgscript = jio.get_string( "script" );
ret = std::make_shared<mapgen_function_lua>( mgscript, mgweight );
oter_mapgen[id_base].push_back( ret );
} else if( jio.has_array( "script" ) ) { // or 1 line per entry array
std::string mgscript;
JsonArray jascr = jio.get_array( "script" );
while( jascr.has_more() ) {
mgscript += jascr.next_string();
mgscript += "\n";
}
ret = std::make_shared<mapgen_function_lua>( mgscript, mgweight );
oter_mapgen[id_base].push_back( ret );
// @todo: pass dirname current.json, because the latter two are icky
// } else if ( jio.has_string("file" ) { // or "same-dir-as-this/json/something.lua
} else {
debugmsg( "oter_t[%s]: Invalid mapgen function (missing \"script\" or \"file\" value).",
id_base.c_str() );
}
#ifndef LUA
dbg( D_ERROR ) << "oter_t " << id_base << ": mapgen entry requires a build with LUA=1.";
#endif
} 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, x_offset, y_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\", \"lua\", or \"json\").",
id_base.c_str() );
}
return ret;
}
std::shared_ptr<mapgen_function_json_nested> load_nested_mapgen( JsonObject &jio,
const std::string &id_base )
{
std::shared_ptr<mapgen_function_json_nested> ret;
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();
ret = std::make_shared<mapgen_function_json_nested>( jstr );
nested_mapgen[id_base].push_back( ret );
} 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() );
}
return ret;
}
/*
* feed bits `o json from standalone file to load_mapgen_function. (standalone json "type": "mapgen")
*/
void load_mapgen( JsonObject &jo )
{
if( jo.has_array( "om_terrain" ) ) {
JsonArray ja = jo.get_array( "om_terrain" );
if( ja.test_array() ) {
int x_offset = 0;
int y_offset = 0;
while( ja.has_more() ) {
JsonArray row_items = ja.next_array();
while( row_items.has_more() ) {
const std::string mapgenid = row_items.next_string();
const auto mgfunc = load_mapgen_function( jo, mapgenid, -1, x_offset, y_offset );
if( mgfunc ) {
oter_mapgen[ mapgenid ].push_back( mgfunc );
}
x_offset++;
}
y_offset++;
x_offset = 0;
}
} else {
std::vector<std::string> mapgenid_list;
while( ja.has_more() ) {
mapgenid_list.push_back( ja.next_string() );
}
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 {
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().c_str() );
}
}
void reset_mapgens()
{
oter_mapgen.clear();
nested_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 size_t x, const size_t y ) const
{
if( x >= mapgensize_x ) {
debugmsg( "invalid value %lu for x in calc_index", static_cast<unsigned long>( x ) );
}
if( y >= mapgensize_y ) {
debugmsg( "invalid value %lu for y in calc_index", static_cast<unsigned long>( y ) );
}
return y * mapgensize_y + x;
}
bool mapgen_function_json_base::check_inbounds( const jmapgen_int &x, const jmapgen_int &y ) const
{
const int min = 0;
const int max_x = mapgensize_x - 1;
const int max_y = mapgensize_y - 1;
if( x.val < min || x.val > max_x || x.valmax < min || x.valmax > max_x ||
y.val < min || y.val > max_y || y.valmax < min || y.valmax > max_y ) {
return false;
}
return true;
}
mapgen_function_json_base::mapgen_function_json_base( const std::string &s )
: jdata( std::move( s ) )
, do_format( false )
, is_ready( false )
, mapgensize_x( SEEX * 2 )
, mapgensize_y( SEEY * 2 )
, x_offset( 0 )
, y_offset( 0 )
, objects( 0, 0, mapgensize_x, mapgensize_y )
{
}
mapgen_function_json_base::~mapgen_function_json_base() = default;
mapgen_function_json::mapgen_function_json( const std::string &s, const int w,
const int x_grid_offset, const int y_grid_offset )
: mapgen_function( w )
, mapgen_function_json_base( s )
, fill_ter( t_null )
, rotation( 0 )
{
x_offset = x_grid_offset * mapgensize_x;
y_offset = y_grid_offset * mapgensize_y;
objects = jmapgen_objects( x_offset, y_offset, mapgensize_x, mapgensize_y );
}
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( 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 {
val = valmax = jo.get_int( tag );
}
}
jmapgen_int::jmapgen_int( 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( 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;
const auto inboundchk = [this]( const jmapgen_int & x, const jmapgen_int & y, JsonObject & jo ) {
if( !check_inbounds( x, y ) ) {
jo.throw_error( string_format( "Point must be between [0,0] and [%d,%d]", mapgensize_x,
mapgensize_y ) );
}
};
while( parray.has_more() ) {
JsonObject pjo = parray.next_object();
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( "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" );
inboundchk( tmp_x, tmp_y, pjo );
if( setmap_optype != JMAPGEN_SETMAP_OPTYPE_POINT ) {
tmp_x2 = jmapgen_int( pjo, "x2" );
tmp_y2 = jmapgen_int( pjo, "y2" );
inboundchk( tmp_x2, tmp_y2, pjo );
}
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() ) {
pjo.throw_error( "no such terrain", "id" );
}
tmp_i.val = tid.id();
}
break;
case JMAPGEN_SETMAP_FURN: {
const furn_str_id fid( tmpid );
if( !fid.is_valid() ) {
pjo.throw_error( "no such furniture", "id" );
}
tmp_i.val = fid.id();
}
break;
case JMAPGEN_SETMAP_TRAP: {
const trap_str_id sid( tmpid );
if( !sid.is_valid() ) {
pjo.throw_error( "no such trap", "id" );
}
tmp_i.val = sid.id().to_i();
}
break;
default:
//Suppress warnings
break;
}
tmp_i.valmax = tmp_i.val; // @todo: ... support for random furniture? or not.
}
const jmapgen_int tmp_repeat = jmapgen_int( pjo, "repeat", 1, 1 ); // @todo: sanity check?
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, 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( JsonObject &jsi )
: x( jsi, "x" )
, y( jsi, "y" )
, repeat( jsi, "repeat", 1, 1 )
{
}
void jmapgen_place::offset( const int x_offset, const int y_offset )
{
x.val -= x_offset;
x.valmax -= x_offset;
y.val -= y_offset;
y.valmax -= y_offset;
}
/**
* 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( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float mon_density ) const override {
if( const auto chosen = random_entry_opt( alternatives ) ) {
chosen->get().apply( dat, x, y, mon_density );
}
}
};
/**
* Places fields on the map.
* "field": field type ident.
* "density": initial field density.
* "age": initial field age.
*/
class jmapgen_field : public jmapgen_piece
{
public:
field_id ftype;
int density;
time_duration age;
jmapgen_field( JsonObject &jsi ) : jmapgen_piece()
, ftype( field_from_ident( jsi.get_string( "field" ) ) )
, density( jsi.get_int( "density", 1 ) )
, age( time_duration::from_turns( jsi.get_int( "age", 0 ) ) ) {
if( ftype == fd_null ) {
jsi.throw_error( "invalid field type", "field" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
dat.m.add_field( tripoint( x.get(), y.get(), dat.m.get_abs_sub().z ), ftype, density, 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;
jmapgen_npc( JsonObject &jsi ) : jmapgen_piece()
, npc_class( jsi.get_string( "class" ) ) {
if( !npc_class.is_valid() ) {
jsi.throw_error( "unknown npc class", "class" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
dat.m.place_npc( x.get(), y.get(), npc_class );
}
};
/**
* 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( JsonObject &jsi ) : jmapgen_piece()
, 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( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
const int rx = x.get();
const int ry = y.get();
dat.m.furn_set( rx, ry, f_null );
dat.m.furn_set( rx, ry, furn_str_id( "f_sign" ) );
std::string signtext;
if( !snippet.empty() ) {
// select a snippet from the category
signtext = SNIPPET.get( SNIPPET.assign( snippet ) );
} else if( !signage.empty() ) {
signtext = signage;
}
if( !signtext.empty() ) {
// replace tags
signtext = _( signtext.c_str() );
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;
}
};
/**
* 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( JsonObject &jsi ) : jmapgen_piece()
, 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 ) ) {
jsi.throw_error( "no such item group", "item_group" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
const int rx = x.get();
const int ry = y.get();
dat.m.furn_set( rx, ry, f_null );
dat.m.place_vending( rx, ry, item_group_id, reinforced );
}
};
/**
* 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( JsonObject &jsi ) : jmapgen_piece()
, amount( jsi, "amount", 0, 0 ) {
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
const int rx = x.get();
const int ry = y.get();
const long charges = amount.get();
dat.m.furn_set( rx, ry, f_null );
if( charges == 0 ) {
dat.m.place_toilet( rx, ry ); // Use the default charges supplied as default values
} else {
dat.m.place_toilet( rx, ry, charges );
}
}
};
/**
* 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( JsonObject &jsi ) : jmapgen_piece()
, 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( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
const int rx = x.get();
const int ry = y.get();
long charges = amount.get();
dat.m.furn_set( rx, ry, f_null );
if( charges == 0 ) {
charges = rng( 10000, 50000 );
}
if( !fuel.empty() ) {
dat.m.place_gas_pump( rx, ry, charges, fuel );
} else {
dat.m.place_gas_pump( rx, ry, charges );
}
}
};
/**
* Place a specific liquid into the map.
* "liquid": id of the liquid item (item should use charges)
* "amount": quantity of liquid placed (a value of 0 uses the default amount)
* "chance": chance of liquid being placed, see @ref map::place_items
*/
class jmapgen_liquid_item : public jmapgen_piece
{
public:
jmapgen_int amount;
std::string liquid;
jmapgen_int chance;
jmapgen_liquid_item( JsonObject &jsi ) : jmapgen_piece()
, amount( jsi, "amount", 0, 0 )
, liquid( jsi.get_string( "liquid" ) )
, chance( jsi, "chance", 1, 1 ) {
if( !item::type_is_defined( itype_id( liquid ) ) ) {
jsi.throw_error( "no such item type", "liquid" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
if( one_in( chance.get() ) ) {
item newliquid( liquid, calendar::time_of_cataclysm );
if( amount.valmax > 0 ) {
newliquid.charges = amount.get();
}
dat.m.add_item_or_charges( tripoint( x.get(), y.get(), dat.m.get_abs_sub().z ), newliquid );
}
}
};
/**
* Place items from an item group.
* "item": id of the item group.
* "chance": chance of items being placed, see @ref map::place_items
*/
class jmapgen_item_group : public jmapgen_piece
{
public:
std::string group_id;
jmapgen_int chance;
jmapgen_item_group( JsonObject &jsi ) : jmapgen_piece()
, group_id( jsi.get_string( "item" ) )
, chance( jsi, "chance", 1, 1 ) {
if( !item_group::group_is_defined( group_id ) ) {
jsi.throw_error( "no such item group", "item" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
dat.m.place_items( group_id, chance.get(), x.val, y.val, x.valmax, y.valmax, true, 0 );
}
};
/** Place items from an item group */
class jmapgen_loot : public jmapgen_piece
{
friend jmapgen_objects;
public:
jmapgen_loot( JsonObject &jsi ) : jmapgen_piece()
, result_group( Item_group::Type::G_COLLECTION, 100, jsi.get_int( "ammo", 0 ),
jsi.get_int( "magazine", 0 ) )
, chance( jsi.get_int( "chance", 100 ) ) {
const std::string group = jsi.get_string( "group", std::string() );
const std::string name = jsi.get_string( "item", std::string() );
if( group.empty() == name.empty() ) {
jsi.throw_error( "must provide either item or group" );
}
if( !group.empty() && !item_group::group_is_defined( group ) ) {
jsi.throw_error( "no such item group", "group" );
}
if( !name.empty() && !item::type_is_defined( name ) ) {
jsi.throw_error( "no such item", "item" );
}
// All the probabilities are 100 because we do the roll in @ref apply.
if( group.empty() ) {
result_group.add_item_entry( name, 100 );
} else {
result_group.add_group_entry( group, 100 );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mon_density*/ ) const override {
if( rng( 0, 99 ) < chance ) {
const Item_spawn_data *const isd = &result_group;
const std::vector<item> spawn = isd->create( calendar::time_of_cataclysm );
dat.m.spawn_items( tripoint( rng( x.val, x.valmax ), rng( y.val, y.valmax ),
dat.m.get_abs_sub().z ), spawn );
}
}
private:
Item_group result_group;
int chance;
};
/**
* Place spawn points for a monster group (actual monster spawning is done later).
* "monster": id of the monster group.
* "chance": see @ref map::place_spawns
* "density": see @ref map::place_spawns
*/
class jmapgen_monster_group : public jmapgen_piece
{
public:
mongroup_id id;
float density;
jmapgen_int chance;
jmapgen_monster_group( JsonObject &jsi ) : jmapgen_piece()
, id( jsi.get_string( "monster" ) )
, density( jsi.get_float( "density", -1.0f ) )
, chance( jsi, "chance", 1, 1 ) {
if( !id.is_valid() ) {
jsi.throw_error( "no such monster group", "monster" );
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float mdensity ) const override {
dat.m.place_spawns( id, chance.get(), x.val, y.val, x.valmax, y.valmax,
density == -1.0f ? mdensity : density );
}
};
/**
* Place spawn points for a specific monster (not a group).
* "monster": id of the monster.
* "friendly": whether the new monster is friendly to the player character.
* "name": the name of the monster (if it has one).
* "chance": the percentage chance of a monster, affected by spawn density
* If high density means greater than one hundred percent, can place multiples.
* "repeat": roll this many times for creatures, potentially spawning multiples.
* "pack_size": place this many creatures each time a roll is successful.
* "one_or_none": place max of 1 (or pack_size) monsters, even if spawn density > 1.
* Defaults to true if repeat and pack_size are unset, false if one is set.
*/
class jmapgen_monster : public jmapgen_piece