diff --git a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp index b0f6298fc..3fde6d6ab 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacket.cpp @@ -4,7 +4,7 @@ #include "PacketListener.h" #include "MoveEntityPacket.h" -MoveEntityPacket::MoveEntityPacket() +MoveEntityPacket::MoveEntityPacket() { hasRot = false; @@ -28,7 +28,7 @@ MoveEntityPacket::MoveEntityPacket(int id) xRot = 0; } -void MoveEntityPacket::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::read(DataInputStream *dis) //throws IOException { id = dis->readShort(); } @@ -48,7 +48,7 @@ void MoveEntityPacket::handle(PacketListener *listener) listener->handleMoveEntity(shared_from_this()); } -int MoveEntityPacket::getEstimatedSize() +int MoveEntityPacket::getEstimatedSize() { return 2; } @@ -71,40 +71,40 @@ MoveEntityPacket::PosRot::PosRot() MoveEntityPacket::PosRot::PosRot(int id, char xa, char ya, char za, char yRot, char xRot) : MoveEntityPacket( id ) { - this->xa = (int)(signed char)xa; - this->ya = (int)(signed char)ya; - this->za = (int)(signed char)za; - this->yRot = (int)(signed char)yRot; - this->xRot = (int)(signed char)xRot; + this->xa = xa; + this->ya = ya; + this->za = za; + this->yRot = yRot; + this->xRot = xRot; hasRot = true; } -void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::PosRot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int)(signed char)dis->readByte(); - ya = (int)(signed char)dis->readByte(); - za = (int)(signed char)dis->readByte(); - yRot = (int)(signed char)dis->readByte(); - xRot = (int)(signed char)dis->readByte(); + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); + yRot = (int)dis->readByte(); + xRot = (int)dis->readByte(); } -void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacket::PosRot::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + dos->writeByte((uint8_t)xa); + dos->writeByte((uint8_t)ya); + dos->writeByte((uint8_t)za); + dos->writeByte((uint8_t)yRot); + dos->writeByte((uint8_t)xRot); } -int MoveEntityPacket::PosRot::getEstimatedSize() +int MoveEntityPacket::PosRot::getEstimatedSize() { return 2+5; } -MoveEntityPacket::Pos::Pos() +MoveEntityPacket::Pos::Pos() { } @@ -115,20 +115,20 @@ MoveEntityPacket::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityPacket this->za = za; } -void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::Pos::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - xa = (int)(signed char)dis->readByte(); - ya = (int)(signed char)dis->readByte(); - za = (int)(signed char)dis->readByte(); + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); } void MoveEntityPacket::Pos::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); + dos->writeByte((uint8_t)xa); + dos->writeByte((uint8_t)ya); + dos->writeByte((uint8_t)za); } int MoveEntityPacket::Pos::getEstimatedSize() @@ -136,7 +136,7 @@ int MoveEntityPacket::Pos::getEstimatedSize() return 2+3; } -MoveEntityPacket::Rot::Rot() +MoveEntityPacket::Rot::Rot() { hasRot = true; } @@ -148,18 +148,18 @@ MoveEntityPacket::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket(id) hasRot = true; } -void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacket::Rot::read(DataInputStream *dis) //throws IOException { MoveEntityPacket::read(dis); - yRot = (int)(signed char)dis->readByte(); - xRot = (int)(signed char)dis->readByte(); + yRot = (int)dis->readByte(); + xRot = (int)dis->readByte(); } -void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacket::Rot::write(DataOutputStream *dos) //throws IOException { MoveEntityPacket::write(dos); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + dos->writeByte((uint8_t)yRot); + dos->writeByte((uint8_t)xRot); } int MoveEntityPacket::Rot::getEstimatedSize() diff --git a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp index 1c698f720..23cf4af51 100644 --- a/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp +++ b/Minecraft.World/Network/Packets/MoveEntityPacketSmall.cpp @@ -19,19 +19,30 @@ MoveEntityPacketSmall::MoveEntityPacketSmall() MoveEntityPacketSmall::MoveEntityPacketSmall(int id) { + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + this->id = id; hasRot = false; - xa = ya = za = yRot = xRot = 0; + + xa = 0; + ya = 0; + za = 0; + yRot = 0; + xRot = 0; } -void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::read(DataInputStream *dis) //throws IOException { id = dis->readShort(); } void MoveEntityPacketSmall::write(DataOutputStream *dos) //throws IOException { - if(id < 0 || id > 32767 ) + if( (id < 0 ) || (id >= 2048 ) ) { // We shouln't be tracking an entity that doesn't have a short type of id __debugbreak(); @@ -44,7 +55,7 @@ void MoveEntityPacketSmall::handle(PacketListener *listener) listener->handleMoveEntitySmall(shared_from_this()); } -int MoveEntityPacketSmall::getEstimatedSize() +int MoveEntityPacketSmall::getEstimatedSize() { return 2; } @@ -75,32 +86,36 @@ MoveEntityPacketSmall::PosRot::PosRot(int id, char xa, char ya, char za, char yR hasRot = true; } -void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::PosRot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (signed char)dis->readByte(); - ya = (signed char)dis->readByte(); - za = (signed char)dis->readByte(); - yRot = (signed char)dis->readByte(); - xRot = (signed char)dis->readByte(); + int idAndRot = dis->readShort(); + this->id = idAndRot & 0x07ff; + this->yRot = idAndRot >> 11; + int xAndYAndZ = (int)dis->readShort(); + this->xa = xAndYAndZ >> 11; + this->ya = (xAndYAndZ << 21 ) >> 26; + this->za = (xAndYAndZ << 27 ) >> 27; } -void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacketSmall::PosRot::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); - dos->writeByte((uint8_t)(yRot & 0xFF)); - dos->writeByte((uint8_t)(xRot & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndRot = id | yRot << 11; + dos->writeShort(idAndRot); + short xAndYAndZ = ( xa << 11 ) | ( ( ya & 0x3f ) << 5 ) | ( za & 0x1f ); + dos->writeShort(xAndYAndZ); } -int MoveEntityPacketSmall::PosRot::getEstimatedSize() +int MoveEntityPacketSmall::PosRot::getEstimatedSize() { - return 2+5; + return 4; } -MoveEntityPacketSmall::Pos::Pos() +MoveEntityPacketSmall::Pos::Pos() { } @@ -111,28 +126,35 @@ MoveEntityPacketSmall::Pos::Pos(int id, char xa, char ya, char za) : MoveEntityP this->za = za; } -void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::Pos::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - xa = (signed char)dis->readByte(); - ya = (signed char)dis->readByte(); - za = (signed char)dis->readByte(); + int idAndY = dis->readShort(); + this->id = idAndY & 0x07ff; + this->ya = idAndY >> 11; + int XandZ = (int)((signed char)(dis->readByte())); + xa = XandZ >> 4; + za = ( XandZ << 28 ) >> 28; } void MoveEntityPacketSmall::Pos::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(xa & 0xFF)); - dos->writeByte((uint8_t)(ya & 0xFF)); - dos->writeByte((uint8_t)(za & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndY = id | ya << 11; + dos->writeShort(idAndY); + char XandZ = ( xa << 4 ) | ( za & 0x0f ); + dos->writeByte((uint8_t)XandZ); } int MoveEntityPacketSmall::Pos::getEstimatedSize() { - return 2+3; + return 3; } -MoveEntityPacketSmall::Rot::Rot() +MoveEntityPacketSmall::Rot::Rot() { hasRot = true; } @@ -145,19 +167,25 @@ MoveEntityPacketSmall::Rot::Rot(int id, char yRot, char xRot) : MoveEntityPacket hasRot = true; } -void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException +void MoveEntityPacketSmall::Rot::read(DataInputStream *dis) //throws IOException { - MoveEntityPacketSmall::read(dis); - yRot = (signed char)dis->readByte(); + int idAndRot = (int)dis->readShort(); + this->id = idAndRot & 0x07ff; + this->yRot = idAndRot >> 11; } -void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException +void MoveEntityPacketSmall::Rot::write(DataOutputStream *dos) //throws IOException { - MoveEntityPacketSmall::write(dos); - dos->writeByte((uint8_t)(yRot & 0xFF)); + if( (id < 0 ) || (id >= 2048 ) ) + { + // We shouln't be tracking an entity that doesn't have a short type of id + __debugbreak(); + } + short idAndRot = id | yRot << 11; + dos->writeShort(idAndRot); } int MoveEntityPacketSmall::Rot::getEstimatedSize() { - return 2+1; + return 2; } diff --git a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp index 70ea64f92..cb6d2a42c 100644 --- a/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp +++ b/Minecraft.World/Network/Packets/SetEntityMotionPacket.cpp @@ -20,11 +20,19 @@ void SetEntityMotionPacket::_init(int id, double xd, double yd, double zd) xa = (int) (xd * 8000.0); ya = (int) (yd * 8000.0); za = (int) (zd * 8000.0); - - useBytes = false; + // 4J - if we could transmit this as bytes (in 1/16 accuracy) then flag to do so + if( ( xa >= (-128 * 16 ) ) && ( ya >= (-128 * 16 ) ) && ( za >= (-128 * 16 ) ) && + ( xa < (128 * 16 ) ) && ( ya < (128 * 16 ) ) && ( za < (128 * 16 ) ) ) + { + useBytes = true; + } + else + { + useBytes = false; + } } -SetEntityMotionPacket::SetEntityMotionPacket() +SetEntityMotionPacket::SetEntityMotionPacket() { _init(0, 0.0f, 0.0f, 0.0f); } @@ -36,27 +44,53 @@ SetEntityMotionPacket::SetEntityMotionPacket(std::shared_ptr e) SetEntityMotionPacket::SetEntityMotionPacket(int id, double xd, double yd, double zd) { - _init(id, xd, yd, zd); + _init(id, xd, yd, zd); } -void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException +void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException { - id = dis->readShort(); - - xa = dis->readShort(); - ya = dis->readShort(); - za = dis->readShort(); - - useBytes = false; + short idAndFlag = dis->readShort(); + id = idAndFlag & 0x07ff; + if( idAndFlag & 0x0800 ) + { + xa = (int)dis->readByte(); + ya = (int)dis->readByte(); + za = (int)dis->readByte(); + xa = ( xa << 24 ) >> 24; + ya = ( ya << 24 ) >> 24; + za = ( za << 24 ) >> 24; + xa *= 16; + ya *= 16; + za *= 16; + useBytes = true; + } + else + { + xa = dis->readShort(); + ya = dis->readShort(); + za = dis->readShort(); + useBytes = false; + } } void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException { - dos->writeShort(id); - - dos->writeShort(xa); - dos->writeShort(ya); - dos->writeShort(za); + if( useBytes ) + { + // Masking the id to 11 bits before writing to account for large entitty ids. + dos->writeShort((id & 0x07FF) | 0x800); + dos->writeByte(xa/16); + dos->writeByte(ya/16); + dos->writeByte(za/16); + } + else + { + // same thing as line 80 here + dos->writeShort((id & 0x07FF)); + dos->writeShort(xa); + dos->writeShort(ya); + dos->writeShort(za); + } } void SetEntityMotionPacket::handle(PacketListener *listener) @@ -66,7 +100,7 @@ void SetEntityMotionPacket::handle(PacketListener *listener) int SetEntityMotionPacket::getEstimatedSize() { - return 8; + return useBytes ? 5 : 8; } bool SetEntityMotionPacket::canBeInvalidated() diff --git a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp index 3a0c9927f..fcd88af71 100644 --- a/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp +++ b/Minecraft.World/Network/Packets/TeleportEntityPacket.cpp @@ -55,10 +55,16 @@ void TeleportEntityPacket::read(DataInputStream *dis) //throws IOException void TeleportEntityPacket::write(DataOutputStream *dos) //throws IOException { - dos->writeShort((short)id); + dos->writeShort(id); +#ifdef _LARGE_WORLDS dos->writeInt(x); dos->writeInt(y); dos->writeInt(z); +#else + dos->writeShort(x); + dos->writeShort(y); + dos->writeShort(z); +#endif dos->write(yRot); dos->write(xRot); }