Archived
0

уточнение алгоритма сериализации чанков (ChunkDataPacket)

This commit is contained in:
2018-08-16 11:40:04 +03:00
parent 54992d8b59
commit e17acb812b
7 changed files with 95 additions and 26 deletions

View File

@@ -17,6 +17,8 @@ import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
@@ -77,26 +79,74 @@ public class ChunkDataPacket implements SCPacket {
private int z;
@Setter
private boolean initChunk = true; // "Ground-Up Continuous"
@Setter
private Chunk chunk;
private List<ChunkSection> sectionList;
private int serializeBlockState(BlockType blockType) {
return (blockType.getId() << 4) | blockType.getMeta();
}
public void setChunk(Chunk chunk) {
this.sectionList = null;
this.chunk = chunk;
}
public void setChunkSectionList(List<ChunkSection> sectionList) {
this.chunk = null;
this.sectionList = sectionList;
}
@Override
public void writeSelf(NetOutputStream netStream) {
netStream.writeInt(x); // Chunk X
netStream.writeInt(z); // Chunk Y
netStream.writeBoolean(initChunk); // Init Chunk
netStream.writeVarInt(0b00000001); // Primary Bit Mask
if (sectionList == null && chunk != null) {
int bitMask = 0;
for (int h = 15; h >= 0; h--) {
bitMask = bitMask << 1;
ChunkSection chunkSection = chunk.getChunkSection(h);
if (chunkSection != null && chunkSection.getY() == h) {
bitMask |= 0x01;
} else {
bitMask |= 0x00;
}
}
netStream.writeVarInt(bitMask); // Primary Bit Mask
} else if (sectionList != null && chunk == null) {
sectionList.sort(Comparator.comparingInt(ChunkSection::getY));
int bitMask = 0;
for (int h = 15, i = 0; h >= 0; h--) {
bitMask = bitMask << 1;
ChunkSection chunkSection = sectionList.get(i);
if (chunkSection != null && chunkSection.getY() == h) {
bitMask |= 0x01;
} else {
bitMask |= 0x00;
}
}
netStream.writeVarInt(bitMask); // Primary Bit Mask
} else {
log.warn("Empty chunk data");
return;
}
final ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
int dataItems = 0;
final int airBlockPalette = serializeBlockState(BlockType.AIR);
for (int h = 0; h < 1/*потому что у нас пока только единичная сейция*/; h++) {
ChunkSection chunkSection = chunk.getChunkSection(h);
for (int h = 0; h < 16; h++) {
ChunkSection chunkSection = null;
if (chunk != null) {
chunkSection = chunk.getChunkSection(h);
} else if (sectionList != null) {
chunkSection = sectionList.remove(0);
}
if (chunkSection == null) {
continue;
}
@@ -156,7 +206,7 @@ public class ChunkDataPacket implements SCPacket {
}
if (!biomeFinally) {
biomes.writeByte(chunk.getBiome(x, z).getId());
biomes.writeByte(chunkSection.getBiome(x, z).getId());
if (x == 15 && z == 15) {
biomeFinally = true;
}

View File

@@ -31,6 +31,11 @@ public class DummyWorld implements World {
public void setAddition(int x, int y, int z, int value) {
}
@Override
public Biome getBiome(int localX, int localZ) {
return Biome.PLAINS;
}
@Override
public int getX() {
return 0;