From da9897a11e9e799b476aefd2e0f7295e25bfd2df Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Wed, 15 Nov 2023 10:10:03 +1000 Subject: [PATCH] bean --- build.gradle | 1 + .../java/me/cortex/voxelmon/Voxelmon.java | 11 ++- .../me/cortex/voxelmon/core/VoxelCore.java | 4 +- .../voxelmon/core/world/WorldEngine.java | 4 +- .../terrain/SparseTerrainGenerator.java | 75 +++++++++++++++++++ .../terrain/TestSparseGenCommand.java | 30 ++++++++ src/main/resources/fabric.mod.json | 1 + 7 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/cortex/voxelmon/terrain/SparseTerrainGenerator.java create mode 100644 src/main/java/me/cortex/voxelmon/terrain/TestSparseGenCommand.java diff --git a/build.gradle b/build.gradle index 61d5c29e..d0373ff8 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,7 @@ dependencies { modImplementation(fabricApi.module("fabric-api-base", project.fabric_version)) modImplementation(fabricApi.module("fabric-rendering-fluids-v1", project.fabric_version)) modImplementation(fabricApi.module("fabric-resource-loader-v0", project.fabric_version)) + modImplementation(fabricApi.module("fabric-command-api-v2", project.fabric_version)) modImplementation("net.fabricmc.fabric-api:fabric-rendering-data-attachment-v1:0.3.38+73761d2e9a") modImplementation "maven.modrinth:sodium:mc1.20.2-0.5.3" diff --git a/src/main/java/me/cortex/voxelmon/Voxelmon.java b/src/main/java/me/cortex/voxelmon/Voxelmon.java index ade79f3b..0550f0c9 100644 --- a/src/main/java/me/cortex/voxelmon/Voxelmon.java +++ b/src/main/java/me/cortex/voxelmon/Voxelmon.java @@ -1,4 +1,13 @@ package me.cortex.voxelmon; -public class Voxelmon { +import me.cortex.voxelmon.terrain.TestSparseGenCommand; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; + +public class Voxelmon implements ClientModInitializer { + @Override + public void onInitializeClient() { + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> TestSparseGenCommand.register(dispatcher)); + } } diff --git a/src/main/java/me/cortex/voxelmon/core/VoxelCore.java b/src/main/java/me/cortex/voxelmon/core/VoxelCore.java index b23c8229..2d43fafd 100644 --- a/src/main/java/me/cortex/voxelmon/core/VoxelCore.java +++ b/src/main/java/me/cortex/voxelmon/core/VoxelCore.java @@ -57,7 +57,7 @@ public class VoxelCore { //Trigger the shared index buffer loading SharedIndexBuffer.INSTANCE.id(); this.renderer = new Gl46FarWorldRenderer(); - this.world = new WorldEngine(new File("ethoslab.db"), 16, 5);//"hc9.db"//"storagefile.db" + this.world = new WorldEngine(new File("storagefile.db"), 16, 5);//"hc9.db"//"storagefile.db" this.renderTracker = new RenderTracker(this.world, this.renderer); this.renderGen = new RenderGenerationService(this.world, this.renderTracker,4); @@ -90,7 +90,7 @@ public class VoxelCore { //WorldImporter importer = new WorldImporter(this.world, MinecraftClient.getInstance().world); - //importer.importWorldAsyncStart(new File("saves/Etho's LP Ep550/region")); + //importer.importWorldAsyncStart(new File("saves/New World/region")); Set biomeTintableAllFaces = new HashSet<>(List.of(Blocks.OAK_LEAVES, Blocks.JUNGLE_LEAVES, Blocks.ACACIA_LEAVES, Blocks.DARK_OAK_LEAVES, Blocks.VINE, Blocks.MANGROVE_LEAVES, Blocks.TALL_GRASS, Blocks.LARGE_FERN)); diff --git a/src/main/java/me/cortex/voxelmon/core/world/WorldEngine.java b/src/main/java/me/cortex/voxelmon/core/world/WorldEngine.java index 9d53d741..0c7f33bc 100644 --- a/src/main/java/me/cortex/voxelmon/core/world/WorldEngine.java +++ b/src/main/java/me/cortex/voxelmon/core/world/WorldEngine.java @@ -78,11 +78,13 @@ public class WorldEngine { public void tryUnload(WorldSection section) { synchronized (this.loadedSectionCache[section.lvl]) { if (section.getRefCount() != 0) { + section.assertNotFree(); return; } + //TODO: make a thing where it checks if the section is dirty, if it is, enqueue it for a save first and return - section.setFreed(); + section.setFreed();//TODO: FIXME THIS IS SOMEHOW FAILING var removedSection = this.loadedSectionCache[section.lvl].remove(section.getKey()); if (removedSection != section) { throw new IllegalStateException("Removed section not the same as attempted to remove"); diff --git a/src/main/java/me/cortex/voxelmon/terrain/SparseTerrainGenerator.java b/src/main/java/me/cortex/voxelmon/terrain/SparseTerrainGenerator.java new file mode 100644 index 00000000..15ba32b3 --- /dev/null +++ b/src/main/java/me/cortex/voxelmon/terrain/SparseTerrainGenerator.java @@ -0,0 +1,75 @@ +package me.cortex.voxelmon.terrain; + +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.util.math.ColumnPos; +import net.minecraft.world.biome.Biome; +import net.minecraft.world.biome.source.BiomeSource; +import net.minecraft.world.biome.source.util.MultiNoiseUtil; +import net.minecraft.world.gen.densityfunction.DensityFunction; +import net.minecraft.world.gen.noise.NoiseConfig; + +import java.util.List; + +public class SparseTerrainGenerator { + + private final DensityFunction initialDensity; + private final DensityFunction finalDensity; + private final BiomeSource biomeSource; + private final MultiNoiseUtil.MultiNoiseSampler biomeSampler; + public SparseTerrainGenerator(NoiseConfig config, BiomeSource biomeSource) { + this.biomeSource = biomeSource; + + var router = config.getNoiseRouter(); + this.initialDensity = router.initialDensityWithoutJaggedness(); + this.finalDensity = router.finalDensity(); + + this.biomeSampler = new MultiNoiseUtil.MultiNoiseSampler(router.temperature(), router.vegetation(), router.continents(), router.erosion(), router.depth(), router.ridges(), List.of()); + } + + public int getInitialHeight(int x, int z) { + int minHeight = -64; + int verticalCellBlockCount = 8; + int worldHeight = 384; + for(int y = minHeight + worldHeight; y >= minHeight; y -= verticalCellBlockCount) { + if (this.initialDensity.sample(new DensityFunction.UnblendedNoisePos(x, y, z)) > 0.390625) { + return y; + } + } + + return Integer.MAX_VALUE; + } + + public RegistryEntry getBiome(int bx, int by, int bz) { + return this.biomeSource.getBiome(bx, by, bz, this.biomeSampler); + } + + + + + private final class NoisePos implements DensityFunction.NoisePos { + private final int x; + private final int y; + private final int z; + + private NoisePos(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public int blockX() { + return this.x; + } + + @Override + public int blockY() { + return this.y; + } + + @Override + public int blockZ() { + return this.z; + } + } +} diff --git a/src/main/java/me/cortex/voxelmon/terrain/TestSparseGenCommand.java b/src/main/java/me/cortex/voxelmon/terrain/TestSparseGenCommand.java new file mode 100644 index 00000000..26a92f56 --- /dev/null +++ b/src/main/java/me/cortex/voxelmon/terrain/TestSparseGenCommand.java @@ -0,0 +1,30 @@ +package me.cortex.voxelmon.terrain; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.client.MinecraftClient; +import net.minecraft.server.command.ServerCommandSource; + +import static net.minecraft.server.command.CommandManager.literal; + +public class TestSparseGenCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(literal("testsparsegen") + .executes(ctx -> test())); + } + + private static int test() { + var world = MinecraftClient.getInstance().getServer().getWorld(MinecraftClient.getInstance().world.getRegistryKey()); + var gen = new SparseTerrainGenerator(world.getChunkManager().getNoiseConfig(), world.getChunkManager().getChunkGenerator().getBiomeSource()); + long s = System.currentTimeMillis(); + int c = 0; + for (int x = -50; x <= 50; x++) { + for (int z = -50; z <= 50; z++) { + var biome = gen.getBiome((x*16)>>2, 64>>2, (z*16)>>2); + int minHeight = gen.getInitialHeight(x*16, z*16); + c += minHeight; + } + } + System.err.println((System.currentTimeMillis()-s) + " e " + c); + return 0; + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index b5d54813..681f3375 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -11,6 +11,7 @@ "icon": "assets/voxelmon/icon.png", "environment": "client", "entrypoints": { + "client": ["me.cortex.voxelmon.Voxelmon"] }, "mixins": [ "voxelmon.mixins.json"