From 14483367099e4355bc390fda6b1b6f568c6aef7f Mon Sep 17 00:00:00 2001 From: paulevsGitch Date: Tue, 30 Nov 2021 13:55:21 +0300 Subject: [PATCH] Updated portal position code --- .../mixin/common/EndDragonFightMixin.java | 5 +- .../mixin/common/EndPodiumFeatureMixin.java | 43 ++++++----- .../mixin/common/SpikeFeatureMixin.java | 3 +- .../world/generator/GeneratorOptions.java | 9 --- .../world/generator/TerrainGenerator.java | 77 +++++++++++-------- 5 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/main/java/ru/betterend/mixin/common/EndDragonFightMixin.java b/src/main/java/ru/betterend/mixin/common/EndDragonFightMixin.java index dddd68de..a7ff667f 100644 --- a/src/main/java/ru/betterend/mixin/common/EndDragonFightMixin.java +++ b/src/main/java/ru/betterend/mixin/common/EndDragonFightMixin.java @@ -64,14 +64,11 @@ public class EndDragonFightMixin { else { LOGGER.debug("Found the exit portal & temporarily using it."); } - - blockPos = portalLocation; } List crystals = Lists.newArrayList(); - BlockPos center = GeneratorOptions.getPortalPos().above(5); for (Direction dir : BlocksHelper.HORIZONTAL) { - BlockPos central = center.relative(dir, 4); + BlockPos central = BlockPos.ZERO.relative(dir, 4); List crystalList = level.getEntitiesOfClass( EndCrystal.class, new AABB(central.below(255).south().west(), central.above(255).north().east()) diff --git a/src/main/java/ru/betterend/mixin/common/EndPodiumFeatureMixin.java b/src/main/java/ru/betterend/mixin/common/EndPodiumFeatureMixin.java index 5bc8cace..69720262 100644 --- a/src/main/java/ru/betterend/mixin/common/EndPodiumFeatureMixin.java +++ b/src/main/java/ru/betterend/mixin/common/EndPodiumFeatureMixin.java @@ -2,9 +2,9 @@ package ru.betterend.mixin.common; import net.minecraft.core.BlockPos; import net.minecraft.core.Vec3i; +import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.world.level.WorldGenLevel; -import net.minecraft.world.level.chunk.ChunkStatus; import net.minecraft.world.level.levelgen.Heightmap.Types; import net.minecraft.world.level.levelgen.feature.EndPodiumFeature; import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; @@ -27,6 +27,8 @@ import java.util.Random; @Mixin(EndPodiumFeature.class) public class EndPodiumFeatureMixin { + private static BlockPos be_portalPosition; + @Final @Shadow private boolean active; @@ -40,10 +42,10 @@ public class EndPodiumFeatureMixin { else if (GeneratorOptions.replacePortal()) { Random random = featurePlaceContext.random(); WorldGenLevel world = featurePlaceContext.level(); - BlockPos blockPos = be_updatePos(featurePlaceContext.origin(), world); + BlockPos blockPos = be_updatePortalPos(world); StructureTemplate structure = StructureHelper.readStructure(BetterEnd.makeID(active ? "portal/end_portal_active" : "portal/end_portal_inactive")); Vec3i size = structure.getSize(); - blockPos = blockPos.offset(-(size.getX() >> 1), -1, -(size.getZ() >> 1)); + blockPos = blockPos.offset(-(size.getX() >> 1), -3, -(size.getZ() >> 1)); structure.placeInWorld(world, blockPos, blockPos, new StructurePlaceSettings(), random, 2); info.setReturnValue(true); info.cancel(); @@ -53,8 +55,8 @@ public class EndPodiumFeatureMixin { @ModifyVariable(method = "place", ordinal = 0, at = @At("HEAD")) private FeaturePlaceContext be_setPosOnGround(FeaturePlaceContext featurePlaceContext) { WorldGenLevel world = featurePlaceContext.level(); - BlockPos pos = be_updatePos(featurePlaceContext.origin(), world); - return new FeaturePlaceContext( + BlockPos pos = be_updatePortalPos(world); + return new FeaturePlaceContext<>( world, featurePlaceContext.chunkGenerator(), featurePlaceContext.random(), @@ -63,21 +65,24 @@ public class EndPodiumFeatureMixin { ); } - private BlockPos be_updatePos(BlockPos blockPos, WorldGenLevel world) { - if (GeneratorOptions.useNewGenerator()) { - BlockPos pos = GeneratorOptions.getPortalPos(); - if (pos.equals(BlockPos.ZERO)) { - int y = world.getChunk(0, 0, ChunkStatus.FULL).getHeight(Types.WORLD_SURFACE, blockPos.getX(), blockPos.getZ()); - if (y < 1) { - y = 65; - } - pos = new BlockPos(pos.getX(), y, pos.getZ()); - GeneratorOptions.setPortalPos(pos); - WorldDataAPI.getRootTag(BetterEnd.MOD_ID).put("portal", NbtUtils.writeBlockPos(pos)); - WorldDataAPI.saveFile(BetterEnd.MOD_ID); + private BlockPos be_updatePortalPos(WorldGenLevel world) { + CompoundTag compound = WorldDataAPI.getRootTag(BetterEnd.MOD_ID).getCompound("portal"); + be_portalPosition = NbtUtils.readBlockPos(compound); + + if (be_portalPosition.getY() == 0) { + /*if (GeneratorOptions.useNewGenerator()) { + int y = TerrainGenerator.getHeight(0, 0, world.getLevel().getChunkSource().getGenerator().getBiomeSource()); + be_portalPosition = new BlockPos(0, y, 0); } - return pos; + else { + be_portalPosition = new BlockPos(0, 65, 0); + }*/ + int y = world.getHeight(Types.WORLD_SURFACE, 0, 0); + be_portalPosition = new BlockPos(0, y, 0); + WorldDataAPI.getRootTag(BetterEnd.MOD_ID).put("portal", NbtUtils.writeBlockPos(be_portalPosition)); + WorldDataAPI.saveFile(BetterEnd.MOD_ID); } - return blockPos; + + return be_portalPosition; } } diff --git a/src/main/java/ru/betterend/mixin/common/SpikeFeatureMixin.java b/src/main/java/ru/betterend/mixin/common/SpikeFeatureMixin.java index d1a779f4..b42926c9 100644 --- a/src/main/java/ru/betterend/mixin/common/SpikeFeatureMixin.java +++ b/src/main/java/ru/betterend/mixin/common/SpikeFeatureMixin.java @@ -52,8 +52,7 @@ public class SpikeFeatureMixin { String pillarID = String.format("%d_%d", x, z); CompoundTag pillar = WorldDataAPI.getCompoundTag(BetterEnd.MOD_ID, "pillars"); boolean haveValue = pillar.contains(pillarID); - minY = haveValue ? pillar.getInt(pillarID) : world.getChunk(x >> 4, z >> 4) - .getHeight(Types.WORLD_SURFACE, x & 15, z); + minY = haveValue ? pillar.getInt(pillarID) : world.getChunk(x >> 4, z >> 4).getHeight(Types.WORLD_SURFACE, x & 15, z); if (!haveValue) { pillar.putInt(pillarID, minY); WorldDataAPI.saveFile(BetterEnd.MOD_ID); diff --git a/src/main/java/ru/betterend/world/generator/GeneratorOptions.java b/src/main/java/ru/betterend/world/generator/GeneratorOptions.java index cb4fc8c5..4e404a6f 100644 --- a/src/main/java/ru/betterend/world/generator/GeneratorOptions.java +++ b/src/main/java/ru/betterend/world/generator/GeneratorOptions.java @@ -20,7 +20,6 @@ public class GeneratorOptions { public static LayerOptions smallOptions; private static boolean changeSpawn; private static BlockPos spawn; - private static BlockPos portal = BlockPos.ZERO; private static boolean replacePortal; private static boolean replacePillars; private static long islandDistBlock; @@ -130,14 +129,6 @@ public class GeneratorOptions { return spawn; } - public static BlockPos getPortalPos() { - return portal; - } - - public static void setPortalPos(BlockPos portal) { - GeneratorOptions.portal = portal; - } - public static boolean replacePortal() { return replacePortal; } diff --git a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java index 7cbe7599..e013b896 100644 --- a/src/main/java/ru/betterend/world/generator/TerrainGenerator.java +++ b/src/main/java/ru/betterend/world/generator/TerrainGenerator.java @@ -173,45 +173,56 @@ public class TerrainGenerator { * @param x - block pos x * @param z - block pos z */ - public static int getHeight(int x, int z) { + public static int getHeight(int x, int z, BiomeSource biomeSource) { + int posX = (x >> 3) << 3; + int posZ = (z >> 3) << 3; + float dx = (x - posX) / 8.0F; + float dz = (z - posZ) / 8.0F; + double[][][] buffer = new double[2][2][32]; + LOCKER.lock(); + for (int i = 0; i < 4; i++) { + int ix = i & 1; + int iz = i >> 1; + int px = ((ix << 3) + posX) >> 3; + int pz = ((iz << 3) + posZ) >> 3; + fillTerrainDensity(buffer[ix][iz], px, pz, biomeSource); + } + LOCKER.unlock(); - double px = (double) x / 8.0; - double pz = (double) z / 8.0; - - double distortion1 = noise1.eval(px * 0.1, pz * 0.1) * 20 + noise2.eval(px * 0.2, pz * 0.2) * 10 + noise1.eval( - px * 0.4, - pz * 0.4 - ) * 5; - double distortion2 = noise2.eval(px * 0.1, pz * 0.1) * 20 + noise1.eval(px * 0.2, pz * 0.2) * 10 + noise2.eval( - px * 0.4, - pz * 0.4 - ) * 5; - px = (double) x * SCALE_XZ + distortion1; - pz = (double) z * SCALE_XZ + distortion2; - - largeIslands.updatePositions(px, pz); - mediumIslands.updatePositions(px, pz); - smallIslands.updatePositions(px, pz); - - for (int y = 32; y >= 0; y--) { - double py = (double) y * SCALE_Y; - float dist = largeIslands.getDensity(px, py, pz); - dist = dist > 1 ? dist : MHelper.max(dist, mediumIslands.getDensity(px, py, pz)); - dist = dist > 1 ? dist : MHelper.max(dist, smallIslands.getDensity(px, py, pz)); - if (dist > -0.5F) { - dist += noise1.eval(px * 0.01, py * 0.01, pz * 0.01) * 0.02 + 0.02; - dist += noise2.eval(px * 0.05, py * 0.05, pz * 0.05) * 0.01 + 0.01; - dist += noise1.eval(px * 0.1, py * 0.1, pz * 0.1) * 0.005 + 0.005; + for (int j = 30; j >= 0; j--) { + float a = (float) buffer[0][0][j]; + float b = (float) buffer[1][0][j]; + float c = (float) buffer[0][1][j]; + float d = (float) buffer[1][1][j]; + + float e = (float) buffer[0][0][j + 1]; + float f = (float) buffer[1][0][j + 1]; + float g = (float) buffer[0][1][j + 1]; + float h = (float) buffer[1][1][j + 1]; + + if (a < 0 && b < 0 && c < 0 && d < 0 && e < 0 && f < 0 && g < 0 && h < 0) { + continue; } - if (dist > 0) { - LOCKER.unlock(); - return Mth.floor(Mth.clamp(y + dist, y, y + 1) * SCALE_Y); + + a = Mth.lerp(dx, a, b); + b = Mth.lerp(dx, c, d); + c = Mth.lerp(dx, e, f); + d = Mth.lerp(dx, g, h); + + a = Mth.lerp(dz, a, b); + b = Mth.lerp(dz, c, d); + + for (int n = 7; n >= 0; n--) { + float dy = n / 8.0F; + float dens = Mth.lerp(dy, a, b); + if (dens > 0) { + return (j << 3 | n) + 1; + } } } - LOCKER.unlock(); - return 0; + return -256; } static {