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
|
#include "StarWorldTemplate.hpp"
#include "StarJsonExtra.hpp"
#include "StarInterpolation.hpp"
#include "StarIterator.hpp"
#include "StarBiome.hpp"
#include "StarRoot.hpp"
#include "StarTerrainDatabase.hpp"
#include "StarLiquidTypes.hpp"
#include "StarAssets.hpp"
#include "StarLogging.hpp"
#include "StarDungeonGenerator.hpp"
namespace Star {
WorldTemplate::WorldTemplate(Vec2U const& size) : WorldTemplate() {
m_geometry = size;
}
WorldTemplate::WorldTemplate(CelestialCoordinate const& celestialCoordinate, CelestialDatabasePtr const& celestialDatabase)
: WorldTemplate() {
auto celestialParameters = celestialDatabase->parameters(celestialCoordinate);
if (!celestialParameters)
throw StarException("Celestial parameters for constructing WorldTemplate not found!");
m_celestialParameters = celestialParameters;
m_worldParameters = m_celestialParameters->visitableParameters();
if (!m_worldParameters)
throw StarException("Cannot create WorldTemplate from non-visitable world");
m_skyParameters = SkyParameters(celestialCoordinate, celestialDatabase);
m_seed = m_celestialParameters->seed();
m_geometry = WorldGeometry(m_worldParameters->worldSize);
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildTerrestrialLayout(*terrestrialParameters, m_seed));
else if (auto asteroidsParameters = as<AsteroidsWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildAsteroidsLayout(*asteroidsParameters, m_seed));
else if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildFloatingDungeonLayout(*floatingDungeonParameters, m_seed));
determineWorldName();
}
WorldTemplate::WorldTemplate(VisitableWorldParametersConstPtr const& worldParameters, SkyParameters const& skyParameters, uint64_t seed)
: WorldTemplate() {
if (!worldParameters)
throw StarException("Cannot create WorldTemplate from non-visitable world");
m_worldParameters = worldParameters;
m_skyParameters = skyParameters;
m_seed = seed;
m_geometry = WorldGeometry(m_worldParameters->worldSize);
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildTerrestrialLayout(*terrestrialParameters, seed));
else if (auto asteroidsParameters = as<AsteroidsWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildAsteroidsLayout(*asteroidsParameters, seed));
else if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters))
m_layout = make_shared<WorldLayout>(WorldLayout::buildFloatingDungeonLayout(*floatingDungeonParameters, m_seed));
determineWorldName();
}
WorldTemplate::WorldTemplate(Json const& store) : WorldTemplate() {
m_celestialParameters = jsonToMaybe<CelestialParameters>(store.get("celestialParameters", {}));
m_worldParameters = diskLoadVisitableWorldParameters(store.get("worldParameters", {}));
m_skyParameters = SkyParameters(store.get("skyParameters"));
m_seed = store.getUInt("seed");
m_geometry = WorldGeometry(jsonToVec2U(store.get("size")));
if (auto regionData = store.opt("regionData"))
m_layout = make_shared<WorldLayout>(regionData.take());
m_customTerrainRegions = store.getArray("customTerrainRegions", JsonArray()).transformed([](Json const& config) {
CustomTerrainRegion ctr = {jsonToPolyF(config.get("region")), {}, config.getBool("solid")};
ctr.regionBounds = ctr.region.boundBox();
return ctr;
});
determineWorldName();
}
Json WorldTemplate::store() const {
return JsonObject{
{"celestialParameters", jsonFromMaybe(m_celestialParameters, mem_fn(&CelestialParameters::diskStore))},
{"worldParameters", diskStoreVisitableWorldParameters(m_worldParameters)},
{"skyParameters", m_skyParameters.toJson()},
{"seed", m_seed},
{"size", jsonFromVec2U(m_geometry.size())},
{"regionData", m_layout ? m_layout->toJson() : Json()},
{"customTerrainRegions", transform<JsonArray>(m_customTerrainRegions, [](CustomTerrainRegion const& region) {
return JsonObject{{"region", jsonFromPolyF(region.region)}, {"solid", region.solid}};
})}
};
}
Maybe<CelestialParameters> const& WorldTemplate::celestialParameters() const {
return m_celestialParameters;
}
VisitableWorldParametersConstPtr WorldTemplate::worldParameters() const {
return m_worldParameters;
}
SkyParameters WorldTemplate::skyParameters() const {
return m_skyParameters;
}
WorldLayoutPtr WorldTemplate::worldLayout() const {
return m_layout;
}
void WorldTemplate::setWorldParameters(VisitableWorldParametersPtr newParameters) {
m_worldParameters = take(newParameters);
}
void WorldTemplate::setWorldLayout(WorldLayoutPtr newLayout) {
m_layout = take(newLayout);
m_blockCache.clear();
}
void WorldTemplate::setSkyParameters(SkyParameters newParameters) {
m_skyParameters = take(newParameters);
}
uint64_t WorldTemplate::worldSeed() const {
return m_seed;
}
String WorldTemplate::worldName() const {
return m_worldName;
}
Vec2U WorldTemplate::size() const {
return m_geometry.size();
}
float WorldTemplate::surfaceLevel() const {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters))
return terrestrialParameters->surfaceLayer.layerBaseHeight;
return m_geometry.size()[1] / 2.0f;
}
float WorldTemplate::undergroundLevel() const {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters))
return terrestrialParameters->surfaceLayer.layerMinHeight;
else if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters))
return floatingDungeonParameters->dungeonUndergroundLevel;
return 0.0f;
}
bool WorldTemplate::inSurfaceLayer(Vec2I const& position) const {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
auto posLayerAndCell = m_layout->findLayerAndCell(position[0], position[1]);
auto surfaceLayerAndCell = m_layout->findLayerAndCell(position[0], terrestrialParameters->surfaceLayer.layerBaseHeight);
return posLayerAndCell.first == surfaceLayerAndCell.first;
}
return false;
}
Maybe<Vec2I> WorldTemplate::findSensiblePlayerStart() const {
if (!m_layout)
return {};
auto playerStartSearchRegions = m_layout->playerStartSearchRegions();
if (playerStartSearchRegions.empty())
return {};
int playerStartSearchTries = m_templateConfig.getInt("playerStartSearchTries");
int playerStartFreeBlocksRadius = m_templateConfig.getInt("playerStartFreeBlocksRadius");
int playerStartFreeBlocksHeight = m_templateConfig.getInt("playerStartFreeBlocksHeight");
for (int i = 0; i < playerStartSearchTries; ++i) {
RectI searchRegion = Random::randFrom(playerStartSearchRegions);
int x = Random::randInt(searchRegion.xMin(), searchRegion.xMax());
for (int y = searchRegion.yMax() - 1; y >= searchRegion.yMin(); --y) {
if (getBlockInfo(x, y).terrain && !getBlockInfo(x, y + 1).terrain) {
if (isOutside(RectI(x - playerStartFreeBlocksRadius, y + 1, x + playerStartFreeBlocksRadius, y + playerStartFreeBlocksHeight)))
return Vec2I(x, y + 1);
}
}
}
return {};
}
void WorldTemplate::addCustomTerrainRegion(PolyF poly) {
m_customTerrainRegions.append({poly, poly.boundBox(), true});
m_blockCache.clear();
}
void WorldTemplate::addCustomSpaceRegion(PolyF poly) {
m_customTerrainRegions.append({poly, poly.boundBox(), false});
m_blockCache.clear();
}
void WorldTemplate::clearCustomTerrains() {
m_customTerrainRegions.clear();
m_blockCache.clear();
}
List<RectI> WorldTemplate::previewAddBiomeRegion(Vec2I const& position, int width) {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
auto regionRects = m_layout->previewAddBiomeRegion(position, width);
regionRects.transform([terrestrialParameters](RectI const& rect) {
return rect.padded(ceil(terrestrialParameters->blendSize));
});
return regionRects;
} else {
Logger::error("Cannot add biome region to non-terrestrial world!");
// throw StarException("Cannot add biome region to non-terrestrial world!");
return List<RectI>();
}
}
List<RectI> WorldTemplate::previewExpandBiomeRegion(Vec2I const& position, int newWidth) {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
auto regionRects = m_layout->previewExpandBiomeRegion(position, newWidth);
regionRects.transform([terrestrialParameters](RectI const& rect) {
return rect.padded(ceil(terrestrialParameters->blendSize));
});
return regionRects;
} else {
Logger::error("Cannot expand biome region on non-terrestrial world!");
// throw StarException("Cannot expand biome region on non-terrestrial world!");
return List<RectI>();
}
}
void WorldTemplate::addBiomeRegion(Vec2I const& position, String const& biomeName, String const& subBlockSelector, int width) {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
m_layout->addBiomeRegion(*terrestrialParameters, m_seed, position, biomeName, subBlockSelector, width);
m_blockCache.clear();
} else {
Logger::error("Cannot add biome region to non-terrestrial world!");
// throw StarException("Cannot add biome region to non-terrestrial world!");
}
}
void WorldTemplate::expandBiomeRegion(Vec2I const& position, int newWidth) {
if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
m_layout->expandBiomeRegion(position, newWidth);
m_blockCache.clear();
} else {
Logger::error("Cannot expand biome region on non-terrestrial world!");
// throw StarException("Cannot expand biome region on non-terrestrial world!");
}
}
List<WorldTemplate::Dungeon> WorldTemplate::dungeons() const {
List<Dungeon> dungeonList;
if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters)) {
dungeonList.append({floatingDungeonParameters->primaryDungeon, floatingDungeonParameters->dungeonBaseHeight, 0, 0, true, false});
} else if (auto terrestrialParameters = as<TerrestrialWorldParameters>(m_worldParameters)) {
auto addLayerDungeons = [this, &dungeonList](TerrestrialWorldParameters::TerrestrialLayer const& layer) {
if (layer.dungeons.size() > 0) {
int dungeonSpacing = floor(m_geometry.width() / layer.dungeons.size());
uint32_t dungeonOffset = staticRandomU32Range(0, m_geometry.width(), m_seed, layer.layerBaseHeight);
for (auto const& dp : enumerateIterator(layer.dungeons)) {
dungeonList.append({dp.first, layer.layerBaseHeight, (int)dungeonOffset, layer.dungeonXVariance, false, true});
dungeonOffset = (dungeonOffset + dungeonSpacing) % m_geometry.width();
}
}
};
addLayerDungeons(terrestrialParameters->spaceLayer);
addLayerDungeons(terrestrialParameters->atmosphereLayer);
addLayerDungeons(terrestrialParameters->surfaceLayer);
addLayerDungeons(terrestrialParameters->subsurfaceLayer);
for (auto const& ul : terrestrialParameters->undergroundLayers)
addLayerDungeons(ul);
addLayerDungeons(terrestrialParameters->coreLayer);
}
return dungeonList;
}
WorldTemplate::BlockInfo WorldTemplate::blockInfo(int x, int y) const {
return getBlockInfo(m_geometry.xwrap(x), y);
}
WorldTemplate::BlockInfo WorldTemplate::blockBiomeInfo(int x, int y) const {
BlockInfo blockInfo;
if (!m_layout)
return blockInfo;
// The environment biome is calculated with weighting based on the flat coordinates.
List<WorldLayout::RegionWeighting> flatWeighting = m_layout->getWeighting(x, y);
int blendNoiseOffset = 0;
if (auto const& blendNoise = m_layout->blendNoise())
blendNoiseOffset = (int)blendNoise->get(x, y);
Vec2I blockPos;
List<WorldLayout::RegionWeighting> blockWeighting;
List<WorldLayout::RegionWeighting> transitionWeighting;
if (auto const& blockNoise = m_layout->blockNoise()) {
blockPos = blockNoise->apply(Vec2I(x, y), m_geometry.size());
blockWeighting = m_layout->getWeighting(blockPos[0] + blendNoiseOffset, blockPos[1]);
transitionWeighting = m_layout->getWeighting(blockPos[0], blockPos[1]);
} else {
blockPos = Vec2I(x, y);
blockWeighting = flatWeighting;
transitionWeighting = flatWeighting;
}
if (flatWeighting.empty() || blockWeighting.empty())
return blockInfo;
auto const& primaryFlatWeighting = flatWeighting.first();
auto const& primaryBlockWeighting = blockWeighting.first();
blockInfo.blockBiomeIndex = primaryBlockWeighting.region->blockBiomeIndex;
blockInfo.environmentBiomeIndex = primaryFlatWeighting.region->environmentBiomeIndex;
blockInfo.biomeTransition = transitionWeighting.first().weight < m_templateConfig.getFloat("biomeTransitionThreshold", 0);
if (auto blockBiome = biome(blockInfo.blockBiomeIndex)) {
if (!blockInfo.foregroundCave) {
blockInfo.foreground = blockBiome->mainBlock;
blockInfo.background = blockInfo.foreground;
} else if (!blockInfo.backgroundCave) {
blockInfo.background = blockBiome->mainBlock;
}
if (!primaryBlockWeighting.region->subBlockSelectorIndexes.empty()) {
for (size_t i = 0; i < blockBiome->subBlocks.size(); ++i) {
auto const& selector = m_layout->getTerrainSelector(primaryBlockWeighting.region->subBlockSelectorIndexes.at(i));
if (selector->get(primaryBlockWeighting.xValue - blendNoiseOffset, blockPos[1]) > 0.0f) {
if (!blockInfo.foregroundCave) {
blockInfo.foreground = blockBiome->subBlocks.at(i);
blockInfo.background = blockInfo.foreground;
} else if (!blockInfo.backgroundCave) {
blockInfo.background = blockBiome->subBlocks.at(i);
}
break;
}
}
}
}
return blockInfo;
}
bool WorldTemplate::isOutside(int x, int y) const {
return !getBlockInfo(m_geometry.xwrap(x), y).terrain;
}
bool WorldTemplate::isOutside(RectI const& region) const {
for (int x = region.xMin(); x < region.xMax(); ++x) {
for (int y = region.yMin(); y < region.yMax(); ++y) {
if (getBlockInfo(m_geometry.xwrap(x), y).terrain)
return false;
}
}
return true;
}
BiomeIndex WorldTemplate::blockBiomeIndex(int x, int y) const {
return getBlockInfo(m_geometry.xwrap(x), y).blockBiomeIndex;
}
BiomeIndex WorldTemplate::environmentBiomeIndex(int x, int y) const {
return getBlockInfo(m_geometry.xwrap(x), y).environmentBiomeIndex;
}
BiomeConstPtr WorldTemplate::biome(BiomeIndex biomeIndex) const {
if (!m_layout)
return {};
if (biomeIndex == NullBiomeIndex)
return {};
return m_layout->getBiome(biomeIndex);
}
BiomeConstPtr WorldTemplate::blockBiome(int x, int y) const {
return biome(blockBiomeIndex(m_geometry.xwrap(x), y));
}
BiomeConstPtr WorldTemplate::environmentBiome(int x, int y) const {
return biome(environmentBiomeIndex(m_geometry.xwrap(x), y));
}
MaterialHue WorldTemplate::biomeMaterialHueShift(BiomeIndex biomeIndex, MaterialId material) const {
if (m_layout && biomeIndex != NullBiomeIndex) {
auto const& biome = m_layout->getBiome(biomeIndex);
if (material == biome->mainBlock)
return biome->materialHueShift;
}
return MaterialHue();
}
MaterialHue WorldTemplate::biomeModHueShift(BiomeIndex biomeIndex, ModId mod) const {
if (m_layout && biomeIndex != NullBiomeIndex) {
auto const& biome = m_layout->getBiome(biomeIndex);
if (mod == biome->surfacePlaceables.grassMod || mod == biome->surfacePlaceables.ceilingGrassMod
|| mod == biome->undergroundPlaceables.grassMod
|| mod == biome->undergroundPlaceables.ceilingGrassMod)
return biome->materialHueShift;
}
return MaterialHue();
}
WeatherPool WorldTemplate::weathers() const {
if (m_worldParameters)
return m_worldParameters->weatherPool;
return {};
}
AmbientNoisesDescriptionPtr WorldTemplate::ambientNoises(int x, int y) const {
if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters)) {
if (floatingDungeonParameters->dayAmbientNoises || floatingDungeonParameters->nightAmbientNoises) {
auto dayTracks = floatingDungeonParameters->dayAmbientNoises
? AmbientTrackGroup(StringList{*floatingDungeonParameters->dayAmbientNoises})
: AmbientTrackGroup();
auto nightTracks = floatingDungeonParameters->nightAmbientNoises
? AmbientTrackGroup(StringList{*floatingDungeonParameters->nightAmbientNoises})
: AmbientTrackGroup();
return make_shared<AmbientNoisesDescription>(dayTracks, nightTracks);
}
}
if (auto biome = environmentBiome(x, y))
return biome->ambientNoises;
return {};
}
AmbientNoisesDescriptionPtr WorldTemplate::musicTrack(int x, int y) const {
if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters)) {
if (floatingDungeonParameters->dayMusicTrack || floatingDungeonParameters->nightMusicTrack) {
auto dayTracks = floatingDungeonParameters->dayMusicTrack
? AmbientTrackGroup(StringList{*floatingDungeonParameters->dayMusicTrack})
: AmbientTrackGroup();
auto nightTracks = floatingDungeonParameters->nightMusicTrack
? AmbientTrackGroup(StringList{*floatingDungeonParameters->nightMusicTrack})
: AmbientTrackGroup();
return make_shared<AmbientNoisesDescription>(dayTracks, nightTracks);
}
}
if (auto biome = environmentBiome(x, y))
return biome->musicTrack;
return {};
}
StringList WorldTemplate::environmentStatusEffects(int, int) const {
if (m_worldParameters)
return m_worldParameters->environmentStatusEffects;
return {};
}
bool WorldTemplate::breathable(int, int) const {
if (m_worldParameters)
return !m_worldParameters->airless;
return true;
}
void WorldTemplate::addPotentialBiomeItems(int x, int y, PotentialBiomeItems& items, List<BiomeItemDistribution> const& distributions, BiomePlacementArea area, Maybe<BiomePlacementMode> mode) const {
for (auto const& itemDistribution : distributions) {
auto placeMode = mode.value(itemDistribution.mode());
if (area == BiomePlacementArea::Surface) {
if (placeMode == itemDistribution.mode() && placeMode == BiomePlacementMode::Floor) {
if (auto itemToPlace = itemDistribution.itemToPlace(x, y))
items.surfaceBiomeItems.append(itemToPlace.take());
}
if (placeMode == itemDistribution.mode() && placeMode == BiomePlacementMode::Ocean) {
if (auto itemToPlace = itemDistribution.itemToPlace(x, y))
items.oceanItems.append(itemToPlace.take());
}
} else if (area == BiomePlacementArea::Underground) {
if (placeMode == itemDistribution.mode() && placeMode == BiomePlacementMode::Floor) {
if (auto itemToPlace = itemDistribution.itemToPlace(x, y))
items.caveSurfaceBiomeItems.append(itemToPlace.take());
}
if (placeMode == itemDistribution.mode() && placeMode == BiomePlacementMode::Ceiling) {
if (auto itemToPlace = itemDistribution.itemToPlace(x, y))
items.caveCeilingBiomeItems.append(itemToPlace.take());
}
if (placeMode == itemDistribution.mode() && placeMode == BiomePlacementMode::Background) {
if (auto itemToPlace = itemDistribution.itemToPlace(x, y))
items.caveBackgroundBiomeItems.append(itemToPlace.take());
}
}
}
}
WorldTemplate::PotentialBiomeItems WorldTemplate::potentialBiomeItemsAt(int x, int y) const {
if (!m_layout || y <= 0 || y >= (int)m_geometry.height() - 1)
return {};
x = m_geometry.xwrap(x);
auto blockBiome = [this](int x, int y) -> Biome const* {
BiomeIndex index = blockBiomeIndex(m_geometry.xwrap(x), y);
if (index == NullBiomeIndex)
return nullptr;
return m_layout->getBiome(index).get();
};
auto lowerBlockBiome = blockBiome(x, y - 1);
auto upperBlockBiome = blockBiome(x, y + 1);
auto thisBlockBiome = blockBiome(x, y);
PotentialBiomeItems potentialBiomeItems;
// surface floor, surface ocean
if (lowerBlockBiome)
addPotentialBiomeItems(x, y, potentialBiomeItems, lowerBlockBiome->surfacePlaceables.itemDistributions, BiomePlacementArea::Surface, BiomePlacementMode::Floor);
if (thisBlockBiome)
addPotentialBiomeItems(x, y, potentialBiomeItems, thisBlockBiome->surfacePlaceables.itemDistributions, BiomePlacementArea::Surface, BiomePlacementMode::Ocean);
// underground floor, ceiling, background
if (lowerBlockBiome)
addPotentialBiomeItems(x, y, potentialBiomeItems, lowerBlockBiome->undergroundPlaceables.itemDistributions, BiomePlacementArea::Underground, BiomePlacementMode::Floor);
if (upperBlockBiome)
addPotentialBiomeItems(x, y, potentialBiomeItems, upperBlockBiome->undergroundPlaceables.itemDistributions, BiomePlacementArea::Underground, BiomePlacementMode::Ceiling);
if (thisBlockBiome)
addPotentialBiomeItems(x, y, potentialBiomeItems, thisBlockBiome->undergroundPlaceables.itemDistributions, BiomePlacementArea::Underground, BiomePlacementMode::Background);
return potentialBiomeItems;
}
List<BiomeItemPlacement> WorldTemplate::validBiomeItems(int x, int y, PotentialBiomeItems potentialBiomeItems) const {
if (y <= 0 || y >= (int)m_geometry.height() - 1)
return {};
x = m_geometry.xwrap(x);
auto block = getBlockInfo(x, y);
if (block.biomeTransition)
return {};
List<BiomeItemPlacement> biomeItems;
auto blockAbove = getBlockInfo(x, y + 1);
auto blockBelow = getBlockInfo(x, y - 1);
if (!blockBelow.biomeTransition && blockBelow.terrain && !block.terrain && !blockBelow.foregroundCave)
biomeItems.appendAll(std::move(potentialBiomeItems.surfaceBiomeItems));
if (!blockBelow.biomeTransition && blockBelow.terrain && block.terrain && !blockBelow.foregroundCave && block.foregroundCave)
biomeItems.appendAll(std::move(potentialBiomeItems.caveSurfaceBiomeItems));
if (!blockAbove.biomeTransition && blockAbove.terrain && block.terrain && !blockAbove.foregroundCave && block.foregroundCave)
biomeItems.appendAll(std::move(potentialBiomeItems.caveCeilingBiomeItems));
if (block.terrain && block.foregroundCave && !block.backgroundCave)
biomeItems.appendAll(std::move(potentialBiomeItems.caveBackgroundBiomeItems));
if (block.oceanLiquid != EmptyLiquidId && y == block.oceanLiquidLevel)
biomeItems.appendAll(std::move(potentialBiomeItems.oceanItems));
return biomeItems;
}
float WorldTemplate::gravity() const {
if (m_worldParameters)
return m_worldParameters->gravity;
return m_templateConfig.getFloat("defaultGravity");
}
float WorldTemplate::threatLevel() const {
if (m_worldParameters)
return m_worldParameters->threatLevel;
return 0.0f;
}
uint64_t WorldTemplate::seedFor(int x, int y) const {
return staticRandomU64(m_seed, m_geometry.xwrap(x), y, "Block");
}
WorldTemplate::WorldTemplate() {
auto assets = Root::singleton().assets();
m_templateConfig = Root::singleton().assets()->json("/world_template.config");
m_customTerrainBlendSize = m_templateConfig.getFloat("customTerrainBlendSize");
m_customTerrainBlendWeight = m_templateConfig.getFloat("customTerrainBlendWeight");
m_blockCache.setMaxSize(m_templateConfig.getInt("blockCacheSize"));
m_geometry = Vec2U(2048, 2048);
m_seed = Random::randu64();
}
void WorldTemplate::determineWorldName() {
if (m_celestialParameters)
m_worldName = m_celestialParameters->name();
else if (auto floatingDungeonParameters = as<FloatingDungeonWorldParameters>(m_worldParameters))
m_worldName = Root::singleton().dungeonDefinitions()->get(floatingDungeonParameters->primaryDungeon)->displayName();
else
m_worldName = "";
}
pair<float, float> WorldTemplate::customTerrainWeighting(int x, int y) const {
auto assets = Root::singleton().assets();
float minimumDistance = highest<float>();
float finalSolidWeight = 0.0f;
float totalWeight = 0.0f;
for (auto const& region : m_customTerrainRegions) {
if (!m_geometry.rectContains(region.regionBounds.padded(m_customTerrainBlendSize), Vec2F(x, y)))
continue;
float distance = m_geometry.polyDistance(region.region, Vec2F(x, y));
if (distance >= m_customTerrainBlendSize)
continue;
float weight = 1.0f - distance / m_customTerrainBlendSize;
totalWeight += weight;
if (!region.solid)
weight *= -1.0f;
finalSolidWeight += weight;
minimumDistance = min(minimumDistance, distance);
}
if (minimumDistance > m_customTerrainBlendSize)
return {0.0f, 0.0f};
finalSolidWeight /= totalWeight;
return {finalSolidWeight * m_customTerrainBlendWeight, 1.0f - minimumDistance / m_customTerrainBlendSize};
}
WorldTemplate::BlockInfo WorldTemplate::getBlockInfo(uint32_t x, uint32_t y) const {
return m_blockCache.get(Vector<uint32_t, 2>(x, y), [this, x, y](Vector<uint32_t, 2>) {
BlockInfo blockInfo;
if (!m_layout)
return blockInfo;
// The environment biome is calculated with weighting based on the flat coordinates.
List<WorldLayout::RegionWeighting> flatWeighting = m_layout->getWeighting(x, y);
// The block biome is calculated optionally with higher frequency noise
// added to prevent straight lines appearing on the boundaries of
// regions.
int blendNoiseOffset = 0;
if (auto const& blendNoise = m_layout->blendNoise())
blendNoiseOffset = (int)blendNoise->get(x, y);
Vec2I blockPos;
List<WorldLayout::RegionWeighting> blockWeighting;
List<WorldLayout::RegionWeighting> transitionWeighting;
if (auto const& blockNoise = m_layout->blockNoise()) {
blockPos = blockNoise->apply(Vec2I(x, y), m_geometry.size());
blockWeighting = m_layout->getWeighting(blockPos[0] + blendNoiseOffset, blockPos[1]);
transitionWeighting = m_layout->getWeighting(blockPos[0], blockPos[1]);
} else {
blockPos = Vec2I(x, y);
blockWeighting = flatWeighting;
transitionWeighting = flatWeighting;
}
if (flatWeighting.empty() || blockWeighting.empty())
return blockInfo;
auto const& primaryFlatWeighting = flatWeighting.first();
auto const& primaryBlockWeighting = blockWeighting.first();
blockInfo.blockBiomeIndex = primaryBlockWeighting.region->blockBiomeIndex;
blockInfo.environmentBiomeIndex = primaryFlatWeighting.region->environmentBiomeIndex;
blockInfo.biomeTransition = transitionWeighting.first().weight < m_templateConfig.getFloat("biomeTransitionThreshold", 0);
float terrainSelect = 0.0f;
float foregroundCaveSelect = 0.0f;
float backgroundCaveSelect = 0.0f;
// Terrain weighting uses the flat weighting, and weights each selector
// to blend among them.
for (auto const& weighting : flatWeighting) {
if (weighting.region->terrainSelectorIndex != NullTerrainSelectorIndex) {
auto const& terrainSelector = m_layout->getTerrainSelector(weighting.region->terrainSelectorIndex);
float select = terrainSelector->get(weighting.xValue, y) * weighting.weight;
terrainSelect += select;
}
}
// This is a bit of a cheat. Since customTerrainWeighting is always flat,
// there are some odd effects that come from linearly interpolating from
// the generally non-flat terrain sources to flat regions of space. By
// using an interpolator that has an exaggerated S curve between the
// points, this hides some of these effects.
auto ctweighting = customTerrainWeighting(x, y);
terrainSelect = quintic2(ctweighting.second, terrainSelect, ctweighting.first);
if (terrainSelect > 0.0f) {
blockInfo.terrain = true;
for (auto const& weighting : flatWeighting) {
if (weighting.region->foregroundCaveSelectorIndex != NullTerrainSelectorIndex) {
auto const& foregroundCaveSelector = m_layout->getTerrainSelector(weighting.region->foregroundCaveSelectorIndex);
foregroundCaveSelect += foregroundCaveSelector->get(weighting.xValue, y) * weighting.weight;
}
if (weighting.region->backgroundCaveSelectorIndex != NullTerrainSelectorIndex) {
auto const& backgroundCaveSelector = m_layout->getTerrainSelector(weighting.region->backgroundCaveSelectorIndex);
backgroundCaveSelect += backgroundCaveSelector->get(weighting.xValue, y) * weighting.weight;
}
}
auto surfaceCaveAttenuationDist = m_templateConfig.getFloat("surfaceCaveAttenuationDist", 0);
if (terrainSelect < surfaceCaveAttenuationDist) {
auto surfaceCaveAttenuationFactor = m_templateConfig.getFloat("surfaceCaveAttenuationFactor", 1);
foregroundCaveSelect -= (surfaceCaveAttenuationDist - terrainSelect) * surfaceCaveAttenuationFactor;
backgroundCaveSelect -= (surfaceCaveAttenuationDist - terrainSelect) * surfaceCaveAttenuationFactor;
}
}
blockInfo.foregroundCave = foregroundCaveSelect > 0.0f;
blockInfo.backgroundCave = backgroundCaveSelect > 0.0f;
auto const& regionLiquids = primaryFlatWeighting.region->regionLiquids;
blockInfo.caveLiquid = regionLiquids.caveLiquid;
blockInfo.caveLiquidSeedDensity = regionLiquids.caveLiquidSeedDensity;
blockInfo.oceanLiquid = regionLiquids.oceanLiquid;
blockInfo.oceanLiquidLevel = regionLiquids.oceanLiquidLevel;
blockInfo.encloseLiquids = regionLiquids.encloseLiquids;
blockInfo.fillMicrodungeons = regionLiquids.fillMicrodungeons;
if (!blockInfo.terrain && blockInfo.encloseLiquids && (int)y < blockInfo.oceanLiquidLevel) {
blockInfo.terrain = true;
blockInfo.foregroundCave = true;
}
if (blockInfo.terrain) {
if (auto blockBiome = biome(blockInfo.blockBiomeIndex)) {
if (!blockInfo.foregroundCave) {
blockInfo.foreground = blockBiome->mainBlock;
blockInfo.background = blockInfo.foreground;
} else if (!blockInfo.backgroundCave) {
blockInfo.background = blockBiome->mainBlock;
}
// subBlock, foregroundOre, and backgroundOre selectors can be empty
// if they are not enabled, otherwise they will always have the
// correct count
if (!primaryBlockWeighting.region->subBlockSelectorIndexes.empty()) {
for (size_t i = 0; i < blockBiome->subBlocks.size(); ++i) {
auto const& selector = m_layout->getTerrainSelector(primaryBlockWeighting.region->subBlockSelectorIndexes.at(i));
if (selector->get(primaryBlockWeighting.xValue - blendNoiseOffset, blockPos[1]) > 0.0f) {
if (!blockInfo.foregroundCave) {
blockInfo.foreground = blockBiome->subBlocks.at(i);
blockInfo.background = blockInfo.foreground;
} else if (!blockInfo.backgroundCave) {
blockInfo.background = blockBiome->subBlocks.at(i);
}
break;
}
}
}
if (!blockInfo.foregroundCave && !primaryBlockWeighting.region->foregroundOreSelectorIndexes.empty()) {
for (size_t i = 0; i < blockBiome->ores.size(); ++i) {
auto const& selector = m_layout->getTerrainSelector(primaryBlockWeighting.region->foregroundOreSelectorIndexes.at(i));
if (selector->get(x, y) > 0.0f) {
blockInfo.foregroundMod = blockBiome->ores.at(i).first;
break;
}
}
}
if (!blockInfo.backgroundCave && !primaryBlockWeighting.region->backgroundOreSelectorIndexes.empty()) {
for (size_t i = 0; i < blockBiome->ores.size(); ++i) {
auto const& selector = m_layout->getTerrainSelector(primaryBlockWeighting.region->backgroundOreSelectorIndexes.at(i));
if (selector->get(x, y) > 0.0f) {
blockInfo.backgroundMod = blockBiome->ores.at(i).first;
break;
}
}
}
}
}
return blockInfo;
});
}
}
|