From d22e91edec8db532e68b6473585283fb33f9c6a4 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 13 Jan 2024 14:04:06 -0500 Subject: [PATCH] Use cloned version of light data Not actually cloned yet since ClonedChunkSection doesn't deep-copy --- .../mods/sodium/client/world/WorldSlice.java | 75 ++++++++++++++++++- .../world/cloned/ClonedChunkSection.java | 9 +++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java b/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java index e960414fc..c40bc17e0 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/world/WorldSlice.java @@ -14,6 +14,7 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; +import net.minecraft.world.EnumSkyBlock; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraft.world.WorldType; @@ -64,6 +65,9 @@ public class WorldSlice implements IBlockAccess { // The world this slice has copied data from private final World world; + private WorldType worldType; + private final int defaultSkyLightValue; + // Local Section->BlockState table. private final IBlockState[][] blockStatesArrays; @@ -93,7 +97,6 @@ public class WorldSlice implements IBlockAccess { // The volume that this slice contains private StructureBoundingBox volume; - private WorldType worldType; public static ChunkRenderContext prepare(World world, ChunkSectionPos origin, ClonedChunkSectionCache sectionCache) { Chunk chunk = world.getChunk(origin.getX(), origin.getZ()); @@ -139,6 +142,7 @@ public static ChunkRenderContext prepare(World world, ChunkSectionPos origin, Cl public WorldSlice(World world) { this.world = world; this.worldType = world.getWorldType(); + this.defaultSkyLightValue = this.world.provider.hasSkyLight() ? EnumSkyBlock.SKY.defaultLightValue : 0; this.sections = new ClonedChunkSection[SECTION_TABLE_ARRAY_SIZE]; this.blockStatesArrays = new IBlockState[SECTION_TABLE_ARRAY_SIZE][]; @@ -293,8 +297,71 @@ public TileEntity getBlockEntity(int x, int y, int z) { } @Override - public int getCombinedLight(BlockPos pos, int ambientDarkness) { - return this.world.getCombinedLight(pos, ambientDarkness); + public int getCombinedLight(BlockPos pos, int ambientLight) { + if (!blockBoxContains(this.volume, pos.getX(), pos.getY(), pos.getZ())) { + return (this.defaultSkyLightValue << 20) | (ambientLight << 4); + } + + int i = this.getLightFromNeighborsFor(EnumSkyBlock.SKY, pos); + int j = this.getLightFromNeighborsFor(EnumSkyBlock.BLOCK, pos); + + if (j < ambientLight) + { + j = ambientLight; + } + + return i << 20 | j << 4; + } + + private int getLightFor(EnumSkyBlock type, int relX, int relY, int relZ) { + ClonedChunkSection section = this.sections[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)]; + + return section.getLightLevel(relX & 15, relY & 15, relZ & 15, type); + } + + private int getLightFromNeighborsFor(EnumSkyBlock type, BlockPos pos) { + if(!this.world.provider.hasSkyLight() && type == EnumSkyBlock.SKY) { + return this.defaultSkyLightValue; + } + + int relX = pos.getX() - this.baseX; + int relY = pos.getY() - this.baseY; + int relZ = pos.getZ() - this.baseZ; + + IBlockState state = this.getBlockStateRelative(relX, relY, relZ); + + if(!state.useNeighborBrightness()) { + return getLightFor(type, relX, relY, relZ); + } else { + int west = getLightFor(type, relX - 1, relY, relZ); + int east = getLightFor(type, relX + 1, relY, relZ); + int up = getLightFor(type, relX, relY + 1, relZ); + int down = getLightFor(type, relX, relY - 1, relZ); + int north = getLightFor(type, relX, relY, relZ + 1); + int south = getLightFor(type, relX, relY, relZ - 1); + + if(east > west) { + west = east; + } + + if(up > west) { + west = up; + } + + if(down > west) { + west = down; + } + + if(north > west) { + west = north; + } + + if(south > west) { + west = south; + } + + return west; + } } @Override @@ -308,7 +375,7 @@ public Biome getBiome(BlockPos pos) { return section.getBiomeForNoiseGen(pos.getX() & 15, pos.getZ() & 15); } - return this.world.getBiomeForCoordsBody(pos); + return Biomes.PLAINS; } @Override diff --git a/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSection.java b/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSection.java index e4cee9d07..911f86d48 100644 --- a/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSection.java +++ b/src/main/java/me/jellysquid/mods/sodium/client/world/cloned/ClonedChunkSection.java @@ -6,9 +6,11 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.EnumSkyBlock; import net.minecraft.world.World; import net.minecraft.world.biome.Biome; import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.NibbleArray; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import net.minecraft.world.gen.structure.StructureBoundingBox; @@ -30,6 +32,8 @@ public class ClonedChunkSection { private Biome[] biomeData; + private byte[][] lightData; + private long lastUsedTimestamp = Long.MAX_VALUE; ClonedChunkSection(ClonedChunkSectionCache backingCache, World world) { @@ -94,6 +98,11 @@ public ChunkSectionPos getPosition() { return this.pos; } + public int getLightLevel(int x, int y, int z, EnumSkyBlock type) { + NibbleArray lightArray = type == EnumSkyBlock.BLOCK ? this.data.getBlockLight() : this.data.getSkyLight(); + return lightArray != null ? lightArray.get(x, y, z) : type.defaultLightValue; + } + private static ExtendedBlockStorage getChunkSection(Chunk chunk, ChunkSectionPos pos) { ExtendedBlockStorage section = null;