fix lighting

This commit is contained in:
Sen 2025-06-29 21:33:06 +02:00
parent 15459fc627
commit 5357dde0e3
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
3 changed files with 103 additions and 110 deletions

View file

@ -24,7 +24,7 @@ public class ChunkClient extends Chunk {
private void genHeights() { private void genHeights() {
int top = this.top; int top = this.top;
int bottom = this.bottom; int bottom = this.bottom < -64 ? -64 : this.bottom;
this.minHeight = Integer.MAX_VALUE; this.minHeight = Integer.MAX_VALUE;
for(int x = 0; x < 16; ++x) { for(int x = 0; x < 16; ++x) {

View file

@ -83,7 +83,7 @@ public abstract class Chunk {
public void genSkyLight() { public void genSkyLight() {
int top = this.top; int top = this.top;
int bottom = this.bottom; int bottom = this.bottom < -64 ? -64 : this.bottom;
this.minHeight = Integer.MAX_VALUE; this.minHeight = Integer.MAX_VALUE;
for(int x = 0; x < 16; ++x) { for(int x = 0; x < 16; ++x) {
@ -198,7 +198,7 @@ public abstract class Chunk {
private void relightBlock(int x, int y, int z) { private void relightBlock(int x, int y, int z) {
int h = this.height[z << 4 | x]; int h = this.height[z << 4 | x];
int min = this.bottom; int min = this.bottom < -64 ? -64 : this.bottom;
int cy = h; int cy = h;
if(y > h) { if(y > h) {
@ -412,9 +412,17 @@ public abstract class Chunk {
int y = pos.getY(); int y = pos.getY();
int z = pos.getZ() & 15; int z = pos.getZ() & 15;
BlockArray stor = this.getArray(y >> 4); BlockArray stor = this.getArray(y >> 4);
return stor == null ? (this.canSeeSky(pos) ? type.defValue : 0) if(type == LightType.SKY) {
: (type == LightType.SKY ? (this.world.dimension.hasNoLight() ? 0 : stor.getSky(x, y & 15, z)) int l = stor == null ? (this.canSeeSky(pos) ? type.defValue : 0) : (this.world.dimension.hasNoLight() ? 0 : stor.getSky(x, y & 15, z));
: (type == LightType.BLOCK ? stor.getLight(x, y & 15, z) : type.defValue)); if(y < 0) {
int max = y < -64 ? 0 : (y + 64) / 4;
l = l > max ? max : l;
}
return l;
}
if(stor == null)
return this.canSeeSky(pos) ? type.defValue : 0;
return type == LightType.BLOCK ? stor.getLight(x, y & 15, z) : type.defValue;
} }
public void setLight(LightType type, BlockPos pos, int value) { public void setLight(LightType type, BlockPos pos, int value) {
@ -446,22 +454,16 @@ public abstract class Chunk {
int y = pos.getY(); int y = pos.getY();
int z = pos.getZ() & 15; int z = pos.getZ() & 15;
BlockArray stor = this.getArray(y >> 4); BlockArray stor = this.getArray(y >> 4);
int l = stor == null ? LightType.SKY.defValue : (this.world.dimension.hasNoLight() ? 0 : stor.getSky(x, y & 15, z));
if(stor == null) { if(y < 0) {
return !this.world.dimension.hasNoLight() && amount < LightType.SKY.defValue int max = y < -64 ? 0 : (y + 64) / 4;
? LightType.SKY.defValue - amount : 0; l = l > max ? max : l;
}
else {
int l = this.world.dimension.hasNoLight() ? 0 : stor.getSky(x, y & 15, z);
l = l - amount;
int b = stor.getLight(x, y & 15, z);
if(b > l) {
l = b;
}
return l;
} }
if(stor == null)
return !this.world.dimension.hasNoLight() && amount < l ? l - amount : 0;
l = l - amount;
int b = stor.getLight(x, y & 15, z);
return b > l ? b : l;
} }
public void addEntity(Entity entity) { public void addEntity(Entity entity) {
@ -631,11 +633,11 @@ public abstract class Chunk {
if(loc.getY() == -99999999) { if(loc.getY() == -99999999) {
int y = this.top + 15; int y = this.top + 15;
int min = this.bottom; int min = this.bottom < -64 ? -64 : this.bottom;
loc = new BlockPos(pos.getX(), y, pos.getZ()); loc = new BlockPos(pos.getX(), y, pos.getZ());
int h = -1; int h = -99999999;
while(loc.getY() > min && h == -1) { while(loc.getY() > min && h == -99999999) {
Block block = this.getBlock(loc); Block block = this.getBlock(loc);
Material mat = block.getMaterial(); Material mat = block.getMaterial();
@ -796,7 +798,7 @@ public abstract class Chunk {
private boolean updateColumn(int x, int z) { private boolean updateColumn(int x, int z) {
int top = this.top; int top = this.top;
int bottom = this.bottom; int bottom = this.bottom < -64 ? -64 : this.bottom;
boolean opaque = false; boolean opaque = false;
boolean below = false; boolean below = false;
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos((this.xPos << 4) + x, 0, (this.zPos << 4) + z); BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos((this.xPos << 4) + x, 0, (this.zPos << 4) + z);

View file

@ -1601,7 +1601,7 @@ public abstract class World implements IWorldAccess {
private int getRawLight(BlockPos pos, LightType lightType) { private int getRawLight(BlockPos pos, LightType lightType) {
if(lightType == LightType.SKY && this.canSeeSky(pos)) { if(lightType == LightType.SKY && this.canSeeSky(pos)) {
return 15; return pos.getY() < 0 ? (pos.getY() < -64 ? 0 : (pos.getY() + 64) / 4) : 15;
} }
else { else {
Block block = this.getState(pos).getBlock(); Block block = this.getState(pos).getBlock();
@ -1641,119 +1641,110 @@ public abstract class World implements IWorldAccess {
} }
} }
public boolean checkLightFor(LightType lightType, BlockPos pos) { public boolean checkLightFor(LightType type, BlockPos pos) {
if(!this.isAreaLoaded(pos, 17, false)) { if(!this.isAreaLoaded(pos, 17, false))
return false; return false;
int done = 0;
int cnt = 0;
int light = this.getLightFor(type, pos);
int raw = this.getRawLight(pos, type);
int bx = pos.getX();
int by = pos.getY();
int bz = pos.getZ();
if(raw > light) {
this.lightUpdate[cnt++] = 133152;
} }
else { else if(raw < light) {
int i = 0; this.lightUpdate[cnt++] = 133152 | light << 18;
int j = 0;
// this.profiler.start("getBrightness");
int k = this.getLightFor(lightType, pos);
int l = this.getRawLight(pos, lightType);
int i1 = pos.getX();
int j1 = pos.getY();
int k1 = pos.getZ();
if(l > k) { while(done < cnt) {
this.lightUpdate[j++] = 133152; int p = this.lightUpdate[done++];
} int x = (p & 63) - 32 + bx;
else if(l < k) { int y = (p >> 6 & 63) - 32 + by;
this.lightUpdate[j++] = 133152 | k << 18; int z = (p >> 12 & 63) - 32 + bz;
int s = p >> 18 & 15;
BlockPos blk = new BlockPos(x, y, z);
int l = this.getLightFor(type, blk);
while(i < j) { if(l == s) {
int l1 = this.lightUpdate[i++]; this.setLightFor(type, blk, 0);
int i2 = (l1 & 63) - 32 + i1;
int j2 = (l1 >> 6 & 63) - 32 + j1;
int k2 = (l1 >> 12 & 63) - 32 + k1;
int l2 = l1 >> 18 & 15;
BlockPos blockpos = new BlockPos(i2, j2, k2);
int i3 = this.getLightFor(lightType, blockpos);
if(i3 == l2) { if(s > 0) {
this.setLightFor(lightType, blockpos, 0); int dx = ExtMath.absi(x - bx);
int dy = ExtMath.absi(y - by);
int dz = ExtMath.absi(z - bz);
if(l2 > 0) { if(dx + dy + dz < 17) {
int j3 = ExtMath.absi(i2 - i1); BlockPos.MutableBlockPos bpos = new BlockPos.MutableBlockPos();
int k3 = ExtMath.absi(j2 - j1);
int l3 = ExtMath.absi(k2 - k1);
if(j3 + k3 + l3 < 17) { for(Facing dir : Facing.values()) {
BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos(); int ox = x + dir.getFrontOffsetX();
int oy = y + dir.getFrontOffsetY();
int oz = z + dir.getFrontOffsetZ();
bpos.set(ox, oy, oz);
int op = Math.max(1, this.getState(bpos).getBlock().getLightOpacity());
l = this.getLightFor(type, bpos);
for(Facing enumfacing : Facing.values()) { if(l == s - op && cnt < this.lightUpdate.length) {
int i4 = i2 + enumfacing.getFrontOffsetX(); this.lightUpdate[cnt++] = ox - bx + 32 | oy - by + 32 << 6 | oz - bz + 32 << 12 | s - op << 18;
int j4 = j2 + enumfacing.getFrontOffsetY();
int k4 = k2 + enumfacing.getFrontOffsetZ();
blockpos$mutableblockpos.set(i4, j4, k4);
int l4 = Math.max(1, this.getState(blockpos$mutableblockpos).getBlock().getLightOpacity());
i3 = this.getLightFor(lightType, blockpos$mutableblockpos);
if(i3 == l2 - l4 && j < this.lightUpdate.length) {
this.lightUpdate[j++] = i4 - i1 + 32 | j4 - j1 + 32 << 6 | k4 - k1 + 32 << 12 | l2 - l4 << 18;
}
} }
} }
} }
} }
} }
i = 0;
} }
// this.profiler.end(); done = 0;
// this.profiler.start("checkedPosition < toCheckCount"); }
while(i < j) { while(done < cnt) {
int i5 = this.lightUpdate[i++]; int p = this.lightUpdate[done++];
int j5 = (i5 & 63) - 32 + i1; int x = (p & 63) - 32 + bx;
int k5 = (i5 >> 6 & 63) - 32 + j1; int y = (p >> 6 & 63) - 32 + by;
int l5 = (i5 >> 12 & 63) - 32 + k1; int z = (p >> 12 & 63) - 32 + bz;
BlockPos blockpos1 = new BlockPos(j5, k5, l5); BlockPos blk = new BlockPos(x, y, z);
int i6 = this.getLightFor(lightType, blockpos1); int l = this.getLightFor(type, blk);
int j6 = this.getRawLight(blockpos1, lightType); int r = this.getRawLight(blk, type);
if(j6 != i6) { if(r != l) {
this.setLightFor(lightType, blockpos1, j6); this.setLightFor(type, blk, r);
if(j6 > i6) { if(r > l) {
int k6 = Math.abs(j5 - i1); int k6 = Math.abs(x - bx);
int l6 = Math.abs(k5 - j1); int l6 = Math.abs(y - by);
int i7 = Math.abs(l5 - k1); int i7 = Math.abs(z - bz);
boolean flag = j < this.lightUpdate.length - 6; boolean flag = cnt < this.lightUpdate.length - 6;
if(k6 + l6 + i7 < 17 && flag) { if(k6 + l6 + i7 < 17 && flag) {
if(this.getLightFor(lightType, blockpos1.west()) < j6) { if(this.getLightFor(type, blk.west()) < r) {
this.lightUpdate[j++] = j5 - 1 - i1 + 32 + (k5 - j1 + 32 << 6) + (l5 - k1 + 32 << 12); this.lightUpdate[cnt++] = x - 1 - bx + 32 + (y - by + 32 << 6) + (z - bz + 32 << 12);
} }
if(this.getLightFor(lightType, blockpos1.east()) < j6) { if(this.getLightFor(type, blk.east()) < r) {
this.lightUpdate[j++] = j5 + 1 - i1 + 32 + (k5 - j1 + 32 << 6) + (l5 - k1 + 32 << 12); this.lightUpdate[cnt++] = x + 1 - bx + 32 + (y - by + 32 << 6) + (z - bz + 32 << 12);
} }
if(this.getLightFor(lightType, blockpos1.down()) < j6) { if(this.getLightFor(type, blk.down()) < r) {
this.lightUpdate[j++] = j5 - i1 + 32 + (k5 - 1 - j1 + 32 << 6) + (l5 - k1 + 32 << 12); this.lightUpdate[cnt++] = x - bx + 32 + (y - 1 - by + 32 << 6) + (z - bz + 32 << 12);
} }
if(this.getLightFor(lightType, blockpos1.up()) < j6) { if(this.getLightFor(type, blk.up()) < r) {
this.lightUpdate[j++] = j5 - i1 + 32 + (k5 + 1 - j1 + 32 << 6) + (l5 - k1 + 32 << 12); this.lightUpdate[cnt++] = x - bx + 32 + (y + 1 - by + 32 << 6) + (z - bz + 32 << 12);
} }
if(this.getLightFor(lightType, blockpos1.north()) < j6) { if(this.getLightFor(type, blk.north()) < r) {
this.lightUpdate[j++] = j5 - i1 + 32 + (k5 - j1 + 32 << 6) + (l5 - 1 - k1 + 32 << 12); this.lightUpdate[cnt++] = x - bx + 32 + (y - by + 32 << 6) + (z - 1 - bz + 32 << 12);
} }
if(this.getLightFor(lightType, blockpos1.south()) < j6) { if(this.getLightFor(type, blk.south()) < r) {
this.lightUpdate[j++] = j5 - i1 + 32 + (k5 - j1 + 32 << 6) + (l5 + 1 - k1 + 32 << 12); this.lightUpdate[cnt++] = x - bx + 32 + (y - by + 32 << 6) + (z + 1 - bz + 32 << 12);
}
} }
} }
} }
} }
// this.profiler.end();
return true;
} }
return true;
} }
public List<Entity> getEntitiesWithinAABBExcludingEntity(Entity entityIn, BoundingBox bb) { public List<Entity> getEntitiesWithinAABBExcludingEntity(Entity entityIn, BoundingBox bb) {