Ext collision box chunk translation optimization

Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
Joshua Castle 2023-05-12 20:05:13 -07:00
parent 8b8cc5bd33
commit bbd297c5d9
No known key found for this signature in database
GPG key ID: F674F38216C35D5D

View file

@ -81,6 +81,7 @@ import static org.geysermc.geyser.util.ChunkUtils.indexYZXtoXZY;
@Translator(packet = ClientboundLevelChunkWithLightPacket.class) @Translator(packet = ClientboundLevelChunkWithLightPacket.class)
public class JavaLevelChunkWithLightTranslator extends PacketTranslator<ClientboundLevelChunkWithLightPacket> { public class JavaLevelChunkWithLightTranslator extends PacketTranslator<ClientboundLevelChunkWithLightPacket> {
private static final ThreadLocal<ExtendedCollisionsStorage> EXTENDED_COLLISIONS_STORAGE = ThreadLocal.withInitial(ExtendedCollisionsStorage::new);
@Override @Override
public void translate(GeyserSession session, ClientboundLevelChunkWithLightPacket packet) { public void translate(GeyserSession session, ClientboundLevelChunkWithLightPacket packet) {
@ -113,7 +114,6 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
try { try {
ByteBuf in = Unpooled.wrappedBuffer(packet.getChunkData()); ByteBuf in = Unpooled.wrappedBuffer(packet.getChunkData());
int[] runningSectionExtendedCollisions = new int[BlockStorage.SIZE];
boolean extendedCollisionNextSection = false; boolean extendedCollisionNextSection = false;
for (int sectionY = 0; sectionY < chunkSize; sectionY++) { for (int sectionY = 0; sectionY < chunkSize; sectionY++) {
ChunkSection javaSection = session.getDownstream().getCodecHelper().readChunkSection(in, biomeGlobalPalette); ChunkSection javaSection = session.getDownstream().getCodecHelper().readChunkSection(in, biomeGlobalPalette);
@ -151,14 +151,14 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
// Extended collision blocks // Extended collision blocks
if (!session.getBlockMappings().getExtendedCollisionBoxes().isEmpty()) { if (!session.getBlockMappings().getExtendedCollisionBoxes().isEmpty()) {
if (javaId == BlockStateValues.JAVA_AIR_ID && runningSectionExtendedCollisions[yzx] != 0) { if (javaId == BlockStateValues.JAVA_AIR_ID && EXTENDED_COLLISIONS_STORAGE.get().get(yzx) != 0) {
section.getBlockStorageArray()[0].setFullBlock(xzy, runningSectionExtendedCollisions[yzx]); section.getBlockStorageArray()[0].setFullBlock(xzy, EXTENDED_COLLISIONS_STORAGE.get().get(yzx));
runningSectionExtendedCollisions[yzx] = 0; EXTENDED_COLLISIONS_STORAGE.get().set(yzx, 0);
continue; continue;
} }
BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes().get(javaId); BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes().get(javaId);
if (aboveBedrockExtendedCollisionDefinition != null) { if (aboveBedrockExtendedCollisionDefinition != null) {
runningSectionExtendedCollisions[((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8))] = aboveBedrockExtendedCollisionDefinition.getRuntimeId(); EXTENDED_COLLISIONS_STORAGE.get().set(((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8)), aboveBedrockExtendedCollisionDefinition.getRuntimeId());
if ((xzy & 0xF) == 15) { if ((xzy & 0xF) == 15) {
thisExtendedCollisionNextSection = true; thisExtendedCollisionNextSection = true;
} }
@ -279,15 +279,15 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
int xzy = indexYZXtoXZY(yzx); int xzy = indexYZXtoXZY(yzx);
bedrockData.set(xzy, paletteId); bedrockData.set(xzy, paletteId);
if (paletteId == airPaletteId && runningSectionExtendedCollisions[yzx] != 0) { if (paletteId == airPaletteId && EXTENDED_COLLISIONS_STORAGE.get().get(yzx) != 0) {
bedrockData.set(xzy, layer0.idFor(runningSectionExtendedCollisions[yzx])); bedrockData.set(xzy, layer0.idFor(EXTENDED_COLLISIONS_STORAGE.get().get(yzx)));
runningSectionExtendedCollisions[yzx] = 0; EXTENDED_COLLISIONS_STORAGE.get().set(yzx, 0);
continue; continue;
} }
BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings() BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings()
.getExtendedCollisionBoxes().get(javaPalette.idToState(paletteId)); .getExtendedCollisionBoxes().get(javaPalette.idToState(paletteId));
if (aboveBedrockExtendedCollisionDefinition != null) { if (aboveBedrockExtendedCollisionDefinition != null) {
runningSectionExtendedCollisions[((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8))] = aboveBedrockExtendedCollisionDefinition.getRuntimeId(); EXTENDED_COLLISIONS_STORAGE.get().set(((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8)), aboveBedrockExtendedCollisionDefinition.getRuntimeId());
if ((xzy & 0xF) == 15) { if ((xzy & 0xF) == 15) {
thisExtendedCollisionNextSection = true; thisExtendedCollisionNextSection = true;
} }
@ -306,15 +306,15 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
layer1Data[xzy >> 5] |= 1 << (xzy & 0x1F); layer1Data[xzy >> 5] |= 1 << (xzy & 0x1F);
} }
if (paletteId == airPaletteId && runningSectionExtendedCollisions[yzx] != 0) { if (paletteId == airPaletteId && EXTENDED_COLLISIONS_STORAGE.get().get(yzx) != 0) {
bedrockData.set(xzy, layer0.idFor(runningSectionExtendedCollisions[yzx])); bedrockData.set(xzy, layer0.idFor(EXTENDED_COLLISIONS_STORAGE.get().get(yzx)));
runningSectionExtendedCollisions[yzx] = 0; EXTENDED_COLLISIONS_STORAGE.get().set(yzx, 0);
continue; continue;
} }
BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes() BlockDefinition aboveBedrockExtendedCollisionDefinition = session.getBlockMappings().getExtendedCollisionBoxes()
.get(javaPalette.idToState(paletteId)); .get(javaPalette.idToState(paletteId));
if (aboveBedrockExtendedCollisionDefinition != null) { if (aboveBedrockExtendedCollisionDefinition != null) {
runningSectionExtendedCollisions[((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8))] = aboveBedrockExtendedCollisionDefinition.getRuntimeId(); EXTENDED_COLLISIONS_STORAGE.get().set(((yzx & 0x0ff) | (((yzx >> 8) + ((xzy & 0xF) < 15 ? 1 : -15)) << 8)), aboveBedrockExtendedCollisionDefinition.getRuntimeId());
if ((xzy & 0xF) == 15) { if ((xzy & 0xF) == 15) {
thisExtendedCollisionNextSection = true; thisExtendedCollisionNextSection = true;
} }
@ -482,3 +482,23 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
} }
} }
} }
class ExtendedCollisionsStorage {
private int[] data;
public int get(int index) {
ensureDataExists();
return data[index];
}
public void set(int index, int value) {
ensureDataExists();
data[index] = value;
}
private void ensureDataExists() {
if (data == null) {
data = new int[BlockStorage.SIZE];
}
}
}