Skip to content

Commit

Permalink
Use cloned version of light data
Browse files Browse the repository at this point in the history
Not actually cloned yet since ClonedChunkSection doesn't
deep-copy
  • Loading branch information
embeddedt committed Jan 13, 2024
1 parent 49e338d commit d22e91e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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][];
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +32,8 @@ public class ClonedChunkSection {

private Biome[] biomeData;

private byte[][] lightData;

private long lastUsedTimestamp = Long.MAX_VALUE;

ClonedChunkSection(ClonedChunkSectionCache backingCache, World world) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit d22e91e

Please sign in to comment.