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
|
#include "StarWorldLayout.hpp"
#include "StarJsonExtra.hpp"
#include "StarWorldGeometry.hpp"
#include "StarAssets.hpp"
#include "StarBiomeDatabase.hpp"
#include "StarTerrainDatabase.hpp"
#include "StarParallax.hpp"
#include "StarRoot.hpp"
namespace Star {
WorldRegion::WorldRegion()
: terrainSelectorIndex(NullTerrainSelectorIndex),
foregroundCaveSelectorIndex(NullTerrainSelectorIndex),
backgroundCaveSelectorIndex(NullTerrainSelectorIndex),
blockBiomeIndex(NullBiomeIndex),
environmentBiomeIndex(NullBiomeIndex) {}
WorldRegion::WorldRegion(Json const& store) {
terrainSelectorIndex = store.getUInt("terrainSelectorIndex");
foregroundCaveSelectorIndex = store.getUInt("foregroundCaveSelectorIndex");
backgroundCaveSelectorIndex = store.getUInt("backgroundCaveSelectorIndex");
blockBiomeIndex = store.getUInt("blockBiomeIndex");
environmentBiomeIndex = store.getUInt("environmentBiomeIndex");
regionLiquids.caveLiquid = store.getUInt("caveLiquid");
regionLiquids.caveLiquidSeedDensity = store.getFloat("caveLiquidSeedDensity");
regionLiquids.oceanLiquid = store.getUInt("oceanLiquid");
regionLiquids.oceanLiquidLevel = store.getInt("oceanLiquidLevel");
regionLiquids.encloseLiquids = store.getBool("encloseLiquids");
regionLiquids.fillMicrodungeons = store.getBool("fillMicrodungeons");
subBlockSelectorIndexes = transform<List<TerrainSelectorIndex>>(store.getArray("subBlockSelectorIndexes"), mem_fn(&Json::toUInt));
foregroundOreSelectorIndexes = transform<List<TerrainSelectorIndex>>(store.getArray("foregroundOreSelectorIndexes"), mem_fn(&Json::toUInt));
backgroundOreSelectorIndexes = transform<List<TerrainSelectorIndex>>(store.getArray("backgroundOreSelectorIndexes"), mem_fn(&Json::toUInt));
}
Json WorldRegion::toJson() const {
return JsonObject{
{"terrainSelectorIndex", terrainSelectorIndex},
{"foregroundCaveSelectorIndex", foregroundCaveSelectorIndex},
{"backgroundCaveSelectorIndex", backgroundCaveSelectorIndex},
{"blockBiomeIndex", blockBiomeIndex},
{"environmentBiomeIndex", environmentBiomeIndex},
{"caveLiquid", regionLiquids.caveLiquid},
{"caveLiquidSeedDensity", regionLiquids.caveLiquidSeedDensity},
{"oceanLiquid", regionLiquids.oceanLiquid},
{"oceanLiquidLevel", regionLiquids.oceanLiquidLevel},
{"encloseLiquids", regionLiquids.encloseLiquids},
{"fillMicrodungeons", regionLiquids.fillMicrodungeons},
{"subBlockSelectorIndexes", subBlockSelectorIndexes.transformed(construct<Json>())},
{"foregroundOreSelectorIndexes", foregroundOreSelectorIndexes.transformed(construct<Json>())},
{"backgroundOreSelectorIndexes", backgroundOreSelectorIndexes.transformed(construct<Json>())}
};
}
WorldLayout::BlockNoise WorldLayout::BlockNoise::build(Json const& config, uint64_t seed) {
BlockNoise blockNoise;
blockNoise.horizontalNoise = PerlinF(config.get("horizontalNoise"), staticRandomU64(seed, "HorizontalNoise"));
blockNoise.verticalNoise = PerlinF(config.get("verticalNoise"), staticRandomU64(seed, "VerticalNoise"));
blockNoise.xNoise = PerlinF(config.get("noise"), staticRandomU64(seed, "XNoise"));
blockNoise.yNoise = PerlinF(config.get("noise"), staticRandomU64(seed, "yNoise"));
return blockNoise;
}
WorldLayout::BlockNoise::BlockNoise() {}
WorldLayout::BlockNoise::BlockNoise(Json const& store) {
horizontalNoise = PerlinF(store.get("horizontalNoise"));
verticalNoise = PerlinF(store.get("verticalNoise"));
xNoise = PerlinF(store.get("xNoise"));
yNoise = PerlinF(store.get("yNoise"));
}
Json WorldLayout::BlockNoise::toJson() const {
return JsonObject{
{"horizontalNoise", horizontalNoise.toJson()},
{"verticalNoise", verticalNoise.toJson()},
{"xNoise", xNoise.toJson()},
{"yNoise", yNoise.toJson()},
};
}
Vec2I WorldLayout::BlockNoise::apply(Vec2I const& input, Vec2U const& worldSize) const {
float angle = (input[0] / (float)worldSize[0]) * 2 * Constants::pi;
float xc = std::sin(angle) / (2 * Constants::pi) * worldSize[0];
float zc = std::cos(angle) / (2 * Constants::pi) * worldSize[0];
Vec2I noisePos = Vec2I(
floor(input[0] + horizontalNoise.get(input[1]) + xNoise.get(xc, input[1], zc)),
floor(input[1] + verticalNoise.get(xc, zc) + yNoise.get(xc, input[1], zc))
);
noisePos[1] = clamp<int>(noisePos[1], 0, worldSize[1]);
return noisePos;
}
WorldLayout WorldLayout::buildTerrestrialLayout(TerrestrialWorldParameters const& terrestrialParameters, uint64_t seed) {
auto& root = Root::singleton();
auto assets = root.assets();
auto terrainDatabase = root.terrainDatabase();
auto biomeDatabase = root.biomeDatabase();
RandomSource randSource(seed);
WorldLayout layout;
layout.m_worldSize = terrestrialParameters.worldSize;
auto addLayer = [&](TerrestrialWorldParameters::TerrestrialLayer const& terrestrialLayer) {
RegionParams primaryRegionParams = {
terrestrialLayer.layerBaseHeight,
terrestrialParameters.threatLevel,
terrestrialLayer.primaryRegion.biome,
terrestrialLayer.primaryRegion.blockSelector,
terrestrialLayer.primaryRegion.fgCaveSelector,
terrestrialLayer.primaryRegion.bgCaveSelector,
terrestrialLayer.primaryRegion.fgOreSelector,
terrestrialLayer.primaryRegion.bgOreSelector,
terrestrialLayer.primaryRegion.subBlockSelector,
{
terrestrialLayer.primaryRegion.caveLiquid,
terrestrialLayer.primaryRegion.caveLiquidSeedDensity,
terrestrialLayer.primaryRegion.oceanLiquid,
terrestrialLayer.primaryRegion.oceanLiquidLevel,
terrestrialLayer.primaryRegion.encloseLiquids,
terrestrialLayer.primaryRegion.fillMicrodungeons
}
};
RegionParams primarySubRegionParams = {
terrestrialLayer.layerBaseHeight,
terrestrialParameters.threatLevel,
terrestrialLayer.primarySubRegion.biome,
terrestrialLayer.primarySubRegion.blockSelector,
terrestrialLayer.primarySubRegion.fgCaveSelector,
terrestrialLayer.primarySubRegion.bgCaveSelector,
terrestrialLayer.primarySubRegion.fgOreSelector,
terrestrialLayer.primarySubRegion.bgOreSelector,
terrestrialLayer.primarySubRegion.subBlockSelector,
{
terrestrialLayer.primarySubRegion.caveLiquid,
terrestrialLayer.primarySubRegion.caveLiquidSeedDensity,
terrestrialLayer.primarySubRegion.oceanLiquid,
terrestrialLayer.primarySubRegion.oceanLiquidLevel,
terrestrialLayer.primarySubRegion.encloseLiquids,
terrestrialLayer.primarySubRegion.fillMicrodungeons
}
};
List<RegionParams> secondaryRegions;
for (auto const& secondaryRegion : terrestrialLayer.secondaryRegions) {
RegionParams secondaryRegionParams = {
terrestrialLayer.layerBaseHeight,
terrestrialParameters.threatLevel,
secondaryRegion.biome,
secondaryRegion.blockSelector,
secondaryRegion.fgCaveSelector,
secondaryRegion.bgCaveSelector,
secondaryRegion.fgOreSelector,
secondaryRegion.bgOreSelector,
secondaryRegion.subBlockSelector,
{
secondaryRegion.caveLiquid,
secondaryRegion.caveLiquidSeedDensity,
secondaryRegion.oceanLiquid,
secondaryRegion.oceanLiquidLevel,
secondaryRegion.encloseLiquids,
secondaryRegion.fillMicrodungeons
}
};
secondaryRegions.append(secondaryRegionParams);
}
List<RegionParams> secondarySubRegions;
for (auto const& secondarySubRegion : terrestrialLayer.secondarySubRegions) {
RegionParams secondarySubRegionParams = {
terrestrialLayer.layerBaseHeight,
terrestrialParameters.threatLevel,
secondarySubRegion.biome,
secondarySubRegion.blockSelector,
secondarySubRegion.fgCaveSelector,
secondarySubRegion.bgCaveSelector,
secondarySubRegion.fgOreSelector,
secondarySubRegion.bgOreSelector,
secondarySubRegion.subBlockSelector,
{
secondarySubRegion.caveLiquid,
secondarySubRegion.caveLiquidSeedDensity,
secondarySubRegion.oceanLiquid,
secondarySubRegion.oceanLiquidLevel,
secondarySubRegion.encloseLiquids,
secondarySubRegion.fillMicrodungeons
}
};
secondarySubRegions.append(secondarySubRegionParams);
}
layout.addLayer(seed,
terrestrialLayer.layerMinHeight,
terrestrialLayer.layerBaseHeight,
terrestrialParameters.primaryBiome,
primaryRegionParams,
primarySubRegionParams,
secondaryRegions,
secondarySubRegions,
terrestrialLayer.secondaryRegionSizeRange,
terrestrialLayer.subRegionSizeRange);
};
addLayer(terrestrialParameters.coreLayer);
for (auto const& undergroundLayer : reverseIterate(terrestrialParameters.undergroundLayers))
addLayer(undergroundLayer);
addLayer(terrestrialParameters.subsurfaceLayer);
addLayer(terrestrialParameters.surfaceLayer);
addLayer(terrestrialParameters.atmosphereLayer);
addLayer(terrestrialParameters.spaceLayer);
layout.m_regionBlending = terrestrialParameters.blendSize;
if (terrestrialParameters.blockNoiseConfig)
layout.m_blockNoise = BlockNoise::build(terrestrialParameters.blockNoiseConfig, seed);
if (terrestrialParameters.blendNoiseConfig)
layout.m_blendNoise = PerlinF(terrestrialParameters.blendNoiseConfig, staticRandomU64(seed, "BlendNoise"));
layout.finalize(terrestrialParameters.skyColoring.mainColor);
return layout;
}
WorldLayout WorldLayout::buildAsteroidsLayout(AsteroidsWorldParameters const& asteroidParameters, uint64_t seed) {
auto assets = Root::singleton().assets();
RandomSource randSource(seed);
auto asteroidsConfig = assets->json("/asteroids_worlds.config");
auto asteroidTerrainConfig = randSource.randFrom(asteroidsConfig.get("terrains").toArray());
auto emptyTerrainConfig = asteroidsConfig.get("emptyTerrain");
WorldLayout layout;
layout.m_worldSize = asteroidParameters.worldSize;
RegionParams asteroidRegion{
(int)asteroidParameters.worldSize[1] / 2,
asteroidParameters.threatLevel,
asteroidParameters.asteroidBiome,
asteroidTerrainConfig.getString("terrainSelector"),
asteroidTerrainConfig.getString("caveSelector"),
asteroidTerrainConfig.getString("bgCaveSelector"),
asteroidTerrainConfig.getString("oreSelector"),
asteroidTerrainConfig.getString("oreSelector"),
asteroidTerrainConfig.getString("subBlockSelector"),
{EmptyLiquidId, 0.0f, EmptyLiquidId, 0, false, false}
};
RegionParams emptyRegion{
(int)asteroidParameters.worldSize[1] / 2,
asteroidParameters.threatLevel,
asteroidParameters.asteroidBiome,
emptyTerrainConfig.getString("terrainSelector"),
emptyTerrainConfig.getString("caveSelector"),
emptyTerrainConfig.getString("bgCaveSelector"),
emptyTerrainConfig.getString("oreSelector"),
emptyTerrainConfig.getString("oreSelector"),
emptyTerrainConfig.getString("subBlockSelector"),
{EmptyLiquidId, 0.0f, EmptyLiquidId, 0, false, false}
};
layout.addLayer(seed, 0, emptyRegion);
layout.addLayer(seed, asteroidParameters.asteroidBottomLevel, asteroidRegion);
layout.addLayer(seed, asteroidParameters.asteroidTopLevel, emptyRegion);
layout.m_regionBlending = asteroidParameters.blendSize;
layout.m_blockNoise = asteroidsConfig.opt("blockNoise").apply(bind(&BlockNoise::build, _1, seed));
layout.m_playerStartSearchRegions.append(RectI(0, asteroidParameters.asteroidBottomLevel, asteroidParameters.worldSize[0], asteroidParameters.asteroidTopLevel));
layout.finalize(Color::Black);
return layout;
}
WorldLayout WorldLayout::buildFloatingDungeonLayout(FloatingDungeonWorldParameters const& floatingDungeonParameters, uint64_t seed) {
auto assets = Root::singleton().assets();
auto biomeDatabase = Root::singleton().biomeDatabase();
RandomSource randSource(seed);
WorldLayout layout;
layout.m_worldSize = floatingDungeonParameters.worldSize;
RegionParams biomeRegion{
(int)floatingDungeonParameters.dungeonSurfaceHeight,
floatingDungeonParameters.threatLevel,
floatingDungeonParameters.biome,
{}, {}, {}, {}, {}, {},
{EmptyLiquidId, 0.0f, EmptyLiquidId, 0, false, false}
};
layout.addLayer(seed, 0, biomeRegion);
if (floatingDungeonParameters.biome)
biomeDatabase->biomeSkyColoring(*floatingDungeonParameters.biome, seed);
else
layout.finalize(Color::Black);
return layout;
}
WorldLayout::WorldLayout() : m_regionBlending(0.0f) {}
WorldLayout::WorldLayout(Json const& store) : WorldLayout() {
auto terrainDatabase = Root::singleton().terrainDatabase();
m_worldSize = jsonToVec2U(store.get("worldSize"));
m_biomes = store.getArray("biomes").transformed([](Json const& json) {
return BiomeConstPtr(make_shared<Biome>(json));
});
m_terrainSelectors = store.getArray("terrainSelectors").transformed([terrainDatabase](Json const& v) {
return TerrainSelectorConstPtr(terrainDatabase->loadSelector(v));
});
m_layers = store.getArray("layers").transformed([](Json const& l) {
WorldLayer layer;
layer.yStart = l.getInt("yStart");
for (auto const& b : l.getArray("boundaries"))
layer.boundaries.append(b.toInt());
for (auto const& r : l.getArray("cells"))
layer.cells.append(make_shared<WorldRegion>(r));
return layer;
});
m_regionBlending = store.getFloat("regionBlending");
m_blockNoise = store.opt("blockNoise").apply(construct<BlockNoise>());
m_blendNoise = store.opt("blendNoise").apply(construct<PerlinF>());
m_playerStartSearchRegions = store.getArray("playerStartSearchRegions").transformed(jsonToRectI);
}
Json WorldLayout::toJson() const {
auto terrainDatabase = Root::singleton().terrainDatabase();
return JsonObject{
{"worldSize", jsonFromVec2U(m_worldSize)},
{"biomes", transform<JsonArray>(m_biomes, [](auto const& biome) {
return biome->toJson();
})},
{"terrainSelectors", transform<JsonArray>(m_terrainSelectors, [terrainDatabase](auto const& selector) {
return terrainDatabase->storeSelector(selector);
})},
{"layers", m_layers.transformed([](WorldLayer const& layer) -> Json {
return JsonObject{
{"yStart", layer.yStart},
{"boundaries", JsonArray::from(layer.boundaries.transformed(construct<Json>()))},
{"cells", JsonArray::from(layer.cells.transformed(mem_fn(&WorldRegion::toJson)))}
};
})},
{"regionBlending", m_regionBlending},
{"blockNoise", m_blockNoise.apply(mem_fn(&BlockNoise::toJson)).value()},
{"blendNoise", m_blendNoise.apply(mem_fn(&PerlinF::toJson)).value()},
{"playerStartSearchRegions", JsonArray::from(m_playerStartSearchRegions.transformed(jsonFromRectI))}
};
}
Maybe<WorldLayout::BlockNoise> const& WorldLayout::blockNoise() const {
return m_blockNoise;
}
Maybe<PerlinF> const& WorldLayout::blendNoise() const {
return m_blendNoise;
}
List<RectI> WorldLayout::playerStartSearchRegions() const {
return m_playerStartSearchRegions;
}
List<WorldLayout::RegionWeighting> WorldLayout::getWeighting(int x, int y) const {
List<RegionWeighting> weighting;
WorldGeometry geometry(m_worldSize);
auto cellWeighting = [&](WorldLayer const& layer, size_t cellIndex, int x) -> float {
int xMin = 0;
if (cellIndex > 0)
xMin = layer.boundaries[cellIndex - 1];
int xMax = m_worldSize[0];
if (cellIndex < layer.boundaries.size())
xMax = layer.boundaries[cellIndex];
if (x > (xMin + xMax) / 2.0f)
return clamp<float>(0.5f - (x - xMax) / m_regionBlending, 0.0f, 1.0f);
else
return clamp<float>(0.5f - (xMin - x) / m_regionBlending, 0.0f, 1.0f);
};
auto addLayerWeighting = [&](WorldLayer const& layer, int x, float weightFactor) {
if (layer.cells.empty())
return;
size_t innerCellIndex;
int innerCellXValue;
tie(innerCellIndex, innerCellXValue) = findContainingCell(layer, x);
float innerCellWeight = cellWeighting(layer, innerCellIndex, innerCellXValue);
size_t leftCellIndex;
int leftCellXValue;
tie(leftCellIndex, leftCellXValue) = leftCell(layer, innerCellIndex, innerCellXValue);
float leftCellWeight = cellWeighting(layer, leftCellIndex, leftCellXValue);
size_t rightCellIndex;
int rightCellXValue;
tie(rightCellIndex, rightCellXValue) = rightCell(layer, innerCellIndex, innerCellXValue);
float rightCellWeight = cellWeighting(layer, rightCellIndex, rightCellXValue);
float totalWeight = innerCellWeight + leftCellWeight + rightCellWeight;
if (totalWeight <= 0.0f)
return;
innerCellWeight *= weightFactor / totalWeight;
leftCellWeight *= weightFactor / totalWeight;
rightCellWeight *= weightFactor / totalWeight;
if (innerCellWeight > 0.0f)
weighting.append(RegionWeighting{innerCellWeight, innerCellXValue, layer.cells[innerCellIndex].get()});
if (leftCellWeight > 0.0f)
weighting.append(RegionWeighting{leftCellWeight, leftCellXValue, layer.cells[leftCellIndex].get()});
if (rightCellWeight > 0.0f)
weighting.append(RegionWeighting{rightCellWeight, rightCellXValue, layer.cells[rightCellIndex].get()});
};
auto yi = std::lower_bound(m_layers.begin(), m_layers.end(), y, [](WorldLayer const& layer, int y) {
return layer.yStart < y;
});
if (yi == m_layers.end() || yi->yStart != y) {
if (yi == m_layers.begin())
return {};
else
--yi;
}
if (y - yi->yStart < (m_regionBlending / 2)) {
if (yi == m_layers.begin()) {
addLayerWeighting(*yi, x, 1.0f);
} else {
auto ypi = yi;
--ypi;
float yWeight = 0.5f + (y - yi->yStart) / m_regionBlending;
addLayerWeighting(*yi, x, yWeight);
addLayerWeighting(*ypi, x, 1.0f - yWeight);
}
} else {
auto yni = yi;
++yni;
if (yni == m_layers.end()) {
addLayerWeighting(*yi, x, 1.0f);
} else if (y <= yni->yStart - (m_regionBlending / 2)) {
addLayerWeighting(*yi, x, 1.0f);
} else {
float yWeight = 0.5f - (yni->yStart - y) / m_regionBlending;
addLayerWeighting(*yi, x, 1.0f - yWeight);
addLayerWeighting(*yni, x, yWeight);
}
}
// Need to return weighting in order of greatest to least
sort(weighting, [](RegionWeighting const& lhs, RegionWeighting const& rhs) {
return lhs.weight > rhs.weight;
});
return weighting;
}
List<RectI> WorldLayout::previewAddBiomeRegion(Vec2I const& position, int width) const {
auto layerAndCell = findLayerAndCell(position[0], position[1]);
auto targetLayer = m_layers[layerAndCell.first];
auto targetRegion = targetLayer.cells[layerAndCell.second];
int insertX = position[0] > 0 ? position[0] : 1;
// need a dummy region to expand
std::shared_ptr<WorldRegion> dummyRegion;
targetLayer.boundaries.insertAt(layerAndCell.second, insertX);
targetLayer.cells.insertAt(layerAndCell.second, dummyRegion);
targetLayer.boundaries.insertAt(layerAndCell.second, insertX - 1);
targetLayer.cells.insertAt(layerAndCell.second, targetRegion);
auto expandResult = expandRegionInLayer(targetLayer, layerAndCell.second + 1, width);
return expandResult.second;
}
List<RectI> WorldLayout::previewExpandBiomeRegion(Vec2I const& position, int width) const {
auto layerAndCell = findLayerAndCell(position[0], position[1]);
auto targetLayer = m_layers[layerAndCell.first];
auto expandResult = expandRegionInLayer(targetLayer, layerAndCell.second, width);
return expandResult.second;
}
String WorldLayout::setLayerEnvironmentBiome(Vec2I const& position) {
auto layerAndCell = findLayerAndCell(position[0], position[1]);
auto targetLayer = m_layers[layerAndCell.first];
auto targetBiomeIndex = targetLayer.cells[layerAndCell.second]->blockBiomeIndex;
for (size_t i = 0; i < targetLayer.cells.size(); ++i)
targetLayer.cells[i]->environmentBiomeIndex = targetBiomeIndex;
m_layers[layerAndCell.first] = targetLayer;
return getBiome(targetBiomeIndex)->baseName;
}
void WorldLayout::addBiomeRegion(
TerrestrialWorldParameters const& terrestrialParameters,
uint64_t seed,
Vec2I const& position,
String biomeName,
String const& subBlockSelector,
int width) {
auto layerAndCell = findLayerAndCell(position[0], position[1]);
// Logger::info("inserting biome {} into region with layerIndex {} cellIndex {}", biomeName, layerAndCell.first, layerAndCell.second);
auto targetLayer = m_layers[layerAndCell.first];
// do this annoying dance to figure out which terrestrial layer we're in, so
// we can extract the base height
TerrestrialWorldParameters::TerrestrialLayer terrestrialLayer = terrestrialParameters.coreLayer;
auto checkLayer = [targetLayer, &terrestrialLayer](TerrestrialWorldParameters::TerrestrialLayer const& layer) {
if (layer.layerMinHeight == targetLayer.yStart)
terrestrialLayer = layer;
};
for (auto undergroundLayer : terrestrialParameters.undergroundLayers)
checkLayer(undergroundLayer);
checkLayer(terrestrialParameters.subsurfaceLayer);
checkLayer(terrestrialParameters.surfaceLayer);
checkLayer(terrestrialParameters.atmosphereLayer);
checkLayer(terrestrialParameters.spaceLayer);
// build a new region using the biomeName and the parameters from the target region
auto targetRegion = targetLayer.cells[layerAndCell.second];
WorldRegion newRegion;
newRegion.terrainSelectorIndex = targetRegion->terrainSelectorIndex;
newRegion.foregroundCaveSelectorIndex = targetRegion->foregroundCaveSelectorIndex;
newRegion.backgroundCaveSelectorIndex = targetRegion->backgroundCaveSelectorIndex;
newRegion.foregroundOreSelectorIndexes = targetRegion->foregroundOreSelectorIndexes;
newRegion.backgroundOreSelectorIndexes = targetRegion->backgroundOreSelectorIndexes;
newRegion.regionLiquids = targetRegion->regionLiquids;
auto biomeDatabase = Root::singleton().biomeDatabase();
auto newBiome = biomeDatabase->createBiome(biomeName, staticRandomU64(seed, "BiomeSeed"), terrestrialLayer.layerBaseHeight, terrestrialParameters.threatLevel);
auto oldBiome = getBiome(targetRegion->blockBiomeIndex);
newBiome->ores = oldBiome->ores;
// build new sub block selectors; this is the only region-level property that needs to be
// newly constructed for the biome
TerrainSelectorParameters baseSelectorParameters;
baseSelectorParameters.worldWidth = m_worldSize[0];
baseSelectorParameters.baseHeight = terrestrialLayer.layerBaseHeight;
auto terrainDatabase = Root::singleton().terrainDatabase();
for (size_t i = 0; i < newBiome->subBlocks.size(); ++i) {
auto selector = terrainDatabase->createNamedSelector(subBlockSelector, baseSelectorParameters.withSeed(staticRandomU64(seed, i, "subBlocks")));
newRegion.subBlockSelectorIndexes.append(registerTerrainSelector(selector));
}
newRegion.environmentBiomeIndex = targetRegion->environmentBiomeIndex;
newRegion.blockBiomeIndex = registerBiome(newBiome);
WorldRegionPtr newRegionPtr = make_shared<WorldRegion>(newRegion);
// Logger::info("boundaries before region insertion are {}", targetLayer.boundaries);
// handle case where insert x position is exactly at world wrap
int insertX = position[0] > 0 ? position[0] : 1;
// insert the new region boundary
targetLayer.boundaries.insertAt(layerAndCell.second, insertX);
targetLayer.cells.insertAt(layerAndCell.second, newRegionPtr);
// insert the left side of the (now split) target region
targetLayer.boundaries.insertAt(layerAndCell.second, insertX - 1);
targetLayer.cells.insertAt(layerAndCell.second, targetRegion);
// Logger::info("boundaries after region insertion are {}", targetLayer.boundaries);
// expand the cell to the desired size
auto expandResult = expandRegionInLayer(targetLayer, layerAndCell.second + 1, width);
// update the layer in the template
m_layers[layerAndCell.first] = expandResult.first;
}
void WorldLayout::expandBiomeRegion(Vec2I const& position, int newWidth) {
auto layerAndCell = findLayerAndCell(position[0], position[1]);
auto targetLayer = m_layers[layerAndCell.first];
auto expandResult = expandRegionInLayer(targetLayer, layerAndCell.second, newWidth);
m_layers[layerAndCell.first] = expandResult.first;
}
pair<size_t, size_t> WorldLayout::findLayerAndCell(int x, int y) const {
// find the target layer
size_t targetLayerIndex{};
for (size_t i = 0; i < m_layers.size(); ++i) {
if (m_layers[i].yStart < y)
targetLayerIndex = i;
else
break;
}
auto targetLayer = m_layers[targetLayerIndex];
auto targetCell = findContainingCell(targetLayer, x);
return {targetLayerIndex, targetCell.first};
}
WorldLayout::WorldLayer::WorldLayer() : yStart(0) {}
pair<WorldLayout::WorldLayer, List<RectI>> WorldLayout::expandRegionInLayer(WorldLayout::WorldLayer targetLayer, size_t cellIndex, int newWidth) const {
struct RegionCell {
int lBound;
int rBound;
WorldRegionPtr region;
};
// auto printRegionCells = [](List<RegionCell> const& cells) {
// String output = "";
// for (auto cell : cells)
// output += strf("[{} {}] ", cell.lBound, cell.rBound);
// return output;
// };
// Logger::info("expanding region in layer with cellIndex {} newWidth {}", cellIndex, newWidth);
List<RectI> regionRects;
if (targetLayer.cells.size() == 1) {
Logger::info("Cannot expand region as it already fills the layer");
return {targetLayer, regionRects};
}
// Logger::info("boundaries before expansion are {}", targetLayer.boundaries);
// TODO: this is a messy way to get the top of the layer, but maybe it's ok
int layerTop = (int)m_worldSize[1];
for (size_t i = 0; i < m_layers.size(); ++i) {
if (m_layers[i].yStart == targetLayer.yStart && m_layers.size() > i + 1) {
layerTop = m_layers[i + 1].yStart;
break;
}
}
// if the region is going to cover the full layer width, this is much simpler
if (newWidth == (int)m_worldSize[0]) {
targetLayer.cells = {targetLayer.cells[cellIndex]};
targetLayer.boundaries = {};
regionRects.append(RectI{0, targetLayer.yStart, (int)m_worldSize[0], layerTop});
} else {
auto targetRegion = targetLayer.cells[cellIndex];
// convert cells and boundaries into something more tractable
List<RegionCell> targetCells;
List<RegionCell> otherCells;
int lastBoundary = 0;
size_t lastCellIndex = targetLayer.cells.size() - 1;
for (size_t i = 0; i <= lastCellIndex; ++i) {
int nextBoundary = i == lastCellIndex ? (int)m_worldSize[0] : targetLayer.boundaries[i];
if (i == cellIndex ||
(i == 0 && cellIndex == lastCellIndex && targetLayer.cells[i] == targetRegion) ||
(cellIndex == 0 && i == lastCellIndex && targetLayer.cells[i] == targetRegion))
targetCells.append(RegionCell{lastBoundary, nextBoundary, targetLayer.cells[i]});
else
otherCells.append(RegionCell{lastBoundary, nextBoundary, targetLayer.cells[i]});
lastBoundary = nextBoundary;
}
// Logger::info("before expansion:\ntarget cells are: {}\nother cells are: {}", printRegionCells(targetCells), printRegionCells(otherCells));
starAssert(targetCells.size() > 0);
starAssert(targetCells.size() < 3);
// check the current width to see how much (if any) to expand
int currentWidth = 0;
for (auto regionCell : targetCells)
currentWidth += (regionCell.rBound - regionCell.lBound);
if (currentWidth >= newWidth) {
Logger::info("New cell width ({}) must be greater than current cell width {}!", newWidth, currentWidth);
return {targetLayer, regionRects};
}
// expand the leftmost cell to the right and the rightmost cell to the left (they may be the same cell)
int expandRight = ceil(0.5 * (newWidth - currentWidth));
int expandLeft = floor(0.5 * (newWidth - currentWidth));
// build the rects for the areas NEWLY covered by the region; these don't need to be wrapped because
// they'll be split when they're consumed
regionRects.append(RectI{targetCells[0].rBound, targetLayer.yStart, targetCells[0].rBound + expandRight, layerTop});
regionRects.append(RectI{targetCells[targetCells.size() - 1].lBound - expandLeft, targetLayer.yStart, targetCells[targetCells.size() - 1].lBound, layerTop});
targetCells[0].rBound += expandRight;
targetCells[targetCells.size() - 1].lBound -= expandLeft;
// Logger::info("after expansion:\ntarget cells are: {}\nother cells are: {}", printRegionCells(targetCells), printRegionCells(otherCells));
// split any target cells that now cross the world wrap
List<RegionCell> wrappedTargetCells;
for (auto cell : targetCells) {
if (cell.lBound < 0) {
wrappedTargetCells.append(RegionCell{0, cell.rBound, cell.region});
wrappedTargetCells.append(RegionCell{(int)m_worldSize[0] + cell.lBound, (int)m_worldSize[0], cell.region});
} else if (cell.rBound > (int)m_worldSize[0]) {
wrappedTargetCells.append(RegionCell{cell.lBound, (int)m_worldSize[0], cell.region});
wrappedTargetCells.append(RegionCell{0, cell.rBound - (int)m_worldSize[0], cell.region});
} else {
wrappedTargetCells.append(cell);
}
}
targetCells = wrappedTargetCells;
// modify/delete any overlapped cells
for (auto targetCell : targetCells) {
List<RegionCell> newOtherCells;
for (auto otherCell : otherCells) {
bool rInside = otherCell.rBound <= targetCell.rBound && otherCell.rBound >= targetCell.lBound;
bool lInside = otherCell.lBound <= targetCell.rBound && otherCell.lBound >= targetCell.lBound;
if (rInside && lInside)
continue;
else if (rInside)
newOtherCells.append(RegionCell{otherCell.lBound, targetCell.lBound, otherCell.region});
else if (lInside)
newOtherCells.append(RegionCell{targetCell.rBound, otherCell.rBound, otherCell.region});
else
newOtherCells.append(otherCell);
}
otherCells = newOtherCells;
}
// Logger::info("after de-overlapping:\ntarget cells are: {}\nother cells are: {}", printRegionCells(targetCells), printRegionCells(otherCells));
// combine lists and sort
otherCells.appendAll(targetCells);
otherCells.sort([](RegionCell const& a, RegionCell const& b) { return a.rBound < b.rBound; });
// convert back into cells and boundaries
targetLayer.cells.clear();
targetLayer.boundaries.clear();
for (size_t i = 0; i < otherCells.size(); ++i) {
targetLayer.cells.append(otherCells[i].region);
if (i < otherCells.size() - 1)
targetLayer.boundaries.append(otherCells[i].rBound);
}
}
// Logger::info("boundaries after expansion are {}", targetLayer.boundaries);
return {targetLayer, regionRects};
}
BiomeIndex WorldLayout::registerBiome(BiomeConstPtr biome) {
size_t foundIndex = m_biomes.indexOf(biome);
if (foundIndex != NPos)
return foundIndex + 1;
m_biomes.append(biome);
return m_biomes.size();
}
TerrainSelectorIndex WorldLayout::registerTerrainSelector(TerrainSelectorConstPtr terrainSelector) {
size_t foundIndex = m_terrainSelectors.indexOf(terrainSelector);
if (foundIndex != NPos)
return foundIndex + 1;
m_terrainSelectors.append(terrainSelector);
return m_terrainSelectors.size();
}
WorldRegion WorldLayout::buildRegion(uint64_t seed, RegionParams const& regionParams) {
auto terrainDatabase = Root::singleton().terrainDatabase();
auto biomeDatabase = Root::singleton().biomeDatabase();
WorldRegion region;
TerrainSelectorParameters baseSelectorParameters;
baseSelectorParameters.worldWidth = m_worldSize[0];
baseSelectorParameters.baseHeight = regionParams.baseHeight;
TerrainSelectorParameters terrainSelectorParameters = baseSelectorParameters.withSeed(staticRandomU64(seed, "Terrain"));
TerrainSelectorParameters foregroundCaveSelectorParameters = baseSelectorParameters.withSeed(staticRandomU64(seed, "ForegroundCaveSeed"));
TerrainSelectorParameters backgroundCaveSelectorParameters = baseSelectorParameters.withSeed(staticRandomU64(seed, "BackgroundCave"));
if (regionParams.terrainSelector)
region.terrainSelectorIndex = registerTerrainSelector(terrainDatabase->createNamedSelector(*regionParams.terrainSelector, terrainSelectorParameters));
if (regionParams.fgCaveSelector)
region.foregroundCaveSelectorIndex = registerTerrainSelector(terrainDatabase->createNamedSelector(*regionParams.fgCaveSelector, foregroundCaveSelectorParameters));
if (regionParams.bgCaveSelector)
region.backgroundCaveSelectorIndex = registerTerrainSelector(terrainDatabase->createNamedSelector(*regionParams.bgCaveSelector, backgroundCaveSelectorParameters));
if (regionParams.biomeName) {
auto biome = biomeDatabase->createBiome(*regionParams.biomeName, staticRandomU64(seed, "BiomeSeed"), regionParams.baseHeight, regionParams.threatLevel);
if (regionParams.subBlockSelector) {
for (size_t i = 0; i < biome->subBlocks.size(); ++i) {
auto selector = terrainDatabase->createNamedSelector(*regionParams.subBlockSelector, terrainSelectorParameters.withSeed(staticRandomU64(seed, i, "subBlocks")));
region.subBlockSelectorIndexes.append(registerTerrainSelector(selector));
}
}
for (auto const& p : enumerateIterator(biome->ores)) {
auto oreSelectorTerrainParameters = terrainSelectorParameters.withCommonality(p.first.second);
if (regionParams.fgOreSelector) {
auto fgSelector = terrainDatabase->createNamedSelector(*regionParams.fgOreSelector, oreSelectorTerrainParameters.withSeed(staticRandomU64(seed, p.second, "FGOreSelector")));
region.foregroundOreSelectorIndexes.append(registerTerrainSelector(fgSelector));
}
if (regionParams.bgOreSelector) {
auto bgSelector = terrainDatabase->createNamedSelector(*regionParams.bgOreSelector, oreSelectorTerrainParameters.withSeed(staticRandomU64(seed, p.second, "BGOreSelector")));
region.backgroundOreSelectorIndexes.append(registerTerrainSelector(bgSelector));
}
}
region.blockBiomeIndex = registerBiome(biome);
region.environmentBiomeIndex = region.blockBiomeIndex;
}
region.regionLiquids = regionParams.regionLiquids;
return region;
}
void WorldLayout::addLayer(uint64_t seed, int yStart, RegionParams regionParams) {
WorldLayer layer;
layer.yStart = yStart;
WorldRegionPtr region = make_shared<WorldRegion>(buildRegion(seed, regionParams));
layer.cells.append(region);
m_layers.append(layer);
}
void WorldLayout::addLayer(uint64_t seed, int yStart, int yBase, String const& primaryBiome,
RegionParams primaryRegionParams, RegionParams primarySubRegionParams,
List<RegionParams> secondaryRegions, List<RegionParams> secondarySubRegions,
Vec2F secondaryRegionSize, Vec2F subRegionSize) {
WorldLayer layer;
layer.yStart = yStart;
List<float> relativeRegionSizes;
float totalRelativeSize = 0.0;
int mix = 0;
BiomeIndex primaryEnvironmentBiomeIndex = buildRegion(seed, primaryRegionParams).environmentBiomeIndex;
Set<BiomeIndex> spawnBiomeIndexes;
auto addRegion = [&](RegionParams const& regionParams, RegionParams const& subRegionParams, Vec2F const& regionSizeRange) {
WorldRegionPtr region = make_shared<WorldRegion>(buildRegion(seed, regionParams));
WorldRegionPtr subRegion = make_shared<WorldRegion>(buildRegion(seed, subRegionParams));
if (!Root::singleton().assets()->json("/terrestrial_worlds.config:useSecondaryEnvironmentBiomeIndex").toBool())
region->environmentBiomeIndex = primaryEnvironmentBiomeIndex;
subRegion->environmentBiomeIndex = region->environmentBiomeIndex;
if (regionParams.biomeName == primaryBiome)
spawnBiomeIndexes.add(region->blockBiomeIndex);
if (subRegionParams.biomeName == primaryBiome)
spawnBiomeIndexes.add(subRegion->blockBiomeIndex);
layer.cells.append(region);
layer.cells.append(subRegion);
layer.cells.append(region);
float regionRelativeSize = staticRandomFloatRange(regionSizeRange[0], regionSizeRange[1], seed, ++mix, yStart);
float subRegionRelativeSize = staticRandomFloatRange(subRegionSize[0], subRegionSize[1], seed, ++mix, yStart);
totalRelativeSize += regionRelativeSize;
if (subRegionRelativeSize >= 1.0f)
throw StarException("Relative size of subRegion must be less than 1.0!");
subRegionRelativeSize *= regionRelativeSize;
regionRelativeSize -= subRegionRelativeSize;
relativeRegionSizes.append(regionRelativeSize / 2);
relativeRegionSizes.append(subRegionRelativeSize);
relativeRegionSizes.append(regionRelativeSize / 2);
};
// construct list of region cells and relative sizes
addRegion(primaryRegionParams, primarySubRegionParams, Vec2F(1, 1));
for (size_t i = 0; i < secondaryRegions.size(); ++i)
addRegion(secondaryRegions[i], secondarySubRegions[i], secondaryRegionSize);
// construct boundaries based on normalized sizes
int nextBoundary = staticRandomI32Range(0, m_worldSize[0] - 1, seed, yStart, "LayerOffset");
layer.boundaries.append(nextBoundary);
for (size_t i = 0; i + 1 < relativeRegionSizes.size(); ++i) {
int regionSize = (int)m_worldSize[0] * (relativeRegionSizes[i] / totalRelativeSize);
nextBoundary += regionSize;
layer.boundaries.append(nextBoundary);
}
// wrap cells + boundaries
while (layer.boundaries.last() > (int)m_worldSize[0]) {
layer.cells.prepend(layer.cells.takeLast());
layer.boundaries.prepend(layer.boundaries.takeLast() - m_worldSize[0]);
}
layer.cells.prepend(layer.cells.last());
int yRange = Root::singleton().assets()->json("/world_template.config:playerStartSearchYRange").toInt();
int i = 0;
int lastBoundary = 0;
for (auto region : layer.cells) {
int nextBoundary = i < (int)layer.boundaries.size() ? layer.boundaries[i] : m_worldSize[0];
if (spawnBiomeIndexes.contains(region->blockBiomeIndex))
m_playerStartSearchRegions.append(RectI(lastBoundary, std::max(0, yBase - yRange), nextBoundary, std::min((int)m_worldSize[1], yBase + yRange)));
lastBoundary = nextBoundary;
i++;
}
m_layers.append(layer);
}
void WorldLayout::finalize(Color mainSkyColor) {
sort(m_layers, [](WorldLayer const& a, WorldLayer const& b) { return a.yStart < b.yStart; });
// Post-process all parallaxes
for (auto const& biome : m_biomes) {
if (biome->parallax)
biome->parallax->fadeToSkyColor(mainSkyColor);
}
}
pair<size_t, int> WorldLayout::findContainingCell(WorldLayer const& layer, int x) const {
x = WorldGeometry(m_worldSize).xwrap(x);
auto xi = std::lower_bound(layer.boundaries.begin(), layer.boundaries.end(), x);
return {xi - layer.boundaries.begin(), x};
}
pair<size_t, int> WorldLayout::leftCell(WorldLayer const& layer, size_t cellIndex, int x) const {
if (cellIndex == 0)
return {layer.cells.size() - 1, x + (int)m_worldSize[0]};
else
return {cellIndex - 1, x};
}
pair<size_t, int> WorldLayout::rightCell(WorldLayer const& layer, size_t cellIndex, int x) const {
if (cellIndex >= layer.cells.size() - 1)
return {0, x - (int)m_worldSize[0]};
else
return {cellIndex + 1, x};
}
}
|