Sending multiple networkevents in one packet, removed limb updates from ImportantEntityUpdate

This commit is contained in:
Regalis
2015-11-04 00:12:53 +02:00
parent 963f46e7d1
commit 9f9f0205e7
6 changed files with 138 additions and 61 deletions
+34 -34
View File
@@ -1222,21 +1222,21 @@ namespace Barotrauma
return inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data); return inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
case NetworkEventType.ImportantEntityUpdate: case NetworkEventType.ImportantEntityUpdate:
int i = 0; //int i = 0;
foreach (Limb limb in AnimController.Limbs) //foreach (Limb limb in AnimController.Limbs)
{ //{
if (limb.SimPosition.Length() > NetConfig.CharacterIgnoreDistance) return false; // if (limb.SimPosition.Length() > NetConfig.CharacterIgnoreDistance) return false;
message.WriteRangedSingle(limb.body.SimPosition.X, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); // message.WriteRangedSingle(limb.body.SimPosition.X, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
message.WriteRangedSingle(limb.body.SimPosition.Y, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); // message.WriteRangedSingle(limb.body.SimPosition.Y, -NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
//message.Write(limb.body.LinearVelocity.X); // //message.Write(limb.body.LinearVelocity.X);
//message.Write(limb.body.LinearVelocity.Y); // //message.Write(limb.body.LinearVelocity.Y);
message.Write(limb.body.Rotation); // message.Write(limb.body.Rotation);
//message.WriteRangedSingle(MathHelper.Clamp(limb.body.AngularVelocity, -10.0f, 10.0f), -10.0f, 10.0f, 8); // //message.WriteRangedSingle(MathHelper.Clamp(limb.body.AngularVelocity, -10.0f, 10.0f), -10.0f, 10.0f, 8);
i++; // i++;
} //}
message.WriteRangedSingle(MathHelper.Clamp(AnimController.StunTimer,0.0f,60.0f), 0.0f, 60.0f, 8); message.WriteRangedSingle(MathHelper.Clamp(AnimController.StunTimer,0.0f,60.0f), 0.0f, 60.0f, 8);
message.Write((byte)((health/maxHealth)*255.0f)); message.Write((byte)((health/maxHealth)*255.0f));
@@ -1366,32 +1366,32 @@ namespace Barotrauma
inventory.ReadNetworkData(NetworkEventType.InventoryUpdate, message); inventory.ReadNetworkData(NetworkEventType.InventoryUpdate, message);
return; return;
case NetworkEventType.ImportantEntityUpdate: case NetworkEventType.ImportantEntityUpdate:
foreach (Limb limb in AnimController.Limbs) //foreach (Limb limb in AnimController.Limbs)
{ //{
Vector2 limbPos = limb.SimPosition, vel = Vector2.Zero; // Vector2 limbPos = limb.SimPosition, vel = Vector2.Zero;
float rotation = limb.Rotation; // float rotation = limb.Rotation;
try // try
{ // {
limbPos.X = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); // limbPos.X = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
limbPos.Y = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16); // limbPos.Y = message.ReadRangedSingle(-NetConfig.CharacterIgnoreDistance, NetConfig.CharacterIgnoreDistance, 16);
rotation = message.ReadFloat(); // rotation = message.ReadFloat();
} // }
catch // catch
{ // {
return; // return;
} // }
if (limb.body != null) // if (limb.body != null)
{ // {
limb.body.TargetVelocity = limb.body.LinearVelocity; // limb.body.TargetVelocity = limb.body.LinearVelocity;
limb.body.TargetPosition = limbPos;// +vel * (float)(deltaTime / 60.0); // limb.body.TargetPosition = limbPos;// +vel * (float)(deltaTime / 60.0);
limb.body.TargetRotation = rotation;// +angularVel * (float)(deltaTime / 60.0); // limb.body.TargetRotation = rotation;// +angularVel * (float)(deltaTime / 60.0);
limb.body.TargetAngularVelocity = limb.body.AngularVelocity; // limb.body.TargetAngularVelocity = limb.body.AngularVelocity;
} // }
} //}
float newStunTimer = 0.0f, newHealth = 0.0f, newOxygen = 0.0f; float newStunTimer = 0.0f, newHealth = 0.0f, newOxygen = 0.0f;
+12 -17
View File
@@ -549,7 +549,7 @@ namespace Barotrauma
{ {
if (!character.Enabled) return; if (!character.Enabled) return;
UpdateNetplayerPosition(); UpdateNetPlayerPosition();
Vector2 flowForce = Vector2.Zero; Vector2 flowForce = Vector2.Zero;
@@ -654,7 +654,7 @@ namespace Barotrauma
} }
private void UpdateNetplayerPosition() private void UpdateNetPlayerPosition()
{ {
if (refLimb.body.TargetPosition == Vector2.Zero) if (refLimb.body.TargetPosition == Vector2.Zero)
{ {
@@ -662,18 +662,13 @@ namespace Barotrauma
return; return;
} }
//if the limb is further away than resetdistance, all limbs are immediately snapped to their targetpositions
float resetDistance = NetConfig.ResetRagdollDistance;
//if the limb is closer than alloweddistance, just ignore the difference //if the limb is closer than alloweddistance, just ignore the difference
float allowedDistance = NetConfig.AllowedRagdollDistance * ((inWater) ? 2.0f : 1.0f); float allowedDistance = NetConfig.AllowedRagdollDistance * ((inWater) ? 2.0f : 1.0f);
float dist = Vector2.Distance(refLimb.body.SimPosition, refLimb.body.TargetPosition); float dist = Vector2.Distance(refLimb.body.SimPosition, refLimb.body.TargetPosition);
bool resetAll = dist > resetDistance;
if (resetAll) //if the limb is further away than resetdistance, all limbs are immediately snapped to their targetpositions
{ bool resetAll = dist > NetConfig.ResetRagdollDistance;
if (Limbs.FirstOrDefault(limb => !limb.ignoreCollisions && limb.body.TargetPosition == Vector2.Zero) != null) resetAll = false;
}
Vector2 diff = (refLimb.body.TargetPosition - refLimb.body.SimPosition); Vector2 diff = (refLimb.body.TargetPosition - refLimb.body.SimPosition);
@@ -717,16 +712,16 @@ namespace Barotrauma
foreach (Limb limb in Limbs) foreach (Limb limb in Limbs)
{ {
if (limb.body.TargetPosition == Vector2.Zero) //if (limb.body.TargetPosition == Vector2.Zero)
{ //{
limb.body.SetTransform(limb.body.SimPosition + diff, limb.body.Rotation); limb.body.SetTransform(limb.body.SimPosition + diff, limb.body.Rotation);
continue; //continue;
} //}
limb.body.LinearVelocity = limb.body.TargetVelocity; //limb.body.LinearVelocity = limb.body.TargetVelocity;
limb.body.AngularVelocity = limb.body.TargetAngularVelocity; //limb.body.AngularVelocity = limb.body.TargetAngularVelocity;
limb.body.SetTransform(limb.body.TargetPosition, limb.body.TargetRotation); //limb.body.SetTransform(limb.body.TargetPosition, limb.body.TargetRotation);
limb.body.TargetPosition = Vector2.Zero; limb.body.TargetPosition = Vector2.Zero;
} }
} }
+28 -1
View File
@@ -474,7 +474,34 @@ namespace Barotrauma.Networking
case (byte)PacketTypes.NetworkEvent: case (byte)PacketTypes.NetworkEvent:
//read the data from the message and update client state accordingly //read the data from the message and update client state accordingly
if (!gameStarted) break; if (!gameStarted) break;
NetworkEvent.ReadData(inc);
byte msgCount = inc.ReadByte();
long currPos = inc.PositionInBytes;
System.Diagnostics.Debug.WriteLine("msgcount: " + msgCount + " startpos: " + inc.PositionInBytes);
for (int i = 0; i < msgCount; i++ )
{
byte msgLength = inc.ReadByte();
System.Diagnostics.Debug.WriteLine("msglength: "+msgLength);
try
{
NetworkEvent.ReadData(inc);
}
catch
{
int afghj = 1;
}
//+1 because msgLength is one additional byte
currPos += msgLength+1;
inc.Position = currPos*8;
System.Diagnostics.Debug.WriteLine("currpos: " + currPos);
}
break; break;
case (byte)PacketTypes.UpdateNetLobby: case (byte)PacketTypes.UpdateNetLobby:
if (gameStarted) continue; if (gameStarted) continue;
+61 -7
View File
@@ -636,20 +636,40 @@ namespace Barotrauma.Networking
if (recipients.Count == 0) return; if (recipients.Count == 0) return;
foreach (NetworkEvent networkEvent in NetworkEvent.events) for (int i = 0; i<2; i++)
{ {
bool important = i==0;
var unreliableEvents = NetworkEvent.events.FindAll(e => e.IsImportant == important);
if (unreliableEvents.Count == 0) continue;
NetOutgoingMessage message = server.CreateMessage(); NetOutgoingMessage message = server.CreateMessage();
message.Write((byte)PacketTypes.NetworkEvent); message.Write((byte)PacketTypes.NetworkEvent);
//if (!networkEvent.IsClient) continue;
if (!networkEvent.FillData(message))
List<byte[]> msgBytes = new List<byte[]>();
foreach (NetworkEvent unreliableEvent in unreliableEvents)
{ {
continue; NetOutgoingMessage tempMessage = server.CreateMessage();
if (!unreliableEvent.FillData(tempMessage)) continue;
tempMessage.WritePadBits();
tempMessage.Position = 0;
msgBytes.Add(tempMessage.ReadBytes(tempMessage.LengthBytes));
} }
//Entity e = Entity.FindEntityByID(networkEvent.ID); message.Write((byte)msgBytes.Count);
//if (e == null) continue; foreach (byte[] msgData in msgBytes)
if (networkEvent.IsImportant) {
if (msgData.Length > 255) DebugConsole.ThrowError("too large networkevent ("+msgData.Length+" bytes)");
message.Write((byte)msgData.Length);
message.Write(msgData);
}
if (important)
{ {
foreach (Client c in recipients) foreach (Client c in recipients)
{ {
@@ -668,6 +688,40 @@ namespace Barotrauma.Networking
} }
} }
} }
//foreach (NetworkEvent networkEvent in NetworkEvent.events)
//{
// if (!networkEvent.IsImportant) co
// //if (!networkEvent.IsClient) continue;
// if (!networkEvent.FillData(message))
// {
// continue;
// }
// //Entity e = Entity.FindEntityByID(networkEvent.ID);
// //if (e == null) continue;
// if (networkEvent.IsImportant)
// {
// foreach (Client c in recipients)
// {
// ReliableMessage reliableMessage = c.ReliableChannel.CreateMessage();
// message.Position = 0;
// reliableMessage.InnerMessage.Write(message.ReadBytes(message.LengthBytes));
// c.ReliableChannel.SendMessage(reliableMessage, c.Connection);
// }
// }
// else
// {
// if (server.ConnectionsCount>0)
// {
// server.SendMessage(message, recipientConnections, NetDeliveryMethod.Unreliable, 0);
// }
// }
//}
NetworkEvent.events.Clear(); NetworkEvent.events.Clear();
} }
@@ -168,6 +168,7 @@ namespace Barotrauma.Networking
} }
Entity e = Entity.FindEntityByID(id); Entity e = Entity.FindEntityByID(id);
System.Diagnostics.Debug.WriteLine(e.ToString());
if (e == null) if (e == null)
{ {
#if DEBUG #if DEBUG
Binary file not shown.