Submarine position syncing

This commit is contained in:
Regalis
2016-10-25 20:50:47 +03:00
parent 62dd055e8d
commit df9f89e383
5 changed files with 25 additions and 11 deletions
+4 -1
View File
@@ -1111,6 +1111,10 @@ namespace Barotrauma
public void ServerWrite(NetOutgoingMessage msg, Client c) 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.X);
msg.Write(PhysicsBody.SimPosition.Y); msg.Write(PhysicsBody.SimPosition.Y);
} }
@@ -1121,7 +1125,6 @@ namespace Barotrauma
msg.ReadFloat(), msg.ReadFloat(),
msg.ReadFloat()); msg.ReadFloat());
//already interpolating with more up-to-date data -> ignore //already interpolating with more up-to-date data -> ignore
if (subBody.MemPos.Count > 1 && subBody.MemPos[0].Timestamp > sendingTime) if (subBody.MemPos.Count > 1 && subBody.MemPos[0].Timestamp > sendingTime)
{ {
+5 -5
View File
@@ -121,7 +121,7 @@ namespace Barotrauma.Networking
config.SimulatedLoss = 0.05f; config.SimulatedLoss = 0.05f;
config.SimulatedDuplicatesChance = 0.05f; config.SimulatedDuplicatesChance = 0.05f;
config.SimulatedMinimumLatency = 0.1f; config.SimulatedMinimumLatency = 0.1f;
config.SimulatedRandomLatency = 0.2f; config.SimulatedRandomLatency = 0.05f;
#endif #endif
config.DisableMessageType(NetIncomingMessageType.DebugMessage | NetIncomingMessageType.WarningMessage | NetIncomingMessageType.Receipt config.DisableMessageType(NetIncomingMessageType.DebugMessage | NetIncomingMessageType.WarningMessage | NetIncomingMessageType.Receipt
@@ -706,19 +706,19 @@ namespace Barotrauma.Networking
case ServerNetObject.SYNC_IDS: case ServerNetObject.SYNC_IDS:
lastSentChatMsgID = inc.ReadUInt32(); lastSentChatMsgID = inc.ReadUInt32();
break; break;
case ServerNetObject.CHARACTER_POSITION: case ServerNetObject.ENTITY_POSITION:
UInt16 id = inc.ReadUInt16(); UInt16 id = inc.ReadUInt16();
byte msgLength = inc.ReadByte(); byte msgLength = inc.ReadByte();
var character = Entity.FindEntityByID(id) as Character; var entity = Entity.FindEntityByID(id) as IServerSerializable;
if (character == null) if (entity == null)
{ {
//skip through the rest of the message //skip through the rest of the message
inc.Position += msgLength * 8; inc.Position += msgLength * 8;
} }
else else
{ {
character.ClientRead(inc, sendingTime); entity.ClientRead(inc, sendingTime);
} }
inc.ReadPadBits(); inc.ReadPadBits();
+13 -2
View File
@@ -69,7 +69,7 @@ namespace Barotrauma.Networking
#if DEBUG #if DEBUG
config.SimulatedLoss = 0.05f; config.SimulatedLoss = 0.05f;
config.SimulatedRandomLatency = 0.2f; config.SimulatedRandomLatency = 0.05f;
config.SimulatedDuplicatesChance = 0.05f; config.SimulatedDuplicatesChance = 0.05f;
config.SimulatedMinimumLatency = 0.1f; config.SimulatedMinimumLatency = 0.1f;
#endif #endif
@@ -664,11 +664,22 @@ namespace Barotrauma.Networking
{ {
if (character is AICharacter) continue; if (character is AICharacter) continue;
outmsg.Write((byte)ServerNetObject.CHARACTER_POSITION); outmsg.Write((byte)ServerNetObject.ENTITY_POSITION);
character.ServerWrite(outmsg, c); character.ServerWrite(outmsg, c);
outmsg.WritePadBits(); 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); outmsg.Write((byte)ServerNetObject.END_OF_MESSAGE);
server.SendMessage(outmsg, c.Connection, NetDeliveryMethod.Unreliable); server.SendMessage(outmsg, c.Connection, NetDeliveryMethod.Unreliable);
} }
@@ -44,7 +44,7 @@ namespace Barotrauma.Networking
SYNC_IDS, SYNC_IDS,
CHAT_MESSAGE, CHAT_MESSAGE,
VOTE, VOTE,
CHARACTER_POSITION, ENTITY_POSITION,
ITEM_STATE, ITEM_STATE,
ENTITY_SPAWN, ENTITY_SPAWN,
+1 -1
View File
@@ -503,7 +503,7 @@ namespace Barotrauma
{ {
//if there are more than 2 positions in the buffer, //if there are more than 2 positions in the buffer,
//increase the interpolation speed to catch up with the server //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); netInterpolationState += (deltaTime * speedMultiplier) / (next.Timestamp - prev.Timestamp);
newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState); newPosition = Vector2.Lerp(prev.Position, next.Position, netInterpolationState);