Finish support for 10 bit blocks

This commit is contained in:
UnknownShadow200 2018-03-08 10:32:44 +11:00
parent c4d43a76da
commit 8010538a05
4 changed files with 76 additions and 15 deletions

View File

@ -95,7 +95,7 @@ namespace MCGalaxy.Drawing {
public void Set(BlockID block, int index) {
if (block >= Block.Extended) {
#if TEN_BIT_BLOCKS
blocks[index] = Block.ExtendedClass[b.Block >> Block.ExtendedShift];
blocks[index] = Block.ExtendedClass[block >> Block.ExtendedShift];
#else
blocks[index] = Block.custom_block;
#endif

View File

@ -71,27 +71,34 @@ namespace MCGalaxy.Network {
}
void SendLevel() {
byte[] bulk = null, normal = null, noBlockDefs = null, original = null;
byte[] bulk = null, normal = null, noBlockDefs = null, original = null, ext = null;
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) {
if (p.level != level) continue;
byte[] packet = MakePacket(p, ref bulk, ref normal,
ref noBlockDefs, ref original);
ref noBlockDefs, ref original, ref ext);
p.Socket.SendLowPriority(packet);
}
}
void SendPlayer() {
byte[] bulk = null, normal = null, noBlockDefs = null, original = null;
byte[] bulk = null, normal = null, noBlockDefs = null, original = null, ext = null;
byte[] packet = MakePacket(player, ref bulk, ref normal,
ref noBlockDefs, ref original);
ref noBlockDefs, ref original, ref ext);
player.Socket.SendLowPriority(packet);
}
#region Packet construction
byte[] MakePacket(Player p, ref byte[] bulk, ref byte[] normal,
ref byte[] noBlockDefs, ref byte[] original) {
ref byte[] noBlockDefs, ref byte[] original, ref byte[] ext) {
#if TEN_BIT_BLOCKS
if (p.hasExtBlocks) {
if (ext == null) ext = MakeExt();
return ext;
}
#endif
// Different clients support varying types of blocks
if (p.hasBulkBlockUpdate && p.hasCustomBlocks && p.hasBlockDefs && count >= 160) {
if (bulk == null) bulk = MakeBulk();
@ -107,6 +114,28 @@ namespace MCGalaxy.Network {
return original;
}
}
#if TEN_BIT_BLOCKS
byte[] MakeExt() {
byte[] data = new byte[count * 9];
for (int i = 0, j = 0; i < count; i++) {
int index = indices[i];
int x = (index % level.Width);
int y = (index / level.Width) / level.Length;
int z = (index / level.Width) % level.Length;
data[j++] = Opcode.SetBlock;
data[j++] = (byte)(x >> 8); data[j++] = (byte)x;
data[j++] = (byte)(y >> 8); data[j++] = (byte)y;
data[j++] = (byte)(z >> 8); data[j++] = (byte)z;
BlockID block = Block.ToRaw(blocks[i]);
data[j++] = (byte)(block >> 8);
data[j++] = (byte)block;
}
return data;
}
#endif
byte[] MakeBulk() {
byte[] data = new byte[2 + 256 * 5];

View File

@ -133,6 +133,33 @@ namespace MCGalaxy.Network {
dst.length = blocks.Length;
// compress the map data in 64 kb chunks
#if TEN_BIT_BLOCKS
if (p.hasExtBlocks) {
for (int i = 0; i < blocks.Length; ++i) {
byte block = blocks[i];
if (block == Block.custom_block) {
buffer[bIndex] = lvl.GetExtTile(i);
buffer[bIndex + 1] = 0;
} else if (block == Block.custom_block_2) {
buffer[bIndex] = lvl.GetExtTile(i);
buffer[bIndex + 1] = 1;
} else if (block == Block.custom_block_3) {
buffer[bIndex] = lvl.GetExtTile(i);
buffer[bIndex + 1] = 2;
} else {
buffer[bIndex] = conv[block];
buffer[bIndex + 1] = 0;
}
bIndex += 2;
if (bIndex == bufferSize) {
dst.position = i;
gs.Write(buffer, 0, bufferSize); bIndex = 0;
}
}
} else
#endif
if (p.hasBlockDefs) {
for (int i = 0; i < blocks.Length; ++i) {
byte block = blocks[i];

View File

@ -93,7 +93,12 @@ namespace MCGalaxy {
hasTwoWayPing = true;
} else if (ext.ExtName == CpeExt.BulkBlockUpdate) {
hasBulkBlockUpdate = true;
}
#if TEN_BIT_BLOCKS
else if (ext.ExtName == CpeExt.ExtBlocks) {
hasExtBlocks = true;
}
#endif
}
/// <summary> Returns whether this player's client supports the given CPE extension. </summary>
@ -148,13 +153,9 @@ namespace MCGalaxy {
}
public void SendCurrentBlockPermissions() {
if (!Supports(CpeExt.BlockPermissions)) return;
if (!Supports(CpeExt.BlockPermissions)) return;
// Write the block permissions as one bulk TCP packet
int count = NumBlockPermissions();
byte[] bulk = new byte[count * (hasExtBlocks ? 5 : 4)];
WriteBlockPermissions(bulk);
Send(bulk);
SendAllBlockPermissions();
}
int NumBlockPermissions() {
@ -163,14 +164,18 @@ namespace MCGalaxy {
return hasCustomBlocks ? Block.CpeCount : Block.OriginalCount;
}
void WriteBlockPermissions(byte[] bulk) {
void SendAllBlockPermissions() {
int count = NumBlockPermissions();
int size = hasExtBlocks ? 5 : 4;
byte[] bulk = new byte[count * size];
for (int i = 0; i < count; i++) {
BlockID block = Block.FromRaw((byte)i);
BlockID block = Block.FromRaw((BlockID)i);
bool place = BlockPerms.UsableBy(this, block) && level.CanPlace;
bool delete = BlockPerms.UsableBy(this, block) && level.CanDelete;
Packet.WriteBlockPermission((byte)i, place, delete, hasExtBlocks, bulk, i * 4);
Packet.WriteBlockPermission((BlockID)i, place, delete, hasExtBlocks, bulk, i * size);
}
Send(bulk);
}
}