Created
January 22, 2024 12:51
-
-
Save hYdos/57c41772c045350adb9c0cdf41056ff4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.hydos.naclmon.world.gen; | |
import me.hydos.naclmon.world.NaclmonBiomes; | |
import me.hydos.naclmon.world.gen.noise.IslandRegionNoise; | |
import me.hydos.naclmon.world.gen.noise.OpenSimplexNoise; | |
import net.minecraft.core.Holder; | |
import net.minecraft.resources.ResourceLocation; | |
import net.minecraft.util.RandomSource; | |
import net.minecraft.world.level.biome.Biome; | |
import net.minecraft.world.level.chunk.ChunkAccess; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
public class RegionGenerator { | |
private final Holder<Biome> ocean; | |
private final OpenSimplexNoise oceanNoise; | |
private final IslandRegionNoise islandNoise; | |
private final int seaLevel; | |
private final RegionBiomeSource biomeSource; | |
public RegionGenerator(RandomSource random, RegionBiomeSource biomeSource, Holder<Biome> ocean, int seaLevel) { | |
this.biomeSource = biomeSource; | |
this.ocean = ocean; | |
this.seaLevel = seaLevel; | |
this.oceanNoise = new OpenSimplexNoise(random); | |
this.islandNoise = new IslandRegionNoise(random); | |
} | |
public int getHeightAt(int x, int z, ChunkAccess chunk) { | |
var biomes = getSurroundingBiomeWeights(x, z); | |
var combinedHeight = 0; | |
var heightsCombined = 0; | |
for (var entry : biomes.entrySet()) { | |
var sample = oceanNoise.sample(x * 0.0001, z * 0.0001) * 3; | |
var biome = entry.getKey(); | |
if (NaclmonBiomes.ISLAND_FOREST.equals(biome)) sample += islandNoise.sampleForest(x, z); | |
if (NaclmonBiomes.ISLAND_CLEARING.equals(biome)) sample += islandNoise.sampleClearing(x, z); | |
var heightOffset = isChunkOcean(chunk) ? -30 : -10; | |
var overallHeight = (int) (seaLevel + heightOffset + sample); | |
combinedHeight += overallHeight * entry.getValue(); | |
heightsCombined += entry.getValue(); | |
} | |
return combinedHeight / heightsCombined; | |
} | |
private Map<ResourceLocation, Integer> getSurroundingBiomeWeights(int x, int z) { | |
var biomeDistribution = new ConcurrentHashMap<ResourceLocation, Integer>(); | |
for (var checkX = -3; checkX < 3; checkX += 3) { | |
for (var checkZ = -3; checkZ < 3; checkZ += 3) { | |
var biome = biomeSource.getNoiseBiome(x + checkX, 0, z + checkZ, null).unwrapKey().orElseThrow().location(); | |
biomeDistribution.putIfAbsent(biome, 0); | |
biomeDistribution.put(biome, biomeDistribution.get(biome) + 1); | |
} | |
} | |
return biomeDistribution; | |
} | |
private boolean isChunkOcean(ChunkAccess chunk) { | |
var oceanId = this.ocean.unwrapKey().orElseThrow(); | |
var chunkX = ((chunk.getPos().x) << 4); | |
var chunkZ = ((chunk.getPos().z) << 4); | |
for (var checkX = 0; checkX < 16; checkX++) { | |
for (var checkZ = 0; checkZ < 16; checkZ++) { | |
var isOcean = chunk.getNoiseBiome(chunkX + checkX, 0, chunkZ + checkZ).is(oceanId); | |
if (isOcean) return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment