From 2766924ed7355167e3b660d184b5f7707f75e76a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 2 Sep 2017 22:36:26 +1000 Subject: [PATCH] Combine bot movement packets --- MCGalaxy/Bots/PlayerBot.cs | 42 ++++++++++++++++++---------- MCGalaxy/Entity/Entities.cs | 4 +-- MCGalaxy/Network/Socket/TcpSocket.cs | 2 +- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index 950861f82..7e0a09730 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -160,28 +160,42 @@ namespace MCGalaxy { public static void GlobalUpdatePosition() { Level[] levels = LevelInfo.Loaded.Items; for (int i = 0; i < levels.Length; i++) { - PlayerBot[] bots = levels[i].Bots.Items; - for (int j = 0; j < bots.Length; j++) { bots[j].UpdatePosition(); } + UpdatePositions(levels[i]); } } - void UpdatePosition() { - if (movement) PerformMovement(); + unsafe static void UpdatePositions(Level lvl) { + byte* src = stackalloc byte[16 * 256]; // 16 = size of absolute update, with extended positions + byte* ext = stackalloc byte[16 * 256]; + byte* ptrSrc = src, ptrExt = ext; - Position pos = Pos; Orientation rot = Rot; - if (pos == lastPos && rot.HeadX == lastRot.HeadX && rot.RotY == lastRot.RotY) return; - lastPos = pos; lastRot = rot; + PlayerBot[] bots = lvl.Bots.Items; + for (int i = 0; i < bots.Length; i++) { + PlayerBot bot = bots[i]; + if (bot.movement) bot.PerformMovement(); + Position pos = bot.Pos; Orientation rot = bot.Rot; + + Entities.GetPositionPacket(ref ptrSrc, bot.id, true, false, + pos, bot.lastPos, rot, bot.lastRot); + Entities.GetPositionPacket(ref ptrExt, bot.id, true, true, + pos, bot.lastPos, rot, bot.lastRot); + bot.lastPos = pos; bot.lastRot = rot; + } - // TODO: relative position updates, combine packets - byte[] packet = Packet.Teleport(id, pos, rot, false); - byte[] extPacket = Packet.Teleport(id, pos, rot, true); + int countSrc = (int)(ptrSrc - src); + if (countSrc == 0) return; + int countExt = (int)(ptrExt - ext); + + byte[] srcPacket = new byte[countSrc]; + byte[] extPacket = new byte[countExt]; + for (int i = 0; i < srcPacket.Length; i++) { srcPacket[i] = src[i]; } + for (int i = 0; i < extPacket.Length; i++) { extPacket[i] = ext[i]; } Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - if (p.level != level) continue; - - if (p.hasExtPositions) p.Send(extPacket); - else p.Send(packet); + if (p.level != lvl) continue; + byte[] packet = p.hasExtPositions ? extPacket : srcPacket; + p.Send(packet); } } diff --git a/MCGalaxy/Entity/Entities.cs b/MCGalaxy/Entity/Entities.cs index 222068473..70b835e0c 100644 --- a/MCGalaxy/Entity/Entities.cs +++ b/MCGalaxy/Entity/Entities.cs @@ -319,9 +319,9 @@ namespace MCGalaxy { int count = (int)(ptr - src); if (count == 0) return; + byte[] packet = new byte[count]; - for (int i = 0; i < packet.Length; i++) - packet[i] = src[i]; + for (int i = 0; i < packet.Length; i++) { packet[i] = src[i]; } p.Send(packet); } diff --git a/MCGalaxy/Network/Socket/TcpSocket.cs b/MCGalaxy/Network/Socket/TcpSocket.cs index 0ec0c86b7..f3830ed32 100644 --- a/MCGalaxy/Network/Socket/TcpSocket.cs +++ b/MCGalaxy/Network/Socket/TcpSocket.cs @@ -29,7 +29,7 @@ namespace MCGalaxy.Network { int unprocessedLen; readonly SocketAsyncEventArgs recvArgs = new SocketAsyncEventArgs(); - byte[] sendBuffer = new byte[2048]; + byte[] sendBuffer = new byte[4096]; readonly object sendLock = new object(); readonly Queue sendQueue = new Queue(64); volatile bool sendInProgress;