From df9f89e383c1f92995897b59fa6d7b7f4c12287c Mon Sep 17 00:00:00 2001 From: Regalis Date: Tue, 25 Oct 2016 20:50:47 +0300 Subject: [PATCH] Submarine position syncing --- Subsurface/Source/Map/Submarine.cs | 7 +++++-- Subsurface/Source/Networking/GameClient.cs | 10 +++++----- Subsurface/Source/Networking/GameServer.cs | 15 +++++++++++++-- Subsurface/Source/Networking/NetworkMember.cs | 2 +- Subsurface/Source/Physics/PhysicsBody.cs | 2 +- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index 89b5e8c0f..428d46649 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -1111,6 +1111,10 @@ namespace Barotrauma public void ServerWrite(NetOutgoingMessage msg, Client c) { + msg.Write(ID); + //length in bytes + msg.Write((byte)(4 + 4)); + msg.Write(PhysicsBody.SimPosition.X); msg.Write(PhysicsBody.SimPosition.Y); } @@ -1119,8 +1123,7 @@ namespace Barotrauma { var newTargetPosition = new Vector2( msg.ReadFloat(), - msg.ReadFloat()); - + msg.ReadFloat()); //already interpolating with more up-to-date data -> ignore if (subBody.MemPos.Count > 1 && subBody.MemPos[0].Timestamp > sendingTime) diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 211b5d86e..d86ed7923 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -121,7 +121,7 @@ namespace Barotrauma.Networking config.SimulatedLoss = 0.05f; config.SimulatedDuplicatesChance = 0.05f; config.SimulatedMinimumLatency = 0.1f; - config.SimulatedRandomLatency = 0.2f; + config.SimulatedRandomLatency = 0.05f; #endif config.DisableMessageType(NetIncomingMessageType.DebugMessage | NetIncomingMessageType.WarningMessage | NetIncomingMessageType.Receipt @@ -706,19 +706,19 @@ namespace Barotrauma.Networking case ServerNetObject.SYNC_IDS: lastSentChatMsgID = inc.ReadUInt32(); break; - case ServerNetObject.CHARACTER_POSITION: + case ServerNetObject.ENTITY_POSITION: UInt16 id = inc.ReadUInt16(); byte msgLength = inc.ReadByte(); - var character = Entity.FindEntityByID(id) as Character; - if (character == null) + var entity = Entity.FindEntityByID(id) as IServerSerializable; + if (entity == null) { //skip through the rest of the message inc.Position += msgLength * 8; } else { - character.ClientRead(inc, sendingTime); + entity.ClientRead(inc, sendingTime); } inc.ReadPadBits(); diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 323cde979..b7e002dad 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -69,7 +69,7 @@ namespace Barotrauma.Networking #if DEBUG config.SimulatedLoss = 0.05f; - config.SimulatedRandomLatency = 0.2f; + config.SimulatedRandomLatency = 0.05f; config.SimulatedDuplicatesChance = 0.05f; config.SimulatedMinimumLatency = 0.1f; #endif @@ -664,11 +664,22 @@ namespace Barotrauma.Networking { if (character is AICharacter) continue; - outmsg.Write((byte)ServerNetObject.CHARACTER_POSITION); + outmsg.Write((byte)ServerNetObject.ENTITY_POSITION); character.ServerWrite(outmsg, c); outmsg.WritePadBits(); } + foreach (Submarine sub in Submarine.Loaded) + { + //if docked to a sub with a smaller ID, don't send an update + // (= update is only sent for the docked sub that has the smallest ID, doesn't matter if it's the main sub or a shuttle) + if (sub.DockedTo.Any(s => s.ID < sub.ID)) continue; + + outmsg.Write((byte)ServerNetObject.ENTITY_POSITION); + sub.ServerWrite(outmsg, c); + outmsg.WritePadBits(); + } + outmsg.Write((byte)ServerNetObject.END_OF_MESSAGE); server.SendMessage(outmsg, c.Connection, NetDeliveryMethod.Unreliable); } diff --git a/Subsurface/Source/Networking/NetworkMember.cs b/Subsurface/Source/Networking/NetworkMember.cs index 9285927d0..98f81a12c 100644 --- a/Subsurface/Source/Networking/NetworkMember.cs +++ b/Subsurface/Source/Networking/NetworkMember.cs @@ -44,7 +44,7 @@ namespace Barotrauma.Networking SYNC_IDS, CHAT_MESSAGE, VOTE, - CHARACTER_POSITION, + ENTITY_POSITION, ITEM_STATE, ENTITY_SPAWN, diff --git a/Subsurface/Source/Physics/PhysicsBody.cs b/Subsurface/Source/Physics/PhysicsBody.cs index 5e4060224..4b03ab976 100644 --- a/Subsurface/Source/Physics/PhysicsBody.cs +++ b/Subsurface/Source/Physics/PhysicsBody.cs @@ -503,7 +503,7 @@ namespace Barotrauma { //if there are more than 2 positions in the buffer, //increase the interpolation speed to catch up with the server - float speedMultiplier = 1.0f + (float)Math.Pow((positionBuffer.Count - 2) / 2.0f, 2.0f); + float speedMultiplier = 0.9f + (float)Math.Pow((positionBuffer.Count - 2) / 5.0f, 2.0f); netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp); newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);